FreeSql ISelect/IUpdate/IInsert/IDelete 增加 AsTable 方法,实现分表

This commit is contained in:
28810
2019-03-05 21:04:52 +08:00
parent f9fef12d37
commit 90f69f1b57
38 changed files with 813 additions and 87 deletions

View File

@ -15,6 +15,7 @@ namespace FreeSql.Internal.CommonProvider {
protected CommonExpression _commonExpression;
protected List<T1> _source = new List<T1>();
protected TableInfo _table;
protected Func<string, string> _tableRule;
protected StringBuilder _where = new StringBuilder();
protected int _whereTimes = 0;
protected List<DbParameter> _params = new List<DbParameter>();
@ -24,7 +25,6 @@ namespace FreeSql.Internal.CommonProvider {
_commonUtils = commonUtils;
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_where.Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(_table.DbName)).Append(" WHERE ");
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>();
}
@ -54,6 +54,10 @@ namespace FreeSql.Internal.CommonProvider {
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 string ToSql() => _whereTimes <= 0 ? null : _where.ToString();
public IDelete<T1> AsTable(Func<string, string> tableRule) {
_tableRule = tableRule;
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

@ -17,6 +17,7 @@ namespace FreeSql.Internal.CommonProvider {
protected List<T1> _source = new List<T1>();
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected TableInfo _table;
protected Func<string, string> _tableRule;
protected DbParameter[] _params;
public InsertProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression) {
@ -58,10 +59,14 @@ namespace FreeSql.Internal.CommonProvider {
return this;
}
public IInsert<T1> AsTable(Func<string, string> tableRule) {
_tableRule = tableRule;
return this;
}
public virtual string ToSql() {
if (_source == null || _source.Any() == false) return null;
var sb = new StringBuilder();
sb.Append("INSERT INTO ").Append(_commonUtils.QuoteSqlName(_table.DbName)).Append("(");
sb.Append("INSERT INTO ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append("(");
var colidx = 0;
foreach (var col in _table.Columns.Values)
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {

View File

@ -19,6 +19,7 @@ namespace FreeSql.Internal.CommonProvider {
protected StringBuilder _where = new StringBuilder();
protected List<DbParameter> _params = new List<DbParameter>();
internal List<SelectTableInfo> _tables = new List<SelectTableInfo>();
protected Func<Type, string, string> _tableRule;
protected StringBuilder _join = new StringBuilder();
protected (int seconds, string key) _cache = (0, null);
protected IFreeSql _orm;
@ -31,9 +32,13 @@ namespace FreeSql.Internal.CommonProvider {
toType.GetField("_limit", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._limit);
toType.GetField("_skip", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._skip);
toType.GetField("_select", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._select);
toType.GetField("_orderby", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._orderby);
toType.GetField("_groupby", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._groupby);
toType.GetField("_having", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._having);
toType.GetField("_where", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, new StringBuilder().Append(from._where.ToString()));
toType.GetField("_params", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, new List<DbParameter>(from._params.ToArray()));
toType.GetField("_tables", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, new List<SelectTableInfo>(from._tables.ToArray()));
toType.GetField("_tableRule", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._tableRule);
toType.GetField("_join", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, new StringBuilder().Append(from._join.ToString()));
toType.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._cache);
//toType.GetField("_orm", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._orm);
@ -397,6 +402,11 @@ namespace FreeSql.Internal.CommonProvider {
}
return (map, field.ToString());
}
public TSelect AsTable(Func<Type, string, string> tableRule) {
_tableRule = tableRule;
return this as TSelect;
}
public abstract string ToSql(string field = null);
public TSelect Where(string sql, object parms = null) => this.WhereIf(true, sql, parms);

View File

@ -17,6 +17,7 @@ namespace FreeSql.Internal.CommonProvider {
protected List<T1> _source = new List<T1>();
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected TableInfo _table;
protected Func<string, string> _tableRule;
protected StringBuilder _where = new StringBuilder();
protected StringBuilder _set = new StringBuilder();
protected List<DbParameter> _params = new List<DbParameter>();
@ -105,11 +106,15 @@ namespace FreeSql.Internal.CommonProvider {
protected abstract void ToSqlCase(StringBuilder caseWhen, ColumnInfo[] primarys);
protected abstract void ToSqlWhen(StringBuilder sb, ColumnInfo[] primarys, object d);
public IUpdate<T1> AsTable(Func<string, string> tableRule) {
_tableRule = tableRule;
return this;
}
public string ToSql() {
if (_where.Length == 0) return null;
var sb = new StringBuilder();
sb.Append("UPDATE ").Append(_commonUtils.QuoteSqlName(_table.DbName)).Append(" SET ");
sb.Append("UPDATE ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append(" SET ");
if (_set.Length > 0) { //指定 set 更新
sb.Append(_set.ToString().Substring(2));