def makeTables(sinedonname,modulename,dbname=None,xmlfile=None,check_exist=False): ### use alternate db name if desired if dbname is not None: print "setting alternate database name" sinedon.setConfig(sinedonname, db=dbname) ### connect to DB dbconf = sinedon.getConfig(sinedonname) dbd = sqldict.SQLDict(**dbconf) ### import desire module module = __import__(modulename) modbase = re.sub("^.*\.", "", modulename) tableData = getattr(module, modbase) ## hope this works ### get module members funcs = inspect.getmembers(tableData, inspect.isclass) print "Found %d classes in module"%(len(funcs)) #print funcs ### parse members count = 0 if xmlfile is not None: xmlf = open(xmlfile, 'w') xmlf.write("<defaulttables>\n <definition>\n") for func in funcs: ### Check if member is valid len 2 tuple if len(func) != 2: continue ### Check if member is a sinedon Data class if not issubclass(func[1], sinedon.data.Data) or func[0] == "Data": continue ### Create table tablename = func[0] tableclass = func[1]() table = (dbname, tablename) definition, formatedData = sqldict.dataSQLColumns(tableclass, False) create_flag=False if check_exist: try: dbd.diffSQLTable(tablename,definition) except (MySQLdb.ProgrammingError, MySQLdb.OperationalError), e: errno = e.args[0] ## some version of mysqlpython parses the exception differently if not isinstance(errno, int): errno = errno.args[0] ## 1146: table does not exist if errno in (1146,): print tablename,' does not yet exist, and will be created' create_flag=True else: create_flag=True if create_flag: if xmlfile is None: dbd.createSQLTable(table, definition) else: definitionToXml(xmlf, tablename, definition) count += 1
def flatInsert(self, newdata, force=False, skipinsert=False, fail=True): dbname = dbconfig.getConfig(newdata.__module__)['db'] tablename = newdata.__class__.__name__ table = (dbname, tablename) definition, formatedData = sqldict.dataSQLColumns(newdata, fail) ## check for any new columns that have not been created if table not in columns_created: columns_created[table] = {} fields = [d['Field'] for d in definition] for field in formatedData.keys(): if field not in fields: del formatedData[field] create_table = False for field in fields: if field not in columns_created[table]: columns_created[table][field] = None create_table = True if create_table: self.dbd.createSQLTable(table, definition) myTable = self.dbd.Table(table) if skipinsert is True: return None newid = myTable.insert([formatedData], force=force) return newid
def makeTables(sinedonname, modulename, dbname=None, xmlfile=None, check_exist=False): ### use alternate db name if desired if dbname is not None: print "setting alternate database name" sinedon.setConfig(sinedonname, db=dbname) ### connect to DB dbconf = sinedon.getConfig(sinedonname) dbd = sqldict.SQLDict(**dbconf) ### import desire module module = __import__(modulename) modbase = re.sub("^.*\.", "", modulename) tableData = getattr(module, modbase) ## hope this works ### get module members funcs = inspect.getmembers(tableData, inspect.isclass) print "Found %d classes in module" % (len(funcs)) #print funcs ### parse members count = 0 if xmlfile is not None: xmlf = open(xmlfile, 'w') xmlf.write("<defaulttables>\n <definition>\n") for func in funcs: ### Check if member is valid len 2 tuple if len(func) != 2: continue ### Check if member is a sinedon Data class if not issubclass(func[1], sinedon.data.Data) or func[0] == "Data": continue ### Create table tablename = func[0] tableclass = func[1]() table = (dbname, tablename) definition, formatedData = sqldict.dataSQLColumns(tableclass, False) create_flag = False if check_exist: try: dbd.diffSQLTable(tablename, definition) except (MySQLdb.ProgrammingError, MySQLdb.OperationalError), e: errno = e.args[0] ## some version of mysqlpython parses the exception differently if not isinstance(errno, int): errno = errno.args[0] ## 1146: table does not exist if errno in (1146, ): print tablename, ' does not yet exist, and will be created' create_flag = True else: create_flag = True if create_flag: if xmlfile is None: dbd.createSQLTable(table, definition) else: definitionToXml(xmlf, tablename, definition) count += 1
def diffData(self, newdata): table = newdata.__class__.__name__ definition, formated = sqldict.dataSQLColumns(newdata) return self.dbd.diffSQLTable(table, definition)