Column
public class Column : Field, IndexColumn
The Column
class is used to represent a single column in an SQL table in Swift.
A combination of columns are used to construct a Table
class which matches a specific table in an SQL database.
The Column
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 the Column
class, is defined.
public 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)
}
-
The name of the column.
Declaration
Swift
public let name: String
-
The alias of the column.
Declaration
Swift
public var alias: String?
-
The type of the column.
Declaration
Swift
public let type: SQLDataType.Type?
-
The length of the column values according to the type.
Declaration
Swift
public let length: Int?
-
An indication whether the column is the primary key of the table.
Declaration
Swift
public let isPrimaryKey: Bool
-
An indication whether the column is not nullable.
Declaration
Swift
public let isNotNullable: Bool
-
An indication whether the column values have to be unique.
Declaration
Swift
public let isUnique: Bool
-
The default value of the column.
Declaration
Swift
public let defaultValue: Any?
-
Property denoting whether default value is NULL If set to true a
nil
value for thedefaultValue
property will be interpreted asNULL
Declaration
Swift
public let nullDefaultValue: Bool
-
An indication whether the column autoincrements.
Declaration
Swift
public let autoIncrement: Bool
-
A boolean expression constraint, which values inserted into the column will be checked against.
Declaration
Swift
public let checkExpression: String?
-
The collation rule for the column.
Declaration
Swift
public let collate: String?
-
The table to which the column belongs.
Declaration
Swift
public var table: Table { get }
-
init(_:
_: length: autoIncrement: primaryKey: notNull: unique: defaultValue: nullDefaultValue: check: collate: ) The initializer for the
Column
class. This creates an instance ofColumn
using the provided parameters. Name must be provided, but all other fields will default to either nil or false if not given.Usage Example:
In this example, an instance of the
Column
class is created to match the person_id column of an SQL table. To represent this aColumn
is initialised with name set to “person_id”, type set as Int32.self (self is required to pass Int32 as the class) and primaryKey properties set to true.let person_id = Column("person_id", Int32.self, autoIncrement: true, primaryKey: true, notNull: true, unique: true)
Declaration
Swift
public init(_ name: String, _ type: SQLDataType.Type? = nil, length: Int? = nil, autoIncrement: Bool = false, primaryKey: Bool = false, notNull: Bool = false, unique: Bool = false, defaultValue: Any? = nil, nullDefaultValue: Bool = false, check: String? = nil, collate: String? = nil)
Parameters
name
The name of the column.
type
The type of the column. Defaults to nil.
length
The length of the column values according to the type. Defaults to nil.
autoIncrement
An indication whether the column autoincrements. Defaults to false.
primaryKey
An indication whether the column is the primary key of the table. Defaults to false.
notNull
An indication whether the column is not nullable. Defaults to false.
unique
An indication whether the column values have to be unique. Defaults to false.
defaultValue
The default value of the column. Defaults to nil.
nullDefaultValue
Property denoting whether default value is NULL. Defaults to false.
check
The expression to check for values inserted into the column. Defaults to nil.
collate
The collation rule for the column. Defaults to nil.
-
Function to build a String representation for referencing a
Column
instance. AQueryBuilder
is used to handle variances between the various database engines and produce a correct SQL description. This function is required to conform to theBuildable
protocol.Usage Example:
In this example,
QueryBuilder
and aPersonTable
(as defined at the top of this class) instances are initialized. The build function is then called to produce a String description and the results are printed.let queryBuilder = QueryBuilder() let personTable = PersonTable() let description = try personTable.name.build(queryBuilder: queryBuilder) print(description) // Prints personTable.name
Throws
QueryError.syntaxError if query build fails.Declaration
Swift
public func build(queryBuilder: QueryBuilder) throws -> String
Parameters
queryBuilder
The QueryBuilder to use.
Return Value
A String representation of the column.
-
Function to build a String representation of the index of a
Column
instance. AQueryBuilder
is used to handle variances between the various database engines and produce a correct SQL description.Usage Example:
In this example,
QueryBuilder
andPersonTable
(as defined at the top of this class) instances are initialized. The build function is then called to produce a String representation of the buildIndex and the results are printed.let queryBuilder = QueryBuilder() let personTable = PersonTable() let description = personTable.name.buildIndex(queryBuilder: queryBuilder) print(description) // Prints name
Declaration
Swift
public func buildIndex(queryBuilder: QueryBuilder) -> String
Parameters
queryBuilder
The QueryBuilder to use.
Return Value
A String representation of the index column.
-
Function to create a String representation of a
Column
instance for use in an SQL CREATE TABLE query. AQueryBuilder
is used to handle variances between the various database engines and produce a correct SQL description.Usage Example:
In this example,
QueryBuilder
and aPersonTable
(as defined at the top of this class) instances are initialized. The create function is then used to produce a String description of theColumn
and print the results.let queryBuilder = QueryBuilder() let personTable = PersonTable() let description = try personTable.person_id.create(queryBuilder: queryBuilder) print(description) // Prints "person_id integer AUTO_INCREMENT PRIMARY KEY NOT NULL UNIQUE"
Throws
QueryError.syntaxError if column creation fails.Declaration
Swift
public func create(queryBuilder: QueryBuilder) throws -> String
Parameters
queryBuilder
The QueryBuilder to use.
Return Value
A String representation of the column.
-
Function to return a copy of the current
Column
instance with the given name as its alias. This is equivalent to the SQL AS operator.Usage Example:
In this example, a
PersonTable
(as defined at the top of this class) instance is created which contains aColumn
. An alias for thisColumn
instance is then created and its alias printed.let personTable = PersonTable() let aliasColumn = personTable.name.as("new name") print(String(describing: aliasColumn.alias)) // Prints Optional("new name")
Declaration
Swift
public func `as`(_ newName: String) -> Column
Parameters
newName
A String containing the alias for the column.
Return Value
A new Column instance with the alias.