Skip to content

Commit 7597698

Browse files
committed
Fix for issue #592 : Fix places where the parameterPlaceholder is hardcoded to a ?
Added compiler to the constructor of SqlResult Made ParameterPlaceholder public
1 parent d9d0441 commit 7597698

19 files changed

+170
-99
lines changed

QueryBuilder.Tests/Firebird/FirebirdLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public FirebirdLimitTests()
1717
public void NoLimitNorOffset()
1818
{
1919
var query = new Query("Table");
20-
var ctx = new SqlResult { Query = query };
20+
var ctx = new SqlResult(compiler) { Query = query };
2121

2222
Assert.Null(compiler.CompileLimit(ctx));
2323
}
@@ -26,7 +26,7 @@ public void NoLimitNorOffset()
2626
public void LimitOnly()
2727
{
2828
var query = new Query("Table").Limit(10);
29-
var ctx = new SqlResult { Query = query };
29+
var ctx = new SqlResult(compiler) { Query = query };
3030

3131
Assert.Null(compiler.CompileLimit(ctx));
3232
}
@@ -35,7 +35,7 @@ public void LimitOnly()
3535
public void OffsetOnly()
3636
{
3737
var query = new Query("Table").Offset(20);
38-
var ctx = new SqlResult { Query = query };
38+
var ctx = new SqlResult(compiler) { Query = query };
3939

4040
Assert.Null(compiler.CompileLimit(ctx));
4141
}
@@ -44,7 +44,7 @@ public void OffsetOnly()
4444
public void LimitAndOffset()
4545
{
4646
var query = new Query("Table").Limit(5).Offset(20);
47-
var ctx = new SqlResult { Query = query };
47+
var ctx = new SqlResult(compiler) { Query = query };
4848

4949
Assert.Equal("ROWS ? TO ?", compiler.CompileLimit(ctx));
5050
Assert.Equal(21L, ctx.Bindings[0]);

QueryBuilder.Tests/Infrastructure/TestCompilersContainer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
using SqlKata.Compilers;
12
using System;
23
using System.Collections.Generic;
34
using System.Linq;
4-
using SqlKata.Compilers;
5-
65
namespace SqlKata.Tests.Infrastructure
76
{
87
public class TestCompilersContainer

QueryBuilder.Tests/MySql/MySqlLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public MySqlLimitTests()
1717
public void WithNoLimitNorOffset()
1818
{
1919
var query = new Query("Table");
20-
var ctx = new SqlResult { Query = query };
20+
var ctx = new SqlResult(compiler) { Query = query };
2121

2222
Assert.Null(compiler.CompileLimit(ctx));
2323
}
@@ -26,7 +26,7 @@ public void WithNoLimitNorOffset()
2626
public void WithNoOffset()
2727
{
2828
var query = new Query("Table").Limit(10);
29-
var ctx = new SqlResult { Query = query };
29+
var ctx = new SqlResult(compiler) { Query = query };
3030

3131
Assert.Equal("LIMIT ?", compiler.CompileLimit(ctx));
3232
Assert.Equal(10, ctx.Bindings[0]);
@@ -36,7 +36,7 @@ public void WithNoOffset()
3636
public void WithNoLimit()
3737
{
3838
var query = new Query("Table").Offset(20);
39-
var ctx = new SqlResult { Query = query };
39+
var ctx = new SqlResult(compiler) { Query = query };
4040

4141
Assert.Equal("LIMIT 18446744073709551615 OFFSET ?", compiler.CompileLimit(ctx));
4242
Assert.Equal(20L, ctx.Bindings[0]);
@@ -47,7 +47,7 @@ public void WithNoLimit()
4747
public void WithLimitAndOffset()
4848
{
4949
var query = new Query("Table").Limit(5).Offset(20);
50-
var ctx = new SqlResult { Query = query };
50+
var ctx = new SqlResult(compiler) { Query = query };
5151

5252
Assert.Equal("LIMIT ? OFFSET ?", compiler.CompileLimit(ctx));
5353
Assert.Equal(5, ctx.Bindings[0]);

QueryBuilder.Tests/MySqlExecutionTest.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
using MySql.Data.MySqlClient;
12
using SqlKata.Compilers;
2-
using Xunit;
33
using SqlKata.Execution;
4-
using MySql.Data.MySqlClient;
5-
using System;
6-
using System.Linq;
7-
using static SqlKata.Expressions;
84
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
97

108
namespace SqlKata.Tests
119
{
@@ -174,7 +172,7 @@ public void ExistsShouldReturnFalseForEmptyTable()
174172
});
175173

176174
var exists = db.Query("Transaction").Exists();
177-
Assert.Equal(false, exists);
175+
Assert.False(exists);
178176

179177
db.Drop("Transaction");
180178
}
@@ -195,7 +193,7 @@ public void ExistsShouldReturnTrueForNonEmptyTable()
195193
});
196194

197195
var exists = db.Query("Transaction").Exists();
198-
Assert.Equal(true, exists);
196+
Assert.True(exists);
199197

200198
db.Drop("Transaction");
201199
}
@@ -266,4 +264,4 @@ QueryFactory DB()
266264

