Skip to content

Commit

Permalink
Merge pull request #41 from noppoMan/returns-nonoptional-querystatus
Browse files Browse the repository at this point in the history
Returns non optional QueryStatus
  • Loading branch information
noppoMan authored Feb 20, 2017
2 parents 1880759 + 9e2326d commit 762bd97
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Sources/Mysql/QueryStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
public struct QueryStatus {
public let affectedRows: UInt64
public let insertId: UInt64

public init(affectedRows: UInt64, insertId: UInt64) {
self.affectedRows = affectedRows
self.insertId = insertId
}

public init() {
self.affectedRows = 0
self.insertId = 0
}
}
28 changes: 14 additions & 14 deletions Sources/SwiftKnex/Knex+Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@ extension Knex {
return try execute(.select, trx).asResultSet()
}

public func insert(into table: String, collection: [Serializable], trx: Connection? = nil) throws -> QueryStatus? {
public func insert(into table: String, collection: [Serializable], trx: Connection? = nil) throws -> QueryStatus {
queryBuilder.table(table)

return try execute(.batchInsert(collection.map({ try $0.serialize() })), trx).asQueryStatus()
return try execute(.batchInsert(collection.map({ try $0.serialize() })), trx).asQueryStatus() ?? QueryStatus()
}

public func insert(into table: String, collection: [[String: Any]], trx: Connection? = nil) throws -> QueryStatus? {
public func insert(into table: String, collection: [[String: Any]], trx: Connection? = nil) throws -> QueryStatus {
queryBuilder.table(table)

return try execute(.batchInsert(collection), trx).asQueryStatus()
return try execute(.batchInsert(collection), trx).asQueryStatus() ?? QueryStatus()
}

public func insert(into table: String, values: Serializable, trx: Connection? = nil) throws -> QueryStatus? {
public func insert(into table: String, values: Serializable, trx: Connection? = nil) throws -> QueryStatus {
queryBuilder.table(table)
return try execute(.insert(values.serialize()), trx).asQueryStatus()
return try execute(.insert(values.serialize()), trx).asQueryStatus() ?? QueryStatus()
}

public func insert(into table: String, values: [String: Any], trx: Connection? = nil) throws -> QueryStatus? {
public func insert(into table: String, values: [String: Any], trx: Connection? = nil) throws -> QueryStatus {
queryBuilder.table(table)
return try execute(.insert(values), trx).asQueryStatus()
return try execute(.insert(values), trx).asQueryStatus() ?? QueryStatus()
}

public func update(sets: [String: Any], trx: Connection? = nil) throws -> QueryStatus? {
return try execute(.update(sets), trx).asQueryStatus()
public func update(sets: [String: Any], trx: Connection? = nil) throws -> QueryStatus {
return try execute(.update(sets), trx).asQueryStatus() ?? QueryStatus()
}

public func update(query: String, params: [Any] = [], trx: Connection? = nil) throws -> QueryStatus? {
return try execute(.updateRaw(query: query, params: params), trx).asQueryStatus()
public func update(query: String, params: [Any] = [], trx: Connection? = nil) throws -> QueryStatus {
return try execute(.updateRaw(query: query, params: params), trx).asQueryStatus() ?? QueryStatus()
}

public func delete(trx: Connection? = nil) throws -> QueryStatus? {
return try execute(.delete, trx).asQueryStatus()
public func delete(trx: Connection? = nil) throws -> QueryStatus {
return try execute(.delete, trx).asQueryStatus() ?? QueryStatus()
}

private func execute(_ type: QueryType, _ trx: Connection?) throws -> QueryResult {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftKnexTests/InsertTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ class InsertTests: XCTestCase {
func testTypeSafeInsert() {
let user = User(id: 1, name: "new-user", email: "new-user@example.com", age: 30, country: nil)
let res = try! con.knex().insert(into: "test_users", values: user)
XCTAssertEqual(res!.insertId, 1)
XCTAssertEqual(res.insertId, 1)
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftKnexTests/JSONDataTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ class JSONDataTypeTests: XCTestCase {
.where(raw("JSON_UNQUOTE(JSON_EXTRACT(body,'$.name')) = ?", ["Luke"]))
.update(query: "body = JSON_REPLACE(body, '$.name', ?)", params: ["Obi Wan"])

XCTAssertEqual(res!.affectedRows, 1)
XCTAssertEqual(res.affectedRows, 1)
}
}

0 comments on commit 762bd97

Please sign in to comment.