using FreeSql.Internal.Model; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace FreeSql.Internal.CommonProvider { public abstract class Select9Provider : Select0Provider, T1>, ISelect where T2 : class where T3 : class where T4 : class where T5 : class where T6 : class where T7 : class where T8 : class where T9 : class { public Select9Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9)); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T2)), Alias = $"SP10b", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T3)), Alias = $"SP10c", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T4)), Alias = $"SP10d", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T5)), Alias = $"SP10e", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T6)), Alias = $"SP10f", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T7)), Alias = $"SP10g", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T8)), Alias = $"SP10h", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T9)), Alias = $"SP10i", On = null, Type = SelectTableInfoType.From }); } ISelect ISelect.WithSql(string sqlT1, string sqlT2, string sqlT3, string sqlT4, string sqlT5, string sqlT6, string sqlT7, string sqlT8, string sqlT9, object parms) { this.AsTable((type, old) => { if (type == _tables[0].Table?.Type && string.IsNullOrEmpty(sqlT1) == false) return $"({sqlT1})"; if (type == _tables[1].Table?.Type && string.IsNullOrEmpty(sqlT2) == false) return $"({sqlT2})"; if (type == _tables[2].Table?.Type && string.IsNullOrEmpty(sqlT3) == false) return $"({sqlT3})"; if (type == _tables[3].Table?.Type && string.IsNullOrEmpty(sqlT4) == false) return $"({sqlT4})"; if (type == _tables[4].Table?.Type && string.IsNullOrEmpty(sqlT5) == false) return $"({sqlT5})"; if (type == _tables[5].Table?.Type && string.IsNullOrEmpty(sqlT6) == false) return $"({sqlT6})"; if (type == _tables[6].Table?.Type && string.IsNullOrEmpty(sqlT7) == false) return $"({sqlT7})"; if (type == _tables[7].Table?.Type && string.IsNullOrEmpty(sqlT8) == false) return $"({sqlT8})"; if (type == _tables[8].Table?.Type && string.IsNullOrEmpty(sqlT9) == false) return $"({sqlT9})"; return old; }); if (parms != null) _params.AddRange(_commonUtils.GetDbParamtersByObject($"{sqlT1};\r\n{sqlT2};\r\n{sqlT3};\r\n{sqlT4};\r\n{sqlT5};\r\n{sqlT6};\r\n{sqlT7};\r\n{sqlT8};\r\n{sqlT9}", parms)); return this; } double ISelect.Avg(Expression> column) { if (column == null) return default(double); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalAvg(column?.Body); } ISelectGrouping> ISelect.GroupBy(Expression> exp) { if (exp == null) return this.InternalGroupBy>(exp?.Body); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.InternalGroupBy>(exp?.Body); } TMember ISelect.Max(Expression> column) { if (column == null) return default(TMember); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalMax(column?.Body); } TMember ISelect.Min(Expression> column) { if (column == null) return default(TMember); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalMin(column?.Body); } ISelect ISelect.OrderBy(Expression> column) { if (column == null) this.InternalOrderBy(column?.Body); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalOrderBy(column?.Body); } ISelect ISelect.OrderByDescending(Expression> column) { if (column == null) this.InternalOrderBy(column?.Body); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalOrderByDescending(column?.Body); } ISelect ISelect.OrderByIf(bool condition, Expression> column, bool descending) { if (condition == false || column == null) this.InternalOrderBy(column?.Body); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return descending ? this.InternalOrderByDescending(column?.Body) : this.InternalOrderBy(column?.Body); } decimal ISelect.Sum(Expression> column) { if (column == null) this.InternalOrderBy(column?.Body); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalSum(column?.Body); } TReturn ISelect.ToAggregate(Expression, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, TReturn>> select) { if (select == null) return default(TReturn); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToAggregate(select?.Body); } ISelect ISelect.Aggregate(Expression, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, TReturn>> select, out TReturn result) { result = (this as ISelect).ToAggregate(select); return this; } List ISelect.ToList(Expression> select) { if (select == null) return this.InternalToList(select?.Body); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToList(select?.Body); } List ISelect.ToList() => (this as ISelect).ToList(GetToListDtoSelector()); Expression> GetToListDtoSelector() { return Expression.Lambda>( typeof(TDto).InternalNewExpression(), _tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"), Expression.Parameter(typeof(T2), "b"), Expression.Parameter(typeof(T3), "c"), Expression.Parameter(typeof(T4), "d"), Expression.Parameter(typeof(T5), "e"), Expression.Parameter(typeof(T6), "f"), Expression.Parameter(typeof(T7), "g"), Expression.Parameter(typeof(T8), "h"), Expression.Parameter(typeof(T9), "i")); } public void ToChunk(Expression> select, int size, Action>> done) { if (select == null || done == null) return; for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; this.InternalToChunk(select.Body, size, done); } DataTable ISelect.ToDataTable(Expression> select) { if (select == null) return this.InternalToDataTable(select?.Body); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToDataTable(select?.Body); } int ISelect.InsertInto(string tableName, Expression> select) { if (select == null) return this.InternalInsertInto(tableName, select); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalInsertInto(tableName, select?.Body); } string ISelect.ToSql(Expression> select, FieldAliasOptions fieldAlias) { if (select == null) return this.InternalToSql(select?.Body, fieldAlias); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToSql(select?.Body, fieldAlias); } ISelect ISelect.LeftJoin(Expression> exp) { if (exp == null) return this.InternalJoin(exp?.Body, SelectTableInfoType.LeftJoin); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.InternalJoin(exp?.Body, SelectTableInfoType.LeftJoin); } ISelect ISelect.InnerJoin(Expression> exp) { if (exp == null) return this.InternalJoin(exp?.Body, SelectTableInfoType.LeftJoin); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.InternalJoin(exp?.Body, SelectTableInfoType.InnerJoin); } ISelect ISelect.RightJoin(Expression> exp) { if (exp == null) return this.InternalJoin(exp?.Body, SelectTableInfoType.LeftJoin); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.InternalJoin(exp?.Body, SelectTableInfoType.RightJoin); } ISelect ISelect.Where(Expression> exp) { if (exp == null) return this.Where(null); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereGlobalFilter, _params)); } ISelect ISelect.WhereIf(bool condition, Expression> exp) { if (condition == false || exp == null) return this; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereGlobalFilter, _params)); } bool ISelect.Any(Expression> exp) { if (exp == null) return this.Any(); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; var oldwhere = _where.ToString(); var ret = this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereGlobalFilter, _params)).Any(); _where.Clear().Append(oldwhere); return ret; } TReturn ISelect.ToOne(Expression> select) => (this as ISelect).Limit(1).ToList(select).FirstOrDefault(); TReturn ISelect.First(Expression> select) => (this as ISelect).Limit(1).ToList(select).FirstOrDefault(); TDto ISelect.First() => (this as ISelect).Limit(1).ToList().FirstOrDefault(); #region HzyTuple 元组 double ISelect.Avg(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).Avg((Expression>)expModify); } ISelectGrouping> ISelect.GroupBy(Expression, TKey>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).GroupBy((Expression>)expModify); } TMember ISelect.Max(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).Max((Expression>)expModify); } TMember ISelect.Min(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).Min((Expression>)expModify); } ISelect ISelect.OrderBy(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).OrderBy((Expression>)expModify); } ISelect ISelect.OrderByDescending(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).OrderByDescending((Expression>)expModify); } ISelect ISelect.OrderByIf(bool condition, Expression, TMember>> column, bool descending) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).OrderByIf(condition, (Expression>)expModify, descending); } decimal ISelect.Sum(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).Sum((Expression>)expModify); } List ISelect.ToList(Expression, TReturn>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).ToList((Expression>)expModify); } public void ToChunk(Expression, TReturn>> select, int size, Action>> done) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); (this as ISelect).ToChunk((Expression>)expModify, size, done); } DataTable ISelect.ToDataTable(Expression, TReturn>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).ToDataTable((Expression>)expModify); } int ISelect.InsertInto(string tableName, Expression, TTargetEntity>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).InsertInto(tableName, (Expression>)expModify); } string ISelect.ToSql(Expression, TReturn>> select, FieldAliasOptions fieldAlias) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).ToSql((Expression>)expModify, fieldAlias); } ISelect ISelect.LeftJoin(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).LeftJoin((Expression>)expModify); } ISelect ISelect.InnerJoin(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).InnerJoin((Expression>)expModify); } ISelect ISelect.RightJoin(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).RightJoin((Expression>)expModify); } ISelect ISelect.Where(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).Where((Expression>)expModify); } ISelect ISelect.WhereIf(bool condition, Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).WhereIf(condition, (Expression>)expModify); } bool ISelect.Any(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return (this as ISelect).Any((Expression>)expModify); } TReturn ISelect.ToOne(Expression, TReturn>> select) => (this as ISelect).Limit(1).ToList(select).FirstOrDefault(); TReturn ISelect.First(Expression, TReturn>> select) => (this as ISelect).Limit(1).ToList(select).FirstOrDefault(); #endregion #if net40 #else Task ISelect.AvgAsync(Expression> column) { if (column == null) return Task.FromResult(default(double)); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalAvgAsync(column?.Body); } Task ISelect.MaxAsync(Expression> column) { if (column == null) return Task.FromResult(default(TMember)); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalMaxAsync(column?.Body); } Task ISelect.MinAsync(Expression> column) { if (column == null) return Task.FromResult(default(TMember)); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalMinAsync(column?.Body); } Task ISelect.SumAsync(Expression> column) { if (column == null) this.InternalOrderBy(column?.Body); for (var a = 0; a < column.Parameters.Count; a++) _tables[a].Parameter = column.Parameters[a]; return this.InternalSumAsync(column?.Body); } Task ISelect.ToAggregateAsync(Expression, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, ISelectGroupingAggregate, TReturn>> select) { if (select == null) return Task.FromResult(default(TReturn)); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToAggregateAsync(select?.Body); } Task> ISelect.ToListAsync(Expression> select) { if (select == null) return this.InternalToListAsync(select?.Body); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToListAsync(select?.Body); } Task> ISelect.ToListAsync() => (this as ISelect).ToListAsync(GetToListDtoSelector()); Task ISelect.ToDataTableAsync(Expression> select) { if (select == null) return this.InternalToDataTableAsync(select?.Body); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalToDataTableAsync(select?.Body); } Task ISelect.InsertIntoAsync(string tableName, Expression> select) { if (select == null) return this.InternalInsertIntoAsync(tableName, select); for (var a = 0; a < select.Parameters.Count; a++) _tables[a].Parameter = select.Parameters[a]; return this.InternalInsertIntoAsync(tableName, select?.Body); } async Task ISelect.AnyAsync(Expression> exp) { if (exp == null) return await this.AnyAsync(); for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; var oldwhere = _where.ToString(); var ret = await this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereGlobalFilter, _params)).AnyAsync(); _where.Clear().Append(oldwhere); return ret; } async Task ISelect.ToOneAsync(Expression> select) => (await (this as ISelect).Limit(1).ToListAsync(select)).FirstOrDefault(); async Task ISelect.FirstAsync(Expression> select) => (await (this as ISelect).Limit(1).ToListAsync(select)).FirstOrDefault(); async Task ISelect.FirstAsync() => (await (this as ISelect).Limit(1).ToListAsync()).FirstOrDefault(); #region HzyTuple 元组 Task ISelect.AvgAsync(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).AvgAsync((Expression>)expModify); } Task ISelect.MaxAsync(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).MaxAsync((Expression>)expModify); } Task ISelect.MinAsync(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).MinAsync((Expression>)expModify); } Task ISelect.SumAsync(Expression, TMember>> column) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(column, _tables); return (this as ISelect).SumAsync((Expression>)expModify); } Task> ISelect.ToListAsync(Expression, TReturn>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).ToListAsync((Expression>)expModify); } Task ISelect.ToDataTableAsync(Expression, TReturn>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).ToDataTableAsync((Expression>)expModify); } Task ISelect.InsertIntoAsync(string tableName, Expression, TTargetEntity>> select) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(select, _tables); return (this as ISelect).InsertIntoAsync(tableName, (Expression>)expModify); } async Task ISelect.AnyAsync(Expression, bool>> exp) { var expModify = new CommonExpression.ReplaceHzyTupleToMultiParam().Modify(exp, _tables); return await (this as ISelect).AnyAsync((Expression>)expModify); } async Task ISelect.ToOneAsync(Expression, TReturn>> select) => (await (this as ISelect).Limit(1).ToListAsync(select)).FirstOrDefault(); async Task ISelect.FirstAsync(Expression, TReturn>> select) => (await (this as ISelect).Limit(1).ToListAsync(select)).FirstOrDefault(); #endregion #endif } }