mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Update Gbase
This commit is contained in:
parent
8ef4d41b15
commit
d542a4b611
@ -10,6 +10,8 @@ namespace GbaseTest
|
||||
Demo0_SqlSugarClient.Init();
|
||||
Demo1_Queryable.Init();
|
||||
Demo3_Insertable.Init();
|
||||
Demo4_Deleteable.Init();
|
||||
Demo2_Updateable.Init();
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,13 @@ namespace SqlSugar.GBase
|
||||
{
|
||||
public class GBaseInsertBuilder:InsertBuilder
|
||||
{
|
||||
public override string SqlTemplateBatch
|
||||
{
|
||||
get
|
||||
{
|
||||
return "INSERT into {0} ({1})";
|
||||
}
|
||||
}
|
||||
public override string SqlTemplate
|
||||
{
|
||||
get
|
||||
@ -59,21 +66,14 @@ namespace SqlSugar.GBase
|
||||
else
|
||||
{
|
||||
StringBuilder batchInsetrSql = new StringBuilder();
|
||||
int pageSize = 200;
|
||||
if (this.EntityInfo.Columns.Count > 30)
|
||||
{
|
||||
pageSize = 50;
|
||||
}
|
||||
else if (this.EntityInfo.Columns.Count > 20)
|
||||
{
|
||||
pageSize = 100;
|
||||
}
|
||||
int pageSize = groupList.Count;
|
||||
int pageIndex = 1;
|
||||
int totalRecord = groupList.Count;
|
||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||
while (pageCount >= pageIndex)
|
||||
{
|
||||
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
|
||||
batchInsetrSql.AppendFormat("SELECT * FROM (");
|
||||
int i = 0;
|
||||
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
||||
{
|
||||
@ -86,7 +86,7 @@ namespace SqlSugar.GBase
|
||||
++i;
|
||||
}
|
||||
pageIndex++;
|
||||
batchInsetrSql.Append("\r\n;\r\n");
|
||||
batchInsetrSql.Append(") temp1\r\n;\r\n");
|
||||
}
|
||||
var result = batchInsetrSql.ToString();
|
||||
//if (this.Context.CurrentConnectionConfig.DbType == DbType.GBase)
|
||||
|
@ -7,73 +7,107 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.GBase
|
||||
{
|
||||
public class GBaseUpdateBuilder: UpdateBuilder
|
||||
public class GBaseUpdateBuilder : UpdateBuilder
|
||||
{
|
||||
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
||||
{
|
||||
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> 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)
|
||||
if (groupList == null || groupList.Count == 0)
|
||||
{
|
||||
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("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
|
||||
return result;
|
||||
}));
|
||||
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
|
||||
int i = 0;
|
||||
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 SELECT " + string.Join(",", columns.Select(it => string.Format(base.SqlTemplateBatchSelect, GetValue(it), Builder.GetTranslationColumnName(it.DbColumnName)))));
|
||||
++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 += Regex.Replace(item, "\\" + this.Builder.SqlTranslationLeft, "S." + this.Builder.SqlTranslationLeft);
|
||||
}
|
||||
}
|
||||
if (PrimaryKeys!=null&&PrimaryKeys.HasValue())
|
||||
{
|
||||
foreach (var item in PrimaryKeys)
|
||||
{
|
||||
var isFirst = whereString == null;
|
||||
whereString += (isFirst ? null : " AND ");
|
||||
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
|
||||
}
|
||||
}
|
||||
batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString);
|
||||
return "select 0 from DUAL";
|
||||
}
|
||||
return batchUpdateSql.ToString();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int i = 0;
|
||||
sb.AppendLine(string.Join(UtilConstants.ReplaceCommaKey.Replace("{", "").Replace("}", ""), groupList.Select(t =>
|
||||
{
|
||||
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<string> whereList = new List<string>();
|
||||
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();
|
||||
}
|
||||
|
||||
private object GetValue(DbColumnInfo it)
|
||||
{
|
||||
return FormatValue(it.Value);
|
||||
}
|
||||
private string GetOracleUpdateColums(int i, DbColumnInfo m, bool iswhere)
|
||||
{
|
||||
return string.Format("{0}={1}", m.DbColumnName, FormatValue(i, m.DbColumnName, m.Value, iswhere));
|
||||
}
|
||||
public object FormatValue(int i, string name, object value, bool iswhere)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = UtilMethods.GetUnderType(value.GetType());
|
||||
if (type == UtilConstants.DateType && iswhere == false)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
|
||||
{
|
||||
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig?.MoreSettings?.DisableMillisecond == true)
|
||||
{
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss") + "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
}
|
||||
else if (type == UtilConstants.DateType && iswhere)
|
||||
{
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
return parameterName;
|
||||
}
|
||||
else if (type.IsEnum())
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
|
||||
{
|
||||
return value.ToSqlValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
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 (value is int || value is long || value is short || value is short || value is byte)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else if (value is bool)
|
||||
{
|
||||
return value.ObjToString().ToLower();
|
||||
}
|
||||
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
|
||||
{
|
||||
return "'" + value.ToString().ToSqlFilter() + "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "'" + value.ToString() + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user