Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DbDataSource implementation #553

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions ClickHouse.Client/ADO/ClickHouseDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#if NET7_0_OR_GREATER
using System;
using System.Data.Common;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace ClickHouse.Client.ADO;

public sealed class ClickHouseDataSource : DbDataSource, IClickHouseDataSource
{
private readonly Func<ClickHouseConnection> connectionFactory;
private readonly HttpClient httpClient;

/// <summary>
/// Initializes a new instance of the <see cref="ClickHouseDataSource"/> class using provided HttpClient.
/// Note that HttpClient must have AutomaticDecompression enabled if compression is not disabled in connection string
/// </summary>
/// <param name="connectionString">Connection string</param>
/// <param name="httpClient">instance of HttpClient</param>
/// <param name="disposeHttpClient">dispose of the passed-in instance of HttpClient</param>
public ClickHouseDataSource(string connectionString, HttpClient httpClient = null, bool disposeHttpClient = true)
{
ConnectionString = connectionString;
connectionFactory = httpClient != null
? () => new ClickHouseConnection(connectionString, httpClient)
: () => new ClickHouseConnection(connectionString);
if (disposeHttpClient)
{
this.httpClient = httpClient;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="ClickHouseDataSource"/> class using an HttpClient generated by the provided <paramref name="httpClientFactory"/>.
/// </summary>
/// <param name="connectionString">The ClickHouse connection string.</param>
/// <param name="httpClientFactory">The factory to be used for creating the clients.</param>
/// <param name="httpClientName">
/// The name of the HTTP client you want to be created using the provided factory.
/// If left empty, the default client will be created.
/// </param>
/// <remarks>
/// <list type="bullet">
/// <item>
/// If compression is not disabled in the <paramref name="connectionString"/>, the <paramref name="httpClientFactory"/>
/// must be configured to enable <see cref="HttpClientHandler.AutomaticDecompression"/> for its generated clients.
/// <example>
/// For example, you can do this while registering the HTTP client:
/// <code>
/// services.AddHttpClient("ClickHouseClient").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
/// {
/// AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
/// });
/// </code>
/// </example>
/// </item>
/// <item>
/// The <paramref name="httpClientFactory"/> must set the timeout for its clients if needed.
/// <example>
/// For example, you can do this while registering the HTTP client:
/// <code>
/// services.AddHttpClient("ClickHouseClient", c => c.Timeout = TimeSpan.FromMinutes(5));
/// </code>
/// </example>
/// </item>
/// </list>
/// </remarks>
public ClickHouseDataSource(string connectionString, IHttpClientFactory httpClientFactory, string httpClientName = "")
{
ArgumentNullException.ThrowIfNull(httpClientFactory);
ArgumentNullException.ThrowIfNull(httpClientName);
ConnectionString = connectionString;
connectionFactory = () => new ClickHouseConnection(connectionString, httpClientFactory, httpClientName);
}

public override string ConnectionString
{
get;
}

public ILogger Logger
{
get;
set;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
httpClient?.Dispose();
}
}

protected override DbConnection CreateDbConnection()
{
var cn = connectionFactory();
if (cn.Logger == null && Logger != null)
{
cn.Logger = Logger;
}

return cn;
}

public new ClickHouseConnection CreateConnection() => (ClickHouseConnection)CreateDbConnection();

IClickHouseConnection IClickHouseDataSource.CreateConnection() => CreateConnection();

public new ClickHouseConnection OpenConnection() => (ClickHouseConnection)OpenDbConnection();

IClickHouseConnection IClickHouseDataSource.OpenConnection() => OpenConnection();

public new async Task<ClickHouseConnection> OpenConnectionAsync(CancellationToken cancellationToken)
{
var cn = await OpenDbConnectionAsync(cancellationToken).ConfigureAwait(false);
return (ClickHouseConnection)cn;
}

async Task<IClickHouseConnection> IClickHouseDataSource.OpenConnectionAsync(CancellationToken cancellationToken)
{
var cn = await OpenDbConnectionAsync(cancellationToken).ConfigureAwait(false);
return (IClickHouseConnection)cn;
}
}
#endif
19 changes: 19 additions & 0 deletions ClickHouse.Client/IClickHouseDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if NET7_0_OR_GREATER
using System.Threading;
using System.Threading.Tasks;

namespace ClickHouse.Client;

public interface IClickHouseDataSource
{
string ConnectionString {
get;
}

IClickHouseConnection CreateConnection();

IClickHouseConnection OpenConnection();

Task<IClickHouseConnection> OpenConnectionAsync(CancellationToken cancellationToken = default);
}
#endif
Loading