SqliteReservationDao.swift
99 lignes · 3382 octets
import Foundation struct SqliteReservationDao: ReservationDao { let db: SqliteDatabase private static let columns = """ localId, serverId, cibleType, cibleId, debutMs, finMs, motif, statut, \ updatedAt, calendarEventId, dirtyLocal, conflictPending """ private static func map(_ row: SqliteRow) -> ReservationEntity { var e = ReservationEntity() e.localId = row.int64(0) e.serverId = row.textOrNil(1) e.cibleType = row.text(2) e.cibleId = row.text(3) e.debutMs = row.int64(4) e.finMs = row.int64(5) e.motif = row.text(6) e.statut = row.text(7) e.updatedAt = row.int64(8) e.calendarEventId = row.int64OrNil(9) e.dirtyLocal = row.bool(10) e.conflictPending = row.bool(11) return e } @discardableResult func upsert(_ entity: ReservationEntity) async throws -> Int64 { try await db.insert( "INSERT OR REPLACE INTO reservations (\(Self.columns)) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ .rowId(entity.localId), .optText(entity.serverId), .text(entity.cibleType), .text(entity.cibleId), .int(entity.debutMs), .int(entity.finMs), .text(entity.motif), .text(entity.statut), .int(entity.updatedAt), .optInt(entity.calendarEventId), .bool(entity.dirtyLocal), .bool(entity.conflictPending), ] ) } func getByServerId(_ serverId: String) async throws -> ReservationEntity? { try await db.query( "SELECT \(Self.columns) FROM reservations WHERE serverId = ?", [.text(serverId)], map: Self.map ).first } func getByLocalId(_ localId: Int64) async throws -> ReservationEntity? { try await db.query( "SELECT \(Self.columns) FROM reservations WHERE localId = ?", [.int(localId)], map: Self.map ).first } func getByCalendarEventId(_ calendarEventId: Int64) async throws -> ReservationEntity? { try await db.query( "SELECT \(Self.columns) FROM reservations WHERE calendarEventId = ?", [.int(calendarEventId)], map: Self.map ).first } func listDirty() async throws -> [ReservationEntity] { try await db.query( "SELECT \(Self.columns) FROM reservations WHERE dirtyLocal = 1", map: Self.map ) } func deleteByServerId(_ serverId: String) async throws { try await db.write("DELETE FROM reservations WHERE serverId = ?", [.text(serverId)]) } func deleteByLocalId(_ localId: Int64) async throws { try await db.write("DELETE FROM reservations WHERE localId = ?", [.int(localId)]) } func listByCible(cibleType: String, cibleId: String) async throws -> [ReservationEntity] { try await db.query( "SELECT \(Self.columns) FROM reservations WHERE cibleType = ? AND cibleId = ?", [.text(cibleType), .text(cibleId)], map: Self.map ) } func listAll() async throws -> [ReservationEntity] { try await db.query("SELECT \(Self.columns) FROM reservations", map: Self.map) } }
GitRust