Skip to content

Fix for issue #592, json query fix when question mark is used in data #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/Firebird/FirebirdLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public FirebirdLimitTests()
public void NoLimitNorOffset()
{
var query = new Query("Table");
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -26,7 +26,7 @@ public void NoLimitNorOffset()
public void LimitOnly()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -35,7 +35,7 @@ public void LimitOnly()
public void OffsetOnly()
{
var query = new Query("Table").Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -44,7 +44,7 @@ public void OffsetOnly()
public void LimitAndOffset()
{
var query = new Query("Table").Limit(5).Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Equal("ROWS ? TO ?", compiler.CompileLimit(ctx));
Assert.Equal(21L, ctx.Bindings[0]);
Expand Down
3 changes: 1 addition & 2 deletions QueryBuilder.Tests/Infrastructure/TestCompilersContainer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using SqlKata.Compilers;
using System;
using System.Collections.Generic;
using System.Linq;
using SqlKata.Compilers;

namespace SqlKata.Tests.Infrastructure
{
public class TestCompilersContainer
Expand Down
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/MySql/MySqlLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public MySqlLimitTests()
public void WithNoLimitNorOffset()
{
var query = new Query("Table");
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -26,7 +26,7 @@ public void WithNoLimitNorOffset()
public void WithNoOffset()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

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

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

Assert.Equal("LIMIT ? OFFSET ?", compiler.CompileLimit(ctx));
Assert.Equal(5, ctx.Bindings[0]);
Expand Down
14 changes: 6 additions & 8 deletions QueryBuilder.Tests/MySqlExecutionTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using MySql.Data.MySqlClient;
using SqlKata.Compilers;
using Xunit;
using SqlKata.Execution;
using MySql.Data.MySqlClient;
using System;
using System.Linq;
using static SqlKata.Expressions;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace SqlKata.Tests
{
Expand Down Expand Up @@ -174,7 +172,7 @@ public void ExistsShouldReturnFalseForEmptyTable()
});

var exists = db.Query("Transaction").Exists();
Assert.Equal(false, exists);
Assert.False(exists);

db.Drop("Transaction");
}
Expand All @@ -195,7 +193,7 @@ public void ExistsShouldReturnTrueForNonEmptyTable()
});

var exists = db.Query("Transaction").Exists();
Assert.Equal(true, exists);
Assert.True(exists);

db.Drop("Transaction");
}
Expand Down Expand Up @@ -266,4 +264,4 @@ QueryFactory DB()


}
}
}
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/Oracle/OracleLegacyLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void WithNoLimitNorOffset()
{
// Arrange:
var query = new Query(TableName);
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };

// Act:
compiler.ApplyLegacyLimit(ctx);
Expand All @@ -35,7 +35,7 @@ public void WithNoOffset()
{
// Arrange:
var query = new Query(TableName).Limit(10);
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };

// Act:
compiler.ApplyLegacyLimit(ctx);
Expand All @@ -51,7 +51,7 @@ public void WithNoLimit()
{
// Arrange:
var query = new Query(TableName).Offset(20);
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };

// Act:
compiler.ApplyLegacyLimit(ctx);
Expand All @@ -67,7 +67,7 @@ public void WithLimitAndOffset()
{
// Arrange:
var query = new Query(TableName).Limit(5).Offset(20);
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };

