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