SQL

public struct SQL
extension SQL: SQLSpecificExpressible
extension SQL: SQLSelectable
extension SQL: SQLOrderingTerm
extension SQL: ExpressibleByStringInterpolation

SQL helps you build SQL literal with SQL Interpolation.

For example:

try dbQueue.write { db in
    let name: String = ...
    let id: Int64 = ...
    let query: SQL = "UPDATE player SET name = \(name) WHERE id = \(id)"
    try db.execute(literal: query)
}
  • Creates an SQL literal from a plain SQL string, and eventual arguments.

    For example:

    let query = SQL(
        sql: "UPDATE player SET name = ? WHERE id = ?",
        arguments: [name, id])
    

    Declaration

    Swift

    public init(sql: String, arguments: StatementArguments = StatementArguments())
  • Creates an SQL literal from an SQL expression.

    For example:

    let columnLiteral = SQL(Column("username"))
    let suffixLiteral = SQL("@example.com".databaseValue)
    let emailLiteral = [columnLiteral, suffixLiteral].joined(separator: " || ")
    let request = User.select(emailLiteral.sqlExpression)
    let emails = try String.fetchAll(db, request)
    

    Declaration

    Swift

    public init(_ expression: SQLSpecificExpressible)
  • Returns true if this literal generates an empty SQL string

    Declaration

    Swift

    public var isEmpty: Bool { get }
  • Turn a SQL literal into raw SQL and arguments.

    Declaration

    Swift

    public func build(_ db: Database) throws -> (sql: String, arguments: StatementArguments)

    Parameters

    db

    A database connection.

    Return Value

    A tuple made of a raw SQL string, and statement arguments.

  • Returns the SQL literal produced by the concatenation of two literals.

    let name = "O'Brien"
    let selection: SQL = "SELECT * FROM player "
    let condition: SQL = "WHERE name = \(name)"
    let query = selection + condition
    

    Declaration

    Swift

    public static func + (lhs: SQL, rhs: SQL) -> SQL
  • Appends an SQL literal to the receiver.

    let name = "O'Brien"
    var query: SQL = "SELECT * FROM player "
    query += "WHERE name = \(name)"
    

    Declaration

    Swift

    public static func += (lhs: inout SQL, rhs: SQL)
  • Appends an SQL literal to the receiver.

    let name = "O'Brien"
    var query: SQL = "SELECT * FROM player "
    query.append(literal: "WHERE name = \(name)")
    

    Declaration

    Swift

    public mutating func append(literal sqlLiteral: SQL)
  • Appends a plain SQL string to the receiver, and eventual arguments.

    let name = "O'Brien"
    var query: SQL = "SELECT * FROM player "
    query.append(sql: "WHERE name = ?", arguments: [name])
    

    Declaration

    Swift

    public mutating func append(sql: String, arguments: StatementArguments = StatementArguments())
  • Creates a literal SQL expression.

    Use this property when you need an explicit SQLExpression. For example:

    func date(_ value: SQLExpressible) -> SQLExpression {
        SQL("DATE(\(value))").sqlExpression
    }
    
    // SELECT * FROM "player" WHERE DATE("createdAt") = '2020-01-23'
    let createdAt = Column("createdAt")
    let request = Player.filter(date(createdAt) == "2020-01-23")
    

    Declaration

    Swift

    public var sqlExpression: SQLExpression { get }
  • Declaration

    Swift

    public var sqlSelection: SQLSelection { get }
  • Declaration

    Swift

    public var sqlOrdering: SQLOrdering { get }

ExpressibleByStringInterpolation