Package
SQLKit
Overview
SQLKit’s query construction facilities provide mappings between Swift types and database field types, and a direct interface for executing SQL queries. It attempts to abstract away the many differences between the various dialects of SQL whenever practical, allowing users to construct queries for use with any of the supported database systems. Custom SQL can be directly specified as needed, such as when abstraction of syntax is not possible or unimplemented.
Note
Having been originally designed as a low-level “construction kit” for the Fluent ORM, the current incarnation of SQLKit is often excessively verbose, and offers relatively few user-friendly APIs. A future major release of Fluent is expected to replace both packages with an API designed around the same concepts as SQLKit, except targeted for both high-level and low-level use.
SQLKit does not provide facilities for creating or managing database connections; this functionality must be provided by a separate driver package which implements the required SQLKit protocols.
Topics
- Basic UsageGetting started with SQLKit
Fundamentals
protocol SQLExpressionThe fundamental base type of anything which can be represented as SQL using SQLKit.struct SQLSerializerEncapsulates the most basic operations for serializingSQLExpressions into a raw SQL string and a (potentially empty) sequence of bound parameter values.struct SQLStatementAn alternative API for serialization ofSQLExpressions.
Data Access
protocol SQLDatabaseThe common interface to SQLKit for both drivers and client code.protocol SQLRowRepresents a single row in a result set returned from an executed SQL query.struct SQLRowDecoderAn implementation ofDecoderdesigned to decode “models” (or, in general, aggregateDecodabletypes) fromSQLRows returned from a database query.struct SQLQueryEncoderAn implementation ofEncoderdesigned to encode “models” (or, in general, aggregateEncodabletypes) into a form which can be used as input to a database query.
Drivers
protocol SQLDialectAn abstract definition of a specific dialect of SQL.protocol SQLDatabaseReportedVersionProvides a protocol for reporting and comparing database version numbers.struct SQLAlterTableSyntaxEncapsulates a dialect’s support forALTER TABLEsyntax.struct SQLTriggerSyntaxEncapsulates a dialect’s support forCREATE TRIGGERandDROP TRIGGERsyntax.struct SQLUnionFeaturesA set of feature support flags forUNIONqueries.enum SQLEnumSyntaxPossible values for a dialect’s strongly-typed enumeration support.enum SQLUpsertSyntaxThe supported syntax variants which a SQL dialect can use to to specify conflict resolution clauses.
Builder Protocols
protocol SQLAliasedColumnListBuilderCommon definitions for query builders which permit specifying aliased column names.protocol SQLColumnUpdateBuilderCommon definitions for query builders which support assigning values to columns.protocol SQLCommonTableExpressionBuilderCommon definitions for query builders which support Common Table Expressions.protocol SQLCommonUnionBuilderprotocol SQLJoinBuilderCommon definitions for any query builder which permits specifying joins.protocol SQLPartialResultBuilderCommon definitions for any query builder which permits specifying range and ordering behaviors.protocol SQLPredicateBuilderCommon definitions for any query builder which permits specifying a primary predicate.protocol SQLQueryBuilderBase definitions for builders which set up queries and execute them against a given database.protocol SQLQueryFetcherCommon definitions forSQLQueryBuilders which support retrieving result rows.protocol SQLReturningBuilderCommon definitions for any query builder which can return values from data-mutating queries.protocol SQLSecondaryPredicateBuilderCommon definitions for any query builder which permits specifying a secondary predicate.protocol SQLSubqueryClauseBuilderA builder which can construct - but not execute - a completeSELECTquery. Useful for building CTEs,CREATE TABLE ... SELECTclauses, etc., not to mention actualSELECTqueries.protocol SQLUnqualifiedColumnListBuilderCommon definitions for query builders which permit or require specifying a list of unqualified column names.
Query Builders
class SQLAlterEnumBuilderBuildsSQLAlterEnumqueries.class SQLAlterTableBuilderBuildsSQLAlterTablequeries.class SQLConflictUpdateBuilderA builder for specifying column updates and an optional predicate to be applied to rows that caused unique key conflicts during anINSERT.class SQLCreateEnumBuilderBuildsSQLCreateEnumqueries.class SQLCreateIndexBuilderBuildsSQLCreateIndexqueries.class SQLCreateTableBuilderBuildsSQLCreateTablequeries.class SQLCreateTriggerBuilderBuildsSQLCreateTriggerqueries.class SQLDeleteBuilderBuildsSQLDeletequeries.class SQLDropEnumBuilderBuildsSQLDropEnumqueries.class SQLDropIndexBuilderBuildsSQLDropIndexqueries.class SQLDropTableBuilderBuildsSQLDropTablequeries.class SQLDropTriggerBuilderBuildsSQLDropTriggerqueries.class SQLInsertBuilderBuildsSQLInsertqueries.class SQLPredicateGroupBuilderNestedSQLPredicateBuilderfor building expression groups.class SQLRawBuilderBuilds raw SQL queries.class SQLReturningResultBuilderA builder returned from the methods ofSQLReturningBuilder; this builder wraps the original builder with one which providesSQLQueryFetcherconformance. As such, thereturning(_:)methods must always be the last ones in any call chain.class SQLSecondaryPredicateGroupBuilderNestedSQLSecondaryPredicateBuilderfor building expression groups.class SQLSelectBuilderBuildsSQLSelectqueries.class SQLSubqueryBuilderBuildsSQLSubqueryqueries.class SQLUnionBuilderBuilds top-levelSQLUnionqueries which may be executed on their own.class SQLUnionSubqueryBuilderBuildsSQLUnionsubqueries meant to be embedded within other queries.class SQLUpdateBuilderBuildsSQLUpdatequeries.
Syntactic Expressions
struct SQLBinaryExpressionA fundamental syntactical expression - a left and right operand joined by an infix operator.enum SQLBinaryOperatorSQL binary expression operators.struct SQLBindA parameterizied value bound to the SQL query.struct SQLFunctionA call to a function available in SQL, expressed as a name and a (possibly empty) list of arguments.struct SQLGroupExpressionA fundamental syntactical expression - an arbitrary expression or list of expressions, surroudned by parenthesis.struct SQLIdentifierA fundamental syntactical expression - a quoted identifier (also often referred to as a “name” or “object name”).struct SQLListA fundamental syntactical expression - a list of subexpresions with a specified “separator” subexpression.enum SQLLiteralA fundamental syntactical expression - one of several various kinds of literal SQL expressions.struct SQLRawA fundamental syntactical expression - an arbitrary string of raw SQL with no escaping or formating of any kind.
Basic Expressions
struct SQLAliasEncapsulates SQL’s<expression> [AS] <name>syntax, most often used to declare aliaed names for columns and tables.struct SQLBetweenAnSQLExpressionwhich constructs SQL of the form<operand> BETWEEN <lowerBound> AND <upperBound>.struct SQLColumnAn expression representing an optionally table-qualified column in an SQL table.struct SQLConstraintAn expression representing the combination of a constraint name and algorithm for table constraints.enum SQLDataTypeRepresents a value’s type in SQL.enum SQLDirectionDescribes an ordering direction for a given sorting key.struct SQLDistinctAn expression representing the subexpression of an aggregate function call which specifies whether the aggregate groups over all result rows or only distinct rows.enum SQLForeignKeyActionAn expression specifying a behavior for a foreign key constraint violation.struct SQLNestedSubpathExpressionA “nested subpath” expression is used to descend into the “deeper” structure of a non-scalar value, such as a dictionary, array, or JSON value.struct SQLQualifiedTableAn expression representing an optionally second-level-qualified SQL table.struct SQLQueryStringAn expression consisting of an array of constituent subexpressions generated by custom string interpolations.
Clause Expressions
struct SQLAlterColumnDefinitionTypeA clause specifying a new data type to be applied to an existing column.struct SQLColumnAssignmentEncapsulates acolumn_name=valueexpression in the context of anUPDATEquery’s value assignment list. This is distinct from anSQLBinaryExpressionusing the.equaloperator in that the left side must be an unqualified column name, the operator must be=, and the right side may useSQLExcludedColumnwhen the assignment appears in theassignmentslist of aSQLConflictAction.update(assignments:predicate:)specification.enum SQLColumnConstraintAlgorithmColumn-level data constraints.struct SQLColumnDefinitionA clause expressing a column definition, for use when creating and altering tables.struct SQLCommonTableExpressionA clause describing a single Common Table Expressions, which in itws simplest form provides additional data to a primary query in the same way as joining to a subquery.struct SQLCommonTableExpressionGroupA clause representing a group of one or moreSQLCommonTableExpressions.enum SQLConflictActionAn action to take when anINSERTquery encounters a unique constraint violation.struct SQLConflictResolutionStrategySpecifies a conflict resolution strategy and associated conditions for anINSERTquery. AnINSERTwith a conflict strategy is often refered to as anUPSERT(“insert or update”). Databases are not required to support any given subset of upsert functionality, or any at all.enum SQLDropBehaviorSpecifies a behavior when performing aDROPoperation on a database object which is referenced by other objects.struct SQLEnumDataTypeRepresents a data type which specifies an enumeration in the database.struct SQLExcludedColumnAn expression appearing on the right side of a column assignment which, when the assignment list is part of an upsert’s update acion, refers to the value which was originally to be inserted for the given column.struct SQLForeignKeyA completeFOREIGN KEYconstraint specification.struct SQLInsertModifierSimple helper for working around MySQL’s refusal to implement standard SQL. Only emits SQL when needed.struct SQLJoinEncapsulates a single SQLJOIN, specifying the join type, the right-side table, and condition.enum SQLJoinMethodThe method used by a table join clause.enum SQLLockingClauseAn SQL locking clause.struct SQLOrderByA pair of expressions, one describing a query sort key and the other a directionality for that key.struct SQLReturningA clause describing a list of values to be returned from a data-modifying query.struct SQLSubqueryAn expression which wraps aSQLSelectquery in aSQLGroupExpressionin order to form a syntactically valid subquery expression.enum SQLTableConstraintAlgorithmTable-level data constraints.struct SQLUnionJoinerAn expression representing one of the six supported query union operations.struct SQLUnionSubqueryA trivial copy ofSQLSubquerywith a different type for its subquery property.
Query Expressions
struct SQLAlterEnumAn expression representing anALTER TYPEquery. Used to add new cases to enumeration types.struct SQLAlterTableAn expression representing anALTER TABLEquery. Used to modify the structure of existing tables.struct SQLCreateEnumAn expression representing aCREATE TYPEquery. Used to create enumeration types.struct SQLCreateIndexAn expression representing aCREATE INDEXquery. Used to add indexes over columns to an existing table.struct SQLCreateTableAn expression representing aCREATE TABLEquery. Used to create new tables.struct SQLCreateTriggerAn expression representing aCREATE TRIGGERquery. Used to create new triggers for actions on a table.struct SQLDeleteAn expression representing aCREATE TRIGGERquery. Used to remove rows from a table.struct SQLDropEnumAn expression representing aDROP TYPEquery. Used to delete enumeration types.struct SQLDropIndexAn expression representing aDROP INDEXquery. Used to delete indexes from tables.struct SQLDropTableAn expression representing aDROP TABLEquery. Used to delete entire tables.struct SQLDropTriggerAn expression representing aDROP TRIGGERquery. Used to delete triggers.struct SQLInsertAn expression representing anINSERTquery. Used to add new rows to a table.struct SQLSelectAn expression representing aSELECTquery. Used to retrieve rows and expression results from a database.struct SQLUnionAn expression representing two or moreSELECTqueries joined byUNIONclauses. Used to merge the results of multiple queries into a single result set.struct SQLUpdateAn expression representing anUPDATEquery. Used to modify existing rows in a single table.
Miscellaneous
struct SomeCodingKeyA straightforward implementation ofCodingKey, used to represent arbitrary keys.
Deprecated
typealias SQLCreateTableAsSubqueryBuilderFormerly a separate builder used to constructSELECTsubqueries inCREATE TABLEqueries, now a deprecated alias for the more general-purposeSQLSubqueryBuilder.protocol SQLErrorAn error from a SQL query or database operation. Constains an additional property detailing what type of SQL error has occurred.struct SQLErrorTypeTypes of SQL errors.typealias SQLTriggerEachOld name forSQLCreateTrigger.EachSpecifier.typealias SQLTriggerEventOld name forSQLCreateTrigger.EventSpecifier.typealias SQLTriggerOrderOld name forSQLCreateTrigger.OrderSpecifier.typealias SQLTriggerTimingOld name forSQLCreateTrigger.TimingSpecifier.typealias SQLTriggerWhenOld name forSQLCreateTrigger.WhenSpecifier.