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

Adds a value to the bind parameters of the query.
Sets the function that is called once the query has been run.
Adds a column definition to a 'create' or 'change' query.
Escapes a value for use inside an SQL string literal.
Builds the SQL string of the query and hands it to the adapter, along with the values of its bind parameters.
Changes the table the query operates on.
Appends the NOT NULL / DEFAULT clauses to the definition of the column that is being created.
Sets whether a 'drop' query ignores a missing table (DROP TABLE IF EXISTS) instead of failing.
Sets whether a 'create' query leaves an existing table alone (CREATE TABLE IF NOT EXISTS) instead of failing.
Creates an empty query.
Sets the value of a column for an 'insert' query.
Limits the amount of rows affected by a 'select' or 'delete' query.
Sets the amount of rows that a 'select' query skips.
Sets the function that is called if the query fails.
Adds a column to the ORDER BY clause.
Sets whether a 'create' query drops an existing table first.
Turns a value into an escaped, quoted SQL string literal.
Quotes an identifier such as a table or column name.
Marks a column to be dropped by a 'change' query.
Marks a column to be renamed by a 'change' query.
Adds a column to the list of columns to select.
Sets the column used for the PRIMARY KEY clause of a 'create' query.
Sets the new value of a column for an 'update' query.
Adds a "column = value" condition.
Adds a "column = value" condition.
Adds a "column > value" condition.
Adds a "column >= value" condition.
Adds a "column LIKE pattern" condition.
Adds a "column < value" condition.
Adds a "column <= value" condition.
Adds a "column != value" condition.
Adds a "column NOT LIKE pattern" condition.
Adds a raw SQL condition.

Methods

ActiveRecord.Query:bind(value)

shared packages/active_record/lib/database/query.lua:98

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

ActiveRecord.Query:callback(callback)

shared packages/active_record/lib/database/query.lua:224

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

ActiveRecord.Query:create(key, value)

shared packages/active_record/lib/database/query.lua:281

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'

ActiveRecord.Query:escape(text)

shared packages/active_record/lib/database/query.lua:71

Escapes a value for use inside an SQL string literal.

Parameters

  • text Any value to escape, converted to a string

Returns

  • String

ActiveRecord.Query:execute(queue_query)

shared packages/active_record/lib/database/query.lua:614

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 false put 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

ActiveRecord.Query:for_table(table_name)

shared packages/active_record/lib/database/query.lua:117

Changes the table the query operates on.

Parameters

  • table_name String

ActiveRecord.Query:handle_create_args(args)

shared packages/active_record/lib/database/query.lua:56

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)

ActiveRecord.Query:if_exists(if_exists)

shared packages/active_record/lib/database/query.lua:324

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:317

Sets 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:35

Creates 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'

ActiveRecord.Query:insert(key, value)

shared packages/active_record/lib/database/query.lua:263

Sets the value of a column for an 'insert' query.

Parameters

  • key String column name
  • value Any value to insert, converted to a string

ActiveRecord.Query:limit(value)

shared packages/active_record/lib/database/query.lua:295

Limits the amount of rows affected by a 'select' or 'delete' query.

Parameters

  • value Number

ActiveRecord.Query:offset(value)

shared packages/active_record/lib/database/query.lua:303

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

ActiveRecord.Query:on_error(callback)

shared packages/active_record/lib/database/query.lua:230

Sets the function that is called if the query fails. The error is reported as well.

Parameters

  • callback Function receives the error text

ActiveRecord.Query:order(key)

shared packages/active_record/lib/database/query.lua:207

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

ActiveRecord.Query:overwrite(overwrite)

shared packages/active_record/lib/database/query.lua:310

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

ActiveRecord.Query:quote(text)

shared packages/active_record/lib/database/query.lua:78

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

ActiveRecord.Query:quote_column(text)

shared packages/active_record/lib/database/query.lua:89

Quotes an identifier such as a table or column name.

Parameters

  • text Any identifier, converted to a string

Returns

  • String

ActiveRecord.Query:remove(field_name)

shared packages/active_record/lib/database/query.lua:245

Marks a column to be dropped by a 'change' query.

Parameters

  • field_name String column name

ActiveRecord.Query:rename(what, into)

shared packages/active_record/lib/database/query.lua:254

Marks a column to be renamed by a 'change' query.

Parameters

  • what String current column name
  • into String new column name

ActiveRecord.Query:select(field_name)

shared packages/active_record/lib/database/query.lua:237

Adds a column to the list of columns to select. All columns are selected if none are added.

Parameters

  • field_name String column name

ActiveRecord.Query:set_primary_key(key)

shared packages/active_record/lib/database/query.lua:289

Sets the column used for the PRIMARY KEY clause of a 'create' query.

Parameters

  • key String column name

ActiveRecord.Query:update(key, value)

shared packages/active_record/lib/database/query.lua:272

Sets the new value of a column for an 'update' query.

Parameters

  • key String column name
  • value Any new value, converted to a string

ActiveRecord.Query:where(key, value)

shared packages/active_record/lib/database/query.lua:125

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

ActiveRecord.Query:where_equal(key, value)

shared packages/active_record/lib/database/query.lua:146

Adds a "column = value" condition.

Parameters

  • key String column name
  • value Any value to compare with, converted to a string

ActiveRecord.Query:where_gt(key, value)

shared packages/active_record/lib/database/query.lua:174

Adds a "column > value" condition.

Parameters

  • key String column name
  • value Any value to compare with, converted to a string

ActiveRecord.Query:where_gte(key, value)

shared packages/active_record/lib/database/query.lua:188

Adds a "column >= value" condition.

Parameters

  • key String column name
  • value Any value to compare with, converted to a string

ActiveRecord.Query:where_like(key, value)

shared packages/active_record/lib/database/query.lua:160

Adds a "column LIKE pattern" condition.

Parameters

  • key String column name
  • value String SQL LIKE pattern

ActiveRecord.Query:where_lt(key, value)

shared packages/active_record/lib/database/query.lua:181

Adds a "column < value" condition.

Parameters

  • key String column name
  • value Any value to compare with, converted to a string

ActiveRecord.Query:where_lte(key, value)

shared packages/active_record/lib/database/query.lua:195

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:153

Adds 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:167

Adds 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:137

Adds 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 nil values that are bound in place of the ? placeholders in the condition. Without it the question marks in the condition are left as they are

Defined in