protected override SqlGenerate Where(MemberExpression expression, SqlGenerate sqlGenerate) { if (expression.Expression != null) { if (expression.Member.DeclaringType.IsNullableType()) { if (expression.Member.Name == "Value") //Can't use C# 6 yet: nameof(Nullable<bool>.Value) { SqlExpressionProvider.Where(expression.Expression, sqlGenerate); return(sqlGenerate); } if (expression.Member.Name == "HasValue") { var doesNotEqualNull = Expression.MakeBinary(ExpressionType.NotEqual, expression.Expression, Expression.Constant(null)); SqlExpressionProvider.Where(doesNotEqualNull, sqlGenerate); return(sqlGenerate); } throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>"); } if (expression.IsParameterOrConvertAccess()) { sqlGenerate += $" {expression.Member.Name.ParamSql(sqlGenerate)}"; return(sqlGenerate); } } var val = SqlExpressionCompiler.Evaluate(expression); sqlGenerate.AddDbParameter(val); return(sqlGenerate); }
/// <summary> /// OrderBy /// </summary> /// <param name="orderBy"></param> /// <returns></returns> public SqlExpressionCore <T> OrderBy(string orderBy) { if (string.IsNullOrEmpty(orderBy)) { var keyAttr = typeof(T).GetProperty <KeyAttribute>(); if (keyAttr == null) { throw new Exception("实体未设置Key属性"); } orderBy = $"order by {keyAttr.Name} desc"; } if (!orderBy.StartsWith("order by")) { orderBy = $"order by {orderBy}"; } switch (sqlGenerate.DatabaseType) { case DatabaseType.SqlServer: sqlGenerate.Sql.Replace("select", $"select row_number() over({orderBy}) as RowNumber,"); break; case DatabaseType.MySql: sqlGenerate += $"{Environment.NewLine}{orderBy}"; break; case DatabaseType.SQLite: sqlGenerate += $"{Environment.NewLine}{orderBy}"; break; } return(this); }
protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate) { var result = SqlExpressionCompiler.Evaluate(expression); var inArgs = (result as IEnumerable).Flatten(); sqlGenerate += "("; if (inArgs?.Count > 0) { foreach (var item in inArgs) { sqlGenerate.AddDbParameter(item); sqlGenerate += ","; } if (sqlGenerate.Sql.ToString().EndsWith(",")) { sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1); } } else { sqlGenerate += "NULL"; } sqlGenerate += ")"; return(sqlGenerate); }
protected override SqlGenerate Where(LambdaExpression expression, SqlGenerate sqlGenerate) { if (expression.Body.NodeType == ExpressionType.MemberAccess) { var memberExpression = expression.Body as MemberExpression; if (memberExpression.Expression == null) { return(sqlGenerate); } //添加属性 SqlExpressionProvider.Where(memberExpression, sqlGenerate); if (memberExpression.Expression.Type.IsNullableType()) { return(sqlGenerate); } sqlGenerate += " = 1"; return(sqlGenerate); } SqlExpressionProvider.Where(expression.Body, sqlGenerate); return(sqlGenerate); }
protected override SqlGenerate Where(BinaryExpression expression, SqlGenerate sqlGenerate) { int leftBracketIndex = -1, rightBracketIndex = -1, signIndex = -1, sqlLength = -1; leftBracketIndex = sqlGenerate.Length; #region 内部内容 if (expression.NodeType == ExpressionType.AndAlso || expression.NodeType == ExpressionType.OrElse) { if (expression.Left.IsBooleanComparison()) { SqlExpressionProvider.Where(expression.Left, sqlGenerate); sqlGenerate += $" = 1"; } else { SqlExpressionProvider.Where(expression.Left, sqlGenerate); } signIndex = sqlGenerate.Length; if (expression.Right.IsBooleanComparison()) { SqlExpressionProvider.Where(expression.Right, sqlGenerate); sqlGenerate += $" = 1"; } else { SqlExpressionProvider.Where(expression.Right, sqlGenerate); } sqlLength = sqlGenerate.Length; } else { SqlExpressionProvider.Where(expression.Left, sqlGenerate); signIndex = sqlGenerate.Length; SqlExpressionProvider.Where(expression.Right, sqlGenerate); sqlLength = sqlGenerate.Length; } if (sqlLength - signIndex == 5 && sqlGenerate.ToString().EndsWith("null")) { OperatorParser(expression.NodeType, signIndex, sqlGenerate, true); } else { OperatorParser(expression.NodeType, signIndex, sqlGenerate); } #endregion if (expression.NodeType == ExpressionType.OrElse || expression.NodeType == ExpressionType.AndAlso) { sqlGenerate.Sql.Insert(leftBracketIndex, " ( "); rightBracketIndex = sqlGenerate.Length; sqlGenerate.Sql.Insert(rightBracketIndex, " ) "); } return(sqlGenerate); }
/// <summary> /// 修改 /// </summary> /// <param name="expression"></param> /// <returns></returns> public SqlExpressionCore <T> Update(Expression <Func <object> > expression = null) { sqlGenerate.Clear(); sqlGenerate += $"update {sqlGenerate.TableName} set "; SqlExpressionProvider.Update(expression, sqlGenerate); return(this); }
protected override SqlGenerate Select(NewExpression expression, SqlGenerate sqlGenerate) { foreach (Expression item in expression.Arguments) { SqlExpressionProvider.Select(item, sqlGenerate); } return(sqlGenerate); }
private static void StartsWith(MethodCallExpression expression, SqlGenerate sqlGenerate) { SqlExpressionProvider.Where(expression.Object, sqlGenerate); SqlExpressionProvider.Where(expression.Arguments[0], sqlGenerate); sqlGenerate += " like "; var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]); sqlGenerate.AddDbParameter($"{val}%"); }
/// <summary> /// Where条件 /// </summary> /// <param name="expression"></param> /// <returns></returns> public SqlExpressionCore <T> Where(Expression <Func <T, bool> > expression) { if (expression == null) { return(this); } sqlGenerate += $"{Environment.NewLine}where"; SqlExpressionProvider.Where(expression, sqlGenerate); return(this); }
protected override SqlGenerate Where(UnaryExpression expression, SqlGenerate sqlGenerate) { SqlExpressionProvider.Where(expression.Operand, sqlGenerate); switch (expression.NodeType) { case ExpressionType.Not: sqlGenerate += " = 0"; break; } return(sqlGenerate); }
protected override SqlGenerate OrderBy(NewExpression expression, SqlGenerate sqlGenerate) { foreach (Expression item in expression.Arguments) { SqlExpressionProvider.OrderBy(item, sqlGenerate); sqlGenerate += ","; } if (sqlGenerate[sqlGenerate.Length - 1] == ',') { sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1); } return(sqlGenerate); }
protected override SqlGenerate In(NewArrayExpression expression, SqlGenerate sqlGenerate) { sqlGenerate += "("; foreach (Expression expressionItem in expression.Expressions) { SqlExpressionProvider.In(expressionItem, sqlGenerate); } if (sqlGenerate.Sql[sqlGenerate.Sql.Length - 1] == ',') { sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1); } sqlGenerate += ")"; return(sqlGenerate); }
protected override SqlGenerate Update(NewExpression expression, SqlGenerate sqlGenerate) { for (int i = 0; i < expression.Members.Count; i++) { var m = expression.Members[i]; sqlGenerate += $"{m.Name.ParamSql(sqlGenerate)} = "; var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]); sqlGenerate.AddDbParameter(val); sqlGenerate += ","; } if (sqlGenerate[sqlGenerate.Length - 1] == ',') { sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1); } return(sqlGenerate); }
protected override SqlGenerate Where(MethodCallExpression expression, SqlGenerate sqlGenerate) { var key = expression.Method; if (key.IsGenericMethod) { key = key.GetGenericMethodDefinition(); } Action <MethodCallExpression, SqlGenerate> action; if (_Methods.TryGetValue(key.Name, out action)) { action(expression, sqlGenerate); return(sqlGenerate); } throw new NotImplementedException("无法解析方法" + expression.Method); }
private static void Contains(MethodCallExpression expression, SqlGenerate sqlGenerate) { if (IsStaticArrayMethod(expression)) { DoStaticArrayMethodCall(expression, sqlGenerate); return; } if (IsEnumerableMethod(expression)) { DoEnumerableMethodCall(expression, sqlGenerate); return; } SqlExpressionProvider.Where(expression.Object, sqlGenerate); sqlGenerate += " like "; var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]); sqlGenerate.AddDbParameter($"%{val}%"); }
protected override SqlGenerate Update(MemberExpression expression, SqlGenerate sqlGenerate) { var obj = SqlExpressionCompiler.Evaluate(expression); if (obj == null) { throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>"); } if (obj.GetType().IsValueType) { throw new ArgumentException($"Expression '{expression}' accesses unsupported valuetype"); } if (obj is IDictionary) { IDictionary dictionary = (IDictionary)obj; foreach (string key in dictionary.Keys) { sqlGenerate += $"{key.ParamSql(sqlGenerate)} = "; sqlGenerate.AddDbParameter(dictionary[key]); sqlGenerate += ","; } } else { var pis = obj.GetType().GetProperties(); foreach (var p in pis) { sqlGenerate += $"{p.Name.ParamSql(sqlGenerate)} = "; sqlGenerate.AddDbParameter(p.GetValue(obj)); sqlGenerate += ","; } } if (sqlGenerate[sqlGenerate.Length - 1] == ',') { sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1); } return(sqlGenerate); }
/// <summary> /// Limit /// </summary> /// <param name="page"></param> /// <param name="rows"></param> /// <returns></returns> public SqlExpressionCore <T> Limit(int page, int rows) { var skip = (page - 1) * rows; switch (sqlGenerate.DatabaseType) { case DatabaseType.SqlServer: sqlGenerate.Sql = new StringBuilder($"SELECT it.* FROM ({sqlGenerate.Sql}) it where it.RowNumber > {skip} AND it.RowNumber <= {rows}"); break; case DatabaseType.MySql: sqlGenerate += $" limit {skip}, {rows}"; break; case DatabaseType.SQLite: sqlGenerate += $" limit {rows} offset {skip}"; break; default: break; } return(this); }
protected override SqlGenerate Select(UnaryExpression expression, SqlGenerate sqlGenerate) { SqlExpressionProvider.Select(expression.Operand, sqlGenerate); return(sqlGenerate); }
protected override SqlGenerate Sum(MemberExpression expression, SqlGenerate sqlGenerate) { sqlGenerate.Sql.AppendFormat("select sum({0}) from {1}", expression.Member.Name.ParamSql(sqlGenerate), sqlGenerate.TableName); return(sqlGenerate); }
public SqlGenerate Count(Expression expression, SqlGenerate sqlGenerate) { return(Count((T)expression, sqlGenerate)); }
public SqlGenerate Avg(Expression expression, SqlGenerate sqlGenerate) { return(Avg((T)expression, sqlGenerate)); }
public SqlGenerate Min(Expression expression, SqlGenerate sqlGenerate) { return(Min((T)expression, sqlGenerate)); }
public SqlGenerate OrderBy(Expression expression, SqlGenerate sqlGenerate) { return(OrderBy((T)expression, sqlGenerate)); }
protected virtual SqlGenerate Update(T expression, SqlGenerate sqlGenerate) { throw new NotImplementedException("未实现" + typeof(T).Name + "2Sql.Update方法"); }
public SqlGenerate Where(Expression expression, SqlGenerate sqlGenerate) { return(Where((T)expression, sqlGenerate)); }
public SqlGenerate Select(Expression expression, SqlGenerate sqlGenerate) { return(Select((T)expression, sqlGenerate)); }
public SqlGenerate Update(Expression expression, SqlGenerate sqlGenerate) { return(Update((T)expression, sqlGenerate)); }
public SqlGenerate Sum(Expression expression, SqlGenerate sqlGenerate) { return(Sum((T)expression, sqlGenerate)); }
protected override SqlGenerate OrderBy(MemberExpression expression, SqlGenerate sqlGenerate) { sqlGenerate += expression.Member.Name.ParamSql(sqlGenerate); return(sqlGenerate); }
/// <summary> /// 删除 /// </summary> /// <returns></returns> public SqlExpressionCore <T> Delete() { sqlGenerate.Clear(); sqlGenerate += $"delete from {sqlGenerate.TableName}"; return(this); }