Base class of the database adapters. An adapter connects ActiveRecord to one kind of database: it opens the connection, runs raw SQL, escapes and quotes values, and tells the query builder about the SQL dialect (its types table maps the abstract column types such as 'string' to SQL types, placeholder gives the form of a bind parameter). It can run queries asynchronously or, in sync mode, block until the result is there, and it keeps a queue of deferred queries that is worked off one query a second.

The abstract adapter talks to no database; ActiveRecord::Adapters::Sqlite, ActiveRecord::Adapters::Mysqloo and ActiveRecord::Adapters::Pg override what their database needs. ActiveRecord.establish_connection creates the adapter named in the database settings and stores it in ActiveRecord.adapter. An adapter for another database is a class ActiveRecord::Adapters::<Name> that extends this one, placed in lib/adapters/<name>.lua.

Summary

Methods

Hook called by ActiveRecord::Query#execute before the SQL string is built, so that the adapter can adjust the query object.
Hook called by ActiveRecord::Query#execute once the SQL string is built.
Starts a transaction.
Raises an error if more rows refer to rows that do not exist through a foreign key than before, see #foreign_key_violations.
Commits the current transaction.
Connects to the database.
Checks whether or not the adapter is connected to a database.
Hook called after a column is added to a 'create' or 'change' query through one of the column type methods (t:string, t:integer, ...).
Stops enforcing foreign keys until #enable_foreign_keys is called.
Closes the database connection.
Enforces foreign keys again after #disable_foreign_keys.
Escapes a string for safe use inside an SQL string literal.
Counts the rows that refer to rows that do not exist through a foreign key, which can only be written while #disable_foreign_keys is in effect.
Checks whether the database enforces foreign keys on this connection right now.
Converts a unix timestamp to the date-time string that the adapter writes into date-time columns.
Returns the SQL dialect the adapter speaks.
Checks whether a transaction started with #begin_transaction is open.
init()shared
Resets the connection state, the sync flag and the query queue of a new adapter.
Checks whether the adapter talks to a MySQL database.
Checks whether the adapter talks to a PostgreSQL database.
Checks whether a query result contains at least one row.
Checks whether the adapter talks to an SQLite database.
is_sync()shared
Checks whether the adapter is in synchronous (blocking) query mode.
Called when the database connects successfully.
Called when the database connection fails.
Returns the placeholder that stands for a bind parameter in the SQL of the adapter.
Reports a failed query.
Puts a raw SQL string into the queue.
Escapes a string and wraps it in single quotes, producing an SQL string literal.
Quotes an identifier such as a table or column name.
Sets whether a failed query raises a Lua error, on top of being printed.
Calls the error callback of a failed query, if it has one, and reports the failure through #query_failed.
Rolls the current transaction back.
Checks whether the database can roll back schema changes (CREATE TABLE, ALTER TABLE and so on) made inside of a transaction.
Switches the adapter between synchronous (blocking) and asynchronous query mode.
think()shared
Runs the oldest queued query, if there is one.
Reverts #escape on a string that was read back from the database.
Waits until a condition is met, running what the asynchronous queries have to do once they are done in the meantime.

Methods

ActiveRecord.Adapters.Abstract:append_query(_query, _query_type, _queue)

shared packages/active_record/lib/adapters/abstract.lua:295

Hook called by ActiveRecord::Query#execute before the SQL string is built, so that the adapter can adjust the query object. Does nothing in the abstract adapter.

Parameters

  • _query ActiveRecord::Query
  • _query_type String lowercase query type, e.g. 'create'
  • _queue Boolean optional, defaults to nil whether the query will be queued instead of run right away

ActiveRecord.Adapters.Abstract:append_query_string(_query, _query_string, _query_type)

shared packages/active_record/lib/adapters/abstract.lua:303

Hook called by ActiveRecord::Query#execute once the SQL string is built. Adapters may return a string to replace it. Does nothing in the abstract adapter.

