labkeyDatabaseBrowser.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. fNames=[f['name'] for f in fields]
  35. if schemaName=='lists':
  36. listDomainDefinition['kind']='IntList'
  37. #check if options were set
  38. try:
  39. x=options['keyName']
  40. except KeyError:
  41. if 'Key' not in fNames:
  42. fields.insert(0,{'name':'Key','rangeURI':'int'})
  43. options['keyName']='Key'
  44. if queryName.find('enum')==0:
  45. options['keyType']='Integer'
  46. else:
  47. options['keyType']='AutoIncrementInteger'
  48. listDomainDefinition['options']=options
  49. if schemaName=='study':
  50. listDomainDefinition['kind']='StudyDatasetVisit'
  51. domainDesign={}
  52. domainDesign['name']=queryName
  53. domainDesign['description']='Some description'
  54. domainDesign['fields']=fields
  55. listDomainDefinition['domainDesign']=domainDesign
  56. url=self.net.GetLabkeyUrl()+'/'+project
  57. url+='/property-createDomain.api'
  58. response=self.net.post(url,json.dumps(listDomainDefinition))
  59. encoding=chardet.detect(response.data)["encoding"]
  60. return json.loads(response.data.decode(encoding))
  61. def getQueryDesign(self,project,schemaName,queryName):
  62. url=self.net.GetLabkeyUrl()+'/'+project
  63. url+='/property-getDomain.api?'
  64. url+='schemaName={}'.format(schemaName)
  65. url+='&queryName={}'.format(queryName)
  66. response=self.net.get(url)
  67. encoding=chardet.detect(response.data)["encoding"]
  68. return json.loads(response.data.decode(encoding))