mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
https://github.com/apple/swift-evolution/blob/main/proposals/0361-bound-generic-extensions.md
193 lines
6.2 KiB
Swift
193 lines
6.2 KiB
Swift
/// A FetchRequest built from raw SQL.
|
|
public struct SQLRequest<RowDecoder> {
|
|
/// There are two statement caches: one "public" for statements generated by
|
|
/// the user, and one "internal" for the statements generated by GRDB. Those
|
|
/// are separated so that GRDB has no opportunity to inadvertently modify
|
|
/// the arguments of user's cached statements.
|
|
enum Cache {
|
|
/// The public cache, for library user
|
|
case `public`
|
|
|
|
/// The internal cache, for GRDB
|
|
case `internal`
|
|
}
|
|
|
|
/// The request adapter
|
|
public var adapter: (any RowAdapter)?
|
|
|
|
private(set) var sqlLiteral: SQL
|
|
let cache: Cache?
|
|
|
|
private init(
|
|
literal sqlLiteral: SQL,
|
|
adapter: (any RowAdapter)?,
|
|
fromCache cache: Cache?,
|
|
type: RowDecoder.Type)
|
|
{
|
|
self.sqlLiteral = sqlLiteral
|
|
self.adapter = adapter
|
|
self.cache = cache
|
|
}
|
|
}
|
|
|
|
extension SQLRequest {
|
|
/// Creates a request from an SQL string, optional arguments, and
|
|
/// optional row adapter.
|
|
///
|
|
/// let request = SQLRequest<String>(sql: """
|
|
/// SELECT name FROM player
|
|
/// """)
|
|
/// let request = SQLRequest<Player>(sql: """
|
|
/// SELECT * FROM player WHERE id = ?
|
|
/// """, arguments: [1])
|
|
///
|
|
/// - parameters:
|
|
/// - sql: An SQL query.
|
|
/// - arguments: Statement arguments.
|
|
/// - adapter: Optional RowAdapter.
|
|
/// - cached: Defaults to false. If true, the request reuses a cached
|
|
/// prepared statement.
|
|
/// - returns: A SQLRequest
|
|
public init(
|
|
sql: String,
|
|
arguments: StatementArguments = StatementArguments(),
|
|
adapter: (any RowAdapter)? = nil,
|
|
cached: Bool = false)
|
|
{
|
|
self.init(
|
|
literal: SQL(sql: sql, arguments: arguments),
|
|
adapter: adapter,
|
|
fromCache: cached ? .public : nil,
|
|
type: RowDecoder.self)
|
|
}
|
|
|
|
/// Creates a request from an `SQL` literal, and optional row adapter.
|
|
///
|
|
/// Literals allow you to safely embed raw values in your SQL, without any
|
|
/// risk of syntax errors or SQL injection:
|
|
///
|
|
/// let name = "O'brien"
|
|
/// let request = SQLRequest<Player>(literal: """
|
|
/// SELECT * FROM player WHERE name = \(name)
|
|
/// """)
|
|
///
|
|
/// - parameters:
|
|
/// - sqlLiteral: An `SQL` literal.
|
|
/// - adapter: Optional RowAdapter.
|
|
/// - cached: Defaults to false. If true, the request reuses a cached
|
|
/// prepared statement.
|
|
/// - returns: A SQLRequest
|
|
public init(literal sqlLiteral: SQL, adapter: (any RowAdapter)? = nil, cached: Bool = false) {
|
|
self.init(
|
|
literal: sqlLiteral,
|
|
adapter: adapter,
|
|
fromCache: cached ? .public : nil,
|
|
type: RowDecoder.self)
|
|
}
|
|
}
|
|
|
|
extension SQLRequest<Row> {
|
|
/// Creates a request from an SQL string, optional arguments, and
|
|
/// optional row adapter.
|
|
///
|
|
/// let request = SQLRequest(sql: """
|
|
/// SELECT * FROM player WHERE id = ?
|
|
/// """, arguments: [1])
|
|
///
|
|
/// - parameters:
|
|
/// - sql: An SQL query.
|
|
/// - arguments: Statement arguments.
|
|
/// - adapter: Optional RowAdapter.
|
|
/// - cached: Defaults to false. If true, the request reuses a cached
|
|
/// prepared statement.
|
|
/// - returns: A SQLRequest
|
|
public init(
|
|
sql: String,
|
|
arguments: StatementArguments = StatementArguments(),
|
|
adapter: (any RowAdapter)? = nil,
|
|
cached: Bool = false)
|
|
{
|
|
self.init(
|
|
literal: SQL(sql: sql, arguments: arguments),
|
|
adapter: adapter,
|
|
fromCache: cached ? .public : nil,
|
|
type: Row.self)
|
|
}
|
|
|
|
/// Creates a request from an `SQL` literal, and optional row adapter.
|
|
///
|
|
/// Literals allow you to safely embed raw values in your SQL, without any
|
|
/// risk of syntax errors or SQL injection:
|
|
///
|
|
/// let name = "O'brien"
|
|
/// let request = SQLRequest(literal: """
|
|
/// SELECT * FROM player WHERE name = \(name)
|
|
/// """)
|
|
///
|
|
/// - parameters:
|
|
/// - sqlLiteral: An `SQL` literal.
|
|
/// - adapter: Optional RowAdapter.
|
|
/// - cached: Defaults to false. If true, the request reuses a cached
|
|
/// prepared statement.
|
|
/// - returns: A SQLRequest
|
|
public init(literal sqlLiteral: SQL, adapter: (any RowAdapter)? = nil, cached: Bool = false) {
|
|
self.init(
|
|
literal: sqlLiteral,
|
|
adapter: adapter,
|
|
fromCache: cached ? .public : nil,
|
|
type: Row.self)
|
|
}
|
|
}
|
|
|
|
extension SQLRequest: FetchRequest {
|
|
public var sqlSubquery: SQLSubquery {
|
|
.literal(sqlLiteral)
|
|
}
|
|
|
|
public func fetchCount(_ db: Database) throws -> Int {
|
|
try SQLRequest<Int>("SELECT COUNT(*) FROM (\(self))").fetchOne(db)!
|
|
}
|
|
|
|
public func makePreparedRequest(
|
|
_ db: Database,
|
|
forSingleResult singleResult: Bool = false)
|
|
throws -> PreparedRequest
|
|
{
|
|
let context = SQLGenerationContext(db)
|
|
let sql = try sqlLiteral.sql(context)
|
|
let statement: Statement
|
|
switch cache {
|
|
case .none:
|
|
statement = try db.makeStatement(sql: sql)
|
|
case .public?:
|
|
statement = try db.cachedStatement(sql: sql)
|
|
case .internal?:
|
|
statement = try db.internalCachedStatement(sql: sql)
|
|
}
|
|
try statement.setArguments(context.arguments)
|
|
return PreparedRequest(statement: statement, adapter: adapter)
|
|
}
|
|
}
|
|
|
|
extension SQLRequest: ExpressibleByStringInterpolation {
|
|
/// :nodoc
|
|
public init(unicodeScalarLiteral: String) {
|
|
self.init(sql: unicodeScalarLiteral)
|
|
}
|
|
|
|
/// :nodoc:
|
|
public init(extendedGraphemeClusterLiteral: String) {
|
|
self.init(sql: extendedGraphemeClusterLiteral)
|
|
}
|
|
|
|
/// :nodoc:
|
|
public init(stringLiteral: String) {
|
|
self.init(sql: stringLiteral)
|
|
}
|
|
|
|
/// :nodoc:
|
|
public init(stringInterpolation sqlInterpolation: SQLInterpolation) {
|
|
self.init(literal: SQL(stringInterpolation: sqlInterpolation))
|
|
}
|
|
}
|