SQLSelectBuilder

public final class SQLSelectBuilder<Connection>: SQLQueryFetcher, SQLPredicateBuilder
    where Connection: SQLConnection

Builds SQLSelect queries.

conn.select()
    .all().from(Planet.self)
    .where(\Planet.name == "Earth")
    .all(decoding: Planet.self)

See SQLQueryFetcher and SQLPredicateBuilder for more information.

  • Select query being built.

    Declaration

    Swift

    public var select: Connection.Query.Select
  • Declaration

    Swift

    public var connection: Connection
  • Declaration

    Swift

    public var query: Connection.Query { get }
  • See SQLWhereBuilder.

    Declaration

    Swift

    public var predicate: Connection.Query.Select.Expression? { get set }
  • Creates a new SQLCreateTableBuilder.

    Declaration

    Swift

    public init(_ select: Connection.Query.Select, on connection: Connection)
  • Adds a function expression column to the result set.

    conn.select()
        .column(function: "count", .all, as: "count")
    

    Declaration

    Swift

    public func column(
        function: String,
        _ arguments: Connection.Query.Select.SelectExpression.Expression.Function.Argument...,
        as alias: Connection.Query.Select.SelectExpression.Identifier? = nil
    ) -> Self

    Parameters

    function

    Name of the function to execute.

    arguments

    Zero or more arguments to pass to the function. See SQLArgument.

    alias

    Optional alias for the result. This will be the value’s key in the result set.

    Return Value

    Self for chaining.

  • Adds an expression column to the result set.

    conn.select()
        .column(expression: .binary(1, .plus, 1), as: "two")
    

    Declaration

    Swift

    public func column(
        expression: Connection.Query.Select.SelectExpression.Expression,
        as alias: Connection.Query.Select.SelectExpression.Identifier? = nil
    ) -> Self

    Parameters

    expression

    Expression to resolve.

    alias

    Optional alias for the result. This will be the value’s key in the result set.

    Return Value

    Self for chaining.

  • All columns, i.e., *.

    conn.select()
        .all().from(Planet.self)
        .where(\Planet.name == "Earth")
        .all(decoding: Planet.self)
    

    Declaration

    Swift

    public func all() -> Self

    Return Value

    Self for chaining.

  • All columns from a specified table, i.e., table.*.

    conn.select()
        .all(table: Planet.self).from(Planet.self)
        .where(\Planet.name == "Earth")
        .all(decoding: Planet.self)
    

    Declaration

    Swift

    public func all<T>(table: T.Type) -> Self
        where T: SQLTable

    Parameters

    table

    SQLTable to select all columns from.

    Return Value

    Self for chaining.

  • Adds a SQLSelectExpression to the result set.

    Declaration

    Swift

    public func column(_ column: Connection.Query.Select.SelectExpression) -> Self
  • Adds a table to the FROM clause.

    conn.select()
        .all().from(Planet.self)
        .where(\Planet.name == "Earth")
        .all(decoding: Planet.self)
    

    Declaration

    Swift

    public func from<Table>(_ table: Table.Type) -> Self
        where Table: SQLTable

    Parameters

    table

    SQLTable type to select from.

    Return Value

    Self for chaining.

  • Adds one or more tables to the FROM clause.

    conn.select()
        .all().from("planets")
        .where(\Planet.name == "Earth")
        .all(decoding: Planet.self)
    

    Declaration

    Swift

    public func from(_ tables: Connection.Query.Select.TableIdentifier...) -> Self

    Parameters

    tables

    One or more table identifiers

    Return Value

    Self for chaining.

  • Adds a JOIN clause to the select statement.

    conn.select()
        .all().from(Planet.self)
        .join(\Planet.galaxyID, to: \Galaxy.id)
    

    Use in conjunction with multiple decode methods from SQLQueryFetcher to fetch joined data.

    Declaration

    Swift

    public func join<A, B, C, D>(
        _ local: KeyPath<A, B>,
        to foreign: KeyPath<C, D>,
        method: Connection.Query.Select.Join.Method = .default
    ) -> Self where A: SQLTable, B: Encodable, C: SQLTable, D: Encodable

    Parameters

    local

    Local column to join.

    foreign

    Foreign column to join.

    method

    SQLJoinMethod to use.

    Return Value

    Self for chaining.

  • Adds a JOIN clause to the select statement.

    conn.select()
        .all().from(Planet.self)
        .join(Galaxy.self, on: \Planet.galaxyID == \Galaxy.id)
    

    Use in conjunction with multiple decode methods from SQLQueryFetcher to fetch joined data.

    Declaration

    Swift

    public func join<Table>(
        _ table: Table.Type,
        on expression: Connection.Query.Select.Join.Expression,
        method: Connection.Query.Select.Join.Method = .default
    ) -> Self
        where Table: SQLTable

    Parameters

    table

    Foreign SQLTable to join.

    expression

    SQLExpression to use for joining the tables.

    method

    SQLJoinMethod to use.

    Return Value

    Self for chaining.

  • Adds a GROUP BY clause to the select statement.

    conn.select()
        .all().from(Planet.self)
        .groupBy(\Planet.name)
    

    Declaration

    Swift

    public func groupBy<T,V>(_ keyPath: KeyPath<T, V>) -> Self
        where T: SQLTable

    Parameters

    keyPath

    Key path to group by.

    Return Value

    Self for chaining.

  • Adds a GROUP BY clause to the select statement.

    Declaration

    Swift

    public func groupBy(_ expression: Connection.Query.Select.GroupBy.Expression) -> Self

    Parameters

    expression

    SQLExpression to group by.

    Return Value

    Self for chaining.

  • Adds an ORDER BY clause to the select statement.

    conn.select()
        .all().from(Planet.self)
        .orderBy(\Planet.name, .ascending)
    

    Declaration

    Swift

    public func orderBy<T,V>(
        _ keyPath: KeyPath<T, V>,
        _ direction: Connection.Query.Select.OrderBy.Direction = .ascending
    ) -> Self
        where T: SQLTable

    Parameters

    keyPath

    Key path to order by.

    direction

    SQLDirection to sort the results. Defaults to ascending.

    Return Value

    Self for chaining.

  • Adds an ORDER BY clause to the select statement.

    Declaration

    Swift

    public func orderBy(_ expression: Connection.Query.Select.OrderBy.Expression, _ direction: Connection.Query.Select.OrderBy.Direction = .ascending) -> Self

    Parameters

    expression

    SQLExpression to order by.

    direction

    SQLDirection to sort the results. Defaults to ascending.

    Return Value

    Self for chaining.