Skip to content

Commit 3093a68

Browse files
Make ConnectionString settable again (#98)
* Make ConnectionString settable again * Make database setting mutable to support changing db for open connections
1 parent e5fbfbb commit 3093a68

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

ClickHouse.Driver.Tests/ADO/ConnectionTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ public async Task ShouldCreateConnectionWithSkipServerCertificateValidation()
4747
Assert.That(conn.SkipServerCertificateValidation, Is.True);
4848
}
4949

50+
[Test]
51+
public async Task ShouldBeAbleToSetConnectionStringAfterCreation() // Necessary to support this for some scenarios involving ClickHouseConnectionFactory
52+
{
53+
var conn = new ClickHouseConnection();
54+
conn.ConnectionString = TestUtilities.GetConnectionStringBuilder().ToString();
55+
await conn.OpenAsync();
56+
57+
Assert.That(conn.State, Is.EqualTo(ConnectionState.Open));
58+
}
59+
60+
[Test]
61+
public async Task ShouldNotBeAbleToSetConnectionStringWhileOpen()
62+
{
63+
using var conn = new ClickHouseConnection(TestUtilities.GetConnectionStringBuilder().ToString());
64+
await conn.OpenAsync();
65+
66+
Assert.That(conn.State, Is.EqualTo(ConnectionState.Open));
67+
Assert.Throws<InvalidOperationException>(() => conn.ConnectionString = "Host=otherhost");
68+
}
69+
5070
[Test]
5171
public void ShouldParseCustomParameter()
5272
{

ClickHouse.Driver/ADO/ClickHouseClientSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public ClickHouseClientSettings(ClickHouseClientSettings other)
9393
/// Gets or sets the database name.
9494
/// Default: "" (if empty, will use the user's default database if it has been configured).
9595
/// </summary>
96-
public string Database { get; init; } = ClickHouseDefaults.Database;
96+
public string Database { get; set; } = ClickHouseDefaults.Database;
9797

9898
/// <summary>
9999
/// Gets or sets the path component of the URL (for reverse proxy scenarios).

ClickHouse.Driver/ADO/ClickHouseConnection.cs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ClickHouseConnection(string connectionString, bool skipServerCertificateV
5959
SkipServerCertificateValidation = skipServerCertificateValidation,
6060
};
6161

62-
ApplySettings(settings);
62+
Settings = settings;
6363
}
6464

6565
/// <summary>
@@ -75,7 +75,7 @@ public ClickHouseConnection(string connectionString, HttpClient httpClient)
7575
HttpClient = httpClient,
7676
};
7777

78-
ApplySettings(settings);
78+
Settings = settings;
7979
}
8080

8181
/// <summary>
@@ -121,7 +121,7 @@ public ClickHouseConnection(string connectionString, IHttpClientFactory httpClie
121121
HttpClientName = httpClientName,
122122
};
123123

124-
ApplySettings(settings);
124+
Settings = settings;
125125
}
126126

127127
/// <summary>
@@ -132,8 +132,7 @@ public ClickHouseConnection(ClickHouseClientSettings settings)
132132
{
133133
if (settings == null)
134134
throw new ArgumentNullException(nameof(settings));
135-
136-
ApplySettings(settings);
135+
Settings = settings;
137136
}
138137

139138
private ILoggerFactory loggerFactory;
@@ -158,15 +157,24 @@ internal ILogger GetLogger(string categoryName)
158157
/// <summary>
159158
/// Gets the string defining connection settings for ClickHouse server
160159
/// Example: Host=localhost;Port=8123;Username=default;Password=123;Compression=true
161-
/// Setting the connection string is not supported; create a new connection with the desired settings instead.
160+
/// It is generally recommended create a new connection instead of modifying the settings of an existing one.
162161
/// </summary>
163162
public sealed override string ConnectionString
164163
{
165164
get => ConnectionStringBuilder.ToString();
166-
set => throw new NotSupportedException("Connection string cannot be changed after construction. Create a new connection with the desired settings.");
165+
set => Settings = new ClickHouseClientSettings(value);
167166
}
168167

