--- ZSQLExtend/importFMPXML.py 2008/02/13 19:48:50 1.23 +++ ZSQLExtend/importFMPXML.py 2008/10/06 16:02:31 1.28 @@ -22,7 +22,7 @@ except: fm_ns = 'http://www.filemaker.com/fmpxmlresult' -version_string = "V0.6 ROC 13.2.2008" +version_string = "V0.6.2 ROC 6.10.2008" def unicodify(text, withNone=False): """decode str (utf-8 or latin-1 representation) into unicode object""" @@ -146,7 +146,7 @@ class xml_handler: # connect database self.dbCon = psycopg.connect(options.dsn) - logging.debug("DB encoding: %s"%self.dbCon.encoding) + logging.debug("DB encoding: %s"%getattr(self.dbCon, 'encoding', 'UNKNOWN')) self.db = self.dbCon.cursor() assert self.db, "AIIEE no db cursor for %s!!"%options.dsn @@ -160,6 +160,7 @@ class xml_handler: self.replace_table = getattr(options,"replace_table",None) self.backup_table = getattr(options,"backup_table",None) self.read_before_update = getattr(options,"read_before_update",None) + self.debug_data = getattr(options,"debug_data",None) self.logger.debug("dsn: "+repr(getattr(options,"dsn",None))) self.logger.debug("table: "+repr(self.table)) @@ -172,6 +173,7 @@ class xml_handler: self.logger.debug("replace_table: "+repr(self.replace_table)) self.logger.debug("backup_table: "+repr(self.backup_table)) self.logger.debug("read_before_update: "+repr(self.read_before_update)) + self.logger.debug("debug_data: "+repr(self.debug_data)) self.dbIDs = {} self.rowcnt = 0 @@ -261,10 +263,10 @@ class xml_handler: qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0 and relname = '%s'" self.sql_fields={} for f in SimpleSearch(self.db, qstr%self.table): - n = f[0] - t = f[1] + fn = f[0] + ft = f[1] #print "SQL fields: %s (%s)"%(n,t) - self.sql_fields[n] = TableColumn(n,t) + self.sql_fields[fn] = TableColumn(fn,ft) # translate id_field (SQL-name) to XML-name self.xml_id = self.sql_field_map.get(self.id_field, None) @@ -313,7 +315,9 @@ class xml_handler: self.logger.debug("field %s has different type (%s vs %s)"%(f,f.getType(),sf.getType())) elif uf is not None: # add field to table - qstr="alter table %s add %s %s"%(self.table,uf.getName(),uf.getType()) + fn = uf.getName() + ft = uf.getType() + qstr="alter table %s add \"%s\" %s"%(self.table,fn,ft) self.logger.info("db add field:"+qstr) if self.ascii_db and type(qstr)==types.UnicodeType: @@ -321,15 +325,17 @@ class xml_handler: self.db.execute(qstr) self.dbCon.commit() + # add field to field list + self.sql_fields[fn] = TableColumn(fn, ft) # prepare sql statements for update (do not update id_field) - setStr=string.join(["%s = %%s"%self.xml_field_map[f] for f in self.xml_update_list if f != self.xml_id], ', ') - self.updQuery="UPDATE %s SET %s WHERE %s = %%s"%(self.table,setStr,self.id_field) + setStr=string.join(["\"%s\" = %%s"%self.xml_field_map[f] for f in self.xml_update_list if f != self.xml_id], ', ') + self.updQuery="UPDATE %s SET %s WHERE \"%s\" = %%s"%(self.table,setStr,self.id_field) # and select (for update check) selStr=string.join([self.xml_field_map[f].getName() for f in self.xml_update_list if f != self.xml_id], ', ') - self.selQuery="SELECT %s FROM %s WHERE %s = %%s"%(selStr,self.table,self.id_field) + self.selQuery="SELECT %s FROM %s WHERE \"%s\" = %%s"%(selStr,self.table,self.id_field) # and insert - fields=string.join([self.xml_field_map[x].getName() for x in self.xml_update_list], ',') + fields=string.join(["\"%s\""%self.xml_field_map[x].getName() for x in self.xml_update_list], ',') values=string.join(['%s' for f in self.xml_update_list], ',') self.addQuery="INSERT INTO %s (%s) VALUES (%s)"%(self.table,fields,values) self.logger.debug("update-query: "+self.updQuery) @@ -380,11 +386,11 @@ class xml_handler: 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) + 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:"+id) + self.logger.info(" delete: %s"%id) SimpleSearch(self.db, delQuery, [id], ascii=self.ascii_db) elif self.dbIDs[id] > 1: @@ -464,7 +470,8 @@ class xml_handler: # update existing row (by id_field) if self.read_before_update: # read data - self.logger.debug("update check: %s = %s"%(id_val, args)) + if self.debug_data: + self.logger.debug("update check: %s = %s"%(id_val, args)) oldrow = SimpleSearch(self.db, self.selQuery, [id_val], ascii=self.ascii_db) #i = 0 #for v in oldrow[0]: @@ -472,19 +479,22 @@ class xml_handler: # i += 1 if tuple(oldrow[0]) != tuple(args): # data has changed -- update - self.logger.debug("really update: %s = %s"%(id_val, args)) + if self.debug_data: + self.logger.debug("really update: %s = %s"%(id_val, args)) args.append(id_val) # last arg is id SimpleSearch(self.db, self.updQuery, args, ascii=self.ascii_db) else: # always update - self.logger.debug("update: %s = %s"%(id_val, args)) + if self.debug_data: + self.logger.debug("update: %s = %s"%(id_val, args)) args.append(id_val) # last arg is id SimpleSearch(self.db, self.updQuery, args, ascii=self.ascii_db) else: # create new row - self.logger.debug("insert: %s"%args) + if self.debug_data: + self.logger.debug("insert: %s"%args) SimpleSearch(self.db, self.addQuery, args, ascii=self.ascii_db) #self.logger.info(" row:"+"%d (%s)"%(self.rowcnt,id_val)) @@ -614,6 +624,9 @@ if __name__ == "__main__": opars.add_option("-d", "--debug", default=False, action="store_true", dest="debug", help="debug mode (more output)") + opars.add_option("--debug-data", default=False, action="store_true", + dest="debug_data", + help="debug mode for data (even more output)") (options, args) = opars.parse_args()