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 |
|
# Introspection.py |
23 |
|
# |
24 |
|
# DESCRIPTION: |
25 |
|
# |
26 |
|
# NOTES: |
27 |
|
# |
28 |
|
|
29 |
|
__all__ = ['Introspection'] |
30 |
|
|
31 |
|
import string |
32 |
|
from string import lower, join, split |
33 |
|
import sys |
34 |
|
|
35 |
|
from gnue.common.apps import GDebug, GConfig |
36 |
|
from gnue.common.apps import GDebug, GConfig |
37 |
|
from gnue.common.datasources import GIntrospection |
38 |
|
|
39 |
|
class Introspection(GIntrospection.Introspection): |
40 |
|
|
41 |
|
# list of the types of Schema objects this driver provides |
42 |
|
types =[ ('view',_('Views'),1), |
43 |
|
('table',_('Tables'),1) ] |
44 |
|
|
45 |
|
# |
46 |
|
# TODO: This is a quick hack to get this class |
47 |
|
# TODO: into the new-style schema format. |
48 |
|
# TODO: getSchema* should be merged into find() |
49 |
|
# |
50 |
|
def find(self, name=None, type=None): |
51 |
|
if name is None: |
52 |
|
return self.getSchemaList(type) |
53 |
|
else: |
54 |
|
rs = self.getSchemaByName(name, type) |
55 |
|
if rs: |
56 |
|
return [rs] |
57 |
|
else: |
58 |
|
return None |
59 |
|
|
60 |
|
|
61 |
|
# TODO: Merge into find() |
62 |
|
# Return a list of Schema objects |
63 |
|
# Return a list of Schema objects |
64 |
|
def getSchemaList(self, type=None): |
65 |
|
tablename=self._connection.native.fname |
66 |
|
if tablename[-4:]=='.dbf': |
67 |
|
tablename=tablename[:-4] |
68 |
|
|
69 |
|
list = [Schema(attrs={'id':tablename,\ |
70 |
|
'name':tablename, \ |
71 |
|
'type':1},\ |
72 |
|
getChildSchema=self.__getFieldSchema)] |
73 |
|
return list |
74 |
|
|
75 |
|
|
76 |
|
# Find a schema object with specified name |
77 |
|
def getSchemaByName(self, name, type=None): |
78 |
|
tablename=self._connection.native.fname |
79 |
|
if tablename[-4:]=='.dbf': |
80 |
|
tablename=tablename[:-4] |
81 |
|
|
82 |
|
if name==tablename: |
83 |
|
return self.getSchemaList() |
84 |
|
else: |
85 |
|
return None |
86 |
|
|
87 |
|
# Get fields for a table |
88 |
|
def __getFieldSchema(self, parent): |
89 |
|
|
90 |
|
list = [] |
91 |
|
for field in self._connection.native.fields: |
92 |
|
|
93 |
|
fname=string.lower(field[0]) |
94 |
|
|
95 |
|
attrs={'id': "%s.%s" % (parent.id,fname), 'name': fname, |
96 |
|
'type':'field', 'nativetype': field[1], |
97 |
|
'required': 0} |
98 |
|
if field[1] == 'C': |
99 |
|
attrs['datatype']='text' |
100 |
|
attrs['length']='%s' % field[2] |
101 |
|
elif field[1] == 'N': |
102 |
|
attrs['datatype']='number' |
103 |
|
elif field[1] == 'D': |
104 |
|
attrs['datatype']='date' |
105 |
|
elif field[1] == 'L': |
106 |
|
attrs['datatype']='boolean' |
107 |
|
else: |
108 |
|
GDebug.printMesg(1,'WARNING: dbf native type error: %s' % field[1]) |
109 |
|
|
110 |
|
list.append(Schema(attrs=attrs)) |
111 |
|
|
112 |
|
return list |
113 |
|
|