Skip to content
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

Minimize ad-hoc queries by omitting repeating column name specifiers #668

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
12 changes: 6 additions & 6 deletions QueryBuilder.Tests/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public void AdHoc_SingletonRow()
Assert.Equal("WITH `rows` AS (SELECT 1 AS `a`)\nSELECT * FROM `rows`", c[EngineCodes.MySql].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\")\nSELECT * FROM \"rows\"", c[EngineCodes.Sqlite].ToString());
Assert.Equal("WITH \"ROWS\" AS (SELECT 1 AS \"A\" FROM RDB$DATABASE)\nSELECT * FROM \"ROWS\"", c[EngineCodes.Firebird].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\" FROM DUAL)\nSELECT * FROM \"rows\"", c[EngineCodes.Oracle].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 \"a\" FROM DUAL)\nSELECT * FROM \"rows\"", c[EngineCodes.Oracle].ToString());
}

[Fact]
Expand All @@ -482,11 +482,11 @@ public void AdHoc_TwoRows()
var c = Compilers.Compile(query);

Assert.Equal("WITH [rows] AS (SELECT [a], [b], [c] FROM (VALUES (1, 2, 3), (4, 5, 6)) AS tbl ([a], [b], [c]))\nSELECT * FROM [rows]", c[EngineCodes.SqlServer].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\", 2 AS \"b\", 3 AS \"c\" UNION ALL SELECT 4 AS \"a\", 5 AS \"b\", 6 AS \"c\")\nSELECT * FROM \"rows\"", c[EngineCodes.PostgreSql].ToString());
Assert.Equal("WITH `rows` AS (SELECT 1 AS `a`, 2 AS `b`, 3 AS `c` UNION ALL SELECT 4 AS `a`, 5 AS `b`, 6 AS `c`)\nSELECT * FROM `rows`", c[EngineCodes.MySql].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\", 2 AS \"b\", 3 AS \"c\" UNION ALL SELECT 4 AS \"a\", 5 AS \"b\", 6 AS \"c\")\nSELECT * FROM \"rows\"", c[EngineCodes.Sqlite].ToString());
Assert.Equal("WITH \"ROWS\" AS (SELECT 1 AS \"A\", 2 AS \"B\", 3 AS \"C\" FROM RDB$DATABASE UNION ALL SELECT 4 AS \"A\", 5 AS \"B\", 6 AS \"C\" FROM RDB$DATABASE)\nSELECT * FROM \"ROWS\"", c[EngineCodes.Firebird].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\", 2 AS \"b\", 3 AS \"c\" FROM DUAL UNION ALL SELECT 4 AS \"a\", 5 AS \"b\", 6 AS \"c\" FROM DUAL)\nSELECT * FROM \"rows\"", c[EngineCodes.Oracle].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\", 2 AS \"b\", 3 AS \"c\" UNION ALL SELECT 4, 5, 6)\nSELECT * FROM \"rows\"", c[EngineCodes.PostgreSql].ToString());
Assert.Equal("WITH `rows` AS (SELECT 1 AS `a`, 2 AS `b`, 3 AS `c` UNION ALL SELECT 4, 5, 6)\nSELECT * FROM `rows`", c[EngineCodes.MySql].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 AS \"a\", 2 AS \"b\", 3 AS \"c\" UNION ALL SELECT 4, 5, 6)\nSELECT * FROM \"rows\"", c[EngineCodes.Sqlite].ToString());
Assert.Equal("WITH \"ROWS\" AS (SELECT 1 AS \"A\", 2 AS \"B\", 3 AS \"C\" FROM RDB$DATABASE UNION ALL SELECT 4, 5, 6 FROM RDB$DATABASE)\nSELECT * FROM \"ROWS\"", c[EngineCodes.Firebird].ToString());
Assert.Equal("WITH \"rows\" AS (SELECT 1 \"a\", 2 \"b\", 3 \"c\" FROM DUAL UNION ALL SELECT 4, 5, 6 FROM DUAL)\nSELECT * FROM \"rows\"", c[EngineCodes.Oracle].ToString());
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions QueryBuilder.Tests/ParameterTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 15 additions & 10 deletions QueryBuilder/Compilers/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,22 @@ protected virtual SqlResult CompileAdHocQuery(AdHocTableFromClause adHoc)
{
var ctx = new SqlResult();

var row = "SELECT " + string.Join(", ", adHoc.Columns.Select(col => $"{parameterPlaceholder} AS {Wrap(col)}"));
var firstRow = "SELECT " + string.Join(", ", adHoc.Columns.Select(col => $"{parameterPlaceholder} {ColumnAsKeyword}{Wrap(col)}"));
var row = "SELECT " + string.Join(", ", adHoc.Columns.Select(col => $"{parameterPlaceholder}"));

var fromTable = SingleRowDummyTableName;

if (fromTable != null)
{
row += $" FROM {fromTable}";
var fromClause = $" FROM {fromTable}";
firstRow += fromClause;
row += fromClause;
}

var rows = string.Join(" UNION ALL ", Enumerable.Repeat(row, adHoc.Values.Count / adHoc.Columns.Count));
var rowCount = adHoc.Values.Count / adHoc.Columns.Count;
var rows = rowCount > 0
? string.Join(" UNION ALL ", Enumerable.Repeat(row, rowCount - 1).Prepend(firstRow))
: string.Empty;

ctx.RawSql = rows;
ctx.Bindings = adHoc.Values;
Expand Down Expand Up @@ -555,7 +561,7 @@ public virtual string CompileColumn(SqlResult ctx, AbstractColumn column)
return $"{agg}(CASE WHEN {filterCondition} THEN {col} END){alias}";
}