Parameters

  • _query ActiveRecord::Query
  • _query_string String generated SQL, nil if the query could not be built
  • _query_type String lowercase query type, e.g. 'insert'

ActiveRecord.Adapters.Abstract:begin_transaction()

shared packages/active_record/lib/adapters/abstract.lua:74

Starts a transaction.

ActiveRecord.Adapters.Abstract:check_foreign_keys(_table_name, _before)

shared packages/active_record/lib/adapters/abstract.lua:131

Raises an error if more rows refer to rows that do not exist through a foreign key than before, see #foreign_key_violations. Does nothing in the abstract adapter.

Parameters

  • _table_name String optional, defaults to nil only check the rows of this table
  • _before Map optional, defaults to nil what #foreign_key_violations returned before; these rows are not reported

ActiveRecord.Adapters.Abstract:commit_transaction()

shared packages/active_record/lib/adapters/abstract.lua:80

Commits the current transaction.

ActiveRecord.Adapters.Abstract:connect(_config, _on_connected)

shared packages/active_record/lib/adapters/abstract.lua:196

Connects to the database. The abstract implementation only marks the adapter as connected and never calls the callback; real adapters override it.

Parameters

  • _config Map database settings: host, user, password, port, database
  • _on_connected Function called with the adapter once the connection is ready

ActiveRecord.Adapters.Abstract:connected()

shared packages/active_record/lib/adapters/abstract.lua:377

Checks whether or not the adapter is connected to a database.

Returns

  • Boolean

ActiveRecord.Adapters.Abstract:create_column(_query, _column, _args, _obj, _type, _def)

shared packages/active_record/lib/adapters/abstract.lua:314

Hook called after a column is added to a 'create' or 'change' query through one of the column type methods (t:string, t:integer, ...). Does nothing in the abstract adapter.

Parameters

  • _query ActiveRecord::Query query the column was added to
  • _column String column name
  • _args Map column options, such as null and default
  • _obj ActiveRecord::Query object the column type method was generated for
  • _type String abstract column type, e.g. 'primary_key'
  • _def String adapter-specific SQL type definition

ActiveRecord.Adapters.Abstract:disable_foreign_keys()

shared packages/active_record/lib/adapters/abstract.lua:109

Stops enforcing foreign keys until #enable_foreign_keys is called. The migrator does so while a migration runs on a database whose schema changes rebuild tables, as those of SQLite do, since dropping a table that is referenced would otherwise delete the rows that refer to it. It has to be called outside of a transaction. The abstract adapter leaves the enforcement on.

Returns

  • Boolean whether the enforcement was on and has been turned off

ActiveRecord.Adapters.Abstract:disconnect(_config)

shared packages/active_record/lib/adapters/abstract.lua:203

Closes the database connection. The abstract implementation only clears the connected flag.

Parameters

  • _config Map optional, defaults to nil unused

ActiveRecord.Adapters.Abstract:enable_foreign_keys()

shared packages/active_record/lib/adapters/abstract.lua:115

Enforces foreign keys again after #disable_foreign_keys. Does nothing in the abstract adapter.

ActiveRecord.Adapters.Abstract:escape(str)

shared packages/active_record/lib/adapters/abstract.lua:211

Escapes a string for safe use inside an SQL string literal. The abstract implementation returns the string unchanged.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Abstract:foreign_key_violations(_table_name)

shared packages/active_record/lib/adapters/abstract.lua:122

Counts the rows that refer to rows that do not exist through a foreign key, which can only be written while #disable_foreign_keys is in effect.

Parameters

  • _table_name String optional, defaults to nil only count the rows of this table

Returns

  • Map numbers of such rows, keyed by the foreign key; empty in the abstract adapter

ActiveRecord.Adapters.Abstract:foreign_keys_enabled()

shared packages/active_record/lib/adapters/abstract.lua:99

Checks whether the database enforces foreign keys on this connection right now.

Returns

  • Boolean true, unless overridden by the adapter

ActiveRecord.Adapters.Abstract:format_datetime(unix_time)

