labkeyDatabaseBrowser.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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):
  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. url=self.net.GetLabkeyUrl()+'/'+project
  52. url+='/property-createDomain.api'
  53. response=self.net.post(url,json.dumps(listDomainDefinition))
  54. encoding=chardet.detect(response.data)["encoding"]
  55. return json.loads(response.data.decode(encoding))
  56. def getQueryDesign(self,project,schemaName,queryName):
  57. url=self.net.GetLabkeyUrl()+'/'+project
  58. url+='/property-getDomain.api?'
  59. url+='schemaName={}'.format(schemaName)
  60. url+='&queryName={}'.format(queryName)
  61. response=self.net.get(url)
  62. encoding=chardet.detect(response.data)["encoding"]
  63. return json.loads(response.data.decode(encoding))