From 89482f3125a9b35e3674b81a733d1f8ba0c8c716 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 30 Jul 2022 17:23:40 +0800 Subject: [PATCH] Update QuestDb --- .../DbMaintenance/QuestDBDbMaintenance.cs | 6 +- .../SqlBuilder/QuestDBInsertBuilder.cs | 2 +- .../SqlBuilder/QuestDBUpdateBuilder.cs | 183 +++++------------- 3 files changed, 48 insertions(+), 143 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs index b9d47fc24..77d6ebff2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/DbMaintenance/QuestDBDbMaintenance.cs @@ -12,7 +12,7 @@ namespace SqlSugar { get { - throw new NotSupportedException("GetDataBase"); + return CreateDataBaseSql; } } protected override string GetColumnInfosByTableNameSql @@ -37,7 +37,7 @@ namespace SqlSugar { get { - throw new NotSupportedException("GetView"); + return "select * from (select 1 as id) t where id=0"; } } #endregion @@ -47,7 +47,7 @@ namespace SqlSugar { get { - return ""; + return "select * from (select 1 as id) t where id=0"; } } protected override string AddPrimaryKeySql diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBInsertBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBInsertBuilder.cs index b37ef7fd8..a6bf7b549 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBInsertBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBInsertBuilder.cs @@ -80,7 +80,7 @@ namespace SqlSugar object value = null; if (it.Value is DateTime) { - value = ((DateTime)it.Value).ToString("O"); + return $" cast('{it.Value.ObjToDate().ToString("yyyy-MM-dd HH:mm:ss.fff")}' as date)"; } else { diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBUpdateBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBUpdateBuilder.cs index 63ff771db..a12cb1225 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBUpdateBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/QuestDB/SqlBuilder/QuestDBUpdateBuilder.cs @@ -2,39 +2,41 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace SqlSugar { public class QuestDBUpdateBuilder : UpdateBuilder { - public override string SqlTemplateBatch - { - get - { - return @"UPDATE {1} {2} SET {0} FROM ${{0}} "; - } - } - public override string SqlTemplateJoin + protected override string TomultipleSqlString(List> groupList) { - get + StringBuilder sb = new StringBuilder(); + int i = 0; + sb.AppendLine(string.Join("\r\n", groupList.Select(t => { - return @" (VALUES - {0} - - ) AS T ({2}) WHERE {1} - "; - } + var updateTable = string.Format("UPDATE {0} SET", base.GetTableNameStringNoWith); + var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(i, m, false)).ToArray()); + var pkList = t.Where(s => s.IsPrimarykey).ToList(); + List whereList = new List(); + foreach (var item in pkList) + { + var isFirst = pkList.First() == item; + var whereString = ""; + whereString += GetOracleUpdateColums(i, item, true); + whereList.Add(whereString); + } + i++; + return string.Format("{0} {1} WHERE {2};", updateTable, setValues, string.Join(" AND", whereList)); + }).ToArray())); + return sb.ToString(); } - public override string SqlTemplateBatchUnion + private string GetOracleUpdateColums(int i, DbColumnInfo m, bool iswhere) { - get - { - return ","; - } + return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(), FormatValue(i, m.DbColumnName, m.Value, iswhere)); } - public object FormatValue(object value,string name,int i,DbColumnInfo columnInfo) + public object FormatValue(int i, string name, object value, bool iswhere) { if (value == null) { @@ -42,26 +44,28 @@ namespace SqlSugar } else { - var type = value.GetType(); - if (type == UtilConstants.DateType||columnInfo.IsArray||columnInfo.IsJson) + var type = UtilMethods.GetUnderType(value.GetType()); + if (type == UtilConstants.DateType && iswhere == false) { - var parameterName = this.Builder.SqlParameterKeyWord + name + i; - var paramter = new SugarParameter(parameterName, value); - if (columnInfo.IsJson) + var date = value.ObjToDate(); + if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig)) { - paramter.IsJson = true; + date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig); } - if (columnInfo.IsArray) + if (this.Context.CurrentConnectionConfig?.MoreSettings?.DisableMillisecond == true) { - paramter.IsArray = true; + return "'" + date.ToString("yyyy-MM-dd HH:mm:ss") + "'"; + } + else + { + return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"; } - this.Parameters.Add(paramter); - return parameterName; } - else if (type == UtilConstants.ByteArrayType) + else if (type == UtilConstants.DateType && iswhere) { - string bytesString = "0x" + BitConverter.ToString((byte[])value); - return bytesString; + var parameterName = this.Builder.SqlParameterKeyWord + name + i; + this.Parameters.Add(new SugarParameter(parameterName, value)); + return parameterName; } else if (type.IsEnum()) { @@ -74,6 +78,12 @@ namespace SqlSugar return Convert.ToInt64(value); } } + else if (type == UtilConstants.ByteArrayType) + { + var parameterName = this.Builder.SqlParameterKeyWord + name + i; + this.Parameters.Add(new SugarParameter(parameterName, value)); + return parameterName; + } else if (type == UtilConstants.BoolType) { return value.ObjToBool() ? "1" : "0"; @@ -88,110 +98,5 @@ namespace SqlSugar } } } - - protected override string TomultipleSqlString(List> groupList) - { - Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List need Primary key"); - int pageSize = 200; - int pageIndex = 1; - int totalRecord = groupList.Count; - int pageCount = (totalRecord + pageSize - 1) / pageSize; - StringBuilder batchUpdateSql = new StringBuilder(); - while (pageCount >= pageIndex) - { - StringBuilder updateTable = new StringBuilder(); - string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it => - { - if (SetValues.IsValuable()) - { - var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); - if (setValue != null && setValue.Any()) - { - return setValue.First().Value; - } - } - var result = string.Format("{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName)); - return result; - })); - string tempColumnValue = string.Join(",", groupList.First().Select(it => - { - if (SetValues.IsValuable()) - { - var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); - if (setValue != null && setValue.Any()) - { - return setValue.First().Value; - } - } - var result = Builder.GetTranslationColumnName(it.DbColumnName); - return result; - })); - batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString); - int i = 0; - var tableColumnList = this.Context.DbMaintenance.GetColumnInfosByTableName(GetTableNameStringNoWith); - - foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()) - { - var isFirst = i == 0; - if (!isFirst) - { - updateTable.Append(SqlTemplateBatchUnion); - } - updateTable.Append("\r\n (" + string.Join(",", columns.Select(it => - { - var columnInfo = tableColumnList.FirstOrDefault(x => x.DbColumnName.Equals(it.DbColumnName, StringComparison.OrdinalIgnoreCase)); - var dbType = columnInfo?.DataType; - if (dbType == null) { - var typeName = it.PropertyType.Name.ToLower(); - if (typeName == "int32") - typeName = "int"; - if (typeName == "int64") - typeName = "long"; - if (typeName == "int16") - typeName = "short"; - if (typeName == "boolean") - typeName = "bool"; - - var isAnyType = PostgreSQLDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).Any(); - if (isAnyType) - { - dbType = PostgreSQLDbBind.MappingTypesConst.Where(x => x.Value.ToString().ToLower() == typeName).FirstOrDefault().Key; - } - else { - dbType = "varchar"; - } - } - return string.Format("CAST({0} AS {1})", FormatValue(it.Value,it.DbColumnName,i,it), dbType); - - })) + ")"); - ++i; - } - pageIndex++; - updateTable.Append("\r\n"); - string whereString = null; - if (this.WhereValues.HasValue()) - { - foreach (var item in WhereValues) - { - var isFirst = whereString == null; - whereString += (isFirst ? null : " AND "); - whereString += item; - } - } - else if (PrimaryKeys.HasValue()) - { - foreach (var item in PrimaryKeys) - { - var isFirst = whereString == null; - whereString += (isFirst ? null : " AND "); - whereString += string.Format("{0}.{1}=T.{1}", GetTableNameStringNoWith, Builder.GetTranslationColumnName(item)); - } - } - var format = string.Format(SqlTemplateJoin, updateTable, whereString, tempColumnValue); - batchUpdateSql.Replace("${0}", format); - batchUpdateSql.Append(";"); - } - return batchUpdateSql.ToString(); - } } }