--- ZSQLExtend/importFMPXML.py 2007/08/09 15:09:27 1.15 +++ ZSQLExtend/importFMPXML.py 2007/12/11 16:21:24 1.16 @@ -19,7 +19,7 @@ except: fm_ns = 'http://www.filemaker.com/fmpxmlresult' -version_string = "V0.4.1 ROC 9.8.2007" +version_string = "V0.5 ROC 11.12.2007" def getTextFromNode(nodename): """get the cdata content of a node""" @@ -191,9 +191,11 @@ class xml_handler: # rename table for backup if self.backup_table: self.orig_table = self.table - self.table = self.table + "_tmp" + self.tmp_table = self.table + "_tmp" + backup_name = "%s_%s"%(self.table,time.strftime('%Y_%m_%d_%H_%M_%S')) + # remove old temp table - qstr = "DROP TABLE %s"%(self.table) + qstr = "DROP TABLE %s"%(self.tmp_table) try: self.db.execute(qstr) except: @@ -202,12 +204,13 @@ class xml_handler: self.dbCon.commit() if self.id_field: - # sync mode -- copy table - self.logger.info("copy table %s to %s"%(self.orig_table,self.table)) - qstr = "CREATE TABLE %s AS (SELECT * FROM %s)"%(self.table,self.orig_table) + # sync mode -- copy backup table, update current table + self.logger.info("copy table %s to %s"%(self.table,backup_name)) + qstr = "CREATE TABLE %s AS (SELECT * FROM %s)"%(backup_name,self.table) else: - # rename table and create empty new one + # replace mode -- create empty tmp table, insert into tmp table + self.table = self.tmp_table self.logger.info("create empty table %s"%(self.table)) qstr = "CREATE TABLE %s AS (SELECT * FROM %s WHERE 1=0)"%(self.table,self.orig_table) @@ -353,7 +356,7 @@ class xml_handler: self.dbCon.commit() # reinstate backup tables - if self.backup_table: + if self.backup_table and not self.id_field: backup_name = "%s_%s"%(self.orig_table,time.strftime('%Y_%m_%d_%H_%M_%S')) self.logger.info("rename backup table %s to %s"%(self.orig_table,backup_name)) qstr = "ALTER TABLE %s RENAME TO %s"%(self.orig_table,backup_name) @@ -478,7 +481,7 @@ def importFMPXML(options): @param options.keep_fields: (optional) don't add fields to SQL database @param options.ascii_db: (optional) assume ascii encoding in db @param options.replace_table: (optional) delete and re-insert data - @param options.backup_table: (optional) create backup of old table (breaks indices) + @param options.backup_table: (optional) create backup of old table """ if getattr(options,'update_fields',None): @@ -544,7 +547,7 @@ if __name__ == "__main__": help="replace table i.e. delete and re-insert data") opars.add_option("--backup", default=False, action="store_true", dest="backup_table", - help="create backup of old table (breaks indices)") + help="create backup of old table") opars.add_option("-d", "--debug", default=False, action="store_true", dest="debug", help="debug mode (more output)")