Structs
The following structs are available globally.
-
Configuration for a DatabaseQueue or DatabasePool.
See moreDeclaration
Swift
public struct Configuration
-
Declaration
Swift
public struct IndexInfo
-
You get primary keys from table names, with the Database.primaryKey(_) method.
Primary key is nil when table has no primary key:
// CREATE TABLE items (name TEXT) let itemPk = try db.primaryKey("items") // nil
Primary keys have one or several columns. When the primary key has a single column, it may contain the row id:
See more// CREATE TABLE persons ( // id INTEGER PRIMARY KEY, // name TEXT // ) let personPk = try db.primaryKey("persons")! personPk.columns // ["id"] personPk.rowIDColumn // "id" // CREATE TABLE countries ( // isoCode TEXT NOT NULL PRIMARY KEY // name TEXT // ) let countryPk = db.primaryKey("countries")! countryPk.columns // ["isoCode"] countryPk.rowIDColumn // nil // CREATE TABLE citizenships ( // personID INTEGER NOT NULL REFERENCES persons(id) // countryIsoCode TEXT NOT NULL REFERENCES countries(isoCode) // PRIMARY KEY (personID, countryIsoCode) // ) let citizenshipsPk = db.primaryKey("citizenships")! citizenshipsPk.columns // ["personID", "countryIsoCode"] citizenshipsPk.rowIDColumn // nil
Declaration
Swift
public struct PrimaryKeyInfo
-
DatabaseCoder reads and stores objects that conform to NSCoding in the database.
See moreDeclaration
Swift
public struct DatabaseCoder: DatabaseValueConvertible
-
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("createPersons") { db in try db.execute( "CREATE TABLE persons (" + "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, " + "ownerID INTEGER NOT NULL " + " REFERENCES persons(id) " + " ON DELETE CASCADE ON UPDATE CASCADE, " + "title TEXT NOT NULL" + ")") } // v2.0 database migrator.registerMigration("AddAgeToPersons") { db in try db.execute("ALTER TABLE persons ADD COLUMN age INT") } try migrator.migrate(dbQueue)
Declaration
Swift
public struct DatabaseMigrator
-
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>
-
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
-
A QueryInterfaceRequest describes an SQL query.
See https://github.com/groue/GRDB.swift#the-query-interface
See moreDeclaration
Swift
public struct QueryInterfaceRequest<T> : TypedRequest
-
Declaration
Swift
public struct Column
-
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
-
Undocumented
See more
-
Undocumented
See more
-
Undocumented
See more
-
Undocumented
See more
-
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
-
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
-
LayoutedColumnMapping is a type that supports the RowAdapter protocol.
See moreDeclaration
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(named: "value") // "foo" } if let barRow = row.scopeed(on: "bar") { barRow.value(named: "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 persons WHERE email = 'arthur@example.com' COLLATE NOCASE Persons.filter(email == "arthur@example.com")
Declaration
Swift
public struct SQLCollatedExpression
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
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
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLUnaryOperator is a SQLite unary operator.
See moreDeclaration
Swift
public struct SQLUnaryOperator : Hashable
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLExpressionUnary is an expression made of an unary operator and an operand expression.
See moreSQLExpressionUnary(.not, Column("favorite"))
Declaration
Swift
public struct SQLExpressionUnary : SQLExpression
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLBinaryOperator is a SQLite binary operator.
See moreDeclaration
Swift
public struct SQLBinaryOperator : Hashable
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
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
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLExpressionExists is an expression that checks if a subquery would return rows or not with the
See moreEXISTS
operator.Declaration
Swift
public struct SQLExpressionExists : SQLExpression
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLFunctionName is an SQL function name.
See moreDeclaration
Swift
public struct SQLFunctionName : Hashable
-
This type is an implementation detail of the query interface. Do not use it directly.
See https://github.com/groue/GRDB.swift/#the-query-interface
Low Level Query Interface
SQLExpressionFunction is an SQL function call.
See more// ABS(-1) SQLExpressionFunction(.abs, [-1.databaseValue])
Declaration
Swift
public struct SQLExpressionFunction : SQLExpression
-
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, :age)", arguments: StatementArguments(["name": "Arthur", "age": 41])) // Dictionary literals are automatically converted: db.execute( "INSERT ... (:name, :age)", arguments: ["name": "Arthur", "age": 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