Structures
The following structures are available globally.
-
Declaration
Swift
public struct Column
-
Configuration for a DatabaseQueue or DatabasePool.
See moreDeclaration
Swift
public struct Configuration
-
Declaration
Swift
public struct IndexInfo
-
Primary keys are returned from the Database.primaryKey(_:) method.
When the table’s primary key is the rowid:
// CREATE TABLE items (name TEXT) let pk = try db.primaryKey("items") pk.columns // ["rowid"] pk.rowIDColumn // nil pk.isRowID // true // CREATE TABLE citizens ( // id INTEGER PRIMARY KEY, // name TEXT // ) let pk = try db.primaryKey("citizens")! pk.columns // ["id"] pk.rowIDColumn // "id" pk.isRowID // true
When the table’s primary key is not the rowid:
See more// CREATE TABLE countries ( // isoCode TEXT NOT NULL PRIMARY KEY // name TEXT // ) let pk = db.primaryKey("countries")! pk.columns // ["isoCode"] pk.rowIDColumn // nil pk.isRowID // false // CREATE TABLE citizenships ( // citizenID INTEGER NOT NULL REFERENCES citizens(id) // countryIsoCode TEXT NOT NULL REFERENCES countries(isoCode) // PRIMARY KEY (citizenID, countryIsoCode) // ) let pk = db.primaryKey("citizenships")! pk.columns // ["citizenID", "countryIsoCode"] pk.rowIDColumn // nil pk.isRowID // false
Declaration
Swift
public struct PrimaryKeyInfo
-
You get foreign keys from table names, with the
See moreforeignKeys(on:)
method.Declaration
Swift
public struct ForeignKeyInfo
-
DatabaseDateComponents reads and stores DateComponents in the database.
See moreDeclaration
Swift
public struct DatabaseDateComponents : DatabaseValueConvertible
-
A DatabaseMigrator registers and applies database migrations.
Migrations are named blocks of SQL statements that are guaranteed to be applied in order, once and only once.
When a user upgrades your application, only non-applied migration are run.
Usage:
See morevar migrator = DatabaseMigrator() // v1.0 database migrator.registerMigration("createAuthors") { db in try db.execute(""" CREATE TABLE authors ( id INTEGER PRIMARY KEY, creationDate TEXT, name TEXT NOT NULL ) """) } migrator.registerMigration("createBooks") { db in try db.execute(""" CREATE TABLE books ( uuid TEXT PRIMARY KEY, authorID INTEGER NOT NULL REFERENCES authors(id) ON DELETE CASCADE ON UPDATE CASCADE, title TEXT NOT NULL ) """) } // v2.0 database migrator.registerMigration("AddBirthYearToAuthors") { db in try db.execute("ALTER TABLE authors ADD COLUMN birthYear INT") } try migrator.migrate(dbQueue)
Declaration
Swift
public struct DatabaseMigrator
-
DatabaseRegion defines a region in the database. DatabaseRegion is dedicated to help transaction observers recognize impactful database changes in their
observes(eventsOfKind:)
anddatabaseDidChange(with:)
methods.A database region is the union of any number of
table regions
, which can cover a full table, or the combination of columns and rows (identified by their rowids):|Table1 | |Table2 | |Table3 | |Table4 | |Table5 | |——-| |——-| |——-| |——-| |——-| |x|x|x|x| |x| | | | |x|x|x|x| |x|x| |x| | | | | | |x|x|x|x| |x| | | | | | | | | | | | | | | |x| | | |x|x|x|x| |x| | | | | | | | | |x|x| |x| | | | | | |x|x|x|x| |x| | | | | | | | | | | | | | | | | | |
You don’t create a database region directly. Instead, you use one of those methods:
SelectStatement.fetchedRegion
:let statement = db.makeSelectStatement(
SELECT name, score FROM players
) print(statement.fetchedRegion) // printsplayers(name,score)
-
let request = Player.filter(key: 1) try print(request.fetchedRegion(db)) // prints
players(*)[1]
Database regions returned by requests can be more precise than regions returned by select statements. Especially, regions returned by statements don’t know about rowids:
See more// A plain statement let statement = db.makeSelectStatement("SELECT * FROM players WHERE id = 1") statement.fetchedRegion // "players(*)" // A query interface request that executes the same statement: let request = Player.filter(key: 1) try request.fetchedRegion(db) // "players(*)[1]"
Declaration
Swift
public struct DatabaseRegion
-
DatabaseValue is the intermediate type between SQLite and your values.
See https://www.sqlite.org/datatype3.html
See moreDeclaration
Swift
public struct DatabaseValue
-
A section given by a FetchedRecordsController.
See moreDeclaration
Swift
public struct FetchedRecordsSectionInfo<Record: RowConvertible>
-
A QueryInterfaceRequest describes an SQL query.
See https://github.com/groue/GRDB.swift#the-query-interface
See moreDeclaration
Swift
public struct QueryInterfaceRequest<T>
-
FTS3 lets you define
fts3
virtual tables.
See more// CREATE VIRTUAL TABLE documents USING fts3(content) try db.create(virtualTable: "documents", using: FTS3()) { t in t.column("content") }
Declaration
Swift
public struct FTS3 : VirtualTableModule
-
A full text pattern that can query FTS3 and FTS4 virtual tables.
See moreDeclaration
Swift
public struct FTS3Pattern
-
An FTS3 tokenizer, suitable for FTS3 and FTS4 table definitions:
db.create(virtualTable: "books", using: FTS4()) { t in t.tokenizer = .simple // FTS3TokenizerDescriptor }
See https://www.sqlite.org/fts3.html#tokenizer
See moreDeclaration
Swift
public struct FTS3TokenizerDescriptor
-
FTS4 lets you define
fts4
virtual tables.// CREATE VIRTUAL TABLE documents USING fts4(content) try db.create(virtualTable: "documents", using: FTS4()) { t in t.column("content") }
See https://www.sqlite.org/fts3.html
See moreDeclaration
Swift
public struct FTS4 : VirtualTableModule
-
FTS5 lets you define
fts5
virtual tables.// CREATE VIRTUAL TABLE documents USING fts5(content) try db.create(virtualTable: "documents", using: FTS5()) { t in t.column("content") }
See https://www.sqlite.org/fts5.html
See more
-
A full text pattern that can query FTS5 virtual tables.
See more
-
The reason why FTS5 is requesting tokenization.
See https://www.sqlite.org/fts5.html#custom_tokenizers
See more
-
An FTS5 tokenizer, suitable for FTS5 table definitions:
db.create(virtualTable: "books", using: FTS5()) { t in t.tokenizer = .unicode61() // FTS5TokenizerDescriptor }
See https://www.sqlite.org/fts5.html#tokenizers
See more
-
Flags that tell SQLite how to register a token.
See https://www.sqlite.org/fts5.html#custom_tokenizers
See more
-
Use persistence containers in the
encode(to:)
method of your persistable records:
See morestruct Player : MutablePersistable { var id: Int64? var name: String? func encode(to container: inout PersistenceContainer) { container["id"] = id container["name"] = name } }
Declaration
Swift
public struct PersistenceContainer
-
The MutablePersistable protocol uses this type in order to handle SQLite conflicts when records are inserted or updated.
See
MutablePersistable.persistenceConflictPolicy
.See https://www.sqlite.org/lang_conflict.html
See moreDeclaration
Swift
public struct PersistenceConflictPolicy
-
An adapted request.
See more -
An adapted typed request.
See moreDeclaration
Swift
public struct AdaptedTypedRequest<Base: TypedRequest> : TypedRequest
-
A type-erased TypedRequest.
An instance of AnyTypedRequest forwards its operations to an underlying typed request, hiding its specifics.
See moreDeclaration
Swift
public struct AnyTypedRequest<T> : TypedRequest
-
Indexes to (ColumnName, DatabaseValue) pairs in a database row.
See moreDeclaration
Swift
public struct RowIndex : Comparable
-
Declaration
Swift
public struct LayoutedColumnMapping
-
ColumnMapping is a row adapter that maps column names.
See morelet adapter = ColumnMapping(["foo": "bar"]) let sql = "SELECT 'foo' AS foo, 'bar' AS bar, 'baz' AS baz" // <Row foo:"bar"> try Row.fetchOne(db, sql, adapter: adapter)
Declaration
Swift
public struct ColumnMapping : RowAdapter
-
SuffixRowAdapter is a row adapter that hides the first columns in a row.
See morelet adapter = SuffixRowAdapter(fromIndex: 2) let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz" // <Row baz:3> try Row.fetchOne(db, sql, adapter: adapter)
Declaration
Swift
public struct SuffixRowAdapter : RowAdapter
-
RangeRowAdapter is a row adapter that only exposes a range of columns.
See morelet adapter = RangeRowAdapter(1..<3) let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz, 4 as qux" // <Row bar:2 baz: 3> try Row.fetchOne(db, sql, adapter: adapter)
Declaration
Swift
public struct RangeRowAdapter : RowAdapter
-
ScopeAdapter is a row adapter that lets you define scopes on rows.
See more// Two adapters let fooAdapter = ColumnMapping(["value": "foo"]) let barAdapter = ColumnMapping(["value": "bar"]) // Define scopes let adapter = ScopeAdapter([ "foo": fooAdapter, "bar": barAdapter]) // Fetch let sql = "SELECT 'foo' AS foo, 'bar' AS bar" let row = try Row.fetchOne(db, sql, adapter: adapter)! // Scoped rows: if let fooRow = row.scoped(on: "foo") { fooRow["value"] // "foo" } if let barRow = row.scopeed(on: "bar") { barRow["value"] // "bar" }
Declaration
Swift
public struct ScopeAdapter : RowAdapter
-
SQLCollatedExpression taints an expression so that every derived expression is eventually evaluated using an SQLite collation.
You create one by calling the SQLSpecificExpressible.collating() method.
See morelet email: SQLCollatedExpression = Column("email").collating(.nocase) // SELECT * FROM players WHERE email = 'arthur@example.com' COLLATE NOCASE Players.filter(email == "arthur@example.com")
Declaration
Swift
public struct SQLCollatedExpression
-
SQLExpressionLiteral is an expression built from a raw SQL snippet.
SQLExpressionLiteral("1 + 2")
The SQL literal may contain
?
and colon-prefixed tokens:
See moreSQLExpressionLiteral("? + ?", arguments: [1, 2]) SQLExpressionLiteral(":one + :two", arguments: ["one": 1, "two": 2])
Declaration
Swift
public struct SQLExpressionLiteral : SQLExpression
-
Declaration
Swift
public struct SQLUnaryOperator : Hashable
-
SQLExpressionUnary is an expression made of an unary operator and an operand expression.
See moreSQLExpressionUnary(.not, Column("favorite"))
Declaration
Swift
public struct SQLExpressionUnary : SQLExpression
-
Declaration
Swift
public struct SQLBinaryOperator : Hashable
-
SQLExpressionBinary is an expression made of two expressions joined with a binary operator.
See moreSQLExpressionBinary(.multiply, Column("length"), Column("width"))
Declaration
Swift
public struct SQLExpressionBinary : SQLExpression
-
Declaration
Swift
public struct SQLFunctionName : Hashable
-
SQLExpressionFunction is an SQL function call.
See more// ABS(-1) SQLExpressionFunction(.abs, [-1.databaseValue])
Declaration
Swift
public struct SQLExpressionFunction : SQLExpression
-
AllColumns is the
*
inSELECT *
.You use AllColumns in your custom implementation of TableMapping.databaseSelection.
For example:
See morestruct Player : TableMapping { static var databaseTableName = "players" static let databaseSelection: [SQLSelectable] = [AllColumns(), Column.rowID] } // SELECT *, rowid FROM players let request = Player.all()
Declaration
Swift
public struct AllColumns
-
StatementArguments provide values to argument placeholders in raw SQL queries.
Placeholders can take several forms (see https://www.sqlite.org/lang_expr.html#varparam for more information):
?NNN
(e.g.?2
): the NNN-th argument (starts at 1)?
: the N-th argument, where N is one greater than the largest argument number already assigned:AAAA
(e.g.:name
): named argument$AAAA
(e.g.$name
): named argument
Positional Arguments
To fill question marks placeholders, feed StatementArguments with an array:
db.execute( "INSERT ... (?, ?)", arguments: StatementArguments(["Arthur", 41])) // Array literals are automatically converted: db.execute( "INSERT ... (?, ?)", arguments: ["Arthur", 41])
Named Arguments
To fill named arguments, feed StatementArguments with a dictionary:
db.execute( "INSERT ... (:name, :score)", arguments: StatementArguments(["name": "Arthur", "score": 41])) // Dictionary literals are automatically converted: db.execute( "INSERT ... (:name, :score)", arguments: ["name": "Arthur", "score": 41])
Concatenating Arguments
Several arguments can be concatenated and mixed with the
append(contentsOf:)
method and the+
,&+
,+=
operators:var arguments: StatementArguments = ["Arthur"] arguments += [41] db.execute("INSERT ... (?, ?)", arguments: arguments)
+
and+=
operators consider that overriding named arguments is a programmer error:var arguments: StatementArguments = ["name": "Arthur"] arguments += ["name": "Barbara"] // fatal error: already defined statement argument: name
&+
andappend(contentsOf:)
allow overriding named arguments:var arguments: StatementArguments = ["name": "Arthur"] arguments = arguments &+ ["name": "Barbara"] print(arguments) // Prints ["name": "Barbara"]
Mixed Arguments
When a statement consumes a mix of named and positional arguments, it prefers named arguments over positional ones. For example:
See morelet sql = "SELECT ?2 AS two, :foo AS foo, ?1 AS one, :foo AS foo2, :bar AS bar" let row = try Row.fetchOne(db, sql, arguments: [1, 2, "bar"] + ["foo": "foo"])! print(row) // Prints <Row two:2 foo:"foo" one:1 foo2:"foo" bar:"bar">
Declaration
Swift
public struct StatementArguments
-
A database event, notified to TransactionObserver.
See moreDeclaration
Swift
public struct DatabaseEvent