27 |
|
|
28 |
# write schema support |
# write schema support |
29 |
from gnue.common.schema.Objects import * |
from gnue.common.schema.Objects import * |
30 |
|
from gnue.common.definitions.GParserHelpers import GContent |
31 |
|
|
32 |
|
SCHEMA_STORE_BOTH = 0 |
33 |
|
SCHEMA_STORE_SCHEMA = 1 |
34 |
|
SCHEMA_STORE_DATA = 2 |
35 |
|
|
36 |
class SchemaSupport: |
class SchemaSupport: |
37 |
|
|
51 |
|
|
52 |
def loadSchemaFromFile(self, filename, overwrite=0, install=0): |
def loadSchemaFromFile(self, filename, overwrite=0, install=0): |
53 |
|
|
54 |
input = openResource(schema) |
input = openResource(filename) |
55 |
|
|
56 |
schema = GSParser.loadFile(input) |
schema = GSParser.loadFile(input) |
57 |
|
|
58 |
|
for table in schema.findChildrenOfType('GSTable',allowAllChildren=1): |
59 |
|
print 'adding Table %s' % table.name |
60 |
|
for field in table.findChildrenOfType('GSField',allowAllChildren=1): |
61 |
|
print ' with field %s:' % field.name |
62 |
|
|
63 |
if overwrite: |
if overwrite: |
64 |
raise "Overwrite support not working yet." |
raise "Overwrite support not working yet." |
74 |
# Parameter: filename: name of file to write to |
# Parameter: filename: name of file to write to |
75 |
# classlist: list of classes to write to |
# classlist: list of classes to write to |
76 |
# TODO: a) add global class repository locking |
# TODO: a) add global class repository locking |
77 |
|
# b) add a (include system tables) parameter |
78 |
# --------------------------------------------------------------------------- |
# --------------------------------------------------------------------------- |
79 |
|
|
80 |
def writeSchemaToFile(self, filename, classlist='all tables'): |
def writeSchemaToFile(self, filename, classlist='all tables', type=SCHEMA_STORE_BOTH): |
81 |
|
|
82 |
schema = GSSchema() |
schema = GSSchema() |
83 |
schema.title='Appserver Schema Dump' |
schema.title='Appserver Schema Dump' |
87 |
|
|
88 |
gstables = GSTables(schema) |
gstables = GSTables(schema) |
89 |
|
|
90 |
# load all tables first |
if (classlist=='all tables'): |
91 |
|
# load all tables first |
92 |
|
tablelist=[] |
93 |
for tablename in self._classes.keys(): |
for tablename in self._classes.keys(): |
94 |
if (classlist=='all tables') or (tablename in classlist): |
tablelist.append(tablename) |
95 |
table=GSTable(gstables) |
else: |
96 |
table.name=tablename |
tablelist=classlist |
97 |
#table.description='' |
|
98 |
|
if (type==SCHEMA_STORE_BOTH) or (type==SCHEMA_STORE_SCHEMA): |
99 |
# add fields listing (required) |
for tablename in tablelist: |
100 |
fields=GSFields(table) |
self.createTableDef(gstables, tablename) |
101 |
|
|
102 |
proplist=self._classes[tablename].properties |
if (type==SCHEMA_STORE_BOTH) or (type==SCHEMA_STORE_DATA): |
103 |
|
gsdata = GSData(schema) |
104 |
for propname in proplist.keys(): |
self.createDataList(gsdata, tablelist) |
105 |
# add one field per property |
|
|
field = GSField(fields) |
|
|
field.name=propname |
|
|
field.type=proplist[propname].gnue_type |
|
|
field.length=int(proplist[propname].gnue_length) |
|
|
field.precision=int(proplist[propname].gnue_scale) |
|
|
field.description=proplist[propname].gnue_comment |
|
|
# TODO: where should 'gnue_module' be stored? |
|
|
|
|
106 |
dest = open(filename,"w") |
dest = open(filename,"w") |
107 |
|
|
108 |
dest.write("""<?xml version="1.0"?> |
dest.write("""<?xml version="1.0"?> |
113 |
dest.write(schema.dumpXML()) |
dest.write(schema.dumpXML()) |
114 |
dest.close() |
dest.close() |
115 |
|
|
116 |
|
def createTableDef(self, gstables, tablename): |
117 |
|
table=GSTable(gstables) |
118 |
|
table.name=tablename |
119 |
|
#table.description='' |
120 |
|
|
121 |
|
# add fields listing (required) |
122 |
|
fields=GSFields(table) |
123 |
|
GSConstraints(table) |
124 |
|
GSIndexes(table) |
125 |
|
|
126 |
|
proplist=self._classes[tablename].properties |
127 |
|
|
128 |
|
for propname in proplist.keys(): |
129 |
|
# add one field per property |
130 |
|
field = GSField(fields) |
131 |
|
field.name=propname |
132 |
|
field.type=proplist[propname].gnue_type |
133 |
|
field.length=int(proplist[propname].gnue_length) |
134 |
|
field.precision=int(proplist[propname].gnue_scale) |
135 |
|
field.description=proplist[propname].gnue_comment |
136 |
|
# TODO: where should 'gnue_module' be stored? |
137 |
|
|
138 |
|
# set gnue_id to be a primary key |
139 |
|
if (propname=='gnue_id') and (proplist[propname].gnue_type=='id'): |
140 |
|
pk=GSPrimaryKey(table) |
141 |
|
pk.name='gnue_id_pk' |
142 |
|
pkf=GSPKField(pk) |
143 |
|
pkf.name='gnue_id' |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
def createDataList(self, gsdata, tablelist): |
148 |
|
# TODO: do we need to add information about modules too? |
149 |
|
|
150 |
|
tabledata=GSTableData(gsdata) |
151 |
|
tabledata.name='gnue_class_dump' |
152 |
|
tabledata.tablename='gnue_class' |
153 |
|
tablerows=GSRows(tabledata) |
154 |
|
|
155 |
|
propdata=GSTableData(gsdata) |
156 |
|
propdata.name='gnue_property_dump' |
157 |
|
propdata.tablename='gnue_property' |
158 |
|
proprows=GSRows(propdata) |
159 |
|
|
160 |
|
for tablename in tablelist: |
161 |
|
classdef = self._classes[tablename] |
162 |
|
proplist=classdef.properties |
163 |
|
|
164 |
|
# save tabledata |
165 |
|
row = GSRow(tablerows) |
166 |
|
self._buildValue(row,'gnue_id',classdef.gnue_id) |
167 |
|
self._buildValue(row,'gnue_module',classdef.gnue_module) |
168 |
|
self._buildValue(row,'gnue_name',classdef.gnue_name) |
169 |
|
self._buildValue(row,'gnue_comment',classdef.gnue_comment) |
170 |
|
|
171 |
|
for propname in proplist.keys(): |
172 |
|
# add one field per property |
173 |
|
row = GSRow(proprows) |
174 |
|
row.name=propname |
175 |
|
self._buildValue(row,'gnue_id',proplist[propname].gnue_id) |
176 |
|
self._buildValue(row,'gnue_module',proplist[propname].gnue_module) |
177 |
|
self._buildValue(row,'gnue_class',proplist[propname].gnue_class) |
178 |
|
self._buildValue(row,'gnue_name',proplist[propname].gnue_name) |
179 |
|
self._buildValue(row,'gnue_type',proplist[propname].gnue_type) |
180 |
|
self._buildValue(row,'gnue_length',int(proplist[propname].gnue_length)) |
181 |
|
self._buildValue(row,'gnue_scale',int(proplist[propname].gnue_scale)) |
182 |
|
self._buildValue(row,'gnue_comment',proplist[propname].gnue_comment) |
183 |
|
|
184 |
|
def _buildValue(self,row,field,data): |
185 |
|
val = GSValue(row) |
186 |
|
#val.field=field |
187 |
|
GContent(val,"'%s'" % data) |
188 |
|
|
189 |
# ============================================================================= |
# ============================================================================= |
190 |
# test program |
# test program |
191 |
# ============================================================================= |
# ============================================================================= |
200 |
gsdSupp = SchemaSupport(sm._modules,sm._classes) |
gsdSupp = SchemaSupport(sm._modules,sm._classes) |
201 |
|
|
202 |
gsdSupp.writeSchemaToFile('AS_schema.gsd') |
gsdSupp.writeSchemaToFile('AS_schema.gsd') |
203 |
|
gsdSupp.writeSchemaToFile('AS_schema2.gsd',['address_person']) |
204 |
|
|
205 |
#gsdSupp.loadSchemaFromFile('AS_schema.gsd',overwrite=1) |
gsdSupp.loadSchemaFromFile('AS_schema.gsd',overwrite=0) |
206 |
|
|