ActiveRecord.Query class
Part of Active Record
A single SQL query under construction. A query is created through
ActiveRecord::Database for one table and one type; its methods collect conditions,
values, ordering and column definitions, and execute builds the SQL in the dialect of
the current adapter and hands it over to be run. The methods return nothing, so calls
are not chained. Values given to where, insert and update are sent to the
database as bind parameters, apart from the SQL, and need no escaping.
'create' and 'change' queries also get a method for every column type of the adapter
(query:string, query:integer and so on) along with timestamps and references:
they are the t that create_table and change_table hand to their callback.
Based off mysql from Alex Grist.
Summary
Methods
Methods
Adds a value to the bind parameters of the query. The value is sent to the database apart from the SQL, so nothing in it has to be escaped.
Parameters
- value Any value to bind, converted to a string; booleans become '1' and '0'
Returns
- String placeholder that stands for the value in the SQL; NULL if the value is nil
Sets the function that is called once the query has been run.
Parameters
- callback Function receives the result rows, the SQL string, the time the query took in seconds and the List of values bound to its placeholders
Adds a column definition to a 'create' or 'change' query.
Parameters
- key String column name
- value String SQL definition of the column, e.g. 'varchar(255) NOT NULL'
Escapes a value for use inside an SQL string literal.
Parameters
- text Any value to escape, converted to a string
Returns
- String
Builds the SQL string of the query and hands it to the adapter, along with the values of its bind parameters.
Parameters
- queue_query Boolean optional, defaults to
falseput the query into the adapter's queue instead of running it right away
Returns
- Any whatever the query callback returns if the adapter ran the query synchronously, nothing otherwise
Changes the table the query operates on.
Parameters
- table_name String
Appends the NOT NULL / DEFAULT clauses to the definition of the column that is being created.
Parameters
- args Map column options: null (Boolean) and default (inserted into the SQL as is)
Sets whether a 'drop' query ignores a missing table (DROP TABLE IF EXISTS) instead of failing.
Parameters
- if_exists Boolean
ActiveRecord.Query:if_not_exists(if_not_exists)
shared packages/active_record/lib/database/query.lua:317Sets whether a 'create' query leaves an existing table alone (CREATE TABLE IF NOT EXISTS) instead of failing.
Parameters
- if_not_exists Boolean
ActiveRecord.Query:init(table_name, query_type)
shared packages/active_record/lib/database/query.lua:35Creates an empty query. Queries are normally created through ActiveRecord::Database.
Parameters
- table_name String
- query_type String 'select', 'insert', 'update', 'delete', 'drop', 'truncate', 'create' or 'change'
Sets the value of a column for an 'insert' query.
Parameters
- key String column name
- value Any value to insert, converted to a string
Limits the amount of rows affected by a 'select' or 'delete' query.
Parameters
- value Number
Sets the amount of rows that a 'select' query skips. Without #limit every row after them is returned; the query then carries the largest LIMIT every database accepts, since SQLite and MySQL do not take an OFFSET on its own.
Parameters
- value Number
Sets the function that is called if the query fails. The error is reported as well.
Parameters
- callback Function receives the error text
Adds a column to the ORDER BY clause.
query:order('id') -- ORDER BY id ASC
query:order({ asc = 'name' }) -- ORDER BY name ASC
query:order({ desc = 'name' }) -- ORDER BY name DESC
Parameters
- key String/Map column name (sorted in ascending order), or a hash with the column name stored under the 'asc' or 'desc' key
Sets whether a 'create' query drops an existing table first. Without it, and without #if_not_exists, creating a table that exists already fails.
Parameters
- overwrite Boolean
Turns a value into an escaped, quoted SQL string literal.
Parameters
- text Any value to quote, converted to a string; nil becomes NULL
Returns
- String
Quotes an identifier such as a table or column name.
Parameters
- text Any identifier, converted to a string
Returns
- String
Marks a column to be dropped by a 'change' query.
Parameters
- field_name String column name
Marks a column to be renamed by a 'change' query.
Parameters
- what String current column name
- into String new column name
Adds a column to the list of columns to select. All columns are selected if none are added.
Parameters
- field_name String column name
Sets the column used for the PRIMARY KEY clause of a 'create' query.
Parameters
- key String column name
Sets the new value of a column for an 'update' query.
Parameters
- key String column name
- value Any new value, converted to a string
Adds a "column = value" condition. Multiple conditions are joined with AND.
Parameters
- key String column name
- value Any value to compare with, converted to a string
See also
Adds a "column = value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
Adds a "column > value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
Adds a "column >= value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
Adds a "column LIKE pattern" condition.
Parameters
- key String column name
- value String SQL LIKE pattern
Adds a "column < value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
Adds a "column <= value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
ActiveRecord.Query:where_not_equal(key, value)
shared packages/active_record/lib/database/query.lua:153Adds a "column != value" condition.
Parameters
- key String column name
- value Any value to compare with, converted to a string
ActiveRecord.Query:where_not_like(key, value)
shared packages/active_record/lib/database/query.lua:167Adds a "column NOT LIKE pattern" condition.
Parameters
- key String column name
- value String SQL LIKE pattern
ActiveRecord.Query:where_raw(condition, values)
shared packages/active_record/lib/database/query.lua:137Adds a raw SQL condition. Nothing in it is escaped or quoted.
query:where_raw('money > 100')
query:where_raw('money > ? AND name != ?', { 100, 'John' })
Parameters
- condition String SQL condition
- values List optional, defaults to
nilvalues that are bound in place of the ? placeholders in the condition. Without it the question marks in the condition are left as they are