public ClickHouseDB(TSchema schema, ClickHouseConnectionSettings settings, CreateDatabaseParameters createParams = null) { ProxyLog.Info($"{GetType().Name}: Database={settings.Database}; Server={settings.Host}:{settings.Port}"); ConnectionSettings = settings; if (createParams == null) { CreateParameters = new CreateDatabaseParameters(ClickHouseNodeMode.Autonom); } else { CreateParameters = createParams; if (CreateParameters.NodeMode != ClickHouseNodeMode.Autonom) { if (string.IsNullOrWhiteSpace(CreateParameters.Cluster)) { CreateParameters.Cluster = ConnectionSettings.GetClusterName(); } if (string.IsNullOrWhiteSpace(CreateParameters.Cluster)) { throw new ArgumentNullException(nameof(CreateParameters.Cluster)); } } if (CreateParameters.NodeMode == ClickHouseNodeMode.Distributed) { if (string.IsNullOrWhiteSpace(CreateParameters.DistribitedSourceDatabaseName)) { CreateParameters.DistribitedSourceDatabaseName = ConnectionSettings.Database; } } } Schema = schema; CommandText = new DatabaseCommandText(schema); TableEngines = new Dictionary <Type, ITableEngine>(); DefineTableEngines(); }
public static IEnumerable <T> Read <T>(this ClickHouseDatabase db, string commandText) where T : new() { ProxyLog.Info(commandText); var entities = db.ExecuteQueryMapping <T>(commandText); ProxyLog.Info($"{entities.Count()} row(s)"); return(entities); }
public static T Scalar <T>(this ClickHouseConnection conn, string commandText) { ProxyLog.Info(commandText); using (ClickHouseCommand cmd = new ClickHouseCommand(conn, commandText)) { T result = (T)cmd.ExecuteScalar(); ProxyLog.Info($"Result: {result}"); return(result); } }
//public static string GetServerConnectionString(string host, string port = "9000", string user = "******", string password = "") => // $"Host={host};Port={port};User={user};Password={password};"; #endregion #region ExecuteNonQuery public static int Execute(this ClickHouseConnection conn, string commandText) { ProxyLog.Info(commandText); using (ClickHouseCommand cmd = new ClickHouseCommand(conn, commandText)) { int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { ProxyLog.Info($"{rowsAffected} row(s)"); } return(rowsAffected); } }
public static IEnumerable <string> GetDatabaseList(this ClickHouseConnectionSettings settings) { string commandText = "SELECT name FROM system.databases"; ProxyLog.Info(commandText); using (ClickHouseConnection conn = new ClickHouseConnection(settings)) { conn.Open(); using (ClickHouseCommand cmd = new ClickHouseCommand(conn, commandText)) { using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { yield return(reader.GetString(0)); } ProxyLog.Info($"{reader.RecordsAffected} row(s)"); } } } }
public static void Insert <T>(this ClickHouseConnection conn, ClickHouseTable table, params T[] entities) { BatchData <T> data = new BatchData <T>(entities); string cmdText = $"INSERT INTO {(table.CommandText.IsTemporary ? "" : conn.ConnectionSettings.Database + ".")}{table.Name} ({BatchData<T>.GetFieldNames()}) VALUES @bulk"; ProxyLog.Info(cmdText); using (var command = conn.CreateCommand(cmdText)) { command.Parameters.Add(new ClickHouseParameter { ParameterName = "bulk", Value = data }); int rowsAffected = command.ExecuteNonQuery(); if (rowsAffected == 0) { rowsAffected = data.Count(); } ProxyLog.Info($"{rowsAffected} row(s)"); } }