diff --git a/src/Octonica.ClickHouseClient.Tests/TypeTests.cs b/src/Octonica.ClickHouseClient.Tests/TypeTests.cs index 253209c..041819e 100644 --- a/src/Octonica.ClickHouseClient.Tests/TypeTests.cs +++ b/src/Octonica.ClickHouseClient.Tests/TypeTests.cs @@ -112,6 +112,19 @@ public async Task ReadStringWithEncoding() Assert.False(success); } + [Fact] + public async Task ReadStringWithEncodingScalar() + { + const string str = "АБВГДЕ"; + var encoding = Encoding.GetEncoding("windows-1251"); + + await using var connection = await OpenConnectionAsync(); + await using var cmd = connection.CreateCommand($"SELECT convertCharset('{str}', 'UTF8', 'windows-1251') AS c"); + + var strResult = await cmd.ExecuteScalarAsync(new ClickHouseColumnSettings(encoding)); + Assert.Equal(str, strResult); + } + [Fact] public async Task ReadGuidScalar() { diff --git a/src/Octonica.ClickHouseClient/ClickHouseCommand.cs b/src/Octonica.ClickHouseClient/ClickHouseCommand.cs index d87f638..621e7a0 100644 --- a/src/Octonica.ClickHouseClient/ClickHouseCommand.cs +++ b/src/Octonica.ClickHouseClient/ClickHouseCommand.cs @@ -193,41 +193,71 @@ private async ValueTask ExecuteNonQuery(bool async, CancellationToken cance public override object ExecuteScalar() { - return TaskHelper.WaitNonAsyncTask(ExecuteScalar(false, CancellationToken.None)); + return TaskHelper.WaitNonAsyncTask(ExecuteScalar(null, false, CancellationToken.None)); + } + + public object ExecuteScalar(ClickHouseColumnSettings? columnSettings) + { + return TaskHelper.WaitNonAsyncTask(ExecuteScalar(columnSettings, false, CancellationToken.None)); } public T ExecuteScalar() { - return TaskHelper.WaitNonAsyncTask(ExecuteScalar(false, CancellationToken.None)); + return TaskHelper.WaitNonAsyncTask(ExecuteScalar(null, false, CancellationToken.None)); + } + + public T ExecuteScalar(ClickHouseColumnSettings? columnSettings) + { + return TaskHelper.WaitNonAsyncTask(ExecuteScalar(columnSettings, false, CancellationToken.None)); } public override async Task ExecuteScalarAsync(CancellationToken cancellationToken) { - return await ExecuteScalar(true, cancellationToken); + return await ExecuteScalar(null, true, cancellationToken); + } + + public async Task ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings) + { + return await ExecuteScalar(columnSettings, true, CancellationToken.None); + } + + public async Task ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings, CancellationToken cancellationToken) + { + return await ExecuteScalar(columnSettings, true, cancellationToken); } public async Task ExecuteScalarAsync() { - return await ExecuteScalar(true, CancellationToken.None); + return await ExecuteScalar(null, true, CancellationToken.None); } public async Task ExecuteScalarAsync(CancellationToken cancellationToken) { - return await ExecuteScalar(true, cancellationToken); + return await ExecuteScalar(null, true, cancellationToken); + } + + public async Task ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings) + { + return await ExecuteScalar(columnSettings, true, CancellationToken.None); } - private async ValueTask ExecuteScalar(bool async, CancellationToken cancellationToken) + public async Task ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings, CancellationToken cancellationToken) { - var result = await ExecuteScalar(reader => reader.GetFieldValue(0), async, cancellationToken); + return await ExecuteScalar(columnSettings, true, cancellationToken); + } + + private async ValueTask ExecuteScalar(ClickHouseColumnSettings? columnSettings, bool async, CancellationToken cancellationToken) + { + var result = await ExecuteScalar(columnSettings, reader => reader.GetFieldValue(0), async, cancellationToken); return (T) result; } - private ValueTask ExecuteScalar(bool async, CancellationToken cancellationToken) + private ValueTask ExecuteScalar(ClickHouseColumnSettings? columnSettings, bool async, CancellationToken cancellationToken) { - return ExecuteScalar(reader => reader.GetValue(0), async, cancellationToken); + return ExecuteScalar(columnSettings, reader => reader.GetValue(0), async, cancellationToken); } - private async ValueTask ExecuteScalar(Func valueSelector, bool async, CancellationToken cancellationToken) + private async ValueTask ExecuteScalar(ClickHouseColumnSettings? columnSettings, Func valueSelector, bool async, CancellationToken cancellationToken) { ClickHouseDataReader? reader = null; try @@ -237,6 +267,9 @@ private async ValueTask ExecuteScalar(Func