Skip to content

Commit

Permalink
Merge pull request #54 from siemensikkema/master
Browse files Browse the repository at this point in the history
Fix SQL UPDATE syntax
  • Loading branch information
tanner0101 authored Jul 20, 2016
2 parents 855480e + 60ca7a4 commit 4a7a95f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/Fluent/SQL/GeneralSQLSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public class GeneralSQLSerializer: SQLSerializer {

statement += "UPDATE"
statement += sql(table)
statement += "SET"

let (dataClause, dataValues) = sql(data)
let (dataClause, dataValues) = sql(update: data)
statement += dataClause
values += dataValues

Expand Down Expand Up @@ -246,6 +247,17 @@ public class GeneralSQLSerializer: SQLSerializer {
)
}

public func sql(update data: [String: Value]) -> (String, [Value]) {
return (
data.map(sql).joined(separator: ", "),
Array(data.values)
)
}

public func sql(key: String, value: Value) -> String {
return sql(key) + " = " + sql(value)
}

public func sql(_ strings: [String]) -> String {
return strings.joined(separator: " ")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fluent/SQLSerializerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SQLSerializerTests: XCTestCase {
let update = SQL.update(table: "friends", filters: [filter], data: ["not it": true])
let (statement, values) = serialize(update)

XCTAssertEqual(statement, "UPDATE `friends` (`not it`) VALUES (?) WHERE `name` = ?")
XCTAssertEqual(statement, "UPDATE `friends` SET `not it` = ? WHERE `name` = ?")
XCTAssertEqual(values.first?.bool, true)
XCTAssertEqual(values.last?.string, "duck")
XCTAssertEqual(values.count, 2)
Expand Down

0 comments on commit 4a7a95f

Please sign in to comment.