Skip to content

Commit 57d789e

Browse files
committed
Add bingings tests
1 parent afb041c commit 57d789e

File tree

4 files changed

+157
-14
lines changed

4 files changed

+157
-14
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/SwiftSQL.xcscheme

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@
4141
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4242
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4343
shouldUseLaunchSchemeArgsEnv = "YES"
44-
codeCoverageEnabled = "YES">
44+
codeCoverageEnabled = "YES"
45+
onlyGenerateCoverageForSpecifiedTargets = "YES">
46+
<CodeCoverageTargets>
47+
<BuildableReference
48+
BuildableIdentifier = "primary"
49+
BlueprintIdentifier = "SwiftSQL"
50+
BuildableName = "SwiftSQL"
51+
BlueprintName = "SwiftSQL"
52+
ReferencedContainer = "container:">
53+
</BuildableReference>
54+
</CodeCoverageTargets>
4555
<Testables>
4656
<TestableReference
4757
skipped = "NO">

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img src="https://github.com/kean/SwiftSQL/workflows/CI/badge.svg">
44
</p>
55

6-
**SwiftSQL** introduces a Swift API for [SQLite](https://www.sqlite.org/index.html). It doesn't have any ORM-like features. It maps directly to the SQLite concepts with some affordances to make it great Swift API. It is feature-complete, fully documented and tested.
6+
**SwiftSQL** introduces a Swift API for [SQLite](https://www.sqlite.org/index.html). It doesn't have any ORM-like features. It maps directly to the SQLite concepts with some affordances to make it great a Swift API. It is feature-complete, fully documented and tested.
77

88
<br/>
99

@@ -24,7 +24,8 @@ The life-cycle of a prepared statement object usually goes like this:
2424
```swift
2525
let db = try SQLConnection(url: storeURL)
2626
let statement = try db.statement("""
27-
INSERT INTO Users (Name, Surname) VALUES (?, ?)
27+
INSERT INTO Users (Name, Surname)
28+
VALUES (?, ?)
2829
""")
2930
```
3031

Sources/SwiftSQL/SQLStatement.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ public final class SQLStatement {
8282

8383
// MARK: Binding Parameters
8484

85-
#warning("TODO: document")
86-
#warning("TODO: bind should throw")
85+
/// Binds values to the SQL statement parameters.
8786
@discardableResult
8887
public func bind(_ parameters: SQLDataType?...) throws -> Self {
8988
try bind(parameters)
9089
return self
9190
}
9291

92+
/// Binds values to the SQL statement parameters.
9393
@discardableResult
9494
public func bind(_ parameters: [SQLDataType?]) throws -> Self {
9595
for (index, value) in zip(parameters.indices, parameters) {
@@ -98,6 +98,7 @@ public final class SQLStatement {
9898
return self
9999
}
100100

101+
/// Binds values to the SQL statement parameters.
101102
@discardableResult
102103
public func bind(_ parameters: [String: SQLDataType?]) throws -> Self {
103104
for (name, value) in parameters {
@@ -106,6 +107,9 @@ public final class SQLStatement {
106107
return self
107108
}
108109

110+
/// Binds values to the parameter with the given name.
111+
///
112+
/// - parameter name:
109113
@discardableResult
110114
public func bind<T: SQLDataType>(_ value: T?, for name: String) throws -> Self {
111115
let index = sqlite3_bind_parameter_index(ref, name)
@@ -114,6 +118,8 @@ public final class SQLStatement {
114118
return self
115119
}
116120

121+
/// Binds value to the given index.
122+
///
117123
/// - parameter index: The index starts at 0.
118124
@discardableResult
119125
public func bind<T: SQLDataType>(_ value: T?, at index: Int) throws -> Self {
@@ -155,8 +161,10 @@ public final class SQLStatement {
155161
/// `clearBindings()` allows you to clear those bound values. It is not required
156162
/// to call `clearBindings()` every time. Simplify overwriting the existing values
157163
/// does the trick.
158-
public func clearBindings() throws {
164+
@discardableResult
165+
public func clearBindings() throws -> SQLStatement {
159166
try isOK(sqlite3_clear_bindings(ref))
167+
return self
160168
}
161169

162170
// MARK: Reset
@@ -167,8 +175,10 @@ public final class SQLStatement {
167175
/// After a prepared statement has been evaluated it can be reset in order to
168176
/// be evaluated again by a call to `reset()`. Reusing compiled statements
169177
/// can give a significant performance improvement.
170-
public func reset() throws {
178+
@discardableResult
179+
public func reset() throws -> SQLStatement {
171180
try isOK(sqlite3_reset(ref))
181+
return self
172182
}
173183

174184
// MARK: Private

Tests/SwiftSQLTests/SQLStatementTests.swift

Lines changed: 129 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,158 @@ final class SQLStatementTests: XCTestCase {
3939

4040
// MARK: Binding Parameters
4141

42-
func testBindParameters() throws {
42+
func testBindUsingIndexes() throws {
4343
/// GIVEN
4444
try db.createTables()
45+
4546
let statement = try db.statement("""
4647
INSERT INTO Users (Level, Name)
4748
VALUES (?, ?)
4849
""")
4950

5051
// WHEN
5152
try statement
52-
.bind(Int64(80), at: 0)
53+
.bind(80, at: 0)
5354
.bind("Alex", at: 1)
5455
.execute()
5556

5657
// THEN
57-
#warning("TODO: execute statement")
58+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
59+
XCTAssertEqual(row[0], 80)
60+
XCTAssertEqual(row[1], "Alex")
5861
}
5962

60-
func testBindInt() throws {
63+
func testBindNilUsingIndexes() throws {
64+
/// GIVEN
65+
try db.createTables()
66+
67+
let statement = try db.statement("""
68+
INSERT INTO Users (Level, Name)
69+
VALUES (?, ?)
70+
""")
71+
72+
// WHEN
73+
try statement
74+
.bind(80, at: 0)
75+
.bind(nil as String?, at: 1)
76+
.execute()
6177

78+
// THEN
79+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
80+
XCTAssertEqual(row[0], 80)
81+
XCTAssertEqual(row[1] as String?, nil)
6282
}
6383

64-
// ...
84+
func testBindUsingArray() throws {
85+
/// GIVEN
86+
try db.createTables()
6587

66-
func testBindByName() throws {
88+
let statement = try db.statement("""
89+
INSERT INTO Users (Level, Name)
90+
VALUES (?, ?)
91+
""")
92+
93+
// WHEN
94+
try statement
95+
.bind([80, "Alex"])
96+
.execute()
97+
98+
// THEN
99+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
100+
XCTAssertEqual(row[0], 80)
101+
XCTAssertEqual(row[1], "Alex")
102+
}
103+
104+
func testBindUsingArrayNilValue() throws {
105+
/// GIVEN
106+
try db.createTables()
107+
108+
let statement = try db.statement("""
109+
INSERT INTO Users (Level, Name)
110+
VALUES (?, ?)
111+
""")
112+
113+
// WHEN
114+
try statement
115+
.bind([80, nil])
116+
.execute()
117+
118+
// THEN
119+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
120+
XCTAssertEqual(row[0], 80)
121+
XCTAssertEqual(row[1] as String?, nil)
122+
}
123+
124+
func testBindUsingVariadics() throws {
125+
/// GIVEN
126+
try db.createTables()
67127

128+
let statement = try db.statement("""
129+
INSERT INTO Users (Level, Name)
130+
VALUES (?, ?)
131+
""")
132+
133+
// WHEN
134+
try statement
135+
.bind(80, "Alex")
136+
.execute()
137+
138+
// THEN
139+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
140+
XCTAssertEqual(row[0], 80)
141+
XCTAssertEqual(row[1], "Alex")
68142
}
69143

70-
func testBindByNameMultiple() throws {
144+
func testBindUsingVariadicsNilValue() throws {
145+
/// GIVEN
146+
try db.createTables()
71147

148+
let statement = try db.statement("""
149+
INSERT INTO Users (Level, Name)
150+
VALUES (?, ?)
151+
""")
152+
153+
// WHEN
154+
try statement
155+
.bind(80, nil)
156+
.execute()
157+
158+
// THEN
159+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
160+
XCTAssertEqual(row[0], 80)
161+
XCTAssertEqual(row[1] as String?, nil)
162+
}
163+
164+
func testBindByName() throws {
165+
#warning("TODO: implement")
166+
}
167+
168+
func testClearBinding() throws {
169+
/// GIVEN
170+
try db.createTables()
171+
172+
let statement = try db.statement("""
173+
INSERT INTO Users (Level, Name)
174+
VALUES (?, ?)
175+
""")
176+
177+
try statement
178+
.bind(80, nil)
179+
.execute()
180+
181+
try db.execute("DELETE FROM Users")
182+
183+
// WHEN
184+
try statement
185+
.reset()
186+
.clearBindings()
187+
.bind("Alex", at: 1)
188+
.execute()
189+
190+
// THEN
191+
let row = try XCTUnwrap(db.statement("SELECT Level, Name FROM Users").next())
192+
XCTAssertEqual(row[0] as Int?, nil)
193+
XCTAssertEqual(row[1], "Alex")
72194
}
73195

74196
// MARK: Query

0 commit comments

Comments
 (0)