Skip to content

Protocol

SQLDatabase

The common interface to SQLKit for both drivers and client code.
protocol SQLDatabase : Sendable

Mentioned in

Overview

SQLDatabase is the core of an SQLKit driver and the primary entry point for user code. This common interface provides the information and behaviors necessary to define and leverage the package’s functionality.

Conformances to SQLDatabase are typically provided by an external database-specific driver package, alongside several wrapper types for handling connection logic and other details. A driver package must at minimum provide concrete implementations of SQLDatabase, SQLDialect, and SQLRow.

The API described by the base SQLDatabase protocol is low-level, meant for SQLKit drivers to implement; most users will not need to interact with these APIs directly. The high-level starting point for SQLKit is SQLQueryBuilder; the various query builders provide extension methods on SQLDatabase which are the intended public interface.

For comparison, this is an example of using SQLDatabase and SQLExpressions directly:

let database: SQLDatabase = ...

var select = SQLSelect()

select.columns = [SQLColumn(SQLIdentifier("x"))]
select.tables = [SQLIdentifier("y")]
select.predicate = SQLBinaryExpression(
    left: SQLColumn(SQLIdentifier("z")),
    op: SQLBinaryOperator.equal,
    right: SQLLiteral.numeric("1")
)

nonisolated(unsafe) var resultRows: [SQLRow] = []

try await database.execute(sql: select, { resultRows.append($0) })
// Executed query: SELECT x FROM y WHERE z = 1

var resultValues: [Int] = try resultRows.map {
    try $0.decode(column: "x", as: Int.self)
}

And this is the same example, written to make use of SQLSelectBuilder:

let database: SQLDatabase = ...
let resultValues: [Int] = try await database.select()
    .column("x")
    .from("y")
    .where("z", .equal, 1)
    .all(decodingColumn: "x", as: Int.self)

Topics

Properties

  • logger
    The Logger used for logging all operations relating to a given database.
  • eventLoop
    The EventLoop used for asynchronous operations on a given database.
  • version
    The version number the database reports for itself.
  • dialect
    The descriptor for the dialect of SQL supported by the given database.
  • queryLogLevel
    The logging level used for reporting queries run on the given database to the database’s logger. Defaults to .debug.

Query interface

  • execute(sql:_:)
    Requests that the given generic SQL query be serialized and executed on the database, and that the onRow closure be invoked once for each result row the query returns (if any).
  • withSession(_:)
    Requests the provided closure be called with a database which is guaranteed to represent a single “session”, suitable for e.g. executing a series of queries representing a transaction.

DML queries

DDL queries

Raw queries

Logging

  • logging(to:)
    Return a new SQLDatabase which is indistinguishable from the original save that its logger property is replaced by the given Logger.

Legacy query interface

  • execute(sql:_:)
    Requests that the given generic SQL query be serialized and executed on the database, and that the onRow closure be invoked once for each result row the query returns (if any).

Instance Methods

Relationships

Inherits From

  • Swift.Sendable
  • Swift.SendableMetatype

See Also

Data Access

  • SQLRow
    Represents a single row in a result set returned from an executed SQL query.
  • SQLRowDecoder
    An implementation of Decoder designed to decode “models” (or, in general, aggregate Decodable types) from SQLRows returned from a database query.
  • SQLQueryEncoder
    An implementation of Encoder designed to encode “models” (or, in general, aggregate Encodable types) into a form which can be used as input to a database query.