@@ -6,7 +6,6 @@ import SQLite3
66/// To execute an SQL statement, it must first be compiled into a byte-code program using one of these routines.
77/// Or, in other words, these routines are constructors for the prepared statement object.
88public final class PreparedStatement : DatabaseHandle {
9- @usableFromInline
109 let stmt : OpaquePointer
1110
1211 /// Find the database handle of a prepared statement.
@@ -80,54 +79,6 @@ public final class PreparedStatement: DatabaseHandle {
8079 public func decode< T> ( _ type: T . Type ) throws -> T where T: Decodable {
8180 try StatementDecoder ( ) . decode ( type, from: self )
8281 }
83-
84- // MARK: - String
85-
86- public func string( at index: Int32 ) -> String ? {
87- sqlite3_column_text ( stmt, index) . map { String ( cString: $0) }
88- }
89-
90- public func string( for name: String ) -> String ? {
91- columnIndexByName [ name] . flatMap { string ( at: $0) }
92- }
93-
94- // MARK: - Int64
95-
96- public func int64( at index: Int32 ) -> Int64 {
97- sqlite3_column_int64 ( stmt, index)
98- }
99-
100- public func int64( for name: String ) -> Int64 ? {
101- columnIndexByName [ name] . map { int64 ( at: $0) }
102- }
103-
104- // MARK: - Double
105-
106- public func double( at index: Int32 ) -> Double {
107- sqlite3_column_double ( stmt, index)
108- }
109-
110- public func double( for name: String ) -> Double ? {
111- columnIndexByName [ name] . map { double ( at: $0) }
112- }
113-
114- // MARK: - Blob
115-
116- public func blob( at index: Int32 ) -> Data ? {
117- sqlite3_column_blob ( stmt, index) . map { bytes in
118- Data ( bytes: bytes, count: Int ( sqlite3_column_bytes ( stmt, index) ) )
119- }
120- }
121-
122- // MARK: - Null
123-
124- public func null( at index: Int32 ) -> Bool {
125- sqlite3_column_type ( stmt, index) == SQLITE_NULL
126- }
127-
128- public func null( for name: String ) -> Bool {
129- columnIndexByName [ name] . map { null ( at: $0) } ?? true
130- }
13182}
13283
13384// MARK: - Retrieving Statement SQL
@@ -168,61 +119,41 @@ extension PreparedStatement {
168119 }
169120}
170121
171- // MARK: - Bind SQL Parameters
122+ // MARK: - Binding values
172123
173124private let SQLITE_TRANSIENT = unsafeBitCast ( - 1 , to: sqlite3_destructor_type. self)
174125
175126extension PreparedStatement {
176127 @discardableResult
177- public func bind( name: String , _ parameter: SQLParameter ? ) throws -> PreparedStatement {
178- try bind ( index: parameterIndex ( for: name) , parameter)
128+ public func bind( name: String , parameter: SQLParameter ) throws -> PreparedStatement {
129+ try bind ( index: parameterIndex ( for: name) , parameter: parameter )
179130 }
180131
181132 @discardableResult
182- public func bind( index: Int32 , _ parameter: SQLParameter ? ) throws -> PreparedStatement {
183- var code = SQLITE_OK
184- switch parameter {
185- case . none:
186- code = sqlite3_bind_null ( stmt, index)
187- case . int64( let number) ? :
188- code = sqlite3_bind_int64 ( stmt, index, number)
189- case . double( let double) ? :
190- code = sqlite3_bind_double ( stmt, index, double)
191- case . text( let string) ? :
192- code = sqlite3_bind_text ( stmt, index, string, - 1 , SQLITE_TRANSIENT)
193- case . blob( let data) ? :
194- code = data. withUnsafeBytes { ptr in
195- sqlite3_bind_blob ( stmt, index, ptr. baseAddress, Int32 ( data. count) , SQLITE_TRANSIENT)
196- }
133+ public func bind( parameters: SQLParameter ... ) throws -> PreparedStatement {
134+ for (index, parameter) in parameters. enumerated ( ) {
135+ try bind ( index: Int32 ( index + 1 ) , parameter: parameter)
197136 }
198- return try check ( code)
199- }
200-
201- @discardableResult
202- public func bind( index: Int32 , int64: Int64 ) throws -> PreparedStatement {
203- try check ( sqlite3_bind_int64 ( stmt, index, int64) )
137+ return self
204138 }
205139
206140 @discardableResult
207- public func bind( index: Int32 , double: Double ) throws -> PreparedStatement {
208- try check ( sqlite3_bind_double ( stmt, index, double) )
209- }
210-
211- @discardableResult
212- public func bind( name: String , string: String ) throws -> PreparedStatement {
213- try bind ( index: parameterIndex ( for: name) , string: string)
214- }
215-
216- @discardableResult
217- public func bind( index: Int32 , string: String ) throws -> PreparedStatement {
218- try check ( sqlite3_bind_text ( stmt, index, string, - 1 , SQLITE_TRANSIENT) )
219- }
220-
221- @discardableResult
222- public func bind( index: Int32 , data: Data ) throws -> PreparedStatement {
223- let code = data. withUnsafeBytes { ptr in
224- sqlite3_bind_blob ( stmt, index, ptr. baseAddress, Int32 ( data. count) , SQLITE_TRANSIENT)
225- }
141+ public func bind( index: Int32 , parameter: SQLParameter ) throws -> PreparedStatement {
142+ let code =
143+ switch parameter {
144+ case . null:
145+ sqlite3_bind_null ( stmt, index)
146+ case . int64( let number) :
147+ sqlite3_bind_int64 ( stmt, index, number)
148+ case . double( let double) :
149+ sqlite3_bind_double ( stmt, index, double)
150+ case . text( let string) :
151+ sqlite3_bind_text ( stmt, index, string, - 1 , SQLITE_TRANSIENT)
152+ case . blob( let data) :
153+ data. withUnsafeBytes { ptr in
154+ sqlite3_bind_blob ( stmt, index, ptr. baseAddress, Int32 ( data. count) , SQLITE_TRANSIENT)
155+ }
156+ }
226157 return try check ( code)
227158 }
228159}
0 commit comments