Skip to content

Commit

Permalink
Add AUTO INCREMENT support for SQLite (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilnerm authored Jun 4, 2018
1 parent 337499d commit 3892fee
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.build
build
Package.resolved
.DS_Store
.DS_Store
Packages
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/CLibpq.git", .upToNextMinor(from: "0.1.0")),
.package(url: "https://github.com/IBM-Swift/Swift-Kuery.git", from: "1.3.0"),
.package(url: "https://github.com/IBM-Swift/Swift-Kuery.git", from: "2.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftKueryPostgreSQL/PostgreSQLConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class PostgreSQLConnection: Connection {
}


static func createAutoIncrement(_ type: String) -> String {
static func createAutoIncrement(_ type: String, _: Bool) -> String {
switch type {
case "smallint":
return "smallserial"
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwiftKueryPostgreSQLTests/CommonUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func executeRawQuery(_ raw: String, connection: Connection, callback: @escaping
}

func cleanUp(table: String, connection: Connection, callback: @escaping (QueryResult)->()) {
connection.execute("DROP TABLE " + table) { result in
connection.execute("DROP TABLE \"" + table + "\"") { result in
callback(result)
}
}
Expand Down Expand Up @@ -151,6 +151,7 @@ func rowsAsArray(_ result: ResultSet) -> [[Any?]] {
}

func createConnection() -> PostgreSQLConnection {
// We do not specify a database name for our tests so postgreSQL defaults to using the username as the database name.
let host = read(fileName: "host.txt")
let port = Int32(read(fileName: "port.txt"))!
let username = read(fileName: "username.txt")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftKueryPostgreSQLTests/TestAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TestAlias: XCTestCase {
}
cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftKueryPostgreSQLTests/TestInsert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ class TestInsert: XCTestCase {
cleanUp(table: t.tableName, connection: connection) { result in
cleanUp(table: t2.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t2.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t2.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -181,7 +181,7 @@ class TestInsert: XCTestCase {
}

cleanUp(table: t3.tableName, connection: connection) { result in
executeRawQuery("CREATE TABLE " + t3.tableName + " (a SERIAL PRIMARY KEY, b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t3.tableName + "\" (a SERIAL PRIMARY KEY, b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")
let i7 = Insert(into: t3, valueTuples: [(t3.b, 5)], returnID: true)
Expand Down
12 changes: 6 additions & 6 deletions Tests/SwiftKueryPostgreSQLTests/TestJoin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ class TestJoin: XCTestCase {

cleanUp(table: myTable3.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + myTable1.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable1.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in

XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + myTable2.tableName + " (c varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable2.tableName + "\" (c varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + myTable3.tableName + " (d varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable3.tableName + "\" (d varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -203,16 +203,16 @@ class TestJoin: XCTestCase {

cleanUp(table: myTable3.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + myTable1.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable1.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in

XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + myTable2.tableName + " (c varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable2.tableName + "\" (c varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + myTable3.tableName + " (d varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + myTable3.tableName + "\" (d varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftKueryPostgreSQLTests/TestParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TestParameters: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -94,7 +94,7 @@ class TestParameters: XCTestCase {
XCTAssertEqual(rows![2][0]! as! String, "peach", "Wrong value in row 2 column 0")
XCTAssertEqual(rows![2][1]! as! Int32, 2, "Wrong value in row 2 column 1")

let raw = "UPDATE " + t.tableName + " SET a = 'banana', b = $1 WHERE a = $2"
let raw = "UPDATE \"" + t.tableName + "\" SET a = 'banana', b = $1 WHERE a = $2"
executeRawQueryWithParameters(raw, connection: connection, parameters: 4, "peach") { result, rows in
XCTAssertEqual(result.success, true, "UPDATE failed")
XCTAssertNil(result.asError, "Error in UPDATE: \(result.asError!)")
Expand Down Expand Up @@ -143,7 +143,7 @@ class TestParameters: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -243,7 +243,7 @@ class TestParameters: XCTestCase {
let s1 = Select(from: t).where(t.a == Parameter())
let preparedSelect = try connection.prepareStatement(s1)

let s2 = "SELECT * FROM " + t.tableName
let s2 = "SELECT * FROM \"" + t.tableName + "\""
let preparedSelect2 = try connection.prepareStatement(s2)

connection.execute(preparedStatement: preparedInsert, parameters: ["apple", 3, "banana", -8]) { result in
Expand Down
12 changes: 6 additions & 6 deletions Tests/SwiftKueryPostgreSQLTests/TestSelect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TestSelect: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -194,7 +194,7 @@ class TestSelect: XCTestCase {
XCTAssertEqual(rows!.count, 2, "SELECT returned wrong number of rows: \(rows!.count) instead of 2")
XCTAssertEqual(rows![0][0]! as! String, "apple", "Wrong value in row 0 column 0")

let s9 = "Select * from \(t.tableName) where a IN ('apple', 'lalala')"
let s9 = "Select * from \"\(t.tableName)\" where a IN ('apple', 'lalala')"
executeRawQuery(s9, connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "SELECT failed")
XCTAssertNotNil(result.asResultSet, "SELECT returned no rows")
Expand Down Expand Up @@ -242,15 +242,15 @@ class TestSelect: XCTestCase {
cleanUp(table: t2.tableName, connection: connection) { result in
cleanUp(table: t3.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t1.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t1.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t2.tableName + " (c varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t2.tableName + "\" (c varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t3.tableName + " (d varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t3.tableName + "\" (d varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -321,7 +321,7 @@ class TestSelect: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), date date, timestamp timestamp with time zone, time time with time zone)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), date date, timestamp timestamp with time zone, time time with time zone)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftKueryPostgreSQLTests/TestSubquery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestSubquery: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down
12 changes: 6 additions & 6 deletions Tests/SwiftKueryPostgreSQLTests/TestTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TestTransaction: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -109,7 +109,7 @@ class TestTransaction: XCTestCase {

cleanUp(table: t.tableName, connection: connection) { result in

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -163,7 +163,7 @@ class TestTransaction: XCTestCase {
XCTAssertEqual(result.success, true, "Failed to start transaction")
XCTAssertNil(result.asError, "Error in start transaction: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -264,7 +264,7 @@ class TestTransaction: XCTestCase {
connection.startTransaction() { result in
XCTAssertEqual(result.success, false, "Started second transaction")

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -320,7 +320,7 @@ class TestTransaction: XCTestCase {
XCTAssertEqual(result.success, true, "Failed to start transaction")
XCTAssertNil(result.asError, "Error in start transaction: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down Expand Up @@ -375,7 +375,7 @@ class TestTransaction: XCTestCase {
XCTAssertEqual(result.success, true, "Failed to start transaction")
XCTAssertNil(result.asError, "Error in start transaction: \(result.asError!)")

executeRawQuery("CREATE TABLE " + t.tableName + " (a varchar(40), b integer)", connection: connection) { result, rows in
executeRawQuery("CREATE TABLE \"" + t.tableName + "\" (a varchar(40), b integer)", connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "CREATE TABLE failed")
XCTAssertNil(result.asError, "Error in CREATE TABLE: \(result.asError!)")

Expand Down
Loading

0 comments on commit 3892fee

Please sign in to comment.