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 |
|
# mysql/DBdriver.py |
23 |
|
# |
24 |
|
# DESCRIPTION: |
25 |
|
# Driver to provide access to data vi MySQL |
26 |
|
# |
27 |
|
# NOTES: |
28 |
|
# Supports transactions if the MySQL server is compiled w/transaction support |
29 |
|
# (which it does NOT by default) |
30 |
|
|
31 |
|
__all__ = ['Connection'] |
32 |
|
|
33 |
|
|
34 |
|
import string |
35 |
|
import sys |
36 |
|
from gnue.common.datasources import GDataObjects, GConditions, GConnections |
37 |
|
from gnue.common.apps import GDebug |
38 |
|
|
39 |
|
try: |
40 |
|
import MySQLdb |
41 |
|
except ImportError, message: |
42 |
|
tmsg = _("\nCould not load MySQLdb. For MySQL support, please install \n") \ |
43 |
|
+ _("mysql-python 0.9.0 or later from") \ |
44 |
|
+ "http://sourceforge.net/projects/mysql-python\n" \ |
45 |
|
+ _("[Error: %s]") % message |
46 |
|
|
47 |
|
raise GConnections.AdapterNotInstalled, tmsg |
48 |
|
|
49 |
|
from gnue.common.datasources.drivers import DBSIG2 |
50 |
|
from DataObject import * |
51 |
|
from gnue.common.datasources.drivers.mysql.Schema.Discovery.Introspection import Introspection |
52 |
|
|
53 |
|
|
54 |
|
###################################################################### |
55 |
|
# |
56 |
|
# GConnection object for PostgreSQL-based drivers |
57 |
|
# |
58 |
|
class Connection(DBSIG2.Connection): |
59 |
|
|
60 |
|
defaultBehavior = Introspection |
61 |
|
_DatabaseError = MySQLdb.DatabaseError |
62 |
|
supportedDataObjects = { |
63 |
|
'object': DataObject_Object, |
64 |
|
'sql': DataObject_SQL |
65 |
|
} |
66 |
|
|
67 |
|
_mysql = MySQLdb |
68 |
|
|
69 |
|
def connect(self, connectData={}): |
70 |
|
GDebug.printMesg(1,"Mysql database driver initializing") |
71 |
|
|
72 |
|
# 1. just allow string type username/password 2. None -> '' |
73 |
|
user = str(connectData['_username'] or '') |
74 |
|
passwd = str(connectData['_password'] or '') |
75 |
|
|
76 |
|
try: |
77 |
|
self.native = MySQLdb.connect(user=user, |
78 |
|
passwd=passwd, |
79 |
|
host=connectData['host'], |
80 |
|
db=connectData['dbname']) |
81 |
|
|
82 |
|
except self._DatabaseError, value: |
83 |
|
raise GDataObjects.LoginError, value |
84 |
|
|
85 |
|
self._beginTransaction() |
86 |
|
|
87 |
|
|
88 |
|
def _beginTransaction(self): |
89 |
|
try: |
90 |
|
self.native.begin() |
91 |
|
except: |
92 |
|
pass |
93 |
|
|
94 |
|
|
95 |
|
######### |
96 |
|
# |
97 |
|
# Extensions to the basic GConnection object |
98 |
|
# |
99 |
|
|
100 |
|
# Return the current date, according to database |
101 |
|
def getTimeStamp(self): |
102 |
|
return self.__singleQuery("select current_timestamp") |
103 |
|
|
104 |
|
# Return a sequence number from sequence 'name' |
105 |
|
#def getSequence(self, name): |
106 |
|
# raise "Not supported" |
107 |
|
|
108 |
|
# Run the SQL statement 'statement' |
109 |
|
def sql(self, statement): |
110 |
|
cursor = self.native.cursor() |
111 |
|
try: |
112 |
|
cursor.execute(statement) |
113 |
|
cursor.close() |
114 |
|
except: |
115 |
|
cursor.close() |
116 |
|
raise |
117 |
|
|
118 |
|
# Used internally |
119 |
|
def __singleQuery(self, statement): |
120 |
|
cursor = self.native.cursor() |
121 |
|
try: |
122 |
|
cursor.execute(statement) |
123 |
|
rv = cursor.fetchone() |
124 |
|
cursor.close() |
125 |
|
except: |
126 |
|
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!" |
127 |
|
GDebug.printMesg(1,"**** Unable to execute extension query") |
128 |
|
GDebug.printMesg(1,"**** %s" % sys.exc_info()[1]) |
129 |
|
cursor.close() |
130 |
|
raise |
131 |
|
|
132 |
|
try: |
133 |
|
return rv[0] |
134 |
|
except: |
135 |
|
return None |