From 16a2c776943249b4e6796f738b3f64bd0f394202 Mon Sep 17 00:00:00 2001 From: zqlovejyc <943620963@qq.com> Date: Sun, 15 May 2022 16:11:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96null=E7=A9=BA=E5=80=BC?= =?UTF-8?q?=E5=88=A4=E6=96=AD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SQLBuilder/Entry/SqlBuilderCore.cs | 527 ++++++++++++++++++----------- 1 file changed, 336 insertions(+), 191 deletions(-) diff --git a/SQLBuilder/Entry/SqlBuilderCore.cs b/SQLBuilder/Entry/SqlBuilderCore.cs index b729793..a8c5ee3 100644 --- a/SQLBuilder/Entry/SqlBuilderCore.cs +++ b/SQLBuilder/Entry/SqlBuilderCore.cs @@ -152,69 +152,69 @@ public void Clear() { var list = new List<(Type type, string alias)>(); - if (expression != null && types?.Length > 0) + if (expression == null || types.IsNullOrEmpty()) + return list.ToArray(); + + if (expression is NewExpression newExpression && newExpression.Arguments?.Count > 0) { - if (expression is NewExpression newExpression && newExpression.Arguments?.Count > 0) + foreach (var item in newExpression.Arguments) { - foreach (var item in newExpression.Arguments) + if (item.NodeType == ExpressionType.MemberAccess) { - if (item.NodeType == ExpressionType.MemberAccess) - { - if (item is MemberExpression memberExpr && memberExpr.Expression is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) - list.Add((parameterExpr.Type, parameterExpr.Name)); - } - else if (item.NodeType == ExpressionType.Parameter) - { - if (item is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) - list.Add((parameterExpr.Type, parameterExpr.Name)); - } + if (item is MemberExpression memberExpr && memberExpr.Expression is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) + list.Add((parameterExpr.Type, parameterExpr.Name)); } - } - - else if (expression is LambdaExpression lambdaExpression && lambdaExpression.Parameters?.Count > 0) - { - foreach (var item in lambdaExpression.Parameters) + else if (item.NodeType == ExpressionType.Parameter) { if (item is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) list.Add((parameterExpr.Type, parameterExpr.Name)); } } + } - else if (expression is ParameterExpression parameterExpression) + else if (expression is LambdaExpression lambdaExpression && lambdaExpression.Parameters?.Count > 0) + { + foreach (var item in lambdaExpression.Parameters) { - if (types.Any(x => x == parameterExpression.Type)) - list.Add((parameterExpression.Type, parameterExpression.Name)); + if (item is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) + list.Add((parameterExpr.Type, parameterExpr.Name)); } + } - else if (expression is UnaryExpression unaryExpression) - { - if (unaryExpression.Operand is MemberExpression memberExpr && memberExpr.Expression is ParameterExpression parameterExpr) - { - if (types.Any(x => x == parameterExpr.Type)) - list.Add((parameterExpr.Type, parameterExpr.Name)); - } - } + else if (expression is ParameterExpression parameterExpression) + { + if (types.Any(x => x == parameterExpression.Type)) + list.Add((parameterExpression.Type, parameterExpression.Name)); + } - else if (expression is MemberExpression memberExpression) + else if (expression is UnaryExpression unaryExpression) + { + if (unaryExpression.Operand is MemberExpression memberExpr && memberExpr.Expression is ParameterExpression parameterExpr) { - if (memberExpression.Expression is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) + if (types.Any(x => x == parameterExpr.Type)) list.Add((parameterExpr.Type, parameterExpr.Name)); } + } - else if (expression is ConstantExpression constantExpression) - { - list.Add((types[0], null)); - } + else if (expression is MemberExpression memberExpression) + { + if (memberExpression.Expression is ParameterExpression parameterExpr && types.Any(x => x == parameterExpr.Type)) + list.Add((parameterExpr.Type, parameterExpr.Name)); + } - else if (expression is MemberInitExpression memberInitExpression) + else if (expression is ConstantExpression constantExpression) + { + list.Add((types[0], null)); + } + + else if (expression is MemberInitExpression memberInitExpression) + { + foreach (MemberAssignment ma in memberInitExpression.Bindings) { - foreach (MemberAssignment ma in memberInitExpression.Bindings) + if (ma.Expression is MemberExpression memberExpr && memberExpr?.Expression is ParameterExpression parameterExpr) { - if (ma.Expression is MemberExpression memberExpr && memberExpr?.Expression is ParameterExpression parameterExpr) - { - if (types.Any(x => x == parameterExpr.Type)) - list.Add((parameterExpr.Type, parameterExpr.Name)); - } + if (types.Any(x => x == parameterExpr.Type)) + list.Add((parameterExpr.Type, parameterExpr.Name)); } } } @@ -251,7 +251,7 @@ private string Select(string aggregateSyntax = null, Func tabl if (tableAlias.IsNullOrEmpty()) @as = ""; - if (tableNameFunc.IsNull()) + if (tableNameFunc == null) return $"SELECT {(aggregateSyntax.IsNullOrEmpty() ? "{0}" : aggregateSyntax)} FROM {tableName}{@as}{tableAlias}"; else return $"SELECT {(aggregateSyntax.IsNullOrEmpty() ? "{0}" : aggregateSyntax)} FROM {tableNameFunc(new[] { tableName, @as, tableAlias })}"; @@ -307,10 +307,11 @@ public SqlBuilderCore Select(Expression expression = null, string sql = null, public SqlBuilderCore Select(Expression> expression = null, Func tableNameFunc = null) { var expr = expression?.Body; - if (expr?.NodeType == ExpressionType.Constant || - expr?.NodeType == ExpressionType.Parameter || - expr?.NodeType == ExpressionType.MemberInit || - expr?.NodeType == ExpressionType.New) + if (expr != null && + (expr.NodeType == ExpressionType.Constant || + expr.NodeType == ExpressionType.Parameter || + expr.NodeType == ExpressionType.MemberInit || + expr.NodeType == ExpressionType.New)) expr = expression; return this.Select(expr, null, tableNameFunc); @@ -525,6 +526,9 @@ private Type GetJoinType(params Type[] types) { Type type = null; + if (types.IsNullOrEmpty()) + return type; + for (int i = types.Length - 1; i >= 0; i--) { type = types[i]; @@ -550,8 +554,12 @@ private Type GetJoinType(params Type[] types) /// public SqlBuilderCore Join(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " JOIN "; this.sqlWrapper += sql; + return this; } @@ -562,8 +570,12 @@ public SqlBuilderCore Join(string sql) /// public SqlBuilderCore Join(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " JOIN "; this.sqlWrapper += sql; + return this; } @@ -577,6 +589,9 @@ public SqlBuilderCore Join(StringBuilder sql) /// SqlBuilderCore public SqlBuilderCore Join(Type type, Expression expression, string join, Func tableNameFunc = null) { + if (type == null || expression == null) + return this; + this.sqlWrapper.AddJoinType(type); var alias = this.GetExpressionAlias(expression, type).Last().alias; @@ -596,7 +611,7 @@ public SqlBuilderCore Join(Type type, Expression expression, string join, Fun if (tableAlias.IsNullOrEmpty()) @as = ""; - if (tableNameFunc.IsNull()) + if (tableNameFunc == null) this.sqlWrapper.Append($"{(join.IsNullOrEmpty() ? "" : $" {join}")} JOIN {tableName}{@as}{tableAlias} ON "); else this.sqlWrapper.Append($"{(join.IsNullOrEmpty() ? "" : $" {join}")} JOIN {tableNameFunc(new[] { tableName, @as, tableAlias })} ON "); @@ -741,8 +756,12 @@ public SqlBuilderCore Join(Expression> /// public SqlBuilderCore InnerJoin(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " INNER JOIN "; this.sqlWrapper += sql; + return this; } @@ -753,8 +772,12 @@ public SqlBuilderCore InnerJoin(string sql) /// public SqlBuilderCore InnerJoin(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " INNER JOIN "; this.sqlWrapper += sql; + return this; } @@ -840,8 +863,12 @@ public SqlBuilderCore InnerJoin(ExpressionSqlBuilderCore public SqlBuilderCore LeftJoin(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " LEFT JOIN "; this.sqlWrapper += sql; + return this; } @@ -852,8 +879,12 @@ public SqlBuilderCore LeftJoin(string sql) /// SqlBuilderCore public SqlBuilderCore LeftJoin(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " LEFT JOIN "; this.sqlWrapper += sql; + return this; } @@ -939,8 +970,12 @@ public SqlBuilderCore LeftJoin(ExpressionSqlBuilderCore public SqlBuilderCore RightJoin(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " RIGHT JOIN "; this.sqlWrapper += sql; + return this; } @@ -951,8 +986,12 @@ public SqlBuilderCore RightJoin(string sql) /// SqlBuilderCore public SqlBuilderCore RightJoin(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " RIGHT JOIN "; this.sqlWrapper += sql; + return this; } @@ -1038,8 +1077,12 @@ public SqlBuilderCore RightJoin(ExpressionSqlBuilderCore public SqlBuilderCore FullJoin(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " FULL JOIN "; this.sqlWrapper += sql; + return this; } @@ -1050,8 +1093,12 @@ public SqlBuilderCore FullJoin(string sql) /// SqlBuilderCore public SqlBuilderCore FullJoin(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " FULL JOIN "; this.sqlWrapper += sql; + return this; } @@ -1139,11 +1186,15 @@ public SqlBuilderCore FullJoin(Expression public SqlBuilderCore Where(string sql) { + if (sql.IsNullOrEmpty()) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); this.sqlWrapper += " WHERE "; this.sqlWrapper += sql; + return this; } @@ -1155,6 +1206,9 @@ public SqlBuilderCore Where(string sql) /// public SqlBuilderCore Where(string sql, ref bool hasWhere) { + if (sql.IsNullOrEmpty()) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); @@ -1178,11 +1232,15 @@ public SqlBuilderCore Where(string sql, ref bool hasWhere) /// public SqlBuilderCore Where(StringBuilder sql) { + if (sql == null) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); this.sqlWrapper += " WHERE "; this.sqlWrapper += sql; + return this; } @@ -1194,6 +1252,9 @@ public SqlBuilderCore Where(StringBuilder sql) /// public SqlBuilderCore Where(StringBuilder sql, ref bool hasWhere) { + if (sql == null) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); @@ -1216,6 +1277,9 @@ public SqlBuilderCore Where(StringBuilder sql, ref bool hasWhere) /// 表达式树 public SqlBuilderCore Where(Expression expression) { + if (expression == null) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); @@ -1240,6 +1304,9 @@ public SqlBuilderCore Where(Expression expression) /// 指定是否已包含where关键字 public SqlBuilderCore Where(Expression expression, ref bool hasWhere) { + if (expression == null) + return this; + if (this.sqlWrapper.Length == 0) this.Select(expression: null); @@ -1270,7 +1337,7 @@ public SqlBuilderCore Where(Expression expression, ref bool hasWhere) /// SqlBuilderCore public SqlBuilderCore Where(Expression> expression) { - return this.Where(expression.Body); + return this.Where(expression?.Body); } /// @@ -1281,7 +1348,7 @@ public SqlBuilderCore Where(Expression> expression) /// SqlBuilderCore public SqlBuilderCore Where(Expression> expression, ref bool hasWhere) { - return this.Where(expression.Body, ref hasWhere); + return this.Where(expression?.Body, ref hasWhere); } /// @@ -1293,7 +1360,7 @@ public SqlBuilderCore Where(Expression> expression, ref bool ha public SqlBuilderCore Where(Expression> expression) where T2 : class { - return this.Where(expression.Body); + return this.Where(expression?.Body); } /// @@ -1306,7 +1373,7 @@ public SqlBuilderCore Where(Expression> expression) public SqlBuilderCore Where(Expression> expression, ref bool hasWhere) where T2 : class { - return this.Where(expression.Body, ref hasWhere); + return this.Where(expression?.Body, ref hasWhere); } /// @@ -1320,7 +1387,7 @@ public SqlBuilderCore Where(Expression> express where T2 : class where T3 : class { - return this.Where(expression.Body); + return this.Where(expression?.Body); } /// @@ -1335,7 +1402,7 @@ public SqlBuilderCore Where(Expression> express where T2 : class where T3 : class { - return this.Where(expression.Body, ref hasWhere); + return this.Where(expression?.Body, ref hasWhere); } /// @@ -1351,7 +1418,7 @@ public SqlBuilderCore Where(Expression> where T3 : class where T4 : class { - return this.Where(expression.Body); + return this.Where(expression?.Body); } /// @@ -1368,7 +1435,7 @@ public SqlBuilderCore Where(Expression> where T3 : class where T4 : class { - return this.Where(expression.Body, ref hasWhere); + return this.Where(expression?.Body, ref hasWhere); } /// @@ -1386,7 +1453,7 @@ public SqlBuilderCore Where(Expression @@ -1405,7 +1472,7 @@ public SqlBuilderCore Where(Expression @@ -1425,7 +1492,7 @@ public SqlBuilderCore Where(Expression @@ -1446,7 +1513,7 @@ public SqlBuilderCore Where(Expression @@ -1468,7 +1535,7 @@ public SqlBuilderCore Where(Expression @@ -1491,7 +1558,7 @@ public SqlBuilderCore Where(Expression @@ -1515,7 +1582,7 @@ public SqlBuilderCore Where(Expression @@ -1540,7 +1607,7 @@ public SqlBuilderCore Where(Expression @@ -1566,7 +1633,7 @@ public SqlBuilderCore Where(Expression @@ -1593,7 +1660,7 @@ public SqlBuilderCore Where(Expression @@ -1621,7 +1688,7 @@ public SqlBuilderCore Where(Expression @@ -1650,7 +1717,7 @@ public SqlBuilderCore Where(Expression Where(Expression public SqlBuilderCore AndWhere(string sql) { + if (sql.IsNullOrEmpty()) + return this; + var str = this.sqlWrapper.ToString(); if (str.ContainsIgnoreCase("WHERE") && str.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -1682,6 +1752,9 @@ public SqlBuilderCore AndWhere(string sql) /// public SqlBuilderCore AndWhere(string sql, ref bool hasWhere) { + if (sql.IsNullOrEmpty()) + return this; + if (hasWhere) this.sqlWrapper += " AND "; else @@ -1702,6 +1775,9 @@ public SqlBuilderCore AndWhere(string sql, ref bool hasWhere) /// public SqlBuilderCore AndWhere(StringBuilder sql) { + if (sql == null) + return this; + var str = this.sqlWrapper.ToString(); if (str.ContainsIgnoreCase("WHERE") && str.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -1722,6 +1798,9 @@ public SqlBuilderCore AndWhere(StringBuilder sql) /// public SqlBuilderCore AndWhere(StringBuilder sql, ref bool hasWhere) { + if (sql == null) + return this; + if (hasWhere) this.sqlWrapper += " AND "; else @@ -1742,6 +1821,9 @@ public SqlBuilderCore AndWhere(StringBuilder sql, ref bool hasWhere) /// public SqlBuilderCore AndWhere(Expression expression) { + if (expression == null) + return this; + var sql = this.sqlWrapper.ToString(); if (sql.ContainsIgnoreCase("WHERE") && sql.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -1764,6 +1846,9 @@ public SqlBuilderCore AndWhere(Expression expression) /// public SqlBuilderCore AndWhere(Expression expression, ref bool hasWhere) { + if (expression == null) + return this; + if (hasWhere) this.sqlWrapper += " AND "; else @@ -1786,7 +1871,7 @@ public SqlBuilderCore AndWhere(Expression expression, ref bool hasWhere) /// SqlBuilderCore public SqlBuilderCore AndWhere(Expression> expression) { - return this.AndWhere(expression.Body); + return this.AndWhere(expression?.Body); } /// @@ -1797,7 +1882,7 @@ public SqlBuilderCore AndWhere(Expression> expression) /// SqlBuilderCore public SqlBuilderCore AndWhere(Expression> expression, ref bool hasWhere) { - return this.AndWhere(expression.Body, ref hasWhere); + return this.AndWhere(expression?.Body, ref hasWhere); } /// @@ -1809,7 +1894,7 @@ public SqlBuilderCore AndWhere(Expression> expression, ref bool public SqlBuilderCore AndWhere(Expression> expression) where T2 : class { - return this.AndWhere(expression.Body); + return this.AndWhere(expression?.Body); } /// @@ -1822,7 +1907,7 @@ public SqlBuilderCore AndWhere(Expression> expression) public SqlBuilderCore AndWhere(Expression> expression, ref bool hasWhere) where T2 : class { - return this.AndWhere(expression.Body, ref hasWhere); + return this.AndWhere(expression?.Body, ref hasWhere); } /// @@ -1836,7 +1921,7 @@ public SqlBuilderCore AndWhere(Expression> expr where T2 : class where T3 : class { - return this.AndWhere(expression.Body); + return this.AndWhere(expression?.Body); } /// @@ -1851,7 +1936,7 @@ public SqlBuilderCore AndWhere(Expression> expr where T2 : class where T3 : class { - return this.AndWhere(expression.Body, ref hasWhere); + return this.AndWhere(expression?.Body, ref hasWhere); } /// @@ -1867,7 +1952,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1884,7 +1969,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1902,7 +1987,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1921,7 +2006,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1941,7 +2026,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1962,7 +2047,7 @@ public SqlBuilderCore AndWhere(Expression @@ -1984,7 +2069,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2007,7 +2092,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2031,7 +2116,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2056,7 +2141,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2082,7 +2167,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2109,7 +2194,7 @@ public SqlBuilderCore AndWhere(Expression @@ -2137,7 +2222,7 @@ public SqlBuilderCore AndWhere(Expressio where T9 : class where T10 : class { - return this.AndWhere(expression.Body); + return this.AndWhere(expression?.Body); } /// @@ -2166,7 +2251,7 @@ public SqlBuilderCore AndWhere(Expressio where T9 : class where T10 : class { - return this.AndWhere(expression.Body, ref hasWhere); + return this.AndWhere(expression?.Body, ref hasWhere); } #endregion @@ -2178,6 +2263,9 @@ public SqlBuilderCore AndWhere(Expressio /// public SqlBuilderCore OrWhere(string sql) { + if (sql.IsNullOrEmpty()) + return this; + var str = this.sqlWrapper.ToString(); if (str.ContainsIgnoreCase("WHERE") && str.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -2198,6 +2286,9 @@ public SqlBuilderCore OrWhere(string sql) /// public SqlBuilderCore OrWhere(string sql, ref bool hasWhere) { + if (sql.IsNullOrEmpty()) + return this; + if (hasWhere) this.sqlWrapper += " OR "; else @@ -2218,6 +2309,9 @@ public SqlBuilderCore OrWhere(string sql, ref bool hasWhere) /// public SqlBuilderCore OrWhere(StringBuilder sql) { + if (sql == null) + return this; + var str = this.sqlWrapper.ToString(); if (str.ContainsIgnoreCase("WHERE") && str.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -2238,6 +2332,9 @@ public SqlBuilderCore OrWhere(StringBuilder sql) /// public SqlBuilderCore OrWhere(StringBuilder sql, ref bool hasWhere) { + if (sql == null) + return this; + if (hasWhere) this.sqlWrapper += " OR "; else @@ -2257,6 +2354,9 @@ public SqlBuilderCore OrWhere(StringBuilder sql, ref bool hasWhere) /// 表达式树 public SqlBuilderCore OrWhere(Expression expression) { + if (expression == null) + return this; + var sql = this.sqlWrapper.ToString(); if (sql.ContainsIgnoreCase("WHERE") && sql.SubstringIgnoreCase("WHERE").Trim().IsNotNullOrEmpty()) @@ -2278,6 +2378,9 @@ public SqlBuilderCore OrWhere(Expression expression) /// 指定是否已包含where关键字 public SqlBuilderCore OrWhere(Expression expression, ref bool hasWhere) { + if (expression == null) + return this; + if (hasWhere) this.sqlWrapper += " OR "; else @@ -2300,7 +2403,7 @@ public SqlBuilderCore OrWhere(Expression expression, ref bool hasWhere) /// SqlBuilderCore public SqlBuilderCore OrWhere(Expression> expression) { - return this.OrWhere(expression.Body); + return this.OrWhere(expression?.Body); } /// @@ -2311,7 +2414,7 @@ public SqlBuilderCore OrWhere(Expression> expression) /// SqlBuilderCore public SqlBuilderCore OrWhere(Expression> expression, ref bool hasWhere) { - return this.OrWhere(expression.Body, ref hasWhere); + return this.OrWhere(expression?.Body, ref hasWhere); } /// @@ -2323,7 +2426,7 @@ public SqlBuilderCore OrWhere(Expression> expression, ref bool public SqlBuilderCore OrWhere(Expression> expression) where T2 : class { - return this.OrWhere(expression.Body); + return this.OrWhere(expression?.Body); } /// @@ -2336,7 +2439,7 @@ public SqlBuilderCore OrWhere(Expression> expression) public SqlBuilderCore OrWhere(Expression> expression, ref bool hasWhere) where T2 : class { - return this.OrWhere(expression.Body, ref hasWhere); + return this.OrWhere(expression?.Body, ref hasWhere); } /// @@ -2350,7 +2453,7 @@ public SqlBuilderCore OrWhere(Expression> expre where T2 : class where T3 : class { - return this.OrWhere(expression.Body); + return this.OrWhere(expression?.Body); } /// @@ -2365,7 +2468,7 @@ public SqlBuilderCore OrWhere(Expression> expre where T2 : class where T3 : class { - return this.OrWhere(expression.Body, ref hasWhere); + return this.OrWhere(expression?.Body, ref hasWhere); } /// @@ -2381,7 +2484,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2398,7 +2501,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2416,7 +2519,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2435,7 +2538,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2455,7 +2558,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2476,7 +2579,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2498,7 +2601,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2521,7 +2624,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2545,7 +2648,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2570,7 +2673,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2596,7 +2699,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2623,7 +2726,7 @@ public SqlBuilderCore OrWhere(Expression @@ -2651,7 +2754,7 @@ public SqlBuilderCore OrWhere(Expression where T9 : class where T10 : class { - return this.OrWhere(expression.Body); + return this.OrWhere(expression?.Body); } /// @@ -2680,7 +2783,7 @@ public SqlBuilderCore OrWhere(Expression where T9 : class where T10 : class { - return this.OrWhere(expression.Body, ref hasWhere); + return this.OrWhere(expression?.Body, ref hasWhere); } #endregion @@ -2897,7 +3000,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression expression, ref bool /// public SqlBuilderCore WhereIf(bool condition, Expression> expression) { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -2909,7 +3012,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression> expre /// public SqlBuilderCore WhereIf(bool condition, Expression> expression, Action callback) { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -2921,7 +3024,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression> expre /// public SqlBuilderCore WhereIf(bool condition, Expression> expression, ref bool hasWhere) { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -2934,7 +3037,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression> expre /// public SqlBuilderCore WhereIf(bool condition, Expression> expression, ref bool hasWhere, Action callback) { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -2947,7 +3050,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression> expre public SqlBuilderCore WhereIf(bool condition, Expression> expression) where T2 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -2961,7 +3064,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression WhereIf(bool condition, Expression> expression, Action callback) where T2 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -2975,7 +3078,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression WhereIf(bool condition, Expression> expression, ref bool hasWhere) where T2 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -2990,7 +3093,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression WhereIf(bool condition, Expression> expression, ref bool hasWhere, Action callback) where T2 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -3005,7 +3108,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3021,7 +3124,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3037,7 +3140,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3054,7 +3157,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3071,7 +3174,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3089,7 +3192,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3107,7 +3210,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3126,7 +3229,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3145,7 +3248,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3165,7 +3268,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3185,7 +3288,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3206,7 +3309,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression @@ -3227,7 +3330,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression< where T5 : class where T6 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -3249,7 +3352,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression< where T5 : class where T6 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -3271,7 +3374,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression< where T5 : class where T6 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -3294,7 +3397,7 @@ public SqlBuilderCore WhereIf(bool condition, Expression< where T5 : class where T6 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -3317,7 +3420,7 @@ public SqlBuilderCore WhereIf(bool condition, Express where T6 : class where T7 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -3341,7 +3444,7 @@ public SqlBuilderCore WhereIf(bool condition, Express where T6 : class where T7 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -3365,7 +3468,7 @@ public SqlBuilderCore WhereIf(bool condition, Express where T6 : class where T7 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -3390,7 +3493,7 @@ public SqlBuilderCore WhereIf(bool condition, Express where T6 : class where T7 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -3415,7 +3518,7 @@ public SqlBuilderCore WhereIf(bool condition, Exp where T7 : class where T8 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -3441,7 +3544,7 @@ public SqlBuilderCore WhereIf(bool condition, Exp where T7 : class where T8 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -3467,7 +3570,7 @@ public SqlBuilderCore WhereIf(bool condition, Exp where T7 : class where T8 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -3494,7 +3597,7 @@ public SqlBuilderCore WhereIf(bool condition, Exp where T7 : class where T8 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -3521,7 +3624,7 @@ public SqlBuilderCore WhereIf(bool condition, where T8 : class where T9 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -3549,7 +3652,7 @@ public SqlBuilderCore WhereIf(bool condition, where T8 : class where T9 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -3577,7 +3680,7 @@ public SqlBuilderCore WhereIf(bool condition, where T8 : class where T9 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -3606,7 +3709,7 @@ public SqlBuilderCore WhereIf(bool condition, where T8 : class where T9 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } /// @@ -3635,7 +3738,7 @@ public SqlBuilderCore WhereIf(bool condi where T9 : class where T10 : class { - return this.WhereIf(condition, expression.Body); + return this.WhereIf(condition, expression?.Body); } /// @@ -3665,7 +3768,7 @@ public SqlBuilderCore WhereIf(bool condi where T9 : class where T10 : class { - return this.WhereIf(condition, expression.Body, callback); + return this.WhereIf(condition, expression?.Body, callback); } /// @@ -3695,7 +3798,7 @@ public SqlBuilderCore WhereIf(bool condi where T9 : class where T10 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere); + return this.WhereIf(condition, expression?.Body, ref hasWhere); } /// @@ -3726,7 +3829,7 @@ public SqlBuilderCore WhereIf(bool condi where T9 : class where T10 : class { - return this.WhereIf(condition, expression.Body, ref hasWhere, callback); + return this.WhereIf(condition, expression?.Body, ref hasWhere, callback); } #endregion @@ -3809,7 +3912,6 @@ public SqlBuilderCore WithKey(params dynamic[] keyValue) for (int i = 0; i < keys.Count; i++) { var columnInfo = keys[i]; - //var (key, property, dbType) = keys[i]; if (columnInfo.ColumnName.IsNotNullOrEmpty()) { this.sqlWrapper += $" {(sql.ContainsIgnoreCase("WHERE") || i > 0 ? "AND" : "WHERE")} {tableAlias + columnInfo.ColumnName} = "; @@ -3833,8 +3935,12 @@ public SqlBuilderCore WithKey(params dynamic[] keyValue) /// SqlBuilderCore public SqlBuilderCore GroupBy(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " GROUP BY "; this.sqlWrapper += sql; + return this; } @@ -3845,8 +3951,12 @@ public SqlBuilderCore GroupBy(string sql) /// SqlBuilderCore public SqlBuilderCore GroupBy(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " GROUP BY "; this.sqlWrapper += sql; + return this; } @@ -3857,8 +3967,12 @@ public SqlBuilderCore GroupBy(StringBuilder sql) /// SqlBuilderCore public SqlBuilderCore GroupBy(Expression expression) { + if (expression == null) + return this; + this.sqlWrapper += " GROUP BY "; SqlExpressionProvider.GroupBy(expression, this.sqlWrapper); + return this; } @@ -3869,7 +3983,7 @@ public SqlBuilderCore GroupBy(Expression expression) /// SqlBuilderCore public SqlBuilderCore GroupBy(Expression> expression) { - return this.GroupBy(expression.Body); + return this.GroupBy(expression?.Body); } /// @@ -3881,7 +3995,7 @@ public SqlBuilderCore GroupBy(Expression> expression) public SqlBuilderCore GroupBy(Expression> expression) where T2 : class { - return this.GroupBy(expression.Body); + return this.GroupBy(expression?.Body); } /// @@ -3895,7 +4009,7 @@ public SqlBuilderCore GroupBy(Expression> exp where T2 : class where T3 : class { - return this.GroupBy(expression.Body); + return this.GroupBy(expression?.Body); } /// @@ -3911,7 +4025,7 @@ public SqlBuilderCore GroupBy(Expression @@ -3929,7 +4043,7 @@ public SqlBuilderCore GroupBy(Expression @@ -3949,7 +4063,7 @@ public SqlBuilderCore GroupBy(Expression @@ -3971,7 +4085,7 @@ public SqlBuilderCore GroupBy(Expression @@ -3995,7 +4109,7 @@ public SqlBuilderCore GroupBy(Expression @@ -4021,7 +4135,7 @@ public SqlBuilderCore GroupBy(Expression @@ -4049,7 +4163,7 @@ public SqlBuilderCore GroupBy(Expression where T9 : class where T10 : class { - return this.GroupBy(expression.Body); + return this.GroupBy(expression?.Body); } #endregion @@ -4061,8 +4175,12 @@ public SqlBuilderCore GroupBy(Expression /// public SqlBuilderCore Having(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " HAVING "; this.sqlWrapper += sql; + return this; } @@ -4073,8 +4191,12 @@ public SqlBuilderCore Having(string sql) /// public SqlBuilderCore Having(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " HAVING "; this.sqlWrapper += sql; + return this; } @@ -4085,8 +4207,12 @@ public SqlBuilderCore Having(StringBuilder sql) /// public SqlBuilderCore Having(Expression expression) { + if (expression == null) + return this; + this.sqlWrapper += " HAVING "; SqlExpressionProvider.Having(expression, this.sqlWrapper); + return this; } @@ -4097,7 +4223,7 @@ public SqlBuilderCore Having(Expression expression) /// SqlBuilderCore public SqlBuilderCore Having(Expression> expression) { - return this.Having(expression.Body); + return this.Having(expression?.Body); } /// @@ -4109,7 +4235,7 @@ public SqlBuilderCore Having(Expression> expression) public SqlBuilderCore Having(Expression> expression) where T2 : class { - return this.Having(expression.Body); + return this.Having(expression?.Body); } /// @@ -4123,7 +4249,7 @@ public SqlBuilderCore Having(Expression> expr where T2 : class where T3 : class { - return this.Having(expression.Body); + return this.Having(expression?.Body); } /// @@ -4139,7 +4265,7 @@ public SqlBuilderCore Having(Expression @@ -4157,7 +4283,7 @@ public SqlBuilderCore Having(Expression @@ -4177,7 +4303,7 @@ public SqlBuilderCore Having(Expression @@ -4199,7 +4325,7 @@ public SqlBuilderCore Having(Expression @@ -4223,7 +4349,7 @@ public SqlBuilderCore Having(Expression @@ -4249,7 +4375,7 @@ public SqlBuilderCore Having(Expression @@ -4277,7 +4403,7 @@ public SqlBuilderCore Having(Expression< where T9 : class where T10 : class { - return this.Having(expression.Body); + return this.Having(expression?.Body); } #endregion @@ -4289,8 +4415,12 @@ public SqlBuilderCore Having(Expression< /// SqlBuilderCore public SqlBuilderCore OrderBy(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += " ORDER BY "; this.sqlWrapper += sql; + return this; } @@ -4301,8 +4431,12 @@ public SqlBuilderCore OrderBy(string sql) /// SqlBuilderCore public SqlBuilderCore OrderBy(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += " ORDER BY "; this.sqlWrapper += sql; + return this; } @@ -4314,8 +4448,12 @@ public SqlBuilderCore OrderBy(StringBuilder sql) /// SqlBuilderCore public SqlBuilderCore OrderBy(Expression expression, params OrderType[] orders) { + if (expression == null) + return this; + this.sqlWrapper += " ORDER BY "; SqlExpressionProvider.OrderBy(expression, this.sqlWrapper, orders); + return this; } @@ -4327,7 +4465,7 @@ public SqlBuilderCore OrderBy(Expression expression, params OrderType[] order /// SqlBuilderCore public SqlBuilderCore OrderBy(Expression> expression, params OrderType[] orders) { - return this.OrderBy(expression.Body, orders); + return this.OrderBy(expression?.Body, orders); } /// @@ -4340,7 +4478,7 @@ public SqlBuilderCore OrderBy(Expression> expression, params public SqlBuilderCore OrderBy(Expression> expression, params OrderType[] orders) where T2 : class { - return this.OrderBy(expression.Body, orders); + return this.OrderBy(expression?.Body, orders); } /// @@ -4355,7 +4493,7 @@ public SqlBuilderCore OrderBy(Expression> exp where T2 : class where T3 : class { - return this.OrderBy(expression.Body, orders); + return this.OrderBy(expression?.Body, orders); } /// @@ -4372,7 +4510,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4391,7 +4529,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4412,7 +4550,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4435,7 +4573,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4460,7 +4598,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4487,7 +4625,7 @@ public SqlBuilderCore OrderBy(Expression @@ -4516,7 +4654,7 @@ public SqlBuilderCore OrderBy(Expression where T9 : class where T10 : class { - return this.OrderBy(expression.Body, orders); + return this.OrderBy(expression?.Body, orders); } #endregion @@ -4539,7 +4677,7 @@ public SqlBuilderCore OrderByDescending(Expression> expressio else orders.Add(OrderType.Descending); - return this.OrderBy(expression.Body, orders.ToArray()); + return this.OrderBy(expression?.Body, orders.ToArray()); } #endregion @@ -4730,7 +4868,7 @@ public SqlBuilderCore Update(Expression> expression = null, bool var tableName = this.sqlWrapper.GetTableName(typeof(T)); this.sqlWrapper += $"UPDATE {tableNameFunc?.Invoke(tableName) ?? tableName} SET "; - SqlExpressionProvider.Update(expression.Body, this.sqlWrapper); + SqlExpressionProvider.Update(expression?.Body, this.sqlWrapper); return this; } @@ -4753,7 +4891,7 @@ public SqlBuilderCore Insert(Expression> expression = null, bool var tableName = this.sqlWrapper.GetTableName(typeof(T)); this.sqlWrapper += $"INSERT INTO {tableNameFunc?.Invoke(tableName) ?? tableName} ({{0}}) {(this.sqlWrapper.DatabaseType == DatabaseType.Oracle ? "SELECT" : "VALUES")} "; - SqlExpressionProvider.Insert(expression.Body, this.sqlWrapper); + SqlExpressionProvider.Insert(expression?.Body, this.sqlWrapper); return this; } @@ -4774,7 +4912,7 @@ public SqlBuilderCore Max(Expression> expression, Func Min(Expression> expression, Func Avg(Expression> expression, Func Sum(Expression> expression, Func Top(long topNumber) { this.sqlWrapper.Append($" LIMIT {topNumber} OFFSET 0"); } + return this; } #endregion @@ -5151,6 +5290,9 @@ public SqlBuilderCore Distinct() /// SqlBuilderCore public SqlBuilderCore Append(string sql) { + if (sql.IsNullOrEmpty()) + return this; + this.sqlWrapper += sql; return this; } @@ -5162,6 +5304,9 @@ public SqlBuilderCore Append(string sql) /// SqlBuilderCore public SqlBuilderCore Append(StringBuilder sql) { + if (sql == null) + return this; + this.sqlWrapper += sql; return this; } @@ -5176,7 +5321,7 @@ public SqlBuilderCore Append(StringBuilder sql) /// SqlBuilderCore public SqlBuilderCore AppendIf(bool condition, string sql) { - if (condition) + if (condition && sql.IsNotNullOrEmpty()) this.sqlWrapper += sql; return this; @@ -5191,7 +5336,7 @@ public SqlBuilderCore AppendIf(bool condition, string sql) /// SqlBuilderCore public SqlBuilderCore AppendIf(bool condition, string sql, Action callback) { - if (condition) + if (condition && sql.IsNotNullOrEmpty()) { this.sqlWrapper += sql; callback?.Invoke(); @@ -5208,7 +5353,7 @@ public SqlBuilderCore AppendIf(bool condition, string sql, Action callback) /// SqlBuilderCore public SqlBuilderCore AppendIf(bool condition, StringBuilder sql) { - if (condition) + if (condition && sql != null) this.sqlWrapper += sql; return this; @@ -5223,7 +5368,7 @@ public SqlBuilderCore AppendIf(bool condition, StringBuilder sql) /// SqlBuilderCore public SqlBuilderCore AppendIf(bool condition, StringBuilder sql, Action callback) { - if (condition) + if (condition && sql != null) { this.sqlWrapper += sql; callback?.Invoke(); @@ -5246,7 +5391,7 @@ public string GetTableName() #region GetPrimaryKey /// - /// 获取实体对应表的主键信息 + /// 获取实体对应表的主键名称 /// /// public List GetPrimaryKey()