mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 20:13:41 +08:00
Update Core
This commit is contained in:
@@ -228,6 +228,20 @@ namespace SqlSugar
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
public SqlServerBlueCopy UseSqlServer()
|
||||
{
|
||||
PreToSql();
|
||||
var currentType = this.Context.CurrentConnectionConfig.DbType;
|
||||
Check.Exception(currentType != DbType.SqlServer, "UseSqlServer no support " + currentType);
|
||||
SqlServerBlueCopy result = new SqlServerBlueCopy();
|
||||
result.DbColumnInfoList =this.InsertBuilder.DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
result.Builder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
return result;
|
||||
}
|
||||
|
||||
public IInsertable<T> EnableDiffLogEvent(object businessData = null)
|
||||
{
|
||||
Check.Exception(this.InsertObjs.HasValue() && this.InsertObjs.Count() > 1, "DiffLog does not support batch operations");
|
||||
@@ -237,6 +251,32 @@ namespace SqlSugar
|
||||
diffModel.DiffType = DiffType.insert;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||
{
|
||||
Check.Exception(GetPrimaryKeys().Count == 0, typeof(T).Name + " need Primary key");
|
||||
Check.Exception(GetPrimaryKeys().Count > 1, typeof(T).Name + "Multiple primary keys are not supported");
|
||||
//Check.Exception(this.InsertObjs.Count() > 1, "SubInserable No Support Insertable(List<T>)");
|
||||
//Check.Exception(items.ToString().Contains(".First().")==false, items.ToString()+ " not supported ");
|
||||
if (this.InsertObjs == null || this.InsertObjs.Count() == 0)
|
||||
{
|
||||
return new SubInsertable<T>();
|
||||
}
|
||||
string subMemberName;
|
||||
object sublist;
|
||||
SubInsertable<T> result = new SubInsertable<T>();
|
||||
result.GetList(this.InsertObjs,items, out subMemberName, out sublist);
|
||||
result.InsertObjects = this.InsertObjs;
|
||||
result.Context = this.Context;
|
||||
result.SubList = new Dictionary<string, object>();
|
||||
result.SubList.Add(subMemberName, sublist);
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
result.Pk = GetPrimaryKeys().First();
|
||||
result.Entity = this.EntityInfo;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
@@ -0,0 +1,216 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SubInsertable<T> : ISubInsertable<T> where T : class, new()
|
||||
{
|
||||
internal EntityInfo Entity { get; set; }
|
||||
internal Dictionary<string, object> SubList { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal T [] InsertObjects { get; set; }
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
internal string Pk { get; set; }
|
||||
|
||||
public ISubInsertable<T> AddSubList(Expression<Func<T, object>> items)
|
||||
{
|
||||
if (InsertObjects != null&&InsertObjects.Count() > 0)
|
||||
{
|
||||
string subMemberName;
|
||||
object sublist;
|
||||
GetList(InsertObjects, items, out subMemberName, out sublist);
|
||||
if (!this.SubList.ContainsKey(subMemberName))
|
||||
{
|
||||
this.SubList.Add(subMemberName, sublist);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public object ExecuteReturnPrimaryKey()
|
||||
{
|
||||
|
||||
if (InsertObjects != null && InsertObjects.Count()>0)
|
||||
{
|
||||
int count = 1;
|
||||
foreach (var InsertObject in InsertObjects)
|
||||
{
|
||||
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
||||
int id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
|
||||
object pkValue = null;
|
||||
var qureyable = this.Context.Queryable<T>();
|
||||
if (id.ObjToInt() == 0)
|
||||
{
|
||||
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
||||
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
||||
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
||||
);
|
||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||
qureyable.In(pkValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
qureyable.In(id);
|
||||
pkValue = id;
|
||||
}
|
||||
var data = qureyable.First();
|
||||
foreach (var item in this.SubList)
|
||||
{
|
||||
|
||||
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
EntityInfo subEntity = null;
|
||||
if (item.Value is IEnumerable<object>)
|
||||
{
|
||||
var list = item.Value as IEnumerable<object>;
|
||||
if (list.Count() == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var type = list.First().GetType();
|
||||
this.Context.InitMappingInfo(type);
|
||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||
foreach (var sbItem in list)
|
||||
{
|
||||
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
||||
}
|
||||
}
|
||||
else if (item.Value.GetType().IsClass())
|
||||
{
|
||||
var type = item.Value.GetType();
|
||||
this.Context.InitMappingInfo(type);
|
||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
||||
}
|
||||
count += this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<object> ExecuteReturnPrimaryKeyAsync()
|
||||
{
|
||||
|
||||
if (InsertObjects != null && InsertObjects.Count() > 0)
|
||||
{
|
||||
int count = 1;
|
||||
foreach (var InsertObject in InsertObjects)
|
||||
{
|
||||
List<ConditionalModel> conModel = new List<ConditionalModel>();
|
||||
int id = await this.Context.Insertable(InsertObject).ExecuteReturnIdentityAsync();
|
||||
object pkValue = null;
|
||||
var qureyable = this.Context.Queryable<T>();
|
||||
if (id.ObjToInt() == 0)
|
||||
{
|
||||
var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
|
||||
it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
|
||||
it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
|
||||
);
|
||||
pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
|
||||
qureyable.In(pkValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
qureyable.In(id);
|
||||
pkValue = id;
|
||||
}
|
||||
var data =await qureyable.FirstAsync();
|
||||
foreach (var item in this.SubList)
|
||||
{
|
||||
|
||||
Dictionary<string, object> insertDictionary = new Dictionary<string, object>();
|
||||
if (item.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
EntityInfo subEntity = null;
|
||||
if (item.Value is IEnumerable<object>)
|
||||
{
|
||||
var list = item.Value as IEnumerable<object>;
|
||||
if (list.Count() == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var type = list.First().GetType();
|
||||
this.Context.InitMappingInfo(type);
|
||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||
foreach (var sbItem in list)
|
||||
{
|
||||
SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
|
||||
}
|
||||
}
|
||||
else if (item.Value.GetType().IsClass())
|
||||
{
|
||||
var type = item.Value.GetType();
|
||||
this.Context.InitMappingInfo(type);
|
||||
subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
|
||||
SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
|
||||
}
|
||||
count +=await this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
public void GetList(T[] inserts,Expression<Func<T, object>> items, out string subMemberName, out object sublist)
|
||||
{
|
||||
var lambdaExpression = (items as LambdaExpression).Body;
|
||||
if (lambdaExpression is UnaryExpression)
|
||||
{
|
||||
lambdaExpression = (lambdaExpression as UnaryExpression).Operand;
|
||||
}
|
||||
MemberExpression subMemberException = lambdaExpression as MemberExpression;
|
||||
subMemberName = subMemberException.Member.Name;
|
||||
MemberExpression listMember = null;
|
||||
sublist = null;
|
||||
if (subMemberException.Expression is MethodCallExpression)
|
||||
{
|
||||
listMember = (subMemberException.Expression as MethodCallExpression).Arguments.First() as MemberExpression;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
listMember = (subMemberException.Expression as MemberExpression);
|
||||
}
|
||||
if (listMember == null)
|
||||
{
|
||||
listMember = (items as LambdaExpression).Body as MemberExpression;
|
||||
subMemberName = Guid.NewGuid().ToString();
|
||||
}
|
||||
sublist = inserts.First().GetType().GetProperty(listMember.Member.Name).GetValue(inserts.First());
|
||||
}
|
||||
private void SetItems(Dictionary<string, object> insertDictionary, object sbItem, EntityInfo subEntity,string key,object pkValue)
|
||||
{
|
||||
foreach (var item in subEntity.Columns)
|
||||
{
|
||||
if (item.IsIdentity||item.IsIgnore)
|
||||
continue;
|
||||
if (item.PropertyInfo.Name == key)
|
||||
{
|
||||
insertDictionary.Add(item.DbColumnName, pkValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
insertDictionary.Add(item.DbColumnName, item.PropertyInfo.GetValue(sbItem));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -502,7 +502,7 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name == "Parse" && TempParseType.IsIn(UtilConstants.GuidType))
|
||||
if (name == "Parse" && TempParseType.IsIn(UtilConstants.GuidType)&&model.Args!=null&&model.Args.Count()>1)
|
||||
{
|
||||
name = "Equals";
|
||||
}
|
||||
|
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface ISimpleClient<T> where T : class, new()
|
||||
{
|
||||
IDeleteable<T> AsDeleteable();
|
||||
IInsertable<T> AsInsertable(List<T> insertObjs);
|
||||
IInsertable<T> AsInsertable(T insertObj);
|
||||
IInsertable<T> AsInsertable(T[] insertObjs);
|
||||
ISugarQueryable<T> AsQueryable();
|
||||
ISqlSugarClient AsSugarClient();
|
||||
ITenant AsTenant();
|
||||
IUpdateable<T> AsUpdateable(List<T> updateObjs);
|
||||
IUpdateable<T> AsUpdateable(T updateObj);
|
||||
IUpdateable<T> AsUpdateable(T[] updateObjs);
|
||||
int Count(Expression<Func<T, bool>> whereExpression);
|
||||
bool Delete(Expression<Func<T, bool>> whereExpression);
|
||||
bool Delete(T deleteObj);
|
||||
bool DeleteById(dynamic id);
|
||||
bool DeleteByIds(dynamic[] ids);
|
||||
T GetById(dynamic id);
|
||||
List<T> GetList();
|
||||
List<T> GetList(Expression<Func<T, bool>> whereExpression);
|
||||
List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page);
|
||||
List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
|
||||
List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page);
|
||||
List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
|
||||
T GetSingle(Expression<Func<T, bool>> whereExpression);
|
||||
bool Insert(T insertObj);
|
||||
bool InsertRange(List<T> insertObjs);
|
||||
bool InsertRange(T[] insertObjs);
|
||||
int InsertReturnIdentity(T insertObj);
|
||||
bool IsAny(Expression<Func<T, bool>> whereExpression);
|
||||
bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
|
||||
bool Update(T updateObj);
|
||||
bool UpdateRange(List<T> updateObjs);
|
||||
bool UpdateRange(T[] updateObjs);
|
||||
|
||||
|
||||
|
||||
|
||||
Task<int> CountAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> DeleteAsync(T deleteObj);
|
||||
Task<bool> DeleteByIdAsync(dynamic id);
|
||||
Task<bool> DeleteByIdsAsync(dynamic[] ids);
|
||||
Task<T> GetByIdAsync(dynamic id);
|
||||
Task<List<T>> GetListAsync();
|
||||
Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page);
|
||||
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
|
||||
Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page);
|
||||
Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc);
|
||||
Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> InsertAsync(T insertObj);
|
||||
Task<bool> InsertRangeAsync(List<T> insertObjs);
|
||||
Task<bool> InsertRangeAsync(T[] insertObjs);
|
||||
Task<int> InsertReturnIdentityAsync(T insertObj);
|
||||
Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> UpdateAsync(T updateObj);
|
||||
Task<bool> UpdateRangeAsync(List<T> updateObjs);
|
||||
Task<bool> UpdateRangeAsync(T[] updateObjs);
|
||||
|
||||
}
|
||||
}
|
@@ -44,7 +44,7 @@ namespace SqlSugar
|
||||
|
||||
#region Other methods
|
||||
DateTime GetDate();
|
||||
SimpleClient GetSimpleClient();
|
||||
//SimpleClient GetSimpleClient();
|
||||
SimpleClient<T> GetSimpleClient<T>() where T : class, new();
|
||||
void InitMappingInfo(Type type);
|
||||
void InitMappingInfo<T>();
|
||||
|
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface ISubInsertable<T>
|
||||
{
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
|
||||
object ExecuteReturnPrimaryKey();
|
||||
}
|
||||
}
|
@@ -29,10 +29,12 @@ namespace SqlSugar
|
||||
IInsertable<T> IgnoreColumns(params string[]columns);
|
||||
IInsertable<T> IgnoreColumns(bool ignoreNullColumn, bool isOffIdentity = false);
|
||||
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> SubForeignKey);
|
||||
|
||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IInsertable<T> RemoveDataCache();
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
SqlServerBlueCopy UseSqlServer();
|
||||
void AddQueue();
|
||||
|
||||
#region Obsolete
|
||||
|
@@ -69,7 +69,7 @@ namespace SqlSugar
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
return string.Format(" (DATE_ADD({1} INTERVAL {0} day)) ", parameter.MemberName, parameter2.MemberName);
|
||||
return string.Format(" (DATE_ADD({0}, INTERVAL {1} day)) ", parameter.MemberName, parameter2.MemberName);
|
||||
}
|
||||
|
||||
public override string ToInt32(MethodCallExpressionModel model)
|
||||
|
@@ -50,7 +50,15 @@ namespace SqlSugar
|
||||
|
||||
private object GetSeqValue(string seqName)
|
||||
{
|
||||
return Ado.GetScalar(" SELECT " + seqName + ".currval FROM DUAL");
|
||||
try
|
||||
{
|
||||
return Ado.GetScalar(" SELECT " + seqName + ".currval FROM DUAL");
|
||||
}
|
||||
catch
|
||||
{
|
||||
Ado.GetScalar(" SELECT " + seqName + ".nextval FROM DUAL");
|
||||
return Ado.GetScalar(" SELECT " + seqName + ".currval-1 FROM DUAL");
|
||||
}
|
||||
}
|
||||
protected override void PreToSql()
|
||||
{
|
||||
|
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerBlueCopy
|
||||
{
|
||||
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal ISqlBuilder Builder { get; set; }
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
public int ExecuteBlueCopy()
|
||||
{
|
||||
if (DbColumnInfoList==null||DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
var dt= this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
|
||||
foreach (var rowInfos in DbColumnInfoList)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
foreach (DataColumn item in dt.Columns)
|
||||
{
|
||||
var rows= rowInfos.ToList();
|
||||
var value = rows.FirstOrDefault(it =>
|
||||
it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)||
|
||||
it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)
|
||||
);
|
||||
if (value != null)
|
||||
{
|
||||
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
|
||||
{
|
||||
if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString())
|
||||
{
|
||||
value.Value = Convert.ToDateTime("1753/01/01");
|
||||
}
|
||||
}
|
||||
dr[item.ColumnName] = value.Value;
|
||||
}
|
||||
}
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
|
||||
//获取目标表的名称
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
//写入DataReader对象
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
try
|
||||
{
|
||||
bulkCopy.WriteToServer(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
throw ex;
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
private object AddParameter(int i,string dbColumnName, object value)
|
||||
{
|
||||
var name =Builder.SqlParameterKeyWord+dbColumnName+i;
|
||||
InsertBuilder.Parameters.Add(new SugarParameter(name,value));
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
@@ -57,10 +57,11 @@ namespace SqlSugar
|
||||
batchInsetrSql.Append(columnsString);
|
||||
batchInsetrSql.Append(") VALUES");
|
||||
string insertColumns = "";
|
||||
int i = 0;
|
||||
foreach (var item in groupList)
|
||||
{
|
||||
batchInsetrSql.Append("(");
|
||||
insertColumns = string.Join(",", item.Select(it => FormatValue(it.Value)));
|
||||
insertColumns = string.Join(",", item.Select(it => FormatValue(i,it.DbColumnName,it.Value)));
|
||||
batchInsetrSql.Append(insertColumns);
|
||||
if (groupList.Last() == item)
|
||||
{
|
||||
@@ -70,6 +71,7 @@ namespace SqlSugar
|
||||
{
|
||||
batchInsetrSql.Append("), ");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
batchInsetrSql.AppendLine(";SELECT LAST_INSERT_ROWID();");
|
||||
@@ -77,7 +79,7 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public override object FormatValue(object value)
|
||||
public object FormatValue(int i,string name,object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
@@ -101,8 +103,9 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.ByteArrayType)
|
||||
{
|
||||
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
|
||||
return bytesString;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
return parameterName;
|
||||
}
|
||||
else if (type == UtilConstants.BoolType)
|
||||
{
|
||||
|
@@ -10,29 +10,32 @@ namespace SqlSugar
|
||||
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int i = 0;
|
||||
sb.AppendLine(string.Join("\r\n", groupList.Select(t =>
|
||||
{
|
||||
var updateTable = string.Format("UPDATE {0} SET", base.GetTableNameStringNoWith);
|
||||
var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(m)).ToArray());
|
||||
var setValues = string.Join(",", t.Where(s => !s.IsPrimarykey).Select(m => GetOracleUpdateColums(i,m)).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(item);
|
||||
whereString += GetOracleUpdateColums(i,item);
|
||||
whereList.Add(whereString);
|
||||
}
|
||||
i++;
|
||||
return string.Format("{0} {1} WHERE {2};", updateTable, setValues, string.Join("AND", whereList));
|
||||
}).ToArray()));
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string GetOracleUpdateColums(DbColumnInfo m)
|
||||
private string GetOracleUpdateColums(int i,DbColumnInfo m)
|
||||
{
|
||||
return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(), FormatValue(m.Value));
|
||||
return string.Format("\"{0}\"={1}", m.DbColumnName.ToUpper(), FormatValue(i,m.DbColumnName,m.Value));
|
||||
}
|
||||
public override object FormatValue(object value)
|
||||
|
||||
public object FormatValue(int i,string name,object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
@@ -56,8 +59,9 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.ByteArrayType)
|
||||
{
|
||||
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
|
||||
return bytesString;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
return parameterName;
|
||||
}
|
||||
else if (type == UtilConstants.BoolType)
|
||||
{
|
||||
|
@@ -3,12 +3,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public partial class SimpleClient<T> where T : class, new()
|
||||
public partial class SimpleClient<T> : ISimpleClient<T> where T : class, new()
|
||||
{
|
||||
#region Interface
|
||||
protected ISqlSugarClient Context { get; set; }
|
||||
|
||||
public ITenant AsTenant()
|
||||
@@ -37,7 +39,7 @@ namespace SqlSugar
|
||||
{
|
||||
return Context.Insertable<T>(insertObj);
|
||||
}
|
||||
public IInsertable<T> AsInsertable(T[] insertObjs)
|
||||
public IInsertable<T> AsInsertable(T[] insertObjs)
|
||||
{
|
||||
return Context.Insertable<T>(insertObjs);
|
||||
}
|
||||
@@ -60,117 +62,227 @@ namespace SqlSugar
|
||||
public IDeleteable<T> AsDeleteable()
|
||||
{
|
||||
return Context.Deleteable<T>();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public T GetById(dynamic id)
|
||||
#region Method
|
||||
public virtual T GetById(dynamic id)
|
||||
{
|
||||
return Context.Queryable<T>().InSingle(id);
|
||||
}
|
||||
public List<T> GetList()
|
||||
public virtual List<T> GetList()
|
||||
{
|
||||
return Context.Queryable<T>().ToList();
|
||||
}
|
||||
|
||||
public List<T> GetList(Expression<Func<T, bool>> whereExpression)
|
||||
public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(whereExpression).ToList();
|
||||
}
|
||||
public T GetSingle(Expression<Func<T, bool>> whereExpression)
|
||||
public virtual T GetSingle(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().Single(whereExpression);
|
||||
}
|
||||
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
|
||||
public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
|
||||
{
|
||||
int count = 0;
|
||||
var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
{
|
||||
int count = 0;
|
||||
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
|
||||
public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
|
||||
{
|
||||
int count = 0;
|
||||
var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
{
|
||||
int count = 0;
|
||||
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public bool IsAny(Expression<Func<T, bool>> whereExpression)
|
||||
public virtual bool IsAny(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(whereExpression).Any();
|
||||
}
|
||||
public int Count(Expression<Func<T, bool>> whereExpression)
|
||||
public virtual int Count(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
|
||||
return Context.Queryable<T>().Where(whereExpression).Count();
|
||||
}
|
||||
|
||||
public bool Insert(T insertObj)
|
||||
public virtual bool Insert(T insertObj)
|
||||
{
|
||||
return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
|
||||
}
|
||||
public int InsertReturnIdentity(T insertObj)
|
||||
public virtual int InsertReturnIdentity(T insertObj)
|
||||
{
|
||||
return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
|
||||
}
|
||||
public bool InsertRange(T[] insertObjs)
|
||||
public virtual bool InsertRange(T[] insertObjs)
|
||||
{
|
||||
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool InsertRange(List<T> insertObjs)
|
||||
public virtual bool InsertRange(List<T> insertObjs)
|
||||
{
|
||||
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool Update(T updateObj)
|
||||
public virtual bool Update(T updateObj)
|
||||
{
|
||||
return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool UpdateRange(T[] updateObjs)
|
||||
public virtual bool UpdateRange(T[] updateObjs)
|
||||
{
|
||||
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool UpdateRange(List<T> updateObjs)
|
||||
public virtual bool UpdateRange(List<T> updateObjs)
|
||||
{
|
||||
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
|
||||
public virtual bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool Delete(T deleteObj)
|
||||
public virtual bool Delete(T deleteObj)
|
||||
{
|
||||
return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool Delete(Expression<Func<T, bool>> whereExpression)
|
||||
public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool DeleteById(dynamic id)
|
||||
public virtual bool DeleteById(dynamic id)
|
||||
{
|
||||
return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
|
||||
}
|
||||
public bool DeleteByIds(dynamic[] ids)
|
||||
public virtual bool DeleteByIds(dynamic[] ids)
|
||||
{
|
||||
return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Async Method
|
||||
public virtual Task<T> GetByIdAsync(dynamic id)
|
||||
{
|
||||
return Context.Queryable<T>().InSingleAsync(id);
|
||||
}
|
||||
public virtual Task<List<T>> GetListAsync()
|
||||
{
|
||||
return Context.Queryable<T>().ToListAsync();
|
||||
}
|
||||
|
||||
public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(whereExpression).ToListAsync();
|
||||
}
|
||||
public virtual Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().SingleAsync(whereExpression);
|
||||
}
|
||||
public virtual Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page)
|
||||
{
|
||||
RefAsync<int> count = 0;
|
||||
var result = Context.Queryable<T>().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public virtual Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
{
|
||||
RefAsync<int> count = 0;
|
||||
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public virtual Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page)
|
||||
{
|
||||
RefAsync<int> count = 0;
|
||||
var result = Context.Queryable<T>().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public virtual Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||
{
|
||||
RefAsync<int> count = 0;
|
||||
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
|
||||
page.PageCount = count;
|
||||
return result;
|
||||
}
|
||||
public virtual Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(whereExpression).AnyAsync();
|
||||
}
|
||||
public virtual Task<int> CountAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
|
||||
return Context.Queryable<T>().Where(whereExpression).CountAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<bool> InsertAsync(T insertObj)
|
||||
{
|
||||
return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual Task<int> InsertReturnIdentityAsync(T insertObj)
|
||||
{
|
||||
return this.Context.Insertable(insertObj).ExecuteReturnIdentityAsync();
|
||||
}
|
||||
public virtual async Task<bool> InsertRangeAsync(T[] insertObjs)
|
||||
{
|
||||
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> InsertRangeAsync(List<T> insertObjs)
|
||||
{
|
||||
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> UpdateAsync(T updateObj)
|
||||
{
|
||||
return await this.Context.Updateable(updateObj).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> UpdateRangeAsync(T[] updateObjs)
|
||||
{
|
||||
return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> UpdateRangeAsync(List<T> updateObjs)
|
||||
{
|
||||
return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return await this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteAsync(T deleteObj)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteByIdAsync(dynamic id)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteByIdsAsync(dynamic[] ids)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Obsolete("Use AsSugarClient()")]
|
||||
public ISqlSugarClient FullClient { get { return this.Context; } }
|
||||
}
|
||||
|
||||
|
||||
[Obsolete("Use SimpleClient<T>")]
|
||||
public partial class SimpleClient
|
||||
{
|
||||
protected ISqlSugarClient Context { get; set; }
|
||||
|
@@ -61,11 +61,6 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region SimpleClient
|
||||
public SimpleClient GetSimpleClient()
|
||||
{
|
||||
return this.Context.GetSimpleClient();
|
||||
}
|
||||
|
||||
public SimpleClient<T> GetSimpleClient<T>() where T : class, new()
|
||||
{
|
||||
return this.Context.GetSimpleClient<T>();
|
||||
@@ -901,6 +896,11 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Obsolete
|
||||
[Obsolete("Use GetSimpleClient<T>")]
|
||||
public SimpleClient GetSimpleClient()
|
||||
{
|
||||
return this.Context.GetSimpleClient();
|
||||
}
|
||||
[Obsolete("Use EntityMaintenance")]
|
||||
public EntityMaintenance EntityProvider { get { return this.Context.EntityProvider; } set { this.Context.EntityProvider = value; } }
|
||||
[Obsolete("Use Utilities")]
|
||||
|
@@ -59,6 +59,7 @@ namespace SqlSugar
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\)", "\\" + itemParameter.ParameterName), newName + ")", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\,", "\\" + itemParameter.ParameterName), newName + ",", RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"{0}$", "\\" + itemParameter.ParameterName), newName, RegexOptions.IgnoreCase);
|
||||
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0}\+", "\\" + itemParameter.ParameterName), "+"+newName+"+", RegexOptions.IgnoreCase);
|
||||
return itemSql;
|
||||
}
|
||||
internal static Type GetRootBaseType(Type entityType)
|
||||
|
Reference in New Issue
Block a user