Protocol
SQLDatabase
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
loggerTheLoggerused for logging all operations relating to a given database.eventLoopTheEventLoopused for asynchronous operations on a given database.versionThe version number the database reports for itself.dialectThe descriptor for the dialect of SQL supported by the given database.queryLogLevelThe 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 theonRowclosure 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
delete(from:)Create a newSQLDeleteBuilder.delete(from:)Create a newSQLDeleteBuilder.insert(into:)Create a newSQLInsertBuilder.insert(into:)Create a newSQLInsertBuilder.select()Create a newSQLSelectBuilder.union(_:)Create a newSQLUnionBuilder, providing a builder to create the first query.update(_:)Create a newSQLUpdateBuilderassociated with this database.update(_:)Create a newSQLUpdateBuilderassociated with this database.
DDL queries
alter(table:)Create a newSQLAlterTableBuilder.alter(table:)Create a newSQLAlterTableBuilder.alter(table:)Create a newSQLAlterTableBuilder.create(table:)Create a newSQLCreateTableBuilder.create(table:)Create a newSQLCreateTableBuilder.drop(table:)Create a newSQLDropTableBuilder.drop(table:)Create a newSQLDropTableBuilder.alter(enum:)Create a newSQLAlterEnumBuilder.alter(enum:)Create a newSQLAlterEnumBuilder.create(enum:)Create a newSQLCreateEnumBuilder.create(enum:)Create a newSQLCreateEnumBuilder.drop(enum:)Create a newSQLDropEnumBuilder.drop(enum:)Create a newSQLDropEnumBuilder.create(index:)Creates a newSQLCreateIndexBuilder.create(index:)Creates a newSQLCreateIndexBuilder.drop(index:)Create a newSQLDropIndexBuilder.drop(index:)Create a newSQLDropIndexBuilder.create(trigger:table:when:event:)Create a newSQLCreateTriggerBuilder.create(trigger:table:when:event:)Create a newSQLCreateTriggerBuilder.drop(trigger:)Create a newSQLDropTableBuilder.drop(trigger:)Create a newSQLDropTableBuilder.
Raw queries
raw(_:)Create a newSQLRawBuilder.serialize(_:)Serialize an arbitrarySQLExpressionusing the database’s dialect.
Logging
logging(to:)Return a newSQLDatabasewhich is indistinguishable from the original save that itsloggerproperty is replaced by the givenLogger.
Legacy query interface
execute(sql:_:)Requests that the given generic SQL query be serialized and executed on the database, and that theonRowclosure be invoked once for each result row the query returns (if any).
Instance Methods
alter(enum:)Create a newSQLAlterEnumBuilder.alter(table:)Create a newSQLAlterTableBuilder.create(enum:)Create a newSQLCreateEnumBuilder.create(index:)Creates a newSQLCreateIndexBuilder.create(table:)Create a newSQLCreateTableBuilder.create(trigger:table:when:event:)Create a newSQLCreateTriggerBuilder.delete(from:)Create a newSQLDeleteBuilder.drop(enum:)Create a newSQLDropEnumBuilder.drop(index:)Create a newSQLDropIndexBuilder.drop(table:)Create a newSQLDropTableBuilder.drop(trigger:)Create a newSQLDropTableBuilder.execute(sql:_:)Requests that the given generic SQL query be serialized and executed on the database, and that theonRowclosure be invoked once for each result row the query returns (if any).insert(into:)Create a newSQLInsertBuilder.update(_:)Create a newSQLUpdateBuilderassociated with this database.
Relationships
Inherits From
Swift.SendableSwift.SendableMetatype
See Also
Data Access
SQLRowRepresents a single row in a result set returned from an executed SQL query.SQLRowDecoderAn implementation ofDecoderdesigned to decode “models” (or, in general, aggregateDecodabletypes) fromSQLRows returned from a database query.SQLQueryEncoderAn implementation ofEncoderdesigned to encode “models” (or, in general, aggregateEncodabletypes) into a form which can be used as input to a database query.