使用 GRDB,使用原始 sql 但以方便的形式使用结果?

With GRDB, use raw sql BUT consume the results in the convenience form?

这是一个示例 SQL 您无法使用 GRDB 中的便捷构建器构建的查询...

let q = "job.id, job.name, job.city, 

ifnull(jobcategory.value, 'no category'),
ifnull(jobpriority.value, 'no priority'),

from job  

left join jobcategory on job.category = jobcategory.id
left join jobpriority on job.priority = jobpriority.id

where job.user = 13"

(事实上,我举了一个例子,你不能在任何旧的、支持较少的库中构建 iOS Swift SQL 库)

然后是

for ..
 let id = ..
 let name = ..
 let city = ..
 let category = ..
 let priority = ..

我听说对于 GRDB,您实际上可以使用原始的 SQL(即实际上什至不使用 GRDB 的查询构建器),但是,结果 仍然使用便利消费*,兼顾品类等

如果是这样,实际上你是如何在例子中做到这一点的?

GRDB 提供查询生成器:

let persons = try Person.filter(emailColumn != nil).fetchAll(db) // [Person]

它也理解SQL:

let persons = try Person.fetchAll(db, "SELECT * FROM persons WHERE email IS NOT NULL")

上面的两个代码片段都将数据库行转换为 Person 实例。 RowConvertible 协议和 GRDB 提供的全功能 Record class 支持这种转换。下面的代码使用协议:

struct Person {
    let email: String
    let name: String
}

extension Person : RowConvertible {
    init(row: Row) {
        email = row.value(named: "email")
        name = row.value(named: "name")
    }
}

init(row:) 构造函数用于 "query interface" 请求 Person.filter(...).fetchAll(db) 和 SQL 请求 Person.fetchAll(db, "SELECT ...").

这就是当你想使用raw时GRDB不会惩罚你的意思SQL。您的自定义记录类型支持开箱即用的查询接口请求和 SQL 请求。使用这两种技术获取记录同样容易:

// Two one-liners:
let persons = try Person.filter(emailColumn != nil).fetchAll(db)
let persons = try Person.fetchAll(db, "SELECT * FROM persons WHERE email IS NOT NULL")

现在,你的例子可以写成:

struct Job {
    let id: Int64
    let name: String
    let city: String
    let category: String
    let priority: String
}

extension Job : RowConvertible {
    init(row: Row) {
        id = row.value(named: "id")
        name = row.value(named: "name")
        city = row.value(named: "city")
        category = row.value(named: "category")
        priority = row.value(named: "priority")
    }
}

try dbQueue.inDatabase { db in
    let q = "SELECT job.id, job.name, job.city, " +
        " IFNULL(jobcategory.value, 'no category') AS category, " +
        " IFNULL(jobpriority.value, 'no priority') AS priority " +
        "FROM job " +
        "LEFT JOIN jobcategory ON job.category = jobcategory.id " +
        "LEFT JOIN jobpriority ON job.priority = jobpriority.id " +
        "WHERE job.user = 13"

    let jobs = try Job.fetchAll(db, q)
}

由于类别和优先级不是作业的列,您可能更愿意将上述结构一分为二:

struct Job {
    let id: Int64
    let name: String
    let city: String
}

struct ExtendedJob {
    let job: Job
    let category: String
    let priority: String
}

extension Job : RowConvertible {
    init(row: Row) {
        id = row.value(named: "id")
        name = row.value(named: "name")
        city = row.value(named: "city")
    }
}

extension ExtendedJob : RowConvertible {
    init(row: Row) {
        job = Job(row: row)
        category = row.value(named: "category")
        priority = row.value(named: "priority")
    }
}

try dbQueue.inDatabase { db in
    let q = "SELECT job.id, job.name, job.city, " +
        " IFNULL(jobcategory.value, 'no category') AS category, " +
        " IFNULL(jobpriority.value, 'no priority') AS priority " +
        "FROM job " +
        "LEFT JOIN jobcategory ON job.category = jobcategory.id " +
        "LEFT JOIN jobpriority ON job.priority = jobpriority.id " +
        "WHERE job.user = 13"

    let jobs = try ExtendedJob.fetchAll(db, q)
}

您最终可以将自定义 SQL 查询封装在 "custom requests":

extension ExtendedJob {
    static func filter(userId: Int64) -> AnyTypedRequest<ExtendedJob> {
        let request = SQLRequest(
            "SELECT job.id, job.name, job.city, " +
            " IFNULL(jobcategory.value, 'no category') AS category, " +
            " IFNULL(jobpriority.value, 'no priority') AS priority " +
            "FROM job " +
            "LEFT JOIN jobcategory ON job.category = jobcategory.id " +
            "LEFT JOIN jobpriority ON job.priority = jobpriority.id " +
            "WHERE job.user = ?",
            arguments: [userId])
        return request.asRequest(of: ExtendedJob.self)
    }
}

// No SQL in sight:
let jobs = try dbQueue.inDatabase { db in
    try ExtendedJob.filter(userId: 13).fetchAll(db)
}