Browse Source

Update core

pull/14/head
sunkaixuan 3 years ago
parent
commit
6f59b48faa
  1. 4
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs
  2. 3
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/Common/DateType.cs
  3. 1
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs
  4. 48
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs
  5. 6
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs
  6. 21
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs
  7. 79
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs
  8. 43
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs
  9. 7
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs
  10. 29
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs
  11. 31
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLExpressionContext.cs
  12. 28
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs

4
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/InsertableProvider/ParameterInsertable.cs

@ -98,6 +98,10 @@ namespace SqlSugar
var tableWithString = inserable.InsertBuilder.TableWithString;
var removeCacheFunc = inserable.RemoveCacheFunc;
var objects = inserable.InsertObjs;
if (objects == null || objects.Count() == 0 || (objects.Count() == 1 && objects.First() == null))
{
return result;
}
var identityList = inserable.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.PropertyName).ToArray();
if (inserable.IsOffIdentity)
{

3
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/Common/DateType.cs

@ -14,6 +14,7 @@ namespace SqlSugar
Hour = 4,
Second=5,
Minute=6,
Millisecond=7
Millisecond=7,
Weekday=8
}
}

1
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/Common/SugarParameter.cs

@ -136,6 +136,7 @@ namespace SqlSugar
else if (type!=null&&type.IsEnum())
{
this.DbType = System.Data.DbType.Int64;
this.Value = Convert.ToInt64(Value);
}
}

48
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs

@ -433,18 +433,18 @@ namespace SqlSugar
return string.Format("CHARINDEX ({0},{1})", model.Args[0].MemberName, model.Args[1].MemberName);
}
public string ToVarchar(MethodCallExpressionModel model)
public virtual string ToVarchar(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
return string.Format(" CAST({0} AS VARCHAR(MAX))", parameter.MemberName);
}
public string BitwiseAnd(MethodCallExpressionModel model)
public virtual string BitwiseAnd(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2 = model.Args[1];
return string.Format(" ({0} & {1}) ", parameter.MemberName, parameter2.MemberName); ;
}
public string BitwiseInclusiveOR(MethodCallExpressionModel model)
public virtual string BitwiseInclusiveOR(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2 = model.Args[1];
@ -471,24 +471,60 @@ namespace SqlSugar
return string.Format(" DATEDIFF({0},{1},{2}) ", parameter.MemberValue?.ToString().ToSqlFilter(), parameter2.MemberName, parameter3.MemberName); ;
}
public string Format(MethodCallExpressionModel model)
public virtual string Format(MethodCallExpressionModel model)
{
var str = model.Args[0].MemberValue.ObjToString();
var array = model.Args.Skip(1).Select(it => it.IsMember?it.MemberName:it.MemberValue).ToArray();
return string.Format("'"+str+ "'", array);
}
public string Abs(MethodCallExpressionModel model)
public virtual string Abs(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
return string.Format(" ABS({0}) ", parameter.MemberName);
}
public string Round(MethodCallExpressionModel model)
public virtual string Round(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2= model.Args[1];
return string.Format(" ROUND({0},{1}) ", parameter.MemberName, parameter2.MemberName);
}
public virtual string DateDiff(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2 = model.Args[1];
var parameter3 = model.Args[2];
return string.Format(" DATEDIFF({0},{1},{2}) ", parameter.MemberValue?.ToString().ToSqlFilter(), parameter2.MemberName, parameter3.MemberName);
}
public virtual string GreaterThan(MethodCallExpressionModel model)
{
//>
var parameter = model.Args[0];
var parameter2 = model.Args[1];
return string.Format(" ({0} > {1}) ", parameter.MemberName, parameter2.MemberName);
}
public virtual string GreaterThanOrEqual(MethodCallExpressionModel model)
{
//>=
var parameter = model.Args[0];
var parameter2 = model.Args[1];
return string.Format(" ({0} >= {1}) ", parameter.MemberName, parameter2.MemberName);
}
public virtual string LessThan(MethodCallExpressionModel model)
{
//<
var parameter = model.Args[0];
var parameter2 = model.Args[1];
return string.Format(" ({0} < {1}) ", parameter.MemberName, parameter2.MemberName);
}
public virtual string LessThanOrEqual(MethodCallExpressionModel model)
{
//<=
var parameter = model.Args[0];
var parameter2 = model.Args[1];
return string.Format(" ({0} <= {1}) ", parameter.MemberName, parameter2.MemberName);
}
}
}

