Skip to content

Commit

Permalink
- 修复 Oracle AsTable 分表嵌套 SQL 拼错错误;#1098
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed May 12, 2022
1 parent 1b84a00 commit a3a3d56
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 133 deletions.
10 changes: 7 additions & 3 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class Order // Part of the JSON column
public string ShippingAddress { get; set; }
}

[Table(Name = "tb_TopicMapTypeToListDto")]
[Table(Name = "tb_tmttld"), OraclePrimaryKeyName("TMTTLD_PK01")]
class TopicMapTypeToListDto
{
[Column(IsIdentity = true, IsPrimary = true)]
Expand Down Expand Up @@ -185,8 +185,8 @@ static void Main(string[] args)
.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2")
.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower)

//.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2")
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)


//.UseConnectionString(FreeSql.DataType.OdbcMySql, "Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Persist Security Info=False;Trusted_Connection=Yes;UID=root;PWD=root;DATABASE=cccddd_odbc;Charset=utf8;SslMode=none;Max pool size=2")
Expand Down Expand Up @@ -452,6 +452,10 @@ int LocalConcurrentDictionaryIsTypeKey(Type dictType, int level = 1)
var sqls501 = sqls5.ToSql();
var sqls502 = sqls5.ToList();

var sqls6 = fsql.Select<AsTableLog>().Where(a => a.createtime < DateTime.Parse("2022-5-1")).Limit(10).OrderBy(a => a.createtime);
var sqls601 = sqls6.ToSql();
var sqls602 = sqls6.ToList();

