Skip to content
Josh Wright edited this page Jan 14, 2021 · 4 revisions

Query

public class Query: Sequelizable

Inheritance

Sequelizable

Initializers

init(database:)

public init(database: Database)

Methods

toSQL()

Get the raw SQL for a given query.

public func toSQL() -> SQL

Returns

A SQL value wrapper containing the executable query and bindings.

select(_:)

Set the columns that should be returned by the query.

@discardableResult public func select(_ columns: [Column] = ["*"]) -> Self

Parameters

  • columns: An array of columns to be returned by the query. Defaults to [*].

Returns

The current query builder Query to chain future queries to.

table(_:)

Set the table to perform a query from.

public func table(_ table: String) -> Self

Parameters

  • table: The table to run the query on.

Returns

The current query builder Query to chain future queries to.

from(table:as:)

An alias for table(_ table:​ String) to be used when running. a select query that also lets you alias the table name.

public func from(table: String, as alias: String? = nil) -> Self

Parameters

  • table: The table to select data from.
  • alias: An alias to use in place of table name. Defaults to nil.

Returns

The current query builder Query to chain future queries to.

join(table:first:op:second:type:)

Join data from a separate table into the current query data.

public func join(table: String, first: String, op: Operator = .equals, second: String, type: JoinType = .inner) -> Self

Parameters

  • table: The table to be joined.
  • first: The column from the current query to be matched.
  • op: The Operator to be used in the comparison. Defaults to .equals.
  • second: The column from the joining table to be matched.
  • type: The JoinType of the sql join. Defaults to .inner.

Returns

The current query builder Query to chain future queries to.

leftJoin(table:first:op:second:)

Left join data from a separate table into the current query data.

public func leftJoin(table: String, first: String, op: Operator = .equals, second: String) -> Self

Parameters

  • table: The table to be joined.
  • first: The column from the current query to be matched.
  • op: The Operator to be used in the comparison. Defaults to .equals.
  • second: The column from the joining table to be matched.

Returns

The current query builder Query to chain future queries to.

rightJoin(table:first:op:second:)

Right join data from a separate table into the current query data.

public func rightJoin(table: String, first: String, op: Operator = .equals, second: String) -> Self

Parameters

  • table: The table to be joined.
  • first: The column from the current query to be matched.
  • op: The Operator to be used in the comparison. Defaults to .equals.
  • second: The column from the joining table to be matched.

Returns

The current query builder Query to chain future queries to.

crossJoin(table:first:op:second:)

Cross join data from a separate table into the current query data.

public func crossJoin(table: String, first: String, op: Operator = .equals, second: String) -> Self

Parameters

  • table: The table to be joined.
  • first: The column from the current query to be matched.
  • op: The Operator to be used in the comparison. Defaults to .equals.
  • second: The column from the joining table to be matched.

Returns

The current query builder Query to chain future queries to.

`where`(_:)

Add a basic where clause to the query to filter down results.

public func `where`(_ clause: WhereValue) -> Self

Parameters

  • clause: A WhereValue clause matching a column to a given value.

Returns

The current query builder Query to chain future queries to.

orWhere(_:)

An alias for where(_ clause:​ WhereValue) that appends an or clause instead of an and clause.

public func orWhere(_ clause: WhereValue) -> Self

Parameters

  • clause: A WhereValue clause matching a column to a given value.

Returns

The current query builder Query to chain future queries to.

`where`(_:boolean:)

Add a nested where clause that is a group of combined clauses. This can be used for logically grouping where clauses like you would inside of an if statement. Each clause is wrapped in parenthesis.

public func `where`(_ closure: @escaping WhereNestedClosure, boolean: WhereBoolean = .and) -> Self

For example if you want to logically ensure a user is under 30 and named Paul, or over the age of 50 having any name, you could use a nested where clause along with a separate where value clause:

Query
.from("users")
.where {
    $0.where("age" < 30)
     .orWhere("first_name" == "Paul")
}
.where("age" > 50)

Parameters

  • closure: A WhereNestedClosure that provides a nested clause to attach nested where clauses to.
  • boolean: How the clause should be appended(.and or .or). Defaults to .and.

Returns

The current query builder Query to chain future queries to.

orWhere(_:)

A helper for adding an or where nested closure clause.

public func orWhere(_ closure: @escaping WhereNestedClosure) -> Self

Parameters

  • closure: A WhereNestedClosure that provides a nested query to attach nested where clauses to.

Returns

The current query builder Query to chain future queries to.

`where`(key:in:type:boolean:)

Add a clause requiring that a column match any values in a given array.

public func `where`(key: String, in values: [Parameter], type: WhereIn.InType = .in, boolean: WhereBoolean = .and) -> Self

