def __iter__(self): root = Element('database') root.attrib['name'] = '' schemas = {} objects = {} for (schema, obj, desc, columns) in self.retriever: try: schema_elem = schemas[schema] except KeyError: schema_elem = SubElement(root, 'schema') schema_elem.attrib['name'] = schema schemas[schema] = schema_elem try: obj_elem = objects[(schema, obj)] except KeyError: obj_elem = SubElement(schema_elem, 'relation') obj_elem.attrib['name'] = obj objects[(schema, obj)] = obj_elem SubElement(obj_elem, 'description').text = desc for (column, desc) in sorted(columns.iteritems()): col_elem = SubElement(obj_elem, 'column') col_elem.attrib['name'] = column col_elem.text = desc indent(root) yield '<?xml version="1.0" encoding="UTF-8" ?>\n' # Ensure the output is in UTF-8 encoding s = tostring(root) if isinstance(s, unicode): s = s.encode('UTF-8') yield s
def make_trigger(self, trigger): result = Element('trigger') result.attrib['id'] = trigger.identifier result.attrib['name'] = trigger.name result.attrib['relation'] = trigger.relation.identifier result.attrib['time'] = { 'A': 'after', 'B': 'before', 'I': 'instead', }[trigger.trigger_time] result.attrib['event'] = { 'I': 'insert', 'U': 'update', 'D': 'delete', }[trigger.trigger_event] result.attrib['granularity'] = { 'R': 'row', 'S': 'statement', }[trigger.granularity] if trigger.owner: result.attrib['owner'] = trigger.owner if trigger.system: result.attrib['system'] = 'system' if trigger.created: result.attrib['created'] = trigger.created.isoformat() if trigger.description: SubElement(result, 'description').text = trigger.description if trigger.sql: SubElement(result, 'sql').append(CDATA(trigger.sql)) for dependency in trigger.dependency_list: SubElement(result, 'trigdep').attrib['ref'] = dependency.identifier return result
def make_foreign_key(self, key): action_map = { 'A': 'noaction', 'C': 'cascade', 'N': 'setnull', 'R': 'restrict', } result = Element('foreignkey') result.attrib['id'] = key.identifier result.attrib['name'] = key.name result.attrib['ondelete'] = action_map[key.delete_rule] result.attrib['onupdate'] = action_map[key.update_rule] result.attrib['references'] = key.ref_key.identifier if key.owner: result.attrib['owner'] = key.owner if key.system: result.attrib['system'] = 'system' if key.created: result.attrib['created'] = key.created.isoformat() if key.description: SubElement(result, 'description').text = key.description for (field, parent) in key.fields: e = SubElement(result, 'fkeyfield') e.attrib['sourceref'] = field.identifier e.attrib['targetref'] = parent.identifier return result
def make_datatype(self, datatype): result = Element('datatype') result.attrib['id'] = datatype.identifier result.attrib['name'] = datatype.name if datatype.owner: result.attrib['owner'] = datatype.owner if datatype.system: result.attrib['system'] = 'system' if datatype.created: result.attrib['created'] = datatype.created.isoformat() if datatype.variable_size: result.attrib['variable'] = ['size', 'scale'][datatype.variable_scale] if datatype.source: result.attrib['source'] = datatype.source.identifier if datatype.source.variable_size: result.attrib['size'] = str(datatype.size) if datatype.source.variable_scale: result.attrib['scale'] = str(datatype.scale) if datatype.description: SubElement(result, 'description').text = datatype.description return result
def make_alias(self, alias): result = Element('alias') result.attrib['id'] = alias.identifier result.attrib['name'] = alias.name result.attrib['relation'] = alias.relation.identifier if alias.owner: result.attrib['owner'] = alias.owner if alias.system: result.attrib['system'] = 'system' if alias.created: result.attrib['created'] = alias.created.isoformat() if alias.description: SubElement(result, 'description').text = alias.description # XXX Add creation SQL? return result
def make_table(self, table): result = Element('table') result.attrib['id'] = table.identifier result.attrib['name'] = table.name result.attrib['tablespace'] = table.tablespace.identifier if table.owner: result.attrib['owner'] = table.owner if table.system: result.attrib['system'] = 'system' if table.created: result.attrib['created'] = table.created.isoformat() if table.last_stats: result.attrib['laststats'] = table.last_stats.isoformat() if table.cardinality: result.attrib['cardinality'] = str(table.cardinality) if table.size: result.attrib['size'] = str(table.size) if table.description: SubElement(result, 'description').text = table.description # XXX Add reverse dependencies? # XXX Add associated triggers? # XXX Add creation SQL? return result
def make_param(self, param): result = Element('parameter') result.attrib['id'] = param.identifier result.attrib['name'] = param.name result.attrib['type'] = { 'I': 'in', 'O': 'out', 'B': 'inout', 'R': 'return', }[param.type] result.attrib['position'] = str(param.position) result.attrib['datatype'] = param.datatype.identifier if param.datatype.variable_size: if param.size is not None: result.attrib['size'] = str(param.size) if param.datatype.variable_scale: if param.scale is not None: result.attrib['scale'] = str(param.scale) if param.codepage: result.attrib['codepage'] = str(param.codepage) if param.description: SubElement(result, 'description').text = param.description return result
def make_schema(self, schema): result = Element('schema') result.attrib['id'] = schema.identifier result.attrib['name'] = schema.name if schema.owner: result.attrib['owner'] = schema.owner if schema.system: result.attrib['system'] = 'system' if schema.created: result.attrib['created'] = schema.created.isoformat() if schema.description: SubElement(result, 'description').text = schema.description return result
def make_view(self, view): result = Element('view') result.attrib['id'] = view.identifier result.attrib['name'] = view.name if view.owner: result.attrib['owner'] = view.owner if view.system: result.attrib['system'] = 'system' if view.created: result.attrib['created'] = view.created.isoformat() if view.read_only: result.attrib['readonly'] = 'readonly' if view.description: SubElement(result, 'description').text = view.description SubElement(result, 'sql').append(CDATA(view.sql)) for dependency in view.dependency_list: SubElement(result, 'viewdep').attrib['ref'] = dependency.identifier return result
def make_tablespace(self, tablespace): result = Element('tablespace') result.attrib['id'] = tablespace.identifier result.attrib['name'] = tablespace.name result.attrib['type'] = tablespace.type if tablespace.owner: result.attrib['owner'] = tablespace.owner if tablespace.system: result.attrib['system'] = 'system' if tablespace.created: result.attrib['created'] = tablespace.created.isoformat() if tablespace.description: SubElement(result, 'description').text = tablespace.description # XXX Include table and index lists? #for table in tablespace.table_list: # SubElement(result, 'containstable').attrib['ref'] = table.identifier #for index in tablespace.index_list: # SubElement(result, 'containsindex').attrib['ref'] = index.identifier return result
def make_unique_key(self, key): result = Element('uniquekey') result.attrib['id'] = key.identifier result.attrib['name'] = key.name if key.owner: result.attrib['owner'] = key.owner if key.system: result.attrib['system'] = 'system' if key.created: result.attrib['created'] = key.created.isoformat() if key.description: SubElement(result, 'description').text = key.description for field in key.fields: SubElement(result, 'keyfield').attrib['ref'] = field.identifier # XXX Include parent keys? return result
def make_check(self, check): result = Element('check') result.attrib['id'] = check.identifier result.attrib['name'] = check.name if check.owner: result.attrib['owner'] = check.owner if check.system: result.attrib['system'] = 'system' if check.created: result.attrib['created'] = check.created.isoformat() if check.description: SubElement(result, 'description').text = check.description if check.expression: SubElement(result, 'expression').text = check.expression for field in check.fields: SubElement(result, 'checkfield').attrib['ref'] = field.identifier return result
def make_function(self, function): result = Element('function') result.attrib['id'] = function.identifier result.attrib['name'] = function.name result.attrib['specificname'] = function.specific_name result.attrib['type'] = { 'C': 'column', 'R': 'row', 'T': 'table', 'S': 'scalar', }[function.type] result.attrib['access'] = { None: 'none', 'N': 'none', 'C': 'contains', 'R': 'reads', 'M': 'modifies', }[function.sql_access] if function.owner: result.attrib['owner'] = function.owner if function.system: result.attrib['system'] = 'system' if function.created: result.attrib['created'] = function.created.isoformat() if function.deterministic: result.attrib['deterministic'] = 'deterministic' if function.external_action: result.attrib['externalaction'] = 'externalaction' if function.null_call: result.attrib['nullcall'] = 'nullcall' if function.description: SubElement(result, 'description').text = function.description if function.sql: SubElement(result, 'sql').append(CDATA(function.sql)) return result
def make_field(self, field): result = Element('field') result.attrib['id'] = field.identifier result.attrib['name'] = field.name result.attrib['position'] = str(field.position) result.attrib['datatype'] = field.datatype.identifier if field.datatype.variable_size: result.attrib['size'] = str(field.size) if field.datatype.variable_scale: result.attrib['scale'] = str(field.scale) if field.codepage: result.attrib['codepage'] = str(field.codepage) if field.nullable: result.attrib['nullable'] = 'nullable' if field.null_cardinality: result.attrib['null_cardinality'] = str(field.null_cardinality) if field.cardinality: result.attrib['cardinality'] = str(field.cardinality) if field.identity: result.attrib['identity'] = 'identity' if field.generated == 'N': if field.default: result.attrib['default'] = field.default else: result.attrib['generated'] = { 'A': 'always', 'D': 'default', }[field.generated] if field.default: result.attrib['expression'] = field.default if field.description: SubElement(result, 'description').text = field.description # XXX Add key position? # XXX Add creation SQL? return result
def make_index(self, index): result = Element('index') result.attrib['id'] = index.identifier result.attrib['name'] = index.name result.attrib['table'] = index.table.identifier result.attrib['tablespace'] = index.tablespace.identifier if index.owner: result.attrib['owner'] = index.owner if index.system: result.attrib['system'] = 'system' if index.created: result.attrib['created'] = index.created.isoformat() if index.last_stats: result.attrib['laststats'] = index.last_stats.isoformat() if index.cardinality: result.attrib['cardinality'] = str(index.cardinality) if index.size: result.attrib['size'] = str(index.size) if index.unique: result.attrib['unique'] = 'unique' if index.description: SubElement(result, 'description').text = index.description for (field, order) in index.field_list: e = SubElement(result, 'indexfield') e.attrib['ref'] = field.identifier e.attrib['order'] = { 'A': 'asc', 'D': 'desc', 'I': 'include', }[order] # XXX Add creation SQL? return result
def make_procedure(self, procedure): result = Element('procedure') result.attrib['id'] = procedure.identifier result.attrib['name'] = procedure.name result.attrib['specificname'] = procedure.specific_name result.attrib['access'] = { None: 'none', 'N': 'none', 'C': 'contains', 'R': 'reads', 'M': 'modifies', }[procedure.sql_access] if procedure.owner: result.attrib['owner'] = procedure.owner if procedure.system: result.attrib['system'] = 'system' if procedure.created: result.attrib['created'] = procedure.created.isoformat() if procedure.deterministic: result.attrib['deterministic'] = 'deterministic' if procedure.external_action: result.attrib['externalaction'] = 'externalaction' if procedure.null_call: result.attrib['nullcall'] = 'nullcall' if procedure.description: SubElement(result, 'description').text = procedure.description if procedure.sql: SubElement(result, 'sql').append(CDATA(procedure.sql)) return result
def make_database(self, database): result = Element('database') result.attrib['id'] = database.identifier result.attrib['name'] = database.name return result