Adapter for PostgreSQL servers, built on the pg binary module (gmsv_pg). It is used when the database settings name the 'pg' or 'postgresql' adapter. Queries run asynchronously unless the adapter is in sync mode, bind parameters are numbered ($1, $2 and so on), and insert queries get the id of the new row through RETURNING id. Schema changes can be rolled back, so migrations run in a transaction. It is the only adapter that flux db:create can create the database for.

Summary

Methods

Appends 'RETURNING id' to insert queries, so that the id of the new row is passed to the query callback.
Connects to a PostgreSQL server through the 'pg' module and sets the connection encoding.
Makes the column the primary key of the table when a 'primary_key' column is created.
Closes the PostgreSQL connection, if there is one.
Escapes a string using the PostgreSQL connection.
init()shared
Loads the 'pg' binary module.
Checks whether the adapter talks to a PostgreSQL database.
Returns the placeholder of a bind parameter, which is numbered in PostgreSQL.
Turns a string into an escaped SQL string literal using the PostgreSQL connection.
Quotes an identifier such as a table or column name using the PostgreSQL connection.
Runs a raw SQL query on the PostgreSQL server.
PostgreSQL can roll back schema changes.
Waits until a condition is met, delivering the results of the asynchronous queries in the meantime.

Methods

ActiveRecord.Adapters.Pg:append_query_string(query, query_string, query_type)

shared packages/active_record/lib/adapters/pg.lua:225

Appends 'RETURNING id' to insert queries, so that the id of the new row is passed to the query callback. Queries into tables without an id column set without_id on the query to go without it.

Parameters

  • query ActiveRecord::Query
  • query_string String generated SQL
  • query_type String lowercase query type

Returns

  • String the extended SQL for 'insert' queries, nil for other query types

ActiveRecord.Adapters.Pg:connect(config, on_connected)

shared packages/active_record/lib/adapters/pg.lua:50

Connects to a PostgreSQL server through the 'pg' module and sets the connection encoding. Calls #on_connection_failed if the connection fails.

Parameters

  • config Map database settings: host, user, password, database, port (5432 if omitted) and encoding ('UTF8' if omitted)
  • on_connected Function optional, defaults to nil called with the adapter once the connection is ready

ActiveRecord.Adapters.Pg:create_column(query, column, _args, _obj, type, _def)

shared packages/active_record/lib/adapters/pg.lua:212

Makes the column the primary key of the table when a 'primary_key' column is created.

Parameters

  • query ActiveRecord::Query query the column was added to
  • column String column name
  • _args Map unused
  • _obj ActiveRecord::Query unused
  • type String abstract column type
  • _def String unused

ActiveRecord.Adapters.Pg:disconnect()

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

Closes the PostgreSQL connection, if there is one.

ActiveRecord.Adapters.Pg:escape(str)

shared packages/active_record/lib/adapters/pg.lua:97

Escapes a string using the PostgreSQL connection. Requires an established connection.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Pg:init()

shared packages/active_record/lib/adapters/pg.lua:29

Loads the 'pg' binary module.

ActiveRecord.Adapters.Pg:is_postgres()

shared packages/active_record/lib/adapters/pg.lua:35

Checks whether the adapter talks to a PostgreSQL database.

Returns

  • Boolean always true

ActiveRecord.Adapters.Pg:placeholder(index)

shared packages/active_record/lib/adapters/pg.lua:118

Returns the placeholder of a bind parameter, which is numbered in PostgreSQL.

Parameters

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

Returns

  • String '$1', '$2' and so on

ActiveRecord.Adapters.Pg:quote(str)

shared packages/active_record/lib/adapters/pg.lua:104

Turns a string into an escaped SQL string literal using the PostgreSQL connection.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Pg:quote_name(str)

shared packages/active_record/lib/adapters/pg.lua:111

Quotes an identifier such as a table or column name using the PostgreSQL connection.

Parameters

  • str String

Returns

  • String

ActiveRecord.Adapters.Pg:raw_query(query, callback, query_type, bindings, error_callback)

shared packages/active_record/lib/adapters/pg.lua:132

Runs a raw SQL query on the PostgreSQL server. In sync mode this blocks until the query is done; without a connection the query is put into the queue instead.

Parameters

  • query String SQL to run
  • callback Function optional, defaults to nil called with the result rows (a List of row Maps), the query string, the time the query took in seconds and the bindings
  • query_type String optional, defaults to nil unused
  • bindings List optional, defaults to nil values of $1, $2 and so on in the query. 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

Returns

  • Any whatever the callback returns in sync mode, nothing otherwise

ActiveRecord.Adapters.Pg:supports_ddl_transactions()

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

PostgreSQL can roll back schema changes.

Returns

  • Boolean always true

ActiveRecord.Adapters.Pg:wait_until(condition, timeout)

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

Waits until a condition is met, delivering the results of the asynchronous queries in the meantime. A synchronous query of the pg module does not wait for the asynchronous queries that are queued before it, and the module delivers their results on the Think hook, which does not run while the server waits. Gives up after the timeout.

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