Skip to content

Commit

Permalink
Merge pull request #37 from MikeAmputer/kravtsov/session-parameters
Browse files Browse the repository at this point in the history
Add session parameters
  • Loading branch information
MikeAmputer authored Jun 2, 2024
2 parents d870684 + af8df64 commit b36604e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ public Task<long> BulkInsertAsync(
{
throw new NotImplementedException();
}

public Task SetSessionParameter(string parameterName, object value)
{
return Task.CompletedTask;
}
}
16 changes: 12 additions & 4 deletions src/ClickHouse.Facades/Context/ClickHouseConnectionBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<object> ExecuteScalarAsync(
Dictionary<string, object>? parameters,
CancellationToken cancellationToken)
{
ThrowIfNotConnected();
cancellationToken.ThrowIfCancellationRequested();

await using var command = CreateCommand();
command.CommandText = query;
Expand All @@ -66,7 +66,6 @@ public async Task<int> ExecuteNonQueryAsync(
Dictionary<string, object>? parameters,
CancellationToken cancellationToken)
{
ThrowIfNotConnected();
cancellationToken.ThrowIfCancellationRequested();

await using var command = CreateCommand();
Expand All @@ -81,7 +80,6 @@ public async Task<DbDataReader> ExecuteReaderAsync(
Dictionary<string, object>? parameters,
CancellationToken cancellationToken)
{
ThrowIfNotConnected();
cancellationToken.ThrowIfCancellationRequested();

await using var command = CreateCommand();
Expand All @@ -96,7 +94,6 @@ public DataTable ExecuteDataTable(
Dictionary<string, object>? parameters,
CancellationToken cancellationToken)
{
ThrowIfNotConnected();
cancellationToken.ThrowIfCancellationRequested();

using var command = CreateCommand();
Expand Down Expand Up @@ -154,6 +151,17 @@ public async Task<long> BulkInsertAsync(
return bulkCopyInterface.RowsWritten;
}

public Task SetSessionParameter(string parameterName, object value)
{
if (!_sessionEnabled)
{
throw new InvalidOperationException(
"Unable to set session parameter while sessions are not enabled in the current context.");
}

return _connection.ExecuteStatementAsync($"set {parameterName} = '{value}'");
}

private void SetParameters(ClickHouseCommand command, Dictionary<string, object>? parameters)
{
if (parameters == null)
Expand Down
5 changes: 5 additions & 0 deletions src/ClickHouse.Facades/Context/ClickHouseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public void ChangeDatabase(string databaseName)
_connection!.ChangeDatabase(databaseName);
}

public Task SetSessionParameter(string parameterName, object value)
{
return _connectionBroker.SetSessionParameter(parameterName, value);
}

internal void Initialize(ClickHouseContextOptions<TContext> options)
{
ThrowIfInitialized();
Expand Down
2 changes: 2 additions & 0 deletions src/ClickHouse.Facades/Context/IClickHouseConnectionBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ Task<long> BulkInsertAsync(
int batchSize,
int maxDegreeOfParallelism,
IReadOnlyCollection<string>? columnNames = null);

Task SetSessionParameter(string parameterName, object value);
}

0 comments on commit b36604e

Please sign in to comment.