Update .net core project

This commit is contained in:
sunkaixuan
2022-09-10 21:19:05 +08:00
parent 8f488902a1
commit 1461248b97
14 changed files with 220 additions and 27 deletions

View File

@@ -433,7 +433,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
}
public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] parameters)
@@ -466,7 +466,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
finally
{
@@ -502,7 +502,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
finally
{
@@ -543,7 +543,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
finally
{
@@ -588,7 +588,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
}
public virtual async Task<object> GetScalarAsync(string sql, params SugarParameter[] parameters)
@@ -624,7 +624,7 @@ namespace SqlSugar
CommandType = CommandType.Text;
if (ErrorEvent != null)
ExecuteErrorEvent(sql, parameters, ex);
throw ex;
throw;
}
finally
{

View File

@@ -238,7 +238,20 @@ namespace SqlSugar
{
var result = InsertObjs.First();
var identityKeys = GetIdentityKeys();
if (identityKeys.Count == 0) { return this.ExecuteCommand() > 0; }
if (identityKeys.Count == 0)
{
var snowColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey = true && it.UnderType == UtilConstants.LongType);
if (snowColumn!=null)
{
var id = this.ExecuteReturnSnowflakeId();
snowColumn.PropertyInfo.SetValue(result, id);
return true;
}
else
{
return this.ExecuteCommand() > 0;
}
}
var idValue = ExecuteReturnBigIdentity();
Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
var identityKey = identityKeys.First();
@@ -290,7 +303,20 @@ namespace SqlSugar
{
var result = InsertObjs.First();
var identityKeys = GetIdentityKeys();
if (identityKeys.Count == 0) { return await this.ExecuteCommandAsync() > 0; }
if (identityKeys.Count == 0)
{
var snowColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey = true && it.UnderType == UtilConstants.LongType);
if (snowColumn != null)
{
var id =await this.ExecuteReturnSnowflakeIdAsync();
snowColumn.PropertyInfo.SetValue(result, id);
return true;
}
else
{
return await this.ExecuteCommandAsync() > 0;
}
}
var idValue =await ExecuteReturnBigIdentityAsync();
Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
var identityKey = identityKeys.First();

View File

@@ -408,7 +408,7 @@ namespace SqlSugar
Check.Exception(InstanceFactory.CustomDllName.IsNullOrEmpty(), "DbType.Custom: InstanceFactory.CustomDllName is not null ");
break;
case DbType.QuestDB:
Check.Exception(SugarCompatible.IsFramework, "QuestDB only support .net core");
DependencyManagement.TryPostgreSQL();
break;
case DbType.ClickHouse:
Check.Exception(SugarCompatible.IsFramework, "ClickHouse only support .net core");

View File

@@ -431,7 +431,7 @@ namespace SqlSugar
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
}
internal static List<NewExpressionInfo> GetNewexpressionInfos(Expression item,ExpressionContext context)
internal static List<NewExpressionInfo> GetNewexpressionInfos(Expression item,ExpressionContext context, BaseResolve baseResolve)
{
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
foreach (MemberBinding binding in ((MemberInitExpression)item).Bindings)
@@ -442,7 +442,7 @@ namespace SqlSugar
}
MemberAssignment memberAssignment = (MemberAssignment)binding;
NewExpressionInfo additem = new NewExpressionInfo();
if ((memberAssignment.Expression is MemberExpression))
if (memberAssignment.Expression is MemberExpression)
{
additem.LeftNameName = memberAssignment.Member.Name;
var member = (memberAssignment.Expression as MemberExpression).Expression;
@@ -451,27 +451,85 @@ namespace SqlSugar
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
result.Add(additem);
}
else if (memberAssignment.Expression is ConstantExpression)
{
var value = ((ConstantExpression)memberAssignment.Expression).Value;
//var leftInfo = keys[i];
additem.Type = nameof(ConstantExpression);
additem.RightName = memberAssignment.Member.Name;
additem.ShortName = memberAssignment.Member.Name;
additem.RightName = memberAssignment.Member.Name;
additem.LeftNameName = memberAssignment.Member.Name;
additem.RightDbName = UtilMethods.GetSqlValue(value);
//additem.Value = "";
result.Add(additem);
}
else
{
var value = baseResolve.GetNewExpressionValue(memberAssignment.Expression);
//var leftInfo = keys[i];
additem.Type = nameof(ConstantExpression);
additem.RightName = memberAssignment.Member.Name;
additem.ShortName = memberAssignment.Member.Name;
additem.RightName = memberAssignment.Member.Name;
additem.LeftNameName = memberAssignment.Member.Name;
additem.RightDbName = value;
//additem.Value = "";
result.Add(additem);
}
}
return result;
}
internal static List<NewExpressionInfo> GetNewDynamicexpressionInfos(Expression item, ExpressionContext context)
internal static List<NewExpressionInfo> GetNewDynamicexpressionInfos(Expression item, ExpressionContext context, BaseResolve baseResolve)
{
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
int i = 0;
foreach (var binding in ((NewExpression)item).Arguments)
{
NewExpressionInfo additem = new NewExpressionInfo();
var keys = ((NewExpression)item).Members;
if (binding is MemberExpression)
{
var member = (MemberExpression)binding;
var entityName = member.Expression?.Type?.Name;
//var memberAssignment = binding;
//NewExpressionInfo additem = new NewExpressionInfo();
additem.RightName = member.Member.Name;
additem.ShortName = member.Expression + "";
additem.RightName = member.Member.Name;
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
additem.RightDbName = context.GetDbColumnName(entityName, additem.RightName);
additem.LeftNameName = member.Member.Name;
//additem.Value = "";
result.Add(additem);
}
else if (binding is ConstantExpression)
{
var value = ((ConstantExpression)binding).Value;
var leftInfo = keys[i];
additem.Type = nameof(ConstantExpression);
additem.RightName = leftInfo.Name;
additem.ShortName = leftInfo.Name;
additem.RightName = leftInfo.Name;
additem.LeftNameName = leftInfo.Name;
additem.RightDbName = UtilMethods.GetSqlValue(value);
//additem.Value = "";
result.Add(additem);
}
else
{
var value = baseResolve.GetNewExpressionValue(binding);
var leftInfo = keys[i];
additem.Type = nameof(ConstantExpression);
additem.RightName = leftInfo.Name;
additem.ShortName = leftInfo.Name;
additem.RightName = leftInfo.Name;
additem.LeftNameName = leftInfo.Name;
additem.RightDbName = value;
//additem.Value = "";
result.Add(additem);
}
i++;
}
return result;
}

