mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 20:27:56 +08:00
Update Core
This commit is contained in:
parent
c2268706ec
commit
d3fa5a689b
@ -29,7 +29,7 @@ namespace SqlSugar
|
||||
public bool IsAs { get; set; }
|
||||
public bool IsEnableDiffLogEvent { get; set; }
|
||||
public DiffLogModel diffModel { get; set; }
|
||||
private Action RemoveCacheFunc { get; set; }
|
||||
internal Action RemoveCacheFunc { get; set; }
|
||||
|
||||
|
||||
#region Core
|
||||
@ -222,9 +222,44 @@ namespace SqlSugar
|
||||
After(sql, result);
|
||||
return result;
|
||||
}
|
||||
public OracleBlukCopy UseOracle()
|
||||
|
||||
{
|
||||
|
||||
PreToSql();
|
||||
|
||||
var currentType = this.Context.CurrentConnectionConfig.DbType;
|
||||
|
||||
Check.Exception(currentType != DbType.Oracle, "UseSqlServer no support " + currentType);
|
||||
|
||||
OracleBlukCopy result = new OracleBlukCopy();
|
||||
|
||||
result.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
|
||||
result.Builder = this.SqlBuilder;
|
||||
|
||||
result.Context = this.Context;
|
||||
|
||||
result.Inserts = this.InsertObjs;
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Setting
|
||||
|
||||
public IParameterInsertable<T> UseParameter()
|
||||
{
|
||||
var result = new ParameterInsertable<T>();
|
||||
result.Context= this.Context;
|
||||
result.Inserable = this;
|
||||
return result;
|
||||
}
|
||||
public IInsertable<T> AS(string tableName)
|
||||
{
|
||||
if (tableName == null) return this;
|
||||
@ -301,16 +336,16 @@ namespace SqlSugar
|
||||
};
|
||||
return this;
|
||||
}
|
||||
public MySqlBlueCopy<T> UseMySql()
|
||||
public MySqlBlukCopy<T> UseMySql()
|
||||
{
|
||||
return new MySqlBlueCopy<T>(this.Context, this.SqlBuilder, InsertObjs);
|
||||
return new MySqlBlukCopy<T>(this.Context, this.SqlBuilder, InsertObjs);
|
||||
}
|
||||
public SqlServerBlueCopy UseSqlServer()
|
||||
public SqlServerBlukCopy UseSqlServer()
|
||||
{
|
||||
PreToSql();
|
||||
var currentType = this.Context.CurrentConnectionConfig.DbType;
|
||||
Check.Exception(currentType != DbType.SqlServer, "UseSqlServer no support " + currentType);
|
||||
SqlServerBlueCopy result = new SqlServerBlueCopy();
|
||||
SqlServerBlukCopy result = new SqlServerBlukCopy();
|
||||
result.DbColumnInfoList =this.InsertBuilder.DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
result.Builder = this.SqlBuilder;
|
||||
@ -768,6 +803,7 @@ namespace SqlSugar
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ParameterInsertable<T> : IParameterInsertable<T> where T:class,new()
|
||||
{
|
||||
internal IInsertable<T> Inserable { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
public int ExecuteCommand()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType.IsIn(DbType.Oracle, DbType.Dm))
|
||||
{
|
||||
return DefaultExecuteCommand();
|
||||
}
|
||||
else
|
||||
{
|
||||
return ValuesExecuteCommand();
|
||||
}
|
||||
}
|
||||
public async Task<int> ExecuteCommandAsync()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType.IsIn(DbType.Oracle, DbType.Dm))
|
||||
{
|
||||
return await DefaultExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
return await ValuesExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
public int DefaultExecuteCommand()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns= inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it=>it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
this.Context.Utilities.PageEach(objects, 60, pagelist =>
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
this.Context.AddQueue("begin");
|
||||
foreach (var item in pagelist)
|
||||
{
|
||||
var itemable = this.Context.Insertable(item);
|
||||
itemable.InsertBuilder.DbColumnInfoList = itemable.InsertBuilder.DbColumnInfoList.Where(it => columns.Contains(it.DbColumnName)).ToList();
|
||||
itemable.InsertBuilder.TableWithString = tableWithString;
|
||||
(itemable as InsertableProvider<T>).RemoveCacheFunc = removeCacheFunc;
|
||||
itemable.AddQueue();
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
this.Context.AddQueue("end \r\n");
|
||||
result +=this.Context.SaveQueues(false);
|
||||
});
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
return objects.Length;
|
||||
return result;
|
||||
}
|
||||
public async Task<int> DefaultExecuteCommandAsync()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns = inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it => it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
await this.Context.Utilities.PageEachAsync<T,int>(objects, 60,async pagelist =>
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
this.Context.AddQueue("Begin");
|
||||
foreach (var item in pagelist)
|
||||
{
|
||||
var itemable = this.Context.Insertable(item);
|
||||
itemable.InsertBuilder.DbColumnInfoList = itemable.InsertBuilder.DbColumnInfoList.Where(it => columns.Contains(it.DbColumnName)).ToList();
|
||||
itemable.InsertBuilder.TableWithString = tableWithString;
|
||||
(itemable as InsertableProvider<T>).RemoveCacheFunc = removeCacheFunc;
|
||||
itemable.AddQueue();
|
||||
}
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
this.Context.AddQueue("End");
|
||||
result += await this.Context.SaveQueuesAsync(false);
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
return objects.Length;
|
||||
return result;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
public int ValuesExecuteCommand()
|
||||
{
|
||||
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns = inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it => it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray();
|
||||
this.Context.Utilities.PageEach(objects, 100, pagelist =>
|
||||
{
|
||||
|
||||
StringBuilder batchInsetrSql;
|
||||
List<SugarParameter> allParamter=new List<SugarParameter>();
|
||||
GetInsertValues(identityList,columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter);
|
||||
result += this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(), allParamter);
|
||||
|
||||
});
|
||||
return result;
|
||||
|
||||
}
|
||||
public async Task<int> ValuesExecuteCommandAsync()
|
||||
{
|
||||
int result = 0;
|
||||
var inserable = Inserable as InsertableProvider<T>;
|
||||
var columns = inserable.InsertBuilder.DbColumnInfoList.GroupBy(it => it.DbColumnName).Select(it => it.Key).Distinct().ToList();
|
||||
var tableWithString = inserable.InsertBuilder.TableWithString;
|
||||
var removeCacheFunc = inserable.RemoveCacheFunc;
|
||||
var objects = inserable.InsertObjs;
|
||||
var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray();
|
||||
await this.Context.Utilities.PageEachAsync(objects, 100,async pagelist =>
|
||||
{
|
||||
|
||||
StringBuilder batchInsetrSql;
|
||||
List<SugarParameter> allParamter = new List<SugarParameter>();
|
||||
GetInsertValues(identityList, columns, tableWithString, removeCacheFunc, pagelist, out batchInsetrSql, allParamter);
|
||||
result +=await this.Context.Ado.ExecuteCommandAsync(batchInsetrSql.ToString(), allParamter);
|
||||
|
||||
});
|
||||
return result;
|
||||
}
|
||||
#region Values Helper
|
||||
private void GetInsertValues(string[] identitys, List<string> columns, string tableWithString, Action removeCacheFunc, List<T> items, out StringBuilder batchInsetrSql, List<SugarParameter> allParamter)
|
||||
{
|
||||
var itemable = this.Context.Insertable(items);
|
||||
itemable.InsertBuilder.DbColumnInfoList = itemable.InsertBuilder.DbColumnInfoList.Where(it => columns.Contains(it.DbColumnName)).ToList();
|
||||
itemable.InsertBuilder.TableWithString = tableWithString;
|
||||
(itemable as InsertableProvider<T>).RemoveCacheFunc = removeCacheFunc;
|
||||
batchInsetrSql = new StringBuilder();
|
||||
batchInsetrSql.Append("INSERT INTO " + itemable.InsertBuilder.GetTableNameString + " ");
|
||||
batchInsetrSql.Append("(");
|
||||
var groupList = itemable.InsertBuilder.DbColumnInfoList.Where(it => !identitys.Contains(it.PropertyName)).GroupBy(it => it.TableId).ToList();
|
||||
string columnsString = string.Join(",", groupList.First().Select(it => itemable.InsertBuilder.Builder.GetTranslationColumnName(it.DbColumnName)));
|
||||
batchInsetrSql.Append(columnsString);
|
||||
batchInsetrSql.Append(") VALUES");
|
||||
string insertColumns = "";
|
||||
foreach (var gitem in groupList)
|
||||
{
|
||||
batchInsetrSql.Append("(");
|
||||
insertColumns = string.Join(",", gitem.Select(it => FormatValue(it.DbColumnName, it.Value, allParamter, itemable.InsertBuilder.Builder.SqlParameterKeyWord)));
|
||||
batchInsetrSql.Append(insertColumns);
|
||||
if (groupList.Last() == gitem)
|
||||
{
|
||||
batchInsetrSql.Append(") ");
|
||||
}
|
||||
else
|
||||
{
|
||||
batchInsetrSql.Append("), ");
|
||||
}
|
||||
}
|
||||
}
|
||||
private string FormatValue(string name, object value, List<SugarParameter> allParamter, string keyword)
|
||||
{
|
||||
var result = keyword + name + allParamter.Count;
|
||||
allParamter.Add(new SugarParameter(result, value));
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1794,7 +1794,7 @@ namespace SqlSugar
|
||||
protected List<TResult> _ToList<TResult>()
|
||||
{
|
||||
List<TResult> result = null;
|
||||
var sqlObj = this.ToSql();
|
||||
var sqlObj = this._ToSql();
|
||||
if (IsCache)
|
||||
{
|
||||
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
|
||||
@ -2419,6 +2419,7 @@ namespace SqlSugar
|
||||
asyncQueryableBuilder.AsTables = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AsTables);
|
||||
asyncQueryableBuilder.DisableTop = this.QueryBuilder.DisableTop;
|
||||
asyncQueryableBuilder.Offset = this.QueryBuilder.Offset;
|
||||
asyncQueryableBuilder.IsSqlQuery = this.QueryBuilder.IsSqlQuery;
|
||||
}
|
||||
protected int SetCacheTime(int cacheDurationInSeconds)
|
||||
{
|
||||
|
@ -35,6 +35,7 @@ namespace SqlSugar
|
||||
#region Splicing basic
|
||||
public List<string> IgnoreColumns { get; set; }
|
||||
public bool IsCount { get; set; }
|
||||
public bool IsSqlQuery { get; set; }
|
||||
public int? Skip { get; set; }
|
||||
public int ExternalPageIndex { get; set; }
|
||||
public int ExternalPageSize { get; set; }
|
||||
@ -475,6 +476,24 @@ namespace SqlSugar
|
||||
{
|
||||
return Regex.IsMatch(sql, @"AS \[\w+\.\w+\]");
|
||||
}
|
||||
public string GetSqlQuerySql(string result)
|
||||
{
|
||||
if (this.IsSqlQuery && (Skip == null && Take == null))
|
||||
{
|
||||
var old = result;
|
||||
var regex = @"^SELECT .* FROM \(((.|\n|\r)*)\) t $";
|
||||
if (this.Context.CurrentConnectionConfig.DbType .IsIn( DbType.MySql,DbType.PostgreSQL,DbType.Sqlite))
|
||||
{
|
||||
result = result.Substring(0,result.Length-1);
|
||||
}
|
||||
result = System.Text.RegularExpressions.Regex.Match(result,regex).Groups[1].Value;
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
result = old;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Get SQL Partial
|
||||
|
@ -306,11 +306,6 @@ namespace SqlSugar
|
||||
sqlBuilder.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
|
||||
sqlBuilder.Context = result.SqlBuilder.UpdateBuilder.Context = this;
|
||||
result.Init();
|
||||
var ignoreColumns = result.EntityInfo.Columns.Where(it => it.IsOnlyIgnoreUpdate).ToList();
|
||||
if (ignoreColumns!=null&&ignoreColumns.Any())
|
||||
{
|
||||
result = (UpdateableProvider<T>)result.IgnoreColumns(ignoreColumns.Select(it=>it.PropertyName).ToArray());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -397,7 +397,9 @@ namespace SqlSugar
|
||||
public virtual ISugarQueryable<T> Queryable<T>(ISugarQueryable<T> queryable) where T : class, new()
|
||||
{
|
||||
var sqlobj = queryable.ToSql();
|
||||
return this.SqlQueryable<T>(sqlobj.Key).AddParameters(sqlobj.Value);
|
||||
var result = this.SqlQueryable<T>(sqlobj.Key).AddParameters(sqlobj.Value);
|
||||
result.QueryBuilder.IsSqlQuery = false;
|
||||
return result;
|
||||
}
|
||||
public virtual ISugarQueryable<T, T2> Queryable<T, T2>(
|
||||
ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, Expression<Func<T, T2, bool>> joinExpression) where T : class, new() where T2 : class, new()
|
||||
@ -578,7 +580,9 @@ namespace SqlSugar
|
||||
public ISugarQueryable<T> SqlQueryable<T>(string sql) where T : class, new()
|
||||
{
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
|
||||
return this.Context.Queryable<T>().AS(sqlBuilder.GetPackTable(sql, sqlBuilder.GetDefaultShortName())).With(SqlWith.Null).Select(sqlBuilder.GetDefaultShortName() + ".*");
|
||||
var result= this.Context.Queryable<T>().AS(sqlBuilder.GetPackTable(sql, sqlBuilder.GetDefaultShortName())).With(SqlWith.Null).Select(sqlBuilder.GetDefaultShortName() + ".*");
|
||||
result.QueryBuilder.IsSqlQuery = true;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -1048,10 +1052,10 @@ namespace SqlSugar
|
||||
{
|
||||
try
|
||||
{
|
||||
if (this.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
{
|
||||
throw new Exception("Oracle no support SaveQueues");
|
||||
}
|
||||
//if (this.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
//{
|
||||
// throw new Exception("Oracle no support SaveQueues");
|
||||
//}
|
||||
if (this.Queues == null || this.Queues.Count == 0) return default(T);
|
||||
isTran = isTran && this.Ado.Transaction == null;
|
||||
if (isTran) this.Ado.BeginTran();
|
||||
@ -1084,6 +1088,10 @@ namespace SqlSugar
|
||||
.TrimEnd('\r')
|
||||
.TrimEnd('\n')
|
||||
.TrimEnd(';') + ";";
|
||||
if (itemSql == "begin;" )
|
||||
{
|
||||
itemSql = itemSql.TrimEnd(';')+"\n";
|
||||
}
|
||||
sqlBuilder.AppendLine(itemSql);
|
||||
index++;
|
||||
}
|
||||
|
@ -454,6 +454,12 @@ namespace SqlSugar
|
||||
++i;
|
||||
}
|
||||
this.columns = this.UpdateBuilder.DbColumnInfoList;
|
||||
|
||||
var ignoreColumns = EntityInfo.Columns.Where(it => it.IsOnlyIgnoreUpdate).ToList();
|
||||
if (ignoreColumns != null && ignoreColumns.Any())
|
||||
{
|
||||
this.IgnoreColumns(ignoreColumns.Select(it => it.PropertyName).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private void DataAop(T item)
|
||||
|
@ -9,9 +9,11 @@ namespace SqlSugar
|
||||
public sealed class SnowFlakeSingle
|
||||
{
|
||||
public static readonly SnowFlakeSingle instance = new SnowFlakeSingle();
|
||||
public static int WorkId = 1;
|
||||
public static int DatacenterId = 1;
|
||||
private SnowFlakeSingle()
|
||||
{
|
||||
worker = new DistributedSystem.Snowflake.IdWorker(1, 1);
|
||||
worker = new DistributedSystem.Snowflake.IdWorker(WorkId, DatacenterId);
|
||||
}
|
||||
static SnowFlakeSingle() { }
|
||||
public static SnowFlakeSingle Instance
|
||||
|
@ -0,0 +1,25 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ValueToStringConverter : JsonConverter
|
||||
{
|
||||
public override bool CanRead => false;
|
||||
|
||||
public override bool CanConvert(Type objectType) => objectType.IsValueType;
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
var str = value?.ToString();
|
||||
writer.WriteValue(str);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
|
@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
@ -638,6 +638,33 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PageEachAsync<T>(IEnumerable<T> pageItems, int pageSize, Func<List<T>,Task> action)
|
||||
{
|
||||
if (pageItems != null && pageItems.Any())
|
||||
{
|
||||
int totalRecord = pageItems.Count();
|
||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||
for (int i = 1; i <= pageCount; i++)
|
||||
{
|
||||
var list = pageItems.Skip((i - 1) * pageSize).Take(pageSize).ToList();
|
||||
await action(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
public async Task PageEachAsync<T, ResultType>(IEnumerable<T> pageItems, int pageSize, Func<List<T>, Task<ResultType>> action)
|
||||
{
|
||||
if (pageItems != null && pageItems.Any())
|
||||
{
|
||||
int totalRecord = pageItems.Count();
|
||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||
for (int i = 1; i <= pageCount; i++)
|
||||
{
|
||||
var list = pageItems.Skip((i - 1) * pageSize).Take(pageSize).ToList();
|
||||
await action(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
@ -35,5 +35,7 @@ namespace SqlSugar
|
||||
void RemoveCacheAll<T>();
|
||||
void RemoveCache<T>(string key);
|
||||
void PageEach<T>(IEnumerable<T> pageItems, int pageSize, Action<List<T>> action);
|
||||
Task PageEachAsync<T>(IEnumerable<T> pageItems, int pageSize, Func<List<T>, Task> action);
|
||||
Task PageEachAsync<T, ResultType>(IEnumerable<T> pageItems, int pageSize, Func<List<T>, Task<ResultType>> action);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IParameterInsertable<T>
|
||||
{
|
||||
int ExecuteCommand();
|
||||
Task<int> ExecuteCommandAsync();
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
@ -35,15 +35,16 @@ namespace SqlSugar
|
||||
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||
|
||||
IParameterInsertable<T> UseParameter();
|
||||
IInsertable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||
|
||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IInsertable<T> RemoveDataCache();
|
||||
IInsertable<T> RemoveDataCache(string likeString);
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
SqlServerBlueCopy UseSqlServer();
|
||||
MySqlBlueCopy<T> UseMySql();
|
||||
SqlServerBlukCopy UseSqlServer();
|
||||
MySqlBlukCopy<T> UseMySql();
|
||||
OracleBlukCopy UseOracle();
|
||||
void AddQueue();
|
||||
|
||||
}
|
||||
|
@ -48,6 +48,7 @@ namespace SqlSugar
|
||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
|
||||
|
@ -9,29 +9,29 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MySqlBlueCopy<T>
|
||||
public class MySqlBlukCopy<T>
|
||||
{
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal ISqlBuilder Builder { get; set; }
|
||||
internal T[] Entitys { get; set; }
|
||||
internal string Chara { get; set; }
|
||||
private MySqlBlueCopy()
|
||||
private MySqlBlukCopy()
|
||||
{
|
||||
|
||||
}
|
||||
public MySqlBlueCopy(SqlSugarProvider context, ISqlBuilder builder, T []entitys)
|
||||
public MySqlBlukCopy(SqlSugarProvider context, ISqlBuilder builder, T []entitys)
|
||||
{
|
||||
this.Context = context;
|
||||
this.Builder = builder;
|
||||
this.Entitys = entitys;
|
||||
}
|
||||
public bool ExecuteBlueCopy(string characterSet)
|
||||
public bool ExecuteBlukCopy(string characterSet)
|
||||
{
|
||||
this.Chara = characterSet;
|
||||
return ExecuteBlueCopy();
|
||||
return ExecuteBlukCopy();
|
||||
}
|
||||
|
||||
public bool ExecuteBlueCopy()
|
||||
public bool ExecuteBlukCopy()
|
||||
{
|
||||
var IsBulkLoad = false;
|
||||
if (Entitys == null || Entitys.Length <= 0)
|
||||
@ -111,15 +111,15 @@ namespace SqlSugar
|
||||
return IsBulkLoad; ;
|
||||
}
|
||||
|
||||
public Task<bool> ExecuteBlueCopyAsync()
|
||||
public Task<bool> ExecuteBlukCopyAsync()
|
||||
{
|
||||
return Task.FromResult(ExecuteBlueCopy());
|
||||
return Task.FromResult(ExecuteBlukCopy());
|
||||
}
|
||||
|
||||
public Task<bool> ExecuteBlueCopyAsync(string characterSet)
|
||||
public Task<bool> ExecuteBlukCopyAsync(string characterSet)
|
||||
{
|
||||
this.Chara = characterSet;
|
||||
return Task.FromResult(ExecuteBlueCopy());
|
||||
return Task.FromResult(ExecuteBlukCopy());
|
||||
}
|
||||
|
||||
#region Helper
|
@ -61,6 +61,7 @@ namespace SqlSugar
|
||||
result = sql.ToString();
|
||||
}
|
||||
this.OrderByValue = oldOrderValue;
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
private string ToCountSqlString()
|
||||
|
@ -0,0 +1,318 @@
|
||||
using System;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Data;
|
||||
|
||||
using Oracle.ManagedDataAccess.Client;
|
||||
|
||||
using System.Linq;
|
||||
|
||||
using System.Text;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
|
||||
public class OracleBlukCopy
|
||||
|
||||
{
|
||||
|
||||
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
|
||||
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
|
||||
internal ISqlBuilder Builder { get; set; }
|
||||
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
|
||||
internal object[] Inserts { get; set; }
|
||||
|
||||
|
||||
|
||||
public int ExecuteBlueCopy()
|
||||
|
||||
{
|
||||
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
|
||||
|
||||
if (Inserts.First().GetType() == typeof(DataTable))
|
||||
|
||||
{
|
||||
|
||||
return WriteToServer();
|
||||
|
||||
}
|
||||
|
||||
DataTable dt = GetCopyData();
|
||||
|
||||
OracleBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
|
||||
try
|
||||
|
||||
{
|
||||
|
||||
bulkCopy.WriteToServer(dt);
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
|
||||
{
|
||||
|
||||
CloseDb();
|
||||
|
||||
throw ex;
|
||||
|
||||
}
|
||||
|
||||
CloseDb();
|
||||
|
||||
return DbColumnInfoList.Count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<int> ExecuteBlueCopyAsync()
|
||||
|
||||
{
|
||||
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
|
||||
|
||||
if (Inserts.First().GetType() == typeof(DataTable))
|
||||
|
||||
{
|
||||
|
||||
return WriteToServer();
|
||||
|
||||
}
|
||||
|
||||
DataTable dt = GetCopyData();
|
||||
|
||||
OracleBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
|
||||
try
|
||||
|
||||
{
|
||||
|
||||
await Task.Run(() => bulkCopy.WriteToServer(dt));
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
|
||||
{
|
||||
|
||||
CloseDb();
|
||||
|
||||
throw ex;
|
||||
|
||||
}
|
||||
|
||||
CloseDb();
|
||||
|
||||
return DbColumnInfoList.Count;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int WriteToServer()
|
||||
|
||||
{
|
||||
|
||||
var dt = this.Inserts.First() as DataTable;
|
||||
|
||||
if (dt == null)
|
||||
|
||||
return 0;
|
||||
|
||||
Check.Exception(dt.TableName == "Table", "dt.TableName can't be null ");
|
||||
|
||||
dt = GetCopyWriteDataTable(dt);
|
||||
|
||||
OracleBulkCopy copy = GetBulkCopyInstance();
|
||||
|
||||
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
|
||||
|
||||
copy.WriteToServer(dt);
|
||||
|
||||
CloseDb();
|
||||
|
||||
return dt.Rows.Count;
|
||||
|
||||
}
|
||||
|
||||
private DataTable GetCopyWriteDataTable(DataTable dt)
|
||||
|
||||
{
|
||||
|
||||
var result = this.Context.Ado.GetDataTable("select * from " + this.Builder.GetTranslationColumnName(dt.TableName) + " where 1 > 2 ");
|
||||
|
||||
foreach (DataRow item in dt.Rows)
|
||||
|
||||
{
|
||||
|
||||
DataRow dr = result.NewRow();
|
||||
|
||||
foreach (DataColumn column in result.Columns)
|
||||
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (dt.Columns.Cast<DataColumn>().Select(it => it.ColumnName.ToLower()).Contains(column.ColumnName.ToLower()))
|
||||
|
||||
{
|
||||
|
||||
dr[column.ColumnName] = item[column.ColumnName];
|
||||
|
||||
if (dr[column.ColumnName] == null)
|
||||
|
||||
{
|
||||
|
||||
dr[column.ColumnName] = DBNull.Value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
result.Rows.Add(dr);
|
||||
|
||||
}
|
||||
|
||||
result.TableName = dt.TableName;
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private OracleBulkCopy GetBulkCopyInstance()
|
||||
|
||||
{
|
||||
|
||||
OracleBulkCopy copy;
|
||||
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
|
||||
{
|
||||
|
||||
copy = new OracleBulkCopy((OracleConnection)this.Context.Ado.Connection, Oracle.ManagedDataAccess.Client.OracleBulkCopyOptions.Default);
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
|
||||
{
|
||||
|
||||
copy = new OracleBulkCopy((OracleConnection)this.Context.Ado.Connection, OracleBulkCopyOptions.UseInternalTransaction);
|
||||
|
||||
}
|
||||
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
|
||||
{
|
||||
|
||||
this.Context.Ado.Connection.Open();
|
||||
|
||||
}
|
||||
|
||||
return copy;
|
||||
|
||||
}
|
||||
|
||||
private DataTable GetCopyData()
|
||||
|
||||
{
|
||||
|
||||
var dt = this.Context.Ado.GetDataTable("select * from " + InsertBuilder.GetTableNameString + " where 1 > 2 ");
|
||||
|
||||
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("1900/01/01");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (value.Value == null)
|
||||
|
||||
{
|
||||
|
||||
value.Value = DBNull.Value;
|
||||
|
||||
}
|
||||
|
||||
dr[item.ColumnName] = value.Value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dt.Rows.Add(dr);
|
||||
|
||||
}
|
||||
|
||||
return dt;
|
||||
|
||||
}
|
||||
|
||||
private void CloseDb()
|
||||
|
||||
{
|
||||
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
|
||||
{
|
||||
|
||||
this.Context.Ado.Connection.Close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -51,6 +51,7 @@ namespace SqlSugar
|
||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
|
||||
|
@ -61,6 +61,7 @@ namespace SqlSugar
|
||||
result = sql.ToString();
|
||||
}
|
||||
this.OrderByValue = oldOrderValue;
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerBlueCopy
|
||||
public class SqlServerBlukCopy
|
||||
{
|
||||
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
@ -16,7 +16,7 @@ namespace SqlSugar
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
internal object[] Inserts { get; set; }
|
||||
|
||||
public int ExecuteBlueCopy()
|
||||
public int ExecuteBlukCopy()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
@ -40,7 +40,7 @@ namespace SqlSugar
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
public async Task<int> ExecuteBlueCopyAsync()
|
||||
public async Task<int> ExecuteBlukCopyAsync()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
@ -28,7 +28,7 @@ namespace SqlSugar
|
||||
{
|
||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||
}
|
||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1&&DisableTop==false;
|
||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
|
||||
var isRowNumber = (Skip != null || Take != null) && !isFirst;
|
||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||
string groupByValue = GetGroupByString + HavingInfos;
|
||||
@ -48,14 +48,15 @@ namespace SqlSugar
|
||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
if (!string.IsNullOrEmpty(this.Offset))
|
||||
if (!string.IsNullOrEmpty(this.Offset))
|
||||
{
|
||||
if (this.OrderByValue.IsNullOrEmpty())
|
||||
if (this.OrderByValue.IsNullOrEmpty())
|
||||
{
|
||||
result += " ORDER BY GETDATE() ";
|
||||
}
|
||||
result += this.Offset;
|
||||
}
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,7 @@ namespace SqlSugar
|
||||
result= sql.ToString();
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -327,7 +327,13 @@ namespace SqlSugar
|
||||
|
||||
public ISugarQueryable<T> Queryable<T>(ISugarQueryable<T> queryable) where T : class, new()
|
||||
{
|
||||
return this.Context.Queryable<T>(queryable);
|
||||
|
||||
var result= this.Context.Queryable<T>(queryable);
|
||||
var QueryBuilder = queryable.QueryBuilder;
|
||||
result.QueryBuilder.WhereIndex = QueryBuilder.WhereIndex++;
|
||||
result.QueryBuilder.LambdaExpressions.ParameterIndex = QueryBuilder.LambdaExpressions.ParameterIndex++;
|
||||
result.QueryBuilder.LambdaExpressions.Index = QueryBuilder.LambdaExpressions.Index++;
|
||||
return result;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Queryable<T>(string shortName)
|
||||
|
@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
Loading…
Reference in New Issue
Block a user