AuxiliaryTable

open class AuxiliaryTable : Table

Subclasses of the AuxiliaryTable class are describing a Table that is used in WITH clauses.

Usage Example:

In this example, an AuxiliaryTable class is defined containing two columns. A ToDoTable (as defined in Table), a PersonTable (as defined in Column) and a connection instance are initialized. An instance of the AuxiliaryTable is then initialised from Column instances in “todotable”. This AuxiliaryTable is then used to create an SQL WITH query. A description of the created query is then printed.

class 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

Initializer

  • Initialize an instance of AuxiliaryTable.

    Usage Example:

    In this example, an AuxiliaryTable class is defined containing two columns. A ToDoTable (as defined in Table) instance, and a connection instance are initialized. An instance of this AuxiliaryTable is then initialised from the Column instances in “todotable”.

    class 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 withTable = AuxTable(as: Select(todotable.toDo_completed.as("finished"), todotable.toDo_title.as("name"), from: todotable))
    

    Declaration

    Swift

    public convenience init(as query: Query)

    Parameters

    query

    A query that will be used in a WITH clause.

Build Query

  • Build a String representation of the WITH clause used to create the AuxiliaryTable instance, using QueryBuilder to account for the various databases.

    Usage Example:

    In this example, an AuxiliaryTable class is defined containing two columns. A ToDoTable (as defined in Table) instance and queryBuilder instance are initialized. An instance of this AuxiliaryTable is then initialised from the Column instances in “todotable”. The buildWith function is then called on this AuxiliaryTable instance, with the resulting String being printed out.

    class 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 queryBuilder = QueryBuilder()
    
    let withTable = AuxTable(as: Select(todotable.toDo_completed.as("finished"), todotable.toDo_title.as("name"), from: todotable))
    let withString = try withTable.buildWith(queryBuilder: queryBuilder)
    print(withString)
    // Prints aux_table AS (SELECT toDoTable.toDo_completed AS finished, toDoTable.toDo_title AS name FROM toDoTable)
    

    Throws

    QueryError.syntaxError if query build fails.

    Declaration

    Swift

    public func buildWith(queryBuilder: QueryBuilder) throws -> String

    Parameters

    queryBuilder

    The QueryBuilder to use.

    Return Value

    A String representation of the query.