mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 修复 Delete.Where in 查询为空的时候仍然执行删除;#1068
This commit is contained in:
parent
34011a6b5f
commit
a01478b4b4
@ -538,5 +538,14 @@
|
||||
<param name="that"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
|
||||
<summary>
|
||||
批量注入 Repository,可以参考代码自行调整
|
||||
</summary>
|
||||
<param name="services"></param>
|
||||
<param name="globalDataFilter"></param>
|
||||
<param name="assemblies"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
|
@ -256,13 +256,14 @@ namespace FreeSql.DataAnnotations
|
||||
public string[] GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sqlWhere)) return AllTables;
|
||||
var dictParams = new Dictionary<string, string>();
|
||||
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<string, string>();
|
||||
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");
|
||||
|
||||
|
@ -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<StringBuilder> 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<StringBuilder, Task> fetchAsync)
|
||||
{
|
||||
if (_whereTimes <= 0) return;
|
||||
if (_whereTimes <= 0 || _where.Length == 0) return;
|
||||
var newwhere = new StringBuilder().Append(" WHERE ").Append(_where);
|
||||
|
||||
if (_whereGlobalFilter.Any())
|
||||
|
@ -2268,11 +2268,19 @@ namespace FreeSql.Internal
|
||||
return char.IsLetter(name, 0) ? name : string.Concat("_", name);
|
||||
}
|
||||
|
||||
public static string ReplaceSqlConstString(string sql, Dictionary<string, string> parms)
|
||||
public static string ReplaceSqlConstString(string sql, Dictionary<string, string> 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,12 +2334,18 @@ namespace FreeSql.Internal
|
||||
var remidx = sql.IndexOf("WHERE ");
|
||||
if (remidx != -1) sql = sql.Substring(remidx + 6);
|
||||
|
||||
//sql = Regex.Replace(sql, @"\s*([@:\?][\w_]+)\s*(<|<=|>|>=|=)\s*((\w+)\s*\.)?([\w_]+)");
|
||||
return LocalProcessBrackets(sql);
|
||||
|
||||
|
||||
string LocalProcessBrackets(string locsql)
|
||||
{
|
||||
var sidx = 0;
|
||||
var ltcou = 0;
|
||||
var ltidxStack = new Stack<int>();
|
||||
while (sidx < sql.Length)
|
||||
while (sidx < locsql.Length)
|
||||
{
|
||||
var chr = sql[sidx++];
|
||||
var chr = locsql[sidx++];
|
||||
if (chr == '(')
|
||||
{
|
||||
ltcou++;
|
||||
@ -2341,31 +2355,51 @@ namespace FreeSql.Internal
|
||||
{
|
||||
ltcou--;
|
||||
var ltidx = ltidxStack.Pop();
|
||||
if (ltidx == 0 && sidx == sql.Length - 1)
|
||||
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;
|
||||
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);
|
||||
}
|
||||
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);
|
||||
sqlMidNew = sqlMid.Substring(1, sqlMid.Length - 2).Trim();
|
||||
break;
|
||||
case "or":
|
||||
sqlMidNew = "";
|
||||
break;
|
||||
case "not":
|
||||
break;
|
||||
}
|
||||
}
|
||||
sidx -= sqlMid.Length - sqlMidNew.Length;
|
||||
sql = $"{sqlLeft}{sqlMidNew}{sqlRight}";
|
||||
locsql = $"{sqlLeft}{sqlMidNew}{sqlRight}";
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
return locsql;
|
||||
}
|
||||
}
|
||||
|
||||
static string ParseSqlWhereLevel12(string sql)
|
||||
|
Loading…
x
Reference in New Issue
Block a user