Skip to content

Commit

Permalink
Added ability to specify custom settings in connection string using "…
Browse files Browse the repository at this point in the history
…set_" prefix #40
  • Loading branch information
DarkWanderer committed Sep 16, 2020
1 parent 71de1a0 commit 886af95
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ClickHouse.Client.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public void ShouldThrowExceptionOnInvalidHttpClient()
Assert.Throws<InvalidOperationException>(() => connection.Open());
}

[Test]
public void ShouldParseCustomParameter()
{
using var connection = new ClickHouseConnection("set_my_parameter=aaa");
Assert.AreEqual("aaa", connection.CustomSettings["my_parameter"]);
}

[Test]
public void ShouldEmitCustomParameter()
{
using var connection = new ClickHouseConnection();
connection.CustomSettings.Add("my_parameter", "aaa");
Assert.That(connection.ConnectionString, Contains.Substring("set_my_parameter=aaa"));
}

[Test]
public void ShouldConnectToServer()
{
Expand Down
12 changes: 12 additions & 0 deletions ClickHouse.Client/ADO/ClickHouseConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -18,6 +19,8 @@ namespace ClickHouse.Client.ADO
{
public class ClickHouseConnection : DbConnection, IClickHouseConnection, ICloneable
{
private const string CustomSettingPrefix = "set_";

private readonly HttpClient httpClient;
private readonly ConcurrentDictionary<string, object> customSettings = new ConcurrentDictionary<string, object>();
private ConnectionState state = ConnectionState.Closed; // Not an autoproperty because of interface implementation
Expand Down Expand Up @@ -77,6 +80,10 @@ public sealed override string ConnectionString
UseSession = session != null,
Timeout = timeout,
};

foreach (var kvp in CustomSettings)
builder[CustomSettingPrefix + kvp.Key] = kvp.Value;

return builder.ToString();
}

Expand All @@ -90,6 +97,11 @@ public sealed override string ConnectionString
useCompression = builder.Compression;
session = builder.UseSession ? builder.SessionId ?? Guid.NewGuid().ToString() : null;
timeout = builder.Timeout;

foreach (var key in builder.Keys.Cast<string>().Where(k => k.StartsWith(CustomSettingPrefix)))
{
CustomSettings.Set(key.Replace(CustomSettingPrefix, string.Empty), builder[key]);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions ClickHouse.Client/Utility/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionar
dictionary.Add(key, value);
return true;
}

public static void Set<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary.ContainsKey(key))
dictionary[key] = value;
else
dictionary.Add(key, value);
}
}
}

0 comments on commit 886af95

Please sign in to comment.