Skip to content

Commit

Permalink
- 增加 单条 WhereObject + GenerateCommandParameterWithLambda 参数化;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jul 28, 2024
1 parent c0bb06e commit 934be9c
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 97 deletions.
8 changes: 0 additions & 8 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

246 changes: 198 additions & 48 deletions FreeSql.Tests/FreeSql.Tests/Internal/CommonUtilsTest.cs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions FreeSql/Internal/CommonProvider/DeleteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public DeleteProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
}
Expand Down Expand Up @@ -118,8 +118,8 @@ public IDelete<T1> WhereIf(bool condition, string sql, object parms = null)
public IDelete<T1> Where(T1 item) => this.Where(new[] { item });
public IDelete<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table.Primarys, "", items, _params));
public IDelete<T1> WhereDynamic(object dywhere, bool not = false) => not == false ?
this.Where(_commonUtils.WhereObject(_table, "", dywhere)) :
this.Where($"not({_commonUtils.WhereObject(_table, "", dywhere)})");
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params)) :
this.Where($"not({_commonUtils.WhereObject(_table, "", dywhere, _params)})");
public IDelete<T1> WhereDynamicFilter(DynamicFilterInfo filter)
{
var alias = "t_" + Guid.NewGuid().ToString("n").Substring(0, 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public Select0Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression c
_commonUtils = commonUtils;
_commonExpression = commonExpression;
_tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T1)), Alias = "a", On = null, Type = SelectTableInfoType.From });
this.Where(_commonUtils.WhereObject(_tables.First().Table, "a.", dywhere));
this.Where(_commonUtils.WhereObject(_tables.First().Table, "a.", dywhere, _params));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public ISelect<T1> WhereDynamic(object dywhere, bool not = false)
.Where(oldwhere)
.WhereIf(string.IsNullOrWhiteSpace(newwhere) == false, $"not({newwhere})");
}
var wheresql = _commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere);
var wheresql = _commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere, _params);
return not == false ? this.Where(wheresql) : this.Where($"not({wheresql})");
}

Expand Down
6 changes: 3 additions & 3 deletions FreeSql/Internal/CommonProvider/UpdateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public UpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co
_versionColumn = _table?.VersionColumn;
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
IgnoreCanUpdate();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
Expand Down Expand Up @@ -800,8 +800,8 @@ public IUpdate<T1> Where(string sql, object parms = null)
public IUpdate<T1> Where(T1 item) => this.Where(new[] { item });
public IUpdate<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table.Primarys, "", items, _params));
public IUpdate<T1> WhereDynamic(object dywhere, bool not = false) => not == false ?
this.Where(_commonUtils.WhereObject(_table, "", dywhere)) :
this.Where($"not({_commonUtils.WhereObject(_table, "", dywhere)})");
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params)) :
this.Where($"not({_commonUtils.WhereObject(_table, "", dywhere, _params)})");
public IUpdate<T1> WhereDynamicFilter(DynamicFilterInfo filter)
{
var alias = "t_" + Guid.NewGuid().ToString("n").Substring(0, 8);
Expand Down
86 changes: 53 additions & 33 deletions FreeSql/Internal/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,35 +458,53 @@ public IndexAttribute[] GetEntityIndexAttribute(Type type)
}
return ret.Values.ToArray();
}

public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
public string WhereObject(TableInfo table, string aliasAndDot, object dywhere, List<DbParameter> dbParams)
{
if (dywhere == null) return "";
var type = dywhere.GetType();
var primarys = table.Primarys;
var pk1 = primarys.FirstOrDefault();
if (primarys.Length == 1 && (type == pk1.CsType || type.IsNumberType() && pk1.CsType.IsNumberType()))
{
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {RewriteColumn(pk1, GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere)))}";
}
else if (primarys.Length > 0 && (type == table.Type || type.BaseType == table.Type))
string LocalGetFilterSql(ColumnInfo[] cols, object[] objs)
{
var sb = new StringBuilder();
var pkidx = 0;
foreach (var pk in primarys)
string filter = "";
var colidx = 0;
foreach (var col in cols)
{
if (pkidx > 0) sb.Append(" AND ");
sb.Append(aliasAndDot).Append(this.QuoteSqlName(pk.Attribute.Name)).Append(" = ");
sb.Append(RewriteColumn(pk, GetNoneParamaterSqlValue(null, null, pk, pk.Attribute.MapType, pk.GetDbValue(dywhere))));
++pkidx;
var colval = Utils.GetDataReaderValue(col.Attribute.MapType, objs[colidx]);
filter += $"{(colidx > 0 ? " AND " : "")}{aliasAndDot}{this.QuoteSqlName(col.Attribute.Name)} = ";
if (CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null) //v3.5.100 单条支持参数化
{
AppendParamter(dbParams, null, col, col.Attribute.MapType, colval);
filter += RewriteColumn(col, QuoteWriteParamterAdapter(col.Attribute.MapType, QuoteParamterName($"exp_{dbParams.Count}")));
}
else
filter += RewriteColumn(col, GetNoneParamaterSqlValue(null, null, col, col.Attribute.MapType, colval));
++colidx;
}
return sb.ToString();
return filter;
}
else if (primarys.Length == 1 && type == typeof(string))
object LocalGetSingleElement(IEnumerable ie)
{
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {RewriteColumn(pk1, GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere)))}";
var idx = 0;
object firstElement = null;
foreach (var i in ie)
{
if (i == null) continue;
if (idx == 0) firstElement = i;
++idx;
if (idx > 1) break;
}
return idx == 1 ? firstElement : null;
}
else if (primarys.Length == 1 && dywhere is IEnumerable)

