## v0.5.1(五一版)

- 增加 ISelect/IInsert/IUpdate/IDelete.AsType 实现弱类型curd,如:Select<object>().AsType(实体类型);
- 补充 ISelect.From<T2>;
- 补充 ExpressionTree 单元测试;
- 优化 ToList(a => new Dto()),会按优先级查询 Join 实体属性;
- 补充 IDelete/ISelect/IUpdate WhereDynamic 方法,实现 dywhere 条件;
- 修复 WhereObject 内部方法,当开启 Lazy 延时属性时,并且传递实体查询时条件无效;
This commit is contained in:
28810
2019-04-17 00:52:02 +08:00
parent f011d51f3b
commit bada8ad3cc
12 changed files with 88 additions and 38 deletions

View File

@ -28,7 +28,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
}
protected void ClearData() {
@ -81,11 +81,21 @@ namespace FreeSql.Internal.CommonProvider {
public IDelete<T1> Where(T1 item) => this.Where(new[] { item });
public IDelete<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IDelete<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
public IDelete<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
public IDelete<T1> AsTable(Func<string, string> tableRule) {
_tableRule = tableRule;
return this;
}
public IDelete<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IDelete.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IDelete.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public string ToSql() => _whereTimes <= 0 ? null : new StringBuilder().Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append(" WHERE ").Append(_where).ToString();
}
}

View File

@ -30,7 +30,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
}
protected void ClearData() {
@ -356,6 +356,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule;
return this;
}
public IInsert<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IInsert.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IInsert.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public virtual string ToSql() {
if (_source == null || _source.Any() == false) return null;
var sb = new StringBuilder();

View File

@ -523,8 +523,7 @@ namespace FreeSql.Internal.CommonProvider {
if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object");
if (entityType == _tables[0].Table.Type) return this as TSelect;
var newtb = _commonUtils.GetTableByEntity(entityType);
if (newtb == null) throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型");
_tables[0].Table = newtb;
_tables[0].Table = newtb ?? throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this as TSelect;
}

View File

@ -218,6 +218,7 @@ namespace FreeSql.Internal.CommonProvider {
_tables[0].Parameter = exp.Parameters[0];
return this.InternalWhere(exp?.Body);
}
public ISelect<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere));
public ISelect<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp) {
if (condition == false) return this;

View File

@ -35,7 +35,7 @@ namespace FreeSql.Internal.CommonProvider {
_table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
}
protected void ClearData() {
@ -69,7 +69,7 @@ namespace FreeSql.Internal.CommonProvider {
if (affrows != _source.Count)
throw new Exception($"记录可能不存在,或者【行级乐观锁】版本过旧,更新数量{_source.Count},影响的行数{affrows}。");
foreach (var d in _source)
_orm.SetEntityIncrByWithPropertyName(d, _table.VersionColumn.CsName, 1);
_orm.SetEntityIncrByWithPropertyName(_table.Type, d, _table.VersionColumn.CsName, 1);
}
}
@ -347,6 +347,7 @@ namespace FreeSql.Internal.CommonProvider {
public IUpdate<T1> Where(T1 item) => this.Where(new[] { item });
public IUpdate<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
public IUpdate<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
protected string WhereCaseSource(string CsName, Func<string, string> thenValue) {
if (_source.Any() == false) return null;
@ -401,6 +402,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule;
return this;
}
public IUpdate<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IUpdate.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IUpdate.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public string ToSql() {
if (_where.Length == 0 && _source.Any() == false) return null;