Skip to content

Commit

Permalink
Add case protection for postgreSQL (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilnerm authored and EnriqueL8 committed May 31, 2018
1 parent a96a9c6 commit 20e4a41
Show file tree
Hide file tree
Showing 18 changed files with 1,675 additions and 1,675 deletions.
6 changes: 3 additions & 3 deletions Sources/SwiftKuery/Column.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ public class Column: Field, IndexColumn {
- Throws: QueryError.syntaxError if query build fails.
*/
public func build(queryBuilder: QueryBuilder) throws -> String {
let tableName = Utils.packName(table.nameInQuery, queryBuilder: queryBuilder)
let tableName = table.nameInQuery
if tableName == "" {
throw QueryError.syntaxError("Table name not set. ")
}
var result = tableName + "." + Utils.packName(name, queryBuilder: queryBuilder)
var result = tableName.contains(" ") ? tableName + "." + Utils.packName(name, queryBuilder: queryBuilder) : Utils.packName(tableName + "." + name, queryBuilder: queryBuilder)
if let alias = alias {
result += " AS " + Utils.packName(alias, queryBuilder: queryBuilder)
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public class Column: Field, IndexColumn {
result += " DEFAULT " + Utils.packType(defaultValue)
}
if let checkExpression = checkExpression {
result += " CHECK (" + checkExpression + ")"
result += checkExpression.contains(name) ? " CHECK (" + checkExpression.replacingOccurrences(of: name, with: "\"\(name)\"") + ")" : " CHECK (" + checkExpression + ")"
}
if let collate = collate {
result += " COLLATE \"" + collate + "\""
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftKuery/Insert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public struct Insert: Query {

result += try table.build(queryBuilder: queryBuilder) + " "
if let columns = columns, columns.count != 0 {
result += "(\(columns.map { $0.name }.joined(separator: ", "))) "
result += "(\(columns.map { Utils.packName($0.name, queryBuilder: queryBuilder) }.joined(separator: ", "))) "
}
if let values = values {
result += "VALUES ("
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftKuery/RawField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct RawField: Field {
public func build(queryBuilder: QueryBuilder) -> String {
var result = field
if let alias = alias {
result += " AS " + alias
result += " AS " + Utils.packName(alias, queryBuilder: queryBuilder)
}
return result
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftKuery/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public struct Select: Query {
}

if let using = item.using {
result += " USING (" + using.map { $0.name }.joined(separator: ", ") + ")"
result += " USING (" + using.map { Utils.packName($0.name, queryBuilder: queryBuilder) }.joined(separator: ", ") + ")"
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftKuery/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Utils {
static func packName(_ name: String, queryBuilder: QueryBuilder) -> String {
var result = name
let identifierQuoteCharacter = queryBuilder.substitutions[QueryBuilder.QuerySubstitutionNames.identifierQuoteCharacter.rawValue]
if result.contains(" ") && !result.hasPrefix(identifierQuoteCharacter) {
if !result.hasPrefix(identifierQuoteCharacter) {
result = identifierQuoteCharacter + result + identifierQuoteCharacter
}
return result
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)")
}
}
6 changes: 3 additions & 3 deletions Tests/SwiftKueryTests/TestAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ 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"))
kuery = connection.descriptionOf(query: s)
query = "SELECT * FROM tableAlias AS new"
query = "SELECT * FROM \"tableAlias\" AS \"new\""
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

let t2 = t.as("\"t 2\"")
s = Select(t2.a, from: t2)
kuery = connection.descriptionOf(query: s)
query = "SELECT \"t 2\".a FROM tableAlias AS \"t 2\""
query = "SELECT \"t 2\".\"a\" FROM \"tableAlias\" AS \"t 2\""
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
}
}
Loading

0 comments on commit 20e4a41

Please sign in to comment.