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 =[ ('table', _('Tables'),1), |
43 |
|
('view', _('Views'), 1), |
44 |
|
('synonym', _('Synonyms'),1), |
45 |
|
('result', _('Result Table'),1) ] |
46 |
|
|
47 |
|
# |
48 |
|
# TODO: This is a quick hack to get this class |
49 |
|
# TODO: into the new-style schema format. |
50 |
|
# TODO: getSchema* should be merged into find() |
51 |
|
# |
52 |
|
def find(self, name=None, type=None): |
53 |
|
if name is None: |
54 |
|
return self.getSchemaList(type) |
55 |
|
else: |
56 |
|
rs = self.getSchemaByName(name, type) |
57 |
|
if rs: |
58 |
|
return [rs] |
59 |
|
else: |
60 |
|
return None |
61 |
|
|
62 |
|
|
63 |
|
# TODO: Merge into find() |
64 |
|
# Return a list of Schema objects |
65 |
|
|
66 |
|
# Return a list of Schema objects |
67 |
|
def getSchemaList(self, type=None): |
68 |
|
|
69 |
|
where_user = "" |
70 |
|
if type == None: |
71 |
|
where_type = "where TYPE <> 'SYSTEM' and TYPE <> 'SYNONYM' " |
72 |
|
else: |
73 |
|
where_type = "where TYPE='%s'" % string.upper(type) |
74 |
|
|
75 |
|
|
76 |
|
statement = \ |
77 |
|
"select owner||'.'||tablename||'.'||type, " + \ |
78 |
|
"owner||'.'||tablename table_name, " + \ |
79 |
|
"type table_type " + \ |
80 |
|
"from domain.tables %s" \ |
81 |
|
% (where_type) + \ |
82 |
|
"order by tablename " |
83 |
|
|
84 |
|
GDebug.printMesg(5,statement) |
85 |
|
|
86 |
|
cursor = self._connection.native.cursor() |
87 |
|
cursor.execute(statement) |
88 |
|
|
89 |
|
list = [] |
90 |
|
for rs in cursor.fetchall(): |
91 |
|
list.append(GIntrospection.Schema(attrs={'id':rs[0], 'name':rs[1], |
92 |
|
'type':string.lower(rs[2])}, |
93 |
|
getChildSchema=self.__getFieldSchema)) |
94 |
|
|
95 |
|
cursor.close() |
96 |
|
return list |
97 |
|
|
98 |
|
|
99 |
|
# Find a schema object with specified name |
100 |
|
def getSchemaByName(self, name, type=None): |
101 |
|
|
102 |
|
where_user = "" |
103 |
|
parts = string.split(string.upper(name),'.') |
104 |
|
name = parts[-1] |
105 |
|
if len(parts) > 1: |
106 |
|
schema = " and owner='%s'" % parts[-2] |
107 |
|
else: |
108 |
|
schema = "" |
109 |
|
|
110 |
|
statement = \ |
111 |
|
"select owner||'.'||tablename||'.'||type, " + \ |
112 |
|
"owner||'.'||tablename table_name, " + \ |
113 |
|
"type table_type, " + \ |
114 |
|
"owner, tablename " + \ |
115 |
|
"from domain.tables where tablename='%s'%s" \ |
116 |
|
% (name, schema) + \ |
117 |
|
"order by tablename " |
118 |
|
|
119 |
|
GDebug.printMesg(5,statement) |
120 |
|
|
121 |
|
cursor = self._connection.native.cursor() |
122 |
|
cursor.execute(statement) |
123 |
|
|
124 |
|
list = [] |
125 |
|
for rs in cursor.fetchall(): |
126 |
|
list.append(GIntrospection.Schema(attrs={'id':string.lower(rs[0]), 'name':rs[1], |
127 |
|
'type':rs[2], 'sapdbId': (rs [3], rs [4])}, |
128 |
|
getChildSchema=self.__getFieldSchema)) |
129 |
|
|
130 |
|
cursor.close() |
131 |
|
|
132 |
|
try: |
133 |
|
return list[0] |
134 |
|
except: |
135 |
|
return None |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
# Get fields for a table |
140 |
|
def __getFieldSchema(self, parent): |
141 |
|
|
142 |
|
# TODO: This does not support user-defined datatypes... |
143 |
|
# TODO: it will always report such as TEXT-like fields. |
144 |
|
|
145 |
|
schema, name, type = string.split(parent.id,'.') |
146 |
|
owner, basename = parent.sapdbId |
147 |
|
cursor = self._connection.native.cursor() |
148 |
|
|
149 |
|
# if type == 'synonym': |
150 |
|
# statement = "select base_tabschema, base_tabname " + \ |
151 |
|
# "from syscat.tables " + \ |
152 |
|
# "where tabschema = '%s' and tabname='%s'" % (schema, name) |
153 |
|
# |
154 |
|
# GDebug.printMesg(5,statement) |
155 |
|
# |
156 |
|
# cursor.execute(statement) |
157 |
|
# rs = cursor.fetchone() |
158 |
|
# schema, name = rs |
159 |
|
|
160 |
|
statement = \ |
161 |
|
"select owner||'.'||tablename||'.'||columnname, " + \ |
162 |
|
"columnname, datatype, 'Y', len, dec " + \ |
163 |
|
"from domain.columns " + \ |
164 |
|
"where owner = '%s' and tablename = '%s' " % (owner, basename) + \ |
165 |
|
'order by "POS"' |
166 |
|
|
167 |
|
GDebug.printMesg(5,statement) |
168 |
|
|
169 |
|
cursor.execute(statement) |
170 |
|
|
171 |
|
list = [] |
172 |
|
for rs in cursor.fetchall(): |
173 |
|
|
174 |
|
attrs={'id': rs[0], 'name': rs[1], |
175 |
|
'type':'field', 'nativetype': rs[2], |
176 |
|
'required': 'N'} |
177 |
|
|
178 |
|
if rs[2] in ('BOOLEAN','FIXED','FLOAT','INTEGER','LONG','SMALLINT'): |
179 |
|
attrs['precision'] = rs[5] |
180 |
|
attrs['datatype'] = 'number' |
181 |
|
elif rs[2] in ('DATE','TIME','TIMESTAMP'): |
182 |
|
attrs['datatype'] = 'date' |
183 |
|
else: |
184 |
|
attrs['datatype'] = 'text' |
185 |
|
|
186 |
|
if rs[5] != 0: |
187 |
|
attrs['length'] = rs[4] |
188 |
|
|
189 |
|
list.append(GIntrospection.Schema(attrs=attrs)) |
190 |
|
|
191 |
|
cursor.close() |
192 |
|
return tuple(list) |
193 |
|
|