base
Common interface of the per-driver adapters that run bound statements directly on a native async driver connection.
Each driver has its own API (asyncpg fetch/execute with $1
placeholders, asyncmy and aiosqlite DBAPI-style cursors), so one adapter per
driver hides those differences behind fetch_all, execute,
execute_many and stream. The executor only talks to this interface.
DriverAdapter
Bases: ABC
Runs bound statements on one driver and returns results in the shape the SQLAlchemy path returns them (rows keyed by label, inserted pk, row count).
Every method takes raw, the pooled connection that SQLAlchemy's
connection is using, so native statements share its transaction. It
exposes two objects::
| Text Only | |
|---|---|
1 2 3 4 5 | |
driver_errors lists the exceptions the driver raises, so the executor
knows which ones to translate into SQLAlchemy exceptions.
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
__init__(dialect)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dialect
|
Dialect
|
dialect of the connected database |
required |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
62 63 64 65 66 67 | |
execute(raw, bound)
abstractmethod
async
Run an INSERT, UPDATE or DELETE and return what SQLAlchemy's path would: the primary key of an inserted row, otherwise the number of rows changed::
| Text Only | |
|---|---|
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
pooled raw connection |
required |
bound
|
BoundStatement
|
statement to execute |
required |
Returns:
| Type | Description |
|---|---|
Any
|
inserted primary key for INSERT, row count otherwise |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
execute_many(raw, batch)
abstractmethod
async
Run one statement once per row of arguments with the driver's
executemany, used by bulk_update::
| Text Only | |
|---|---|
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
pooled raw connection |
required |
batch
|
BoundBatch
|
statement and arguments per row |
required |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
fetch_all(raw, bound, as_tuples=False)
abstractmethod
async
Run a statement and return all its rows keyed by result label, or as
tuples of values in select order with as_tuples::
| Text Only | |
|---|---|
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
pooled raw connection |
required |
bound
|
BoundStatement
|
statement to execute |
required |
as_tuples
|
bool
|
return tuples of values in select order instead |
False
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
rows |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
resolve_processors(bound, coltypes)
Look up the plan's result processors on its first execution, once the
driver has reported the result column types (see
resolve_result_processors for what they are). Later executions reuse
them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bound
|
BoundStatement
|
executed statement |
required |
coltypes
|
List[Any]
|
driver-reported column types |
required |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
start_transaction(sa_connection)
async
Make sure the transaction SQLAlchemy began is really open on the driver connection before native statements run on it.
Some SQLAlchemy adapters only send BEGIN lazily with their own first
statement; native statements bypass that, so without this they would run
outside the transaction. No-op for drivers that start it on their own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sa_connection
|
AsyncConnection
|
connection the transaction was begun on |
required |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
145 146 147 148 149 150 151 152 153 154 155 156 | |
stream(raw, bound, batch_size)
abstractmethod
Yield the rows of a statement one by one while fetching batch_size
rows per round trip, so QuerySet.iterate() never holds the whole
result in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
pooled raw connection |
required |
bound
|
BoundStatement
|
statement to execute |
required |
batch_size
|
int
|
rows fetched per round trip |
required |
Returns:
| Type | Description |
|---|---|
AsyncGenerator[Dict[str, Any], None]
|
rows keyed by result label |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
translate_error(error, dbapi_connection)
Return the exception SQLAlchemy's driver wrapper would raise for this
driver error, which sqlalchemy.exc.DBAPIError.instance then wraps.
SQLAlchemy maps driver exceptions to standard DBAPI classes before wrapping them, so users catch the same exception on both paths::
| Text Only | |
|---|---|
1 2 | |
Drivers that already raise DBAPI classes return the error unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
BaseException
|
exception raised by the driver |
required |
dbapi_connection
|
Any
|
SQLAlchemy's adapted DBAPI connection |
required |
Returns:
| Type | Description |
|---|---|
BaseException
|
DBAPI-level exception |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
execute_mutex(raw)
Return the lock SQLAlchemy's driver wrapper uses to run one statement at a time on a connection, or a no-op when the wrapper has none.
asyncpg and asyncmy fail when two statements run on one connection at
once (e.g. two tasks sharing a transaction with asyncio.gather).
Taking the same lock as SQLAlchemy serializes native statements with each
other and with statements SQLAlchemy runs::
| Text Only | |
|---|---|
1 2 | |
aiosqlite has no lock; it runs calls one by one on its own thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
pooled raw connection |
required |
Returns:
| Type | Description |
|---|---|
AsyncContextManager[Any]
|
async lock or a no-op context manager |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
first_value(bound, row)
Return the first column of a RETURNING row (the inserted primary key),
converted by its result processor, e.g. a UUID primary key generated by
the database comes back as uuid.UUID and not as the driver's value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bound
|
BoundStatement
|
executed statement, with resolved result processors |
required |
row
|
Any
|
returned row, or None when no row came back |
required |
Returns:
| Type | Description |
|---|---|
Any
|
converted first value, or None |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
inserted_primary_key(bound, returned, lastrowid)
Return the primary key of an executed INSERT, checked in the order
SQLAlchemy's inserted_primary_key uses, so save() gets the same pk
on both paths:
- the pk the caller bound:
insert().values(id=3)-> 3 - the value of
RETURNING users.id(PostgreSQL, SQLite) - the driver's
lastrowid(MySQL auto increment)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bound
|
BoundStatement
|
executed insert |
required |
returned
|
Any
|
first column of the RETURNING row, if any |
required |
lastrowid
|
Optional[int]
|
driver's last inserted row id |
required |
Returns:
| Type | Description |
|---|---|
Any
|
primary key or None when it cannot be recovered |
Source code in ormar/databases/native/adapters/base.py
| Python | |
|---|---|
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |