Classes
The following classes are available globally.
-
Subclasses of the
AuxiliaryTable
class are describing aTable
that is used in WITH clauses.Usage Example:
In this example, an
AuxiliaryTable
class is defined containing two columns. AToDoTable
(as defined inTable
), aPersonTable
(as defined inColumn
) and a connection instance are initialized. An instance of theAuxiliaryTable
is then initialised fromColumn
instances in “todotable”. ThisAuxiliaryTable
is then used to create an SQL WITH query. A description of the created query is then printed.
See moreclass AuxTable: AuxiliaryTable { let tableName = "aux_table" let name = Column("name") let finished = Column("finished") } let todotable = ToDoTable() // ToDoTable() is a previously defined `Table` class let persontable = PersonTable() // PersonTable() is a previously defined `Table` class let connection = PostgreSQLConnection(host: "localhost", port: 5432, options: [.databaseName("ToDoDatabase")]) let withTable = AuxTable(as: Select(todotable.toDo_completed.as("finished"), todotable.toDo_title.as("name"), from: todotable)) let withQuery = with(withTable, Select(withTable.finished, persontable.monthlyPay, from: persontable).join(withTable).on(persontable.name == withTable.name)) let stringQuery = try connection.descriptionOf(query: withQuery) print(stringQuery) // Prints WITH aux_table AS (SELECT toDoTable.toDo_completed AS finished, toDoTable.toDo_title AS name FROM toDoTable) SELECT aux_table.finished, personTable.monthlyPay FROM personTable JOIN aux_table ON personTable.firstName = aux_table.name
Declaration
Swift
open class AuxiliaryTable : Table
-
The
Column
class is used to represent a single column in an SQL table in Swift. A combination of columns are used to construct aTable
class which matches a specific table in an SQL database. TheColumn
class details the column name, the table the column belongs to, any SQL keywords which apply to the column and the data type of the column.Usage Example:
In this example, a person
Table
class, containing three instances of theColumn
class, is defined.
See morepublic class PersonTable : Table { let tableName = "personTable" let person_id = Column("person_id", Int32.self, autoIncrement: true, primaryKey: true, notNull: true, unique: true) let name = Column("name", String.self, notNull: true) let monthlyPay = Column("monthlyPay", Int32.self) }
Declaration
Swift
public class Column : Field, IndexColumn
-
This class implements a first in, first out connection pool. The pool maintains a cache of
ConnectionPoolConnection
instances, which can be reused for future requests, to enhance performance.Usage Example:
In most cases, you will create a connection pool using a plugin, such as SwiftKuery-postgreSQL, which implements this class. You would then call the specific plugin with the required options. If you wish to create the a ConnectionPool manually you can use this example. Two closures to generate and release Connection instances are defined. A connection pool is then initialized with the defined closures and options. A single Connection is then retrieved from the pool.
See morelet options = ConnectionPoolOptions(initialCapacity: 2, maxCapacity: 5, timeout: 1000) let connectionGenerator: () -> Connection? = { let connection = PostgreSQLConnection(host: "localhost", port: 5432, options: [.databaseName("ExampleDatabase")]) return connection } let connectionReleaser: (_ connection: Connection) -> () = { connection in connection.closeConnection() } let connectionPool = ConnectionPool(options: options, connectionGenerator: connectionGenerator, connectionReleaser: connectionReleaser) guard let singleConnection = connectionPool.getConnection() else { // handle error return }
Declaration
Swift
public class ConnectionPool
-
Note: This class is usually initialised by the
ConnectionPool
instance. Therefore, a standard user will not use this class unless they are creating their own SQL plugin.This class uses a
Connection
instance and aConnectionPool
instance to implement a wrapper for theConnection
class. It implements the functions of theConnection
protocol, in addition to thecloseConnection()
function for releasing aConnection
instance from theConnectionPool
.Usage Example:
In this example, a
ConnectionPool
instance is initialized (parameters defined in docs forConnectionPool
). AConnectionPoolConnection
instance is created by callingconnectionPool.getConnection()
. This connection is then released from the pool.
See morelet connectionPool = ConnectionPool(options: options, connectionGenerator: connectionGenerator, connectionReleaser: connectionReleaser) let connection = connectionPool.getConnection() connection.closeConnection()
Declaration
Swift
public class ConnectionPoolConnection : Connection
-
Undocumented
See moreDeclaration
Swift
public class DummyColumBuilder : ColumnCreator
-
A class to help with migration between two versions of a table.
Usage Example:
The suggested usage is to keep versions of the table classes somewhere in the application code:
public class MyTable_v0: Table { let a = Column("a", ...) let b = Column("b", ...) let tableName = "MyTable" } public class MyTable_v1: Table { let b = Column("b", ...) let c = Column("c", ...) let tableName = "MyTable" }
And use a typealias to refer to the current version of the table class in the application:
typealias MyTable = MyTable_v0 let t = MyTable() let q = Select(from: t) ...
The migration code from v0 to v1 should be something like this:
let t0 = MyTable_v0() let t1 = MyTable_v1() let migration0 = Migration(from: t0, to: t1, using: connection) migration0.alterTableAdd(column: t1.c) { result in ... }
And raw alternations, if needed:
See morelet dropColumnQuery = "ALTER TABLE " + t1.tableName + " DROP COLUMN " + t0.a.name connection.execute(dropColumnQuery) { result in ... }
Declaration
Swift
public class Migration
-
Note: Changing the
QueryBuilder
should only be needed for adding support for a new database plugin.QueryBuilder
is used in code dealing with variances between the various database engines. As different databases have different query syntax, sometimes changes need to be made when generating the actual SQL statement to run. Additional changes should be made by updating theQueryBuilder
substitutions array. Every query component then builds its string representation using that array.Usage Example:
In this example, a
QueryBuilder
for PostgreSQL is initialized. Parameters are set for PostgreSQL and string keywords for SQL queries are substituted into the queryBuilder. ThequeryBuilder
is then used to create a string description of theTable
class instance calledtodotable
.
See morelet queryBuilder = QueryBuilder(withDeleteRequiresUsing: true, withUpdateRequiresFrom: true, createAutoIncrement: createAutoIncrement) queryBuilder.updateSubstitutions([QueryBuilder.QuerySubstitutionNames.ucase : "UPPER", QueryBuilder.QuerySubstitutionNames.lcase : "LOWER", QueryBuilder.QuerySubstitutionNames.len : "LENGTH", QueryBuilder.QuerySubstitutionNames.numberedParameter : "$", QueryBuilder.QuerySubstitutionNames.namedParameter : "", QueryBuilder.QuerySubstitutionNames.double : "double precision" ]) let description = try todotable.build(queryBuilder: queryBuilder)
Declaration
Swift
public class QueryBuilder
-
Represents a query result set. The rows are accessable either in a blocking fashion using a
See moreRowSequence
or in a non-blocking fashion using nextRow() function.Declaration
Swift
public class ResultSet
-
The
Table
class is used to create a class in Swift that maps to an external SQL database table. To reference a table, you must implement this class and define the table name as well as theColumn
instances present. TheTable
class then provides methods to build SQL String descriptions for the various database engines.Usage Example:
In this example, we define a ToDoTable class which maps to an existing table in a SQL database. An instance of the ToDoTable class is then initialized and used in a SQL SELECT query to retrieve the data from the table within the external database.
See morepublic class ToDoTable: Table { let tableName = "toDoTable" let toDo_id = Column("toDo_id", Int32.self, autoIncrement: true, primaryKey: true, notNull: true, unique: true) let toDo_title = Column("toDo_title", String.self, notNull: true) let toDo_completed = Column("toDo_completed", Bool.self, defaultValue: false) } public class Application { let selectQuery = Select(from: ToDoTable()) }
Declaration
Swift
open class Table : Buildable