Skip to content

Commit cc071c7

Browse files
committed
added select for three columns
1 parent 8dfc25f commit cc071c7

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Sources/SQLiteORM/Storage/Storage+nonCRUD.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,27 @@ extension Storage {
2222
} while resultCode != self.apiProvider.SQLITE_DONE
2323
}
2424

25-
public func select<R1, R2> (_ expression1: Expression, _ expression2: Expression, _ constraints: SelectConstraint...) throws -> [(R1, R2)] where R1: ConstructableFromSQLiteValue, R2: ConstructableFromSQLiteValue {
25+
public func select<R1, R2, R3>(_ expression1: Expression, _ expression2: Expression, _ expression3: Expression, _ constraints: SelectConstraint...) throws -> [(R1, R2, R3)] where R1: ConstructableFromSQLiteValue, R2: ConstructableFromSQLiteValue, R3: ConstructableFromSQLiteValue {
26+
let serializationContext = SerializationContext(schemaProvider: self)
27+
let columnText1 = try expression1.serialize(with: serializationContext)
28+
let columnText2 = try expression2.serialize(with: serializationContext)
29+
let columnText3 = try expression3.serialize(with: serializationContext)
30+
var sql = "SELECT \(columnText1), \(columnText2), \(columnText3)"
31+
for constraint in constraints {
32+
let constraintsString = try constraint.serialize(with: serializationContext)
33+
sql += " \(constraintsString)"
34+
}
35+
let connectionRef = try ConnectionRef(connection: self.connection)
36+
var result = [(R1, R2, R3)]()
37+
try self.selectInternal(sql, connectionRef: connectionRef, columnsCount: 3, append: { statement in
38+
result.append((R1(sqliteValue: statement.columnValuePointer(with: 0)),
39+
R2(sqliteValue: statement.columnValuePointer(with: 1)),
40+
R3(sqliteValue: statement.columnValuePointer(with: 2))))
41+
})
42+
return result
43+
}
44+
45+
public func select<R1, R2>(_ expression1: Expression, _ expression2: Expression, _ constraints: SelectConstraint...) throws -> [(R1, R2)] where R1: ConstructableFromSQLiteValue, R2: ConstructableFromSQLiteValue {
2646
let serializationContext = SerializationContext(schemaProvider: self)
2747
let columnText1 = try expression1.serialize(with: serializationContext)
2848
let columnText2 = try expression2.serialize(with: serializationContext)

Tests/SQLiteORMTests/StorageTests/StorageNonCrudTests.swift

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class StorageNonCrudTests: XCTestCase {
4646
try storage.replace(Employee(id: 6, firstname: "Michael", lastname: "Mitchell", title: "IT Manager", email: "[email protected]"))
4747
try storage.replace(Employee(id: 7, firstname: "Robert", lastname: "King", title: "IT Staff", email: "[email protected]"))
4848
try storage.replace(Employee(id: 8, firstname: "Laura", lastname: "Callahan", title: "IT Staff", email: "[email protected]"))
49-
49+
5050
apiProvider.resetCalls()
5151
try section("one column", routine: {
5252
let ids: [Int] = try storage.select(\Employee.id, from(Employee.self))
@@ -139,6 +139,68 @@ class StorageNonCrudTests: XCTestCase {
139139
.init(id: 37, callType: .sqlite3Close(.ignore))
140140
])
141141
})
142+
try section("three columns", routine: {
143+
let rows: [(Int, String, String)] = try storage.select(\Employee.id, \Employee.firstname, \Employee.lastname, from(Employee.self))
144+
let expected: [(Int, String, String)] = [
145+
(1, "Andrew", "Adams"),
146+
(2, "Nancy", "Edwards"),
147+
(3, "Jane", "Peacock"),
148+
(4, "Margaret", "Park"),
149+
(5, "Steve", "Johnson"),
150+
(6, "Michael", "Mitchell"),
151+
(7, "Robert", "King"),
152+
(8, "Laura", "Callahan"),
153+
]
154+
XCTAssert(compareUnordered(rows, expected, { $0.0 == $1.0 && $0.1 == $1.1 && $0.2 == $1.2 }))
155+
XCTAssertEqual(apiProvider.calls, [
156+
.init(id: 0, callType: .sqlite3Open(filename, .ignore)),
157+
.init(id: 1, callType: .sqlite3PrepareV2(.ignore, "SELECT employees.\"id\", employees.\"firstname\", employees.\"lastname\" FROM employees", -1, .ignore, nil)),
158+
.init(id: 2, callType: .sqlite3Step(.ignore)),
159+
.init(id: 3, callType: .sqlite3ColumnCount(.ignore)),
160+
.init(id: 4, callType: .sqlite3ColumnInt(.ignore, 0)),
161+
.init(id: 5, callType: .sqlite3ColumnText(.ignore, 1)),
162+
.init(id: 6, callType: .sqlite3ColumnText(.ignore, 2)),
163+
.init(id: 7, callType: .sqlite3Step(.ignore)),
164+
.init(id: 8, callType: .sqlite3ColumnCount(.ignore)),
165+
.init(id: 9, callType: .sqlite3ColumnInt(.ignore, 0)),
166+
.init(id: 10, callType: .sqlite3ColumnText(.ignore, 1)),
167+
.init(id: 11, callType: .sqlite3ColumnText(.ignore, 2)),
168+
.init(id: 12, callType: .sqlite3Step(.ignore)),
169+
.init(id: 13, callType: .sqlite3ColumnCount(.ignore)),
170+
.init(id: 14, callType: .sqlite3ColumnInt(.ignore, 0)),
171+
.init(id: 15, callType: .sqlite3ColumnText(.ignore, 1)),
172+
.init(id: 16, callType: .sqlite3ColumnText(.ignore, 2)),
173+
.init(id: 17, callType: .sqlite3Step(.ignore)),
174+
.init(id: 18, callType: .sqlite3ColumnCount(.ignore)),
175+
.init(id: 19, callType: .sqlite3ColumnInt(.ignore, 0)),
176+
.init(id: 20, callType: .sqlite3ColumnText(.ignore, 1)),
177+
.init(id: 21, callType: .sqlite3ColumnText(.ignore, 2)),
178+
.init(id: 22, callType: .sqlite3Step(.ignore)),
179+
.init(id: 23, callType: .sqlite3ColumnCount(.ignore)),
180+
.init(id: 24, callType: .sqlite3ColumnInt(.ignore, 0)),
181+
.init(id: 25, callType: .sqlite3ColumnText(.ignore, 1)),
182+
.init(id: 26, callType: .sqlite3ColumnText(.ignore, 2)),
183+
.init(id: 27, callType: .sqlite3Step(.ignore)),
184+
.init(id: 28, callType: .sqlite3ColumnCount(.ignore)),
185+
.init(id: 29, callType: .sqlite3ColumnInt(.ignore, 0)),
186+
.init(id: 30, callType: .sqlite3ColumnText(.ignore, 1)),
187+
.init(id: 31, callType: .sqlite3ColumnText(.ignore, 2)),
188+
.init(id: 32, callType: .sqlite3Step(.ignore)),
189+
.init(id: 33, callType: .sqlite3ColumnCount(.ignore)),
190+
.init(id: 34, callType: .sqlite3ColumnInt(.ignore, 0)),
191+
.init(id: 35, callType: .sqlite3ColumnText(.ignore, 1)),
192+
.init(id: 36, callType: .sqlite3ColumnText(.ignore, 2)),
193+
.init(id: 37, callType: .sqlite3Step(.ignore)),
194+
.init(id: 38, callType: .sqlite3ColumnCount(.ignore)),
195+
.init(id: 39, callType: .sqlite3ColumnInt(.ignore, 0)),
196+
.init(id: 40, callType: .sqlite3ColumnText(.ignore, 1)),
197+
.init(id: 41, callType: .sqlite3ColumnText(.ignore, 2)),
198+
.init(id: 42, callType: .sqlite3Step(.ignore)),
199+
.init(id: 43, callType: .sqlite3ColumnCount(.ignore)),
200+
.init(id: 44, callType: .sqlite3Finalize(.ignore)),
201+
.init(id: 45, callType: .sqlite3Close(.ignore)),
202+
])
203+
})
142204
})
143205
}
144206

0 commit comments

Comments
 (0)