Skip to content

Package

SQLKit

SQLKit is a library for building and serializing SQL queries in Swift.

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

Fundamentals

  • SQLExpression
    The fundamental base type of anything which can be represented as SQL using SQLKit.
  • SQLSerializer
    Encapsulates the most basic operations for serializing SQLExpressions into a raw SQL string and a (potentially empty) sequence of bound parameter values.
  • SQLStatement
    An alternative API for serialization of SQLExpressions.

Data Access

  • SQLDatabase
    The common interface to SQLKit for both drivers and client code.
  • SQLRow
    Represents a single row in a result set returned from an executed SQL query.
  • SQLRowDecoder
    An implementation of Decoder designed to decode “models” (or, in general, aggregate Decodable types) from SQLRows returned from a database query.
  • SQLQueryEncoder
    An implementation of Encoder designed to encode “models” (or, in general, aggregate Encodable types) into a form which can be used as input to a database query.

Drivers

  • SQLDialect
    An abstract definition of a specific dialect of SQL.
  • SQLDatabaseReportedVersion
    Provides a protocol for reporting and comparing database version numbers.
  • SQLAlterTableSyntax
    Encapsulates a dialect’s support for ALTER TABLE syntax.
  • SQLTriggerSyntax
    Encapsulates a dialect’s support for CREATE TRIGGER and DROP TRIGGER syntax.
  • SQLUnionFeatures
    A set of feature support flags for UNION queries.
  • SQLEnumSyntax
    Possible values for a dialect’s strongly-typed enumeration support.
  • SQLUpsertSyntax
    The supported syntax variants which a SQL dialect can use to to specify conflict resolution clauses.

Builder Protocols

  • SQLAliasedColumnListBuilder
    Common definitions for query builders which permit specifying aliased column names.
  • SQLColumnUpdateBuilder
    Common definitions for query builders which support assigning values to columns.
  • SQLJoinBuilder
    Common definitions for any query builder which permits specifying joins.
  • SQLPartialResultBuilder
    Common definitions for any query builder which permits specifying range and ordering behaviors.
  • SQLPredicateBuilder
    Common definitions for any query builder which permits specifying a primary predicate.
  • SQLQueryBuilder
    Base definitions for builders which set up queries and execute them against a given database.
  • SQLQueryFetcher
    Common definitions for SQLQueryBuilders which support retrieving result rows.
  • SQLReturningBuilder
    Common definitions for any query builder which can return values from data-mutating queries.
  • SQLSecondaryPredicateBuilder
    Common definitions for any query builder which permits specifying a secondary predicate.
  • SQLSubqueryClauseBuilder
    A builder which can construct - but not execute - a complete SELECT query. Useful for building CTEs, CREATE TABLE ... SELECT clauses, etc., not to mention actual SELECT queries.
  • SQLUnqualifiedColumnListBuilder
    Common definitions for query builders which permit or require specifying a list of unqualified column names.

Query Builders

Syntactic Expressions

  • SQLBinaryExpression
    A fundamental syntactical expression - a left and right operand joined by an infix operator.
  • SQLBinaryOperator
    SQL binary expression operators.
  • SQLBind
    A parameterizied value bound to the SQL query.
  • SQLFunction
    A call to a function available in SQL, expressed as a name and a (possibly empty) list of arguments.
  • SQLGroupExpression
    A fundamental syntactical expression - an arbitrary expression or list of expressions, surroudned by parenthesis.
  • SQLIdentifier
    A fundamental syntactical expression - a quoted identifier (also often referred to as a “name” or “object name”).
  • SQLList
    A fundamental syntactical expression - a list of subexpresions with a specified “separator” subexpression.
  • SQLLiteral
    A fundamental syntactical expression - one of several various kinds of literal SQL expressions.
  • SQLRaw
    A fundamental syntactical expression - an arbitrary string of raw SQL with no escaping or formating of any kind.

Basic Expressions

  • SQLAlias
    Encapsulates SQL’s <expression> [AS] <name> syntax, most often used to declare aliaed names for columns and tables.
  • SQLBetween
    An SQLExpression which constructs SQL of the form <operand> BETWEEN <lowerBound> AND <upperBound>.
  • SQLColumn
    An expression representing an optionally table-qualified column in an SQL table.
  • SQLConstraint
    An expression representing the combination of a constraint name and algorithm for table constraints.
  • SQLDataType
    Represents a value’s type in SQL.
  • SQLDirection
    Describes an ordering direction for a given sorting key.
  • SQLDistinct
    An expression representing the subexpression of an aggregate function call which specifies whether the aggregate groups over all result rows or only distinct rows.
  • SQLForeignKeyAction
    An expression specifying a behavior for a foreign key constraint violation.
  • SQLNestedSubpathExpression
    A “nested subpath” expression is used to descend into the “deeper” structure of a non-scalar value, such as a dictionary, array, or JSON value.
  • SQLQualifiedTable
    An expression representing an optionally second-level-qualified SQL table.
  • SQLQueryString
    An expression consisting of an array of constituent subexpressions generated by custom string interpolations.

