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

Query interface

DML queries

DDL queries

Raw queries

Logging

Legacy query interface

Instance Methods

Relationships

Inherits From

  • Swift.Sendable
  • Swift.SendableMetatype

See Also

Data Access

  • protocol SQLRow
    Represents a single row in a result set returned from an executed SQL query.
  • struct SQLRowDecoder
    An implementation of Decoder designed to decode “models” (or, in general, aggregate Decodable types) from SQLRows returned from a database query.
  • struct 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.