Skip to content

Commit 886679d

Browse files
authored
Merge pull request #190 from sqlkata/merge-oracle-compilers
Merge oracle compilers
2 parents a15fc7f + 292c009 commit 886679d

6 files changed

Lines changed: 23 additions & 237 deletions

File tree

QueryBuilder.Tests/OracleLegacyLimit.cs

Lines changed: 0 additions & 89 deletions
This file was deleted.

QueryBuilder.Tests/Oracle11gLimitTests.cs renamed to QueryBuilder.Tests/OracleLegacyLimitTests.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,15 @@
55

66
namespace SqlKata.Tests
77
{
8-
public class Oracle11gLimitTests
8+
public class OracleLegacyLimitTests
99
{
1010
private const string TableName = "Table";
1111
private const string SqlPlaceholder = "GENERATED_SQL";
1212

13-
private Oracle11gCompiler compiler = new Oracle11gCompiler();
14-
15-
[Fact]
16-
public void CompileLimitThrowsException()
13+
private OracleCompiler compiler = new OracleCompiler()
1714
{
18-
// Arrange:
19-
var query = new Query(TableName);
20-
var ctx = new SqlResult { Query = query };
21-
22-
// Act:
23-
Assert.Throws<NotSupportedException>(() => compiler.CompileLimit(ctx));
24-
25-
// Assert: Assertion is handled by Throws
26-
}
15+
UseLegacyPagination = true
16+
};
2717

2818
[Fact]
2919
public void WithNoLimitNorOffset()
@@ -33,7 +23,7 @@ public void WithNoLimitNorOffset()
3323
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
3424

3525
// Act:
36-
compiler.ApplyLimit(ctx);
26+
compiler.ApplyLegacyLimit(ctx);
3727

3828
// Assert:
3929
Assert.Equal(SqlPlaceholder, ctx.RawSql);
@@ -47,7 +37,7 @@ public void WithNoOffset()
4737
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
4838

4939
// Act:
50-
compiler.ApplyLimit(ctx);
40+
compiler.ApplyLegacyLimit(ctx);
5141

5242
// Assert:
5343
Assert.Matches($"SELECT \\* FROM \\({SqlPlaceholder}\\) WHERE ROWNUM <= ?", ctx.RawSql);
@@ -63,10 +53,10 @@ public void WithNoLimit()
6353
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
6454

6555
// Act:
66-
compiler.ApplyLimit(ctx);
56+
compiler.ApplyLegacyLimit(ctx);
6757

6858
// Assert:
69-
Assert.Matches($"SELECT \\* FROM \\(SELECT \"(SqlKata_.*__)\"\\.\\*, ROWNUM \"(SqlKata_.*__)\" FROM \\({SqlPlaceholder}\\) \"(SqlKata_.*__)\"\\) WHERE \"(SqlKata_.*__)\" > \\?", ctx.RawSql);
59+
Assert.Equal("SELECT * FROM (SELECT \"results_wrapper\".*, ROWNUM \"row_num\" FROM (GENERATED_SQL) \"results_wrapper\") WHERE \"row_num\" > ?", ctx.RawSql);
7060
Assert.Equal(20, ctx.Bindings[0]);
7161
Assert.Single(ctx.Bindings);
7262
}
@@ -79,10 +69,10 @@ public void WithLimitAndOffset()
7969
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
8070

8171
// Act:
82-
compiler.ApplyLimit(ctx);
72+
compiler.ApplyLegacyLimit(ctx);
8373

8474
// Assert:
85-
Assert.Matches($"SELECT \\* FROM \\(SELECT \"(SqlKata_.*__)\"\\.\\*, ROWNUM \"(SqlKata_.*__)\" FROM \\({SqlPlaceholder}\\) \"(SqlKata_.*__)\" WHERE ROWNUM <= \\?\\) WHERE \"(SqlKata_.*__)\" > \\?", ctx.RawSql);
75+
Assert.Equal("SELECT * FROM (SELECT \"results_wrapper\".*, ROWNUM \"row_num\" FROM (GENERATED_SQL) \"results_wrapper\" WHERE ROWNUM <= ?) WHERE \"row_num\" > ?", ctx.RawSql);
8676
Assert.Equal(25, ctx.Bindings[0]);
8777
Assert.Equal(20, ctx.Bindings[1]);
8878
Assert.Equal(2, ctx.Bindings.Count);

QueryBuilder.Tests/QueryBuilderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class QueryBuilderTest
1313
private readonly Compiler pgsql;
1414
private readonly MySqlCompiler mysql;
1515
private readonly FirebirdCompiler fbsql;
16-
private readonly Oracle11gCompiler oracle;
16+
private readonly OracleCompiler oracle;
1717
public SqlServerCompiler mssql { get; private set; }
1818

