execute, fetch_all and fetch_one¶
Escape hatches for SQL the query builder does not cover. All three functions take an SQL string and positional bind parameters (placeholders are ? on SQLite and $1, $2, ... on Postgres) and honor an active transaction() block. See the Raw SQL guide.
execute
async
¶
execute(sql: str, *args: Any, using: str | None = None, session: Any | None = None, autocommit: bool = False) -> int
Run one SQL statement and return the number of rows it changed.
It runs inside the enclosing transaction() block, if any. Outside one,
two calls may run on different pooled connections; wrap statements that
need one connection (SET LOCAL, an advisory lock) in transaction().
See Raw SQL.
autocommit=True drops the session's settings
Under the default settings delivery, an autocommit statement
carries none of the session's settings, so a row-security policy
sees no tenant while it runs. Use it for maintenance statements,
never for statements that read or write rows a policy protects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL statement, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order: |
()
|
using
|
str | None
|
The connection to run on, outside a session. Defaults to
|
None
|
session
|
Any | None
|
The session to run in. Defaults to |
None
|
autocommit
|
bool
|
Run the statement outside the transaction Ferro opens for
each operation in a session with settings, for the Postgres
statements that refuse one ( |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The number of rows the statement changed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
No session is open and neither |
Examples:
from ferro import execute
changed = await execute('UPDATE "post" SET "views" = "views" + 1 WHERE "published" = ?', True)
assert changed == 2
- Guide Raw SQL
-
Reference
Python API
transaction and Transaction
Transaction
fetch_all
async
¶
fetch_all(sql: str, *args: Any, using: str | None = None, session: Any | None = None) -> list[dict[str, Any]]
Run one SQL query and return every row as a dict.
It runs inside the enclosing transaction() block, if any. Values come
back as plain primitives: UUID, datetime and JSON columns as strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL statement, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order: |
()
|
using
|
str | None
|
The connection to run on, outside a session. Defaults to
|
None
|
session
|
Any | None
|
The session to run in. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
One dict per row, keyed by column name, in the query's order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
No session is open and neither |
Examples:
from ferro import fetch_all
rows = await fetch_all('SELECT "title", "views" FROM "post" WHERE "views" > ? ORDER BY "views"', 900)
assert rows == [
{"title": "The Education of a Computer", "views": 952},
{"title": "Notes on the Analytical Engine", "views": 1843},
]
- Guide Raw SQL
-
Reference
Python API
transaction and Transaction
Transaction
fetch_one
async
¶
fetch_one(sql: str, *args: Any, using: str | None = None, session: Any | None = None) -> dict[str, Any] | None
Run one SQL query and return its first row as a dict, or None.
It runs inside the enclosing transaction() block, if any. Ferro does
not add LIMIT 1; write it when more than one row can match, so the
database stops at the first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
One SQL statement, with |
required |
*args
|
Any
|
The values bound to the placeholders, in order: |
()
|
using
|
str | None
|
The connection to run on, outside a session. Defaults to
|
None
|
session
|
Any | None
|
The session to run in. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The first row, keyed by column name, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
TypeError
|
A value in |
RuntimeError
|
No session is open and neither |
Examples:
from ferro import fetch_one
row = await fetch_one('SELECT "name" FROM "author" WHERE "email" = ?', "ada@example.com")
assert row == {"name": "Ada Lovelace"}
assert await fetch_one('SELECT "name" FROM "author" WHERE "id" = ?', 99) is None
- Guide Raw SQL
-
Reference
Python API
transaction and Transaction
Transaction