267265

268266
}
269-
}
267+
}

QueryBuilder.Tests/Oracle/OracleLegacyLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void WithNoLimitNorOffset()
2121
{
2222
// Arrange:
2323
var query = new Query(TableName);
24-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
24+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
2525

2626
// Act:
2727
compiler.ApplyLegacyLimit(ctx);
@@ -35,7 +35,7 @@ public void WithNoOffset()
3535
{
3636
// Arrange:
3737
var query = new Query(TableName).Limit(10);
38-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
38+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
3939

4040
// Act:
4141
compiler.ApplyLegacyLimit(ctx);
@@ -51,7 +51,7 @@ public void WithNoLimit()
5151
{
5252
// Arrange:
5353
var query = new Query(TableName).Offset(20);
54-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
54+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
5555

5656
// Act:
5757
compiler.ApplyLegacyLimit(ctx);
@@ -67,7 +67,7 @@ public void WithLimitAndOffset()
6767
{
6868
// Arrange:
6969
var query = new Query(TableName).Limit(5).Offset(20);
70-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
70+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
7171

7272
// Act:
7373
compiler.ApplyLegacyLimit(ctx);

QueryBuilder.Tests/Oracle/OracleLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void NoLimitNorOffset()
2121
{
2222
// Arrange:
2323
var query = new Query(TableName);
24-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
24+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
2525

2626
// Act & Assert:
2727
Assert.Null(compiler.CompileLimit(ctx));
@@ -32,7 +32,7 @@ public void LimitOnly()
3232
{
3333
// Arrange:
3434
var query = new Query(TableName).Limit(10);
35-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
35+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
3636

3737
// Act & Assert:
3838
Assert.EndsWith("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", compiler.CompileLimit(ctx));
@@ -46,7 +46,7 @@ public void OffsetOnly()
4646
{
4747
// Arrange:
4848
var query = new Query(TableName).Offset(20);
49-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
49+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
5050

5151
// Act & Assert:
5252
Assert.EndsWith("OFFSET ? ROWS", compiler.CompileLimit(ctx));
@@ -60,7 +60,7 @@ public void LimitAndOffset()
6060
{
6161
// Arrange:
6262
var query = new Query(TableName).Limit(5).Offset(20);
63-
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
63+
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };
6464

6565
// Act & Assert:
6666
Assert.EndsWith("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", compiler.CompileLimit(ctx));

QueryBuilder.Tests/ParameterTypeTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
using SqlKata.Compilers;
2+
using SqlKata.Tests.Infrastructure;
13
using System;
4+
using System.Collections;
25
using System.Collections.Generic;
36
using System.Globalization;
4-
using SqlKata.Compilers;
57
using Xunit;
6-
using System.Collections;
7-
using SqlKata.Tests.Infrastructure;
88

99
namespace SqlKata.Tests
1010
{
@@ -22,9 +22,9 @@ public class ParameterTypeGenerator : IEnumerable<object[]>
2222
private readonly List<object[]> _data = new List<object[]>
2323
{
2424
new object[] {"1", 1},
25-
new object[] {Convert.ToSingle("10.5", CultureInfo.InvariantCulture).ToString(), 10.5},
25+
new object[] {Convert.ToSingle("10.5", CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), 10.5},
2626
new object[] {"-2", -2},
27-
new object[] {Convert.ToSingle("-2.8", CultureInfo.InvariantCulture).ToString(), -2.8},
27+
new object[] {Convert.ToSingle("-2.8", CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), -2.8},
2828
new object[] {"cast(1 as bit)", true},
2929
new object[] {"cast(0 as bit)", false},
3030
new object[] {"'2018-10-28 19:22:00'", new DateTime(2018, 10, 28, 19, 22, 0)},

QueryBuilder.Tests/PostgreSql/PostgreSqlLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public PostgreSqlLimitTests()
1717
public void WithNoLimitNorOffset()
1818
{
1919
var query = new Query("Table");
20-
var ctx = new SqlResult { Query = query };
20+
var ctx = new SqlResult(compiler) { Query = query };
2121

2222
Assert.Null(compiler.CompileLimit(ctx));
2323
}
@@ -26,7 +26,7 @@ public void WithNoLimitNorOffset()
2626
public void WithNoOffset()
2727
{
2828
var query = new Query("Table").Limit(10);
29-
var ctx = new SqlResult { Query = query };
29+
var ctx = new SqlResult(compiler) { Query = query };
3030

3131
Assert.Equal("LIMIT ?", compiler.CompileLimit(ctx));
3232
Assert.Equal(10, ctx.Bindings[0]);
@@ -36,7 +36,7 @@ public void WithNoOffset()
3636
public void WithNoLimit()
3737
{
3838
var query = new Query("Table").Offset(20);
39-
var ctx = new SqlResult { Query = query };
39+
var ctx = new SqlResult(compiler) { Query = query };
4040

4141
Assert.Equal("OFFSET ?", compiler.CompileLimit(ctx));
4242
Assert.Equal(20L, ctx.Bindings[0]);
@@ -47,7 +47,7 @@ public void WithNoLimit()
4747
public void WithLimitAndOffset()
4848
{
4949
var query = new Query("Table").Limit(5).Offset(20);
50-
var ctx = new SqlResult { Query = query };
50+
var ctx = new SqlResult(compiler) { Query = query };
5151

5252
Assert.Equal("LIMIT ? OFFSET ?", compiler.CompileLimit(ctx));
5353
Assert.Equal(5, ctx.Bindings[0]);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using SqlKata.Compilers;
2+
using SqlKata.Tests.Infrastructure;
3+
using System;
4+
using Xunit;
5+
6+
namespace SqlKata.Tests.PostgreSql
7+
{
8+
public class PostgresJsonTests : TestSupport
9+
{
10+
public class JsonAwarePostgresCompiler : PostgresCompiler
11+
{
12+
public override string ParameterPlaceholder { get; protected set; } = "$$";
13+
}
14+
15+
private readonly JsonAwarePostgresCompiler compiler = new();
16+
private PostgresCompiler regularCompiler;
17+
18+
public PostgresJsonTests()
19+
{
20+
regularCompiler = Compilers.Get<PostgresCompiler>(EngineCodes.PostgreSql);
21+
}
22+
23+
[Fact]
24+
public void LimitWithCustomPlaceHolder()
25+
{
26+
var query = new Query("Table").Limit(10);
27+
var ctx = new SqlResult(compiler) { Query = query };
28+
29+
Assert.Equal($"LIMIT $$", compiler.CompileLimit(ctx));
30+
Assert.Equal(10, ctx.Bindings[0]);
31+
}
32+
33+
[Fact]
34+
public void RegularCompilerThrowsExceptionWhereRawJsonContainsQuestionMarkData()
35+
{
36+
Assert.Throws<IndexOutOfRangeException>(() =>
37+
{
38+
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);
39+
40+
SqlResult result = regularCompiler.Compile(query);
41+
Assert.Equal($"SELECT * FROM \"Table\" WHERE {rawCondition}", result.ToString());
42+
});
43+
}
44+
45+
private Query CreateQuestionMarkJsonQuery(out string rawCondition)
46+
{
47+
rawCondition = "my_json_column @> '{\"json_param\" : \"question mark ? \"}'";
48+
var escapedJsonCondition = rawCondition.Replace("{", "\\{").Replace("}", "\\}");
49+
return new Query("Table").WhereRaw(escapedJsonCondition);
50+
}
51+
52+
[Fact]
53+
public void WhereRawJsonWithQuestionMarkData()
54+
{
55+
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);
56+
SqlResult result = compiler.Compile(query);
57+
Assert.Equal($"SELECT * FROM \"Table\" WHERE {rawCondition}", result.ToString());
58+
}
59+
60+
[Fact]
61+
public void UsingJsonArray()
62+
{
63+
var query = new Query("Table").WhereRaw("[Json]->'address'->>'country' in ($$)", new[] { 1, 2, 3, 4 });
64+
65+
SqlResult result = compiler.Compile(query);
66+
67+
Assert.Equal("SELECT * FROM \"Table\" WHERE \"Json\"->'address'->>'country' in (1,2,3,4)", result.ToString());
68+
}
69+
}
70+
}

QueryBuilder.Tests/SqlServer/SqlServerLegacyLimitTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SqlServerLegacyLimitTests()
1818
public void NoLimitNorOffset()
1919
{
2020
var query = new Query("Table");
21-
var ctx = new SqlResult { Query = query };
21+
var ctx = new SqlResult(compiler) { Query = query };
2222

2323
Assert.Null(compiler.CompileLimit(ctx));
2424
}
@@ -27,7 +27,7 @@ public void NoLimitNorOffset()
2727
public void LimitOnly()
2828
{
2929
var query = new Query("Table").Limit(10);
30-
var ctx = new SqlResult { Query = query };
30+
var ctx = new SqlResult(compiler) { Query = query };
3131

3232
Assert.Null(compiler.CompileLimit(ctx));
3333
}
@@ -36,7 +36,7 @@ public void LimitOnly()
3636
public void OffsetOnly()
3737
{
3838
var query = new Query("Table").Offset(20);
39-
var ctx = new SqlResult { Query = query };
39+
var ctx = new SqlResult(compiler) { Query = query };
4040

4141
Assert.Null(compiler.CompileLimit(ctx));
4242
}
@@ -45,7 +45,7 @@ public void OffsetOnly()
4545
public void LimitAndOffset()
4646
{
4747
var query = new Query("Table").Limit(5).Offset(20);
48-
var ctx = new SqlResult { Query = query };
48+
var ctx = new SqlResult(compiler) { Query = query };
4949

5050
Assert.Null(compiler.CompileLimit(ctx));
5151
}

0 commit comments

Comments
 (0)