mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update ClickHouse
This commit is contained in:
parent
fdc58ee457
commit
5f7a099140
@ -69,12 +69,12 @@ namespace SqlSugar.ClickHouse
|
|||||||
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
|
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
|
||||||
{
|
{
|
||||||
var connection=(ClickHouseConnection)this.Connection;
|
var connection=(ClickHouseConnection)this.Connection;
|
||||||
|
CheckConnection();
|
||||||
IDataParameter[] ipars = ToIDbDataParameter(parameters);
|
IDataParameter[] ipars = ToIDbDataParameter(parameters);
|
||||||
ClickHouseCommand sqlCommand =connection.CreateCommand();
|
ClickHouseCommand sqlCommand =connection.CreateCommand();
|
||||||
var pars = ToIDbDataParameter(parameters);
|
var pars = ToIDbDataParameter(parameters);
|
||||||
sqlCommand.CommandText = sql;
|
sqlCommand.CommandText = sql;
|
||||||
sqlCommand.Parameters.AddRange(pars);
|
sqlCommand.Parameters.AddRange(pars);
|
||||||
CheckConnection();
|
|
||||||
return sqlCommand;
|
return sqlCommand;
|
||||||
}
|
}
|
||||||
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
|
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
|
||||||
@ -106,7 +106,9 @@ namespace SqlSugar.ClickHouse
|
|||||||
sqlParameter.Value = parameter.Value;
|
sqlParameter.Value = parameter.Value;
|
||||||
sqlParameter.DbType = parameter.DbType;
|
sqlParameter.DbType = parameter.DbType;
|
||||||
sqlParameter.Direction = parameter.Direction;
|
sqlParameter.Direction = parameter.Direction;
|
||||||
|
result[index] = sqlParameter;
|
||||||
++index;
|
++index;
|
||||||
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,11 @@ namespace SqlSugar.ClickHouse
|
|||||||
{
|
{
|
||||||
public class ClickHouseCodeFirst : CodeFirstProvider
|
public class ClickHouseCodeFirst : CodeFirstProvider
|
||||||
{
|
{
|
||||||
|
protected override int DefultLength
|
||||||
|
{
|
||||||
|
get { return 0; }
|
||||||
|
set { value = 0; }
|
||||||
|
}
|
||||||
protected override void ExistLogicEnd(List<EntityColumnInfo> dbColumns)
|
protected override void ExistLogicEnd(List<EntityColumnInfo> dbColumns)
|
||||||
{
|
{
|
||||||
foreach (EntityColumnInfo column in dbColumns)
|
foreach (EntityColumnInfo column in dbColumns)
|
||||||
@ -73,16 +78,17 @@ namespace SqlSugar.ClickHouse
|
|||||||
{
|
{
|
||||||
item.Length = 0;
|
item.Length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
||||||
{
|
{
|
||||||
this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
//this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||||
if (!item.IsPrimarykey)
|
//if (!item.IsPrimarykey)
|
||||||
this.Context.DbMaintenance.DropConstraint(tableName,null);
|
// this.Context.DbMaintenance.DropConstraint(tableName,null);
|
||||||
if (item.IsPrimarykey)
|
//if (item.IsPrimarykey)
|
||||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
// this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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();
|
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));
|
Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName));
|
||||||
return null;
|
return String.Empty;
|
||||||
}
|
}
|
||||||
else if (propertyTypes.First().Value == CSharpDataType.byteArray)
|
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>("UInt64",CSharpDataType.@long),
|
||||||
new KeyValuePair<string, CSharpDataType>("Int8",CSharpDataType.@int),
|
new KeyValuePair<string, CSharpDataType>("Int8",CSharpDataType.@int),
|
||||||
new KeyValuePair<string, CSharpDataType>("UInt8",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>("Decimal",CSharpDataType.@decimal),
|
||||||
new KeyValuePair<string, CSharpDataType>("Decimal32",CSharpDataType.@decimal),
|
new KeyValuePair<string, CSharpDataType>("Decimal32",CSharpDataType.@decimal),
|
||||||
new KeyValuePair<string, CSharpDataType>("Decimal64",CSharpDataType.@decimal),
|
new KeyValuePair<string, CSharpDataType>("Decimal64",CSharpDataType.@decimal),
|
||||||
|
@ -20,7 +20,11 @@ namespace SqlSugar.ClickHouse
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
string schema = GetSchema();
|
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;
|
return sql;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -368,45 +372,14 @@ namespace SqlSugar.ClickHouse
|
|||||||
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||||||
{
|
{
|
||||||
var result= base.GetColumnInfosByTableName(tableName.TrimEnd('"').TrimStart('"').ToLower(), isCache);
|
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);
|
columnInfo.DataType = columnInfo.DataType.Replace(" ", "");
|
||||||
|
if (columnInfo.DataType.StartsWith("Nullable("))
|
||||||
|
{
|
||||||
|
columnInfo.DataType=columnInfo.DataType.Replace("Nullable", "");
|
||||||
|
columnInfo.DataType = System.Text.RegularExpressions.Regex.Match(columnInfo.DataType,@"^\((.+)\)$").Groups[1].Value;
|
||||||
}
|
}
|
||||||
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)
|
|
||||||
{
|
|
||||||
pkList=GetListOrCache<string>("GetColumnInfosByTableName_N_Pk"+tableName, sql);
|
|
||||||
}
|
|
||||||
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,7 @@ namespace SqlSugar.ClickHouse
|
|||||||
{
|
{
|
||||||
if (IsReturnIdentity)
|
if (IsReturnIdentity)
|
||||||
{
|
{
|
||||||
return @"INSERT INTO {0}
|
return @"INSERT INTO {0} ({1}) VALUES ({2}) ; SELECT 1 ";
|
||||||
({1})
|
|
||||||
VALUES
|
|
||||||
({2}) returning $PrimaryKey";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -27,10 +24,92 @@ namespace SqlSugar.ClickHouse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public override string SqlTemplateBatch => "INSERT INTO {0} ({1})";
|
int i = 0;
|
||||||
public override string SqlTemplateBatchUnion => " VALUES ";
|
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()
|
public override string ToSqlString()
|
||||||
{
|
{
|
||||||
@ -50,44 +129,29 @@ namespace SqlSugar.ClickHouse
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
StringBuilder batchInsetrSql = new StringBuilder();
|
StringBuilder batchInsetrSql = new StringBuilder();
|
||||||
int pageSize = 200;
|
batchInsetrSql.Append("INSERT INTO " + GetTableNameString + " ");
|
||||||
int pageIndex = 1;
|
batchInsetrSql.Append("(");
|
||||||
int totalRecord = groupList.Count;
|
batchInsetrSql.Append(columnsString);
|
||||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
batchInsetrSql.Append(") VALUES");
|
||||||
while (pageCount >= pageIndex)
|
string insertColumns = "";
|
||||||
|
foreach (var item in groupList)
|
||||||
{
|
{
|
||||||
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
|
batchInsetrSql.Append("(");
|
||||||
int i = 0;
|
insertColumns = string.Join(",", item.Select(it => FormatValue(it.Value, it.PropertyName)));
|
||||||
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
batchInsetrSql.Append(insertColumns);
|
||||||
|
if (groupList.Last() == item)
|
||||||
{
|
{
|
||||||
var isFirst = i == 0;
|
batchInsetrSql.Append(") ");
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
value = it.Value;
|
batchInsetrSql.Append("), ");
|
||||||
}
|
}
|
||||||
if (value == null||value==DBNull.Value)
|
|
||||||
{
|
|
||||||
return string.Format(SqlTemplateBatchSelect, "NULL");
|
|
||||||
}
|
}
|
||||||
return string.Format(SqlTemplateBatchSelect, "'" + value.ObjToString().ToSqlFilter() + "'");
|
|
||||||
})) + "),");
|
// batchInsetrSql.AppendLine(";select @@IDENTITY");
|
||||||
++i;
|
var result = batchInsetrSql.ToString();
|
||||||
}
|
return result;
|
||||||
pageIndex++;
|
|
||||||
batchInsetrSql.Remove(batchInsetrSql.Length - 1,1).Append("\r\n;\r\n");
|
|
||||||
}
|
|
||||||
return batchInsetrSql.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user