SqliteTacheDao.swift 105 lignes · 3694 octets
import Foundation

struct SqliteTacheDao: TacheDao {
    let db: SqliteDatabase

    private static let columns = """
    localId, serverId, projetLocalId, projetServerId, titre, description, colonneId, \
    ordre, assigneA, debut, dureeJours, etiquette, parentId, dependDeJson, sousTachesJson, \
    auteur, createdAt, updatedAt
    """

    private static func map(_ row: SqliteRow) -> TacheEntity {
        var e = TacheEntity()
        e.localId = row.int64(0)
        e.serverId = row.textOrNil(1)
        e.projetLocalId = row.int64OrNil(2)
        e.projetServerId = row.text(3)
        e.titre = row.text(4)
        e.description = row.text(5)
        e.colonneId = row.text(6)
        e.ordre = row.int(7)
        e.assigneA = row.textOrNil(8)
        e.debut = row.textOrNil(9)
        e.dureeJours = row.int64OrNil(10).map(Int.init)
        e.etiquette = row.textOrNil(11)
        e.parentId = row.textOrNil(12)
        e.dependDeJson = row.text(13)
        e.sousTachesJson = row.text(14)
        e.auteur = row.text(15)
        e.createdAt = row.int64(16)
        e.updatedAt = row.int64(17)
        return e
    }

    @discardableResult
    func upsert(_ entity: TacheEntity) async throws -> Int64 {
        try await db.insert(
            "INSERT OR REPLACE INTO taches (\(Self.columns)) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            [
                .rowId(entity.localId),
                .optText(entity.serverId),
                .optInt(entity.projetLocalId),
                .text(entity.projetServerId),
                .text(entity.titre),
                .text(entity.description),
                .text(entity.colonneId),
                .int(Int64(entity.ordre)),
                .optText(entity.assigneA),
                .optText(entity.debut),
                .optInt(entity.dureeJours.map(Int64.init)),
                .optText(entity.etiquette),
                .optText(entity.parentId),
                .text(entity.dependDeJson),
                .text(entity.sousTachesJson),
                .text(entity.auteur),
                .int(entity.createdAt),
                .int(entity.updatedAt),
            ]
        )
    }

    func getByServerId(_ serverId: String) async throws -> TacheEntity? {
        try await db.query(
            "SELECT \(Self.columns) FROM taches WHERE serverId = ?",
            [.text(serverId)],
            map: Self.map
        ).first
    }

    func getByLocalId(_ localId: Int64) async throws -> TacheEntity? {
        try await db.query(
            "SELECT \(Self.columns) FROM taches WHERE localId = ?",
            [.int(localId)],
            map: Self.map
        ).first
    }

    func deleteByServerId(_ serverId: String) async throws {
        try await db.write("DELETE FROM taches WHERE serverId = ?", [.text(serverId)])
    }

    func deleteByLocalId(_ localId: Int64) async throws {
        try await db.write("DELETE FROM taches WHERE localId = ?", [.int(localId)])
    }

    func listByProjetServerId(_ projetServerId: String) async throws -> [TacheEntity] {
        try await db.query(
            "SELECT \(Self.columns) FROM taches WHERE projetServerId = ?",
            [.text(projetServerId)],
            map: Self.map
        )
    }

    func fetchByProjetServerId(_ projetServerId: String) async throws -> [TacheEntity] {
        try await db.query(
            "SELECT \(Self.columns) FROM taches WHERE projetServerId = ? ORDER BY ordre ASC",
            [.text(projetServerId)],
            map: Self.map
        )
    }

    func listAll() async throws -> [TacheEntity] {
        try await db.query("SELECT \(Self.columns) FROM taches", map: Self.map)
    }
}