1 |
|
# |
2 |
|
# This file is part of GNU Enterprise. |
3 |
|
# |
4 |
|
# GNU Enterprise is free software; you can redistribute it |
5 |
|
# and/or modify it under the terms of the GNU General Public |
6 |
|
# License as published by the Free Software Foundation; either |
7 |
|
# version 2, or(at your option) any later version. |
8 |
|
# |
9 |
|
# GNU Enterprise is distributed in the hope that it will be |
10 |
|
# useful, but WITHOUT ANY WARRANTY; without even the implied |
11 |
|
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
12 |
|
# PURPOSE. See the GNU General Public License for more details. |
13 |
|
# |
14 |
|
# You should have received a copy of the GNU General Public |
15 |
|
# License along with program; see the file COPYING. If not, |
16 |
|
# write to the Free Software Foundation, Inc., 59 Temple Place |
17 |
|
# - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
# |
19 |
|
# Copyright 2000-2003 Free Software Foundation |
20 |
|
# |
21 |
|
# FILE: |
22 |
|
# _pgsql/DBdriver.py |
23 |
|
# |
24 |
|
# DESCRIPTION: |
25 |
|
# A core Postgresql dbdriver that the other (specific) |
26 |
|
# postgresql drivers can extend |
27 |
|
# |
28 |
|
# NOTES: |
29 |
|
# |
30 |
|
|
31 |
|
__all__ = ['Connection'] |
32 |
|
|
33 |
|
import string |
34 |
|
from string import lower, join, split |
35 |
|
import sys |
36 |
|
from gnue.common.apps import GDebug, GConfig |
37 |
|
from gnue.common.datasources import Exceptions |
38 |
|
from gnue.common.datasources.drivers import DBSIG2 |
39 |
|
from DataObject import * |
40 |
|
from gnue.common.datasources.drivers.postgresql.Schema.Discovery.Introspection import Introspection |
41 |
|
|
42 |
|
|
43 |
|
###################################################################### |
44 |
|
# |
45 |
|
# GConnection object for PostgreSQL-based drivers |
46 |
|
# |
47 |
|
class Connection(DBSIG2.Connection): |
48 |
|
|
49 |
|
defaultBehavior = Introspection |
50 |
|
supportedDataObjects = { |
51 |
|
'object': DataObject_Object, |
52 |
|
'sql': DataObject_SQL |
53 |
|
} |
54 |
|
|
55 |
|
_pg_connectString = 'host=%s dbname=%s user=%s password=%s port=%s' |
56 |
|
_pg_driver = None |
57 |
|
|
58 |
|
|
59 |
|
def connect(self, connectData): |
60 |
|
|
61 |
|
print "Postgresql database driver initializing" |
62 |
|
|
63 |
|
if not hasattr(self,'_DatabaseError'): |
64 |
|
self._DatabaseError = self._pg_driver.DatabaseError |
65 |
|
|
66 |
|
GDebug.printMesg(1,"Postgresql database driver initializing") |
67 |
|
try: |
68 |
|
try: |
69 |
|
port = connectData['port'] |
70 |
|
except: |
71 |
|
port = '5432' |
72 |
|
|
73 |
|
self.native = self._pg_driver.connect(self._pg_connectString % |
74 |
|
(connectData['host'], |
75 |
|
connectData['dbname'], |
76 |
|
connectData['_username'], |
77 |
|
connectData['_password'], |
78 |
|
port)) |
79 |
|
except self._DatabaseError, value: |
80 |
|
GDebug.printMesg(1,"Connect String: %s" % (self._pg_connectString % \ |
81 |
|
(connectData['host'], |
82 |
|
connectData['dbname'], |
83 |
|
connectData['_username'], |
84 |
|
connectData['_password'], |
85 |
|
port))) |
86 |
|
GDebug.printMesg(1,"Exception %s " % value) |
87 |
|
raise Exceptions.LoginError, _("The PostgreSQL driver returned the following error:\n\t%s") % value |
88 |
|
|
89 |
|
try: |
90 |
|
try: |
91 |
|
self._pg_encoding = pg_encTable[self._encoding] |
92 |
|
except KeyError: |
93 |
|
GDebug.printMesg(1,_("Encoding '%s' is not supported by postgresql dbdriver.") % \ |
94 |
|
self._encoding + _('Using default encoding.')) |
95 |
|
self._pg_encoding = '' |
96 |
|
|
97 |
|
if self._pg_encoding not in ("",'DEFAULT'): |
98 |
|
GDebug.printMesg(1,'Setting postgresql client_encoding to %s (%s)' % (self._pg_encoding, |
99 |
|
self._encoding)) |
100 |
|
cursor = self.native.cursor() |
101 |
|
cursor.execute("SET CLIENT_ENCODING TO '%s'" % self._pg_encoding) |
102 |
|
cursor.close() |
103 |
|
|
104 |
|
except self._DatabaseError: |
105 |
|
try: |
106 |
|
cursor.close() |
107 |
|
except: |
108 |
|
pass |
109 |
|
|
110 |
|
if connectData.has_key('datetimeformat'): |
111 |
|
self._dateTimeFormat = "'%s'" % connectData['datetimeformat'] |
112 |
|
else: |
113 |
|
self._dateTimeFormat = "'%Y-%m-%d %H:%M:%S'" |
114 |
|
|
115 |
|
|
116 |
|
######### |
117 |
|
# |
118 |
|
# Extensions to the basic GConnection object |
119 |
|
# |
120 |
|
|
121 |
|
# Return the current date, according to database |
122 |
|
def getTimeStamp(self): |
123 |
|
return self.__singleQuery("select current_timestamp") |
124 |
|
|
125 |
|
# Return a sequence number from sequence 'name' |
126 |
|
def getSequence(self, name): |
127 |
|
return self.__singleQuery("select nextval('%s')" % name) |
128 |
|
|
129 |
|
# Run the SQL statement 'statement' |
130 |
|
def sql(self, statement): |
131 |
|
cursor = self.native.cursor() |
132 |
|
try: |
133 |
|
cursor.execute(statement) |
134 |
|
cursor.close() |
135 |
|
except: |
136 |
|
cursor.close() |
137 |
|
raise |
138 |
|
|
139 |
|
# Used internally |
140 |
|
def __singleQuery(self, statement): |
141 |
|
cursor = self.native.cursor() |
142 |
|
try: |
143 |
|
cursor.execute(statement) |
144 |
|
rv = cursor.fetchone() |
145 |
|
cursor.close() |
146 |
|
except: |
147 |
|
print "DBdriver.py", "You've got your bar in my foo! And you've got your foo on my bar! Two great reams that ream well together!" |
148 |
|
GDebug.printMesg(1,"**** Unable to execute extension query") |
149 |
|
GDebug.printMesg(1,"**** %s" % sys.exc_info()[1]) |
150 |
|
cursor.close() |
151 |
|
raise |
152 |
|
|
153 |
|
try: |
154 |
|
return rv[0] |
155 |
|
except: |
156 |
|
return None |
157 |
|
|
158 |
|
pg_encTable = {'ascii' : 'SQL_ASCII', # ASCII |
159 |
|
'' : 'EUC_JP', # Japanese EUC |
160 |
|
'' : 'EUC_CN', # Chinese EUC |
161 |
|
'' : 'EUC_KR', # Korean EUC |
162 |
|
'' : 'JOHAB', # Korean EUC (Hangle base) |
163 |
|
'' : 'EUC_TW', # Taiwan EUC |
164 |
|
'utf-8' : 'UNICODE', # Unicode (UTF-8) |
165 |
|
'' : 'MULE_INTERNAL', # Mule internal code |
166 |
|
'iso8859-1' : 'LATIN1', # ISO 8859-1 ECMA-94 Latin Alphabet No.1 |
167 |
|
'iso8859-2' : 'LATIN2', # ISO 8859-2 ECMA-94 Latin Alphabet No.2 |
168 |
|
'iso8859-3' : 'LATIN3', # ISO 8859-3 ECMA-94 Latin Alphabet No.3 |
169 |
|
'iso8859-4' : 'LATIN4', # ISO 8859-4 ECMA-94 Latin Alphabet No.4 |
170 |
|
'iso8859-9' : 'LATIN5', # ISO 8859-9 ECMA-128 Latin Alphabet No.5 |
171 |
|
'iso8859-10': 'LATIN6', # ISO 8859-10 ECMA-144 Latin Alphabet No.6 |
172 |
|
'iso8859-13': 'LATIN7', # ISO 8859-13 Latin Alphabet No.7 |
173 |
|
'iso8859-14': 'LATIN8', # ISO 8859-14 Latin Alphabet No.8 |
174 |
|
'iso8859-15': 'LATIN9', # ISO 8859-15 Latin Alphabet No.9 |
175 |
|
'iso8859-16': 'LATIN10', # ISO 8859-16 ASRO SR 14111 Latin Alphabet No.10 |
176 |
|
'iso8859-5' : 'ISO-8859-5', # ECMA-113 Latin/Cyrillic |
177 |
|
'iso8859-6' : 'ISO-8859-6', # ECMA-114 Latin/Arabic |
178 |
|
'iso8859-7' : 'ISO-8859-7', # ECMA-118 Latin/Greek |
179 |
|
'iso8859-8' : 'ISO-8859-8', # ECMA-121 Latin/Hebrew |
180 |
|
'' : 'KOI8', # KOI8-R(U) |
181 |
|
'' : 'WIN', # Windows CP1251 |
182 |
|
'' : 'ALT', # Windows CP866 |
183 |
|
'' : 'WIN1256', # Arabic Windows CP1256 |
184 |
|
'' : 'TCVN', # Vietnamese TCVN-5712 (Windows CP1258) |
185 |
|
'' : 'WIN874'} # Thai Windows CP874 |