-
Notifications
You must be signed in to change notification settings - Fork 14
Query
public class Query: Sequelizable
public init(database: Database)
Get the raw SQL
for a given query.
public func toSQL() -> SQL
A SQL
value wrapper containing the executable query and bindings.
Set the columns that should be returned by the query.
@discardableResult public func select(_ columns: [Column] = ["*"]) -> Self
- columns: An array of columns to be returned by the query. Defaults to
[*]
.
The current query builder Query
to chain future queries to.
Set the table to perform a query from.
public func table(_ table: String) -> Self
- table: The table to run the query on.
The current query builder Query
to chain future queries to.
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
- table: The table to select data from.
- alias: An alias to use in place of table name. Defaults to
nil
.
The current query builder Query
to chain future queries to.
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
- 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
.
The current query builder Query
to chain future queries to.
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
- 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.
The current query builder Query
to chain future queries to.
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
- 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.
The current query builder Query
to chain future queries to.
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
- 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.
The current query builder Query
to chain future queries to.
Add a basic where clause to the query to filter down results.
public func `where`(_ clause: WhereValue) -> Self
- clause: A
WhereValue
clause matching a column to a given value.
The current query builder Query
to chain future queries to.
An alias for where(_ clause: WhereValue)
that appends an or
clause instead of an and clause.
public func orWhere(_ clause: WhereValue) -> Self
- clause: A
WhereValue
clause matching a column to a given value.
The current query builder Query
to chain future queries to.
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)
- 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
.
The current query builder Query
to chain future queries to.
A helper for adding an or where
nested closure clause.
public func orWhere(_ closure: @escaping WhereNestedClosure) -> Self
- closure: A
WhereNestedClosure
that provides a nested query to attach nested where clauses to.
The current query builder Query
to chain future queries to.
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
- 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
.
The current query builder Query
to chain future queries to.
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
- 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
.
The current query builder Query
to chain future queries to.
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
- 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
.
The current query builder Query
to chain future queries to.
A helper for adding an or whereNot
clause.
public func orWhereNot(key: String, in values: [Parameter]) -> Self
- key: The column to match against.
- values: The values that the column should not match.
The current query builder Query
to chain future queries to.
Add a raw SQL where clause to your query.
public func whereRaw(sql: String, bindings: [Parameter], boolean: WhereBoolean = .and) -> Self
- 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
.
The current query builder Query
to chain future queries to.
A helper for adding an or whereRaw
clause.
public func orWhereRaw(sql: String, bindings: [Parameter]) -> Self
- sql: A string representing the SQL where clause to be run.
- bindings: Any variables for binding in the SQL.
The current query builder Query
to chain future queries to.
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
- 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
).
The current query builder Query
to chain future queries to.
A helper for adding an or whereColumn
clause.
public func orWhereColumn(first: String, op: Operator, second: String) -> Self
- first: The first column to match against.
- op: The
Operator
to be used in the comparison. - second: The second column to match against.
The current query builder Query
to chain future queries to.
Add a where clause requiring that a column be null.
public func whereNull(key: String, boolean: WhereBoolean = .and, not: Bool = false) -> Self
- 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.
The current query builder Query
to chain future queries to.
A helper for adding an or whereNull
clause.
public func orWhereNull(key: String) -> Self
- key: The column to match against.
The current query builder Query
to chain future queries to.
Add a where clause requiring that a column not be null.
public func whereNotNull(key: String, boolean: WhereBoolean = .and) -> Self
- key: The column to match against.
- boolean: How the clause should be appended (
.and
or.or
).
The current query builder Query
to chain future queries to.
A helper for adding an or whereNotNull
clause.
public func orWhereNotNull(key: String) -> Self
- key: The column to match against.
The current query builder Query
to chain future queries to.
Add a having clause to filter results from aggregate functions.
public func having(_ clause: WhereValue) -> Self
- clause: A
WhereValue
clause matching a column to a value.
The current query builder Query
to chain future queries to.
An alias for having(_ clause:)
that appends an or clause
instead of an and clause.
public func orHaving(_ clause: WhereValue) -> Self
- clause: A
WhereValue
clause matching a column to a value.
The current query builder Query
to chain future queries to.
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
- 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
).
The current query builder Query
to chain future queries to.
Group returned data by a given column.
public func groupBy(_ group: String) -> Self
- group: The table column to group data on.
The current query builder Query
to chain future queries to.
Order the data from the query based on given clause.
public func orderBy(_ order: OrderClause) -> Self
- order: The
OrderClause
that defines the ordering.
The current query builder Query
to chain future queries to.
Order the data from the query based on a column and direction.
public func orderBy(column: Column, direction: OrderClause.Sort = .asc) -> Self
- column: The column to order data by.
- direction: The
OrderClause.Sort
direction (either.asc
or.desc
). Defaults to.asc
.
The current query builder Query
to chain future queries to.
Set query to only return distinct entries.
public func distinct() -> Self
The current query builder Query
to chain future queries to.
Offset the returned results by a given amount.
public func offset(_ value: Int) -> Self
- value: An amount representing the offset.
The current query builder Query
to chain future queries to.
Limit the returned results to a given amount.
public func limit(_ value: Int) -> Self
- value: An amount to cap the total result at.
The current query builder Query
to chain future queries to.
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
- page: What
page
of results to offset by. - perPage: How many results to show on each page. Defaults to
25
.
The current query builder Query
to chain future queries to.
Run a select query and return the database rows.
public func get(_ columns: [Column]? = nil) -> EventLoopFuture<[DatabaseRow]>
- columns: The columns you would like returned. Defaults to
nil
.
An EventLoopFuture
to be run that contains the returned rows from the database.
Run a select query and return the first database row only row.
public func first(_ columns: [Column]? = nil) -> EventLoopFuture<DatabaseRow?>
- columns: The columns you would like returned. Defaults to
nil
.
An EventLoopFuture
to be run that contains the returned row from the database.
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?>
- columns: The columns you would like returned. Defaults to
nil
.
An EventLoopFuture
to be run that contains the returned row from the database.
Find the total count of the rows that match the given query.
public func count(column: Column = "*", as name: String? = nil) -> EventLoopFuture<Int?>
- column: What column to count. Defaults to
*
. - name: The alias that can be used for renaming the returned count.
An EventLoopFuture
to be run that contains the returned count value.
Perform an insert and create a database row from the provided data.
public func insert(_ value: OrderedDictionary<String, Parameter>) -> EventLoopFuture<[DatabaseRow]>
- value: A dictionary containing the values to be inserted.
An EventLoopFuture
to be run that contains the inserted rows.
Perform an insert and create database rows from the provided data.
public func insert(_ values: [OrderedDictionary<String, Parameter>]) -> EventLoopFuture<[DatabaseRow]>
- value: An array of dictionaries containing the values to be inserted.
An EventLoopFuture
to be run that contains the inserted rows.
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"
])
- values: An dictionary containing the values to be updated.
An EventLoopFuture
to be run that will update all matched rows.
Perform a deletion on all data matching the given query.
public func delete() -> EventLoopFuture<[DatabaseRow]>
An EventLoopFuture
to be run that will delete all matched rows.
Shortcut for running a query with the given table on
Services.db
.
public static func table(_ table: String) -> Query
- table: The table to run the query on.
The current query builder Query
to chain future queries to.
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.
- table: The table to select data from.
- alias: An alias to use in place of table name. Defaults to
nil
.
The current query builder Query
to chain future queries to.
Generated at 2021-01-13T22:24:59-0800 using swift-doc 1.0.0-beta.5.
Alchemy
Types
- AlterTableBuilder
- BCryptDigest
- BasicAuthMiddleware
- BcryptError
- BelongsToRelationship
- CORSMiddleware
- CORSMiddleware.AllowOriginSetting
- CORSMiddleware.Configuration
- ColumnType
- CreateColumn
- CreateColumnBuilder
- CreateIndex
- CreateTableBuilder
- DatabaseConfig
- DatabaseError
- DatabaseField
- DatabaseKeyMappingStrategy
- DatabaseValue
- DayUnit
- Env
- FrequencyTyped
- Grammar
- HTTPAuth
- HTTPAuth.Basic
- HTTPAuth.Bearer
- HTTPBody
- HTTPError
- HasManyRelationship
- HasOneRelationship
- HasRelationship
- HourUnit
- JoinClause
- JoinType
- Launch
- Log
- MIMEType
- MinuteUnit
- ModelQuery
- MySQLDatabase
- Operator
- OrderClause
- OrderClause.Sort
- OrderedDictionary
- PapyrusClientError
- PathParameter
- PathParameter.DecodingError
- PostgresDatabase
- Query
- Request
- Response
- Router
- RuneError
- SQL
- SQLJSON
- Scheduler
- Schema
- SecondUnit
- Services
- Socket
- StaticFileMiddleware
- StringLength
- Thread
- TokenAuthMiddleware
- WeekUnit
- Weekday
- WhereBoolean
- WhereColumn
- WhereIn
- WhereIn.InType
- WhereNested
- WhereRaw
- WhereValue