diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index da7ace6b..bdd16ff9 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -538,5 +538,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql/DataAnnotations/TableAttribute.cs b/FreeSql/DataAnnotations/TableAttribute.cs index 3680ec84..d6d2a4f3 100644 --- a/FreeSql/DataAnnotations/TableAttribute.cs +++ b/FreeSql/DataAnnotations/TableAttribute.cs @@ -256,13 +256,14 @@ namespace FreeSql.DataAnnotations public string[] GetTableNamesBySqlWhere(string sqlWhere, List dbParams, SelectTableInfo tb, CommonUtils commonUtils) { if (string.IsNullOrWhiteSpace(sqlWhere)) return AllTables; - var dictParams = new Dictionary(); - var newSqlWhere = Utils.ReplaceSqlConstString(sqlWhere, dictParams); - var tsqlWhere = Utils.ParseSqlWhereLevel1(sqlWhere); - var quoteParameterName = commonUtils.QuoteParamterName(""); var quoteParameterNameCharArray = quoteParameterName.ToCharArray(); var columnName = commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name); + + var dictParams = new Dictionary(); + var newSqlWhere = Utils.ReplaceSqlConstString(sqlWhere, dictParams, quoteParameterName); + //var tsqlWhere = Utils.ParseSqlWhereLevel1(sqlWhere); + var regs = GetRegSqlWhereDateTimes($"{(string.IsNullOrWhiteSpace(tb.Alias) ? "" : $"{tb.Alias}.")}{commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name)}", quoteParameterName); for (var a = 0; a < 8; a++) newSqlWhere = regs[a].Replace(newSqlWhere, "$1$4"); diff --git a/FreeSql/Internal/CommonProvider/DeleteProvider.cs b/FreeSql/Internal/CommonProvider/DeleteProvider.cs index e3f2348e..89554481 100644 --- a/FreeSql/Internal/CommonProvider/DeleteProvider.cs +++ b/FreeSql/Internal/CommonProvider/DeleteProvider.cs @@ -170,7 +170,7 @@ namespace FreeSql.Internal.CommonProvider public virtual string ToSql() { - if (_whereTimes <= 0) return null; + if (_whereTimes <= 0 || _where.Length == 0) return null; var sb = new StringBuilder(); ToSqlFetch(sql => { @@ -182,7 +182,7 @@ namespace FreeSql.Internal.CommonProvider public void ToSqlFetch(Action fetch) { - if (_whereTimes <= 0) return; + if (_whereTimes <= 0 || _where.Length == 0) return; var newwhere = new StringBuilder().Append(" WHERE ").Append(_where); if (_whereGlobalFilter.Any()) @@ -215,7 +215,7 @@ namespace FreeSql.Internal.CommonProvider #else async public Task ToSqlFetchAsync(Func fetchAsync) { - if (_whereTimes <= 0) return; + if (_whereTimes <= 0 || _where.Length == 0) return; var newwhere = new StringBuilder().Append(" WHERE ").Append(_where); if (_whereGlobalFilter.Any()) diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 13cf5235..72a03c23 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -2268,11 +2268,19 @@ namespace FreeSql.Internal return char.IsLetter(name, 0) ? name : string.Concat("_", name); } - public static string ReplaceSqlConstString(string sql, Dictionary parms) + public static string ReplaceSqlConstString(string sql, Dictionary parms, string paramPrefix = "@") { var nsb = new StringBuilder(); var sidx = 0; var pidx = 0; + var ptmpPrefix = ""; + while (true) + { + pidx++; + ptmpPrefix = $"{paramPrefix}p{pidx}"; + if (sql.Contains(ptmpPrefix) == false) break; + } + pidx = 0; while (sidx < sql.Length) { var chr = sql[sidx++]; @@ -2307,7 +2315,7 @@ namespace FreeSql.Internal while (true) { pidx++; - pname = $"@p{pidx}"; + pname = $"{ptmpPrefix}{pidx}"; if (parms.ContainsKey(pname) == false) break; } } @@ -2326,46 +2334,72 @@ namespace FreeSql.Internal var remidx = sql.IndexOf("WHERE "); if (remidx != -1) sql = sql.Substring(remidx + 6); - var sidx = 0; - var ltcou = 0; - var ltidxStack = new Stack(); - while (sidx < sql.Length) + //sql = Regex.Replace(sql, @"\s*([@:\?][\w_]+)\s*(<|<=|>|>=|=)\s*((\w+)\s*\.)?([\w_]+)"); + return LocalProcessBrackets(sql); + + + string LocalProcessBrackets(string locsql) { - var chr = sql[sidx++]; - if (chr == '(') + var sidx = 0; + var ltcou = 0; + var ltidxStack = new Stack(); + while (sidx < locsql.Length) { - ltcou++; - ltidxStack.Push(sidx - 1); - } - if (chr == ')') - { - ltcou--; - var ltidx = ltidxStack.Pop(); - if (ltidx == 0 && sidx == sql.Length - 1) - break; - var sqlLeft = ltidx == 0 ? "" : sql.Remove(ltidx); - var sqlMid = sql.Substring(ltidx, sidx - ltidx); - var sqlMidNew = ""; - var sqlRight = sidx == sql.Length - 1 ? "" : sql.Substring(sidx + 1); - var mLeft = Regex.Match(sqlLeft, @" (and|or|not)\s*$", RegexOptions.IgnoreCase); - if (mLeft.Success) + var chr = locsql[sidx++]; + if (chr == '(') { - switch (mLeft.Groups[1].Value) - { - case "and": - sqlMidNew = sqlMid.Substring(1, sqlMid.Length - 2); - break; - case "or": - break; - case "not": - break; - } + ltcou++; + ltidxStack.Push(sidx - 1); + } + if (chr == ')') + { + ltcou--; + var ltidx = ltidxStack.Pop(); + var ltidx2 = ltidx; + var sidx2 = sidx; + while(sidx < locsql.Length) + { + var chr2 = locsql[sidx]; + if (chr2 == ')') + { + if (ltidxStack.First() == ltidx - 1) + { + ltidx = ltidxStack.Pop(); + sidx++; + } + } + break; + } + if (ltidx == 0 && sidx == locsql.Length) + { + locsql = locsql.Substring(1, sidx - 2); + break; + } + var sqlLeft = ltidx == 0 ? "" : locsql.Remove(ltidx); + var sqlMid = locsql.Substring(ltidx, sidx - ltidx); + var sqlMidNew = sqlMid; + var sqlRight = sidx == locsql.Length ? "" : locsql.Substring(sidx); + var mLeft = Regex.Match(sqlLeft, @" (and|or|not)\s*$", RegexOptions.IgnoreCase); + if (mLeft.Success) + { + switch (mLeft.Groups[1].Value) + { + case "and": + sqlMidNew = sqlMid.Substring(1, sqlMid.Length - 2).Trim(); + break; + case "or": + sqlMidNew = ""; + break; + case "not": + break; + } + } + sidx -= sqlMid.Length - sqlMidNew.Length; + locsql = $"{sqlLeft}{sqlMidNew}{sqlRight}"; } - sidx -= sqlMid.Length - sqlMidNew.Length; - sql = $"{sqlLeft}{sqlMidNew}{sqlRight}"; } + return locsql; } - return sql; } static string ParseSqlWhereLevel12(string sql)