Hide keyboard shortcuts

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 

3 

4""" 

5=============================================================================== 

6 

7 Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com). 

8 

9 This file is part of cardinal_pythonlib. 

10 

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 

14 

15 https://www.apache.org/licenses/LICENSE-2.0 

16 

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. 

22 

23=============================================================================== 

24 

25**Helper functions relating to SQLAlchemy SQL dialects.** 

26 

27""" 

28 

29from typing import Union 

30 

31# noinspection PyProtectedMember 

32from sqlalchemy.engine import Engine 

33from sqlalchemy.engine.interfaces import Dialect 

34from sqlalchemy.sql.compiler import IdentifierPreparer, SQLCompiler 

35 

36 

37# ============================================================================= 

38# Constants 

39# ============================================================================= 

40 

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' 

53 

54 

55ALL_SQLA_DIALECTS = list(set( 

56 [getattr(SqlaDialectName, k) 

57 for k in dir(SqlaDialectName) if not k.startswith("_")] 

58)) 

59 

60 

61# ============================================================================= 

62# Dialect stuff 

63# ============================================================================= 

64 

65def get_dialect(mixed: Union[SQLCompiler, Engine, Dialect]) -> Dialect: 

66 """ 

67 Finds the SQLAlchemy dialect in use. 

68 

69 Args: 

70 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or 

71 :class:`Dialect` object 

72 

73 Returns: the SQLAlchemy :class:`Dialect` being used 

74 

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") 

84 

85 

86def get_dialect_name(mixed: Union[SQLCompiler, Engine, Dialect]) -> str: 

87 """ 

88 Finds the name of the SQLAlchemy dialect in use. 

89 

90 Args: 

91 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or 

92 :class:`Dialect` object 

93 

94 Returns: the SQLAlchemy dialect name being used 

95 """ 

96 dialect = get_dialect(mixed) 

97 # noinspection PyUnresolvedReferences 

98 return dialect.name 

99 

100 

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. 

106 

107 Args: 

108 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or 

109 :class:`Dialect` object 

110 

111 Returns: an :class:`IdentifierPreparer` 

112 

113 """ 

114 dialect = get_dialect(mixed) 

115 # noinspection PyUnresolvedReferences 

116 return dialect.preparer(dialect) # type: IdentifierPreparer 

117 

118 

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. 

124 

125 Args: 

126 identifier: the identifier to be quoted 

127 mixed: an SQLAlchemy :class:`SQLCompiler`, :class:`Engine`, or 

128 :class:`Dialect` object 

129 

130 Returns: 

131 the quoted identifier 

132 

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)