/[gnue]/gnue-common/src/datasources/drivers/db2/Schema/Discovery/Introspection.py
ViewVC logotype

Diff of /gnue-common/src/datasources/drivers/db2/Schema/Discovery/Introspection.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by jcater, Fri Oct 10 01:21:12 2003 UTC revision 1.2 by jcater, Tue Nov 25 17:01:33 2003 UTC
# Line 0  Line 1 
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                 ('alias',    _('Aliases'),1),
45                 ('summary',  _('Summary Tables'),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      # Schema (metadata) functions
67      #
68    
69      # Return a list of Schema objects
70      def getSchemaList(self, type=None):
71    
72        where_user = ""
73        if type == None:
74          where_type = ['A','S','T','V']
75        else:
76          where_type = [string.upper(type[0])]
77    
78    
79        statement = \
80          "select tabschema||'.'||tabname||'.'||type full_name, " + \
81            "tabschema||'.'||tabname table_name, " + \
82            "type table_type " + \
83            "from syscat.tables where type in ('%s') and status = 'N' %s " \
84                  % (string.join(where_type,"','"), where_user) + \
85              "order by tabname "
86    
87        GDebug.printMesg(5,statement)
88    
89        cursor = self._connection.native.cursor()
90        cursor.execute(statement)
91    
92        list = []
93        for rs in cursor.fetchall():
94          list.append(GIntrospection.Schema(attrs={'id':string.lower(rs[0]), 'name':rs[1],
95                             'type':rs[2]},
96                             getChildSchema=self.__getFieldSchema))
97    
98        cursor.close()
99        return list
100    
101    
102      # Find a schema object with specified name
103      def getSchemaByName(self, name, type=None):
104    
105        where_user = ""
106        parts = string.split(string.upper(name),'.')
107        name = parts[-1]
108        if len(parts) > 1:
109          schema = " and tabschema='%s'" % parts[-2]
110        else:
111          schema = ""
112    
113        statement = \
114          "select tabschema||'.'||tabname||'.'||type full_name, " + \
115            "tabschema||'.'||tabname table_name, " + \
116            "type table_type " + \
117            "from syscat.tables where tabname == '%s' and status = 'N' %s " \
118                  % (name, schema) + \
119              "order by tabname "
120    
121        GDebug.printMesg(5,statement)
122    
123        cursor = self._connection.native.cursor()
124        cursor.execute(statement)
125    
126        list = []
127        for rs in cursor.fetchall():
128          list.append(GIntrospection.Schema(attrs={'id':string.lower(rs[0]), 'name':rs[1],
129                             'type':rs[2]},
130                             getChildSchema=self.__getFieldSchema))
131    
132        cursor.close()
133    
134        try:
135          return list[0]
136        except:
137          return None
138    
139    
140    
141      # Get fields for a table
142      def __getFieldSchema(self, parent):
143    
144        # TODO: This does not support user-defined datatypes...
145        # TODO: it will always report such as TEXT-like fields.
146    
147        schema, name, type = string.split(parent.id,'.')
148    
149        cursor = self._connection.native.cursor()
150    
151        if type == 'a':
152          statement = "select base_tabschema, base_tabname " + \
153                      "from syscat.tables " + \
154                      "where tabschema = '%s' and tabname='%s'" % (schema, name)
155    
156          GDebug.printMesg(5,statement)
157    
158          cursor.execute(statement)
159          rs = cursor.fetchone()
160          schema, name = rs
161    
162        statement = \
163           "select tabschema||'.'||tabname||'.'||colname, " + \
164           "colname, typename, nulls, length, scale, typeschema " + \
165           "from syscat.columns" + \
166           "where tabschema = '%s' and tabname = '%s' " % (schema, name) + \
167           "order by colno"
168    
169        GDebug.printMesg(5,statement)
170    
171        cursor.execute(statement)
172    
173        list = []
174        for rs in cursor.fetchall():
175    
176          attrs={'id': rs[0], 'name': rs[1],
177                 'type':'field', 'nativetype': rs[2],
178                 'required': rs[3] == 'Y'}
179    
180          if rs[2] in ('SMALLINT','INTEGER','INT','FLOAT','DOUBLE',
181                       'DECIMAL','BIGINT','REAL','DEC','NUMERIC','NUMBER',
182                       'NUM'):
183            attrs['precision'] = rs[5]
184            attrs['datatype'] = 'number'
185          elif rs[2] in ('DATE','TIME','TIMESTAMP'):
186            attrs['datatype'] = 'date'
187          else:
188            attrs['datatype'] = 'text'
189    
190          if rs[5] != 0:
191            attrs['length'] = rs[4]
192    
193          list.append(GIntrospection.Schema(attrs=attrs))
194    
195        cursor.close()
196        return tuple(list)
197    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26