labkeyDatabaseBrowser.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import labkeyInterface
  2. import json
  3. import chardet
  4. class labkeyDB:
  5. def __init__(self,net):
  6. self.net=net
  7. def selectRows(self,project,schemaName, queryName,qfilter,viewName='default'):
  8. debug=False
  9. url=self.net.GetLabkeyUrl()+'/'+project
  10. url+='/query-selectRows.api?schemaName='+schemaName+\
  11. '&query.queryName='+queryName
  12. if viewName!='default':
  13. url+='&query.viewName={}'.format(viewName)
  14. for f in qfilter:
  15. url+="&query."+f['variable']+"~"+f['oper']+"="+f['value']
  16. if debug:
  17. print("Sending {}").format(url)
  18. response=self.net.get(url)
  19. encoding=chardet.detect(response.data)["encoding"]
  20. return json.loads(response.data.decode(encoding))
  21. def modifyRows(self,mode, project,schemaName, queryName, rows):
  22. #mode can be insert/update/delete
  23. debug=True
  24. data={}
  25. data['schemaName']=schemaName
  26. data['queryName']=queryName
  27. data['rows']=rows
  28. url=self.net.GetLabkeyUrl()+'/'+project
  29. url+='/query-'+mode+'Rows.api?'
  30. response=self.net.post(url,json.dumps(data))
  31. return response.data
  32. def addQuery(self,project,schemaName,queryName,fields,options={}):
  33. listDomainDefinition={}
  34. if schemaName=='lists':
  35. listDomainDefinition['kind']='IntList'
  36. fields.insert(0,{'name':'Key','rangeURI':'int'})
  37. options={}
  38. options['keyName']='Key'
  39. if queryName.find('enum')==0:
  40. options['keyType']='Integer'
  41. else:
  42. options['keyType']='AutoIncrementInteger'
  43. listDomainDefinition['options']=options
  44. if schemaName=='study':
  45. listDomainDefinition['kind']='StudyDatasetVisit'
  46. domainDesign={}
  47. domainDesign['name']=queryName
  48. domainDesign['description']='Some description'
  49. domainDesign['fields']=fields
  50. listDomainDefinition['domainDesign']=domainDesign
  51. listDomainDefinition['options']=options
  52. url=self.net.GetLabkeyUrl()+'/'+project
  53. url+='/property-createDomain.api'
  54. response=self.net.post(url,json.dumps(listDomainDefinition))
  55. encoding=chardet.detect(response.data)["encoding"]
  56. return json.loads(response.data.decode(encoding))
  57. def getQueryDesign(self,project,schemaName,queryName):
  58. url=self.net.GetLabkeyUrl()+'/'+project
  59. url+='/property-getDomain.api?'
  60. url+='schemaName={}'.format(schemaName)
  61. url+='&queryName={}'.format(queryName)
  62. response=self.net.get(url)
  63. encoding=chardet.detect(response.data)["encoding"]
  64. return json.loads(response.data.decode(encoding))