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
nilvalue for thedefaultValueproperty will be interpreted asNULLDeclaration
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
Columnclass. This creates an instance ofColumnusing 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
Columnclass is created to match the person_id column of an SQL table. To represent this aColumnis 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
nameThe name of the column.
typeThe type of the column. Defaults to nil.
lengthThe length of the column values according to the type. Defaults to nil.
autoIncrementAn indication whether the column autoincrements. Defaults to false.
primaryKeyAn indication whether the column is the primary key of the table. Defaults to false.
notNullAn indication whether the column is not nullable. Defaults to false.
uniqueAn indication whether the column values have to be unique. Defaults to false.
defaultValueThe default value of the column. Defaults to nil.
nullDefaultValueProperty denoting whether default value is NULL. Defaults to false.
checkThe expression to check for values inserted into the column. Defaults to nil.
collateThe collation rule for the column. Defaults to nil.
-
Function to build a String representation for referencing a
Columninstance. AQueryBuilderis used to handle variances between the various database engines and produce a correct SQL description. This function is required to conform to theBuildableprotocol.Usage Example:
In this example,
QueryBuilderand 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.nameThrows
QueryError.syntaxError if query build fails.Declaration
Swift
public func build(queryBuilder: QueryBuilder) throws -> StringParameters
queryBuilderThe QueryBuilder to use.
Return Value
A String representation of the column.
-
Function to build a String representation of the index of a
Columninstance. AQueryBuilderis used to handle variances between the various database engines and produce a correct SQL description.Usage Example:
In this example,
QueryBuilderandPersonTable(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 nameDeclaration
Swift
public func buildIndex(queryBuilder: QueryBuilder) -> StringParameters
queryBuilderThe QueryBuilder to use.
Return Value
A String representation of the index column.
-
Function to create a String representation of a
Columninstance for use in an SQL CREATE TABLE query. AQueryBuilderis used to handle variances between the various database engines and produce a correct SQL description.Usage Example:
In this example,
QueryBuilderand aPersonTable(as defined at the top of this class) instances are initialized. The create function is then used to produce a String description of theColumnand 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 -> StringParameters
queryBuilderThe QueryBuilder to use.
Return Value
A String representation of the column.
-
Function to return a copy of the current
Columninstance 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 thisColumninstance 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) -> ColumnParameters
newNameA String containing the alias for the column.
Return Value
A new Column instance with the alias.
View on GitHub
Column Class Reference