1919
private string[] Compile(Query q)
@@ -24,7 +24,7 @@ private string[] Compile(Query q)
2424
mysql.Compile(q.Clone()).ToString(),
2525
pgsql.Compile(q.Clone()).ToString(),
2626
fbsql.Compile(q.Clone()).ToString(),
27-
oracle.Compile(q.Clone()).ToString(),
27+
oracle.Compile(q.Clone()).ToString()
2828
};
2929
}
3030

@@ -34,7 +34,7 @@ public QueryBuilderTest()
3434
mysql = new MySqlCompiler();
3535
pgsql = new PostgresCompiler();
3636
fbsql = new FirebirdCompiler();
37-
oracle = new Oracle11gCompiler();
37+
oracle = new OracleCompiler();
3838
}
3939

4040
[Fact]

QueryBuilder/Compilers/Extensions/Oracle11gCompilerExtensions.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

QueryBuilder/Compilers/Oracle11gCompiler.cs

Lines changed: 0 additions & 88 deletions
This file was deleted.

QueryBuilder/Compilers/OracleCompiler.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Diagnostics;
22
using System.Linq;
3+
using SqlKata.Compilers.Extensions;
34

45
namespace SqlKata.Compilers
56
{
6-
public sealed class OracleCompiler : Compiler
7+
public class OracleCompiler : Compiler
78
{
89
public OracleCompiler()
910
{
@@ -12,7 +13,7 @@ public OracleCompiler()
1213
parameterPlaceholderPrefix = ":p";
1314
}
1415

15-
public override string EngineCode { get; } = "oracle";
16+
public override string EngineCode { get; } = OracleCompilerExtensions.ENGINE_CODE;
1617
public bool UseLegacyPagination { get; set; } = false;
1718

1819
protected override SqlResult CompileSelectQuery(Query query)
@@ -22,23 +23,11 @@ protected override SqlResult CompileSelectQuery(Query query)
2223
return base.CompileSelectQuery(query);
2324
}
2425

25-
query = query.Clone();
26-
27-
var limit = query.GetLimit(EngineCode);
28-
var offset = query.GetOffset(EngineCode);
29-
30-
query.ClearComponent("limit");
31-
32-
var ctx = new SqlResult
33-
{
34-
Query = query,
35-
};
36-
3726
var result = base.CompileSelectQuery(query);
3827

39-
ApplyLegacyLimit(result, limit, offset);
28+
ApplyLegacyLimit(result);
4029

41-
return ctx;
30+
return result;
4231
}
4332

4433
public override string CompileLimit(SqlResult ctx)
@@ -75,22 +64,20 @@ public override string CompileLimit(SqlResult ctx)
7564
return $"{safeOrder}OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
7665
}
7766

78-
internal void ApplyLegacyLimit(SqlResult ctx, int limit, int offset)
67+
internal void ApplyLegacyLimit(SqlResult ctx)
7968
{
69+
var limit = ctx.Query.GetLimit(EngineCode);
70+
var offset = ctx.Query.GetOffset(EngineCode);
8071

8172
if (limit == 0 && offset == 0)
8273
{
8374
return;
8475
}
8576

86-
//@todo replace with alias generator
87-
var subQueryAlias = WrapValue("subquery");
88-
var rowNumAlias = WrapValue("row_num");
89-
9077
string newSql;
9178
if (limit == 0)
9279
{
93-
newSql = $"SELECT * FROM (SELECT {subQueryAlias}.*, ROWNUM {rowNumAlias} FROM ({ctx.RawSql}) {subQueryAlias}) WHERE {rowNumAlias} > ?";
80+
newSql = $"SELECT * FROM (SELECT \"results_wrapper\".*, ROWNUM \"row_num\" FROM ({ctx.RawSql}) \"results_wrapper\") WHERE \"row_num\" > ?";
9481
ctx.Bindings.Add(offset);
9582
}
9683
else if (offset == 0)
@@ -100,7 +87,7 @@ internal void ApplyLegacyLimit(SqlResult ctx, int limit, int offset)
10087
}
10188
else
10289
{
103-
newSql = $"SELECT * FROM (SELECT {subQueryAlias}.*, ROWNUM {rowNumAlias} FROM ({ctx.RawSql}) {subQueryAlias} WHERE ROWNUM <= ?) WHERE {rowNumAlias} > ?";
90+
newSql = $"SELECT * FROM (SELECT \"results_wrapper\".*, ROWNUM \"row_num\" FROM ({ctx.RawSql}) \"results_wrapper\" WHERE ROWNUM <= ?) WHERE \"row_num\" > ?";
10491
ctx.Bindings.Add(limit + offset);
10592
ctx.Bindings.Add(offset);
10693
}

0 commit comments

Comments
 (0)