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

Diff of /gnue-common/src/datasources/drivers/mysql/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:20 2003 UTC revision 1.2 by jcater, Tue Nov 25 17:01:38 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    
44      #
45      # TODO: This is a quick hack to get this class
46      # TODO: into the new-style schema format.
47      # TODO: getSchema* should be merged into find()
48      #
49      def find(self, name=None, type=None):
50        if name is None:
51          return self.getSchemaList(type)
52        else:
53          rs = self.getSchemaByName(name, type)
54          if rs:
55            return [rs]
56          else:
57            return None
58    
59    
60      # TODO: Merge into find()
61      # Return a list of Schema objects
62    
63      #
64      # Schema (metadata) functions
65      #
66    
67      # Return a list of Schema objects
68      def getSchemaList(self, type=None):
69    
70        # TODO: This excludes any system tables and views. Should it?
71        statement = "SHOW TABLES"
72    
73        cursor = self._connection.native.cursor()
74        cursor.execute(statement)
75    
76        list = []
77        for rs in cursor.fetchall():
78          list.append(GIntrospection.Schema(attrs={'id':rs[0], 'name':rs[0],
79                             'type':'table',
80                             'primarykey': self.__getPrimaryKey(rs[0])},
81                             getChildSchema=self.__getFieldSchema))
82    
83        cursor.close()
84        return list
85    
86    
87      # Find a schema object with specified name
88      def getSchemaByName(self, name, type=None):
89        statement = "DESCRIBE %s" % (name)
90    
91        cursor = self._connection.native.cursor()
92        cursor.execute(statement)
93    
94        rs = cursor.fetchone()
95        if rs:
96          schema = GIntrospection.Schema(attrs={'id':name, 'name':name,
97                               'type':'table',
98                               'primarykey': self.__getPrimaryKey(name,cursor)},
99                               getChildSchema=self.__getFieldSchema)
100        else:
101          schema = None
102    
103        cursor.close()
104        return schema
105    
106    
107      def __getPrimaryKey(self, id, cursor=None):
108        statement = "DESCRIBE %s" % id
109        if not cursor:
110          cursor = self._connection.native.cursor()
111          close_cursor = 1
112        else:
113          close_cursor = 0
114        cursor.execute(statement)
115    
116        lst = []
117        for rs in cursor.fetchall():
118          if rs[3] == 'PRI':
119            lst.append(rs[0])
120    
121        if close_cursor:
122          cursor.close()
123    
124        return tuple(lst)
125    
126      # Get fields for a table
127      def __getFieldSchema(self, parent):
128    
129        statement = "DESCRIBE %s" % parent.id
130    
131        cursor = self._connection.native.cursor()
132        cursor.execute(statement)
133    
134        list = []
135        for rs in cursor.fetchall():
136    
137          nativetype = string.split(string.replace(rs[1],')',''),'(')
138    
139    
140          attrs={'id': "%s.%s" % (parent.id, rs[0]), 'name': rs[0],
141                 'type':'field', 'nativetype': nativetype[0],
142                 'required': rs[2] != 'YES'}
143    
144          if nativetype[0] in ('int','integer','bigint','mediumint',
145                               'smallint','tinyint','float','real',
146                               'double','decimal'):
147            attrs['datatype']='number'
148          elif nativetype[0] in ('date','time','timestamp','datetime'):
149            attrs['datatype']='date'
150          else:
151            attrs['datatype']='text'
152    
153          try:
154            if len(nativetype) == 2:
155              try:
156                ln, prec = nativetype[1].split(',')
157              except:
158                ln = nativetype[1]
159                prec = None
160              attrs['length'] = int(ln.split()[0])
161              if prec != None:
162                attrs['precision'] = int(prec)
163          except ValueError:
164            GDebug.printMesg(1,'WARNING: mysql native type error: %s' % nativetype)
165    
166          if rs[4] not in ('NULL', '0000-00-00 00:00:00','', None):
167            attrs['defaulttype'] = 'constant'
168            attrs['defaultval'] = rs[4]
169    
170          if rs[5] == 'auto_increment':
171            attrs['defaulttype'] = 'serial'
172    
173    
174          list.append(GIntrospection.Schema(attrs=attrs))
175    
176        cursor.close()
177        return list
178    

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