shared packages/active_record/lib/adapters/abstract.lua:147

Converts a unix timestamp to the date-time string that the adapter writes into date-time columns.

Parameters

  • unix_time Number

Returns

  • String ISO 8601 in UTC, e.g. '2019-03-09T12:00:00Z', unless overridden

ActiveRecord.Adapters.Abstract:get_sql_std()

shared packages/active_record/lib/adapters/abstract.lua:170

Returns the SQL dialect the adapter speaks.

Returns

  • String 'abstract', 'mysql', 'postgresql' or 'sqlite'

ActiveRecord.Adapters.Abstract:in_transaction()

shared packages/active_record/lib/adapters/abstract.lua:93

Checks whether a transaction started with #begin_transaction is open.

Returns

  • Boolean

ActiveRecord.Adapters.Abstract:init()

shared packages/active_record/lib/adapters/abstract.lua:28

Resets the connection state, the sync flag and the query queue of a new adapter.

ActiveRecord.Adapters.Abstract:is_mysql()

shared packages/active_record/lib/adapters/abstract.lua:182

Checks whether the adapter talks to a MySQL database.

Returns

  • Boolean false, unless overridden by the adapter

ActiveRecord.Adapters.Abstract:is_postgres()

shared packages/active_record/lib/adapters/abstract.lua:176

Checks whether the adapter talks to a PostgreSQL database.

Returns

  • Boolean false, unless overridden by the adapter

ActiveRecord.Adapters.Abstract:is_result(result)

shared packages/active_record/lib/adapters/abstract.lua:336

Checks whether a query result contains at least one row.

Parameters

  • result Any query result

Returns

  • Boolean

ActiveRecord.Adapters.Abstract:is_sqlite()

shared packages/active_record/lib/adapters/abstract.lua:188

Checks whether the adapter talks to an SQLite database.

Returns

  • Boolean false, unless overridden by the adapter

ActiveRecord.Adapters.Abstract:is_sync()

shared packages/active_record/lib/adapters/abstract.lua:164

Checks whether the adapter is in synchronous (blocking) query mode.

Returns

  • Boolean

ActiveRecord.Adapters.Abstract:on_connected()

shared packages/active_record/lib/adapters/abstract.lua:342

Called when the database connects successfully. Runs ActiveRecord's startup in sync mode and fires the 'DatabaseConnected' hook.

ActiveRecord.Adapters.Abstract:on_connection_failed(error_text)

shared packages/active_record/lib/adapters/abstract.lua:360

Called when the database connection fails. Prints the error and fires the 'DatabaseConnectionFailed' hook.

Parameters

  • error_text String error reported by the database module

ActiveRecord.Adapters.Abstract:placeholder(_index)

shared packages/active_record/lib/adapters/abstract.lua:241

Returns the placeholder that stands for a bind parameter in the SQL of the adapter.

Parameters

  • _index Number position of the parameter in the list of bindings, starting at 1

Returns

  • String '?', unless overridden by the adapter

ActiveRecord.Adapters.Abstract:query_failed(query, error_text, raise)

shared packages/active_record/lib/adapters/abstract.lua:52

Reports a failed query. The error is printed, remembered in the last_error field and, if #raise_errors is on, raised as a Lua error.

Parameters

  • query String SQL that failed
  • error_text String error reported by the database
  • raise Boolean optional, defaults to true false to never raise, e.g. when called from a callback that the database module runs asynchronously

ActiveRecord.Adapters.Abstract:queue(query, callback, query_type, bindings, error_callback)

shared packages/active_record/lib/adapters/abstract.lua:284

Puts a raw SQL string into the queue. Queued queries are run one at a time by #think.

Parameters

  • query String SQL to run; anything that is not a string is ignored
  • callback Function optional, defaults to nil passed on to #raw_query
  • query_type String optional, defaults to nil passed on to #raw_query
  • bindings List optional, defaults to nil passed on to #raw_query
  • error_callback Function optional, defaults to nil passed on to #raw_query

