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
SQLExpressionThe fundamental base type of anything which can be represented as SQL using SQLKit.SQLSerializerEncapsulates the most basic operations for serializingSQLExpressions into a raw SQL string and a (potentially empty) sequence of bound parameter values.SQLStatementAn alternative API for serialization ofSQLExpressions.
Data Access
SQLDatabaseThe common interface to SQLKit for both drivers and client code.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.
Drivers
SQLDialectAn abstract definition of a specific dialect of SQL.SQLDatabaseReportedVersionProvides a protocol for reporting and comparing database version numbers.SQLAlterTableSyntaxEncapsulates a dialect’s support forALTER TABLEsyntax.SQLTriggerSyntaxEncapsulates a dialect’s support forCREATE TRIGGERandDROP TRIGGERsyntax.SQLUnionFeaturesA set of feature support flags forUNIONqueries.SQLEnumSyntaxPossible values for a dialect’s strongly-typed enumeration support.SQLUpsertSyntaxThe supported syntax variants which a SQL dialect can use to to specify conflict resolution clauses.
Builder Protocols
SQLAliasedColumnListBuilderCommon definitions for query builders which permit specifying aliased column names.SQLColumnUpdateBuilderCommon definitions for query builders which support assigning values to columns.SQLJoinBuilderCommon definitions for any query builder which permits specifying joins.SQLPartialResultBuilderCommon definitions for any query builder which permits specifying range and ordering behaviors.SQLPredicateBuilderCommon definitions for any query builder which permits specifying a primary predicate.SQLQueryBuilderBase definitions for builders which set up queries and execute them against a given database.SQLQueryFetcherCommon definitions forSQLQueryBuilders which support retrieving result rows.SQLReturningBuilderCommon definitions for any query builder which can return values from data-mutating queries.SQLSecondaryPredicateBuilderCommon definitions for any query builder which permits specifying a secondary predicate.SQLSubqueryClauseBuilderA builder which can construct - but not execute - a completeSELECTquery. Useful for building CTEs,CREATE TABLE ... SELECTclauses, etc., not to mention actualSELECTqueries.SQLUnqualifiedColumnListBuilderCommon definitions for query builders which permit or require specifying a list of unqualified column names.
Query Builders
SQLAlterEnumBuilderBuildsSQLAlterEnumqueries.SQLAlterTableBuilderBuildsSQLAlterTablequeries.SQLConflictUpdateBuilderA builder for specifying column updates and an optional predicate to be applied to rows that caused unique key conflicts during anINSERT.SQLCreateEnumBuilderBuildsSQLCreateEnumqueries.SQLCreateIndexBuilderBuildsSQLCreateIndexqueries.SQLCreateTableBuilderBuildsSQLCreateTablequeries.SQLCreateTriggerBuilderBuildsSQLCreateTriggerqueries.SQLDeleteBuilderBuildsSQLDeletequeries.SQLDropEnumBuilderBuildsSQLDropEnumqueries.SQLDropIndexBuilderBuildsSQLDropIndexqueries.SQLDropTableBuilderBuildsSQLDropTablequeries.SQLDropTriggerBuilderBuildsSQLDropTriggerqueries.SQLInsertBuilderBuildsSQLInsertqueries.SQLPredicateGroupBuilderNestedSQLPredicateBuilderfor building expression groups.SQLRawBuilderBuilds raw SQL queries.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.SQLSecondaryPredicateGroupBuilderNestedSQLSecondaryPredicateBuilderfor building expression groups.SQLSelectBuilderBuildsSQLSelectqueries.SQLSubqueryBuilderBuildsSQLSubqueryqueries.SQLUnionBuilderBuilds top-levelSQLUnionqueries which may be executed on their own.SQLUpdateBuilderBuildsSQLUpdatequeries.
Syntactic Expressions
SQLBinaryExpressionA fundamental syntactical expression - a left and right operand joined by an infix operator.SQLBinaryOperatorSQL binary expression operators.SQLBindA parameterizied value bound to the SQL query.SQLFunctionA call to a function available in SQL, expressed as a name and a (possibly empty) list of arguments.SQLGroupExpressionA fundamental syntactical expression - an arbitrary expression or list of expressions, surroudned by parenthesis.SQLIdentifierA fundamental syntactical expression - a quoted identifier (also often referred to as a “name” or “object name”).SQLListA fundamental syntactical expression - a list of subexpresions with a specified “separator” subexpression.SQLLiteralA fundamental syntactical expression - one of several various kinds of literal SQL expressions.SQLRawA fundamental syntactical expression - an arbitrary string of raw SQL with no escaping or formating of any kind.
Basic Expressions
SQLAliasEncapsulates SQL’s<expression> [AS] <name>syntax, most often used to declare aliaed names for columns and tables.SQLBetweenAnSQLExpressionwhich constructs SQL of the form<operand> BETWEEN <lowerBound> AND <upperBound>.SQLColumnAn expression representing an optionally table-qualified column in an SQL table.SQLConstraintAn expression representing the combination of a constraint name and algorithm for table constraints.SQLDataTypeRepresents a value’s type in SQL.SQLDirectionDescribes an ordering direction for a given sorting key.SQLDistinctAn expression representing the subexpression of an aggregate function call which specifies whether the aggregate groups over all result rows or only distinct rows.SQLForeignKeyActionAn expression specifying a behavior for a foreign key constraint violation.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.SQLQualifiedTableAn expression representing an optionally second-level-qualified SQL table.SQLQueryStringAn expression consisting of an array of constituent subexpressions generated by custom string interpolations.
Clause Expressions
SQLAlterColumnDefinitionTypeA clause specifying a new data type to be applied to an existing column.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.SQLColumnConstraintAlgorithmColumn-level data constraints.SQLColumnDefinitionA clause expressing a column definition, for use when creating and altering tables.SQLConflictActionAn action to take when anINSERTquery encounters a unique constraint violation.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.SQLDropBehaviorSpecifies a behavior when performing aDROPoperation on a database object which is referenced by other objects.SQLEnumDataTypeRepresents a data type which specifies an enumeration in the database.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.SQLForeignKeyA completeFOREIGN KEYconstraint specification.SQLInsertModifierSimple helper for working around MySQL’s refusal to implement standard SQL. Only emits SQL when needed.SQLJoinEncapsulates a single SQLJOIN, specifying the join type, the right-side table, and condition.SQLJoinMethodThe method used by a table join clause.SQLLockingClauseAn SQL locking clause.SQLOrderByA pair of expressions, one describing a query sort key and the other a directionality for that key.SQLReturningA clause describing a list of values to be returned from a data-modifying query.SQLSubqueryAn expression which wraps aSQLSelectquery in aSQLGroupExpressionin order to form a syntactically valid subquery expression.SQLTableConstraintAlgorithmTable-level data constraints.SQLUnionJoinerAn expression representing one of the six supported query union operations.
Query Expressions
SQLAlterEnumAn expression representing anALTER TYPEquery. Used to add new cases to enumeration types.SQLAlterTableAn expression representing anALTER TABLEquery. Used to modify the structure of existing tables.SQLCreateEnumAn expression representing aCREATE TYPEquery. Used to create enumeration types.SQLCreateIndexAn expression representing aCREATE INDEXquery. Used to add indexes over columns to an existing table.SQLCreateTableAn expression representing aCREATE TABLEquery. Used to create new tables.SQLCreateTriggerAn expression representing aCREATE TRIGGERquery. Used to create new triggers for actions on a table.SQLDeleteAn expression representing aCREATE TRIGGERquery. Used to remove rows from a table.SQLDropEnumAn expression representing aDROP TYPEquery. Used to delete enumeration types.SQLDropIndexAn expression representing aDROP INDEXquery. Used to delete indexes from tables.SQLDropTableAn expression representing aDROP TABLEquery. Used to delete entire tables.SQLDropTriggerAn expression representing aDROP TRIGGERquery. Used to delete triggers.SQLInsertAn expression representing anINSERTquery. Used to add new rows to a table.SQLSelectAn expression representing aSELECTquery. Used to retrieve rows and expression results from a database.SQLUnionAn expression representing two or moreSELECTqueries joined byUNIONclauses. Used to merge the results of multiple queries into a single result set.SQLUpdateAn expression representing anUPDATEquery. Used to modify existing rows in a single table.
Miscellaneous
SomeCodingKeyA straightforward implementation ofCodingKey, used to represent arbitrary keys.
Deprecated
SQLCreateTableAsSubqueryBuilderFormerly a separate builder used to constructSELECTsubqueries inCREATE TABLEqueries, now a deprecated alias for the more general-purposeSQLSubqueryBuilder.SQLErrorAn error from a SQL query or database operation. Constains an additional property detailing what type of SQL error has occurred.SQLErrorTypeTypes of SQL errors.SQLTriggerEachOld name forSQLCreateTrigger.EachSpecifier.SQLTriggerEventOld name forSQLCreateTrigger.EventSpecifier.SQLTriggerOrderOld name forSQLCreateTrigger.OrderSpecifier.SQLTriggerTimingOld name forSQLCreateTrigger.TimingSpecifier.SQLTriggerWhenOld name forSQLCreateTrigger.WhenSpecifier.
Classes
SQLUnionSubqueryBuilderBuildsSQLUnionsubqueries meant to be embedded within other queries.
Protocols
SQLCommonTableExpressionBuilderCommon definitions for query builders which support Common Table Expressions.SQLCommonUnionBuilder
Structures
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.SQLCommonTableExpressionGroupA clause representing a group of one or moreSQLCommonTableExpressions.SQLUnionSubqueryA trivial copy ofSQLSubquerywith a different type for its subquery property.