-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement automatically binding string-interpolated values (#56)
* Implement autom-binding with string interpolation * Implement automatic binding in existing `raw` method This removes the old `bind()` API. * Update to auto-generated Linux test file * Clean up and make Fragement internal
- Loading branch information
1 parent
e282a4c
commit 159322e
Showing
5 changed files
with
97 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
public struct SQLQueryString { | ||
enum Fragment { | ||
case literal(String) | ||
case value(Encodable) | ||
} | ||
|
||
var fragments: [Fragment] | ||
} | ||
|
||
extension SQLQueryString: ExpressibleByStringLiteral { | ||
public init(stringLiteral value: String) { | ||
fragments = [.literal(value)] | ||
} | ||
} | ||
|
||
extension SQLQueryString: ExpressibleByStringInterpolation { | ||
|
||
public init(stringInterpolation: SQLQueryString) { | ||
fragments = stringInterpolation.fragments | ||
} | ||
} | ||
|
||
extension SQLQueryString: StringInterpolationProtocol { | ||
public init(literalCapacity: Int, interpolationCount: Int) { | ||
fragments = [] | ||
} | ||
|
||
mutating public func appendLiteral(_ literal: String) { | ||
fragments.append(.literal(literal)) | ||
} | ||
|
||
mutating public func appendInterpolation(_ literal: String) { | ||
fragments.append(.literal(literal)) | ||
} | ||
|
||
mutating public func appendInterpolation(bind value: Encodable) { | ||
fragments.append(.value(value)) | ||
} | ||
} | ||
|
||
extension SQLQueryString: SQLExpression { | ||
public func serialize(to serializer: inout SQLSerializer) { | ||
for fragment in fragments { | ||
switch fragment { | ||
case let .literal(str): | ||
serializer.write(str) | ||
case let .value(v): | ||
serializer.dialect.nextBindPlaceholder().serialize(to: &serializer) | ||
serializer.binds.append(v) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,8 @@ | ||
import XCTest | ||
|
||
@testable import SQLKitTests | ||
import SQLKitTests | ||
|
||
// MARK: SQLKitTests | ||
var tests = [XCTestCaseEntry]() | ||
tests += SQLKitTests.__allTests() | ||
|
||
extension SQLKitTests { | ||
static let __allSQLKitTestsTests = [ | ||
("testBenchmarker", testBenchmarker), | ||
("testLockingClause_forUpdate", testLockingClause_forUpdate), | ||
("testLockingClause_lockInShareMode", testLockingClause_lockInShareMode), | ||
] | ||
} | ||
|
||
// MARK: Test Runner | ||
|
||
#if !os(macOS) | ||
public func __buildTestEntries() -> [XCTestCaseEntry] { | ||
return [ | ||
// SQLKitTests | ||
testCase(SQLKitTests.__allSQLKitTestsTests), | ||
] | ||
} | ||
|
||
let tests = __buildTestEntries() | ||
XCTMain(tests) | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#if !canImport(ObjectiveC) | ||
import XCTest | ||
|
||
extension SQLKitTests { | ||
// DO NOT MODIFY: This is autogenerated, use: | ||
// `swift test --generate-linuxmain` | ||
// to regenerate. | ||
static let __allTests__SQLKitTests = [ | ||
("testBenchmarker", testBenchmarker), | ||
("testLockingClause_forUpdate", testLockingClause_forUpdate), | ||
("testLockingClause_lockInShareMode", testLockingClause_lockInShareMode), | ||
("testRawQueryStringInterpolation", testRawQueryStringInterpolation), | ||
] | ||
} | ||
|
||
public func __allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(SQLKitTests.__allTests__SQLKitTests), | ||
] | ||
} | ||
#endif |