View File

@@ -12,5 +12,6 @@ namespace SqlSugar
public string RightName { get; set; }
public string RightDbName { get; set; }
public string ShortName { get; set; }
public string Type { get; set; }
}
}

View File

@@ -572,6 +572,31 @@ namespace SqlSugar
var parameter4 = model.Args[3];
return $" STUFF ({parameter1.MemberName}, {parameter2.MemberName}, {parameter3.MemberName}, {parameter4.MemberName}) ";
}
public virtual string Exists(MethodCallExpressionModel model)
{
var parameter1 = model.Args[0];
if (model.Args.Count > 1)
{
var parameter2 = model.Args[1];
if (UtilMethods.IsParentheses(parameter1.MemberName))
{
parameter1.MemberName = $" {parameter1.MemberName.ObjToString().Trim().TrimEnd(')')} AND {parameter2.MemberName}) ";
}
else
{
parameter1.MemberName = $" {parameter1.MemberName} AND {parameter2.MemberName} ";
}
}
if (UtilMethods.IsParentheses(parameter1.MemberName))
{
return $" Exists{parameter1.MemberName} ";
}
else
{
return $" Exists({parameter1.MemberName}) ";
}
}
public virtual string GetDateString(string dateValue, string format)
{
return null;

View File

@@ -89,7 +89,7 @@ namespace SqlSugar
string Stuff(MethodCallExpressionModel model);
string RowNumber(MethodCallExpressionModel model);
string RowCount(MethodCallExpressionModel model);
string Exists(MethodCallExpressionModel model);
string GetDateString(string dateValue,string format);
string GetForXmlPath();
}

View File

@@ -115,6 +115,23 @@ namespace SqlSugar
{
return thisValue.Equals(parameterValue);
}
public static bool Exists(string subQueryableName_Or_OneToOnePropertyName)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool Exists<valueType>(valueType subQueryableName_Or_OneToOnePropertyName) where valueType : struct
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool Exists(string subQueryableName_Or_OneToOnePropertyName, List<IConditionalModel> conditionalModels)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool Exists<valueType>(valueType subQueryableName_Or_OneToOnePropertyName, List<IConditionalModel> conditionalModels) where valueType : struct
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool DateIsSame(DateTime date1, DateTime date2)
{
return date1.ToString("yyyy-MM-dd") == date2.ToString("yyyy-MM-dd");

View File

@@ -480,11 +480,11 @@ namespace SqlSugar
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
if (item is MemberInitExpression)
{
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item,this.Context);
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item,this.Context,this);
}
else
{
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context);
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context,this);
}
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
{
@@ -501,26 +501,35 @@ namespace SqlSugar
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
if (item is MemberInitExpression)
{
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item, this.Context);
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item, this.Context,this);
}
else
{
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context);
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context,this);
}
mappingKeys = new Dictionary<string, string>();
//mappingKeys = new Dictionary<string, string>();
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
{
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
mappingKeys.Add("Single_"+newExpressionInfo.LeftNameName,asName + "." + newExpressionInfo.RightDbName );
mappingKeys.Add("Single_" + newExpressionInfo.LeftNameName, asName + "." + newExpressionInfo.LeftNameName);
if (newExpressionInfo.Type == nameof(ConstantExpression))
{
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
parameter.Context.Result.Append($" {newExpressionInfo.RightDbName} AS { this.Context.SqlTranslationLeft}{asName}.{newExpressionInfo.LeftNameName}{ this.Context.SqlTranslationRight} ");
}
else
{
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
parameter.Context.Result.Append(this.Context.GetAsString(
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.RightDbName + this.Context.SqlTranslationRight,
this.Context.SqlTranslationLeft + asName + "." + newExpressionInfo.LeftNameName + this.Context.SqlTranslationRight,
newExpressionInfo.RightDbName
));
}
}
}
else
{
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);

View File

@@ -1018,6 +1018,16 @@ namespace SqlSugar
return this.Context.DbMehtods.RowNumber(model);
case "RowCount":
return this.Context.DbMehtods.RowCount(model);
case "Exists":
if (model.Args.Count > 1)
{
this.Context.Parameters.RemoveAll(it => model.Args[1].MemberName.ObjToString().Contains(it.ParameterName) );
List<IConditionalModel> conditionalModels = (List<IConditionalModel>) model.Args[1].MemberValue;
var sqlObj = this.Context.SugarContext.Context.Queryable<object>().SqlBuilder.ConditionalModelToSql(conditionalModels, 0);
model.Args[1].MemberName = sqlObj.Key;
this.Context.Parameters.AddRange(sqlObj.Value);
}
return this.Context.DbMehtods.Exists(model);
default:
break;
}

View File

@@ -44,6 +44,7 @@ namespace SqlSugar
long InsertReturnBigIdentity(T insertObj);
long InsertReturnSnowflakeId(T insertObj);
List<long> InsertReturnSnowflakeId(List<T> insertObjs);
T InsertReturnEntity(T insertObj);
bool IsAny(Expression<Func<T, bool>> whereExpression);
@@ -79,6 +80,7 @@ namespace SqlSugar
Task<long> InsertReturnBigIdentityAsync(T insertObj);
Task<long> InsertReturnSnowflakeIdAsync(T insertObj);
Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs);
Task<T> InsertReturnEntityAsync(T insertObj);
Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);

View File

@@ -465,6 +465,7 @@ namespace SqlSugar
});
if (!GetDataBaseList(newDb).Any(it => it.Equals(databaseName, StringComparison.CurrentCultureIgnoreCase)))
{
var separatorChar = UtilMethods.GetSeparatorChar();
var sql = CreateDataBaseSql;
if (databaseDirectory.HasValue())
{
@@ -491,11 +492,16 @@ namespace SqlSugar
maxsize = 1gb,
filegrowth = 10mb
); ";
databaseDirectory = databaseDirectory.Replace("\\", separatorChar);
}
if (databaseName.Contains("."))
{
databaseName = $"[{databaseName}]";
}
else if (Regex.IsMatch(databaseName,@"^\d.*"))
{
databaseName = $"[{databaseName}]";
}
newDb.Ado.ExecuteCommand(string.Format(sql, databaseName, databaseDirectory));
}
return true;

View File

@@ -182,6 +182,12 @@ namespace SqlSugar
{
return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
}
public virtual T InsertReturnEntity(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnEntity();
}
public virtual bool InsertRange(T[] insertObjs)
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
@@ -308,6 +314,10 @@ namespace SqlSugar
{
return this.Context.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
}
public virtual async Task<T> InsertReturnEntityAsync(T insertObj)
{
return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync();
}
public virtual async Task<bool> InsertRangeAsync(T[] insertObjs)
{
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;

View File

@@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@@ -16,6 +17,14 @@ namespace SqlSugar
{
public class UtilMethods
{
public static string GetSeparatorChar()
{
return Path.Combine("a", "a").Replace("a", "");
}
public static bool IsParentheses(object name)
{
return name.ObjToString().Trim().Last() == ')' && name.ObjToString().Trim().First() == '(';
}
internal static bool IsDefaultValue(object value)
{
@@ -530,6 +539,26 @@ namespace SqlSugar
}
}
public static string GetSqlValue(object value)
{
if (value == null)
{
return "null";
}
else if (UtilMethods.IsNumber(value.GetType().Name))
{
return value.ObjToString();
}
else if (value is DateTime)
{
return UtilMethods.GetConvertValue(value) + "";
}
else
{
return value.ToSqlValue();
}
}
public static void DataInoveByExpresson<Type>(Type[] datas, MethodCallExpression callExpresion)
{
var methodInfo = callExpresion.Method;