File tree 3 files changed +22
-8
lines changed 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -269,13 +269,16 @@ public final class SQLStatement {
269
269
270
270
/// Returns the index of a column given its name.
271
271
public func columnIndex( forName name: String ) -> Int ? {
272
- return columnIndices [ name]
272
+ return columnIndices [ name. lowercased ( ) ]
273
273
}
274
- private lazy var columnIndices : [ String : Int ] = {
274
+
275
+ /// Holds each column index key-ed by its name.
276
+ /// Initialized for all columns as soon as it's first accessed.
277
+ public private( set) lazy var columnIndices : [ String : Int ] = {
275
278
var indices : [ String : Int ] = [ : ]
276
279
indices. reserveCapacity ( columnCount)
277
280
for index in 0 ..< columnCount {
278
- indices [ String ( cString : sqlite3_column_name ( ref , Int32 ( index) ) ) ] = index
281
+ indices [ columnName ( at : index) . lowercased ( ) ] = index
279
282
}
280
283
return indices
281
284
} ( )
Original file line number Diff line number Diff line change @@ -59,9 +59,7 @@ public struct SQLRow {
59
59
values = ( 0 ..< statement. columnCount) . map { index in
60
60
statement. column ( at: index)
61
61
}
62
- columnIndicesByNames = Dictionary ( uniqueKeysWithValues: ( 0 ..< statement. columnCount) . map { index in
63
- ( statement. columnName ( at: index) , index)
64
- } )
62
+ columnIndicesByNames = statement. columnIndices
65
63
}
66
64
67
65
/// Returns a single column of the current result row of a query.
@@ -96,7 +94,7 @@ public struct SQLRow {
96
94
///
97
95
/// - parameter columnName: The name of the column.
98
96
public subscript< T: InitializableBySQLColumnValue > ( columnName: String ) -> T {
99
- guard let columnIndex = columnIndicesByNames [ columnName] else {
97
+ guard let columnIndex = columnIndicesByNames [ columnName. lowercased ( ) ] else {
100
98
fatalError ( " No such column \( columnName) " )
101
99
}
102
100
return self [ columnIndex]
@@ -109,7 +107,7 @@ public struct SQLRow {
109
107
///
110
108
/// - parameter columnName: The name of the column.
111
109
public subscript< T: InitializableBySQLColumnValue > ( columnName: String ) -> T ? {
112
- guard let columnIndex = columnIndicesByNames [ columnName] else {
110
+ guard let columnIndex = columnIndicesByNames [ columnName. lowercased ( ) ] else {
113
111
return nil
114
112
}
115
113
return self [ columnIndex]
Original file line number Diff line number Diff line change @@ -163,6 +163,19 @@ final class SwiftSQLExtTests: XCTestCase {
163
163
XCTAssertEqual ( try XCTUnwrap ( int64) , 1 )
164
164
XCTAssertEqual ( try XCTUnwrap ( double) , 1 )
165
165
}
166
+
167
+ func testCaseInsensitiveColumnNameSubscripts( ) throws {
168
+ // GIVEN
169
+ try db. populateStore ( )
170
+
171
+ // WHEN
172
+ let row = try db
173
+ . prepare ( " SELECT Name FROM Persons ORDER BY Level ASC " )
174
+ . row ( )
175
+
176
+ // THEN
177
+ XCTAssertEqual ( try XCTUnwrap ( row) [ " name " ] as String , " Alice " )
178
+ }
166
179
}
167
180
168
181
private extension SQLConnection {
You can’t perform that action at this time.
0 commit comments