Skip to content

Commit 4844d8d

Browse files
authored
Merge pull request #9 from ahmedk92/master
case-insensitive column names for SQLStatement and SQLRow
2 parents 24f0738 + 4b3d553 commit 4844d8d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Sources/SwiftSQL/SQLStatement.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,16 @@ public final class SQLStatement {
269269

270270
/// Returns the index of a column given its name.
271271
public func columnIndex(forName name: String) -> Int? {
272-
return columnIndices[name]
272+
return columnIndices[name.lowercased()]
273273
}
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] = {
275278
var indices: [String : Int] = [:]
276279
indices.reserveCapacity(columnCount)
277280
for index in 0..<columnCount {
278-
indices[String(cString: sqlite3_column_name(ref, Int32(index)))] = index
281+
indices[columnName(at: index).lowercased()] = index
279282
}
280283
return indices
281284
}()

Sources/SwiftSQLExt/SwiftSQLExt.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public struct SQLRow {
5959
values = (0..<statement.columnCount).map { index in
6060
statement.column(at: index)
6161
}
62-
columnIndicesByNames = Dictionary(uniqueKeysWithValues: (0..<statement.columnCount).map { index in
63-
(statement.columnName(at: index), index)
64-
})
62+
columnIndicesByNames = statement.columnIndices
6563
}
6664

6765
/// Returns a single column of the current result row of a query.
@@ -96,7 +94,7 @@ public struct SQLRow {
9694
///
9795
/// - parameter columnName: The name of the column.
9896
public subscript<T: InitializableBySQLColumnValue>(columnName: String) -> T {
99-
guard let columnIndex = columnIndicesByNames[columnName] else {
97+
guard let columnIndex = columnIndicesByNames[columnName.lowercased()] else {
10098
fatalError("No such column \(columnName)")
10199
}
102100
return self[columnIndex]
@@ -109,7 +107,7 @@ public struct SQLRow {
109107
///
110108
/// - parameter columnName: The name of the column.
111109
public subscript<T: InitializableBySQLColumnValue>(columnName: String) -> T? {
112-
guard let columnIndex = columnIndicesByNames[columnName] else {
110+
guard let columnIndex = columnIndicesByNames[columnName.lowercased()] else {
113111
return nil
114112
}
115113
return self[columnIndex]

Tests/SwiftSQLExtTests/SwiftSQLExtTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ final class SwiftSQLExtTests: XCTestCase {
163163
XCTAssertEqual(try XCTUnwrap(int64), 1)
164164
XCTAssertEqual(try XCTUnwrap(double), 1)
165165
}
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+
}
166179
}
167180

168181
private extension SQLConnection {

0 commit comments

Comments
 (0)