// Act:
compiler.ApplyLegacyLimit(ctx);
Expand Down
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/Oracle/OracleLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void NoLimitNorOffset()
{
// Arrange:
var query = new Query(TableName);
var ctx = new SqlResult { Query = query, RawSql = SqlPlaceholder };
var ctx = new SqlResult(compiler) { Query = query, RawSql = SqlPlaceholder };

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

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

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

// Act & Assert:
Assert.EndsWith("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", compiler.CompileLimit(ctx));
Expand Down
10 changes: 5 additions & 5 deletions QueryBuilder.Tests/ParameterTypeTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using SqlKata.Compilers;
using Xunit;
using System.Collections;
using SqlKata.Tests.Infrastructure;

namespace SqlKata.Tests
{
Expand All @@ -22,9 +22,9 @@ public class ParameterTypeGenerator : IEnumerable<object[]>
private readonly List<object[]> _data = new List<object[]>
{
new object[] {"1", 1},
new object[] {Convert.ToSingle("10.5", CultureInfo.InvariantCulture).ToString(), 10.5},
new object[] {Convert.ToSingle("10.5", CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), 10.5},
new object[] {"-2", -2},
new object[] {Convert.ToSingle("-2.8", CultureInfo.InvariantCulture).ToString(), -2.8},
new object[] {Convert.ToSingle("-2.8", CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), -2.8},
new object[] {"cast(1 as bit)", true},
new object[] {"cast(0 as bit)", false},
new object[] {"'2018-10-28 19:22:00'", new DateTime(2018, 10, 28, 19, 22, 0)},
Expand Down
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/PostgreSql/PostgreSqlLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PostgreSqlLimitTests()
public void WithNoLimitNorOffset()
{
var query = new Query("Table");
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -26,7 +26,7 @@ public void WithNoLimitNorOffset()
public void WithNoOffset()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

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

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

Assert.Equal("LIMIT ? OFFSET ?", compiler.CompileLimit(ctx));
Assert.Equal(5, ctx.Bindings[0]);
Expand Down
70 changes: 70 additions & 0 deletions QueryBuilder.Tests/PostgreSql/PostgresJsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using System;
using Xunit;

namespace SqlKata.Tests.PostgreSql
{
public class PostgresJsonTests : TestSupport
{
public class JsonAwarePostgresCompiler : PostgresCompiler
{
public override string ParameterPlaceholder { get; protected set; } = "$$";
}

private readonly JsonAwarePostgresCompiler compiler = new();
private PostgresCompiler regularCompiler;

public PostgresJsonTests()
{
regularCompiler = Compilers.Get<PostgresCompiler>(EngineCodes.PostgreSql);
}

[Fact]
public void LimitWithCustomPlaceHolder()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult(compiler) { Query = query };

Assert.Equal($"LIMIT $$", compiler.CompileLimit(ctx));
Assert.Equal(10, ctx.Bindings[0]);
}

[Fact]
public void RegularCompilerThrowsExceptionWhereRawJsonContainsQuestionMarkData()
{
Assert.Throws<IndexOutOfRangeException>(() =>
{
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);

SqlResult result = regularCompiler.Compile(query);
Assert.Equal($"SELECT * FROM \"Table\" WHERE {rawCondition}", result.ToString());
});
}

private Query CreateQuestionMarkJsonQuery(out string rawCondition)
{
rawCondition = "my_json_column @> '{\"json_param\" : \"question mark ? \"}'";
var escapedJsonCondition = rawCondition.Replace("{", "\\{").Replace("}", "\\}");
return new Query("Table").WhereRaw(escapedJsonCondition);
}

[Fact]
public void WhereRawJsonWithQuestionMarkData()
{
Query query = CreateQuestionMarkJsonQuery(out var rawCondition);
SqlResult result = compiler.Compile(query);
Assert.Equal($"SELECT * FROM \"Table\" WHERE {rawCondition}", result.ToString());
}

[Fact]
public void UsingJsonArray()
{
var query = new Query("Table").WhereRaw("[Json]->'address'->>'country' in ($$)", new[] { 1, 2, 3, 4 });

SqlResult result = compiler.Compile(query);

Assert.Equal("SELECT * FROM \"Table\" WHERE \"Json\"->'address'->>'country' in (1,2,3,4)", result.ToString());
}
}
}
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/SqlServer/SqlServerLegacyLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SqlServerLegacyLimitTests()
public void NoLimitNorOffset()
{
var query = new Query("Table");
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -27,7 +27,7 @@ public void NoLimitNorOffset()
public void LimitOnly()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -36,7 +36,7 @@ public void LimitOnly()
public void OffsetOnly()
{
var query = new Query("Table").Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -45,7 +45,7 @@ public void OffsetOnly()
public void LimitAndOffset()
{
var query = new Query("Table").Limit(5).Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand Down
8 changes: 4 additions & 4 deletions QueryBuilder.Tests/SqlServer/SqlServerLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SqlServerLimitTests()
public void NoLimitNorOffset()
{
var query = new Query("Table");
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.Null(compiler.CompileLimit(ctx));
}
Expand All @@ -27,7 +27,7 @@ public void NoLimitNorOffset()
public void LimitOnly()
{
var query = new Query("Table").Limit(10);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.EndsWith("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", compiler.CompileLimit(ctx));
Assert.Equal(2, ctx.Bindings.Count);
Expand All @@ -39,7 +39,7 @@ public void LimitOnly()
public void OffsetOnly()
{
var query = new Query("Table").Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.EndsWith("OFFSET ? ROWS", compiler.CompileLimit(ctx));

Expand All @@ -51,7 +51,7 @@ public void OffsetOnly()
public void LimitAndOffset()
{
var query = new Query("Table").Limit(5).Offset(20);
var ctx = new SqlResult { Query = query };
var ctx = new SqlResult(compiler) { Query = query };

Assert.EndsWith("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", compiler.CompileLimit(ctx));

Expand Down
Loading