@@ -64,6 +64,8 @@ public final class SQLStatement {
64
64
65
65
// MARK: Execute
66
66
67
+ #warning("TODO: add example")
68
+
67
69
/// Executes the statement and returns `true` if the next row is available.
68
70
/// Returns `false` if the statement is finished executing and no more data
69
71
/// is available. Throws an error if an error is encountered.
@@ -74,6 +76,7 @@ public final class SQLStatement {
74
76
return SQLRow ( statement: self )
75
77
}
76
78
79
+ #warning("TODO: document")
77
80
@discardableResult
78
81
public func execute( ) throws -> SQLStatement {
79
82
try isOK ( sqlite3_step ( ref) )
@@ -82,14 +85,24 @@ public final class SQLStatement {
82
85
83
86
// MARK: Binding Parameters
84
87
85
- /// Binds values to the SQL statement parameters.
88
+ /// Binds values to the statement parameters.
89
+ ///
90
+ /// try db.statement("INSERT INTO Users (Level, Name) VALUES (?, ?)")
91
+ /// .bind(80, "John")
92
+ /// .execute()
93
+ ///
86
94
@discardableResult
87
95
public func bind( _ parameters: SQLDataType ? ... ) throws -> Self {
88
96
try bind ( parameters)
89
97
return self
90
98
}
91
99
92
- /// Binds values to the SQL statement parameters.
100
+ /// Binds values to the statement parameters.
101
+ ///
102
+ /// try db.statement("INSERT INTO Users (Level, Name) VALUES (?, ?)")
103
+ /// .bind([80, "John"])
104
+ /// .execute()
105
+ ///
93
106
@discardableResult
94
107
public func bind( _ parameters: [ SQLDataType ? ] ) throws -> Self {
95
108
for (index, value) in zip ( parameters. indices, parameters) {
@@ -98,23 +111,37 @@ public final class SQLStatement {
98
111
return self
99
112
}
100
113
101
- /// Binds values to the SQL statement parameters.
114
+ /// Binds values to the named statement parameters.
115
+ ///
116
+ /// let row = try db.statement("SELECT Level, Name FROM Users WHERE Name = :param LIMIT 1")
117
+ /// .bind([":param": "John""])
118
+ /// .next()
119
+ ///
120
+ /// - parameter name: The name of the parameter. If the name is missing, throws
121
+ /// an error.
102
122
@discardableResult
103
123
public func bind( _ parameters: [ String : SQLDataType ? ] ) throws -> Self {
104
- for (name , value) in parameters {
105
- try _bind ( value, for: name )
124
+ for (key , value) in parameters {
125
+ try _bind ( value, for: key )
106
126
}
107
127
return self
108
128
}
109
129
110
130
/// Binds values to the parameter with the given name.
111
131
///
112
- /// - parameter name:
132
+ /// let row = try db.statement("SELECT Level, Name FROM Users WHERE Name = :param LIMIT 1")
133
+ /// .bind("John", for: ":param")
134
+ /// .next()
135
+ ///
136
+ /// - parameter name: The name of the parameter. If the name is missing, throws
137
+ /// an error.
113
138
@discardableResult
114
139
public func bind< T: SQLDataType > ( _ value: T ? , for name: String ) throws -> Self {
115
140
let index = sqlite3_bind_parameter_index ( ref, name)
116
- guard index > 0 else { fatalError ( " Parameter not found: \( name) " ) }
117
- try bind ( value, at: Int ( index) )
141
+ guard index > 0 else {
142
+ throw SQLError ( code: SQLITE_MISUSE, message: " Failed to find parameter named \( name) " )
143
+ }
144
+ try bind ( value, at: Int ( index - 1 ) )
118
145
return self
119
146
}
120
147
@@ -134,8 +161,10 @@ public final class SQLStatement {
134
161
135
162
private func _bind( _ value: SQLDataType ? , for name: String ) throws {
136
163
let index = sqlite3_bind_parameter_index ( ref, name)
137
- guard index > 0 else { fatalError ( " Parameter not found: \( name) " ) }
138
- try _bind ( value, at: Int32 ( index) )
164
+ guard index > 0 else {
165
+ throw SQLError ( code: SQLITE_MISUSE, message: " Failed to find parameter named \( name) " )
166
+ }
167
+ try _bind ( value, at: index - 1 )
139
168
}
140
169
141
170
private func _bind( _ value: SQLDataType ? , at index: Int32 ) throws {
0 commit comments