runQuery.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var runQuery={};
  2. runQuery.print=
  3. function(msg){
  4. console.log(msg);
  5. }
  6. runQuery.insertRows=
  7. function(schema,query,rows,action=null,container=null,failure=null){
  8. this.modifyRows('insert',schema,query,rows,action,container);
  9. }
  10. runQuery.deleteRows=
  11. function(schema,query,rows,action=null,container=null,failure=null){
  12. this.modifyRows('delete',schema,query,rows,action,container);
  13. }
  14. runQuery.modifyRows=
  15. function(mode,schema,query,rows,action=null,container=null,failure=null){
  16. //insert rows to container/schema/query and return with action
  17. let fName="[cvModifyRows/"+mode+"]";
  18. this.print(fName+' '+schema+'/'+query);
  19. let qconfig=new Object();
  20. qconfig.schemaName=schema;
  21. qconfig.queryName=query;
  22. if (container) qconfig.containerPath=container;
  23. if (!rows) {
  24. this.print(fName+' rows '+rows);
  25. return;
  26. }
  27. qconfig.rows=rows;
  28. qconfig.success=function(data){;};
  29. if (action) qconfig.success=action;
  30. if (mode=='insert') LABKEY.Query.insertRows(qconfig);
  31. if (mode=='update') LABKEY.Query.updateRows(qconfig);
  32. if (mode=='delete') LABKEY.Query.deleteRows(qconfig);
  33. this.print(fName+" done");
  34. }
  35. runQuery.selectRows=
  36. function(schema,query,filters=[],action=null, container=null, failure=null, columns=null){
  37. let fName="[cvSelectRows]";
  38. this.print(fName+' '+schema+' '+query+' '+container);
  39. let qconfig=new Object();
  40. qconfig.schemaName=schema;
  41. qconfig.queryName=query;
  42. if (container) qconfig.containerPath=container;
  43. qconfig.filterArray=filters;
  44. qconfig.success=function(data){;};
  45. if (action) qconfig.success=action;
  46. if (failure) qconfig.failure=failure;
  47. if (columns) qconfig.columns=columns;
  48. LABKEY.Query.selectRows(qconfig);
  49. this.print(fName+" done");
  50. }
  51. runQuery.makeQuery=
  52. function(targetObject,containerName,queryName,fieldName=null,filterArray=null,schemaName=null){
  53. //call with makeQuery(config.formConfig,getContainer(name),...
  54. let e=new Object();
  55. e.containerName=containerName;
  56. e.queryName=queryName;
  57. e.fieldName=queryName;
  58. if (fieldName) e.fieldName=fieldName;
  59. e.filterArray=[];
  60. if (filterArray) e.filterArray=filterArray;
  61. e.targetObject=targetObject;
  62. e.schemaName='lists';
  63. if (schemaName) e.schemaName=schemaName;
  64. return e;
  65. }
  66. runQuery.makeModification=
  67. function(mode,containerName,schemaName,queryName,rows){
  68. let e=new Object();
  69. e.mode=mode;
  70. e.containerName=containerName;
  71. e.schemaName=schemaName;
  72. e.queryName=queryName;
  73. e.rows=rows;
  74. return e;
  75. }
  76. runQuery.getDataFromQueries=
  77. function(parentClass,queryArray,cb){
  78. //queryArray should contain elements created with make query
  79. //- fieldName to set the data variable
  80. //- containerName to select container (data,config,CRF)
  81. //- queryName to select query
  82. //- filterArray to perform filtering, empty array works
  83. //- callback cb to be called with no arguments
  84. //
  85. this.afterQuery(new Object(),-1,parentClass,queryArray,cb);
  86. }
  87. runQuery.modifyDataFromQueries=
  88. function(parentClass,queryArray,cb){
  89. //queryArray should contain elements created with make query
  90. //- fieldName to set the data variable
  91. //- containerName to select container (data,config,CRF)
  92. //- queryName to select query
  93. //- filterArray to perform filtering, empty array works
  94. //- callback cb to be called with no arguments
  95. //
  96. this.afterQueryUpload(new Object(),-1,parentClass,queryArray,cb);
  97. }
  98. runQuery.afterQuery=
  99. function(data,id,parentClass,queryArray,cb){
  100. let fName='[afterQuery]';
  101. if (id>-1){
  102. let e1=queryArray[id];
  103. let fieldName=e1.fieldName;
  104. parentClass.print(fName+' ['+fieldName+']: '+data.rows.length);
  105. e1.targetObject[fieldName]=data;
  106. }
  107. id+=1;
  108. if (id==queryArray.length) {
  109. if (cb) cb();
  110. return;
  111. }
  112. let e=queryArray[id];
  113. for (v in e){
  114. parentClass.print(fName+' value ['+v+'] '+e[v]);
  115. }
  116. let containerPath=parentClass.getContainer(e.containerName);
  117. if ("containerPath" in e){
  118. parentClass.print(fName+' containerPath '+e.containerPath);
  119. containerPath=e.containerPath;
  120. }
  121. let schemaName="lists";
  122. if ("schemaName" in e){
  123. parentClass.print(fName+' schemaName='+e.schemaName);
  124. schemaName=e.schemaName;
  125. }
  126. let columns=null;
  127. if ("columns" in e){
  128. parentClass.print(fName+' columns='+e.columns);
  129. columns=e.columns;
  130. }
  131. //this should point to configuration container
  132. //don't filter -> so we can pick up other forms (say registration) later on
  133. //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)];
  134. let filterArray=[];
  135. if ("filterArray" in e)
  136. filterArray=e.filterArray;
  137. //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)]
  138. let that=this;
  139. let action=function(data){that.afterQuery(data,id,parentClass,queryArray,cb);};
  140. let failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);};
  141. this.selectRows(schemaName,e.queryName,filterArray,action, containerPath, failure, columns);
  142. }
  143. runQuery.afterQueryUpload=
  144. function(data,id,parentClass,queryArray,cb){
  145. let fName='[afterQueryUpload]';
  146. if (id>-1){
  147. let x=queryArray[id];
  148. let q=x.queryName
  149. parentClass.print(fName+' ['+q+']: '+data.rows.length);
  150. }
  151. id+=1;
  152. if (id==queryArray.length) {
  153. if (cb) cb();
  154. return;
  155. }
  156. let e=queryArray[id];
  157. for (v in e){
  158. parentClass.print(fName+' value ['+v+'] '+e[v]);
  159. }
  160. let containerPath=parentClass.getContainer(e.containerName);
  161. if ("containerPath" in e){
  162. parentClass.print(fName+' containerPath '+e.containerPath);
  163. containerPath=e.containerPath;
  164. }
  165. let that=this;
  166. let action=function(data){that.afterQueryUpload(data,id,parentClass,queryArray,cb);};
  167. let failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);};
  168. this.modifyRows(e.mode,e.schemaName,e.queryName,e.rows,action,containerPath,failure);
  169. }
  170. runQuery.onTAFailure=
  171. function(parentClass, errorInfo, responseObj){
  172. //don't have configObject to rely to
  173. parentClass.print('[afterQuery]: Failure: '+errorInfo.exception);
  174. }