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;
StringBuilder batchUpdateSql = new StringBuilder();
while (pageCount >= pageIndex)
{ {
StringBuilder updateTable = new StringBuilder(); if (value == null)
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it =>
{ {
if (SetValues.IsValuable()) return "NULL";
}
else
{ {
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName)); var type = value.GetType();
if (setValue != null && setValue.Any()) if (type == PubConst.DateType)
{ {
return setValue.First().Value; var date = value.ObjToDate();
if (date < Convert.ToDateTime("1900-1-1"))
{
date = Convert.ToDateTime("1900-1-1");
}
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
else if (type == PubConst.BoolType)
{
return value.ObjToBool() ? "1" : "0";
}
else if (type == PubConst.StringType || type == PubConst.ObjType)
{
return "'" + value.ToString().ToSqlFilter() + "'";
}
else
{
return "'" + value.ToString() + "'";
} }
} }
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(SqlTemplateBatchSelect, FormatValue(it.Value), it.DbColumnName))));
++i;
}
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)