Skip to content

Commit a01478b

Browse files
committed
- 修复 Delete.Where in 查询为空的时候仍然执行删除;#1068
1 parent 34011a6 commit a01478b

File tree

4 files changed

+85
-41
lines changed

4 files changed

+85
-41
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/DataAnnotations/TableAttribute.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,14 @@ static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterN
256256
public string[] GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
257257
{
258258
if (string.IsNullOrWhiteSpace(sqlWhere)) return AllTables;
259-
var dictParams = new Dictionary<string, string>();
260-
var newSqlWhere = Utils.ReplaceSqlConstString(sqlWhere, dictParams);
261-
var tsqlWhere = Utils.ParseSqlWhereLevel1(sqlWhere);
262-
263259
var quoteParameterName = commonUtils.QuoteParamterName("");
264260
var quoteParameterNameCharArray = quoteParameterName.ToCharArray();
265261
var columnName = commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name);
262+
263+
var dictParams = new Dictionary<string, string>();
264+
var newSqlWhere = Utils.ReplaceSqlConstString(sqlWhere, dictParams, quoteParameterName);
265+
//var tsqlWhere = Utils.ParseSqlWhereLevel1(sqlWhere);
266+
266267
var regs = GetRegSqlWhereDateTimes($"{(string.IsNullOrWhiteSpace(tb.Alias) ? "" : $"{tb.Alias}.")}{commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name)}", quoteParameterName);
267268
for (var a = 0; a < 8; a++) newSqlWhere = regs[a].Replace(newSqlWhere, "$1$4");
268269

FreeSql/Internal/CommonProvider/DeleteProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public IDelete<T1> AsType(Type entityType)
170170

171171
public virtual string ToSql()
172172
{
173-
if (_whereTimes <= 0) return null;
173+
if (_whereTimes <= 0 || _where.Length == 0) return null;
174174
var sb = new StringBuilder();
175175
ToSqlFetch(sql =>
176176
{
@@ -182,7 +182,7 @@ public virtual string ToSql()
182182

183183
public void ToSqlFetch(Action<StringBuilder> fetch)
184184
{
185-
if (_whereTimes <= 0) return;
185+
if (_whereTimes <= 0 || _where.Length == 0) return;
186186
var newwhere = new StringBuilder().Append(" WHERE ").Append(_where);
187187

188188
if (_whereGlobalFilter.Any())
@@ -215,7 +215,7 @@ public void ToSqlFetch(Action<StringBuilder> fetch)
215215
#else
216216
async public Task ToSqlFetchAsync(Func<StringBuilder, Task> fetchAsync)
217217
{
218-
if (_whereTimes <= 0) return;
218+
if (_whereTimes <= 0 || _where.Length == 0) return;
219219
var newwhere = new StringBuilder().Append(" WHERE ").Append(_where);
220220

221221
if (_whereGlobalFilter.Any())

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,11 +2268,19 @@ public static string GetCsName(string name)
22682268
return char.IsLetter(name, 0) ? name : string.Concat("_", name);
22692269
}
22702270

2271-
public static string ReplaceSqlConstString(string sql, Dictionary<string, string> parms)
2271+
public static string ReplaceSqlConstString(string sql, Dictionary<string, string> parms, string paramPrefix = "@")
22722272
{
22732273
var nsb = new StringBuilder();
22742274
var sidx = 0;
22752275
var pidx = 0;
2276+
var ptmpPrefix = "";
2277+
while (true)
2278+
{
2279+
pidx++;
2280+
ptmpPrefix = $"{paramPrefix}p{pidx}";
2281+
if (sql.Contains(ptmpPrefix) == false) break;
2282+
}
2283+
pidx = 0;
22762284
while (sidx < sql.Length)
22772285
{
22782286
var chr = sql[sidx++];
@@ -2307,7 +2315,7 @@ public static string ReplaceSqlConstString(string sql, Dictionary<string, string
23072315
while (true)
23082316
{
23092317
pidx++;
2310-
pname = $"@p{pidx}";
2318+
pname = $"{ptmpPrefix}{pidx}";
23112319
if (parms.ContainsKey(pname) == false) break;
23122320
}
23132321
}
@@ -2326,46 +2334,72 @@ internal static string ParseSqlWhereLevel1(string sql)
23262334
var remidx = sql.IndexOf("WHERE ");
23272335
if (remidx != -1) sql = sql.Substring(remidx + 6);
23282336

2329-
var sidx = 0;
2330-
var ltcou = 0;
2331-
var ltidxStack = new Stack<int>();
2332-
while (sidx < sql.Length)
2337+
//sql = Regex.Replace(sql, @"\s*([@:\?][\w_]+)\s*(<|<=|>|>=|=)\s*((\w+)\s*\.)?([\w_]+)");
2338+
return LocalProcessBrackets(sql);
2339+
2340+
2341+
string LocalProcessBrackets(string locsql)
23332342
{
2334-
var chr = sql[sidx++];
2335-
if (chr == '(')
2336-
{
2337-
ltcou++;
2338-
ltidxStack.Push(sidx - 1);
2339-
}
2340-
if (chr == ')')
2343+
var sidx = 0;
2344+
var ltcou = 0;
2345+
var ltidxStack = new Stack<int>();
2346+
while (sidx < locsql.Length)
23412347
{
2342-
ltcou--;
2343-
var ltidx = ltidxStack.Pop();
2344-
if (ltidx == 0 && sidx == sql.Length - 1)
2345-
break;
2346-
var sqlLeft = ltidx == 0 ? "" : sql.Remove(ltidx);
2347-
var sqlMid = sql.Substring(ltidx, sidx - ltidx);
2348-
var sqlMidNew = "";
2349-
var sqlRight = sidx == sql.Length - 1 ? "" : sql.Substring(sidx + 1);
2350-
var mLeft = Regex.Match(sqlLeft, @" (and|or|not)\s*$", RegexOptions.IgnoreCase);
2351-
if (mLeft.Success)
2348+
var chr = locsql[sidx++];
2349+
if (chr == '(')
2350+
{
2351+
ltcou++;
2352+
ltidxStack.Push(sidx - 1);
2353+
}
2354+
if (chr == ')')
23522355
{
2353-
switch (mLeft.Groups[1].Value)
2356+
ltcou--;
2357+
var ltidx = ltidxStack.Pop();
2358+
var ltidx2 = ltidx;
2359+
var sidx2 = sidx;
2360+
while(sidx < locsql.Length)
23542361
{
2355-
case "and":
2356-
sqlMidNew = sqlMid.Substring(1, sqlMid.Length - 2);
2357-
break;
2358-
case "or":
2359-
break;
2360-
case "not":
2361-
break;
2362+
var chr2 = locsql[sidx];
2363+
if (chr2 == ')')
2364+
{
2365+
if (ltidxStack.First() == ltidx - 1)
2366+
{
2367+
ltidx = ltidxStack.Pop();
2368+
sidx++;
2369+
}
2370+
}
2371+
break;
2372+
}
2373+
if (ltidx == 0 && sidx == locsql.Length)
2374+
{
2375+
locsql = locsql.Substring(1, sidx - 2);
2376+
break;
2377+
}
2378+
var sqlLeft = ltidx == 0 ? "" : locsql.Remove(ltidx);
2379+
var sqlMid = locsql.Substring(ltidx, sidx - ltidx);
2380+
var sqlMidNew = sqlMid;
2381+
var sqlRight = sidx == locsql.Length ? "" : locsql.Substring(sidx);
2382+
var mLeft = Regex.Match(sqlLeft, @" (and|or|not)\s*$", RegexOptions.IgnoreCase);
2383+
if (mLeft.Success)
2384+
{
2385+
switch (mLeft.Groups[1].Value)
2386+
{
2387+
case "and":
2388+
sqlMidNew = sqlMid.Substring(1, sqlMid.Length - 2).Trim();
2389+
break;
2390+
case "or":
2391+
sqlMidNew = "";
2392+
break;
2393+
case "not":
2394+
break;
2395+
}
23622396
}
2397+
sidx -= sqlMid.Length - sqlMidNew.Length;
2398+
locsql = $"{sqlLeft}{sqlMidNew}{sqlRight}";
23632399
}
2364-
sidx -= sqlMid.Length - sqlMidNew.Length;
2365-
sql = $"{sqlLeft}{sqlMidNew}{sqlRight}";
23662400
}
2401+
return locsql;
23672402
}
2368-
return sql;
23692403
}
23702404

23712405
static string ParseSqlWhereLevel12(string sql)

0 commit comments

Comments
 (0)