history.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview function used in this file builds history tab and generates query.
  4. *
  5. * @requires jQuery
  6. * @requires moves.js
  7. * @version $Id$
  8. */
  9. var history_array = []; // Global array to store history objects
  10. var select_field = []; // Global array to store informaation for columns which are used in select clause
  11. var g_index;
  12. var vqb_editor = null;
  13. /**
  14. * To display details of objects(where,rename,Having,aggregate,groupby,orderby,having)
  15. *
  16. * @param index index of history_array where change is to be made
  17. *
  18. **/
  19. function detail (index) {
  20. var type = history_array[index].get_type();
  21. var str;
  22. if (type === 'Where') {
  23. str = 'Where ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  24. }
  25. if (type === 'Rename') {
  26. str = 'Rename ' + history_array[index].get_column_name() + ' To ' + history_array[index].get_obj().getrename_to();
  27. }
  28. if (type === 'Aggregate') {
  29. str = 'Select ' + history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
  30. }
  31. if (type === 'GroupBy') {
  32. str = 'GroupBy ' + history_array[index].get_column_name();
  33. }
  34. if (type === 'OrderBy') {
  35. str = 'OrderBy ' + history_array[index].get_column_name() + ' ' + history_array[index].get_obj().get_order();
  36. }
  37. if (type === 'Having') {
  38. str = 'Having ';
  39. if (history_array[index].get_obj().get_operator() !== 'None') {
  40. str += history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
  41. str += history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  42. } else {
  43. str = 'Having ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  44. }
  45. }
  46. return str;
  47. }
  48. /**
  49. * Sorts history_array[] first,using table name as the key and then generates the HTML code for history tab,
  50. * clubbing all objects of same tables together
  51. * This function is called whenever changes are made in history_array[]
  52. *
  53. *
  54. * @param {int} init starting index of unsorted array
  55. * @param {int} finit last index of unsorted array
  56. *
  57. **/
  58. function display (init, finit) {
  59. var str;
  60. var i;
  61. var j;
  62. var k;
  63. var sto;
  64. var temp;
  65. // this part sorts the history array based on table name,this is needed for clubbing all object of same name together.
  66. for (i = init; i < finit; i++) {
  67. sto = history_array[i];
  68. temp = history_array[i].get_tab();// + '.' + history_array[i].get_obj_no(); for Self JOINS
  69. for (j = 0; j < i; j++) {
  70. if (temp > (history_array[j].get_tab())) {// + '.' + history_array[j].get_obj_no())) { //for Self JOINS
  71. for (k = i; k > j; k--) {
  72. history_array[k] = history_array[k - 1];
  73. }
  74. history_array[j] = sto;
  75. break;
  76. }
  77. }
  78. }
  79. // this part generates HTML code for history tab.adds delete,edit,and/or and detail features with objects.
  80. str = ''; // string to store Html code for history tab
  81. for (i = 0; i < history_array.length; i++) {
  82. temp = history_array[i].get_tab(); // + '.' + history_array[i].get_obj_no(); for Self JOIN
  83. str += '<h3 class="tiger"><a href="#">' + temp + '</a></h3>';
  84. str += '<div class="toggle_container">\n';
  85. while ((history_array[i].get_tab()) === temp) { // + '.' + history_array[i].get_obj_no()) === temp) {
  86. str += '<div class="block"> <table width ="250">';
  87. str += '<thead><tr><td>';
  88. if (history_array[i].get_and_or()) {
  89. str += '<img src="' + pmaThemeImage + 'designer/or_icon.png" onclick="and_or(' + i + ')" title="OR"/></td>';
  90. } else {
  91. str += '<img src="' + pmaThemeImage + 'designer/and_icon.png" onclick="and_or(' + i + ')" title="AND"/></td>';
  92. }
  93. str += '<td style="padding-left: 5px;" class="right">' + PMA_getImage('b_sbrowse', 'column name') + '</td>' +
  94. '<td width="175" style="padding-left: 5px">' + history_array[i].get_column_name() + '<td>';
  95. if (history_array[i].get_type() === 'GroupBy' || history_array[i].get_type() === 'OrderBy') {
  96. str += '<td class="center">' + PMA_getImage('s_info', detail(i)) + '</td>' +
  97. '<td title="' + detail(i) + '">' + history_array[i].get_type() + '</td>' +
  98. '<td onclick=history_delete(' + i + ')>' + PMA_getImage('b_drop', PMA_messages.strDelete) + '</td>';
  99. } else {
  100. str += '<td class="center">' + PMA_getImage('s_info', detail(i)) + '</td>' +
  101. '<td title="' + detail(i) + '">' + history_array[i].get_type() + '</td>' +
  102. '<td onclick=history_edit(' + i + ')>' + PMA_getImage('b_edit', PMA_messages.strEdit) + '</td>' +
  103. '<td onclick=history_delete(' + i + ')>' + PMA_getImage('b_drop', PMA_messages.strDelete) + '</td>';
  104. }
  105. str += '</tr></thead>';
  106. i++;
  107. if (i >= history_array.length) {
  108. break;
  109. }
  110. str += '</table></div>';
  111. }
  112. i--;
  113. str += '</div>';
  114. }
  115. return str;
  116. }
  117. /**
  118. * To change And/Or relation in history tab
  119. *
  120. *
  121. * @param {int} index of history_array where change is to be made
  122. *
  123. **/
  124. function and_or (index) {
  125. if (history_array[index].get_and_or()) {
  126. history_array[index].set_and_or(0);
  127. } else {
  128. history_array[index].set_and_or(1);
  129. }
  130. var existingDiv = document.getElementById('ab');
  131. existingDiv.innerHTML = display(0, 0);
  132. $('#ab').accordion('refresh');
  133. }
  134. /**
  135. * Deletes entry in history_array
  136. *
  137. * @param index index of history_array[] which is to be deleted
  138. *
  139. **/
  140. function history_delete (index) {
  141. for (var k = 0; k < from_array.length; k++) {
  142. if (from_array[k] === history_array[index].get_tab()) {
  143. from_array.splice(k, 1);
  144. break;
  145. }
  146. }
  147. history_array.splice(index, 1);
  148. var existingDiv = document.getElementById('ab');
  149. existingDiv.innerHTML = display(0, 0);
  150. $('#ab').accordion('refresh');
  151. }
  152. /**
  153. * To show where,rename,aggregate,having forms to edit a object
  154. *
  155. * @param{int} index index of history_array where change is to be made
  156. *
  157. **/
  158. function history_edit (index) {
  159. g_index = index;
  160. var type = history_array[index].get_type();
  161. if (type === 'Where') {
  162. document.getElementById('eQuery').value = history_array[index].get_obj().getquery();
  163. document.getElementById('erel_opt').value = history_array[index].get_obj().getrelation_operator();
  164. document.getElementById('query_where').style.left = '530px';
  165. document.getElementById('query_where').style.top = '130px';
  166. document.getElementById('query_where').style.position = 'absolute';
  167. document.getElementById('query_where').style.zIndex = '103';
  168. document.getElementById('query_where').style.visibility = 'visible';
  169. document.getElementById('query_where').style.display = 'block';
  170. }
  171. if (type === 'Having') {
  172. document.getElementById('hQuery').value = history_array[index].get_obj().getquery();
  173. document.getElementById('hrel_opt').value = history_array[index].get_obj().getrelation_operator();
  174. document.getElementById('hoperator').value = history_array[index].get_obj().get_operator();
  175. document.getElementById('query_having').style.left = '530px';
  176. document.getElementById('query_having').style.top = '130px';
  177. document.getElementById('query_having').style.position = 'absolute';
  178. document.getElementById('query_having').style.zIndex = '103';
  179. document.getElementById('query_having').style.visibility = 'visible';
  180. document.getElementById('query_having').style.display = 'block';
  181. }
  182. if (type === 'Rename') {
  183. document.getElementById('e_rename').value = history_array[index].get_obj().getrename_to();
  184. document.getElementById('query_rename_to').style.left = '530px';
  185. document.getElementById('query_rename_to').style.top = '130px';
  186. document.getElementById('query_rename_to').style.position = 'absolute';
  187. document.getElementById('query_rename_to').style.zIndex = '103';
  188. document.getElementById('query_rename_to').style.visibility = 'visible';
  189. document.getElementById('query_rename_to').style.display = 'block';
  190. }
  191. if (type === 'Aggregate') {
  192. document.getElementById('e_operator').value = history_array[index].get_obj().get_operator();
  193. document.getElementById('query_Aggregate').style.left = '530px';
  194. document.getElementById('query_Aggregate').style.top = '130px';
  195. document.getElementById('query_Aggregate').style.position = 'absolute';
  196. document.getElementById('query_Aggregate').style.zIndex = '103';
  197. document.getElementById('query_Aggregate').style.visibility = 'visible';
  198. document.getElementById('query_Aggregate').style.display = 'block';
  199. }
  200. }
  201. /**
  202. * Make changes in history_array when Edit button is clicked
  203. * checks for the type of object and then sets the new value
  204. *
  205. * @param index index of history_array where change is to be made
  206. **/
  207. function edit (type) {
  208. if (type === 'Rename') {
  209. if (document.getElementById('e_rename').value !== '') {
  210. history_array[g_index].get_obj().setrename_to(document.getElementById('e_rename').value);
  211. document.getElementById('e_rename').value = '';
  212. }
  213. document.getElementById('query_rename_to').style.visibility = 'hidden';
  214. }
  215. if (type === 'Aggregate') {
  216. if (document.getElementById('e_operator').value !== '---') {
  217. history_array[g_index].get_obj().set_operator(document.getElementById('e_operator').value);
  218. document.getElementById('e_operator').value = '---';
  219. }
  220. document.getElementById('query_Aggregate').style.visibility = 'hidden';
  221. }
  222. if (type === 'Where') {
  223. if (document.getElementById('erel_opt').value !== '--' && document.getElementById('eQuery').value !== '') {
  224. history_array[g_index].get_obj().setquery(document.getElementById('eQuery').value);
  225. history_array[g_index].get_obj().setrelation_operator(document.getElementById('erel_opt').value);
  226. }
  227. document.getElementById('query_where').style.visibility = 'hidden';
  228. }
  229. if (type === 'Having') {
  230. if (document.getElementById('hrel_opt').value !== '--' && document.getElementById('hQuery').value !== '') {
  231. history_array[g_index].get_obj().setquery(document.getElementById('hQuery').value);
  232. history_array[g_index].get_obj().setrelation_operator(document.getElementById('hrel_opt').value);
  233. history_array[g_index].get_obj().set_operator(document.getElementById('hoperator').value);
  234. }
  235. document.getElementById('query_having').style.visibility = 'hidden';
  236. }
  237. var existingDiv = document.getElementById('ab');
  238. existingDiv.innerHTML = display(0, 0);
  239. $('#ab').accordion('refresh');
  240. }
  241. /**
  242. * history object closure
  243. *
  244. * @param ncolumn_name name of the column on which conditions are put
  245. * @param nobj object details(where,rename,orderby,groupby,aggregate)
  246. * @param ntab table name of the column on which conditions are applied
  247. * @param nobj_no object no used for inner join
  248. * @param ntype type of object
  249. *
  250. **/
  251. function history_obj (ncolumn_name, nobj, ntab, nobj_no, ntype) {
  252. var and_or;
  253. var obj;
  254. var tab;
  255. var column_name;
  256. var obj_no;
  257. var type;
  258. this.set_column_name = function (ncolumn_name) {
  259. column_name = ncolumn_name;
  260. };
  261. this.get_column_name = function () {
  262. return column_name;
  263. };
  264. this.set_and_or = function (nand_or) {
  265. and_or = nand_or;
  266. };
  267. this.get_and_or = function () {
  268. return and_or;
  269. };
  270. this.get_relation = function () {
  271. return and_or;
  272. };
  273. this.set_obj = function (nobj) {
  274. obj = nobj;
  275. };
  276. this.get_obj = function () {
  277. return obj;
  278. };
  279. this.set_tab = function (ntab) {
  280. tab = ntab;
  281. };
  282. this.get_tab = function () {
  283. return tab;
  284. };
  285. this.set_obj_no = function (nobj_no) {
  286. obj_no = nobj_no;
  287. };
  288. this.get_obj_no = function () {
  289. return obj_no;
  290. };
  291. this.set_type = function (ntype) {
  292. type = ntype;
  293. };
  294. this.get_type = function () {
  295. return type;
  296. };
  297. this.set_obj_no(nobj_no);
  298. this.set_tab(ntab);
  299. this.set_and_or(0);
  300. this.set_obj(nobj);
  301. this.set_column_name(ncolumn_name);
  302. this.set_type(ntype);
  303. }
  304. /**
  305. * where object closure, makes an object with all information of where
  306. *
  307. * @param nrelation_operator type of relation operator to be applied
  308. * @param nquery stores value of value/sub-query
  309. *
  310. **/
  311. var where = function (nrelation_operator, nquery) {
  312. var relation_operator;
  313. var query;
  314. this.setrelation_operator = function (nrelation_operator) {
  315. relation_operator = nrelation_operator;
  316. };
  317. this.setquery = function (nquery) {
  318. query = nquery;
  319. };
  320. this.getquery = function () {
  321. return query;
  322. };
  323. this.getrelation_operator = function () {
  324. return relation_operator;
  325. };
  326. this.setquery(nquery);
  327. this.setrelation_operator(nrelation_operator);
  328. };
  329. /**
  330. * Orderby object closure
  331. *
  332. * @param norder order, ASC or DESC
  333. */
  334. var orderby = function (norder) {
  335. var order;
  336. this.set_order = function (norder) {
  337. order = norder;
  338. };
  339. this.get_order = function () {
  340. return order;
  341. };
  342. this.set_order(norder);
  343. };
  344. /**
  345. * Having object closure, makes an object with all information of where
  346. *
  347. * @param nrelation_operator type of relation operator to be applied
  348. * @param nquery stores value of value/sub-query
  349. * @param noperator operator
  350. **/
  351. var having = function (nrelation_operator, nquery, noperator) {
  352. var relation_operator;
  353. var query;
  354. var operator;
  355. this.set_operator = function (noperator) {
  356. operator = noperator;
  357. };
  358. this.setrelation_operator = function (nrelation_operator) {
  359. relation_operator = nrelation_operator;
  360. };
  361. this.setquery = function (nquery) {
  362. query = nquery;
  363. };
  364. this.getquery = function () {
  365. return query;
  366. };
  367. this.getrelation_operator = function () {
  368. return relation_operator;
  369. };
  370. this.get_operator = function () {
  371. return operator;
  372. };
  373. this.setquery(nquery);
  374. this.setrelation_operator(nrelation_operator);
  375. this.set_operator(noperator);
  376. };
  377. /**
  378. * rename object closure,makes an object with all information of rename
  379. *
  380. * @param nrename_to new name information
  381. *
  382. **/
  383. var rename = function (nrename_to) {
  384. var rename_to;
  385. this.setrename_to = function (nrename_to) {
  386. rename_to = nrename_to;
  387. };
  388. this.getrename_to = function () {
  389. return rename_to;
  390. };
  391. this.setrename_to(nrename_to);
  392. };
  393. /**
  394. * aggregate object closure
  395. *
  396. * @param noperator aggregte operator
  397. *
  398. **/
  399. var aggregate = function (noperator) {
  400. var operator;
  401. this.set_operator = function (noperator) {
  402. operator = noperator;
  403. };
  404. this.get_operator = function () {
  405. return operator;
  406. };
  407. this.set_operator(noperator);
  408. };
  409. /**
  410. * This function returns unique element from an array
  411. *
  412. * @param arrayName array from which duplicate elem are to be removed.
  413. * @return unique array
  414. */
  415. function unique (arrayName) {
  416. var newArray = [];
  417. uniquetop:
  418. for (var i = 0; i < arrayName.length; i++) {
  419. for (var j = 0; j < newArray.length; j++) {
  420. if (newArray[j] === arrayName[i]) {
  421. continue uniquetop;
  422. }
  423. }
  424. newArray[newArray.length] = arrayName[i];
  425. }
  426. return newArray;
  427. }
  428. /**
  429. * This function takes in array and a value as input and returns 1 if values is present in array
  430. * else returns -1
  431. *
  432. * @param arrayName array
  433. * @param value value which is to be searched in the array
  434. */
  435. function found (arrayName, value) {
  436. for (var i = 0; i < arrayName.length; i++) {
  437. if (arrayName[i] === value) {
  438. return 1;
  439. }
  440. }
  441. return -1;
  442. }
  443. /**
  444. * This function concatenates two array
  445. *
  446. * @params add array elements of which are pushed in
  447. * @params arr array in which elements are added
  448. */
  449. function add_array (add, arr) {
  450. for (var i = 0; i < add.length; i++) {
  451. arr.push(add[i]);
  452. }
  453. return arr;
  454. }
  455. /* This function removes all elements present in one array from the other.
  456. *
  457. * @params rem array from which each element is removed from other array.
  458. * @params arr array from which elements are removed.
  459. *
  460. */
  461. function remove_array (rem, arr) {
  462. for (var i = 0; i < rem.length; i++) {
  463. for (var j = 0; j < arr.length; j++) {
  464. if (rem[i] === arr[j]) {
  465. arr.splice(j, 1);
  466. }
  467. }
  468. }
  469. return arr;
  470. }
  471. /**
  472. * This function builds the groupby clause from history object
  473. *
  474. */
  475. function query_groupby () {
  476. var i;
  477. var str = '';
  478. for (i = 0; i < history_array.length;i++) {
  479. if (history_array[i].get_type() === 'GroupBy') {
  480. str += '`' + history_array[i].get_column_name() + '`, ';
  481. }
  482. }
  483. str = str.substr(0, str.length - 2);
  484. return str;
  485. }
  486. /**
  487. * This function builds the Having clause from the history object.
  488. *
  489. */
  490. function query_having () {
  491. var i;
  492. var and = '(';
  493. for (i = 0; i < history_array.length;i++) {
  494. if (history_array[i].get_type() === 'Having') {
  495. if (history_array[i].get_obj().get_operator() !== 'None') {
  496. and += history_array[i].get_obj().get_operator() + '(`' + history_array[i].get_column_name() + '`) ' + history_array[i].get_obj().getrelation_operator();
  497. and += ' ' + history_array[i].get_obj().getquery() + ', ';
  498. } else {
  499. and += '`' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ', ';
  500. }
  501. }
  502. }
  503. if (and === '(') {
  504. and = '';
  505. } else {
  506. and = and.substr(0, and.length - 2) + ')';
  507. }
  508. return and;
  509. }
  510. /**
  511. * This function builds the orderby clause from the history object.
  512. *
  513. */
  514. function query_orderby () {
  515. var i;
  516. var str = '';
  517. for (i = 0; i < history_array.length;i++) {
  518. if (history_array[i].get_type() === 'OrderBy') {
  519. str += '`' + history_array[i].get_column_name() + '` ' +
  520. history_array[i].get_obj().get_order() + ', ';
  521. }
  522. }
  523. str = str.substr(0, str.length - 2);
  524. return str;
  525. }
  526. /**
  527. * This function builds the Where clause from the history object.
  528. *
  529. */
  530. function query_where () {
  531. var i;
  532. var and = '(';
  533. var or = '(';
  534. for (i = 0; i < history_array.length;i++) {
  535. if (history_array[i].get_type() === 'Where') {
  536. if (history_array[i].get_and_or() === 0) {
  537. and += '( `' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ')';
  538. and += ' AND ';
  539. } else {
  540. or += '( `' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ')';
  541. or += ' OR ';
  542. }
  543. }
  544. }
  545. if (or !== '(') {
  546. or = or.substring(0, (or.length - 4)) + ')';
  547. } else {
  548. or = '';
  549. }
  550. if (and !== '(') {
  551. and = and.substring(0, (and.length - 5)) + ')';
  552. } else {
  553. and = '';
  554. }
  555. if (or !== '') {
  556. and = and + ' OR ' + or + ' )';
  557. }
  558. return and;
  559. }
  560. function check_aggregate (id_this) {
  561. var i;
  562. for (i = 0; i < history_array.length; i++) {
  563. var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
  564. if (temp === id_this && history_array[i].get_type() === 'Aggregate') {
  565. return history_array[i].get_obj().get_operator() + '(' + id_this + ')';
  566. }
  567. }
  568. return '';
  569. }
  570. function check_rename (id_this) {
  571. var i;
  572. for (i = 0; i < history_array.length; i++) {
  573. var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
  574. if (temp === id_this && history_array[i].get_type() === 'Rename') {
  575. return ' AS `' + history_array[i].get_obj().getrename_to() + '`';
  576. }
  577. }
  578. return '';
  579. }
  580. /**
  581. * This function builds from clause of query
  582. * makes automatic joins.
  583. *
  584. *
  585. */
  586. function query_from () {
  587. var i;
  588. var tab_left = [];
  589. var tab_used = [];
  590. var t_tab_used = [];
  591. var t_tab_left = [];
  592. var temp;
  593. var query = '';
  594. var quer = '';
  595. var parts = [];
  596. var t_array = [];
  597. t_array = from_array;
  598. var K = 0;
  599. var k;
  600. var key;
  601. var key2;
  602. var key3;
  603. var parts1;
  604. // the constraints that have been used in the LEFT JOIN
  605. var constraints_added = [];
  606. for (i = 0; i < history_array.length; i++) {
  607. from_array.push(history_array[i].get_tab());
  608. }
  609. from_array = unique(from_array);
  610. tab_left = from_array;
  611. temp = tab_left.shift();
  612. quer = '`' + temp + '`';
  613. tab_used.push(temp);
  614. // if master table (key2) matches with tab used get all keys and check if tab_left matches
  615. // after this check if master table (key2) matches with tab left then check if any foreign matches with master .
  616. for (i = 0; i < 2; i++) {
  617. for (K in contr) {
  618. for (key in contr[K]) {// contr name
  619. for (key2 in contr[K][key]) {// table name
  620. parts = key2.split('.');
  621. if (found(tab_used, parts[1]) > 0) {
  622. for (key3 in contr[K][key][key2]) {
  623. parts1 = contr[K][key][key2][key3][0].split('.');
  624. if (found(tab_left, parts1[1]) > 0) {
  625. if (found(constraints_added, key) > 0) {
  626. query += ' AND ' + '`' + parts[1] + '`.`' + key3 + '` = ';
  627. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
  628. } else {
  629. query += '\n' + 'LEFT JOIN ';
  630. query += '`' + parts[1] + '` ON ';
  631. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
  632. query += '`' + parts[1] + '`.`' + key3 + '` ';
  633. constraints_added.push(key);
  634. }
  635. t_tab_left.push(parts[1]);
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. K = 0;
  643. t_tab_left = unique(t_tab_left);
  644. tab_used = add_array(t_tab_left, tab_used);
  645. tab_left = remove_array(t_tab_left, tab_left);
  646. t_tab_left = [];
  647. for (K in contr) {
  648. for (key in contr[K]) {
  649. for (key2 in contr[K][key]) {// table name
  650. parts = key2.split('.');
  651. if (found(tab_left, parts[1]) > 0) {
  652. for (key3 in contr[K][key][key2]) {
  653. parts1 = contr[K][key][key2][key3][0].split('.');
  654. if (found(tab_used, parts1[1]) > 0) {
  655. if (found(constraints_added, key) > 0) {
  656. query += ' AND ' + '`' + parts[1] + '`.`' + key3 + '` = ';
  657. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
  658. } else {
  659. query += '\n' + 'LEFT JOIN ';
  660. query += '`' + parts[1] + '` ON ';
  661. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
  662. query += '`' + parts[1] + '`.`' + key3 + '` ';
  663. constraints_added.push(key);
  664. }
  665. t_tab_left.push(parts[1]);
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. t_tab_left = unique(t_tab_left);
  673. tab_used = add_array(t_tab_left, tab_used);
  674. tab_left = remove_array(t_tab_left, tab_left);
  675. t_tab_left = [];
  676. }
  677. for (k in tab_left) {
  678. quer += ' , `' + tab_left[k] + '`';
  679. }
  680. query = quer + query;
  681. from_array = t_array;
  682. return query;
  683. }
  684. /**
  685. * This function is the main function for query building.
  686. * uses history object details for this.
  687. *
  688. * @ uses query_where()
  689. * @ uses query_groupby()
  690. * @ uses query_having()
  691. * @ uses query_orderby()
  692. *
  693. * @param formtitle title for the form
  694. * @param fadin
  695. */
  696. function build_query (formtitle, fadin) {
  697. var q_select = 'SELECT ';
  698. var temp;
  699. if (select_field.length > 0) {
  700. for (var i = 0; i < select_field.length; i++) {
  701. temp = check_aggregate(select_field[i]);
  702. if (temp !== '') {
  703. q_select += temp;
  704. temp = check_rename(select_field[i]);
  705. q_select += temp + ', ';
  706. } else {
  707. temp = check_rename(select_field[i]);
  708. q_select += select_field[i] + temp + ', ';
  709. }
  710. }
  711. q_select = q_select.substring(0, q_select.length - 2);
  712. } else {
  713. q_select += '* ';
  714. }
  715. q_select += '\nFROM ' + query_from();
  716. var q_where = query_where();
  717. if (q_where !== '') {
  718. q_select += '\nWHERE ' + q_where;
  719. }
  720. var q_groupby = query_groupby();
  721. if (q_groupby !== '') {
  722. q_select += '\nGROUP BY ' + q_groupby;
  723. }
  724. var q_having = query_having();
  725. if (q_having !== '') {
  726. q_select += '\nHAVING ' + q_having;
  727. }
  728. var q_orderby = query_orderby();
  729. if (q_orderby !== '') {
  730. q_select += '\nORDER BY ' + q_orderby;
  731. }
  732. /**
  733. * @var button_options Object containing options
  734. * for jQueryUI dialog buttons
  735. */
  736. var button_options = {};
  737. button_options[PMA_messages.strClose] = function () {
  738. $(this).dialog('close');
  739. };
  740. button_options[PMA_messages.strSubmit] = function () {
  741. if (vqb_editor) {
  742. var $elm = $ajaxDialog.find('textarea');
  743. vqb_editor.save();
  744. $elm.val(vqb_editor.getValue());
  745. }
  746. $('#vqb_form').submit();
  747. };
  748. var $ajaxDialog = $('#box').dialog({
  749. appendTo: '#page_content',
  750. width: 500,
  751. buttons: button_options,
  752. modal: true,
  753. title: 'SELECT'
  754. });
  755. // Attach syntax highlighted editor to query dialog
  756. /**
  757. * @var $elm jQuery object containing the reference
  758. * to the query textarea.
  759. */
  760. var $elm = $ajaxDialog.find('textarea');
  761. if (! vqb_editor) {
  762. vqb_editor = PMA_getSQLEditor($elm);
  763. }
  764. if (vqb_editor) {
  765. vqb_editor.setValue(q_select);
  766. vqb_editor.focus();
  767. } else {
  768. $elm.val(q_select);
  769. $elm.focus();
  770. }
  771. }
  772. AJAX.registerTeardown('designer/history.js', function () {
  773. vqb_editor = null;
  774. history_array = [];
  775. select_field = [];
  776. $('#ok_edit_rename').off('click');
  777. $('#ok_edit_having').off('click');
  778. $('#ok_edit_Aggr').off('click');
  779. $('#ok_edit_where').off('click');
  780. });
  781. AJAX.registerOnload('designer/history.js', function () {
  782. $('#ok_edit_rename').click(function () {
  783. edit('Rename');
  784. });
  785. $('#ok_edit_having').click(function () {
  786. edit('Having');
  787. });
  788. $('#ok_edit_Aggr').click(function () {
  789. edit('Aggregate');
  790. });
  791. $('#ok_edit_where').click(function () {
  792. edit('Where');
  793. });
  794. $('#ab').accordion({ collapsible : true, active : 'none' });
  795. });