169-
public ClickHouseClientSettings Settings { get; private set; }
168+
public ClickHouseClientSettings Settings { get;
169+
set
170+
{
171+
if (State == ConnectionState.Open)
172+
throw new InvalidOperationException("Cannot change settings while connection is open.");
173+
174+
field = value;
175+
ApplySettings();
176+
}
177+
}
170178

171179
public IDictionary<string, object> CustomSettings => Settings?.CustomSettings;
172180

@@ -210,26 +218,26 @@ public virtual Feature SupportedFeatures
210218
private set => supportedFeatures = value;
211219
}
212220

213-
private void ApplySettings(ClickHouseClientSettings settings)
221+
private void ApplySettings()
214222
{
215-
settings.Validate();
216-
Settings = settings;
223+
Settings.Validate();
217224

218-
serverUri = new UriBuilder(settings.Protocol, settings.Host, settings.Port, settings.Path ?? string.Empty).Uri;
225+
serverUri = new UriBuilder(Settings.Protocol, Settings.Host, Settings.Port, Settings.Path ?? string.Empty).Uri;
219226

220227
// HttpClientFactory/HttpClient
221-
providedHttpClient = settings.HttpClient;
222-
providedHttpClientFactory = settings.HttpClientFactory;
223-
httpClientName = settings.HttpClientName;
228+
providedHttpClient = Settings.HttpClient;
229+
providedHttpClientFactory = Settings.HttpClientFactory;
230+
httpClientName = Settings.HttpClientName;
224231

225232
// Logging
226-
loggerFactory = settings.LoggerFactory;
233+
loggerCache.Clear();
234+
loggerFactory = Settings.LoggerFactory;
227235

228236
#if NET5_0_OR_GREATER
229237
// Debug mode
230-
if (settings.EnableDebugMode)
238+
if (Settings.EnableDebugMode)
231239
{
232-
TraceHelper.Activate(settings.LoggerFactory);
240+
TraceHelper.Activate(Settings.LoggerFactory);
233241
}
234242
#endif
235243

@@ -299,10 +307,7 @@ internal static async Task<HttpResponseMessage> HandleError(HttpResponseMessage
299307

300308
public override void ChangeDatabase(string databaseName)
301309
{
302-
Settings = new ClickHouseClientSettings(Settings)
303-
{
304-
Database = databaseName,
305-
};
310+
Settings.Database = databaseName;
306311
}
307312

308313
public object Clone() => new ClickHouseConnection(ConnectionString);

RELEASENOTES.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ v0.8.2
33

44
**Breaking Changes:**
55
* Removed obsolete MySQL compatibility mapping TIME -> Int64.
6-
* Json serialization of bool arrays now uses the Boolean type instead of UInt8, and is now consistent with bool elements.
6+
* Json serialization of bool arrays now uses the Boolean type instead of UInt8 (it is now consistent with how bool values outside arrays were handled).
77

88
**New Features/Improvements:**
99
* Added support for BFloat16. It is converted to and from a 32-bit float.
@@ -13,8 +13,10 @@ v0.8.2
1313
* Now supports parsing Json that includes Maps; they are read into JsonObjects.
1414
* Added support for decoding BigInteger types, UUID, IPv4, IPv6, and ClickHouseDecimal types (they are handled as strings).
1515
* Expanded binary parsing to cover all types.
16-
* Improved handling of numeric types: now properly detects and preserves integer vs floating-point types (previously all numeric types were handled as double).
17-
* Null values in arrays are now handled properly.
16+
* Improved handling of numeric types when writing Json using BulkCopy: now properly detects and preserves Int32/In64 in addition to double (previously all numeric types were handled as double).
17+
* Parsing null values in arrays is now handled properly.
18+
* ClickHouseConnection.ConnectionString can now be set after creating the connection, to support cases where passing the connection string to the constructor is not possible.
19+
* ClickHouseConnection.CreateCommand() now has an optional argument for the command text.
1820

1921
**Bug Fixes:**
2022
* Fixed a bug where serializing to json with an array of bools with both true and false elements would fail.

0 commit comments

Comments
 (0)