Update ClickHouse

This commit is contained in:
sunkaixuan 2022-08-13 15:15:33 +08:00
parent fdc58ee457
commit 5f7a099140
5 changed files with 131 additions and 86 deletions

View File

@ -69,12 +69,12 @@ namespace SqlSugar.ClickHouse
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
{
var connection=(ClickHouseConnection)this.Connection;
CheckConnection();
IDataParameter[] ipars = ToIDbDataParameter(parameters);
ClickHouseCommand sqlCommand =connection.CreateCommand();
var pars = ToIDbDataParameter(parameters);
sqlCommand.CommandText = sql;
sqlCommand.Parameters.AddRange(pars);
CheckConnection();
return sqlCommand;
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
@ -106,7 +106,9 @@ namespace SqlSugar.ClickHouse
sqlParameter.Value = parameter.Value;
sqlParameter.DbType = parameter.DbType;
sqlParameter.Direction = parameter.Direction;
result[index] = sqlParameter;
++index;
}
return result;
}

View File

@ -7,6 +7,11 @@ namespace SqlSugar.ClickHouse
{
public class ClickHouseCodeFirst : CodeFirstProvider
{
protected override int DefultLength
{
get { return 0; }
set { value = 0; }
}
protected override void ExistLogicEnd(List<EntityColumnInfo> dbColumns)
{
foreach (EntityColumnInfo column in dbColumns)
@ -73,16 +78,17 @@ namespace SqlSugar.ClickHouse
{
item.Length = 0;
}
}
}
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
{
this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
if (!item.IsPrimarykey)
this.Context.DbMaintenance.DropConstraint(tableName,null);
if (item.IsPrimarykey)
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
//this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
//if (!item.IsPrimarykey)
// this.Context.DbMaintenance.DropConstraint(tableName,null);
//if (item.IsPrimarykey)
// this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
}
}

View File

