Classes for query objects.
Classes
BaseQuery (sess) |
A base query object. |
BulkDeleteQuery (sess) |
Represents a bulk delete query. |
BulkQuery (sess) |
Represents a bulk query. |
BulkUpdateQuery (sess) |
Represents a bulk update query. |
InsertQuery (sess) |
Represents an INSERT query. |
ResultGenerator (q) |
A helper class that will generate new results from a query when iterated over. |
RowDeleteQuery (sess) |
Represents a row deletion query. |
RowUpdateQuery (sess) |
Represents a row update query. |
SelectQuery (session) |
Represents a SELECT query, which fetches data from the database. |
UpsertQuery (sess, *columns, rows) |
Represents an UPSERT query. |
asyncqlio.orm.query.
BaseQuery
(sess)[source]¶Bases: asyncqlio.meta.AsyncABC
A base query object.
Parameters: | sess (Session ) – The Session associated with this query. |
---|
asyncqlio.orm.query.
ResultGenerator
(q)[source]¶Bases: collections.abc.AsyncIterator
A helper class that will generate new results from a query when iterated over.
Parameters: | q (SelectQuery ) – The SelectQuery to use. |
---|
asyncqlio.orm.query.
SelectQuery
(session)[source]¶Bases: asyncqlio.orm.query.BaseQuery
Represents a SELECT query, which fetches data from the database.
This is not normally created by user code directly, but rather as a result of a
Session.select()
call.
sess = db.get_session()
async with sess:
query = sess.select.from_(User) # query is instance of SelectQuery
# alternatively, but not recommended
query = sess.select(User)
However, it is possible to create this class manually:
query = SelectQuery(db.get_session()
query.set_table(User)
query.add_condition(User.id == 2)
user = await query.first()
table
= None¶The table being queried.
conditions
= None¶A list of conditions to fulfil.
row_limit
= None¶The limit on the number of rows returned from this query.
row_offset
= None¶The offset to start fetching rows from.
orderer
= None¶The column to order by.
map_columns
(results)[source]¶Maps columns in a result row to a Table
instance object.
Parameters: | results (Mapping [str , Any ]) – A single row of results from the query cursor. |
---|---|
Return type: | Table |
Returns: | A new Table instance that represents the row returned. |
map_many
(*rows)[source]¶Maps many records to one row.
This will group the records by the primary key of the main query table, then add additional columns as appropriate.
from_
(tbl)[source]¶Sets the table this query is selecting from.
Parameters: | tbl – The Table object to select. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
where
(*conditions)[source]¶Adds a WHERE clause to the query. This is a shortcut for SelectQuery.add_condition()
.
sess.select.from_(User).where(User.id == 1)
Parameters: | conditions (BaseOperator ) – The conditions to use for this WHERE clause. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
limit
(row_limit)[source]¶Sets a limit of the number of rows that can be returned from this query.
Parameters: | row_limit (int ) – The maximum number of rows to return. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
offset
(offset)[source]¶Sets the offset of rows to start returning results from/
Parameters: | offset (int ) – The row offset. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
order_by
(*col, sort_order='asc')[source]¶Sets the order by clause for this query.
The argument provided can either be a Column
, or a Sorter
which is
provided by Column.asc()
/ Column.desc()
. By default, asc
is used when
passing a column.
set_table
(tbl)[source]¶Sets the table to query on.
Parameters: | tbl – The Table object to set. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
add_condition
(condition)[source]¶Adds a condition to the query/
Parameters: | condition (BaseOperator ) – The BaseOperator to add. |
---|---|
Return type: | SelectQuery |
Returns: | This query. |
all
(self)[source]¶Gets all results that match from this query.
Return type: | ResultGenerator |
---|---|
Returns: | A ResultGenerator that can be iterated over. |
asyncqlio.orm.query.
InsertQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BaseQuery
Represents an INSERT query.
rows_to_insert
= None¶A list of rows to generate the insert statements for.
rows
(*rows)[source]¶Adds a set of rows to the query.
Parameters: | rows (Table ) – The rows to insert. |
---|---|
Return type: | InsertQuery |
Returns: | This query. |
add_row
(row)[source]¶Adds a row to this query, allowing it to be executed later.
Parameters: | row (Table ) – The Table instance to use for this query. |
---|---|
Return type: | InsertQuery |
Returns: | This query. |
on_conflict
(*columns)[source]¶Get an UpsertQuery
to react upon a conflict.
Parameters: | columns (Column ) – The Column objects upon which to check for a conflict. |
---|---|
Return type: | UpsertQuery |
asyncqlio.orm.query.
UpsertQuery
(sess, *columns, rows)[source]¶Bases: asyncqlio.orm.query.InsertQuery
Represents an UPSERT query.
New in version 0.2.0.
Parameters: |
---|
on_conflict
(*columns)[source]¶Add more conflict columns to this query.
Parameters: | column – The Column objects upon which to check for a conflict. |
---|---|
Return type: | UpsertQuery |
update
(*cols)[source]¶Used to specify which Column
objects to update on a conflict.
Parameters: | cols (Column ) – The Column objects to update. |
---|---|
Return type: | UpsertQuery |
nothing
()[source]¶Specify that this query should do nothing if there’s a conflict.
This is the default behavior.
Return type: | UpsertQuery |
---|
generate_sql
()[source]¶Generates the SQL statements for this upsert query.
Return type: | List [Tuple [str , tuple ]] |
---|---|
Returns: | A list of two-item tuples: - The SQL query to use - The params to use with the query |
add_row
(row)¶Adds a row to this query, allowing it to be executed later.
Parameters: | row (Table ) – The Table instance to use for this query. |
---|---|
Return type: | InsertQuery |
Returns: | This query. |
rows
(*rows)¶Adds a set of rows to the query.
Parameters: | rows (Table ) – The rows to insert. |
---|---|
Return type: | InsertQuery |
Returns: | This query. |
asyncqlio.orm.query.
BulkQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BaseQuery
Represents a bulk query.
This allows adding conditionals to the query.
conditions
= None¶The list of conditions to query by.
generate_sql
()¶Generates the SQL for this query.
:rtype: Tuple
[str
, Mapping
[str
, Any
]]
:return: A two item tuple, the SQL to use and a mapping of params to pass.
run
()¶Runs this query.
asyncqlio.orm.query.
BulkUpdateQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BulkQuery
Represents a bulk update query. This updates many rows based on certain criteria.
query = BulkUpdateQuery(session)
# style 1: manual
query.set_table(User)
query.add_condition(User.xp < 300)
# add on a value
query.set_update(User.xp + 100)
# or set a value
query.set_update(User.xp.set(300))
await query.run()
# style 2: builder
await query.table(User).where(User.xp < 300).set(User.xp + 100).run()
await query.table(User).where(User.xp < 300).set(User.xp, 300).run()
setting
= None¶The thing to set on the updated rows.
add_condition
(condition)¶Adds a condition to this query.
set_table
(table)¶Sets a table on this query.
table
(table)¶Sets the table for this query.
where
(*conditions)¶Sets the conditions for this query.
asyncqlio.orm.query.
BulkDeleteQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BulkQuery
Represents a bulk delete query. This deletes many rows based on criteria.
query = BulkDeleteQuery(session)
# style 1: manual
query.set_table(User)
query.add_condition(User.xp < 300)
await query.run()
# style 2: builder
await query.table(User).where(User.xp < 300).run()
await query.table(User).where(User.xp < 300).run()
generate_sql
()[source]¶Generates the SQL for this query. :return: A two item tuple, the SQL to use and a mapping of params to pass.
add_condition
(condition)¶Adds a condition to this query.
set_table
(table)¶Sets a table on this query.
table
(table)¶Sets the table for this query.
where
(*conditions)¶Sets the conditions for this query.
asyncqlio.orm.query.
RowUpdateQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BaseQuery
Represents a row update query. This is NOT a bulk update query - it is used for updating specific rows.
rows_to_update
= None¶The list of rows to update.
rows
(*rows)[source]¶Adds a set of rows to the query.
Parameters: | rows (Table ) – The rows to insert. |
---|---|
Return type: | RowUpdateQuery |
Returns: | This query. |
add_row
(row)[source]¶Adds a row to this query, allowing it to be executed later.
Parameters: | row (Table ) – The Table instance to use for this query. |
---|---|
Return type: | RowUpdateQuery |
Returns: | This query. |
asyncqlio.orm.query.
RowDeleteQuery
(sess)[source]¶Bases: asyncqlio.orm.query.BaseQuery
Represents a row deletion query. This is NOT a bulk delete query - it is used for deleting specific rows.
rows_to_delete
= None¶The list of rows to delete.
rows
(*rows)[source]¶Adds a set of rows to the query.
Parameters: | rows (Table ) – The rows to insert. |
---|---|
Return type: | RowDeleteQuery |
Returns: | This query. |