Update db.UnionAll

This commit is contained in:
sunkaixuan 2017-08-26 08:57:18 +08:00
parent 7e0f537ffb
commit 864f3e0669
4 changed files with 63 additions and 40 deletions

View File

@ -188,6 +188,11 @@ namespace OrmTest.Demo
var getDateIsSame= db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(DateTime.Now,DateTime.Now,DateType.Hour)).ToList();
var getSqlList = db.Queryable<Student>().AS("(select * from student) t").ToList();
var getUnionAllList = db.UnionAll(db.Queryable<Student>().Where(it => it.Id == 1), db.Queryable<Student>().Where(it => it.Id == 2)).ToList();
var getUnionAllList2 = db.UnionAll(db.Queryable<Student>(), db.Queryable<Student>()).ToList();
}
public static void Page()
{

View File

@ -73,21 +73,11 @@ namespace SqlSugar
}
public virtual string GetPackTable(string sql, string shortName)
{
return string.Format(" ({0}) {1} ", sql, shortName);
return UtilMethods.GetPackTable(sql,shortName);
}
public virtual void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
{
if (appendSql.IsValuable() && parameters.IsValuable())
{
foreach (var parameter in parameters.OrderByDescending(it=>it.ParameterName.Length))
{
//Compatible with.NET CORE parameters case
var name = parameter.ParameterName;
string newName = name + addIndex;
appendSql = appendSql.Replace(name, newName);
parameter.ParameterName = newName;
}
}
UtilMethods.RepairReplicationParameters(ref appendSql,parameters,addIndex);
}
#endregion

View File

@ -261,37 +261,37 @@ namespace SqlSugar
}
#region 9-12
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8,T9> Queryable<T, T2, T3, T4, T5, T6, T7, T8,T9>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8,T9, bool>> joinExpression) where T : class, new()
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, bool>> joinExpression) where T : class, new()
{
InitMppingInfo<T, T2, T3, T4, T5, T6, T8,T9>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8),typeof(T9) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8,T9>(base.CurrentConnectionConfig);
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(base.CurrentConnectionConfig);
base.CreateEasyQueryJoin(joinExpression, types, queryable);
queryable.Where(joinExpression);
return queryable;
}
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9,T10> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9,T10>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9,T10, bool>> joinExpression) where T : class, new()
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> joinExpression) where T : class, new()
{
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9,T10>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9) ,typeof(T10)};
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9,T10>(base.CurrentConnectionConfig);
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(base.CurrentConnectionConfig);
base.CreateEasyQueryJoin(joinExpression, types, queryable);
queryable.Where(joinExpression);
return queryable;
}
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11, bool>> joinExpression) where T : class, new()
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, bool>> joinExpression) where T : class, new()
{
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10,T11>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10),typeof(T11) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10,T11>(base.CurrentConnectionConfig);
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10, T11>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(base.CurrentConnectionConfig);
base.CreateEasyQueryJoin(joinExpression, types, queryable);
queryable.Where(joinExpression);
return queryable;
}
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,T12> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, bool>> joinExpression) where T : class, new()
public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, bool>> joinExpression) where T : class, new()
{
InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10, T11, T12>();
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11),typeof(T12) };
var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12) };
var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(base.CurrentConnectionConfig);
base.CreateEasyQueryJoin(joinExpression, types, queryable);
queryable.Where(joinExpression);
@ -299,23 +299,31 @@ namespace SqlSugar
}
#endregion
public virtual List<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
public virtual ISugarQueryable<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
{
if (queryables.IsNullOrEmpty()) return new List<T>();
List<T> result = new List<T>();
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
int i = 1;
List<KeyValuePair<string, List<SugarParameter>>> allItems =new List<KeyValuePair<string, List<SugarParameter>>>();
foreach (var item in queryables)
{
var addItems = item.ToList();
if (addItems.IsValuable())
{
result.AddRange(addItems);
}
var sqlObj = item.ToSql();
string sql = sqlObj.Key;
UtilMethods.RepairReplicationParameters(ref sql, sqlObj.Value.ToArray(), i);
if (sqlObj.Value.IsValuable())
allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, sqlObj.Value));
else
allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, new List<SugarParameter>()));
i++;
}
return result;
var allSql = string.Join("UNION ALL \r\n", allItems.Select(it=>it.Key));
var allParameters = allItems.SelectMany(it => it.Value).ToArray();
var resulut = this.Queryable<ExpandoObject>().AS(UtilMethods.GetPackTable(allSql, "unionTable"));
resulut.AddParameters(allParameters);
return resulut.Select<T>("*");
}
public virtual List<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
public virtual ISugarQueryable<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
{
if (queryables.IsNullOrEmpty()) return new List<T>();
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
return UnionAll(queryables.ToArray());
}
#endregion
@ -342,7 +350,7 @@ namespace SqlSugar
Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Insertable.columnDictionary can't be null");
var insertObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(columnDictionary));
var columns = columnDictionary.Select(it => it.Key).ToList();
return this.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c,StringComparison.CurrentCultureIgnoreCase))); ;
return this.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ;
}
public virtual IInsertable<T> Insertable<T>(dynamic insertDynamicObject) where T : class, new()
{
@ -353,10 +361,10 @@ namespace SqlSugar
}
else
{
var columns= ((object)insertDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
var columns = ((object)insertDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
Check.Exception(columns.IsNullOrEmpty(), "Insertable.updateDynamicObject can't be null");
T insertObject = this.RewritableMethods.DeserializeObject<T>(this.RewritableMethods.SerializeObject(insertDynamicObject));
return this.Insertable(insertObject).InsertColumns(it=> columns.Any(c=>it.Equals(c,StringComparison.CurrentCultureIgnoreCase)));
return this.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase)));
}
}
#endregion

View File

@ -54,5 +54,25 @@ namespace SqlSugar
{
return (T)Convert.ChangeType(obj, typeof(T));
}
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
{
if (appendSql.IsValuable() && parameters.IsValuable())
{
foreach (var parameter in parameters.OrderByDescending(it => it.ParameterName.Length))
{
//Compatible with.NET CORE parameters case
var name = parameter.ParameterName;
string newName = name + addIndex;
appendSql = appendSql.Replace(name, newName);
parameter.ParameterName = newName;
}
}
}
internal static string GetPackTable(string sql, string shortName)
{
return string.Format(" ({0}) {1} ", sql, shortName);
}
}
}