/// <summary> /// OrderBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <param name="orders">排序方式</param> /// <returns>SqlPack</returns> public override SqlPack OrderBy(MethodCallExpression expression, SqlPack sqlPack, params OrderType[] orders) { var array = (expression.ToObject() as IEnumerable <object>)?.ToList(); if (array != null) { for (var i = 0; i < array.Count; i++) { SqlBuilderProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlPack); if (i <= orders.Length - 1) { sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},"; } else if (!array[i].ToString().ToUpper().Contains("ASC") && !array[i].ToString().ToUpper().Contains("DESC")) { sqlPack += " ASC,"; } else { sqlPack += ","; } } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); } return(sqlPack); }
/// <summary> /// In /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack In(MethodCallExpression expression, SqlPack sqlPack) { var val = expression?.ToObject(); if (val != null) { sqlPack += "("; if (val.GetType().IsArray || typeof(IList).IsAssignableFrom(val.GetType())) { var list = val as IList; if (list?.Count > 0) { foreach (var item in list) { SqlBuilderProvider.In(Expression.Constant(item, item.GetType()), sqlPack); sqlPack += ","; } } } else { SqlBuilderProvider.In(Expression.Constant(val, val.GetType()), sqlPack); } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); } sqlPack += ")"; } return(sqlPack); }
/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(BinaryExpression expression, SqlPack sqlPack) { SqlBuilderProvider.Join(expression.Left, sqlPack); var operatorIndex = sqlPack.Sql.Length; //嵌套条件 var flag = false; if (expression.Right is BinaryExpression binaryExpression && (binaryExpression.Right as BinaryExpression) != null) { flag = true; sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (flag) { sqlPack += ")"; } var sqlLength = sqlPack.Sql.Length; if (sqlLength - operatorIndex == 5 && sqlPack.ToString().ToUpper().EndsWith("NULL")) { OperatorParser(expression.NodeType, operatorIndex, sqlPack, true); } else { OperatorParser(expression.NodeType, operatorIndex, sqlPack); } return(sqlPack); }
/// <summary> /// OrderBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <param name="orders">排序方式</param> /// <returns>SqlPack</returns> public override SqlPack OrderBy(NewArrayExpression expression, SqlPack sqlPack, params OrderType[] orders) { for (var i = 0; i < expression.Expressions.Count; i++) { SqlBuilderProvider.OrderBy(expression.Expressions[i], sqlPack); if (i <= orders.Length - 1) { sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},"; } else if (expression.Expressions[i] is ConstantExpression order) { if (!order.Value.ToString().ToUpper().Contains("ASC") && !order.Value.ToString().ToUpper().Contains("DESC")) { sqlPack += " ASC,"; } else { sqlPack += ","; } } else { sqlPack += " ASC,"; } } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); return(sqlPack); }
/// <summary> /// Sum /// </summary> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Sum(Expression <Func <T, object> > expression) { this._sqlPack.Clear(); this._sqlPack.IsSingleTable = true; SqlBuilderProvider.Sum(expression.Body, this._sqlPack); return(this); }
/// <summary> /// ToLower /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void ToLower(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { sqlPack += "LOWER("; SqlBuilderProvider.Where(expression.Object, sqlPack); sqlPack += ")"; } }
/// <summary> /// Where /// </summary> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Where(Expression <Func <T, bool> > expression) { if (!(expression.Body.NodeType == ExpressionType.Constant && expression.Body.ToObject() is bool b && b)) { this._sqlPack += " WHERE "; SqlBuilderProvider.Where(expression.Body, this._sqlPack); } return(this); }
/// <summary> /// IsNullOrEmpty /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void IsNullOrEmpty(MethodCallExpression expression, SqlPack sqlPack) { sqlPack += "("; SqlBuilderProvider.Where(expression.Arguments[0], sqlPack); sqlPack += " IS NULL OR "; SqlBuilderProvider.Where(expression.Arguments[0], sqlPack); sqlPack += " = ''"; sqlPack += ")"; }
/// <summary> /// GroupBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack GroupBy(NewExpression expression, SqlPack sqlPack) { foreach (Expression item in expression.Arguments) { SqlBuilderProvider.GroupBy(item, sqlPack); } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); return(sqlPack); }
/// <summary> /// GroupBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack GroupBy(MemberExpression expression, SqlPack sqlPack) { string tableName = string.Empty; if (expression.Expression.NodeType == ExpressionType.Parameter) { var type = expression.Expression.Type != expression.Member.DeclaringType ? expression.Expression.Type : expression.Member.DeclaringType; tableName = sqlPack.GetTableName(type); } if (expression.Expression.NodeType == ExpressionType.Constant) { tableName = sqlPack.GetTableName(sqlPack.DefaultType); } sqlPack.SetTableAlias(tableName); var tableAlias = sqlPack.GetTableAlias(tableName); if (!tableAlias.IsNullOrEmpty()) { tableAlias += "."; } if (expression.Expression.NodeType == ExpressionType.Parameter) { sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName; } if (expression.Expression.NodeType == ExpressionType.Constant) { var obj = expression.ToObject(); if (obj != null) { var type = obj.GetType().Name; if (type == "String[]" && obj is string[] array) { foreach (var item in array) { SqlBuilderProvider.GroupBy(Expression.Constant(item, item.GetType()), sqlPack); } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); } if (type == "List`1" && obj is List <string> list) { foreach (var item in list) { SqlBuilderProvider.GroupBy(Expression.Constant(item, item.GetType()), sqlPack); } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); } if (type == "String" && obj is string str) { SqlBuilderProvider.GroupBy(Expression.Constant(str, str.GetType()), sqlPack); sqlPack.Sql.Remove(sqlPack.Length - 1, 1); } } } return(sqlPack); }
/// <summary> /// TrimEnd /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void TrimEnd(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { sqlPack += "RTRIM("; SqlBuilderProvider.Where(expression.Object, sqlPack); sqlPack += ")"; } }
/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(BinaryExpression expression, SqlPack sqlPack) { //左侧嵌套 var leftBinary = expression.Left as BinaryExpression; var isBinaryLeft = leftBinary?.Left is BinaryExpression; var isBoolMethodCallLeft = (leftBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); var isBinaryRight = leftBinary?.Right is BinaryExpression; var isBoolMethodCallRight = (leftBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var leftNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (leftNested) { sqlPack += "("; } SqlBuilderProvider.Join(expression.Left, sqlPack); if (leftNested) { sqlPack += ")"; } var operatorIndex = sqlPack.Sql.Length; //右侧嵌套 var rightBinary = expression.Right as BinaryExpression; isBinaryLeft = rightBinary?.Left is BinaryExpression; isBoolMethodCallLeft = (rightBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); isBinaryRight = rightBinary?.Right is BinaryExpression; isBoolMethodCallRight = (rightBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var rightNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (rightNested) { sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (rightNested) { sqlPack += ")"; } //表达式左侧为bool类型常量且为true时,不进行sql拼接 if (!(expression.Left.NodeType == ExpressionType.Constant && expression.Left.ToObject() is bool b && b)) { var sqlLength = sqlPack.Sql.Length; if (sqlLength - operatorIndex == 5 && sqlPack.ToString().ToUpper().EndsWith("NULL")) { OperatorParser(expression.NodeType, operatorIndex, sqlPack, true); } else { OperatorParser(expression.NodeType, operatorIndex, sqlPack); } } return(sqlPack); }
/// <summary> /// Update /// </summary> /// <param name="expression">表达式树</param> /// <param name="isEnableNullValue">是否对null值属性进行sql拼接操作,默认:是</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Update(Expression <Func <object> > expression = null, bool isEnableNullValue = true) { this._sqlPack.Clear(); this._sqlPack.IsSingleTable = true; this._sqlPack.IsEnableNullValue = isEnableNullValue; this._sqlPack += $"UPDATE {this._sqlPack.GetTableName(typeof(T))} SET "; SqlBuilderProvider.Update(expression.Body, this._sqlPack); return(this); }
/// <summary> /// Insert /// </summary> /// <param name="expression">表达式树</param> /// <param name="isEnableNullValue">是否对null值属性进行sql拼接操作,默认:是</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Insert(Expression <Func <object> > expression = null, bool isEnableNullValue = true) { this._sqlPack.Clear(); this._sqlPack.IsSingleTable = true; this._sqlPack.IsEnableNullValue = isEnableNullValue; this._sqlPack += $"INSERT INTO {this._sqlPack.GetTableName(typeof(T))} ({{0}}) {(this._sqlPack.DatabaseType == DatabaseType.Oracle ? "SELECT" : "VALUES")} "; SqlBuilderProvider.Insert(expression.Body, this._sqlPack); return(this); }
/// <summary> /// GroupBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack GroupBy(NewArrayExpression expression, SqlPack sqlPack) { for (var i = 0; i < expression.Expressions.Count; i++) { SqlBuilderProvider.GroupBy(expression.Expressions[i], sqlPack); } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); return(sqlPack); }
/// <summary> /// Select /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Select(NewExpression expression, SqlPack sqlPack) { for (var i = 0; i < expression.Members.Count; i++) { var argument = expression.Arguments[i]; var member = expression.Members[i]; SqlBuilderProvider.Select(argument, sqlPack); //添加字段别名 if (argument is MemberExpression memberExpression && memberExpression.Member.Name != member.Name) { sqlPack.SelectFields[sqlPack.SelectFields.Count - 1] += $" AS {sqlPack.GetFormatName(member.Name)}"; }
/// <summary> /// Count /// </summary> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Count(Expression <Func <T, object> > expression = null) { this._sqlPack.Clear(); this._sqlPack.IsSingleTable = true; if (expression == null) { this._sqlPack.Sql.Append($"SELECT COUNT(*) FROM {this._sqlPack.GetTableName(typeof(T))}"); } else { SqlBuilderProvider.Count(expression.Body, this._sqlPack); } return(this); }
/// <summary> /// GroupBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack GroupBy(MethodCallExpression expression, SqlPack sqlPack) { var array = (expression.ToObject() as IEnumerable <object>)?.ToList(); if (array != null) { for (var i = 0; i < array.Count; i++) { SqlBuilderProvider.GroupBy(Expression.Constant(array[i], array[i].GetType()), sqlPack); } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); } return(sqlPack); }
/// <summary> /// In /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack In(NewArrayExpression expression, SqlPack sqlPack) { sqlPack += "("; foreach (Expression expressionItem in expression.Expressions) { SqlBuilderProvider.In(expressionItem, sqlPack); sqlPack += ","; } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); } sqlPack += ")"; return(sqlPack); }
/// <summary> /// OrWhere /// </summary> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> OrWhere(Expression <Func <T, bool> > expression) { var sql = this._sqlPack.ToString(); if (sql.Contains("WHERE") && !string.IsNullOrEmpty(sql.Substring("WHERE").Trim())) { this._sqlPack += " OR "; } else { this._sqlPack += " WHERE "; } SqlBuilderProvider.Where(expression.Body, this._sqlPack); return(this); }
/// <summary> /// Select /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Select(NewExpression expression, SqlPack sqlPack) { for (var i = 0; i < expression.Members.Count; i++) { var argument = expression.Arguments[i]; var member = expression.Members[i]; SqlBuilderProvider.Select(argument, sqlPack); //添加字段别名 if ((argument as MemberExpression)?.Member.Name != member.Name) { sqlPack.SelectFields[sqlPack.SelectFields.Count - 1] += " AS " + member.Name; } } return(sqlPack); }
/// <summary> /// Select /// </summary> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Select(Expression <Func <T, object> > expression = null) { var sql = SelectParser(typeof(T)); if (expression == null) { this._sqlPack.Sql.AppendFormat(sql, "*"); } else { SqlBuilderProvider.Select(expression.Body, this._sqlPack); this._sqlPack.Sql.AppendFormat(sql, this._sqlPack.SelectFieldsStr); } return(this); }
/// <summary> /// Like /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void Like(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { SqlBuilderProvider.Where(expression.Object, sqlPack); } SqlBuilderProvider.Where(expression.Arguments[0], sqlPack); switch (sqlPack.DatabaseType) { case DatabaseType.SQLServer: sqlPack += " LIKE '%' + "; break; case DatabaseType.MySQL: case DatabaseType.PostgreSQL: sqlPack += " LIKE CONCAT('%',"; break; case DatabaseType.Oracle: case DatabaseType.SQLite: sqlPack += " LIKE '%' || "; break; default: break; } SqlBuilderProvider.Where(expression.Arguments[1], sqlPack); switch (sqlPack.DatabaseType) { case DatabaseType.SQLServer: sqlPack += " + '%'"; break; case DatabaseType.MySQL: case DatabaseType.PostgreSQL: sqlPack += ",'%')"; break; case DatabaseType.Oracle: case DatabaseType.SQLite: sqlPack += " || '%'"; break; default: break; } }
/// <summary> /// JoinParser /// </summary> /// <typeparam name="T2">泛型类型2</typeparam> /// <param name="expression">表达式树</param> /// <param name="leftOrRightJoin">左连接或者右连接</param> /// <returns>SqlBuilderCore</returns> private SqlBuilderCore <T> JoinParser <T2>(Expression <Func <T, T2, bool> > expression, string leftOrRightJoin = "") where T2 : class { string joinTableName = this._sqlPack.GetTableName(typeof(T2)); this._sqlPack.SetTableAlias(joinTableName); if (this._sqlPack.DatabaseType == DatabaseType.Oracle) { this._sqlPack.Sql.Append($"{(string.IsNullOrEmpty(leftOrRightJoin) ? "" : " " + leftOrRightJoin)} JOIN {joinTableName} {this._sqlPack.GetTableAlias(joinTableName)} ON "); } else { this._sqlPack.Sql.Append($"{(string.IsNullOrEmpty(leftOrRightJoin) ? "" : " " + leftOrRightJoin)} JOIN {joinTableName} AS {this._sqlPack.GetTableAlias(joinTableName)} ON "); } SqlBuilderProvider.Join(expression.Body, this._sqlPack); return(this); }
/// <summary> /// OrderBy /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <param name="orders">排序方式</param> /// <returns>SqlPack</returns> public override SqlPack OrderBy(NewExpression expression, SqlPack sqlPack, params OrderType[] orders) { for (var i = 0; i < expression.Arguments.Count; i++) { SqlBuilderProvider.OrderBy(expression.Arguments[i], sqlPack); if (i <= orders.Length - 1) { sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},"; } else { sqlPack += " ASC,"; } } sqlPack.Sql.Remove(sqlPack.Length - 1, 1); return(sqlPack); }
/// <summary> /// Select /// </summary> /// <typeparam name="T2">泛型类型2</typeparam> /// <typeparam name="T3">泛型类型3</typeparam> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Select <T2, T3>(Expression <Func <T, T2, T3, object> > expression = null) where T2 : class where T3 : class { var sql = SelectParser(typeof(T), typeof(T2), typeof(T3)); if (expression == null) { this._sqlPack.Sql.AppendFormat(sql, "*"); } else { SqlBuilderProvider.Select(expression.Body, this._sqlPack); this._sqlPack.Sql.AppendFormat(sql, this._sqlPack.SelectFieldsStr); } return(this); }
/// <summary> /// Equals /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void Equals(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { SqlBuilderProvider.Where(expression.Object, sqlPack); } var signIndex = sqlPack.Length; SqlBuilderProvider.Where(expression.Arguments[0], sqlPack); if (sqlPack.ToString().ToUpper().EndsWith("NULL")) { sqlPack.Sql.Insert(signIndex, " IS "); } else { sqlPack.Sql.Insert(signIndex, " = "); } }
/// <summary> /// In /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack In(MemberExpression expression, SqlPack sqlPack) { var obj = expression.ToObject(); if (obj is IEnumerable array) { sqlPack += "("; foreach (var item in array) { SqlBuilderProvider.In(Expression.Constant(item), sqlPack); sqlPack += ","; } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); } sqlPack += ")"; } return(sqlPack); }
/// <summary> /// Select /// </summary> /// <typeparam name="T2">泛型类型2</typeparam> /// <typeparam name="T3">泛型类型3</typeparam> /// <typeparam name="T4">泛型类型4</typeparam> /// <typeparam name="T5">泛型类型5</typeparam> /// <typeparam name="T6">泛型类型6</typeparam> /// <typeparam name="T7">泛型类型7</typeparam> /// <typeparam name="T8">泛型类型8</typeparam> /// <typeparam name="T9">泛型类型9</typeparam> /// <param name="expression">表达式树</param> /// <returns>SqlBuilderCore</returns> public SqlBuilderCore <T> Select <T2, T3, T4, T5, T6, T7, T8, T9>(Expression <Func <T, T2, T3, T4, T5, T6, T7, T8, T9, object> > expression = null) where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class { var sql = SelectParser(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9)); if (expression == null) { this._sqlPack.Sql.AppendFormat(sql, "*"); } else { SqlBuilderProvider.Select(expression.Body, this._sqlPack); this._sqlPack.Sql.AppendFormat(sql, this._sqlPack.SelectFieldsStr); } return(this); }
/// <summary> /// Trim /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void Trim(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { if (sqlPack.DatabaseType == DatabaseType.SQLServer) { sqlPack += "LTRIM(RTRIM("; } else { sqlPack += "TRIM("; } SqlBuilderProvider.Where(expression.Object, sqlPack); if (sqlPack.DatabaseType == DatabaseType.SQLServer) { sqlPack += "))"; } else { sqlPack += ")"; } } }