if (dywhere == null) return "";
var type = dywhere.GetType();
var primarys = table.Primarys;
var pk1 = primarys.FirstOrDefault();
if (primarys.Length == 1 && (type == pk1.CsType || type.IsNumberType() && pk1.CsType.IsNumberType()))
return LocalGetFilterSql(primarys, new object[] { dywhere });
if (primarys.Length > 0 && (type == table.Type || type.BaseType == table.Type))
return LocalGetFilterSql(primarys, primarys.Select(pk => pk.GetDbValue(dywhere)).ToArray());
if (primarys.Length == 1 && type == typeof(string))
return LocalGetFilterSql(primarys, new object[] { dywhere });

if (primarys.Length == 1 && dywhere is IEnumerable)
{
var sb = new StringBuilder();
var ie = dywhere as IEnumerable;
Expand All @@ -509,25 +527,27 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
idx = 1;
}
if (idx > 1) sb.Append(",");
var val = isEntityType ? RewriteColumn(primarys[0], GetNoneParamaterSqlValue(null, null, primarys[0], primarys[0].Attribute.MapType, primarys[0].GetDbValue(i))) :
RewriteColumn(pk1, GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, i)));
sb.Append(val);
if (ieidx == 0) ieidx1ret = $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {val}";
ieidx++;
var pkval = isEntityType ? pk1.GetDbValue(i) : i;
if (ieidx == 0) ieidx1ret = LocalGetFilterSql(new[] { pk1 }, new object[] { pkval });
sb.Append(RewriteColumn(pk1, GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, pkval)));
++ieidx;
}
if (ieidx == 0) return "";
if (ieidx == 1) return ieidx1ret;
sb.Append(')');
return sb.ToString();
}
else if (dywhere is IEnumerable)
if (dywhere is IEnumerable)
{

var sb = new StringBuilder();
var ie = dywhere as IEnumerable;
var ieSingle = LocalGetSingleElement(ie);
if (ieSingle != null) return WhereObject(table, aliasAndDot, ieSingle, dbParams);
var ieidx = 0;
foreach (var i in ie)
{
var fw = WhereObject(table, aliasAndDot, i);
var fw = WhereObject(table, aliasAndDot, i, dbParams);
if (string.IsNullOrEmpty(fw)) continue;
if (ieidx > 0) sb.Append(" OR ");
sb.Append(fw);
Expand All @@ -547,8 +567,7 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
if (trycol == null) continue;

if (psidx > 0) sb.Append(" AND ");
sb.Append(aliasAndDot).Append(this.QuoteSqlName(trycol.Attribute.Name)).Append(" = ");
sb.Append(RewriteColumn(trycol, GetNoneParamaterSqlValue(null, null, trycol, trycol.Attribute.MapType, Utils.GetDataReaderValue(trycol.Attribute.MapType, p.GetValue(dywhere, null)))));
sb.Append(LocalGetFilterSql(new[] { trycol }, new object[] { p.GetValue(dywhere, null) }));
++psidx;
}
if (psidx == 0) return "";
Expand All @@ -574,9 +593,10 @@ public string WhereItems<TEntity>(ColumnInfo[] primarys, string aliasAndDot, IEn
if (CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null) //v3.5.100 单条支持参数化
{
AppendParamter(dbParams, null, primarys[0], primarys[0].Attribute.MapType, pkval);
filter += QuoteWriteParamterAdapter(primarys[0].Attribute.MapType, QuoteParamterName($"exp_{dbParams.Count}"));
filter += RewriteColumn(pk, QuoteWriteParamterAdapter(primarys[0].Attribute.MapType, QuoteParamterName($"exp_{dbParams.Count}")));
}
filter += RewriteColumn(pk, GetNoneParamaterSqlValue(null, null, pk, pk.Attribute.MapType, pkval));
else
filter += RewriteColumn(pk, GetNoneParamaterSqlValue(null, null, pk, pk.Attribute.MapType, pkval));
}
return primarys.Length > 1 ? filter.Remove(0, 5) : filter;
}
Expand Down

0 comments on commit 934be9c

Please sign in to comment.