return Wrap((column as Column).Name);
return Wrap(((Column)column).Name);

}

Expand Down Expand Up @@ -618,11 +624,9 @@ protected virtual string CompileColumns(SqlResult ctx)
.Select(x => CompileColumn(ctx, new Column { Name = x }))
.ToList();

string sql = string.Empty;

if (aggregateColumns.Count == 1)
{
sql = string.Join(", ", aggregateColumns);
var sql = string.Join(", ", aggregateColumns);

if (ctx.Query.IsDistinct)
{
Expand Down Expand Up @@ -675,7 +679,7 @@ public virtual string CompileUnion(SqlResult ctx)
}
else
{
var combineRawClause = clause as RawCombine;
var combineRawClause = (RawCombine)clause;

ctx.Bindings.AddRange(combineRawClause.Bindings);

Expand Down Expand Up @@ -802,9 +806,10 @@ public virtual string CompileOrders(SqlResult ctx)
return WrapIdentifiers(raw.Expression);
}

var direction = (x as OrderBy).Ascending ? "" : " DESC";
var orderByClause = (OrderBy)x;
var direction = orderByClause.Ascending ? "" : " DESC";

return Wrap((x as OrderBy).Column) + direction;
return Wrap(orderByClause.Column) + direction;
});

return "ORDER BY " + string.Join(", ", columns);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ I announce updates on My [Twitter Account](https://twitter.com/ahmadmuzavi), and
Usually it's impossible to support all available database vendors, this why we focus on the major ones, and we encourage you to create your own compiler for your database.

### Do you accept new compilers?
Unfortunetly no, the reason is this will add overhead for the project contributors, we prefer to improve the quality of the existing compilers instead.
Unfortunately no, the reason is this will add overhead for the project contributors, we prefer to improve the quality of the existing compilers instead.

### How can I support the project?
- Star the project here in Github, and share it with your friends
Expand Down
2 changes: 1 addition & 1 deletion sqlkata.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
Expand All @@ -14,6 +13,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{86D00525-7428-4DD7-914D-0A10D5C53EDE}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
README.md = README.md
EndProjectSection
EndProject
Global
Expand Down