Parameters

  • key: The column to match against.
  • values: The values that the column should not match.
  • type: How the match should happen (in or notIn). Defaults to .in.
  • boolean: How the clause should be appended (.and or .or). Defaults to .and.

Returns

The current query builder Query to chain future queries to.

orWhere(key:in:type:)

A helper for adding an or variant of the where(key:​in:​) clause.

public func orWhere(key: String, in values: [Parameter], type: WhereIn.InType = .in) -> Self

Parameters

  • key: The column to match against.
  • values: The values that the column should not match.
  • type: How the match should happen (.in or .notIn). Defaults to .in.

Returns

The current query builder Query to chain future queries to.

whereNot(key:in:boolean:)

Add a clause requiring that a column not match any values in a given array. This is a helper method for the where in method.

public func whereNot(key: String, in values: [Parameter], boolean: WhereBoolean = .and) -> Self

Parameters

  • key: The column to match against.
  • values: The values that the column should not match.
  • boolean: How the clause should be appended (.and or .or). Defaults to .and.

Returns

The current query builder Query to chain future queries to.

orWhereNot(key:in:)

A helper for adding an or whereNot clause.

public func orWhereNot(key: String, in values: [Parameter]) -> Self

Parameters

  • key: The column to match against.
  • values: The values that the column should not match.

Returns

The current query builder Query to chain future queries to.

whereRaw(sql:bindings:boolean:)

Add a raw SQL where clause to your query.

public func whereRaw(sql: String, bindings: [Parameter], boolean: WhereBoolean = .and) -> Self

Parameters

  • sql: A string representing the SQL where clause to be run.
  • bindings: Any variables for binding in the SQL.
  • boolean: How the clause should be appended (.and or .or). Defaults to .and.

Returns

The current query builder Query to chain future queries to.

orWhereRaw(sql:bindings:)

A helper for adding an or whereRaw clause.

public func orWhereRaw(sql: String, bindings: [Parameter]) -> Self

Parameters

  • sql: A string representing the SQL where clause to be run.
  • bindings: Any variables for binding in the SQL.

Returns

The current query builder Query to chain future queries to.

whereColumn(first:op:second:boolean:)

Add a where clause requiring that two columns match each other

@discardableResult public func whereColumn(first: String, op: Operator, second: String, boolean: WhereBoolean = .and) -> Self

Parameters

  • first: The first column to match against.
  • op: The Operator to be used in the comparison.
  • second: The second column to match against.
  • boolean: How the clause should be appended (.and or .or).

Returns

The current query builder Query to chain future queries to.

orWhereColumn(first:op:second:)

A helper for adding an or whereColumn clause.

public func orWhereColumn(first: String, op: Operator, second: String) -> Self

Parameters

  • first: The first column to match against.
  • op: The Operator to be used in the comparison.
  • second: The second column to match against.

Returns

The current query builder Query to chain future queries to.

whereNull(key:boolean:not:)

Add a where clause requiring that a column be null.

public func whereNull(key: String, boolean: WhereBoolean = .and, not: Bool = false) -> Self

Parameters

  • key: The column to match against.
  • boolean: How the clause should be appended (.and or .or).
  • not: Should the value be null or not null.

Returns

The current query builder Query to chain future queries to.

orWhereNull(key:)

A helper for adding an or whereNull clause.

public func orWhereNull(key: String) -> Self

Parameters

  • key: The column to match against.

Returns

The current query builder Query to chain future queries to.

whereNotNull(key:boolean:)

Add a where clause requiring that a column not be null.

public func whereNotNull(key: String, boolean: WhereBoolean = .and) -> Self

Parameters

  • key: The column to match against.
  • boolean: How the clause should be appended (.and or .or).

Returns

The current query builder Query to chain future queries to.

orWhereNotNull(key:)

A helper for adding an or whereNotNull clause.

public func orWhereNotNull(key: String) -> Self

Parameters

  • key: The column to match against.

Returns

The current query builder Query to chain future queries to.

having(_:)

Add a having clause to filter results from aggregate functions.

public func having(_ clause: WhereValue) -> Self

Parameters

  • clause: A WhereValue clause matching a column to a value.

Returns

The current query builder Query to chain future queries to.

orHaving(_:)

An alias for having(_ clause:​) that appends an or clause instead of an and clause.

public func orHaving(_ clause: WhereValue) -> Self

Parameters

  • clause: A WhereValue clause matching a column to a value.

Returns

The current query builder Query to chain future queries to.

having(key:op:value:boolean:)

Add a having clause to filter results from aggregate functions that matches a given key to a provided value.

public func having(key: String, op: Operator, value: Parameter, boolean: WhereBoolean = .and) -> Self

Parameters

  • key: The column to match against.
  • op: The Operator to be used in the comparison.
  • value: The value that the column should match.
  • boolean: How the clause should be appended (.and or .or).

Returns