ActiveRecord.Adapters.Abstract:quote(str)

shared packages/active_record/lib/adapters/abstract.lua:226

Escapes a string and wraps it in single quotes, producing an SQL string literal.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Abstract:quote_name(str)

shared packages/active_record/lib/adapters/abstract.lua:234

Quotes an identifier such as a table or column name. The abstract implementation returns the name unchanged.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Abstract:raise_errors(raise)

shared packages/active_record/lib/adapters/abstract.lua:41

Sets whether a failed query raises a Lua error, on top of being printed. The migrator turns this on while a migration runs, so that the migration is canceled on the first failed statement.

Parameters

  • raise Boolean

Returns

  • ActiveRecord::Adapters::Abstract(self)

ActiveRecord.Adapters.Abstract:raw_query(_query, _callback, _query_type, _bindings, _error_callback)

shared packages/active_record/lib/adapters/abstract.lua:256

Sends a raw SQL string to the database. Does nothing in the abstract adapter. Values are best kept out of the SQL itself and passed as bindings instead, which take the place of the #placeholder of the adapter in the query and need no escaping.

Parameters

  • _query String SQL to run
  • _callback Function optional, defaults to nil called with the result rows, the query string, the time the query took in seconds and the bindings
  • _query_type String optional, defaults to nil lowercase query type, e.g. 'select' or 'insert'
  • _bindings List optional, defaults to nil values of the bind parameters of the query, in the order of their placeholders; strings, numbers and booleans are supported. A query that is given bindings has to be a single statement
  • _error_callback Function optional, defaults to nil called with the error text if the query fails

ActiveRecord.Adapters.Abstract:report_failure(query, error_text, error_callback, raise)

shared packages/active_record/lib/adapters/abstract.lua:266

Calls the error callback of a failed query, if it has one, and reports the failure through #query_failed.

Parameters

  • query String SQL that failed
  • error_text String error reported by the database
  • error_callback Function optional, defaults to nil called with the error text
  • raise Boolean optional, defaults to true passed on to #query_failed

Returns

  • Any whatever #query_failed returns

ActiveRecord.Adapters.Abstract:rollback_transaction()

shared packages/active_record/lib/adapters/abstract.lua:86

Rolls the current transaction back.

ActiveRecord.Adapters.Abstract:supports_ddl_transactions()

shared packages/active_record/lib/adapters/abstract.lua:69

Checks whether the database can roll back schema changes (CREATE TABLE, ALTER TABLE and so on) made inside of a transaction. Migrations are wrapped in a transaction when it can.

Returns

  • Boolean false, unless overridden by the adapter

ActiveRecord.Adapters.Abstract:sync(sync)

shared packages/active_record/lib/adapters/abstract.lua:138

Switches the adapter between synchronous (blocking) and asynchronous query mode. In sync mode #raw_query waits for the result and returns what the query callback returns.

Parameters

  • sync Boolean true to make queries blocking

Returns

  • ActiveRecord::Adapters::Abstract(self)

ActiveRecord.Adapters.Abstract:think()

shared packages/active_record/lib/adapters/abstract.lua:318

Runs the oldest queued query, if there is one. Called once a second by a timer.

ActiveRecord.Adapters.Abstract:unescape(str)

shared packages/active_record/lib/adapters/abstract.lua:219

Reverts #escape on a string that was read back from the database. The abstract implementation returns the string unchanged.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Abstract:wait_until(condition, _timeout)

shared packages/active_record/lib/adapters/abstract.lua:158

Waits until a condition is met, running what the asynchronous queries have to do once they are done in the meantime. Adapters whose synchronous queries wait for the asynchronous ones started before them, as those of SQLite and MySQL do, do not need to wait, and the abstract adapter only checks the condition.

Parameters

  • condition Function returns true once there is nothing more to wait for
  • _timeout Number optional, defaults to 10 seconds after which to give up

Returns

  • Boolean whether the condition is met

Defined in