簡體   English   中英

我可以使用直接SQL通過Fluent(Vapor)獲取表中的行數嗎?

[英]Can I use straight SQL to get the number of rows in a table using Fluent (Vapor)?

我想這樣做SELECT COUNT(column_name) FROM table_name; 返回一個Int並將其作為響應的一部分返回。 我不想為了獲取計數而將每個對象都加載到內存中; 像這樣: User.query().all().count

請告訴我Fluent可以實現! :)

raw方法是可能的。 這里有MySQL的例子

guard let mysql = drop.database?.driver as? MySQLDriver else {
    return
}

let count = try mysql.raw("SELECT COUNT(column_name) FROM table_name")

另外,每個驅動程序都必須實現raw方法

public protocol Driver {
    var idKey: String { get }
    func query<T: Entity>(_ query: Query<T>) throws -> Node
    func schema(_ schema: Schema) throws
    func raw(_ raw: String, _ values: [Node]) throws -> Node
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM