2
# This file is part of GNU Enterprise.
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.
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.
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.
19
# Copyright 2001-2005 Free Software Foundation
25
# Virtual database driver for loading data from a DBF file
28
# Used whenever a data has to be imported from dbase III+
31
__all__ = ['ResultSet']
33
from gnue.common.apps import GDebug
35
from gnue.common.datasources.GDataObjects import *
36
from gnue.common.drivers.special.static.Driver import *
40
###########################################################
42
# This is an static data driver for connectionless clients
44
###########################################################
45
class DBF_DataObject (STATIC_DataObject):
48
DataObject.__init__(self)
49
self.triggerExtensions = TriggerExtensions(self)
50
self._DatabaseError = Error
52
def _createResultSet(self, conditions={}, readOnly=0, masterRecordSet=None, sql=""):
53
return DBF_ResultSet(self, masterRecordSet=masterRecordSet)
56
# We don't do connections (we are connectionless)
57
def connect(self, connectData={}):
59
gDebug(9,'Open file: %s' % (connectData['directory']+connectData['dbname']))
60
self._dataConnection = dbf.dbf(connectData['directory']+\
61
connectData['dbname'])
63
tmsg = _('DBF file not found.')
64
raise self._DatabaseError, tmsg
66
tmsg = _('Wrong file format.')
67
raise self._DatabaseError, tmsg
70
self._fieldReferences=[]
71
for f in self._dataConnection.fields:
72
self._fieldReferences.append(string.lower(f[0]))
77
# Schema (metadata) functions
80
# Return a list of the types of Schema objects this driver provides
81
def getSchemaTypes(self):
82
return [('table',_('Tables'),1)]
84
# Return a list of Schema objects
85
def getSchemaList(self, type=None):
86
tablename=self._dataConnection.fname
87
if tablename[-4:]=='.dbf':
88
tablename=tablename[:-4]
90
list = [Schema(attrs={'id':tablename,\
93
getChildSchema=self.__getFieldSchema)]
97
# Find a schema object with specified name
98
def getSchemaByName(self, name, type=None):
99
tablename=self._dataConnection.fname
100
if tablename[-4:]=='.dbf':
101
tablename=tablename[:-4]
104
return self.getSchemaList()
108
# Get fields for a table
109
def __getFieldSchema(self, parent):
112
for field in self._dataConnection.fields:
114
fname=string.lower(field[0])
116
attrs={'id': "%s.%s" % (parent.id,fname), 'name': fname,
117
'type':'field', 'nativetype': field[1],
120
attrs['datatype']='text'
121
attrs['length']='%s' % field[2]
122
elif field[1] == 'N':
123
attrs['datatype']='number'
124
elif field[1] == 'D':
125
attrs['datatype']='date'
126
elif field[1] == 'L':
127
attrs['datatype']='boolean'
129
gDebug(1,'WARNING: dbf native type error: %s' % field[1])
131
list.append(Schema(attrs=attrs))
138
###########################################################
142
###########################################################
143
class DBF_ResultSet(STATIC_ResultSet):
145
def __init__(self, dataObject, cursor=None, defaultValues={}, masterRecordSet=None):
146
ResultSet.__init__(self, dataObject, \
147
cursor, defaultValues, masterRecordSet)
149
self._recordSetClass = STATIC_RecordSet
151
# Returns 1=DataObject has uncommitted changes
153
return 0 # Static DataObjects cannot have pending changes :)
155
# Post changes to the database
157
# Leave this here in case (for some bizarro reason)
158
# a bound dataobject uses us as a master
159
for record in (self._cachedRecords):
162
# Load cacheCount number of new records
163
def _loadNextRecord(self):
164
if hasattr(self,"_alldataloaded"):
168
for row in self._dataObject._dataConnection:
171
for f in self._dataObject._dataConnection.fields:
172
dict[string.lower(f[0])] = row[c]
175
record=self._recordSetClass(parent=self,initialData=dict)
177
self._cachedRecords.append (record)
179
self._recordCount=self._recordCount+1
181
self._alldataloaded = 1
187
######################################
189
# The following hashes describe
190
# this driver's characteristings.
192
######################################
195
# All datasouce "types" and corresponding DataObject class
197
supportedDataObjects = {
198
'static': DBF_DataObject,
199
'object': DBF_DataObject