Skip to content

Commit

Permalink
ExecuteScalar with column settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-sushko committed Jul 22, 2020
1 parent 2ef96ec commit 48eb736
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/Octonica.ClickHouseClient.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
53 changes: 43 additions & 10 deletions src/Octonica.ClickHouseClient/ClickHouseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,41 +193,71 @@ private async ValueTask<int> 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<T>()
{
return TaskHelper.WaitNonAsyncTask(ExecuteScalar<T>(false, CancellationToken.None));
return TaskHelper.WaitNonAsyncTask(ExecuteScalar<T>(null, false, CancellationToken.None));
}

public T ExecuteScalar<T>(ClickHouseColumnSettings? columnSettings)
{
return TaskHelper.WaitNonAsyncTask(ExecuteScalar<T>(columnSettings, false, CancellationToken.None));
}

public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
return await ExecuteScalar(true, cancellationToken);
return await ExecuteScalar(null, true, cancellationToken);
}

public async Task<object> ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings)
{
return await ExecuteScalar(columnSettings, true, CancellationToken.None);
}

public async Task<object> ExecuteScalarAsync(ClickHouseColumnSettings? columnSettings, CancellationToken cancellationToken)
{
return await ExecuteScalar(columnSettings, true, cancellationToken);
}

public async Task<T> ExecuteScalarAsync<T>()
{
return await ExecuteScalar<T>(true, CancellationToken.None);
return await ExecuteScalar<T>(null, true, CancellationToken.None);
}

public async Task<T> ExecuteScalarAsync<T>(CancellationToken cancellationToken)
{
return await ExecuteScalar<T>(true, cancellationToken);
return await ExecuteScalar<T>(null, true, cancellationToken);
}

public async Task<T> ExecuteScalarAsync<T>(ClickHouseColumnSettings? columnSettings)
{
return await ExecuteScalar<T>(columnSettings, true, CancellationToken.None);
}

private async ValueTask<T> ExecuteScalar<T>(bool async, CancellationToken cancellationToken)
public async Task<T> ExecuteScalarAsync<T>(ClickHouseColumnSettings? columnSettings, CancellationToken cancellationToken)
{
var result = await ExecuteScalar(reader => reader.GetFieldValue<T>(0), async, cancellationToken);
return await ExecuteScalar<T>(columnSettings, true, cancellationToken);
}

private async ValueTask<T> ExecuteScalar<T>(ClickHouseColumnSettings? columnSettings, bool async, CancellationToken cancellationToken)
{
var result = await ExecuteScalar(columnSettings, reader => reader.GetFieldValue<T>(0), async, cancellationToken);
return (T) result;
}

private ValueTask<object> ExecuteScalar(bool async, CancellationToken cancellationToken)
private ValueTask<object> 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<object> ExecuteScalar(Func<ClickHouseDataReader, object?> valueSelector, bool async, CancellationToken cancellationToken)
private async ValueTask<object> ExecuteScalar(ClickHouseColumnSettings? columnSettings, Func<ClickHouseDataReader, object?> valueSelector, bool async, CancellationToken cancellationToken)
{
ClickHouseDataReader? reader = null;
try
Expand All @@ -237,6 +267,9 @@ private async ValueTask<object> ExecuteScalar(Func<ClickHouseDataReader, object?
if (!hasAnyColumn)
return DBNull.Value;

if (columnSettings != null)
reader.ConfigureColumn(0, columnSettings);

bool hasAnyRow = async ? await reader.ReadAsync(cancellationToken) : reader.Read();
if (!hasAnyRow)
return DBNull.Value;
Expand Down

0 comments on commit 48eb736

Please sign in to comment.