Column
is a class within the sqlalchemy.sql.schema
module of the SQLAlchemy project.
SchemaItem
is another callable from the sqlalchemy.sql.schema
package with code examples.
databases (project homepage and PyPI page provides asyncio support with an SQLALchemy Core interface for common relational databases such as MySQL, PostgreSQL and SQLite. This is handy for integrating with asynchronous I/O web frameworks like Sanic. The project is open sourced under the BSD 3-Clause "New" or "Revised" License.
databases / databases / backends / postgres.py
# postgres.py
import logging
import typing
from collections.abc import Mapping
import asyncpg
from sqlalchemy.dialects.postgresql import pypostgresql
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.schema import Column
from sqlalchemy.types import TypeEngine
from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend
logger = logging.getLogger("databases")
_result_processors = {} # type: dict
class PostgresBackend(DatabaseBackend):
def __init__(
self, database_url: typing.Union[DatabaseURL, str], **options: typing.Any
) -> None:
self._database_url = DatabaseURL(database_url)
self._options = options
self._dialect = self._get_dialect()
self._pool = None
def _get_dialect(self) -> Dialect:
dialect = pypostgresql.dialect(paramstyle="pyformat")
dialect.implicit_returning = True
## ... source file abbreviated to get to Column examples ...
"_column_map_full",
)
def __init__(
self,
row: asyncpg.Record,
result_columns: tuple,
dialect: Dialect,
column_maps: typing.Tuple[
typing.Mapping[typing.Any, typing.Tuple[int, TypeEngine]],
typing.Mapping[int, typing.Tuple[int, TypeEngine]],
typing.Mapping[str, typing.Tuple[int, TypeEngine]],
],
) -> None:
self._row = row
self._result_columns = result_columns
self._dialect = dialect
self._column_map, self._column_map_int, self._column_map_full = column_maps
def values(self) -> typing.ValuesView:
return self._row.values()
def __getitem__(self, key: typing.Any) -> typing.Any:
if len(self._column_map) == 0: # raw query
return self._row[key]
elif isinstance(key, Column):
idx, datatype = self._column_map_full[str(key)]
elif isinstance(key, int):
idx, datatype = self._column_map_int[key]
else:
idx, datatype = self._column_map[key]
raw = self._row[idx]
try:
processor = _result_processors[datatype]
except KeyError:
processor = datatype.result_processor(self._dialect, None)
_result_processors[datatype] = processor
if processor is not None:
return processor(raw)
return raw
def __iter__(self) -> typing.Iterator:
return iter(self._row.keys())
def __len__(self) -> int:
return len(self._row)
class PostgresConnection(ConnectionBackend):
## ... source file continues with no further Column examples...