6
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs

@ -75,5 +75,11 @@ namespace SqlSugar
string Format(MethodCallExpressionModel model);
string Abs(MethodCallExpressionModel model);
string Round(MethodCallExpressionModel model);
string DateDiff(MethodCallExpressionModel model);
string GreaterThan(MethodCallExpressionModel model);
string GreaterThanOrEqual(MethodCallExpressionModel model);
string LessThan(MethodCallExpressionModel model);
string LessThanOrEqual(MethodCallExpressionModel model);
}
}

21
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs

@ -9,6 +9,27 @@ namespace SqlSugar
{
public partial class SqlFunc
{
public static int DateDiff(DateType dateType,DateTime littleTime, DateTime bigTime)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool GreaterThan(object thisValue,object gtValue)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool GreaterThanOrEqual(object thisValue, object gtValue)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool LessThan(object thisValue, object ltValue)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool LessThanOrEqual(object thisValue, object ltValue)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool HasNumber(object thisValue)
{
return thisValue.ObjToInt() > 0;

79
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

@ -20,6 +20,14 @@ namespace SqlSugar
{
ResolveLength(parameter, isLeft, expression);
}
else if (IsDateDiff(expression))
{
ResolveDateDiff(parameter, isLeft, expression);
}
else if (expression.Member.Name== "DayOfWeek"&& expression.Type==typeof(DayOfWeek))
{
ResolveDayOfWeek(parameter, isLeft, expression);
}
else if (isHasValue)
{
ResolveHasValue(parameter, expression);
@ -63,6 +71,7 @@ namespace SqlSugar
}
#region Resolve default
private void ResolveDefault(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
{
@ -207,6 +216,65 @@ namespace SqlSugar
#endregion
#region Resolve special member
private void ResolveDayOfWeek(ExpressionParameter parameter, bool? isLeft, MemberExpression expression)
{
var exp = expression.Expression;
var value = GetNewExpressionValue(exp);
var result = this.Context.DbMehtods.DateValue(new MethodCallExpressionModel()
{
Args = new List<MethodCallExpressionArgs>() {
new MethodCallExpressionArgs(){
MemberName=value,
MemberValue=value
},
new MethodCallExpressionArgs(){
MemberName=DateType.Weekday,
MemberValue=DateType.Weekday
}
}
}); ;
base.AppendMember(parameter, isLeft, result);
}
private void ResolveDateDiff(ExpressionParameter parameter, bool? isLeft, MemberExpression expression)
{
var binaryExp=expression.Expression as BinaryExpression;
var beginExp = binaryExp.Right;
var endExp = binaryExp.Left;
var dateType = DateType.Day;
var begin = GetNewExpressionValue(beginExp);
var end = GetNewExpressionValue(endExp);
foreach (var item in UtilMethods.EnumToDictionary<DateType>())
{
if (expression.Member.Name.ToLower().Contains(item.Key.ToLower()))
{
dateType = item.Value;
break;
}
}
var result = this.Context.DbMehtods.DateDiff(new MethodCallExpressionModel()
{
Args = new List<MethodCallExpressionArgs>() {
new MethodCallExpressionArgs(){
MemberName=dateType,
MemberValue=dateType
},
new MethodCallExpressionArgs(){
MemberName=begin,
MemberValue=begin
},
new MethodCallExpressionArgs(){
MemberName=end,
MemberValue=end
}
}
}); ;
base.AppendMember(parameter, isLeft, result);
}
private void ResolveDateDateByCall(ExpressionParameter parameter, bool? isLeft, MemberExpression expression)
{
var value = GetNewExpressionValue(expression.Expression);
@ -441,6 +509,17 @@ namespace SqlSugar
#endregion
#region Helper
private static bool IsDateDiff(MemberExpression expression)
{
return
expression.Expression!=null&&
expression.Expression is BinaryExpression &&
expression.Expression.Type == UtilConstants.TimeSpanType&&
expression.Member.Name.StartsWith("Total")&&
expression.Member.Name.EndsWith("s")
;
}
private string AppendMember(ExpressionParameter parameter, bool? isLeft, string fieldName)
{
if (parameter.BaseExpression is BinaryExpression || (parameter.BaseParameter.CommonTempData != null && parameter.BaseParameter.CommonTempData.Equals(CommonTempDataType.Append)))

43
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

@ -559,6 +559,10 @@ namespace SqlSugar
{
parameter.CommonTempData = item.ToString();
}
else if (IsDateItemValue(item))
{
parameter.CommonTempData = GetNewExpressionValue(item);
}
else
{
base.Start();
@ -581,7 +585,7 @@ namespace SqlSugar
methodCallExpressionArgs.IsMember = false;
}
}
if (IsDateDate(item) || IsDateValue(item))
if (IsDateDate(item) || IsDateValue(item)|| IsDateItemValue(item))
{
methodCallExpressionArgs.IsMember = true;
}
@ -604,6 +608,33 @@ namespace SqlSugar
parameter.ChildExpression = null;
}
private bool IsDateItemValue(Expression item)
{
var result = false;
if (item is MemberExpression)
{
var memberExp = item as MemberExpression;
if (memberExp != null && memberExp.Expression != null && memberExp.Expression.Type == UtilConstants.DateType)
{
foreach (var dateType in UtilMethods.EnumToDictionary<DateType>())
{
if (memberExp.Member.Name.EqualCase(dateType.Key))
{
result = true;
break;
}
else if (memberExp.Member.Name=="DayOfWeek")
{
result = true;
break;
}
}
}
}
return result;
}
private static bool IsDateDate(Expression item)
{
return item.Type == UtilConstants.DateType && item is MemberExpression && (item as MemberExpression).Member.Name == "Date" && item.ToString() != "DateTime.Now.Date";
@ -856,6 +887,16 @@ namespace SqlSugar
return this.Context.DbMehtods.Abs(model);
case "Round":
return this.Context.DbMehtods.Round(model);
case "DateDiff":
return this.Context.DbMehtods.DateDiff(model);
case "GreaterThan":
return this.Context.DbMehtods.GreaterThan(model);
case "GreaterThanOrEqual":
return this.Context.DbMehtods.GreaterThanOrEqual(model);
case "LessThan":
return this.Context.DbMehtods.LessThan(model);
case "LessThanOrEqual":
return this.Context.DbMehtods.LessThanOrEqual(model);
default:
break;
}

7
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs

@ -14,6 +14,13 @@ namespace SqlSugar
}
public class MySqlMethod : DefaultDbMethod, IDbMethods
{
public override string DateDiff(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter2 = model.Args[1];
var parameter3 = model.Args[2];
return string.Format(" TIMESTAMPDIFF({0},{1},{2}) ", parameter.MemberValue?.ToString().ToSqlFilter(), parameter2.MemberName, parameter3.MemberName);
}
public override string DateValue(MethodCallExpressionModel model)
{
var parameter = model.Args[0];

29
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs

@ -53,6 +53,33 @@ namespace SqlSugar
}
public partial class OracleMethod : DefaultDbMethod, IDbMethods
{
public override string DateDiff(MethodCallExpressionModel model)
{
var parameter = (DateType)(Enum.Parse(typeof(DateType), model.Args[0].MemberValue.ObjToString()));
var begin = model.Args[1].MemberName;
var end = model.Args[2].MemberName;
switch (parameter)
{
case DateType.Year:
return $" ( cast((months_between( {end} , {begin}))/12 as number(9,0) ) )";
case DateType.Month:
return $" ( cast((months_between( {end} , {begin})) as number(9,0) ) )";
case DateType.Day:
return $" ( ROUND(TO_NUMBER({end} - {begin})) )";
case DateType.Hour:
return $" ( ROUND(TO_NUMBER({end} - {begin}) * 24) )";
case DateType.Minute:
return $" ( ROUND(TO_NUMBER({end} - {begin}) * 24 * 60) )";
case DateType.Second:
return $" ( ROUND(TO_NUMBER({end} - {begin}) * 24 * 60 * 60) )";
case DateType.Millisecond:
return $" ( ROUND(TO_NUMBER({end} - {begin}) * 24 * 60 * 60 * 60) )";
default:
break;
}
throw new Exception(parameter + " datediff no support");
}
private void PageEach<T>(IEnumerable<T> pageItems, int pageSize, Action<List<T>> action)
{
if (pageItems != null && pageItems.Any())
@ -126,6 +153,8 @@ namespace SqlSugar
return string.Format("(CAST(TO_CHAR({0},'mi') AS NUMBER))", parameter.MemberName);
case DateType.Millisecond:
return string.Format("(CAST(TO_CHAR({0},'ff3') AS NUMBER))", parameter.MemberName);
case DateType.Weekday:
return $" to_char({parameter.MemberName},'day') ";
case DateType.Day:
default:
return string.Format("(CAST(TO_CHAR({0},'dd') AS NUMBER))", parameter.MemberName);

31
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLExpressionContext.cs

@ -133,6 +133,32 @@ namespace SqlSugar
}
public class PostgreSQLMethod : DefaultDbMethod, IDbMethods
{
public override string DateDiff(MethodCallExpressionModel model)
{
var parameter = (DateType)(Enum.Parse(typeof(DateType), model.Args[0].MemberValue.ObjToString()));
var begin = model.Args[1].MemberName;
var end = model.Args[2].MemberName;
switch (parameter)
{
case DateType.Year:
return $" ( DATE_PART('Year', {end} ) - DATE_PART('Year', {begin}) )";
case DateType.Month:
return $" ( ( DATE_PART('Year', {end} ) - DATE_PART('Year', {begin}) ) * 12 + (DATE_PART('month', {end}) - DATE_PART('month', {begin})) )";
case DateType.Day:
return $" ( DATE_PART('day', {end} - {begin}) )";
case DateType.Hour:
return $" ( ( DATE_PART('day', {end} - {begin}) ) * 24 + DATE_PART('hour', {end} - {begin} ) )";
case DateType.Minute:
return $" ( ( ( DATE_PART('day', {end} - {begin}) ) * 24 + DATE_PART('hour', {end} - {begin} ) ) * 60 + DATE_PART('minute', {end} - {begin} ) )";
case DateType.Second:
return $" ( ( ( DATE_PART('day', {end} - {begin}) ) * 24 + DATE_PART('hour', {end} - {begin} ) ) * 60 + DATE_PART('minute', {end} - {begin} ) ) * 60 + DATE_PART('minute', {end} - {begin} )";
case DateType.Millisecond:
break;
default:
break;
}
throw new Exception(parameter + " datediff no support");
}
public override string IIF(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
@ -179,6 +205,11 @@ namespace SqlSugar
{
format = "ms";
}
if (parameter2.MemberValue.ObjToString() == DateType.Weekday.ToString())
{
return $" extract(DOW FROM cast({parameter.MemberName} as TIMESTAMP)) ";
}
return string.Format(" cast( to_char({1},'{0}')as integer ) ", format, parameter.MemberName);
}

28
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs

@ -14,6 +14,32 @@ namespace SqlSugar
}
public class SqliteMethod : DefaultDbMethod, IDbMethods
{
public override string DateDiff(MethodCallExpressionModel model)
{
var parameter = (DateType)(Enum.Parse(typeof(DateType), model.Args[0].MemberValue.ObjToString()));
var begin = model.Args[1].MemberName;
var end = model.Args[2].MemberName;
switch (parameter)
{
case DateType.Year:
return $" ( strftime('%Y',{end}) - strftime('%Y',{begin}) )";
case DateType.Month:
return $" ( (strftime('%m',{end}) - strftime('%m',{begin}))+(strftime('%Y',{end}) - strftime('%Y',{begin}))*12 )";
case DateType.Day:
return $" (julianday( strftime('%Y-%m-%d',datetime({end})) )-julianday(strftime('%Y-%m-%d',datetime({begin})))) ";
case DateType.Hour:
return $" ((julianday( strftime('%Y-%m-%d %H:%M',datetime({end})) )- julianday(strftime('%Y-%m-%d %H:%M',datetime({begin}))))*24 )";
case DateType.Minute:
return $" ((julianday( strftime('%Y-%m-%d %H:%M',datetime({end})) )- julianday(strftime('%Y-%m-%d %H:%M',datetime({begin}))))*24*60 )";
case DateType.Second:
return $" ((julianday( strftime('%Y-%m-%d %H:%M:%S',datetime({end})) )- julianday(strftime('%Y-%m-%d %H:%M:%S',datetime({begin}))))*24*60*60 )";
case DateType.Millisecond:
break;
default:
break;
}
throw new Exception(parameter + " datediff no support");
}
public override string Length(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
@ -158,6 +184,8 @@ namespace SqlSugar
case DateType.Minute:
parameter2 = "%M";
break;
case DateType.Weekday:
return $" cast (strftime('%w', {parameter.MemberName}) as integer) ";
case DateType.Millisecond:
default:
Check.ThrowNotSupportedException(typeName);

Loading…
Cancel
Save