Загрузить файлы в «venv/Lib/site-packages/sqlalchemy/dialects/mssql»
This commit is contained in:
87
venv/Lib/site-packages/sqlalchemy/dialects/mssql/__init__.py
Normal file
87
venv/Lib/site-packages/sqlalchemy/dialects/mssql/__init__.py
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
# dialects/mssql/__init__.py
|
||||||
|
# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
|
||||||
|
# <see AUTHORS file>
|
||||||
|
#
|
||||||
|
# This module is part of SQLAlchemy and is released under
|
||||||
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||||
|
# mypy: ignore-errors
|
||||||
|
|
||||||
|
from . import aioodbc # noqa
|
||||||
|
from . import base # noqa
|
||||||
|
from . import pymssql # noqa
|
||||||
|
from . import pyodbc # noqa
|
||||||
|
from .base import BIGINT
|
||||||
|
from .base import BINARY
|
||||||
|
from .base import BIT
|
||||||
|
from .base import CHAR
|
||||||
|
from .base import DATE
|
||||||
|
from .base import DATETIME
|
||||||
|
from .base import DATETIME2
|
||||||
|
from .base import DATETIMEOFFSET
|
||||||
|
from .base import DECIMAL
|
||||||
|
from .base import DOUBLE_PRECISION
|
||||||
|
from .base import FLOAT
|
||||||
|
from .base import IMAGE
|
||||||
|
from .base import INTEGER
|
||||||
|
from .base import JSON
|
||||||
|
from .base import MONEY
|
||||||
|
from .base import NCHAR
|
||||||
|
from .base import NTEXT
|
||||||
|
from .base import NUMERIC
|
||||||
|
from .base import NVARCHAR
|
||||||
|
from .base import REAL
|
||||||
|
from .base import ROWVERSION
|
||||||
|
from .base import SMALLDATETIME
|
||||||
|
from .base import SMALLINT
|
||||||
|
from .base import SMALLMONEY
|
||||||
|
from .base import SQL_VARIANT
|
||||||
|
from .base import TEXT
|
||||||
|
from .base import TIME
|
||||||
|
from .base import TIMESTAMP
|
||||||
|
from .base import TINYINT
|
||||||
|
from .base import UNIQUEIDENTIFIER
|
||||||
|
from .base import VARBINARY
|
||||||
|
from .base import VARCHAR
|
||||||
|
from .base import XML
|
||||||
|
from ...sql import try_cast
|
||||||
|
|
||||||
|
base.dialect = dialect = pyodbc.dialect
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = (
|
||||||
|
"JSON",
|
||||||
|
"INTEGER",
|
||||||
|
"BIGINT",
|
||||||
|
"SMALLINT",
|
||||||
|
"TINYINT",
|
||||||
|
"VARCHAR",
|
||||||
|
"NVARCHAR",
|
||||||
|
"CHAR",
|
||||||
|
"NCHAR",
|
||||||
|
"TEXT",
|
||||||
|
"NTEXT",
|
||||||
|
"DECIMAL",
|
||||||
|
"NUMERIC",
|
||||||
|
"FLOAT",
|
||||||
|
"DATETIME",
|
||||||
|
"DATETIME2",
|
||||||
|
"DATETIMEOFFSET",
|
||||||
|
"DATE",
|
||||||
|
"DOUBLE_PRECISION",
|
||||||
|
"TIME",
|
||||||
|
"SMALLDATETIME",
|
||||||
|
"BINARY",
|
||||||
|
"VARBINARY",
|
||||||
|
"BIT",
|
||||||
|
"REAL",
|
||||||
|
"IMAGE",
|
||||||
|
"TIMESTAMP",
|
||||||
|
"ROWVERSION",
|
||||||
|
"MONEY",
|
||||||
|
"SMALLMONEY",
|
||||||
|
"UNIQUEIDENTIFIER",
|
||||||
|
"SQL_VARIANT",
|
||||||
|
"XML",
|
||||||
|
"dialect",
|
||||||
|
"try_cast",
|
||||||
|
)
|
||||||
63
venv/Lib/site-packages/sqlalchemy/dialects/mssql/aioodbc.py
Normal file
63
venv/Lib/site-packages/sqlalchemy/dialects/mssql/aioodbc.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# dialects/mssql/aioodbc.py
|
||||||
|
# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
|
||||||
|
# <see AUTHORS file>
|
||||||
|
#
|
||||||
|
# This module is part of SQLAlchemy and is released under
|
||||||
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||||
|
# mypy: ignore-errors
|
||||||
|
r"""
|
||||||
|
.. dialect:: mssql+aioodbc
|
||||||
|
:name: aioodbc
|
||||||
|
:dbapi: aioodbc
|
||||||
|
:connectstring: mssql+aioodbc://<username>:<password>@<dsnname>
|
||||||
|
:url: https://pypi.org/project/aioodbc/
|
||||||
|
|
||||||
|
|
||||||
|
Support for the SQL Server database in asyncio style, using the aioodbc
|
||||||
|
driver which itself is a thread-wrapper around pyodbc.
|
||||||
|
|
||||||
|
.. versionadded:: 2.0.23 Added the mssql+aioodbc dialect which builds
|
||||||
|
on top of the pyodbc and general aio* dialect architecture.
|
||||||
|
|
||||||
|
Using a special asyncio mediation layer, the aioodbc dialect is usable
|
||||||
|
as the backend for the :ref:`SQLAlchemy asyncio <asyncio_toplevel>`
|
||||||
|
extension package.
|
||||||
|
|
||||||
|
Most behaviors and caveats for this driver are the same as that of the
|
||||||
|
pyodbc dialect used on SQL Server; see :ref:`mssql_pyodbc` for general
|
||||||
|
background.
|
||||||
|
|
||||||
|
This dialect should normally be used only with the
|
||||||
|
:func:`_asyncio.create_async_engine` engine creation function; connection
|
||||||
|
styles are otherwise equivalent to those documented in the pyodbc section::
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||||||
|
|
||||||
|
engine = create_async_engine(
|
||||||
|
"mssql+aioodbc://scott:tiger@mssql2017:1433/test?"
|
||||||
|
"driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
|
||||||
|
)
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from .pyodbc import MSDialect_pyodbc
|
||||||
|
from .pyodbc import MSExecutionContext_pyodbc
|
||||||
|
from ...connectors.aioodbc import aiodbcConnector
|
||||||
|
|
||||||
|
|
||||||
|
class MSExecutionContext_aioodbc(MSExecutionContext_pyodbc):
|
||||||
|
def create_server_side_cursor(self):
|
||||||
|
return self._dbapi_connection.cursor(server_side=True)
|
||||||
|
|
||||||
|
|
||||||
|
class MSDialectAsync_aioodbc(aiodbcConnector, MSDialect_pyodbc):
|
||||||
|
driver = "aioodbc"
|
||||||
|
|
||||||
|
supports_statement_cache = True
|
||||||
|
|
||||||
|
execution_ctx_cls = MSExecutionContext_aioodbc
|
||||||
|
|
||||||
|
|
||||||
|
dialect = MSDialectAsync_aioodbc
|
||||||
4104
venv/Lib/site-packages/sqlalchemy/dialects/mssql/base.py
Normal file
4104
venv/Lib/site-packages/sqlalchemy/dialects/mssql/base.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,284 @@
|
|||||||
|
# dialects/mssql/information_schema.py
|
||||||
|
# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
|
||||||
|
# <see AUTHORS file>
|
||||||
|
#
|
||||||
|
# This module is part of SQLAlchemy and is released under
|
||||||
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||||
|
# mypy: ignore-errors
|
||||||
|
|
||||||
|
from ... import cast
|
||||||
|
from ... import Column
|
||||||
|
from ... import MetaData
|
||||||
|
from ... import Table
|
||||||
|
from ...ext.compiler import compiles
|
||||||
|
from ...sql import expression
|
||||||
|
from ...types import Boolean
|
||||||
|
from ...types import Integer
|
||||||
|
from ...types import Numeric
|
||||||
|
from ...types import NVARCHAR
|
||||||
|
from ...types import String
|
||||||
|
from ...types import TypeDecorator
|
||||||
|
from ...types import Unicode
|
||||||
|
|
||||||
|
ischema = MetaData()
|
||||||
|
|
||||||
|
|
||||||
|
class CoerceUnicode(TypeDecorator):
|
||||||
|
impl = Unicode
|
||||||
|
cache_ok = True
|
||||||
|
|
||||||
|
def bind_expression(self, bindvalue):
|
||||||
|
return _cast_on_2005(bindvalue)
|
||||||
|
|
||||||
|
|
||||||
|
class _cast_on_2005(expression.ColumnElement):
|
||||||
|
def __init__(self, bindvalue):
|
||||||
|
self.bindvalue = bindvalue
|
||||||
|
|
||||||
|
|
||||||
|
@compiles(_cast_on_2005)
|
||||||
|
def _compile(element, compiler, **kw):
|
||||||
|
from . import base
|
||||||
|
|
||||||
|
if (
|
||||||
|
compiler.dialect.server_version_info is None
|
||||||
|
or compiler.dialect.server_version_info < base.MS_2005_VERSION
|
||||||
|
):
|
||||||
|
return compiler.process(element.bindvalue, **kw)
|
||||||
|
else:
|
||||||
|
return compiler.process(cast(element.bindvalue, Unicode), **kw)
|
||||||
|
|
||||||
|
|
||||||
|
schemata = Table(
|
||||||
|
"SCHEMATA",
|
||||||
|
ischema,
|
||||||
|
Column("CATALOG_NAME", CoerceUnicode, key="catalog_name"),
|
||||||
|
Column("SCHEMA_NAME", CoerceUnicode, key="schema_name"),
|
||||||
|
Column("SCHEMA_OWNER", CoerceUnicode, key="schema_owner"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
tables = Table(
|
||||||
|
"TABLES",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("TABLE_TYPE", CoerceUnicode, key="table_type"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
columns = Table(
|
||||||
|
"COLUMNS",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("COLUMN_NAME", CoerceUnicode, key="column_name"),
|
||||||
|
Column("IS_NULLABLE", Integer, key="is_nullable"),
|
||||||
|
Column("DATA_TYPE", String, key="data_type"),
|
||||||
|
Column("ORDINAL_POSITION", Integer, key="ordinal_position"),
|
||||||
|
Column(
|
||||||
|
"CHARACTER_MAXIMUM_LENGTH", Integer, key="character_maximum_length"
|
||||||
|
),
|
||||||
|
Column("NUMERIC_PRECISION", Integer, key="numeric_precision"),
|
||||||
|
Column("NUMERIC_SCALE", Integer, key="numeric_scale"),
|
||||||
|
Column("COLUMN_DEFAULT", Integer, key="column_default"),
|
||||||
|
Column("COLLATION_NAME", String, key="collation_name"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
sys_columns = Table(
|
||||||
|
"columns",
|
||||||
|
ischema,
|
||||||
|
Column("object_id", Integer),
|
||||||
|
Column("name", CoerceUnicode),
|
||||||
|
Column("column_id", Integer),
|
||||||
|
Column("default_object_id", Integer),
|
||||||
|
Column("user_type_id", Integer),
|
||||||
|
Column("is_nullable", Integer),
|
||||||
|
Column("ordinal_position", Integer),
|
||||||
|
Column("max_length", Integer),
|
||||||
|
Column("precision", Integer),
|
||||||
|
Column("scale", Integer),
|
||||||
|
Column("collation_name", String),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
|
||||||
|
sys_types = Table(
|
||||||
|
"types",
|
||||||
|
ischema,
|
||||||
|
Column("name", CoerceUnicode, key="name"),
|
||||||
|
Column("system_type_id", Integer, key="system_type_id"),
|
||||||
|
Column("user_type_id", Integer, key="user_type_id"),
|
||||||
|
Column("schema_id", Integer, key="schema_id"),
|
||||||
|
Column("max_length", Integer, key="max_length"),
|
||||||
|
Column("precision", Integer, key="precision"),
|
||||||
|
Column("scale", Integer, key="scale"),
|
||||||
|
Column("collation_name", CoerceUnicode, key="collation_name"),
|
||||||
|
Column("is_nullable", Boolean, key="is_nullable"),
|
||||||
|
Column("is_user_defined", Boolean, key="is_user_defined"),
|
||||||
|
Column("is_assembly_type", Boolean, key="is_assembly_type"),
|
||||||
|
Column("default_object_id", Integer, key="default_object_id"),
|
||||||
|
Column("rule_object_id", Integer, key="rule_object_id"),
|
||||||
|
Column("is_table_type", Boolean, key="is_table_type"),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
|
||||||
|
constraints = Table(
|
||||||
|
"TABLE_CONSTRAINTS",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),
|
||||||
|
Column("CONSTRAINT_TYPE", CoerceUnicode, key="constraint_type"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
sys_default_constraints = Table(
|
||||||
|
"default_constraints",
|
||||||
|
ischema,
|
||||||
|
Column("object_id", Integer),
|
||||||
|
Column("name", CoerceUnicode),
|
||||||
|
Column("schema_id", Integer),
|
||||||
|
Column("parent_column_id", Integer),
|
||||||
|
Column("definition", CoerceUnicode),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
|
||||||
|
column_constraints = Table(
|
||||||
|
"CONSTRAINT_COLUMN_USAGE",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("COLUMN_NAME", CoerceUnicode, key="column_name"),
|
||||||
|
Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
key_constraints = Table(
|
||||||
|
"KEY_COLUMN_USAGE",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("COLUMN_NAME", CoerceUnicode, key="column_name"),
|
||||||
|
Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),
|
||||||
|
Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"),
|
||||||
|
Column("ORDINAL_POSITION", Integer, key="ordinal_position"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
ref_constraints = Table(
|
||||||
|
"REFERENTIAL_CONSTRAINTS",
|
||||||
|
ischema,
|
||||||
|
Column("CONSTRAINT_CATALOG", CoerceUnicode, key="constraint_catalog"),
|
||||||
|
Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"),
|
||||||
|
Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"),
|
||||||
|
# TODO: is CATLOG misspelled ?
|
||||||
|
Column(
|
||||||
|
"UNIQUE_CONSTRAINT_CATLOG",
|
||||||
|
CoerceUnicode,
|
||||||
|
key="unique_constraint_catalog",
|
||||||
|
),
|
||||||
|
Column(
|
||||||
|
"UNIQUE_CONSTRAINT_SCHEMA",
|
||||||
|
CoerceUnicode,
|
||||||
|
key="unique_constraint_schema",
|
||||||
|
),
|
||||||
|
Column(
|
||||||
|
"UNIQUE_CONSTRAINT_NAME", CoerceUnicode, key="unique_constraint_name"
|
||||||
|
),
|
||||||
|
Column("MATCH_OPTION", String, key="match_option"),
|
||||||
|
Column("UPDATE_RULE", String, key="update_rule"),
|
||||||
|
Column("DELETE_RULE", String, key="delete_rule"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
views = Table(
|
||||||
|
"VIEWS",
|
||||||
|
ischema,
|
||||||
|
Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),
|
||||||
|
Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
|
||||||
|
Column("TABLE_NAME", CoerceUnicode, key="table_name"),
|
||||||
|
Column("VIEW_DEFINITION", CoerceUnicode, key="view_definition"),
|
||||||
|
Column("CHECK_OPTION", String, key="check_option"),
|
||||||
|
Column("IS_UPDATABLE", String, key="is_updatable"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
computed_columns = Table(
|
||||||
|
"computed_columns",
|
||||||
|
ischema,
|
||||||
|
Column("object_id", Integer),
|
||||||
|
Column("name", CoerceUnicode),
|
||||||
|
Column("column_id", Integer),
|
||||||
|
Column("is_computed", Boolean),
|
||||||
|
Column("is_persisted", Boolean),
|
||||||
|
Column("definition", CoerceUnicode),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
|
||||||
|
sequences = Table(
|
||||||
|
"SEQUENCES",
|
||||||
|
ischema,
|
||||||
|
Column("SEQUENCE_CATALOG", CoerceUnicode, key="sequence_catalog"),
|
||||||
|
Column("SEQUENCE_SCHEMA", CoerceUnicode, key="sequence_schema"),
|
||||||
|
Column("SEQUENCE_NAME", CoerceUnicode, key="sequence_name"),
|
||||||
|
schema="INFORMATION_SCHEMA",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NumericSqlVariant(TypeDecorator):
|
||||||
|
r"""This type casts sql_variant columns in the identity_columns view
|
||||||
|
to numeric. This is required because:
|
||||||
|
|
||||||
|
* pyodbc does not support sql_variant
|
||||||
|
* pymssql under python 2 return the byte representation of the number,
|
||||||
|
int 1 is returned as "\x01\x00\x00\x00". On python 3 it returns the
|
||||||
|
correct value as string.
|
||||||
|
"""
|
||||||
|
|
||||||
|
impl = Unicode
|
||||||
|
cache_ok = True
|
||||||
|
|
||||||
|
def column_expression(self, colexpr):
|
||||||
|
return cast(colexpr, Numeric(38, 0))
|
||||||
|
|
||||||
|
|
||||||
|
identity_columns = Table(
|
||||||
|
"identity_columns",
|
||||||
|
ischema,
|
||||||
|
Column("object_id", Integer),
|
||||||
|
Column("name", CoerceUnicode),
|
||||||
|
Column("column_id", Integer),
|
||||||
|
Column("is_identity", Boolean),
|
||||||
|
Column("seed_value", NumericSqlVariant),
|
||||||
|
Column("increment_value", NumericSqlVariant),
|
||||||
|
Column("last_value", NumericSqlVariant),
|
||||||
|
Column("is_not_for_replication", Boolean),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NVarcharSqlVariant(TypeDecorator):
|
||||||
|
"""This type casts sql_variant columns in the extended_properties view
|
||||||
|
to nvarchar. This is required because pyodbc does not support sql_variant
|
||||||
|
"""
|
||||||
|
|
||||||
|
impl = Unicode
|
||||||
|
cache_ok = True
|
||||||
|
|
||||||
|
def column_expression(self, colexpr):
|
||||||
|
return cast(colexpr, NVARCHAR)
|
||||||
|
|
||||||
|
|
||||||
|
extended_properties = Table(
|
||||||
|
"extended_properties",
|
||||||
|
ischema,
|
||||||
|
Column("class", Integer), # TINYINT
|
||||||
|
Column("class_desc", CoerceUnicode),
|
||||||
|
Column("major_id", Integer),
|
||||||
|
Column("minor_id", Integer),
|
||||||
|
Column("name", CoerceUnicode),
|
||||||
|
Column("value", NVarcharSqlVariant),
|
||||||
|
schema="sys",
|
||||||
|
)
|
||||||
129
venv/Lib/site-packages/sqlalchemy/dialects/mssql/json.py
Normal file
129
venv/Lib/site-packages/sqlalchemy/dialects/mssql/json.py
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
# dialects/mssql/json.py
|
||||||
|
# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
|
||||||
|
# <see AUTHORS file>
|
||||||
|
#
|
||||||
|
# This module is part of SQLAlchemy and is released under
|
||||||
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||||
|
# mypy: ignore-errors
|
||||||
|
|
||||||
|
from ... import types as sqltypes
|
||||||
|
|
||||||
|
# technically, all the dialect-specific datatypes that don't have any special
|
||||||
|
# behaviors would be private with names like _MSJson. However, we haven't been
|
||||||
|
# doing this for mysql.JSON or sqlite.JSON which both have JSON / JSONIndexType
|
||||||
|
# / JSONPathType in their json.py files, so keep consistent with that
|
||||||
|
# sub-convention for now. A future change can update them all to be
|
||||||
|
# package-private at once.
|
||||||
|
|
||||||
|
|
||||||
|
class JSON(sqltypes.JSON):
|
||||||
|
"""MSSQL JSON type.
|
||||||
|
|
||||||
|
MSSQL supports JSON-formatted data as of SQL Server 2016.
|
||||||
|
|
||||||
|
The :class:`_mssql.JSON` datatype at the DDL level will represent the
|
||||||
|
datatype as ``NVARCHAR(max)``, but provides for JSON-level comparison
|
||||||
|
functions as well as Python coercion behavior.
|
||||||
|
|
||||||
|
:class:`_mssql.JSON` is used automatically whenever the base
|
||||||
|
:class:`_types.JSON` datatype is used against a SQL Server backend.
|
||||||
|
|
||||||
|
.. seealso::
|
||||||
|
|
||||||
|
:class:`_types.JSON` - main documentation for the generic
|
||||||
|
cross-platform JSON datatype.
|
||||||
|
|
||||||
|
The :class:`_mssql.JSON` type supports persistence of JSON values
|
||||||
|
as well as the core index operations provided by :class:`_types.JSON`
|
||||||
|
datatype, by adapting the operations to render the ``JSON_VALUE``
|
||||||
|
or ``JSON_QUERY`` functions at the database level.
|
||||||
|
|
||||||
|
The SQL Server :class:`_mssql.JSON` type necessarily makes use of the
|
||||||
|
``JSON_QUERY`` and ``JSON_VALUE`` functions when querying for elements
|
||||||
|
of a JSON object. These two functions have a major restriction in that
|
||||||
|
they are **mutually exclusive** based on the type of object to be returned.
|
||||||
|
The ``JSON_QUERY`` function **only** returns a JSON dictionary or list,
|
||||||
|
but not an individual string, numeric, or boolean element; the
|
||||||
|
``JSON_VALUE`` function **only** returns an individual string, numeric,
|
||||||
|
or boolean element. **both functions either return NULL or raise
|
||||||
|
an error if they are not used against the correct expected value**.
|
||||||
|
|
||||||
|
To handle this awkward requirement, indexed access rules are as follows:
|
||||||
|
|
||||||
|
1. When extracting a sub element from a JSON that is itself a JSON
|
||||||
|
dictionary or list, the :meth:`_types.JSON.Comparator.as_json` accessor
|
||||||
|
should be used::
|
||||||
|
|
||||||
|
stmt = select(data_table.c.data["some key"].as_json()).where(
|
||||||
|
data_table.c.data["some key"].as_json() == {"sub": "structure"}
|
||||||
|
)
|
||||||
|
|
||||||
|
2. When extracting a sub element from a JSON that is a plain boolean,
|
||||||
|
string, integer, or float, use the appropriate method among
|
||||||
|
:meth:`_types.JSON.Comparator.as_boolean`,
|
||||||
|
:meth:`_types.JSON.Comparator.as_string`,
|
||||||
|
:meth:`_types.JSON.Comparator.as_integer`,
|
||||||
|
:meth:`_types.JSON.Comparator.as_float`::
|
||||||
|
|
||||||
|
stmt = select(data_table.c.data["some key"].as_string()).where(
|
||||||
|
data_table.c.data["some key"].as_string() == "some string"
|
||||||
|
)
|
||||||
|
|
||||||
|
.. versionadded:: 1.4
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# note there was a result processor here that was looking for "number",
|
||||||
|
# but none of the tests seem to exercise it.
|
||||||
|
|
||||||
|
|
||||||
|
# Note: these objects currently match exactly those of MySQL, however since
|
||||||
|
# these are not generalizable to all JSON implementations, remain separately
|
||||||
|
# implemented for each dialect.
|
||||||
|
class _FormatTypeMixin:
|
||||||
|
def _format_value(self, value):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def bind_processor(self, dialect):
|
||||||
|
super_proc = self.string_bind_processor(dialect)
|
||||||
|
|
||||||
|
def process(value):
|
||||||
|
value = self._format_value(value)
|
||||||
|
if super_proc:
|
||||||
|
value = super_proc(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
return process
|
||||||
|
|
||||||
|
def literal_processor(self, dialect):
|
||||||
|
super_proc = self.string_literal_processor(dialect)
|
||||||
|
|
||||||
|
def process(value):
|
||||||
|
value = self._format_value(value)
|
||||||
|
if super_proc:
|
||||||
|
value = super_proc(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
return process
|
||||||
|
|
||||||
|
|
||||||
|
class JSONIndexType(_FormatTypeMixin, sqltypes.JSON.JSONIndexType):
|
||||||
|
def _format_value(self, value):
|
||||||
|
if isinstance(value, int):
|
||||||
|
value = "$[%s]" % value
|
||||||
|
else:
|
||||||
|
value = '$."%s"' % value
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class JSONPathType(_FormatTypeMixin, sqltypes.JSON.JSONPathType):
|
||||||
|
def _format_value(self, value):
|
||||||
|
return "$%s" % (
|
||||||
|
"".join(
|
||||||
|
[
|
||||||
|
"[%s]" % elem if isinstance(elem, int) else '."%s"' % elem
|
||||||
|
for elem in value
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user