adapters
Per-driver adapters for native statement execution.
Only the drivers listed in ADAPTERS run natively; any other driver
(e.g. aiomysql, aiopg) keeps running every statement through SQLAlchemy.
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 | |
select_adapter(dialect)
Return the native adapter for the dialect's driver, or None when the driver has none and statements run through SQLAlchemy::
| Text Only | |
|---|---|
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dialect
|
Dialect
|
dialect of the connected database |
required |
Returns:
| Type | Description |
|---|---|
Optional[DriverAdapter]
|
adapter instance or None |
Source code in ormar/databases/native/adapters/__init__.py
| Python | |
|---|---|
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |