SQLRequest

public struct SQLRequest<RowDecoder>
extension SQLRequest: FetchRequest
extension SQLRequest: ExpressibleByStringInterpolation

A FetchRequest built from raw SQL.

Available where RowDecoder == 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])
    

    Declaration

    Swift

    public init(
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil,
        cached: Bool = false)

    Parameters

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter.

    cached

    Defaults to false. If true, the request reuses a cached prepared statement.

    Return Value

    A SQLRequest

  • 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)
        """)
    

    Declaration

    Swift

    public init(literal sqlLiteral: SQL, adapter: RowAdapter? = nil, cached: Bool = false)

    Parameters

    sqlLiteral

    An SQL literal.

    adapter

    Optional RowAdapter.

    cached

    Defaults to false. If true, the request reuses a cached prepared statement.

    Return Value

    A SQLRequest