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 |
|
# SQLitedb/DBdriver.py |
23 |
|
# |
24 |
|
# DESCRIPTION: |
25 |
|
# Driver to provide access to data via SQLite's Python Driver. |
26 |
|
# Requires PySQLite (http://pysqlite.sf.net/) |
27 |
|
# |
28 |
|
# NOTES: |
29 |
|
# |
30 |
|
# Supported attributes (via connections.conf or <database> tag) |
31 |
|
# |
32 |
|
# dbname= This is the SQLite database to use (required) |
33 |
|
# |
34 |
|
|
35 |
|
|
36 |
|
from string import lower,find,rfind,split,strip |
37 |
|
import sys |
38 |
|
from gnue.common.datasources import GDataObjects, GConditions, GConnections |
39 |
|
from gnue.common.apps import GDebug |
40 |
|
from gnue.common.datasources.drivers.DBSIG2.Driver \ |
41 |
|
import DBSIG2.RecordSet, DBSIG2.ResultSet, DBSIG2.DataObject, \ |
42 |
|
DBSIG2.DataObject_SQL, DBSIG2.DataObject_Object |
43 |
|
|
44 |
|
try: |
45 |
|
import sqlite as SIG2api |
46 |
|
except ImportError, message: |
47 |
|
tmsg = _("Driver not installed: SQLitedbapi for SQLite 7.x \n[%s]") % message |
48 |
|
raise GConnections.AdapterNotInstalled, tmsg |
49 |
|
|
50 |
|
|
51 |
|
class SQLite_RecordSet(DBSIG2.RecordSet): |
52 |
|
pass |
53 |
|
|
54 |
|
|
55 |
|
class SQLite_ResultSet(DBSIG2.ResultSet): |
56 |
|
def __init__(self, dataObject, cursor=None, defaultValues={}, masterRecordSet=None): |
57 |
|
DBSIG2.ResultSet.__init__(self, dataObject, \ |
58 |
|
cursor, defaultValues, masterRecordSet) |
59 |
|
self._recordSetClass = SQLite_RecordSet |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
class SQLite_DataObject(DBSIG2.DataObject): |
64 |
|
def __init__(self): |
65 |
|
DBSIG2.DataObject.__init__(self) |
66 |
|
self._DatabaseError = SIG2api.DatabaseError |
67 |
|
self._resultSetClass = SQLite_ResultSet |
68 |
|
|
69 |
|
|
70 |
|
def connect(self, connectData={}): |
71 |
|
GDebug.printMesg(1,"SQLite database driver initializing") |
72 |
|
try: |
73 |
|
self._dataConnection = SIG2api.connect( \ |
74 |
|
db=connectData['dbname'], \ |
75 |
|
mode=077 ) |
76 |
|
except self._DatabaseError, value: |
77 |
|
raise GDataObjects.LoginError, value |
78 |
|
|
79 |
|
self._postConnect() |
80 |
|
|
81 |
|
def _postConnect(self): |
82 |
|
self.triggerExtensions = TriggerExtensions(self._dataConnection) |
83 |
|
|
84 |
|
|
85 |
|
# Return a list of necessary login fields (e.g., user/pass). |
86 |
|
# Each list item is another list of ["field label", isPassword?] |
87 |
|
def getLoginFields(self): |
88 |
|
return [] |
89 |
|
|
90 |
|
# |
91 |
|
# Schema (metadata) functions |
92 |
|
# |
93 |
|
|
94 |
|
# Return a list of the types of Schema objects this driver provides |
95 |
|
def getSchemaTypes(self): |
96 |
|
return [('view',_('Views'),1), |
97 |
|
('table',_('Table'),1)] |
98 |
|
|
99 |
|
# Return a list of Schema objects |
100 |
|
def getSchemaList(self, type=None): |
101 |
|
|
102 |
|
if type!=None: |
103 |
|
where=" WHERE type='%s'" % type |
104 |
|
else: |
105 |
|
where="" |
106 |
|
|
107 |
|
statement = "SELECT type,name,tbl_name,sql FROM sqlite_master "+\ |
108 |
|
where+" UNION ALL "+\ |
109 |
|
"SELECT type,name,tbl_name,sql FROM sqlite_temp_master "+\ |
110 |
|
where+" ORDER BY name;" |
111 |
|
|
112 |
|
cursor = self._dataConnection.cursor() |
113 |
|
GDebug.printMesg(1,"** Executing: %s **" % statement) |
114 |
|
cursor.execute(statement) |
115 |
|
|
116 |
|
list = [] |
117 |
|
for rs in cursor.fetchall(): |
118 |
|
if rs[0] in ('table','view'): |
119 |
|
list.append(GDataObjects.Schema(attrs={'id':rs[1], 'name':rs[1], \ |
120 |
|
'type':rs[0],}, |
121 |
|
getChildSchema=self.__getFieldSchema)) |
122 |
|
|
123 |
|
cursor.close() |
124 |
|
print list |
125 |
|
return list |
126 |
|
|
127 |
|
|
128 |
|
# Find a schema object with specified name |
129 |
|
def getSchemaByName(self, name, type=None): |
130 |
|
|
131 |
|
if type!=None: |
132 |
|
where=" AND type='%s'" % type |
133 |
|
else: |
134 |
|
where="" |
135 |
|
|
136 |
|
statement = ("SELECT type,name,tbl_name,sql FROM sqlite_master "+\ |
137 |
|
"WHERE name='%s'"+where+" UNION ALL "+\ |
138 |
|
"SELECT type,name,tbl_name,sql FROM sqlite_temp_master "+\ |
139 |
|
"WHERE name='%s' "+where+" ORDER BY name;") % (name,name) |
140 |
|
|
141 |
|
cursor = self._dataConnection.cursor() |
142 |
|
GDebug.printMesg(1,"** Executing: %s **" % statement) |
143 |
|
cursor.execute(statement) |
144 |
|
|
145 |
|
rs = cursor.fetchone() |
146 |
|
if rs and rs[0] in ('table','view'): |
147 |
|
schema = GDataObjects.Schema(attrs={'id':rs[1], 'name':rs[1], \ |
148 |
|
'type':rs[0],}, |
149 |
|
getChildSchema=self.__getFieldSchema) |
150 |
|
else: |
151 |
|
schema = None |
152 |
|
|
153 |
|
cursor.close() |
154 |
|
return schema |
155 |
|
|
156 |
|
|
157 |
|
# Get fields for a table |
158 |
|
def __getFieldSchema(self, parent): |
159 |
|
|
160 |
|
if parent.type=='view': |
161 |
|
print "Views are not supported at the moment" |
162 |
|
return None |
163 |
|
|
164 |
|
statement = ("SELECT type,name,tbl_name,sql FROM sqlite_master "+\ |
165 |
|
"WHERE type='%s' and name='%s' UNION ALL "+\ |
166 |
|
"SELECT type,name,tbl_name,sql FROM sqlite_temp_master "+\ |
167 |
|
"WHERE type='%s' "+\ |
168 |
|
"and name='%s' ORDER BY name;") % (parent.type,parent.id,\ |
169 |
|
parent.type,parent.id) |
170 |
|
|
171 |
|
cursor = self._dataConnection.cursor() |
172 |
|
GDebug.printMesg(1,"** Executing: %s **" % statement) |
173 |
|
cursor.execute(statement) |
174 |
|
columns = cursor.description |
175 |
|
|
176 |
|
# Because sqlite don't store column definitions, but computes it |
177 |
|
# every time anew from the 'create table' statement, we have to |
178 |
|
# parse that statement to get the data |
179 |
|
|
180 |
|
# get sql definition of table |
181 |
|
rs = cursor.fetchone() |
182 |
|
cursor.close() |
183 |
|
if rs: |
184 |
|
sql=rs[3] |
185 |
|
else: |
186 |
|
return None |
187 |
|
|
188 |
|
# parse the sql definition |
189 |
|
GDebug.printMesg(3,"** Table definition: %s **" % sql) |
190 |
|
|
191 |
|
sql=sql[find(sql,'(')+1:rfind(sql,')')] |
192 |
|
fields = split(sql,',') |
193 |
|
list = [] |
194 |
|
for field in fields: |
195 |
|
|
196 |
|
fls=split(strip(field),' ',2) |
197 |
|
|
198 |
|
if not fls[0] in ('Constraint','Primary'): |
199 |
|
|
200 |
|
try: |
201 |
|
nativetype= fls[1][:find(fls[1],'(')] |
202 |
|
|
203 |
|
size=int(fls[1][find(fls[1],'(')+1:-1]) |
204 |
|
except: |
205 |
|
nativetype = fls[1] |
206 |
|
size=None |
207 |
|
|
208 |
|
attrs={'id': "%s.%s" % (parent.id, fls[0]), 'name': fls[0], |
209 |
|
'type':'field', 'nativetype': nativetype, |
210 |
|
'required':fls[2]=="NOT NULL"} |
211 |
|
|
212 |
|
if size!=None: |
213 |
|
attrs['length'] = size |
214 |
|
|
215 |
|
if nativetype in ('int','integer','bigint','mediumint', |
216 |
|
'smallint','tinyint','float','real', |
217 |
|
'double','decimal'): |
218 |
|
attrs['datatype']='number' |
219 |
|
elif nativetype[0] in ('date','time','timestamp','datetime'): |
220 |
|
attrs['datatype']='date' |
221 |
|
else: |
222 |
|
attrs['datatype']='text' |
223 |
|
|
224 |
|
list.append(GDataObjects.Schema(attrs=attrs)) |
225 |
|
|
226 |
|
return list |
227 |
|
|
228 |
|
|
229 |
|
class SQLite_DataObject_Object(SQLite_DataObject, \ |
230 |
|
DBSIG2.DataObject_Object): |
231 |
|
|
232 |
|
def __init__(self): |
233 |
|
SQLite_DataObject.__init__(self) |
234 |
|
|
235 |
|
def _buildQuery(self, conditions={},forDetail=None,additionalSQL=""): |
236 |
|
return DBSIG2.DataObject_Object._buildQuery(self, conditions,forDetail, additionalSQL) |
237 |
|
|
238 |
|
|
239 |
|
class SQLite_DataObject_SQL(SQLite_DataObject, \ |
240 |
|
DBSIG2.DataObject_SQL): |
241 |
|
def __init__(self): |
242 |
|
# Call DBSIG init first because SQLite_DataObject needs to overwrite |
243 |
|
# some of its values |
244 |
|
DBSIG2.DataObject_SQL.__init__(self) |
245 |
|
SQLite_DataObject.__init__(self) |
246 |
|
|
247 |
|
def _buildQuery(self, conditions={}): |
248 |
|
return DBSIG2.DataObject_SQL._buildQuery(self, conditions) |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
# |
253 |
|
# Extensions to Trigger Namespaces |
254 |
|
# |
255 |
|
class TriggerExtensions: |
256 |
|
|
257 |
|
def __init__(self, connection): |
258 |
|
self.__connection = connection |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
###################################### |
265 |
|
# |
266 |
|
# The following hashes describe |
267 |
|
# this driver's characteristings. |
268 |
|
# |
269 |
|
###################################### |
270 |
|
|
271 |
|
# |
272 |
|
# All datasouce "types" and corresponding DataObject class |
273 |
|
# |
274 |
|
supportedDataObjects = { |
275 |
|
'object': SQLite_DataObject_Object, |
276 |
|
'sql': SQLite_DataObject_SQL |
277 |
|
} |
278 |
|
|