This commit is contained in:
sunkaixuan
2017-05-19 15:26:06 +08:00
parent f51a962018
commit 37f5f5ce20
3 changed files with 138 additions and 42 deletions

View File

@@ -1,71 +1,165 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
public class UpdateBuilder : IDMLBuilder
{
public ISqlBuilder Builder { get; internal set; }
public UpdateBuilder()
{
this.sql = new StringBuilder();
this.DbColumnInfoList = new List<DbColumnInfo>();
}
public SqlSugarClient Context { get; set; }
public ILambdaExpressions LambdaExpressions { get; set; }
public ISqlBuilder Builder { get; set; }
public StringBuilder sql { get; set; }
public List<SugarParameter> Parameters { get; set; }
public string TableName { get; set; }
public string TableWithString { get; set; }
public List<DbColumnInfo> DbColumnInfoList { get; set; }
public bool IsUpdateNull { get; set; }
public bool IsReturnIdentity { get; set; }
public SqlSugarClient Context
public virtual string SqlTemplate
{
get
{
throw new NotImplementedException();
}
if (IsReturnIdentity)
{
return @"INSERT INTO {0}
({1})
VALUES
({2}) ;SELECT SCOPE_IDENTITY();";
}
else
{
return @"INSERT INTO {0}
({1})
VALUES
({2}) ;";
set
{
throw new NotImplementedException();
}
}
}
public ILambdaExpressions LambdaExpressions { get; internal set; }
public List<SugarParameter> Parameters
public virtual string SqlTemplateBatch
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
return "INSERT {0} ({1})";
}
}
public StringBuilder sql
public virtual string SqlTemplateBatchSelect
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
return "{0} AS {1}";
}
}
public string SqlTemplate
public virtual string SqlTemplateBatchUnion
{
get
{
throw new NotImplementedException();
return "\t\r\nUNION ALL ";
}
}
public void Clear()
public virtual void Clear()
{
throw new NotImplementedException();
}
public virtual string GetTableNameString
{
get
{
var result = Builder.GetTranslationTableName(TableName);
result += PubConst.Space;
if (this.TableWithString.IsValuable())
{
result += TableWithString + PubConst.Space;
}
return result;
}
}
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
{
ILambdaExpressions resolveExpress = this.LambdaExpressions;
this.LambdaExpressions.Clear();
resolveExpress.MappingColumns = Context.MappingColumns;
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
resolveExpress.Resolve(expression, resolveType);
this.Parameters = new List<SugarParameter>();
this.Parameters.AddRange(resolveExpress.Parameters);
var reval = resolveExpress.Result;
return reval;
}
public virtual string ToSqlString()
{
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
var isSingle = groupList.Count() == 1;
string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.ColumnName)));
if (isSingle)
{
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.ColumnName));
return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
}
else
{
StringBuilder batchInsetrSql = new StringBuilder();
int pageSize = 200;
int pageIndex = 1;
int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize;
while (pageCount >= pageIndex)
{
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
int i = 0;
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
{
var isFirst = i == 0;
if (!isFirst)
{
batchInsetrSql.Append(SqlTemplateBatchUnion);
}
batchInsetrSql.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.ColumnName))));
++i;
}
pageIndex++;
batchInsetrSql.Append("\r\nGO\r\n");
}
return batchInsetrSql.ToString();
}
}
public string ToSqlString()
public object FormatValue(object value)
{
throw new NotImplementedException();
if (value == null)
{
return "NULL";
}
else
{
var type = value.GetType();
if (type == PubConst.DateType)
{
return "'" + value.ObjToDate().ToString("yyyy-MM-dd hh:mm:ss.ms") + "'";
}
else if (type == PubConst.StringType || type == PubConst.ObjType)
{
return "N'" + value.ToString().ToSqlFilter() + "'";
}
else
{
return "N'" + value.ToString() + "'";
}
}
}
}
}

View File

@@ -17,57 +17,59 @@ namespace SqlSugar
public int ExecuteCommand()
{
throw new NotImplementedException();
return 0;
}
public IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> ReSetValue(Func<T, bool> setValueExpression)
public IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression)
{
throw new NotImplementedException();
var expResult=UpdateBuilder.GetExpressionValue(setValueExpression, ResolveExpressType.WhereSingle);
return this;
}
public KeyValuePair<string, List<SugarParameter>> ToSql()
{
throw new NotImplementedException();
return new KeyValuePair<string, List<SugarParameter>>();
}
public IUpdateable<T> Update(T InsertObj)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> UpdateRange(List<T> InsertObjs)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> Where(bool isUpdateNull)
{
throw new NotImplementedException();
return this;
}
public IUpdateable<T> With(string lockString)
{
throw new NotImplementedException();
return this;
}
internal void Init()
{
throw new NotImplementedException();
return this;
}
}
}

View File

@@ -16,7 +16,7 @@ namespace SqlSugar
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod);
IUpdateable<T> ReSetValue(Func<T, bool> setValueExpression);
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
IUpdateable<T> UpdateRange(List<T> InsertObjs);
KeyValuePair<string,List<SugarParameter>> ToSql();
}