--- ZSQLExtend/importFMPXML.py 2008/10/06 16:02:31 1.28 +++ ZSQLExtend/importFMPXML.py 2011/05/20 16:03:18 1.32 @@ -22,7 +22,7 @@ except: fm_ns = 'http://www.filemaker.com/fmpxmlresult' -version_string = "V0.6.2 ROC 6.10.2008" +version_string = "V0.6.6 ROC 20.5.2011" def unicodify(text, withNone=False): """decode str (utf-8 or latin-1 representation) into unicode object""" @@ -205,7 +205,8 @@ class xml_handler: #First round through the generator corresponds to the #start element event self.logger.info("reading metadata...") - self.logger.debug("START METADATA") + if self.debug_data: + self.logger.debug("START METADATA") yield None #delegate is a generator that handles all the events "within" @@ -217,7 +218,8 @@ class xml_handler: yield None #Element closed. Wrap up - self.logger.debug("END METADATA") + if self.debug_data: + self.logger.debug("END METADATA") # rename table for backup if self.backup_table: @@ -367,7 +369,8 @@ class xml_handler: #First round through the generator corresponds to the #start element event self.logger.info("reading data...") - self.logger.debug("START RESULTSET") + if self.debug_data: + self.logger.debug("START RESULTSET") self.rowcnt = 0 yield None @@ -380,23 +383,29 @@ class xml_handler: yield None #Element closed. Wrap up - self.logger.debug("END RESULTSET") + if self.debug_data: + self.logger.debug("END RESULTSET") self.dbCon.commit() if self.sync_mode: # delete unmatched entries in db - self.logger.info("deleting unmatched rows from db") - delQuery = "DELETE FROM %s WHERE \"%s\" = %%s"%(self.table,self.id_field) - for id in self.dbIDs.keys(): - # find all not-updated fields - if self.dbIDs[id] == 0: - self.logger.info(" delete: %s"%id) - SimpleSearch(self.db, delQuery, [id], ascii=self.ascii_db) - - elif self.dbIDs[id] > 1: - self.logger.info(" sync: ID %s used more than once?"%id) - - self.dbCon.commit() + if self.rowcnt > 0: + self.logger.info("deleting unmatched rows from db") + delQuery = "DELETE FROM %s WHERE \"%s\" = %%s"%(self.table,self.id_field) + for id in self.dbIDs.keys(): + # find all not-updated fields + if self.dbIDs[id] == 0: + self.logger.info(" delete: %s"%id) + SimpleSearch(self.db, delQuery, [id], ascii=self.ascii_db) + + elif self.dbIDs[id] > 1: + self.logger.info(" sync: ID %s used more than once?"%id) + + self.dbCon.commit() + + else: + # safety in case we had an empty file + self.logger.warning("no rows read! not deleting unmatched rows!") # reinstate backup tables if self.backup_table and not self.id_field: @@ -409,6 +418,7 @@ class xml_handler: self.db.execute(qstr) self.dbCon.commit() + self.logger.info("Done (%s rows)"%self.rowcnt) return def handle_row(self, end_condition): @@ -416,7 +426,8 @@ class xml_handler: (saxtools.START_ELEMENT, fm_ns, u'COL'): self.handle_col, } - self.logger.debug("START ROW") + if self.debug_data: + self.logger.debug("START ROW") self.xml_data = {} self.colIdx = 0 yield None @@ -430,7 +441,8 @@ class xml_handler: yield None #Element closed. Wrap up - self.logger.debug("END ROW") + if self.debug_data: + self.logger.debug("END ROW") self.rowcnt += 1 # process collected row data update=False @@ -438,9 +450,17 @@ class xml_handler: # synchronize by id_field if self.id_field: if self.id_type == 'integer': - id_val = int(self.xml_data[self.xml_id]) + try: + id_val = int(self.xml_data[self.xml_id]) + except: + pass else: id_val = self.xml_data[self.xml_id] + + if not id_val: + # abort update + self.logger.error("ERROR: unable to sync! emtpy id in row %s"%self.rowcnt) + return if id_val in self.dbIDs: self.dbIDs[id_val] += 1 @@ -456,7 +476,7 @@ class xml_handler: f = self.xml_field_map[fn] val = self.xml_data[fn] type = self.sql_fields[f.getName()].getType() - if type == "date" and len(val) == 0: + if type == "date" and len(val.strip()) == 0: # empty date field val = None