--- ZSQLExtend/importCDLIimglist.py 2007/12/31 09:47:12 1.2 +++ ZSQLExtend/importCDLIimglist.py 2011/02/11 19:08:49 1.6 @@ -11,12 +11,13 @@ from importASCII import ASCII_handler from importASCII import importASCII from importASCII import SimpleSearch -version_string = "V0.2 ROC 29.12.2007" +version_string = "V0.2.3 ROC 25.3.2010" # mapping img_type to SQL field names imgTypeMap = { 'p':'img_p', 'd':'img_d', + 's':'img_s', 'e':'img_e', 'ed':'img_ed', 'l':'img_l', @@ -25,8 +26,8 @@ imgTypeMap = { # list of fields in constant order (for SQL queries) imgTypes = imgTypeMap.keys() -upd_fields = "fn,,img_type,id_text" -id_field = "id_text" +upd_fields = "fn,,img_type,%s" +#id_field = "id_text" img_type_field = "img_type" def setup(self): @@ -34,10 +35,10 @@ def setup(self): ASCII_handler._setup(self) # create special updQuery for img_type fields setStr=string.join(["%s = %%s"%imgTypeMap[f] for f in imgTypes], ', ') - self.updQuery = "UPDATE %s SET %s WHERE id_text = %%s"%(self.table,setStr) + self.updQuery = "UPDATE %s SET %s WHERE %s = %%s"%(self.table,setStr,self.id_field) # create special delQuery for img_type fields delStr=string.join(["%s = null"%imgTypeMap[f] for f in imgTypes], ', ') - self.delQuery = "UPDATE %s SET %s WHERE id_text = %%s"%(self.table,delStr) + self.delQuery = "UPDATE %s SET %s WHERE %s = %%s"%(self.table,delStr,self.id_field) # text file field for img_type self.xml_img_type = self.sql_field_map[img_type_field] # dict of all img fields @@ -53,12 +54,11 @@ def handle_line(self, line): self.rowcnt += 1 # process collected row data update=False + # synchronize by id_field id_val = self.xml_data[self.xml_id] - if id_val in self.dbIDs: - self.dbIDs[id_val] += 1 - update=True - + #logging.debug("id_val=%s xml_id=%s"%(id_val,self.xml_id)) + # get img_type img_type_val = self.xml_data[self.xml_img_type] @@ -70,6 +70,11 @@ def handle_line(self, line): self.logger.debug("END ROW") return + # is the entry new? + if id_val in self.dbIDs: + self.dbIDs[id_val] += 1 + update=True + if update: # update existing row (by id_field) if id_val in self.img_data: @@ -112,15 +117,20 @@ def parse(self, filename): # find all fields if self.dbIDs[id] == 0: # unmatched entry - #self.logger.debug("CLEAN: %s with %s"%(self.delQuery,id)) + self.logger.debug("CLEAN: %s with %s"%(self.delQuery,id)) SimpleSearch(self.db, self.delQuery, [id], ascii=self.ascii_db, result=False) elif self.dbIDs[id] > 0: # assemble query - args = [ self.img_data[id].get(f,None) for f in imgTypes ] + imgd = self.img_data.get(id, None) + if imgd is None: + self.logger.error("No data for id %s while marked for update!"%id) + continue + + args = [ imgd.get(f,None) for f in imgTypes ] args.append(id) # update - #self.logger.debug("UPDATE: %s with %s"%(self.updQuery,args)) + self.logger.debug("UPDATE: %s with %s"%(self.updQuery,args)) SimpleSearch(self.db, self.updQuery, args, ascii=self.ascii_db, result=False) i += 1 @@ -164,6 +174,9 @@ if __name__ == "__main__": opars.add_option("-t", "--table", dest="table", help="database table name") + opars.add_option("--id-field", default="id_text", + dest="id_field", + help="name of id field for synchronisation", metavar="NAME") opars.add_option("--ascii-db", default=False, action="store_true", dest="ascii_db", help="the SQL database stores ASCII instead of unicode") @@ -197,8 +210,8 @@ if __name__ == "__main__": datefmt='%H:%M:%S') # fixed settings for CDLI imglist - options.update_fields = upd_fields - options.id_field = id_field + options.update_fields = upd_fields%options.id_field + #options.id_field = id_field options.update_mode = True importASCII(options)