~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/datasources/drivers/gadfly/Schema/Discovery/Introspection.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2005 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.datasources import GIntrospection
 
37
 
 
38
class Introspection(GIntrospection.Introspection):
 
39
 
 
40
  # list of the types of Schema objects this driver provides
 
41
  types =[ ('view',_('Views'),1),
 
42
           ('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
  # Schema (metadata) functions
 
64
  #
 
65
  def getSchemaList(self, type=None):
 
66
 
 
67
    statement = "select * from __table_names__"
 
68
 
 
69
    cursor = self._connection.native.cursor()
 
70
    GDebug.printMesg(3,"** Executing: %s **" % statement)
 
71
    cursor.execute(statement)
 
72
 
 
73
    list = []
 
74
    for rs in cursor.fetchall():
 
75
      # exclude any system tables and views. f.e. __table_names__
 
76
      if rs[1][:2]!="__":
 
77
        list.append(GIntrospection.Schema(attrs={'id':rs[1], 'name':rs[1], \
 
78
                                'type':rs[0] == 1 and 'view' or 'table',},
 
79
                                       getChildSchema=self.__getFieldSchema))
 
80
 
 
81
    cursor.close()
 
82
    return list
 
83
 
 
84
 
 
85
  # Find a schema object with specified name
 
86
  def getSchemaByName(self, name, type=None):
 
87
    statement = "SELECT * from __table_names__ WHERE TABLE_NAME='%s'" % (name)
 
88
 
 
89
    cursor = self._connection.native.cursor()
 
90
    GDebug.printMesg(3,"** Executing: %s **" % statement)
 
91
    cursor.execute(statement)
 
92
 
 
93
    rs = cursor.fetchone()
 
94
    if rs:
 
95
      schema = GIntrospection.Schema(attrs={'id':rs[1], 'name':rs[1], \
 
96
                                'type':rs[0] == 1 and 'view' or 'table',},
 
97
                                       getChildSchema=self.__getFieldSchema)
 
98
    else:
 
99
      schema = None
 
100
 
 
101
    cursor.close()
 
102
    return schema
 
103
 
 
104
 
 
105
  # Get fields for a table
 
106
  def __getFieldSchema(self, parent):
 
107
 
 
108
    # TODO: Read whole definitions from __DATADEFS__ and parse them
 
109
    #       to distinguish between varchar, float and integer
 
110
    
 
111
    statement = "SELECT * FROM __COLUMNS__ WHERE TABLE_NAME='%s'" % parent.id
 
112
 
 
113
    cursor = self._connection.native.cursor()
 
114
    GDebug.printMesg(3,"** Executing: %s **" % statement)
 
115
    cursor.execute(statement)
 
116
    columns = cursor.description
 
117
 
 
118
    list = []
 
119
    for rs in cursor.fetchall():
 
120
 
 
121
      #nativetype = string.split(string.replace(rs[1],')',''),'(')
 
122
 
 
123
 
 
124
      attrs={'id': "%s.%s" % (parent.id, rs[0]), 'name': rs[0],
 
125
             'type':'field', 'nativetype': 'varchar',
 
126
             'required': 0}
 
127
 
 
128
      #if nativetype[0] in ('int','integer','bigint','mediumint',
 
129
      #                     'smallint','tinyint','float','real',
 
130
      #                     'double','decimal'):
 
131
      #  attrs['datatype']='number'
 
132
      #elif nativetype[0] in ('date','time','timestamp','datetime'):
 
133
      #  attrs['datatype']='date'
 
134
      #else:
 
135
      #  attrs['datatype']='text'
 
136
 
 
137
      ## MORE EVILNESS
 
138
      attrs['datatype']='text'
 
139
      ##END HACK
 
140
      
 
141
      #try:
 
142
      #  if len(nativetype) == 2:
 
143
      #    attrs['length'] = int(string.split(nativetype[1])[0])
 
144
      #except ValueError:
 
145
      #  GDebug.printMesg(1,'WARNING: mysql native type error: %s' % nativetype)
 
146
 
 
147
      list.append(GIntrospection.Schema(attrs=attrs))
 
148
 
 
149
    cursor.close()
 
150
    return list
 
151