ActiveRecord.Database class
Part of Active Record
Entry point of the query builder. Each of its methods starts an ActiveRecord::Query
of one type (select, insert, update, delete, create, change, drop or truncate) on a
table; the query is then set up through its own methods and run with
ActiveRecord::Query#execute. Models and the schema statements are built on top of it,
so it is only needed directly for tables that have no model. The class is used as it
is, without creating objects of it.
It also creates and drops the database itself for the flux db:create and
flux db:drop tasks, and makes the adapter work off its queue of deferred queries.
Based off mysql from Alex Grist.
Summary
Methods
Methods
Starts an ALTER TABLE query. The returned query has a method for every column type of the current adapter, which adds a column of that type.
Parameters
- table_name String
Returns
- ActiveRecord::Query
See also
Starts a CREATE TABLE query. The returned query has a method for every column type of the current adapter (query:string, query:integer, ...).
Parameters
- table_name String
Returns
- ActiveRecord::Query
See also
Starts a DELETE query on a table.
Parameters
- table_name String
Returns
- ActiveRecord::Query
ActiveRecord.Database:destroy(_settings)
shared packages/active_record/lib/database/database.lua:172Disconnects the active adapter from the database.
Parameters
- _settings Map optional, defaults to
nilunused
Starts a DROP TABLE query.
Parameters
- table_name String
Returns
- ActiveRecord::Query
ActiveRecord.Database:drop_database(settings)
shared packages/active_record/lib/database/database.lua:150Drops the database named in the settings and disconnects the adapter afterward. Connects to the 'template1' database to do so.
Parameters
- settings Map database settings: host, user, port, password and database
Starts an INSERT query on a table.
local query = ActiveRecord.Database:insert('ar_metadata')
query:insert('key', 'version')
query:insert('value', 20190309120000)
query:execute()
Parameters
- table_name String
Returns
- ActiveRecord::Query
Starts a SELECT query on a table. Query methods do not chain; configure the query and then run it with #execute.
local query = ActiveRecord.Database:select('ar_metadata')
query:where('key', 'version')
query:limit(1)
query:callback(function(result, query_str, time)
if istable(result) and #result > 0 then
print(result[1].value)
end
end)
query:execute()
Parameters
- table_name String
Returns
- ActiveRecord::Query
Creates the database named in the settings and disconnects the adapter afterward. Only PostgreSQL is supported; on MySQL instructions are printed instead.
Parameters
- settings Map database settings: host, user, port, password and database
ActiveRecord.Database:truncate(table_name)
shared packages/active_record/lib/database/database.lua:77Starts a TRUNCATE TABLE query.
Parameters
- table_name String
Returns
- ActiveRecord::Query
Starts an UPDATE query on a table.
local query = ActiveRecord.Database:update('ar_metadata')
query:where('key', 'version')
query:update('value', 20190309120000)
query:execute()
Parameters
- table_name String
Returns
- ActiveRecord::Query