Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/cardinal_pythonlib/sqlalchemy/dialect.py : 61%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# cardinal_pythonlib/sqlalchemy/dialect.py
4"""
5===============================================================================
7 Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com).
9 This file is part of cardinal_pythonlib.
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
15 https://www.apache.org/licenses/LICENSE-2.0
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
23===============================================================================
25**Helper functions relating to SQLAlchemy SQL dialects.**
27"""
29from typing import Union
31# noinspection PyProtectedMember
32from sqlalchemy.engine import Engine
33from sqlalchemy.engine.interfaces import Dialect
34from sqlalchemy.sql.compiler import IdentifierPreparer, SQLCompiler
37# =============================================================================
38# Constants
39# =============================================================================
41class SqlaDialectName(object):
42 """
43 Dialect names used by SQLAlchemy.
44 """
45 FIREBIRD = "firebird"
46 MYSQL = 'mysql'
47 MSSQL = 'mssql'
48 ORACLE = 'oracle'
49 POSTGRES = 'postgresql'
50 SQLITE = 'sqlite'
51 SQLSERVER = MSSQL # synonym
52 SYBASE = 'sybase'
55ALL_SQLA_DIALECTS = list(set(
56 [getattr(SqlaDialectName, k)
57 for k in dir(SqlaDialectName) if not k.startswith("_")]
58))
61# =============================================================================
62# Dialect stuff
63# =============================================================================
65def get_dialect(mixed: Union[SQLCompiler, Engine, Dialect]) -> Dialect:
66 """
67 Finds the SQLAlchemy dialect in use.
69 Args:
70 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or
71 :class:`Dialect` object
73 Returns: the SQLAlchemy :class:`Dialect` being used
75 """
76 if isinstance(mixed, Dialect):
77 return mixed
78 elif isinstance(mixed, Engine):
79 return mixed.dialect
80 elif isinstance(mixed, SQLCompiler):
81 return mixed.dialect
82 else:
83 raise ValueError("get_dialect: 'mixed' parameter of wrong type")
86def get_dialect_name(mixed: Union[SQLCompiler, Engine, Dialect]) -> str:
87 """
88 Finds the name of the SQLAlchemy dialect in use.
90 Args:
91 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or
92 :class:`Dialect` object
94 Returns: the SQLAlchemy dialect name being used
95 """
96 dialect = get_dialect(mixed)
97 # noinspection PyUnresolvedReferences
98 return dialect.name
101def get_preparer(mixed: Union[SQLCompiler, Engine,
102 Dialect]) -> IdentifierPreparer:
103 """
104 Returns the SQLAlchemy :class:`IdentifierPreparer` in use for the dialect
105 being used.
107 Args:
108 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or
109 :class:`Dialect` object
111 Returns: an :class:`IdentifierPreparer`
113 """
114 dialect = get_dialect(mixed)
115 # noinspection PyUnresolvedReferences
116 return dialect.preparer(dialect) # type: IdentifierPreparer
119def quote_identifier(identifier: str,
120 mixed: Union[SQLCompiler, Engine, Dialect]) -> str:
121 """
122 Converts an SQL identifier to a quoted version, via the SQL dialect in
123 use.
125 Args:
126 identifier: the identifier to be quoted
127 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or
128 :class:`Dialect` object
130 Returns:
131 the quoted identifier
133 """
134 # See also http://sqlalchemy-utils.readthedocs.io/en/latest/_modules/sqlalchemy_utils/functions/orm.html # noqa
135 return get_preparer(mixed).quote(identifier)