PostgreSQL

Some helper functions for connection pools in asyncpg.

class ezcord.sql.postgresql.QueryStatus(type, rowcount=0, inserts=0)[source]

A class to access the status of a PGHandler.exec() call.

type: str
rowcount: int = 0
inserts: int = 0
class ezcord.sql.postgresql.EzConnection(protocol, transport, loop, addr, config, params)[source]

A subclass of asyncpg.Connection that adds aliases to be compatible with the sqlite handler.

async one(sql, *args, default=None, **kwargs)[source]
async all(sql, *args, **kwargs)[source]
Return type:

list

async fetchval(sql, *args, default=None, **kwargs)[source]

Run a query and return a value in the first row.

Parameters:
  • query (str) – Query text.

  • args – Query arguments.

  • column (int) – Numeric index within the record of the value to return (defaults to 0).

  • timeout (float) – Optional timeout value in seconds. If not specified, defaults to the value of command_timeout argument to the Connection instance constructor.

Returns:

The value of the specified column of the first record, or None if no records were returned by the query.

async exec(sql, *args, **kwargs)[source]
Return type:

QueryStatus

async execute(*args, **kwargs)[source]

Alias for exec().

Return type:

QueryStatus

class ezcord.sql.postgresql.PGHandler(*, custom_pool=None, auto_setup=True, **kwargs)[source]

A class that provides helper methods for PostgreSQL databases.

Note

It’s recommended to set the database connection parameters in the .env file.

Parameters:
  • custom_pool (str | None) – Override the default connection pool with a key. Each custom pool has a unique key. Defaults to None.

  • auto_setup (bool) – Whether to call setup() when the first instance of this class is created. Defaults to True.

  • **kwargs – Keyword arguments for asyncpg.create_pool().

pool: Pool | None = None
async one(sql, *args, default=None, **kwargs)[source]

Returns one result record. If no record is found, None is returned.

Parameters:
  • sql (str) – The SQL query to execute.

  • *args – Arguments for the query.

  • default – When the query returns no results, this value will be returned instead of None.

Return type:

The result record or None.

async all(sql, *args, **kwargs)[source]

Returns all result records.

Parameters:
  • sql (str) – The SQL query to execute.

  • *args – Arguments for the query.

Return type:

A list of result records.

async fetchval(sql, *args, default=None, **kwargs)[source]

Returns one value.

Parameters:
  • sql (str) – The SQL query to execute.

  • *args – Arguments for the query.

  • default – When the query returns no results, this value will be returned instead of None.

Return type:

The value or None.

async exec(sql, *args, **kwargs)[source]

Executes a SQL query.

Parameters:
  • sql (str) – The SQL query to execute.

  • *args – Arguments for the query.

Return type:

QueryStatus

async execute(sql, *args, **kwargs)[source]

Alias for exec().

Return type:

QueryStatus

async executemany(sql, args, **kwargs)[source]

Executes a SQL multiquery.

Parameters:
Return type:

str