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

Diff of /gnue-common/src/datasources/drivers/oracle/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:24 2003 UTC revision 1.2 by jcater, Tue Nov 25 17:01:40 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 =  [ ('user_table',   _('User Tables'),1),
43                 ('user_view',    _('User Views'),1),
44                 ('user_synonym', _('User Synonyms'),1),
45                 ('all_table',    _('System Tables'),1),
46                 ('all_view',     _('System Views'),1),
47                 ('all_synonym',  _('System Synonyms'),1) ]
48    
49      #
50      # TODO: This is a quick hack to get this class
51      # TODO: into the new-style schema format.
52      # TODO: getSchema* should be merged into find()
53      #
54      def find(self, name=None, type=None):
55        if name is None:
56          return self.getSchemaList(type)
57        else:
58          rs = self.getSchemaByName(name, type)
59          if rs:
60            return [rs]
61          else:
62            return None
63    
64    
65      # TODO: Merge into find()
66      # Return a list of Schema objects
67      # Return a list of Schema objects
68      def getSchemaList(self, type=None):
69    
70        where_user = ""
71        if type == None:
72          where_type = ['TABLE', 'VIEW', 'SYNONYM']
73        else:
74          scope, type = string.split(type,'_')
75          where_type = [string.upper(type)]
76          if scope == 'user':
77            where_user = " AND OWNER = USER"
78    
79    
80        statement = \
81          "select owner||'.'||table_name||'.'||table_type full_name, \n" + \
82          "  decode(owner,user,null,owner||'.')||table_name table_name, \n" + \
83          "  decode(owner,user,'user_','all_')||lower(table_type) table_type \n" + \
84          "  from all_catalog where table_type in ('%s') %s \n" \
85                  % (string.join(where_type,"','"), where_user) + \
86          "  order by table_name "
87    
88        GDebug.printMesg(5,statement)
89    
90        cursor = self._connection.native.cursor()
91        cursor.execute(statement)
92    
93        list = []
94        for rs in cursor.fetchall():
95          list.append(GIntrospection.Schema(attrs={'id':rs[0], 'name':string.lower(rs[1]),
96                             'type':rs[2]},
97                             getChildSchema=self.__getFieldSchema))
98    
99        cursor.close()
100        return list
101    
102    
103      # Find a schema object with specified name
104      def getSchemaByName(self, name, type=None):
105    
106        spl = string.split(string.upper(name),'.')
107        where = "TABLE_NAME='%s'" % spl[-1]
108        if len(spl) > 1:
109          where += " AND OWNER='%s'" % spl[-2]
110    
111        statement = \
112          "select owner||'.'||table_name||'.'||table_type full_name, \n" + \
113          "  decode(owner,user,null,owner||'.')||table_name table_name, \n" + \
114          "  decode(owner,user,'user_','all_')||lower(table_type) table_type \n" + \
115          "  from all_catalog where %s " \
116                  % (where)
117    
118        GDebug.printMesg(5,statement)
119    
120        cursor = self._connection.native.cursor()
121        cursor.execute(statement)
122    
123        list = []
124        rs = cursor.fetchone()
125        if rs:
126          rv = GIntrospection.Schema(attrs={'id':rs[0], 'name':string.lower(rs[1]),
127                             'type':rs[2]},
128                             getChildSchema=self.__getFieldSchema)
129        else:
130          rv = None
131    
132        cursor.close()
133        return rv
134    
135    
136      # Get fields for a table
137      def __getFieldSchema(self, parent):
138    
139        owner, name, type = string.split(parent.id,'.')
140    
141        cursor = self._connection.native.cursor()
142    
143        if type == 'SYNONYM':
144          statement = "select table_owner, table_name, " + \
145                      "decode(db_link,null,'','@'||db_link) name " + \
146                      "from all_synonyms " + \
147                      "where owner = '%s' and synonym_name='%s'" % (owner, name)
148    
149          GDebug.printMesg(5,statement)
150    
151          cursor.execute(statement)
152          rs = cursor.fetchone()
153          owner, name, link = rs
154          if link is None:
155            link = ""
156        else:
157          link = ""
158    
159        statement = \
160           "select owner||'.'||table_name||'.'||column_name||'.%s', " % (link) + \
161           "column_name, data_type, nullable, data_length, data_scale, data_precision " + \
162           "from all_tab_columns%s " % (link) + \
163           "where owner = '%s' and table_name = '%s' " % (owner, name) + \
164           "order by column_id"
165    
166        GDebug.printMesg(5,statement)
167    
168        cursor.execute(statement)
169    
170        list = []
171        for rs in cursor.fetchall():
172    
173          attrs={'id': rs[0], 'name': string.lower(rs[1]),
174                 'type':'field', 'nativetype': rs[2],
175                 'required': rs[3] == 'N'}
176    
177          if rs[2] in ('NUMBER',):
178            attrs['precision'] = int(rs[5])
179            attrs['datatype'] = 'number'
180            attrs['length'] = int(rs[6])
181          elif rs[2] in ('DATE',):
182            attrs['datatype'] = 'date'
183          else:
184            attrs['datatype'] = 'text'
185            if int(rs[4]):
186              attrs['length'] = int(rs[4])
187    
188          list.append(GIntrospection.Schema(attrs=attrs))
189    
190        cursor.close()
191        return tuple(list)
192    

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