Diff for /ZSQLExtend/importFMPXML.py between versions 1.15 and 1.19

version 1.15, 2007/08/09 15:09:27 version 1.19, 2007/12/14 21:30:55
Line 19  except: Line 19  except:
   
 fm_ns = 'http://www.filemaker.com/fmpxmlresult'  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):  def getTextFromNode(nodename):
     """get the cdata content of a node"""      """get the cdata content of a node"""
Line 49  def SimpleSearch(curs,query, args=None, Line 49  def SimpleSearch(curs,query, args=None,
         if args is not None:          if args is not None:
             encargs = []              encargs = []
             for a in args:              for a in args:
                 if a is not None:                  if a is not None and isinstance(a, str):
                     a = a.encode("UTF-8")                      a = a.encode("UTF-8")
                 encargs.append(a)                  encargs.append(a)
                           
Line 191  class xml_handler: Line 191  class xml_handler:
         # rename table for backup          # rename table for backup
         if self.backup_table:          if self.backup_table:
             self.orig_table = self.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              # remove old temp table
             qstr = "DROP TABLE %s"%(self.table)              qstr = "DROP TABLE %s"%(self.tmp_table)
             try:              try:
                 self.db.execute(qstr)                  self.db.execute(qstr)
             except:              except:
Line 202  class xml_handler: Line 204  class xml_handler:
             self.dbCon.commit()              self.dbCon.commit()
                         
             if self.id_field:              if self.id_field:
                 # sync mode -- copy table                  # sync mode -- copy backup table, update current table 
                 self.logger.info("copy table %s to %s"%(self.orig_table,self.table))                  self.logger.info("copy table %s to %s"%(self.table,backup_name))
                 qstr = "CREATE TABLE %s AS (SELECT * FROM %s)"%(self.table,self.orig_table)                  qstr = "CREATE TABLE %s AS (SELECT * FROM %s)"%(backup_name,self.table)
   
             else:              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))                  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)                  qstr = "CREATE TABLE %s AS (SELECT * FROM %s WHERE 1=0)"%(self.table,self.orig_table)
                           
Line 224  class xml_handler: Line 227  class xml_handler:
         # try to match date style with XML          # try to match date style with XML
         self.db.execute("set datestyle to 'german'")          self.db.execute("set datestyle to 'german'")
                   
         # translate id_field (SQL-name) to XML-name  
         self.xml_id = self.sql_field_map.get(self.id_field, None)  
           
         #self.logger.debug("xml-fieldnames:"+repr(self.xml_field_names))          #self.logger.debug("xml-fieldnames:"+repr(self.xml_field_names))
         # get list of fields and types of db table          # get list of fields and types of db table
         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'"          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'"
Line 237  class xml_handler: Line 237  class xml_handler:
             #print "SQL fields: %s (%s)"%(n,t)              #print "SQL fields: %s (%s)"%(n,t)
             self.sql_fields[n] = TableColumn(n,t)              self.sql_fields[n] = TableColumn(n,t)
                   
           # translate id_field (SQL-name) to XML-name
           self.xml_id = self.sql_field_map.get(self.id_field, None)
           # get type of id_field
           if self.id_field:
               self.id_type = self.sql_fields[self.id_field].getType()
           else:
               self.id_type = None
           
         # check fields to update          # check fields to update
         if self.update_fields is None:          if self.update_fields is None:
             if self.keep_fields:              if self.keep_fields:
Line 285  class xml_handler: Line 293  class xml_handler:
                     self.db.execute(qstr)                      self.db.execute(qstr)
                     self.dbCon.commit()                      self.dbCon.commit()
                                   
         # prepare sql statements for update          # 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], ', ')          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)          self.updQuery="UPDATE %s SET %s WHERE %s = %%s"%(self.table,setStr,self.id_field)
         # and insert          # and insert
         fields=string.join([self.xml_field_map[x].getName() for x in self.xml_update_list], ',')          fields=string.join([self.xml_field_map[x].getName() for x in self.xml_update_list], ',')
Line 353  class xml_handler: Line 361  class xml_handler:
             self.dbCon.commit()              self.dbCon.commit()
                           
         # reinstate backup tables          # 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'))              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))              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)              qstr = "ALTER TABLE %s RENAME TO %s"%(self.orig_table,backup_name)
Line 391  class xml_handler: Line 399  class xml_handler:
         id_val=''          id_val=''
         # synchronize by id_field          # synchronize by id_field
         if self.id_field:          if self.id_field:
               if self.id_type == 'integer':
                   id_val = int(self.xml_data[self.xml_id])
               else:
             id_val = self.xml_data[self.xml_id]              id_val = self.xml_data[self.xml_id]
                   
             if id_val in self.dbIDs:              if id_val in self.dbIDs:
                 self.dbIDs[id_val] += 1                  self.dbIDs[id_val] += 1
                 update=True                  update=True
Line 399  class xml_handler: Line 411  class xml_handler:
         # collect all values          # collect all values
         args = []          args = []
         for fn in self.xml_update_list:          for fn in self.xml_update_list:
               # do not update id_field
               if update and fn == self.xml_id:
                   continue
               
             f = self.xml_field_map[fn]              f = self.xml_field_map[fn]
             val = self.xml_data[fn]              val = self.xml_data[fn]
             type = self.sql_fields[f.getName()].getType()              type = self.sql_fields[f.getName()].getType()
Line 478  def importFMPXML(options): Line 494  def importFMPXML(options):
         @param options.keep_fields: (optional) don't add fields to SQL database          @param options.keep_fields: (optional) don't add fields to SQL database
         @param options.ascii_db: (optional) assume ascii encoding in db          @param options.ascii_db: (optional) assume ascii encoding in db
         @param options.replace_table: (optional) delete and re-insert data          @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):      if getattr(options,'update_fields',None):
Line 544  if __name__ == "__main__": Line 560  if __name__ == "__main__":
                      help="replace table i.e. delete and re-insert data")                       help="replace table i.e. delete and re-insert data")
     opars.add_option("--backup", default=False, action="store_true",       opars.add_option("--backup", default=False, action="store_true", 
                      dest="backup_table",                        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",       opars.add_option("-d", "--debug", default=False, action="store_true", 
                      dest="debug",                        dest="debug", 
                      help="debug mode (more output)")                       help="debug mode (more output)")

Removed from v.1.15  
changed lines
  Added in v.1.19


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>