mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 增加 AsTable 多次,可查询分表后的多个子表记录,以 UNION ALL 形式执行;
This commit is contained in:
parent
ebec260d97
commit
bcc154ee43
@ -0,0 +1,34 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NpgsqlTypes;
|
||||
using Npgsql.LegacyPostgis;
|
||||
using FreeSql.Internal;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql.Internal.Model;
|
||||
|
||||
namespace FreeSql.ExpressionTree
|
||||
{
|
||||
public class GetAllTableRuleTest
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void Test()
|
||||
{
|
||||
//var _tables = new List<SelectTableInfo>
|
||||
//{
|
||||
// [0] = new SelectTableInfo { }
|
||||
//};
|
||||
//var tableRuleInvoke = new Func<Type, string, string[]>((type, oldname) =>
|
||||
//{
|
||||
// return new[] { oldname };
|
||||
//});
|
||||
|
||||
//CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ namespace FreeSql.Tests.MySqlMapType
|
||||
|
||||
[Column(MapType = typeof(DateTime))]
|
||||
public DateTimeOffset dtos_to_dt { get; set; }
|
||||
[Column(MapType = typeof(DateTime))]
|
||||
[Column(MapType = typeof(DateTime?))]
|
||||
public DateTimeOffset? dtosnullable_to_dt { get; set; }
|
||||
}
|
||||
[Fact]
|
||||
@ -27,7 +27,7 @@ namespace FreeSql.Tests.MySqlMapType
|
||||
Assert.NotNull(find);
|
||||
Assert.Equal(item.id, find.id);
|
||||
Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g"));
|
||||
Assert.Equal(item.dtosnullable_to_dt.Value.ToString("g"), find.dtosnullable_to_dt.Value.ToString("g"));
|
||||
Assert.Equal(item.dtosnullable_to_dt, find.dtosnullable_to_dt);
|
||||
|
||||
//update all
|
||||
item.dtos_to_dt = DateTimeOffset.Now;
|
||||
@ -36,7 +36,7 @@ namespace FreeSql.Tests.MySqlMapType
|
||||
Assert.NotNull(find);
|
||||
Assert.Equal(item.id, find.id);
|
||||
Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g"));
|
||||
Assert.Equal(item.dtosnullable_to_dt.Value.ToString("g"), find.dtosnullable_to_dt.Value.ToString("g"));
|
||||
Assert.Equal(item.dtosnullable_to_dt, find.dtosnullable_to_dt);
|
||||
|
||||
item.dtosnullable_to_dt = DateTimeOffset.Now;
|
||||
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
|
||||
|
@ -790,10 +790,22 @@ namespace FreeSql.Tests.Sqlite
|
||||
else if (type == typeof(TestTypeInfo)) return oldname + "AsTable2";
|
||||
return oldname + "AsTable";
|
||||
};
|
||||
Func<Type, string, string> tableRule2 = (type, oldname) =>
|
||||
{
|
||||
if (type == typeof(Topic)) return oldname + "Test2";
|
||||
else if (type == typeof(TestTypeInfo)) return oldname + "Test2";
|
||||
return oldname + "Test2";
|
||||
};
|
||||
var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule).AsTable(tableRule2);
|
||||
var sql = query.ToSql();
|
||||
|
||||
query = select.AsTable((type, oldname) => "table_1").AsTable((type, oldname) => "table_2").AsTable((type, oldname) => "table_3");
|
||||
sql = query.ToSql(a => a.Id);
|
||||
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
|
||||
var sql = query.ToSql().Replace("\r\n", "");
|
||||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
|
||||
sql = query.ToSql().Replace("\r\n", "");
|
||||
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a__Type.\"Guid\", a__Type.\"ParentId\", a__Type.\"Name\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22AsTable1\" a LEFT JOIN \"TestTypeInfoAsTable2\" a__Type ON a__Type.\"Guid\" = a.\"TypeGuid\"", sql);
|
||||
|
||||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").AsTable(tableRule);
|
||||
|
@ -297,6 +297,7 @@ namespace FreeSql.Tests
|
||||
public bool OptionsEntity02 { get; set; } = false;
|
||||
public bool OptionsEntity03 { get; set; } = false;
|
||||
public int OptionsEntity04 { get; set; }
|
||||
public int? score { get; set; }
|
||||
|
||||
[Navigate("TbId")]
|
||||
public virtual ICollection<TaskBuildInfo> Builds { get; set; }
|
||||
@ -405,6 +406,8 @@ namespace FreeSql.Tests
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
var testorderbysql = g.mysql.Select<TaskBuild>().OrderByDescending(a => a.OptionsEntity04 + (a.score ?? 0)).ToSql();
|
||||
|
||||
var testincludeMemberssql1 = g.sqlite.Select<TaskBuild>().Where(a => a.Templates.Title == "1").ToList();
|
||||
var testincludeMemberssql2 = g.sqlite.Select<TaskBuild>().Include(a => a.Templates).ToList();
|
||||
|
||||
|
@ -809,7 +809,14 @@
|
||||
</member>
|
||||
<member name="M:FreeSql.ISelect0`2.AsTable(System.Func{System.Type,System.String,System.String})">
|
||||
<summary>
|
||||
设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名;
|
||||
设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名; <para></para>
|
||||
设置后可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
如:select.AsTable((type, oldname) => "table_1").AsTable((type, oldname) => "table_2").AsTable((type, oldname) => "table_3").ToSql(a => a.Id); <para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_1" a) ftb <para></para>
|
||||
UNION ALL<para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
UNION ALL<para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_3" a) ftb
|
||||
</summary>
|
||||
<param name="tableRule"></param>
|
||||
<returns></returns>
|
||||
|
@ -68,7 +68,14 @@ namespace FreeSql
|
||||
Task<T1> FirstAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名;
|
||||
/// 设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名; <para></para>
|
||||
/// 设置后可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
/// 如:select.AsTable((type, oldname) => "table_1").AsTable((type, oldname) => "table_2").AsTable((type, oldname) => "table_3").ToSql(a => a.Id); <para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_1" a) ftb <para></para>
|
||||
/// UNION ALL<para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
/// UNION ALL<para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_3" a) ftb
|
||||
/// </summary>
|
||||
/// <param name="tableRule"></param>
|
||||
/// <returns></returns>
|
||||
|
@ -906,14 +906,16 @@ namespace FreeSql.Internal.CommonProvider
|
||||
return (map, field.ToString());
|
||||
}
|
||||
|
||||
protected string TableRuleInvoke(Type type, string oldname)
|
||||
protected string[] TableRuleInvoke(Type type, string oldname)
|
||||
{
|
||||
for (var a = _tableRules.Count - 1; a >= 0; a--)
|
||||
List<string> newnames = new List<string>();
|
||||
foreach (var tr in _tableRules)
|
||||
{
|
||||
var newname = _tableRules[a]?.Invoke(type, oldname);
|
||||
if (!string.IsNullOrEmpty(newname)) return newname;
|
||||
var newname = tr?.Invoke(type, oldname);
|
||||
if (!string.IsNullOrEmpty(newname)) newnames.Add(newname);
|
||||
}
|
||||
return oldname;
|
||||
if (newnames.Any() == false) return new[] { oldname };
|
||||
return newnames.Distinct().ToArray();
|
||||
}
|
||||
|
||||
public TSelect AsTable(Func<Type, string, string> tableRule)
|
||||
|
@ -363,5 +363,36 @@ namespace FreeSql.Internal
|
||||
}
|
||||
while (initConns.TryTake(out var conn)) pool.Return(conn);
|
||||
}
|
||||
|
||||
public static List<Dictionary<Type, string>> GetAllTableRule(List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke)
|
||||
{
|
||||
var tableRuleSorted = new List<(Type, string[])>();
|
||||
var tableRuleDict = new Dictionary<Type, bool>();
|
||||
foreach(var tb in _tables)
|
||||
{
|
||||
if (tb.Type == SelectTableInfoType.Parent) continue;
|
||||
if (tableRuleDict.ContainsKey(tb.Table.Type)) continue;
|
||||
var names = tableRuleInvoke(tb.Table.Type, tb.Table.DbName);
|
||||
tableRuleSorted.Add((tb.Table.Type, names));
|
||||
tableRuleDict.Add(tb.Table.Type, true);
|
||||
}
|
||||
var tableRules = new List<Dictionary<Type, string>>();
|
||||
tableRules.Add(tableRuleSorted.Select(a => (a.Item1, a.Item2.First())).ToDictionary(a => a.Item1, a => a.Item2));
|
||||
for (var z = tableRuleSorted.Count - 1; z >=0; z--)
|
||||
{
|
||||
var tbrd = tableRuleSorted[z];
|
||||
var curpos = tableRules.Count;
|
||||
for (var a = 1; a < tbrd.Item2.Length; a++)
|
||||
{
|
||||
for (var b = 0; b < curpos; b++)
|
||||
{
|
||||
var tr = new Dictionary<Type, string>();
|
||||
foreach (var oldtd in tableRules[b]) tr.Add(oldtd.Key, tbrd.Item1 == oldtd.Key ? tbrd.Item2[a] : oldtd.Value);
|
||||
tableRules.Add(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tableRules;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ namespace FreeSql.MySql.Curd
|
||||
class MySqlSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class
|
||||
{
|
||||
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -22,6 +22,14 @@ namespace FreeSql.MySql.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -30,13 +38,13 @@ namespace FreeSql.MySql.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -70,7 +78,7 @@ namespace FreeSql.MySql.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -101,6 +109,8 @@ namespace FreeSql.MySql.Curd
|
||||
sb.Append(" \r\nlimit ").Append(Math.Max(0, _skip)).Append(",").Append(_limit > 0 ? _limit : -1);
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ namespace FreeSql.Oracle.Curd
|
||||
class OracleSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class
|
||||
{
|
||||
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -22,6 +22,14 @@ namespace FreeSql.Oracle.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -32,13 +40,13 @@ namespace FreeSql.Oracle.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -72,7 +80,7 @@ namespace FreeSql.Oracle.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -113,6 +121,8 @@ namespace FreeSql.Oracle.Curd
|
||||
}
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ namespace FreeSql.PostgreSQL.Curd
|
||||
class PostgreSQLSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class
|
||||
{
|
||||
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -22,6 +22,14 @@ namespace FreeSql.PostgreSQL.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -30,13 +38,13 @@ namespace FreeSql.PostgreSQL.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -70,7 +78,7 @@ namespace FreeSql.PostgreSQL.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -103,6 +111,8 @@ namespace FreeSql.PostgreSQL.Curd
|
||||
sb.Append(" \r\noffset ").Append(_skip);
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
@ -12,13 +12,13 @@ namespace FreeSql.SqlServer.Curd
|
||||
class SqlServerSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class
|
||||
{
|
||||
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
=> (_commonUtils as SqlServerUtils).IsSelectRowNumber ?
|
||||
ToSqlStaticRowNumber(_commonUtils, _commonExpression, _select, _distinct, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _whereCascadeExpression, _orm) :
|
||||
ToSqlStaticOffsetFetchNext(_commonUtils, _commonExpression, _select, _distinct, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _whereCascadeExpression, _orm);
|
||||
|
||||
#region SqlServer 2005 row_number
|
||||
internal static string ToSqlStaticRowNumber(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStaticRowNumber(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -28,6 +28,14 @@ namespace FreeSql.SqlServer.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -48,13 +56,13 @@ namespace FreeSql.SqlServer.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -88,7 +96,7 @@ namespace FreeSql.SqlServer.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -120,12 +128,14 @@ namespace FreeSql.SqlServer.Curd
|
||||
sb.Insert(0, "WITH t AS ( ").Append(" ) SELECT t.* FROM t where __rownum__ > ").Append(_skip);
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SqlServer 2012+ offset feach next
|
||||
internal static string ToSqlStaticOffsetFetchNext(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStaticOffsetFetchNext(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -135,6 +145,14 @@ namespace FreeSql.SqlServer.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -145,13 +163,13 @@ namespace FreeSql.SqlServer.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -185,7 +203,7 @@ namespace FreeSql.SqlServer.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -228,6 +246,8 @@ namespace FreeSql.SqlServer.Curd
|
||||
}
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
@ -12,7 +12,7 @@ namespace FreeSql.Sqlite.Curd
|
||||
class SqliteSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class
|
||||
{
|
||||
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _commonExpression, string _select, bool _distinct, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke, List<LambdaExpression> _whereCascadeExpression, IFreeSql _orm)
|
||||
{
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure)
|
||||
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
|
||||
@ -22,6 +22,14 @@ namespace FreeSql.Sqlite.Curd
|
||||
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereCascadeExpression);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var tbrules = CommonUtils.GetAllTableRule(_tables, tableRuleInvoke);
|
||||
var tbrulesGt0 = tbrules.Count > 1;
|
||||
for (var tbrulesIdx = 0; tbrulesIdx < tbrules.Count; tbrulesIdx++)
|
||||
{
|
||||
if (tbrulesIdx > 0) sb.Append(" \r\n\r\nUNION ALL\r\n\r\n");
|
||||
if (tbrulesGt0) sb.Append("select * from (");
|
||||
var tbrule = tbrules[tbrulesIdx];
|
||||
|
||||
var sbnav = new StringBuilder();
|
||||
sb.Append(_select);
|
||||
if (_distinct) sb.Append("DISTINCT ");
|
||||
@ -30,13 +38,13 @@ namespace FreeSql.Sqlite.Curd
|
||||
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
|
||||
for (var a = 0; a < tbsfrom.Length; a++)
|
||||
{
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[a].Table.Type])).Append(" ").Append(tbsfrom[a].Alias);
|
||||
if (tbsjoin.Length > 0)
|
||||
{
|
||||
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
|
||||
for (var b = 1; b < tbsfrom.Length; b++)
|
||||
{
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
|
||||
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbrule[tbsfrom[b].Table.Type])).Append(" ").Append(tbsfrom[b].Alias);
|
||||
|
||||
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
|
||||
else
|
||||
@ -70,7 +78,7 @@ namespace FreeSql.Sqlite.Curd
|
||||
sb.Append(" \r\nRIGHT JOIN ");
|
||||
break;
|
||||
}
|
||||
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
sb.Append(_commonUtils.QuoteSqlName(tbrule[tb.Table.Type])).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
|
||||
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND (").Append(tb.Cascade).Append(")");
|
||||
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
|
||||
}
|
||||
@ -101,6 +109,8 @@ namespace FreeSql.Sqlite.Curd
|
||||
sb.Append(" \r\nlimit ").Append(Math.Max(0, _skip)).Append(",").Append(_limit > 0 ? _limit : -1);
|
||||
|
||||
sbnav.Clear();
|
||||
if (tbrulesGt0) sb.Append(") ftb");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user