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

Diff of /gnue-common/src/datasources/drivers/sqlite/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:32 2003 UTC revision 1.2 by jcater, Tue Nov 25 17:01:46 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 =[ ('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      def getSchemaList(self, type=None):
64    
65        if type!=None:
66          where=" WHERE type='%s'" % type
67        else:
68          where=""
69    
70        statement = "SELECT type,name,tbl_name,sql FROM master "+\
71                    where+" UNION ALL "+\
72                    "SELECT type,name,tbl_name,sql FROM temp_master "+\
73                    where+" ORDER BY name;"
74    
75        cursor = self._connection.native.cursor()
76        GDebug.printMesg(1,"** Executing: %s **" % statement)
77        cursor.execute(statement)
78    
79        list = []
80        for rs in cursor.fetchall():
81          if rs[0] in ('table','view'):
82            list.append(GIntrospection.Schema(attrs={'id':rs[1], 'name':rs[1], \
83                                                   'type':rs[0],},
84                                            getChildSchema=self.__getFieldSchema))
85    
86        cursor.close()
87        print list
88        return list
89    
90    
91      # Find a schema object with specified name
92      def getSchemaByName(self, name, type=None):
93    
94        if type!=None:
95          where=" AND type='%s'" % type
96        else:
97          where=""
98    
99        statement = ("SELECT type,name,tbl_name,sql FROM master "+\
100                     "WHERE name='%s'"+where+" UNION ALL "+\
101                     "SELECT type,name,tbl_name,sql FROM temp_master "+\
102                     "WHERE name='%s' "+where+" ORDER BY name;") % (name,name)
103    
104        cursor = self._connection.native.cursor()
105        GDebug.printMesg(1,"** Executing: %s **" % statement)
106        cursor.execute(statement)
107    
108        rs = cursor.fetchone()
109        if rs and rs[0] in ('table','view'):
110          schema = GIntrospection.Schema(attrs={'id':rs[1], 'name':rs[1], \
111                                              'type':rs[0],},
112                                       getChildSchema=self.__getFieldSchema)
113        else:
114          schema = None
115    
116        cursor.close()
117        return schema
118    
119    
120      # Get fields for a table
121      def __getFieldSchema(self, parent):
122    
123        if parent.type=='view':
124          print "Views are not supported at the moment"
125          return None
126    
127        statement = ("SELECT type,name,tbl_name,sql FROM master "+\
128                     "WHERE type='%s' and name='%s' UNION ALL "+\
129                     "SELECT type,name,tbl_name,sql FROM temp_master "+\
130                     "WHERE type='%s' "+\
131                     "and name='%s' ORDER BY name;") % (parent.type,parent.id,\
132                                                        parent.type,parent.id)
133    
134        cursor = self._connection.native.cursor()
135        GDebug.printMesg(1,"** Executing: %s **" % statement)
136        cursor.execute(statement)
137        columns = cursor.description
138    
139        # Because sqlite don't store column definitions, but computes it
140        # every time anew from the 'create table' statement, we have to
141        # parse that statement to get the data
142    
143        # get sql definition of table
144        rs = cursor.fetchone()
145        cursor.close()
146        if rs:
147          sql=rs[3]
148        else:
149          return None
150    
151        # parse the sql definition
152        GDebug.printMesg(3,"** Table definition: %s **" % sql)
153    
154        sql=sql[find(sql,'(')+1:rfind(sql,')')]
155        fields = split(sql,',')
156        list = []
157        for field in fields:
158    
159          fls=split(strip(field),' ',2)
160    
161          if not fls[0] in ('Constraint','Primary'):
162            
163            try:
164              nativetype= fls[1][:find(fls[1],'(')]
165    
166              size=int(fls[1][find(fls[1],'(')+1:-1])
167            except:
168              nativetype = fls[1]
169              size=None
170            
171            attrs={'id': "%s.%s" % (parent.id, fls[0]), 'name': fls[0],
172                   'type':'field', 'nativetype': nativetype,
173                   'required':fls[2]=="NOT NULL"}
174            
175            if size!=None:
176              attrs['length'] = size
177            
178            if nativetype in ('int','integer','bigint','mediumint',
179                               'smallint','tinyint','float','real',
180                               'double','decimal'):
181              attrs['datatype']='number'
182            elif nativetype[0] in ('date','time','timestamp','datetime'):
183              attrs['datatype']='date'
184            else:
185              attrs['datatype']='text'
186    
187            list.append(GIntrospection.Schema(attrs=attrs))
188    
189        return list
190    

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