Update Sqlite

This commit is contained in:
sunkaixuan
2017-07-10 01:15:54 +08:00
parent 2a900481fd
commit 7dd24adb4c
4 changed files with 35 additions and 79 deletions

View File

@@ -241,7 +241,7 @@ namespace SqlSugar
return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString); return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
} }
public object FormatValue(object value) public virtual object FormatValue(object value)
{ {
if (value == null) if (value == null)
{ {

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -6,86 +7,41 @@ namespace SqlSugar
{ {
public class SqliteUpdateBuilder : UpdateBuilder public class SqliteUpdateBuilder : UpdateBuilder
{ {
public override string SqlTemplateBatch
{
get
{
return @"UPDATE {1} S {2} INNER JOIN ${{0}} SET {0} ";
}
}
public override string SqlTemplateJoin
{
get
{
return @" (
{0}
) T ON {1}
";
}
}
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList) protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{ {
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key"); throw new Exception("Batch updates are not supported for the time being. Please wait for updates");
int pageSize = 200; }
int pageIndex = 1; public override object FormatValue(object value)
int totalRecord = groupList.Count; {
int pageCount = (totalRecord + pageSize - 1) / pageSize; if (value == null)
StringBuilder batchUpdateSql = new StringBuilder();
while (pageCount >= pageIndex)
{ {
StringBuilder updateTable = new StringBuilder(); return "NULL";
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it => }
{ else
if (SetValues.IsValuable()) {
{ var type = value.GetType();
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); if (type == PubConst.DateType)
if (setValue != null && setValue.Any()) {
{ var date = value.ObjToDate();
return setValue.First().Value; if (date < Convert.ToDateTime("1900-1-1"))
} {
} date = Convert.ToDateTime("1900-1-1");
var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName)); }
return result; return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
})); }
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString); else if (type == PubConst.BoolType)
int i = 0; {
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()) return value.ObjToBool() ? "1" : "0";
{ }
var isFirst = i == 0; else if (type == PubConst.StringType || type == PubConst.ObjType)
if (!isFirst) {
{ return "'" + value.ToString().ToSqlFilter() + "'";
updateTable.Append(SqlTemplateBatchUnion); }
} else
updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.DbColumnName)))); {
++i; return "'" + value.ToString() + "'";
} }
pageIndex++;
updateTable.Append("\r\n");
string whereString = null;
if (this.WhereValues.IsValuable())
{
foreach (var item in WhereValues)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += item;
}
}
else if (PrimaryKeys.IsValuable())
{
foreach (var item in PrimaryKeys)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
}
}
var format= string.Format(SqlTemplateJoin, updateTable, whereString);
batchUpdateSql.Replace("${0}",format);
batchUpdateSql.Append(";");
} }
return batchUpdateSql.ToString();
} }
} }
} }

View File

@@ -38,7 +38,7 @@ namespace OrmTest.Demo
var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand(); var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand();
//update List<T> //update List<T>
var t7 = db.Updateable(updateObjs).ExecuteCommand(); // var t7 = db.Updateable(updateObjs).ExecuteCommand();
//Re Set Value //Re Set Value
var t8 = db.Updateable(updateObj) var t8 = db.Updateable(updateObj)