@ -33,7 +33,7 @@ namespace SqlSugar.ClickHouse
return MappingTypes.Where(it => it.Value.ToString().ToLower() == dbTypeName2 || it.Key.ToLower() == dbTypeName2).Select(it => it.Value + "[]").First();
}
Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName));
return null;
return String.Empty;
}
else if (propertyTypes.First().Value == CSharpDataType.byteArray)
{
@ -69,7 +69,7 @@ namespace SqlSugar.ClickHouse
new KeyValuePair<string, CSharpDataType>("UInt64",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("Int8",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("UInt8",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("Decimal128(19)",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("Decimal(38,19)",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("Decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("Decimal32",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("Decimal64",CSharpDataType.@decimal),

View File

@ -20,7 +20,11 @@ namespace SqlSugar.ClickHouse
get
{
string schema = GetSchema();
string sql = @"select * from information_schema.columns a where lower(table_name) =lower('{0}')";
string sql = @"select table_name as TableName,
column_type as DataType ,
is_nullable as IsNullable,
column_name DbColumnName,
column_comment as ColumnDescription from information_schema.columns a where lower(table_name) =lower('{0}')";
return sql;
}
}
@ -368,45 +372,14 @@ namespace SqlSugar.ClickHouse
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
{
var result= base.GetColumnInfosByTableName(tableName.TrimEnd('"').TrimStart('"').ToLower(), isCache);
if (result == null || result.Count() == 0)
foreach (var columnInfo in result)
{
result = base.GetColumnInfosByTableName(tableName, isCache);
}
try
{
string sql = $@"select
kcu.column_name as key_column
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
where tco.constraint_type = 'PRIMARY KEY'
and kcu.table_schema='{GetSchema()}' and
upper(kcu.table_name)=upper('{tableName.TrimEnd('"').TrimStart('"')}')";
List<string> pkList = new List<string>();
if (isCache)
columnInfo.DataType = columnInfo.DataType.Replace(" ", "");
if (columnInfo.DataType.StartsWith("Nullable("))
{
pkList=GetListOrCache<string>("GetColumnInfosByTableName_N_Pk"+tableName, sql);
columnInfo.DataType=columnInfo.DataType.Replace("Nullable", "");
columnInfo.DataType = System.Text.RegularExpressions.Regex.Match(columnInfo.DataType,@"^\((.+)\)$").Groups[1].Value;
}
else
{
pkList = this.Context.Ado.SqlQuery<string>(sql);
}
if (pkList.Count >1)
{
foreach (var item in result)
{
if (pkList.Select(it=>it.ToUpper()).Contains(item.DbColumnName.ToUpper()))
{
item.IsPrimarykey = true;
}
}
}
}
catch
{
}
return result;
}

View File

@ -12,10 +12,7 @@ namespace SqlSugar.ClickHouse
{
if (IsReturnIdentity)
{
return @"INSERT INTO {0}
({1})
VALUES
({2}) returning $PrimaryKey";
return @"INSERT INTO {0} ({1}) VALUES ({2}) ; SELECT 1 ";
}
else
{
@ -27,10 +24,92 @@ namespace SqlSugar.ClickHouse
}
}
}
public override string SqlTemplateBatch => "INSERT INTO {0} ({1})";
public override string SqlTemplateBatchUnion => " VALUES ";
int i = 0;
public object FormatValue(object value, string name)
{
var n = "N";
if (this.Context.CurrentConnectionConfig.MoreSettings != null && this.Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar)
{
n = "";
}
if (value == null)
{
return "NULL";
}
else
{
var type = UtilMethods.GetUnderType(value.GetType());
if (type == UtilConstants.DateType)
{
return GetDateTimeString(value);
}
else if (value is DateTimeOffset)
{
return GetDateTimeOffsetString(value);
}
else if (type == UtilConstants.ByteArrayType)
{
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
return bytesString;
}
else if (type.IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
return value.ToSqlValue(); ;
}
else
{
return Convert.ToInt64(value);
}
}
else if (type == UtilConstants.BoolType)
{
return value.ObjToBool() ? "1" : "0";
}
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
{
++i;
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
this.Parameters.Add(new SugarParameter(parameterName, value));
return parameterName;
}
else
{
return n + "'" + GetString(value) + "'";
}
}
}
public override string SqlTemplateBatchSelect => " {0} ";
private object GetDateTimeOffsetString(object value)
{
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
{
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
}
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
private object GetDateTimeString(object value)
{
var date = value.ObjToDate();
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
{
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
}
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
private string GetString(object value)
{
var result = value.ToString();
if (result.HasValue() && result.Contains("\\"))
{
result = result.Replace("\\", "\\\\");
}
return result;
}
public override string ToSqlString()
{
@ -50,44 +129,29 @@ namespace SqlSugar.ClickHouse
else
{
StringBuilder batchInsetrSql = new StringBuilder();
int pageSize = 200;
int pageIndex = 1;
int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize;
while (pageCount >= pageIndex)
batchInsetrSql.Append("INSERT INTO " + GetTableNameString + " ");
batchInsetrSql.Append("(");
batchInsetrSql.Append(columnsString);
batchInsetrSql.Append(") VALUES");
string insertColumns = "";
foreach (var item in groupList)
{
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
int i = 0;
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
batchInsetrSql.Append("(");
insertColumns = string.Join(",", item.Select(it => FormatValue(it.Value, it.PropertyName)));
batchInsetrSql.Append(insertColumns);
if (groupList.Last() == item)
{
var isFirst = i == 0;
if (isFirst)
{
batchInsetrSql.Append(SqlTemplateBatchUnion);
}
batchInsetrSql.Append("\r\n ( " + string.Join(",", columns.Select(it =>
{
object value = null;
if (it.Value is DateTime)
{
value = ((DateTime)it.Value).ToString("O");
}
else
{
value = it.Value;
}
if (value == null||value==DBNull.Value)
{
return string.Format(SqlTemplateBatchSelect, "NULL");
}
return string.Format(SqlTemplateBatchSelect, "'" + value.ObjToString().ToSqlFilter() + "'");
})) + "),");
++i;
batchInsetrSql.Append(") ");
}
else
{
batchInsetrSql.Append("), ");
}
pageIndex++;
batchInsetrSql.Remove(batchInsetrSql.Length - 1,1).Append("\r\n;\r\n");
}
return batchInsetrSql.ToString();
// batchInsetrSql.AppendLine(";select @@IDENTITY");
var result = batchInsetrSql.ToString();
return result;
}
}
}