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 |
|
|
32 |
|
import string |
33 |
|
import sys |
34 |
|
from gnue.common.datasources.drivers.DBSIG2.Driver import DBSIG2 |
35 |
|
from gnue.common.datasources import GDataObjects.ConnectionError |
36 |
|
|
37 |
|
class MySQL_ResultSet(DBSIG2.ResultSet): |
38 |
|
def __init__(self, dataObject, cursor=None, defaultValues={}, masterRecordSet=None): |
39 |
|
DBSIG2.ResultSet.__init__(self, dataObject, \ |
40 |
|
cursor, defaultValues, masterRecordSet) |
41 |
|
self._recordSetClass = MySQL_RecordSet |
42 |
|
|
43 |
|
# Compensate for bug in python mysql drivers older than 0.9.2a2 |
44 |
|
if MySQLdb.__version__ >= '0.9.2a2': |
45 |
|
self.fetchBugFix = self._cursor.fetchmany |
46 |
|
else: |
47 |
|
self.__done = 0 |
48 |
|
self.fetchBugFix = self.__mySqlNeedsLotsOfTLC |
49 |
|
|
50 |
|
|
51 |
|
# Compensate for MySQ bug |
52 |
|
def __mySqlNeedsLotsOfTLC(self): |
53 |
|
if self.__done: |
54 |
|
return None |
55 |
|
|
56 |
|
self.__done = 1 |
57 |
|
return self._cursor.fetchall() |
58 |
|
|
59 |
|
|
60 |
|
def _loadNextRecord(self): |
61 |
|
if self._cursor: |
62 |
|
rs = None |
63 |
|
|
64 |
|
try: |
65 |
|
# See __init__ for details |
66 |
|
rsets = self.fetchBugFix() |
67 |
|
|
68 |
|
except self._dataObject._DatabaseError, err: |
69 |
|
raise GDataObjects.ConnectionError, err |
70 |
|
|
71 |
|
if rsets and len(rsets): |
72 |
|
for rs in(rsets): |
73 |
|
if rs: |
74 |
|
i = 0 |
75 |
|
dict = {} |
76 |
|
for f in (rs): |
77 |
|
dict[string.lower(self._fieldNames[i])] = f |
78 |
|
i += 1 |
79 |
|
self._cachedRecords.append (self._recordSetClass(parent=self, \ |
80 |
|
initialData=dict)) |
81 |
|
else: |
82 |
|
return 0 |
83 |
|
return 1 |
84 |
|
else: |
85 |
|
return 0 |
86 |
|
else: |
87 |
|
return 0 |