history.js 28 KB

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