12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import labkeyInterface
- import json
- import chardet
- class labkeyDB:
- def __init__(self,net):
- self.net=net
- def selectRows(self,project,schemaName, queryName,qfilter,viewName='default'):
- debug=False
- url=self.net.GetLabkeyUrl()+'/'+project
- url+='/query-selectRows.api?schemaName='+schemaName+\
- '&query.queryName='+queryName
- if viewName!='default':
- url+='&query.viewName={}'.format(viewName)
- for f in qfilter:
- url+="&query."+f['variable']+"~"+f['oper']+"="+f['value']
- if debug:
- print("Sending {}").format(url)
- response=self.net.get(url)
- encoding=chardet.detect(response.data)["encoding"]
- return json.loads(response.data.decode(encoding))
- def modifyRows(self,mode, project,schemaName, queryName, rows):
- #mode can be insert/update/delete
- debug=True
- data={}
- data['schemaName']=schemaName
- data['queryName']=queryName
- data['rows']=rows
- url=self.net.GetLabkeyUrl()+'/'+project
- url+='/query-'+mode+'Rows.api?'
- response=self.net.post(url,json.dumps(data))
- return response.data
- def addQuery(self,project,schemaName,queryName,fields,options={}):
- listDomainDefinition={}
- fNames=[f['name'] for f in fields]
- if schemaName=='lists':
- listDomainDefinition['kind']='IntList'
- #check if options were set
- try:
- x=options['keyName']
- except KeyError:
- if 'Key' not in fNames:
- fields.insert(0,{'name':'Key','rangeURI':'int'})
- options['keyName']='Key'
- if queryName.find('enum')==0:
- options['keyType']='Integer'
- else:
- options['keyType']='AutoIncrementInteger'
- listDomainDefinition['options']=options
-
- if schemaName=='study':
- listDomainDefinition['kind']='StudyDatasetVisit'
-
- domainDesign={}
- domainDesign['name']=queryName
- domainDesign['description']='Some description'
- domainDesign['fields']=fields
- listDomainDefinition['domainDesign']=domainDesign
- url=self.net.GetLabkeyUrl()+'/'+project
- url+='/property-createDomain.api'
- response=self.net.post(url,json.dumps(listDomainDefinition))
- encoding=chardet.detect(response.data)["encoding"]
- return json.loads(response.data.decode(encoding))
- def getQueryDesign(self,project,schemaName,queryName):
- url=self.net.GetLabkeyUrl()+'/'+project
- url+='/property-getDomain.api?'
- url+='schemaName={}'.format(schemaName)
- url+='&queryName={}'.format(queryName)
- response=self.net.get(url)
- encoding=chardet.detect(response.data)["encoding"]
- return json.loads(response.data.decode(encoding))
|