fsql.Aop.AuditValue += new EventHandler<FreeSql.Aop.AuditValueEventArgs>((_, e) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public void OrderBy()
public void OrderByRandom()
{
var t1 = select.OrderByRandom().Limit(10).ToSql("1");
Assert.Equal(@"SELECT t.* FROM (SELECT 1
Assert.Equal(@"SELECT t.* FROM (SELECT 1
FROM ""TB_TOPIC22"" a
ORDER BY dbms_random.value) t WHERE ROWNUM < 11", t1);
var t2 = select.OrderByRandom().Limit(10).ToList();
Expand Down Expand Up @@ -1018,12 +1018,12 @@ public void AsTable()

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
query.ToList();

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
query.Count();

select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public void OrderBy()
public void OrderByRandom()
{
var t1 = select.OrderByRandom().Limit(10).ToSql("1");
Assert.Equal(@"SELECT t.* FROM (SELECT 1
Assert.Equal(@"SELECT t.* FROM (SELECT 1
FROM ""TB_TOPIC22"" a
ORDER BY dbms_random.value) t WHERE ROWNUM < 11", t1);
var t2 = select.OrderByRandom().Limit(10).ToList();
Expand Down Expand Up @@ -1019,12 +1019,12 @@ public void AsTable()

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
query.ToList();

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
query.Count();

select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/FreeSql.Tests/Dameng/Curd/DamengSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ public void OrderBy()
public void OrderByRandom()
{
var t1 = select.OrderByRandom().Limit(10).ToSql("1");
Assert.Equal(@"SELECT t.* FROM (SELECT 1
Assert.Equal(@"SELECT t.* FROM (SELECT 1
FROM ""TB_TOPIC22"" a
ORDER BY dbms_random.value) t WHERE ROWNUM < 11", t1);
var t2 = select.OrderByRandom().Limit(10).ToList();
Expand Down Expand Up @@ -1154,12 +1154,12 @@ public void AsTable()

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
query.ToList();

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
query.Count();

select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
Expand Down
6 changes: 3 additions & 3 deletions FreeSql.Tests/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public void OrderBy()
public void OrderByRandom()
{
var t1 = select.OrderByRandom().Limit(10).ToSql("1");
Assert.Equal(@"SELECT t.* FROM (SELECT 1
Assert.Equal(@"SELECT t.* FROM (SELECT 1
FROM ""TB_TOPIC22"" a
ORDER BY dbms_random.value) t WHERE ROWNUM < 11", t1);
var t2 = select.OrderByRandom().Limit(10).ToList();
Expand Down Expand Up @@ -1138,12 +1138,12 @@ public void AsTable()

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
query.ToList();

query = select.AsTable((_, old) => old).AsTable((_, old) => old);
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALL SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
query.Count();

select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
Expand Down
68 changes: 38 additions & 30 deletions Providers/FreeSql.Provider.Dameng/Curd/DamengSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,40 @@ internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _c
tb.Cascade = _commonExpression.GetWhereCascadeSql(tb, _whereGlobalFilter, true);

var sb = new StringBuilder();
var sbunion = new StringBuilder();
var sbnav = new StringBuilder();
var tbUnionsGt0 = tbUnions.Count > 1;
for (var tbUnionsIdx = 0; tbUnionsIdx < tbUnions.Count; tbUnionsIdx++)
{
if (tbUnionsIdx > 0) sb.Append("\r\n \r\nUNION ALL\r\n \r\n");
if (tbUnionsGt0) sb.Append(_select).Append(" * from (");
var tbUnion = tbUnions[tbUnionsIdx];

var sbnav = new StringBuilder();
sb.Append(_select);
if (_distinct) sb.Append("DISTINCT ");
sb.Append(field);
if (string.IsNullOrEmpty(_orderby) && _skip > 0) sb.Append(", ROWNUM AS \"__rownum__\"");
sb.Append(" \r\nFROM ");
sbunion.Append(_select);
if (_distinct) sbunion.Append("DISTINCT ");
sbunion.Append(field);
if (string.IsNullOrEmpty(_orderby) && _skip > 0) sbunion.Append(", ROWNUM AS \"__rownum__\"");
sbunion.Append(" \r\nFROM ");
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
for (var a = 0; a < tbsfrom.Length; a++)
{
sb.Append(_commonUtils.QuoteSqlName(tbUnion[tbsfrom[a].Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tbsfrom[a].Table.Type, tbsfrom[a].Alias) ?? tbsfrom[a].Alias);
sbunion.Append(_commonUtils.QuoteSqlName(tbUnion[tbsfrom[a].Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tbsfrom[a].Table.Type, tbsfrom[a].Alias) ?? 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(tbUnion[tbsfrom[b].Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tbsfrom[b].Table.Type, tbsfrom[b].Alias) ?? tbsfrom[b].Alias);
sbunion.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tbUnion[tbsfrom[b].Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tbsfrom[b].Table.Type, tbsfrom[b].Alias) ?? tbsfrom[b].Alias);

if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sb.Append(" ON 1 = 1");
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On) && string.IsNullOrEmpty(tbsfrom[b].Cascade)) sbunion.Append(" ON 1 = 1");
else
{
var onSql = tbsfrom[b].NavigateCondition ?? tbsfrom[b].On;
sb.Append(" ON ").Append(onSql);
sbunion.Append(" ON ").Append(onSql);
if (string.IsNullOrEmpty(tbsfrom[b].Cascade) == false)
{
if (string.IsNullOrEmpty(onSql)) sb.Append(tbsfrom[b].Cascade);
else sb.Append(" AND ").Append(tbsfrom[b].Cascade);
if (string.IsNullOrEmpty(onSql)) sbunion.Append(tbsfrom[b].Cascade);
else sbunion.Append(" AND ").Append(tbsfrom[b].Cascade);
}
}
}
Expand All @@ -67,28 +67,28 @@ internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _c
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
if (a > 0 && !string.IsNullOrEmpty(tbsfrom[a].Cascade)) sbnav.Append(" AND ").Append(tbsfrom[a].Cascade);
}
if (a < tbsfrom.Length - 1) sb.Append(", ");
if (a < tbsfrom.Length - 1) sbunion.Append(", ");
}
foreach (var tb in tbsjoin)
{
if (tb.Type == SelectTableInfoType.Parent) continue;
switch (tb.Type)
{
case SelectTableInfoType.LeftJoin:
sb.Append(" \r\nLEFT JOIN ");
sbunion.Append(" \r\nLEFT JOIN ");
break;
case SelectTableInfoType.InnerJoin:
sb.Append(" \r\nINNER JOIN ");
sbunion.Append(" \r\nINNER JOIN ");
break;
case SelectTableInfoType.RightJoin:
sb.Append(" \r\nRIGHT JOIN ");
sbunion.Append(" \r\nRIGHT JOIN ");
break;
}
sb.Append(_commonUtils.QuoteSqlName(tbUnion[tb.Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tb.Table.Type, tb.Alias) ?? tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.Cascade)) sb.Append(" AND ").Append(tb.Cascade);
sbunion.Append(_commonUtils.QuoteSqlName(tbUnion[tb.Table.Type])).Append(" ").Append(_aliasRule?.Invoke(tb.Table.Type, tb.Alias) ?? tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.Cascade)) sbunion.Append(" AND ").Append(tb.Cascade);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
}
if (_join.Length > 0) sb.Append(_join);
if (_join.Length > 0) sbunion.Append(_join);

sbnav.Append(_where);
if (!string.IsNullOrEmpty(_tables[0].Cascade))
Expand All @@ -97,31 +97,39 @@ internal static string ToSqlStatic(CommonUtils _commonUtils, CommonExpression _c
if (string.IsNullOrEmpty(_orderby) && (_skip > 0 || _limit > 0))
sbnav.Append(" AND ROWNUM < ").Append(_skip + _limit + 1);
if (sbnav.Length > 0)
sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
sbunion.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
if (string.IsNullOrEmpty(_groupby) == false)
{
sb.Append(_groupby);
sbunion.Append(_groupby);
if (string.IsNullOrEmpty(_having) == false)
sb.Append(" \r\nHAVING ").Append(_having.Substring(5));
sbunion.Append(" \r\nHAVING ").Append(_having.Substring(5));
}
sb.Append(_orderby);
sbunion.Append(_orderby);

if (string.IsNullOrEmpty(_orderby))
{
if (_skip > 0)
sb.Insert(0, $"{_select} t.* FROM (").Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
sbunion.Insert(0, $"{_select}t.* FROM (").Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
}
else
{
if (_skip > 0 && _limit > 0) sb.Insert(0, $"{_select} t.* FROM (SELECT rt.*, ROWNUM AS \"__rownum__\" FROM (").Append(") rt WHERE ROWNUM < ").Append(_skip + _limit + 1).Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
else if (_skip > 0) sb.Insert(0, $"{_select} t.* FROM (").Append(") t WHERE ROWNUM > ").Append(_skip);
else if (_limit > 0) sb.Insert(0, $"{_select} t.* FROM (").Append(") t WHERE ROWNUM < ").Append(_limit + 1);
if (_skip > 0 && _limit > 0) sbunion.Insert(0, $"{_select}t.* FROM (SELECT rt.*, ROWNUM AS \"__rownum__\" FROM (").Append(") rt WHERE ROWNUM < ").Append(_skip + _limit + 1).Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
else if (_skip > 0) sbunion.Insert(0, $"{_select}t.* FROM (").Append(") t WHERE ROWNUM > ").Append(_skip);
else if (_limit > 0) sbunion.Insert(0, $"{_select}t.* FROM (").Append(") t WHERE ROWNUM < ").Append(_limit + 1);
}

if (tbUnionsGt0) sbunion.Insert(0, $"{_select}* from (").Append(") ftb");
sb.Append(sbunion);
sbnav.Clear();
if (tbUnionsGt0) sb.Append(") ftb");
sbunion.Clear();
}
return sb.Append(_tosqlAppendContent).ToString();
var sql = sb.Append(_tosqlAppendContent).ToString();

var aliasGreater30 = 0;
foreach (var tb in _tables)
if (tb.Alias.Length > 30) sql = sql.Replace(tb.Alias, $"than30_{aliasGreater30++}");

return sql;
}

public DamengSelect(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) { }
Expand Down
Loading

0 comments on commit a3a3d56

Please sign in to comment.