Skip to content

Commit 593a749

Browse files
authored
Merge pull request #1115 from justinmeiners/get-column-performance
Improve performance of prefixed column resolution
2 parents b08d211 + c659dc9 commit 593a749

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Sources/SQLite/Typed/Query.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,16 +1169,25 @@ public struct Row {
11691169
}
11701170

11711171
guard let idx = columnNames[column.template] else {
1172-
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }
1172+
func similar(_ name: String) -> Bool {
1173+
return name.hasSuffix(".\(column.template)")
1174+
}
11731175

1174-
switch similar.count {
1175-
case 0:
1176+
guard let firstIndex = columnNames.firstIndex(where: { similar($0.key) }) else {
11761177
throw QueryError.noSuchColumn(name: column.template, columns: columnNames.keys.sorted())
1177-
case 1:
1178-
return valueAtIndex(columnNames[similar[0]]!)
1179-
default:
1180-
throw QueryError.ambiguousColumn(name: column.template, similar: similar)
11811178
}
1179+
1180+
let secondIndex = columnNames
1181+
.suffix(from: columnNames.index(after: firstIndex))
1182+
.firstIndex(where: { similar($0.key) })
1183+
1184+
guard secondIndex == nil else {
1185+
throw QueryError.ambiguousColumn(
1186+
name: column.template,
1187+
similar: columnNames.keys.filter(similar).sorted()
1188+
)
1189+
}
1190+
return valueAtIndex(columnNames[firstIndex].value)
11821191
}
11831192

11841193
return valueAtIndex(idx)

0 commit comments

Comments
 (0)