The current query builder Query to chain future queries to.

groupBy(_:)

Group returned data by a given column.

public func groupBy(_ group: String) -> Self

Parameters

  • group: The table column to group data on.

Returns

The current query builder Query to chain future queries to.

orderBy(_:)

Order the data from the query based on given clause.

public func orderBy(_ order: OrderClause) -> Self

Parameters

  • order: The OrderClause that defines the ordering.

Returns

The current query builder Query to chain future queries to.

orderBy(column:direction:)

Order the data from the query based on a column and direction.

public func orderBy(column: Column, direction: OrderClause.Sort = .asc) -> Self

Parameters

  • column: The column to order data by.
  • direction: The OrderClause.Sort direction (either .asc or .desc). Defaults to .asc.

Returns

The current query builder Query to chain future queries to.

distinct()

Set query to only return distinct entries.

public func distinct() -> Self

Returns

The current query builder Query to chain future queries to.

offset(_:)

Offset the returned results by a given amount.

public func offset(_ value: Int) -> Self

Parameters

  • value: An amount representing the offset.

Returns

The current query builder Query to chain future queries to.

limit(_:)

Limit the returned results to a given amount.

public func limit(_ value: Int) -> Self

Parameters

  • value: An amount to cap the total result at.

Returns

The current query builder Query to chain future queries to.

forPage(_:perPage:)

A helper method to be used when needing to page returned results. Internally this uses the limit and offset methods.

public func forPage(_ page: Int, perPage: Int = 25) -> Self

Parameters

  • page: What page of results to offset by.
  • perPage: How many results to show on each page. Defaults to 25.

Returns

The current query builder Query to chain future queries to.

get(_:)

Run a select query and return the database rows.

public func get(_ columns: [Column]? = nil) -> EventLoopFuture<[DatabaseRow]>

Parameters

  • columns: The columns you would like returned. Defaults to nil.

Returns

An EventLoopFuture to be run that contains the returned rows from the database.

first(_:)

Run a select query and return the first database row only row.

public func first(_ columns: [Column]? = nil) -> EventLoopFuture<DatabaseRow?>

Parameters

  • columns: The columns you would like returned. Defaults to nil.

Returns

An EventLoopFuture to be run that contains the returned row from the database.

find(field:columns:)

Run a select query that looks for a single row matching the given database column and value.

public func find(field: DatabaseField, columns: [Column]? = nil) -> EventLoopFuture<DatabaseRow?>

Parameters

  • columns: The columns you would like returned. Defaults to nil.

Returns

An EventLoopFuture to be run that contains the returned row from the database.

count(column:as:)

Find the total count of the rows that match the given query.

public func count(column: Column = "*", as name: String? = nil) -> EventLoopFuture<Int?>

Parameters

  • column: What column to count. Defaults to *.
  • name: The alias that can be used for renaming the returned count.

Returns

An EventLoopFuture to be run that contains the returned count value.

insert(_:)

Perform an insert and create a database row from the provided data.

public func insert(_ value: OrderedDictionary<String, Parameter>) -> EventLoopFuture<[DatabaseRow]>

Parameters

  • value: A dictionary containing the values to be inserted.

Returns

An EventLoopFuture to be run that contains the inserted rows.

insert(_:)

Perform an insert and create database rows from the provided data.

public func insert(_ values: [OrderedDictionary<String, Parameter>]) -> EventLoopFuture<[DatabaseRow]>

Parameters

  • value: An array of dictionaries containing the values to be inserted.

Returns

An EventLoopFuture to be run that contains the inserted rows.

update(values:)

Perform an update on all data matching the query in the builder with the values provided.

public func update(values: [String: Parameter]) throws -> EventLoopFuture<[DatabaseRow]>

For example, if you wanted to update the first name of a user whose ID equals 10, you could do so as follows:

Query
    .table("users")
    .where("id" == 10)
    .update(values: [
        "first_name": "Ashley"
    ])

Parameters

  • values: An dictionary containing the values to be updated.

Returns

An EventLoopFuture to be run that will update all matched rows.

delete()

Perform a deletion on all data matching the given query.

public func delete() -> EventLoopFuture<[DatabaseRow]>

Returns

An EventLoopFuture to be run that will delete all matched rows.

table(_:)

Shortcut for running a query with the given table on Services.db.

public static func table(_ table: String) -> Query

Parameters

  • table: The table to run the query on.

Returns

The current query builder Query to chain future queries to.

from(table:as:)

Shortcut for running a query with the given table on Services.db.

public static func from(table: String, as alias: String? = nil) -> Query

An alias for table(_ table: String) to be used when running a select query that also lets you alias the table name.

Parameters

  • table: The table to select data from.
  • alias: An alias to use in place of table name. Defaults to nil.

Returns

The current query builder Query to chain future queries to.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally