--- ZSQLExtend/ZSQLExtend.py 2006/01/16 17:32:40 1.78 +++ ZSQLExtend/ZSQLExtend.py 2006/03/28 19:54:41 1.79 @@ -19,6 +19,15 @@ import os.path import os from OFS.SimpleItem import SimpleItem +def getTextFromNode(nodename): + nodelist=nodename.childNodes + rc = "" + for node in nodelist: + if node.nodeType == node.TEXT_NODE: + rc = rc + node.data + return rc + + def analyseIntSearch(word): #analyse integer searches @@ -71,34 +80,99 @@ class ZSQLExtendFolder(Folder,Persistent """quote str for sql""" return sql_quote(str) + - def importXMLFile(self,table,containerTagName,file,identify=None,RESPONSE=None): + def importXMLFile(self,table,data,identify=None,RESPONSE=None): #TODO: finish importXMLFile ''' Import XML file into the table @param table: name of the table the xml shall be imported into @param containerTagName: XML-Tag which describes a dataset - @param file: xmlfile handle + @param data: data to be imported @param identify: (optional) field res. tag which identifies a entry uniquely for updating purposes. @param RESPONSE: (optional) ''' from xml.dom.pulldom import parseString + + + #fh=file("/tmp/fmpxml.xml") + import bz2 + import base64 + + ret="" + data=bz2.decompress(base64.decodestring(data)) + #data=fh.read() - doc=parseString(file.read()) + doc=parseString(data) while 1: node=doc.getEvent() - + if node is None: break; else: - if node[1].nodeName==containerTagName: + if node[1].nodeName=='ROW': + doc.expandNode(node[1]) + cols=node[1].getElementsByTagName('COL') + dataSet=[] + for col in cols: + data=col.getElementsByTagName('DATA') + dataSet.append(getTextFromNode(data[0])) + update=False + if identify: + + nr=fieldNames.index(identify) + field=dataSet[nr] + + searchStr="""select %s from %s where %s = '%s'"""%(identify,table,identify,field) + search=self.ZSQLSimpleSearch(searchStr) + if search: + update=True + + if update: + tmp=[] + for fieldName in fieldNames: + tmp.append("""%s = %s"""%(fieldName,self.ZSQLQuote(dataSet[fieldNames.index(fieldName)]))) + setStr=",".join(tmp) + nr=fieldNames.index(identify) + field=dataSet[nr] + + queryStr="""UPDATE %s SET %s WHERE %s = '%s' """%(table,setStr,identify,field) + self.ZSQLSimpleSearch(queryStr) + ret+="ud: %s \n"%field + else: + + + fields=",".join(fieldNames) + values=",".join([""" %s """%self.ZSQLQuote(x) for x in dataSet]) + + + queryStr="""INSERT INTO %s (%s) VALUES (%s)"""%(table,fields,values) + self.ZSQLSimpleSearch(queryStr) + ret+="ad: %s \n"%field + + elif node[1].nodeName=="METADATA": + fieldNames=[] doc.expandNode(node[1]) + + names=node[1].getElementsByTagName('FIELD') + + for name in names: + fieldNames.append(name.getAttribute('NAME')) + + qstr="""select attname from pg_attribute, pg_class where attrelid = pg_class.oid and relname = '%s' """ + columns=[x.attname for x in self.ZSQLSimpleSearch(qstr%table)] + + for fieldName in fieldNames: + if fieldName not in columns: + qstr="""alter table %s add %s %s""" + self.ZSQLSimpleSearch(qstr%(table,fieldName,'text')) + #fn=node[1].getAttribute("xml:id") #nf=file("xtf/"+fn+".xtf",'w') #nf.write(""""""+node[1].toxml()+"") #print "wrote: %s"%fn - - + return ret + def generateIndex(self,field,index_name,table,RESPONSE=None): """erzeuge index aus feld""" index={} @@ -455,13 +529,15 @@ class ZSQLExtendFolder(Folder,Persistent except: zLOG.LOG("ZSQLResetConnection",zLOG.ERROR, '%s %s'%sys.exc_info()[:2]) - def ZSQLSimpleSearch(self,query=None,max_rows=1000000): + def ZSQLSimpleSearch(self,query=None,max_rows=1000000,debug=None): """simple search""" + if not query: query=self.query - + if debug: + print "DEBUG: ZSQLSimpleSearch:", query if (hasattr(self,"_v_searchSQL") and (self._v_searchSQL == None)) or (not hasattr(self,"_v_searchSQL")): self._v_searchSQL=Shared.DC.ZRDB.DA.DA("_v_searchSQL","_v_searchSQL",self.getConnectionObj().getId(),"var","") @@ -1114,7 +1190,7 @@ class ZSQLExtendFolder(Folder,Persistent self.REQUEST.SESSION[storename]['searchFields']=searchFields self.REQUEST.SESSION[storename]['searchFieldsOnly']=searchFieldsOnly - print query + if not NoQuery: return self.ZSQLQuery(query)