Clause Expressions

  • SQLAlterColumnDefinitionType
    A clause specifying a new data type to be applied to an existing column.
  • SQLColumnAssignment
    Encapsulates a column_name=value expression in the context of an UPDATE query’s value assignment list. This is distinct from an SQLBinaryExpression using the .equal operator in that the left side must be an unqualified column name, the operator must be =, and the right side may use SQLExcludedColumn when the assignment appears in the assignments list of a SQLConflictAction.update(assignments:predicate:) specification.
  • SQLColumnConstraintAlgorithm
    Column-level data constraints.
  • SQLColumnDefinition
    A clause expressing a column definition, for use when creating and altering tables.
  • SQLConflictAction
    An action to take when an INSERT query encounters a unique constraint violation.
  • SQLConflictResolutionStrategy
    Specifies a conflict resolution strategy and associated conditions for an INSERT query. An INSERT with a conflict strategy is often refered to as an UPSERT (“insert or update”). Databases are not required to support any given subset of upsert functionality, or any at all.
  • SQLDropBehavior
    Specifies a behavior when performing a DROP operation on a database object which is referenced by other objects.
  • SQLEnumDataType
    Represents a data type which specifies an enumeration in the database.
  • SQLExcludedColumn
    An 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.
  • SQLForeignKey
    A complete FOREIGN KEY constraint specification.
  • SQLInsertModifier
    Simple helper for working around MySQL’s refusal to implement standard SQL. Only emits SQL when needed.
  • SQLJoin
    Encapsulates a single SQL JOIN, specifying the join type, the right-side table, and condition.
  • SQLJoinMethod
    The method used by a table join clause.
  • SQLLockingClause
    An SQL locking clause.
  • SQLOrderBy
    A pair of expressions, one describing a query sort key and the other a directionality for that key.
  • SQLReturning
    A clause describing a list of values to be returned from a data-modifying query.
  • SQLSubquery
    An expression which wraps a SQLSelect query in a SQLGroupExpression in order to form a syntactically valid subquery expression.
  • SQLTableConstraintAlgorithm
    Table-level data constraints.
  • SQLUnionJoiner
    An expression representing one of the six supported query union operations.

Query Expressions

  • SQLAlterEnum
    An expression representing an ALTER TYPE query. Used to add new cases to enumeration types.
  • SQLAlterTable
    An expression representing an ALTER TABLE query. Used to modify the structure of existing tables.
  • SQLCreateEnum
    An expression representing a CREATE TYPE query. Used to create enumeration types.
  • SQLCreateIndex
    An expression representing a CREATE INDEX query. Used to add indexes over columns to an existing table.
  • SQLCreateTable
    An expression representing a CREATE TABLE query. Used to create new tables.
  • SQLCreateTrigger
    An expression representing a CREATE TRIGGER query. Used to create new triggers for actions on a table.
  • SQLDelete
    An expression representing a CREATE TRIGGER query. Used to remove rows from a table.
  • SQLDropEnum
    An expression representing a DROP TYPE query. Used to delete enumeration types.
  • SQLDropIndex
    An expression representing a DROP INDEX query. Used to delete indexes from tables.
  • SQLDropTable
    An expression representing a DROP TABLE query. Used to delete entire tables.
  • SQLDropTrigger
    An expression representing a DROP TRIGGER query. Used to delete triggers.
  • SQLInsert
    An expression representing an INSERT query. Used to add new rows to a table.
  • SQLSelect
    An expression representing a SELECT query. Used to retrieve rows and expression results from a database.
  • SQLUnion
    An expression representing two or more SELECT queries joined by UNION clauses. Used to merge the results of multiple queries into a single result set.
  • SQLUpdate
    An expression representing an UPDATE query. Used to modify existing rows in a single table.

Miscellaneous

  • SomeCodingKey
    A straightforward implementation of CodingKey, used to represent arbitrary keys.

Deprecated

Classes

Protocols

Structures

Extended Modules