Skip to content

Commit

Permalink
Ensure column names are packed correctly (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilnerm authored and EnriqueL8 committed Jun 4, 2018
1 parent 64b2ae3 commit de890f1
Show file tree
Hide file tree
Showing 12 changed files with 1,645 additions and 1,645 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftKuery/Column.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public class Column: Field, IndexColumn {
if tableName == "" {
throw QueryError.syntaxError("Table name not set. ")
}
var result = tableName.contains(" ") ? tableName + "." + Utils.packName(name, queryBuilder: queryBuilder) : Utils.packName(tableName + "." + name, queryBuilder: queryBuilder)
var result = Utils.packName(tableName, queryBuilder: queryBuilder) + "." + Utils.packName(name, queryBuilder: queryBuilder)
if let alias = alias {
result += " AS " + Utils.packName(alias, queryBuilder: queryBuilder)
}
Expand Down
26 changes: 13 additions & 13 deletions Tests/SwiftKueryTests/TestAggregateFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,90 +46,90 @@ class TestAggregateFunctions: XCTestCase {
.group(by: t.a)
.having(avg(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING AVG(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING AVG(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(max(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING MAX(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING MAX(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(min(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING MIN(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING MIN(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(sum(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING SUM(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING SUM(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(last(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING LAST(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING LAST(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(first(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING FIRST(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING FIRST(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(count(t.b) > 3)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING COUNT(\"table.b\") > 3"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING COUNT(\"table\".\"b\") > 3"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(countDistinct(t.b) != 0)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING COUNT(DISTINCT(\"table.b\")) <> 0"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING COUNT(DISTINCT(\"table\".\"b\")) <> 0"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(ucase(sum(t.b)) <= -1)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING UCASE(SUM(\"table.b\")) <= -1"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING UCASE(SUM(\"table\".\"b\")) <= -1"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(lcase(sum(t.b)) <= -1)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING LCASE(SUM(\"table.b\")) <= -1"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING LCASE(SUM(\"table\".\"b\")) <= -1"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(len(sum(t.b)) <= -1)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING LEN(SUM(\"table.b\")) <= -1"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING LEN(SUM(\"table\".\"b\")) <= -1"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
s = Select(t.a, from: t)
.group(by: t.a)
.having(round(sum(t.b), to: 2) >= 9.08)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING ROUND(SUM(\"table.b\"), 2) >= 9.08"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING ROUND(SUM(\"table\".\"b\"), 2) >= 9.08"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(t.a, from: t)
.group(by: t.a)
.having(mid(sum(t.b), start: 3, length: 6) <= -1)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"table.a\" FROM \"table\" GROUP BY \"table.a\" HAVING MID(SUM(\"table.b\"), 3, 6) <= -1"
query = "SELECT \"table\".\"a\" FROM \"table\" GROUP BY \"table\".\"a\" HAVING MID(SUM(\"table\".\"b\"), 3, 6) <= -1"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftKueryTests/TestAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TestAlias: XCTestCase {

var s = Select(t.a.as("\"fruit name\""), t.b.as("number"), from: t)
var kuery = connection.descriptionOf(query: s)
var query = "SELECT \"tableAlias.a\" AS \"fruit name\", \"tableAlias.b\" AS \"number\" FROM \"tableAlias\""
var query = "SELECT \"tableAlias\".\"a\" AS \"fruit name\", \"tableAlias\".\"b\" AS \"number\" FROM \"tableAlias\""
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(from: t.as("new"))
Expand Down
Loading

0 comments on commit de890f1

Please sign in to comment.