--- ZSQLExtend/ZSQLExtend.py 2006/04/24 16:09:52 1.82
+++ ZSQLExtend/ZSQLExtend.py 2006/10/05 09:37:38 1.95
@@ -1,3 +1,4 @@
+
from OFS.Folder import Folder
from Acquisition import Implicit
from Globals import DTMLFile,package_home,Persistent
@@ -17,9 +18,15 @@ import Shared.DC.ZRDB.DA
import zLOG
import os.path
import os
+import copy
+import unicodedata
+
from OFS.SimpleItem import SimpleItem
def getTextFromNode(nodename):
+ """get the cdata content of a node"""
+ if nodename is None:
+ return ""
nodelist=nodename.childNodes
rc = ""
for node in nodelist:
@@ -27,7 +34,6 @@ def getTextFromNode(nodename):
rc = rc + node.data
return rc
-
def analyseIntSearch(word):
#analyse integer searches
@@ -80,6 +86,29 @@ class ZSQLExtendFolder(Folder,Persistent
"""quote str for sql"""
return sql_quote(str)
+
+ def normalizeField(self,table,fieldname, newFieldName=None,mode="alter", RESPONSE=None):
+ """normalize a field"""
+ import unicodedata
+
+ if not newFieldName:
+ newFieldName=fieldname+"_normal"
+
+ def normal(str):
+ if str:
+ return unicodedata.normalize('NFKD', str.decode('utf-8')).encode('ASCII', 'ignore')
+ else:
+ return ""
+ if mode=="create": # create the field
+ qstr="""alter table %s add %s %s"""
+ self.ZSQLSimpleSearch(qstr%(table,newFieldName,'text'))
+
+ qstr="select oid,%s from %s"%(fieldname,table)
+ for result in self.ZSQLSimpleSearch(qstr):
+ qstr="update %s set %s = %s where oid = %s"
+
+ self.ZSQLSimpleSearch(qstr%(table,newFieldName,self.ZSQLQuote(normal(getattr(result,fieldname))),result.oid))
+
def importAccessModell(self,configFileName,RESPONSE=None):
"""import tables from access
@param configFileName: xml-configfile
@@ -102,7 +131,7 @@ class ZSQLExtendFolder(Folder,Persistent
containers=db.xpath("./@container")
identifiers=db.xpath("./@identify")
- print containers
+
if not (len(containers)==1):
return False
else:
@@ -124,6 +153,7 @@ class ZSQLExtendFolder(Folder,Persistent
@param elementNameForTable: must be a element of type complex type. the element of the sequence in the complex type
define the columns of the table >table<
@param data (optional): data to be imported
+
@param filename (optional) or filename
@param identify: (optional) field res. tag which identifies a entry uniquely for updating purposes.
@param RESPONSE: (optional)
@@ -240,7 +270,7 @@ class ZSQLExtendFolder(Folder,Persistent
if col.nodeType is col.ELEMENT_NODE:
data=col.nodeName
dataSet[data]=getTextFromNode(col)
- print dataSet.keys()
+
update=False
if identify:
@@ -288,36 +318,20 @@ class ZSQLExtendFolder(Folder,Persistent
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 data: data to be imported
+ @param file: xmlfile handle
@param identify: (optional) field res. tag which identifies a entry uniquely for updating purposes.
@param RESPONSE: (optional)
'''
- from xml.dom.pulldom import parseString,parse
-
- zLOG.LOG("import xml",zLOG.INFO,"called")
- #fh=file("/tmp/fmpxml.xml")
- import bz2
- import base64
-
- ret=""
- if data:
- data=bz2.decompress(base64.decodestring(data))
-
- zLOG.LOG("import xml",zLOG.INFO,"received file")
- doc=parseString(data)
- zLOG.LOG("import xml",zLOG.INFO,"parsed file")
+ from xml.dom.pulldom import parseString
- elif filename:
- fh=file(filename)
- doc=parse(fh)
- zLOG.LOG("import xml",zLOG.INFO,"parsed file")
+ doc=parseString(file.read())
while 1:
node=doc.getEvent()
-
+
if node is None:
break;
else:
- if node[1].nodeName=='ROW':
+ if node[1].nodeName==containerTagName:
doc.expandNode(node[1])
cols=node[1].getElementsByTagName('COL')
dataSet=[]
@@ -383,8 +397,8 @@ class ZSQLExtendFolder(Folder,Persistent
#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={}
@@ -457,22 +471,14 @@ class ZSQLExtendFolder(Folder,Persistent
return ""
def getLabel(self):
- """getLabel"""
+ """getLabe"""
try:
return self.label
except:
return ""
-
- def getTitle(self):
- """getTitle"""
- try:
- return self.title
- except:
- return ""
-
def getDescription(self):
- """getDescription"""
+ """getLabe"""
try:
return self.description
except:
@@ -501,10 +507,6 @@ class ZSQLExtendFolder(Folder,Persistent
def formatAscii(self,str,url=None):
"""ersetze ascii umbrueche durch
"""
#url=None
-
- if not str:
- return ""
-
str=str.rstrip().lstrip()
if url and str:
@@ -667,14 +669,16 @@ class ZSQLExtendFolder(Folder,Persistent
field=getattr(result,fieldName)
fieldValue=getattr(result,valueName)
if fieldValue:
- print "XXX",field,repr(type(field)),repr(type(selected))
+
if not linelen:
+
if field == selected:
- print "huhu"
+
ret+=""""""%(field,fieldValue)
else:
ret+=""""""%(field,fieldValue)
+
else:
mist = """%s"""%(fieldValue)
if len(mist) > string.atoi(linelen):
@@ -684,7 +688,7 @@ class ZSQLExtendFolder(Folder,Persistent
return ret
- def ZSQLInlineSearchU(self,storename=None,**argv):
+ def ZSQLInlineSearchU(self,storename=None,args=None,**argv):
"""one element if exists"""
qs=[]
if storename:
@@ -693,11 +697,15 @@ class ZSQLExtendFolder(Folder,Persistent
else:
storename="foundCount"
-
+ if args:
+ argTmp=args
+ else:
+ argTmp=argv
+
#print "INLINE:",argv
- for a in argv.keys():
- qs.append(a+"="+urllib.quote(str(argv[a])))
+ for a in argTmp.keys():
+ qs.append(a+"="+urllib.quote(str(argTmp[a])))
#return []
ret = self.parseQueryString(string.join(qs,","),"_",storename=storename)
@@ -705,8 +713,28 @@ class ZSQLExtendFolder(Folder,Persistent
return ret[0]
except:
return None
+
+
+ def ZSQLSequentialSearch(self,_fieldlist,_searchTerm,storename=None,args=None,**argv):
+ """get a list of tuples (fields,searchoptions,table) to search the searchTerm in, returns dictionary. """
+ #print "do search with:",_fieldlist,_searchTerm,storename,args,argv
+ ret={}
+ if args:
+ argTmp=args
+ else:
+ argTmp=argv
+
+ for field in _fieldlist:
+
+ argTmp2=copy.deepcopy(argTmp)
+ argTmp2[field[0]]=_searchTerm
+ argTmp2['_op_'+field[0]]=field[1]
+ argTmp2['_table']=field[2]
+
+ ret[field[0]]=(self.ZSQLInlineSearch(storename=storename,args=argTmp2),field[3],field[4],field[5],field[6])
- def ZSQLInlineSearch(self,storename=None,**argv):
+ return ret
+ def ZSQLInlineSearch(self,storename=None,args=None,**argv):
"""inlinesearch"""
qs=[]
@@ -717,15 +745,22 @@ class ZSQLExtendFolder(Folder,Persistent
+ if args:
+ argTmp=args
+ else:
+ argTmp=argv
+
+
#print "INLINE:",argv
- for a in argv.keys():
+ for a in argTmp.keys():
try:
- qs.append(a+"="+urllib.quote(str(argv[a])))
+ qs.append(a+"="+urllib.quote(str(argTmp[a])))
except:
import urllib
- qs.append(a+"="+urllib.quote(str(argv[a])))
+ qs.append(a+"="+urllib.quote(str(argTmp[a])))
#return []
+
return self.parseQueryString(string.join(qs,","),"_",storename=storename)
def ZSQLInlineSearch2(self,query):
@@ -747,15 +782,14 @@ class ZSQLExtendFolder(Folder,Persistent
except:
zLOG.LOG("ZSQLResetConnection",zLOG.ERROR, '%s %s'%sys.exc_info()[:2])
- def ZSQLSimpleSearch(self,query=None,max_rows=1000000,debug=None):
+ def ZSQLSimpleSearch(self,query=None,max_rows=1000000):
"""simple search"""
-
-
+
+ #print query
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","")
@@ -815,9 +849,14 @@ class ZSQLExtendFolder(Folder,Persistent
- def ZSQLAdd(self,format=None,RESPONSE=None,**argv):
+ def ZSQLAdd(self,format=None,RESPONSE=None,args=None,**argv):
"""Neuer Eintrag"""
+ if args:
+ argTmp=args
+ else:
+ argTmp=argv
+
qs_temp=[]
for a in self.REQUEST.form.keys():
@@ -825,13 +864,13 @@ class ZSQLExtendFolder(Folder,Persistent
qs=string.join(qs_temp,",")
- for field in argv.keys():
+ for field in argTmp.keys():
if field[0]=="_":
fieldTmp="-"+field[1:]
else:
fieldTmp=field
- qs+=",%s=%s"%(fieldTmp,argv[field])
+ qs+=",%s=%s"%(fieldTmp,argTmp[field])
addList={}
@@ -869,10 +908,11 @@ class ZSQLExtendFolder(Folder,Persistent
"""change entries"""
#qs=self.REQUEST['QUERY_STRING']
# very bad hack
-
+
qs_temp=[]
if USE_FORM or RESPONSE:
for a in self.REQUEST.form.keys():
+
qs_temp.append(a+"="+urllib.quote(str(self.REQUEST.form[a])))
@@ -890,12 +930,12 @@ class ZSQLExtendFolder(Folder,Persistent
changeList=[]
for q in qs_temp:
-
+
name=urllib.unquote(re.sub("r'+'"," ",q.split("=")[0].lower()))
value="=".join(q.split("=")[1:])
value=re.sub(r'\+'," ",value)
value=urllib.unquote(value)
-
+
if name=="-table":
table=urllib.unquote(value)
elif name=="-identify":
@@ -903,13 +943,15 @@ class ZSQLExtendFolder(Folder,Persistent
identify=identify.split("=")[0]+"="+sql_quote(identify.split("=")[1])
elif name=="-format":
format=urllib.unquote(value)
- elif (not (name[0]=="-" or name[0]=="_")) and (not len(value)==0):
+ #elif (not (name[0]=="-" or name[0]=="_")) and (not len(value)==0):
+ elif (not (name[0]=="-" or name[0]=="_")):
changeList.append("\""+name+"\"="+sql_quote(urllib.unquote(value)))
changeString=string.join(changeList,",")
queryString="UPDATE %s SET %s WHERE %s"%(table,changeString,identify)
+ zLOG.LOG("ZSQLExtend",zLOG.INFO,"CHANGE: "+queryString)
self.ZSQLSimpleSearch(queryString)
@@ -935,7 +977,7 @@ class ZSQLExtendFolder(Folder,Persistent
def ZSQLFind2(self,qs="",select="oid,*",storename=None,tableList=['cdli_translit','cdli_cat'],restrictField='id_text',**argv):
"""find2"""
-
+
search1= self.ZSQLFind(qs=qs,select=select,storename=storename,tableExt=tableList[1],restrictField=restrictField,NoQuery='yes',NoLimit='yes',**argv)
search2 = self.ZSQLFind(tableExt=tableList[0],qs=qs,select=select,storename=storename,restrictConnect=(tableList[0]+"."+restrictField,search1),**argv)
@@ -966,7 +1008,7 @@ class ZSQLExtendFolder(Folder,Persistent
if field[0]=="_":
fieldTmp="-"+field[1:]
else:
- fieldTmp=urllib.unquote(field)
+ fieldTmp=field
qs+=",%s=%s"%(fieldTmp,argv[field])
@@ -995,21 +1037,19 @@ class ZSQLExtendFolder(Folder,Persistent
qs=string.join(delEmpty(qs.split(",")),",")
- if not storename:
+ if storename:
+ """store"""
+ else:
storename="foundCount"
#store query for further usage
self.REQUEST.SESSION['query']=qs
- print "st",storename,qs
+
#print "calling Query with",repr(NoQuery)
ret=self.parseQueryString(qs,"-",select=select,storemax="yes",storename=storename,tableExt=tableExt,NoQuery=NoQuery,NoLimit=NoLimit,restrictField=restrictField,restrictConnect=restrictConnect,filter=filter)
#print self.REQUEST.SESSION["foundCount"]
-# if not hasattr(self,'_v_results'):
-# self._v_results={}
-#
-#
-# self._v_results[urllib.quote(qs)]=ret[0:]
+
return ret
@@ -1132,6 +1172,7 @@ class ZSQLExtendFolder(Folder,Persistent
opfields={}
lopfields={} #Verknuepfung bei mehrfachauswahl von einem feld
sortfields={} #order of sortfields
+ diacritics={} #diacritische zeichen, falls "no", dann werden diese fuer das feld gefiltert.
sortAllFields=None
skip=""
rangeStart=0
@@ -1174,6 +1215,10 @@ class ZSQLExtendFolder(Folder,Persistent
field=name[5:]
lopfields[field]=lop
+ if name[0:11]==iCT+"diacritics":
+ field=name[12:]
+ diacritics[field]=value
+
if name[0:10]==iCT+"sortorder":
#sort=value
@@ -1199,6 +1244,11 @@ class ZSQLExtendFolder(Folder,Persistent
punktsplit=name.split(".") #sonderfall feld mit punkten(tabelle.suchFeld.ausgewaehltesFeld,feldinoriginal), d.h. suche in anderer tabelle:
#analysiere alle anderen faelle
+
+ if diacritics.get(name,'yes')=='no':
+ """filter diacritische zeichen"""
+ value=unicodedata.normalize('NFKD', value.decode('utf-8')).encode('ASCII', 'ignore')
+
if name==iCT+"lop":
lop=value
elif name==iCT+"table":
@@ -1239,6 +1289,13 @@ class ZSQLExtendFolder(Folder,Persistent
op=value
+ #sonderfall Name hat das Format:
+ #TABELLE.SUCHFELD_IN_DIESER_TABELLE.SELECT_FIELD.IDENTIFIER_IN_TABELLE_-table
+ #i.e. erzeugt wird
+ #das Statement
+ #WHERE DENTIFIER_IN_TABELLE_-table in (select from SELECT_FIELD
+ #where LOWER(UCHFELD_IN_DIESER_TABELLE) something value)
+ #something is defined by _op_TABELLE.SUCHFELD_IN_DIESER_TABELLE.SELECT_FIELD.IDENTIFIER_IN_TABELLE_-table
elif (not name[0]==iCT) and len(punktsplit)==4:
if opfields.has_key(name):
@@ -1247,7 +1304,7 @@ class ZSQLExtendFolder(Folder,Persistent
op="ct"
namealt=name
name="LOWER("+punktsplit[1]+")"
-
+ value=value.lower()
if op=="ct":
tmp=(name+" LIKE "+sql_quote("%"+value+"%"))
elif op=="gt":
@@ -1264,7 +1321,7 @@ class ZSQLExtendFolder(Folder,Persistent
tmps=[]
for word in value.split(" "):
tmps.append(name+" LIKE "+sql_quote("%"+word+"%"))
-
+
tmp=string.join(tmps,' AND ')
elif op=="numerical":
@@ -1278,14 +1335,12 @@ class ZSQLExtendFolder(Folder,Persistent
tmps.append(name+" LIKE "+sql_quote("%"+word+"%"))
tmp=string.join(tmps,' OR ')
-
- searchFieldsOnly[namealt]=value
+
op="all"
searchTmp="""%s in (select %s from %s where %s)"""%(punktsplit[3],punktsplit[2],punktsplit[0],tmp)
-
-
+
queryTemplate.append(searchTmp)
elif (not name[0]==iCT) and (not len(value)==0):
@@ -1343,7 +1398,7 @@ class ZSQLExtendFolder(Folder,Persistent
whereList=["("+searchFields[x]+")" for x in searchFields.keys()]
whereList+=queryTemplate
-
+
if len(whereList)>0:
if filter:
whereStr="("+string.join(whereList," "+lop+" ")+") AND "+filter
@@ -1410,10 +1465,9 @@ class ZSQLExtendFolder(Folder,Persistent
self.REQUEST.SESSION[storename]['rangeEnd']=int(rangeStart)+int(limit)
self.REQUEST.SESSION[storename]['rangeSize']=limit
self.REQUEST.SESSION[storename]['searchFields']=searchFields
- print "SF",searchFieldsOnly,searchFields
+
self.REQUEST.SESSION[storename]['searchFieldsOnly']=searchFieldsOnly
-
if not NoQuery:
return self.ZSQLQuery(query)
@@ -1966,4 +2020,4 @@ def manage_addZSQLBibliography(self, id,
-
+
\ No newline at end of file