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 |
|
# |
64 |
|
# Schema (metadata) functions |
65 |
|
# |
66 |
|
|
67 |
|
# TODO: See postgresql for an example of what these functions do. |
68 |
|
|
69 |
|
# Return a list of Schema objects |
70 |
|
def getSchemaList(self, type=None): |
71 |
|
QueryType = adSchemaTables |
72 |
|
rs = self._connection.native.OpenSchema(QueryType) |
73 |
|
|
74 |
|
rs.MoveFirst() |
75 |
|
tables = [] |
76 |
|
while not rs.EOF: |
77 |
|
if str(rs.Fields(TABLE_TYPE)) == 'TABLE': |
78 |
|
tables.append((str(rs.Fields(TABLE_NAME)),'table')) |
79 |
|
elif str(rs.Fields(TABLE_TYPE)) == 'VIEW': |
80 |
|
tables.append((str(rs.Fields(TABLE_NAME)),'view')) |
81 |
|
rs.MoveNext() |
82 |
|
|
83 |
|
list = [] |
84 |
|
for t in tables: |
85 |
|
list.append(GIntrospection.Schema(attrs={'id':t[0], 'name':t[0], |
86 |
|
'type':t[1], |
87 |
|
'primarykey': self.__getPrimaryKey(t[0])}, |
88 |
|
getChildSchema=self.__getFieldSchema)) |
89 |
|
return list |
90 |
|
|
91 |
|
# Find a schema object with specified name |
92 |
|
def getSchemaByName(self, name, type=None): |
93 |
|
|
94 |
|
schema = GIntrospection.Schema(attrs={'id':name, 'name':name, |
95 |
|
'type':'table', |
96 |
|
'primarykey': self.__getPrimaryKey(name)}, |
97 |
|
getChildSchema=self.__getFieldSchema) |
98 |
|
return schema |
99 |
|
|
100 |
|
# Return a list of fields (for _buildDeleteStatement and for _buildUpdateStatement) |
101 |
|
def __getPrimaryKey(self, relname): |
102 |
|
# TODO |
103 |
|
return [] |
104 |
|
|
105 |
|
# Get fields for a table |
106 |
|
def __getFieldSchema(self, parent): |
107 |
|
statement = "select * from %s where 0=1" % (parent.name) |
108 |
|
|
109 |
|
cursor = self._connection.native.cursor() |
110 |
|
cursor.execute(statement) |
111 |
|
|
112 |
|
list = [] |
113 |
|
|
114 |
|
for d in cursor.description: |
115 |
|
try: |
116 |
|
nativetype = d[DESCRIPTION_TYPE_CODE] |
117 |
|
except AttributeError: |
118 |
|
nativetype='unknown' |
119 |
|
|
120 |
|
attrs={'id':d[DESCRIPTION_NAME], |
121 |
|
'name':d[DESCRIPTION_NAME], |
122 |
|
'type':'field', |
123 |
|
'nativetype': nativetype, |
124 |
|
'required': d[DESCRIPTION_NULL_OK]==0, |
125 |
|
'length': d[DESCRIPTION_INTERNAL_SIZE]} |
126 |
|
|
127 |
|
if nativetype == SIG2api.NUMBER: |
128 |
|
attrs['datatype']='number' |
129 |
|
attrs['precision']=d[DESCRIPTION_SCALE] |
130 |
|
attrs['length']=d[DESCRIPTION_PRECISION] |
131 |
|
elif nativetype == SIG2api.DATETIME: |
132 |
|
attrs['datatype']='date' |
133 |
|
else: |
134 |
|
attrs['datatype']='text' |
135 |
|
|
136 |
|
list.append(GIntrospection.Schema(attrs=attrs)) |
137 |
|
|
138 |
|
cursor.close() |
139 |
|
return list |
140 |
|
|
141 |
|
|