Files
GRDB.swift/GRDB/Core/SQLRequest.swift
T

149 lines
5.5 KiB
Swift

/// A FetchRequest built from raw SQL.
public struct SQLRequest<T> : FetchRequest {
public typealias RowDecoder = T
public var sql: String
public var arguments: StatementArguments
public var adapter: RowAdapter?
private let cache: Cache?
/// Creates a request from an SQL string, optional arguments, and
/// optional row adapter.
///
/// let request = SQLRequest<String>("""
/// SELECT name FROM player
/// """)
/// let request = SQLRequest<Player>("""
/// SELECT * FROM player WHERE id = ?
/// """, arguments: [1])
///
/// - parameters:
/// - sql: An SQL query.
/// - arguments: Optional 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? = nil, adapter: RowAdapter? = nil, cached: Bool = false) {
// TODO: make arguments non optional
// TODO: force sql parameter name: init(sql:...)
self.init(sql, arguments: arguments, adapter: adapter, fromCache: cached ? .public : nil)
}
/// Creates a request from an SQLString, and optional row adapter.
///
/// let request = SQLRequest<String>(SQLString(sql: """
/// SELECT name FROM player
/// """))
/// let request = SQLRequest<Player>(SQLString(sql: """
/// SELECT * FROM player WHERE name = ?
/// """, arguments: ["O'Brien"]))
///
/// With Swift 5, you can profit from string interpolation:
///
/// let request = SQLRequest<Player>(SQLString("""
/// SELECT * FROM player WHERE name = \("O'brien")
/// """))
///
/// - parameters:
/// - sqlString: An SQLString.
/// - adapter: Optional RowAdapter.
/// - cached: Defaults to false. If true, the request reuses a cached
/// prepared statement.
/// - returns: A SQLRequest
public init(_ sqlString: SQLString, adapter: RowAdapter? = nil, cached: Bool = false) {
// TODO: make this optional arguments non optional
self.init(sqlString.sql, arguments: sqlString.arguments, adapter: adapter, fromCache: cached ? .public : nil)
}
/// Creates an SQL request from any other fetch request.
///
/// - parameters:
/// - db: A database connection.
/// - request: A request.
/// - cached: Defaults to false. If true, the request reuses a cached
/// prepared statement.
/// - returns: An SQLRequest
public init<Request: FetchRequest>(_ db: Database, request: Request, cached: Bool = false) throws where Request.RowDecoder == RowDecoder {
let (statement, adapter) = try request.prepare(db)
self.init(statement.sql, arguments: statement.arguments, adapter: adapter, cached: cached)
}
/// Creates an SQL request from an SQL string, optional arguments, and
/// optional row adapter.
///
/// let request = SQLRequest("SELECT * FROM player")
/// let request = SQLRequest("SELECT * FROM player WHERE id = ?", arguments: [1])
///
/// - parameters:
/// - sql: An SQL query.
/// - arguments: Optional statement arguments.
/// - adapter: Optional RowAdapter.
/// - statementCacheName: Optional statement cache name.
/// - returns: A SQLRequest
init(_ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, fromCache cache: Cache?) {
// TODO: make this optional arguments non optional
// TODO: force sql parameter name: init(sql:...)
self.sql = sql
self.arguments = arguments ?? StatementArguments()
self.adapter = adapter
self.cache = cache
}
/// A tuple that contains a prepared statement that is ready to be
/// executed, and an eventual row adapter.
///
/// - parameter db: A database connection.
///
/// :nodoc:
public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
let statement: SelectStatement
switch cache {
case .none:
statement = try db.makeSelectStatement(sql)
case .public?:
statement = try db.cachedSelectStatement(sql)
case .internal?:
statement = try db.internalCachedSelectStatement(sql)
}
try statement.setArgumentsWithValidation(arguments)
return (statement, adapter)
}
/// There are two statement caches: one for statements generated by the
/// user, and one 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`
}
}
#if swift(>=5.0)
extension SQLRequest: ExpressibleByStringInterpolation {
/// :nodoc
public init(unicodeScalarLiteral: String) {
self.init(unicodeScalarLiteral)
}
/// :nodoc:
public init(extendedGraphemeClusterLiteral: String) {
self.init(extendedGraphemeClusterLiteral)
}
/// :nodoc:
public init(stringLiteral: String) {
self.init(stringLiteral)
}
/// :nodoc:
public init(stringInterpolation sqlInterpolation: SQLInterpolation) {
self.init(sqlInterpolation.sql, arguments: sqlInterpolation.arguments)
}
}
#endif