Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/pypostgresql.py: 64%

47 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# postgresql/pypostgresql.py 

2# Copyright (C) 2005-2023 the SQLAlchemy authors and contributors 

3# <see AUTHORS file> 

4# 

5# This module is part of SQLAlchemy and is released under 

6# the MIT License: https://www.opensource.org/licenses/mit-license.php 

7""" 

8.. dialect:: postgresql+pypostgresql 

9 :name: py-postgresql 

10 :dbapi: pypostgresql 

11 :connectstring: postgresql+pypostgresql://user:password@host:port/dbname[?key=value&key=value...] 

12 :url: https://python.projects.pgfoundry.org/ 

13 

14.. note:: 

15 

16 The pypostgresql dialect is **not tested as part of SQLAlchemy's continuous 

17 integration** and may have unresolved issues. The recommended PostgreSQL 

18 driver is psycopg2. 

19 

20.. deprecated:: 1.4 The py-postgresql DBAPI is deprecated and will be removed 

21 in a future version. This DBAPI is superseded by the external 

22 version available at external-dialect_. Please use the external version or 

23 one of the supported DBAPIs to connect to PostgreSQL. 

24 

25.. TODO update link 

26.. _external-dialect: https://github.com/PyGreSQL 

27 

28""" # noqa 

29 

30from .base import PGDialect 

31from .base import PGExecutionContext 

32from ... import processors 

33from ... import types as sqltypes 

34from ... import util 

35 

36 

37class PGNumeric(sqltypes.Numeric): 

38 def bind_processor(self, dialect): 

39 return processors.to_str 

40 

41 def result_processor(self, dialect, coltype): 

42 if self.asdecimal: 

43 return None 

44 else: 

45 return processors.to_float 

46 

47 

48class PGExecutionContext_pypostgresql(PGExecutionContext): 

49 pass 

50 

51 

52class PGDialect_pypostgresql(PGDialect): 

53 driver = "pypostgresql" 

54 

55 supports_statement_cache = True 

56 supports_unicode_statements = True 

57 supports_unicode_binds = True 

58 description_encoding = None 

59 default_paramstyle = "pyformat" 

60 

61 # requires trunk version to support sane rowcounts 

62 # TODO: use dbapi version information to set this flag appropriately 

63 supports_sane_rowcount = True 

64 supports_sane_multi_rowcount = False 

65 

66 execution_ctx_cls = PGExecutionContext_pypostgresql 

67 colspecs = util.update_copy( 

68 PGDialect.colspecs, 

69 { 

70 sqltypes.Numeric: PGNumeric, 

71 # prevents PGNumeric from being used 

72 sqltypes.Float: sqltypes.Float, 

73 }, 

74 ) 

75 

76 @classmethod 

77 def dbapi(cls): 

78 from postgresql.driver import dbapi20 

79 

80 # TODO update link 

81 util.warn_deprecated( 

82 "The py-postgresql DBAPI is deprecated and will be removed " 

83 "in a future version. This DBAPI is superseded by the external" 

84 "version available at https://github.com/PyGreSQL. Please " 

85 "use one of the supported DBAPIs to connect to PostgreSQL.", 

86 version="1.4", 

87 ) 

88 

89 return dbapi20 

90 

91 _DBAPI_ERROR_NAMES = [ 

92 "Error", 

93 "InterfaceError", 

94 "DatabaseError", 

95 "DataError", 

96 "OperationalError", 

97 "IntegrityError", 

98 "InternalError", 

99 "ProgrammingError", 

100 "NotSupportedError", 

101 ] 

102 

103 @util.memoized_property 

104 def dbapi_exception_translation_map(self): 

105 if self.dbapi is None: 

106 return {} 

107 

108 return dict( 

109 (getattr(self.dbapi, name).__name__, name) 

110 for name in self._DBAPI_ERROR_NAMES 

111 ) 

112 

113 def create_connect_args(self, url): 

114 opts = url.translate_connect_args(username="user") 

115 if "port" in opts: 

116 opts["port"] = int(opts["port"]) 

117 else: 

118 opts["port"] = 5432 

119 opts.update(url.query) 

120 return ([], opts) 

121 

122 def is_disconnect(self, e, connection, cursor): 

123 return "connection is closed" in str(e) 

124 

125 

126dialect = PGDialect_pypostgresql