|
| 1 | +import Foundation |
| 2 | +import Otter |
| 3 | + |
| 4 | +struct User: Hashable, Sendable, Identifiable, RowDecodable { |
| 5 | + let id: Int |
| 6 | + let firstName: String |
| 7 | + let lastName: String |
| 8 | + let fullName: String |
| 9 | + |
| 10 | + init( |
| 11 | + row: borrowing Otter.Row, |
| 12 | + startingAt start: Int32 |
| 13 | + ) throws(Otter.OtterError) { |
| 14 | + self.id = try row.value(at: start + 0) |
| 15 | + self.firstName = try row.value(at: start + 1) |
| 16 | + self.lastName = try row.value(at: start + 2) |
| 17 | + self.fullName = try row.value(at: start + 3) |
| 18 | + } |
| 19 | + |
| 20 | + init( |
| 21 | + id: Int, |
| 22 | + firstName: String, |
| 23 | + lastName: String, |
| 24 | + fullName: String |
| 25 | + ) { |
| 26 | + self.id = id |
| 27 | + self.firstName = firstName |
| 28 | + self.lastName = lastName |
| 29 | + self.fullName = fullName |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +struct SelectUserWithManyInputsInput: Hashable, Sendable, Identifiable { |
| 34 | + let id: Int |
| 35 | + let firstName: String |
| 36 | +} |
| 37 | + |
| 38 | +@dynamicMemberLookup |
| 39 | +struct SelectUserWithManyInputsOutput: Hashable, Sendable, RowDecodable { |
| 40 | + let user: User |
| 41 | + let favoriteNumber: Int |
| 42 | + |
| 43 | + init( |
| 44 | + row: borrowing Otter.Row, |
| 45 | + startingAt start: Int32 |
| 46 | + ) throws(Otter.OtterError) { |
| 47 | + self.user = try User(row: row, startingAt: start + 0) |
| 48 | + self.favoriteNumber = try row.value(at: start + 4) |
| 49 | + } |
| 50 | + |
| 51 | + init( |
| 52 | + user: User, |
| 53 | + favoriteNumber: Int |
| 54 | + ) { |
| 55 | + self.user = user |
| 56 | + self.favoriteNumber = favoriteNumber |
| 57 | + } |
| 58 | + |
| 59 | + subscript<Value>(dynamicMember dynamicMember: KeyPath<User, Value>) -> Value { |
| 60 | + self.user[keyPath: dynamicMember] |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +protocol QueriesQueries { |
| 65 | + associatedtype SelectUsers: SelectUsersQuery |
| 66 | + var selectUsers: SelectUsers { get } |
| 67 | + associatedtype SelectUserById: SelectUserByIdQuery |
| 68 | + var selectUserById: SelectUserById { get } |
| 69 | + associatedtype SelectUserByIds: SelectUserByIdsQuery |
| 70 | + var selectUserByIds: SelectUserByIds { get } |
| 71 | + associatedtype SelectUserByName: SelectUserByNameQuery |
| 72 | + var selectUserByName: SelectUserByName { get } |
| 73 | + associatedtype SelectUserWithManyInputs: SelectUserWithManyInputsQuery |
| 74 | + var selectUserWithManyInputs: SelectUserWithManyInputs { get } |
| 75 | +} |
| 76 | + |
| 77 | +struct QueriesQueriesNoop: QueriesQueries { |
| 78 | + let selectUsers: AnyQuery<(), [User]> |
| 79 | + let selectUserById: AnyQuery<Int, User?> |
| 80 | + let selectUserByIds: AnyQuery<[Int], [User]> |
| 81 | + let selectUserByName: AnyQuery<String, [User]> |
| 82 | + let selectUserWithManyInputs: AnyQuery<SelectUserWithManyInputsInput, SelectUserWithManyInputsOutput?> |
| 83 | + |
| 84 | + init( |
| 85 | + selectUsers: any SelectUsersQuery = Queries.Just(), |
| 86 | + selectUserById: any SelectUserByIdQuery = Queries.Just(), |
| 87 | + selectUserByIds: any SelectUserByIdsQuery = Queries.Just(), |
| 88 | + selectUserByName: any SelectUserByNameQuery = Queries.Just(), |
| 89 | + selectUserWithManyInputs: any SelectUserWithManyInputsQuery = Queries.Just() |
| 90 | + ) { |
| 91 | + self.selectUsers = selectUsers.eraseToAnyQuery() |
| 92 | + self.selectUserById = selectUserById.eraseToAnyQuery() |
| 93 | + self.selectUserByIds = selectUserByIds.eraseToAnyQuery() |
| 94 | + self.selectUserByName = selectUserByName.eraseToAnyQuery() |
| 95 | + self.selectUserWithManyInputs = selectUserWithManyInputs.eraseToAnyQuery() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +struct QueriesQueriesImpl: QueriesQueries { |
| 100 | + let connection: any Connection |
| 101 | + |
| 102 | + var selectUsers: AnyDatabaseQuery<(), [User]> { |
| 103 | + AnyDatabaseQuery<(), [User]>( |
| 104 | + .read, |
| 105 | + in: connection, |
| 106 | + watchingTables: ["user"] |
| 107 | + ) { input, tx in |
| 108 | + let statement = try Otter.Statement( |
| 109 | + """ |
| 110 | + SELECT * FROM user |
| 111 | + """, |
| 112 | + transaction: tx |
| 113 | + ) |
| 114 | + return try statement.fetchAll() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + var selectUserById: AnyDatabaseQuery<Int, User?> { |
| 119 | + AnyDatabaseQuery<Int, User?>( |
| 120 | + .read, |
| 121 | + in: connection, |
| 122 | + watchingTables: ["user"] |
| 123 | + ) { input, tx in |
| 124 | + var statement = try Otter.Statement( |
| 125 | + """ |
| 126 | + SELECT * FROM user WHERE id = ? |
| 127 | + """, |
| 128 | + transaction: tx |
| 129 | + ) |
| 130 | + try statement.bind(value: input) |
| 131 | + return try statement.fetchOne() |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + var selectUserByIds: AnyDatabaseQuery<[Int], [User]> { |
| 136 | + AnyDatabaseQuery<[Int], [User]>( |
| 137 | + .read, |
| 138 | + in: connection, |
| 139 | + watchingTables: ["user"] |
| 140 | + ) { input, tx in |
| 141 | + var statement = try Otter.Statement( |
| 142 | + """ |
| 143 | + SELECT * FROM user WHERE id IN (\(input.sqlQuestionMarks)) |
| 144 | + """, |
| 145 | + transaction: tx |
| 146 | + ) |
| 147 | + for element in input { |
| 148 | + try statement.bind(value: element) |
| 149 | + } |
| 150 | + return try statement.fetchAll() |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + var selectUserByName: AnyDatabaseQuery<String, [User]> { |
| 155 | + AnyDatabaseQuery<String, [User]>( |
| 156 | + .read, |
| 157 | + in: connection, |
| 158 | + watchingTables: ["user"] |
| 159 | + ) { input, tx in |
| 160 | + var statement = try Otter.Statement( |
| 161 | + """ |
| 162 | + SELECT * FROM user WHERE fullName LIKE ? |
| 163 | + """, |
| 164 | + transaction: tx |
| 165 | + ) |
| 166 | + try statement.bind(value: input) |
| 167 | + return try statement.fetchAll() |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + var selectUserWithManyInputs: AnyDatabaseQuery<SelectUserWithManyInputsInput, SelectUserWithManyInputsOutput?> { |
| 172 | + AnyDatabaseQuery<SelectUserWithManyInputsInput, SelectUserWithManyInputsOutput?>( |
| 173 | + .read, |
| 174 | + in: connection, |
| 175 | + watchingTables: ["user"] |
| 176 | + ) { input, tx in |
| 177 | + var statement = try Otter.Statement( |
| 178 | + """ |
| 179 | + SELECT *, 1 AS favoriteNumber FROM user WHERE id = ? AND firstName = ? |
| 180 | + """, |
| 181 | + transaction: tx |
| 182 | + ) |
| 183 | + try statement.bind(value: input.id) |
| 184 | + try statement.bind(value: input.firstName) |
| 185 | + return try statement.fetchOne() |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +struct DB: Database{ |
| 191 | + let connection: any Otter.Connection |
| 192 | + |
| 193 | + static var migrations: [String] { |
| 194 | + return [ |
| 195 | + """ |
| 196 | + CREATE TABLE user ( |
| 197 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 198 | + firstName TEXT NOT NULL, |
| 199 | + lastName TEXT NOT NULL, |
| 200 | + fullName TEXT NOT NULL GENERATED ALWAYS AS (firstName || ' ' || lastName) |
| 201 | + );; |
| 202 | + """ |
| 203 | + ] |
| 204 | + } |
| 205 | + var queriesQueries: QueriesQueriesImpl { |
| 206 | + QueriesQueriesImpl(connection: connection) |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +typealias SelectUsersQuery = Query<(), [User]> |
| 211 | +typealias SelectUserByIdQuery = Query<Int, User?> |
| 212 | +typealias SelectUserByIdsQuery = Query<[Int], [User]> |
| 213 | +typealias SelectUserByNameQuery = Query<String, [User]> |
| 214 | +typealias SelectUserWithManyInputsQuery = Query<SelectUserWithManyInputsInput, SelectUserWithManyInputsOutput?> |
| 215 | +extension Query where Input == SelectUserWithManyInputsInput {func execute(id: Int, firstName: String) async throws -> Output { |
| 216 | + try await execute(with: SelectUserWithManyInputsInput(id: id, firstName: firstName)) |
| 217 | + } |
| 218 | +} |
0 commit comments