# import the wb module, where various utilities for working with plugins are defined from wb import * # import module for working with Workbench data structures import grt import mforms from mforms import newLabel, newBox, newTextEntry, newButton, FileChooser # create a module information descriptor. The variable name must be ModuleInfo ModuleInfo = DefineModule(name= "importplugin", author= "Hannes Pfannkuch", version="1.0") # export a function from this module, declaring its return and parameter types and then # tell WB that it is a plugin to be shown in the Catalog submenu of the Plugins menu and takes the # catalog of the currently loaded model as input @ModuleInfo.plugin("nql.plugin.import", caption= "Import NQL create statements", input= [wbinputs.currentCatalog()], pluginMenu= "Catalog") @ModuleInfo.export(grt.INT, grt.classes.db_Catalog) def importTable(catalog): f = Dialog() if f.result == mforms.ResultOk and (f.tbfile.get_string_value()!=''): schema = catalog.defaultSchema myfile = open(f.tbfile.get_string_value(),'r') for line in myfile: if line.startswith('Create'): line = line[7:] bracket = line.find('(') tableName = line[0:bracket] tableName = tableName.replace('`', '') table = schema.addNewTable('db.mysql') table.name = tableName table.tableEngine = 'InnoDB' line = line[bracket:] comma = line.find(',') while comma != -1: atrStr = line[0:comma] #split to get the name of the attribute parts = atrStr.split(':') column = grt.classes.db_mysql_Column() column.name = parts[0][1:].replace('`', '') atrStr = parts[1][1:] #split to get the type and to find out, if the attribute is optional parts = atrStr.split(' ') column.setParseType(parts[0], grt.root.wb.doc.physicalModels[0].catalog.simpleDatatypes) if len(parts) >= 2: column.flags.append('UNSIGNED') table.addColumn(column) line = line[comma + 1:] comma = line.find(',') column = grt.classes.db_mysql_Column() parts = line.split(':') column.name = parts[0][1:].replace('`', '') line = parts[1][1:] parts = line.split(' ') if len(parts) >= 2: column.setParseType(parts[0], grt.root.wb.doc.physicalModels[0].catalog.simpleDatatypes) column.flags.append('UNSIGNED') else: column.setParseType(parts[0][0:-2], grt.root.wb.doc.physicalModels[0].catalog.simpleDatatypes) table.addColumn(column) elif line.startswith('MakeSingleLink'): line = line[15:] column = grt.classes.db_mysql_Column() parts = line.split('`') column.name = parts[1].replace('`', '') tableName = parts[3].replace('`', '') targetName = parts[5].replace('`', '') target = '' tables = schema.tables #iterating over the tables to get the targetTable for table in tables: if table.name == targetName: target = table #iterating over the tables again, to get the sourceTable for table in tables: print table.name, #table if table.name == tableName: table.addColumn(column) fk = table.createForeignKey(tableName + targetName) fk.referencedTable = target fk.columns.append(column) fk.owner = table print fk.owner print table.foreignKeys elif line.startswith('MakeMultiLink'): pass return 0 class Dialog(mforms.Form): def __init__(self): mforms.Form.__init__(self, None) self.result = None self.set_title("Import") #bx -- first box as container for others bx = mforms.newBox(False) bx.set_padding(12) bx.set_spacing(12) self.set_content(bx) #bx components self.lbdetail = newLabel("Please choose the file to import:") self.lbdetail.set_enabled(True) bx.add(self.lbdetail, False, True) #bxfile_path -- path selection bxfile_path = newBox(True) bxfile_path.set_spacing(4) self.tbfile = newTextEntry() self.lbfile = newLabel("File path:") self.btfile = newButton() self.btfile.set_text("...") self.btfile.enable_internal_padding(False) self.btfile.set_enabled(True) self.btfile.add_clicked_callback(self.open_file_chooser) bxfile_path.add(self.lbfile, False, False) bxfile_path.add(self.tbfile, True, True) bxfile_path.add(self.btfile, False, False) bx.add(bxfile_path, False, True) #bxOK --- last items on window bxOK = mforms.newBox(True) bx.add(bxOK, False, True) bxOK.set_spacing(4) self.ok = mforms.newButton() self.ok.set_text("OK") bxOK.add_end(self.ok, False, True) cancel = mforms.newButton() cancel.set_text("Cancel") bxOK.add_end(cancel, False, True) self.set_size(400, 150) self.center() self.result = self.run_modal(self.ok, cancel) #-------open_file_chooser method--------- def open_file_chooser(self): filechooser = FileChooser(mforms.OpenFile) filechooser.set_extensions("txt files |*.txt","*txt") if filechooser.run_modal(): self.tbfile.set_value(filechooser.get_path())