history.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. /**
  13. * function for panel, hides and shows toggle_container <div>,which is for history elements uses {@link JQuery}.
  14. *
  15. * @param index has value 1 or 0,decides wheter to hide toggle_container on load.
  16. **/
  17. function panel(index)
  18. {
  19. if (!index) {
  20. $(".toggle_container").hide();
  21. }
  22. $("h2.tiger").click(function () {
  23. $(this).toggleClass("active").next().slideToggle("slow");
  24. });
  25. }
  26. /**
  27. * To display details of obects(where,rename,Having,aggregate,groupby,orderby,having)
  28. *
  29. * @param index index of history_array where change is to be made
  30. *
  31. **/
  32. function detail(index)
  33. {
  34. var type = history_array[index].get_type();
  35. var str;
  36. if (type == "Where") {
  37. str = 'Where ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  38. }
  39. if (type == "Rename") {
  40. str = 'Rename ' + history_array[index].get_column_name() + ' To ' + history_array[index].get_obj().getrename_to();
  41. }
  42. if (type == "Aggregate") {
  43. str = 'Select ' + history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
  44. }
  45. if (type == "GroupBy") {
  46. str = 'GroupBy ' + history_array[index].get_column_name();
  47. }
  48. if (type == "OrderBy") {
  49. str = 'OrderBy ' + history_array[index].get_column_name();
  50. }
  51. if (type == "Having") {
  52. str = 'Having ';
  53. if (history_array[index].get_obj().get_operator() != 'None') {
  54. str += history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
  55. str += history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  56. } else {
  57. str = 'Having ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
  58. }
  59. }
  60. return str;
  61. }
  62. /**
  63. * Sorts history_array[] first,using table name as the key and then generates the HTML code for history tab,
  64. * clubbing all objects of same tables together
  65. * This function is called whenever changes are made in history_array[]
  66. *
  67. *
  68. * @param {int} init starting index of unsorted array
  69. * @param {int} finit last index of unsorted array
  70. *
  71. **/
  72. function display(init, finit)
  73. {
  74. var str, i, j, k, sto, temp;
  75. // this part sorts the history array based on table name,this is needed for clubbing all object of same name together.
  76. for (i = init; i < finit; i++) {
  77. sto = history_array[i];
  78. temp = history_array[i].get_tab();//+ '.' + history_array[i].get_obj_no(); for Self JOINS
  79. for (j = 0; j < i; j++) {
  80. if (temp > (history_array[j].get_tab())) {//+ '.' + history_array[j].get_obj_no())) { //for Self JOINS
  81. for (k = i; k > j; k--) {
  82. history_array[k] = history_array[k - 1];
  83. }
  84. history_array[j] = sto;
  85. break;
  86. }
  87. }
  88. }
  89. // this part generates HTML code for history tab.adds delete,edit,and/or and detail features with objects.
  90. str = ''; // string to store Html code for history tab
  91. for (i = 0; i < history_array.length; i++) {
  92. temp = history_array[i].get_tab(); //+ '.' + history_array[i].get_obj_no(); for Self JOIN
  93. str += '<h2 class="tiger"><a href="#">' + temp + '</a></h2>';
  94. str += '<div class="toggle_container">\n';
  95. while ((history_array[i].get_tab()) == temp) { //+ '.' + history_array[i].get_obj_no()) == temp) {
  96. str += '<div class="block"> <table width ="250">';
  97. str += '<thead><tr><td>';
  98. if (history_array[i].get_and_or()) {
  99. str += '<img src="' + pmaThemeImage + 'pmd/or_icon.png" onclick="and_or(' + i + ')" title="OR"/></td>';
  100. } else {
  101. str += '<img src="' + pmaThemeImage + 'pmd/and_icon.png" onclick="and_or(' + i + ')" title="AND"/></td>';
  102. }
  103. str += '<td style="padding-left: 5px;" class="right">' + PMA_getImage('b_sbrowse.png', 'column name') + '</td><td width="175" style="padding-left: 5px">' + history_array[i].get_column_name();
  104. if (history_array[i].get_type() == "GroupBy" || history_array[i].get_type() == "OrderBy") {
  105. str += '</td><td class="center">' + PMA_getImage('s_info.png', detail(i)) + '<td title="' + detail(i) + '">' + history_array[i].get_type() + '</td></td><td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_delete(' + i + ')>' + PMA_getImage('b_drop.png', 'Delete') + '</td></tr></thead>';
  106. } else {
  107. str += '</td><td class="center">' + PMA_getImage('s_info.png', detail(i)) + '</td><td title="' + detail(i) + '">' + history_array[i].get_type() + '</td><td <td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_edit(' + i + ')>' + PMA_getImage('b_edit.png', PMA_messages.strEdit) + '</td><td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_delete(' + i + ')><img src="themes/original/img/b_drop.png" title="Delete"></td></tr></thead>';
  108. }
  109. i++;
  110. if (i >= history_array.length) {
  111. break;
  112. }
  113. str += '</table></div><br/>';
  114. }
  115. i--;
  116. str += '</div><br/>';
  117. }
  118. return str;
  119. }
  120. /**
  121. * To change And/Or relation in history tab
  122. *
  123. *
  124. * @param {int} index of history_array where change is to be made
  125. *
  126. **/
  127. function and_or(index)
  128. {
  129. if (history_array[index].get_and_or()) {
  130. history_array[index].set_and_or(0);
  131. } else {
  132. history_array[index].set_and_or(1);
  133. }
  134. var existingDiv = document.getElementById('ab');
  135. existingDiv.innerHTML = display(0, 0);
  136. panel(1);
  137. }
  138. /**
  139. * Deletes entry in history_array
  140. *
  141. * @param index index of history_array[] which is to be deleted
  142. *
  143. **/
  144. function history_delete(index)
  145. {
  146. for (var k = 0; k < from_array.length; k++) {
  147. if (from_array[k] == history_array[index].get_tab()) {
  148. from_array.splice(k, 1);
  149. break;
  150. }
  151. }
  152. history_array.splice(index, 1);
  153. var existingDiv = document.getElementById('ab');
  154. existingDiv.innerHTML = display(0, 0);
  155. panel(1);
  156. }
  157. /**
  158. * To show where,rename,aggregate,having forms to edit a object
  159. *
  160. * @param{int} index index of history_array where change is to be made
  161. *
  162. **/
  163. function history_edit(index)
  164. {
  165. g_index = index;
  166. var type = history_array[index].get_type();
  167. if (type == "Where") {
  168. document.getElementById('eQuery').value = history_array[index].get_obj().getquery();
  169. document.getElementById('erel_opt').value = history_array[index].get_obj().getrelation_operator();
  170. document.getElementById('query_where').style.left = '530px';
  171. document.getElementById('query_where').style.top = '130px';
  172. document.getElementById('query_where').style.position = 'absolute';
  173. document.getElementById('query_where').style.zIndex = '9';
  174. document.getElementById('query_where').style.visibility = 'visible';
  175. }
  176. if (type == "Having") {
  177. document.getElementById('hQuery').value = history_array[index].get_obj().getquery();
  178. document.getElementById('hrel_opt').value = history_array[index].get_obj().getrelation_operator();
  179. document.getElementById('hoperator').value = history_array[index].get_obj().get_operator();
  180. document.getElementById('query_having').style.left = '530px';
  181. document.getElementById('query_having').style.top = '130px';
  182. document.getElementById('query_having').style.position = 'absolute';
  183. document.getElementById('query_having').style.zIndex = '9';
  184. document.getElementById('query_having').style.visibility = 'visible';
  185. }
  186. if (type == "Rename") {
  187. document.getElementById('query_rename_to').style.left = '530px';
  188. document.getElementById('query_rename_to').style.top = '130px';
  189. document.getElementById('query_rename_to').style.position = 'absolute';
  190. document.getElementById('query_rename_to').style.zIndex = '9';
  191. document.getElementById('query_rename_to').style.visibility = 'visible';
  192. }
  193. if (type == "Aggregate") {
  194. document.getElementById('query_Aggregate').style.left = '530px';
  195. document.getElementById('query_Aggregate').style.top = '130px';
  196. document.getElementById('query_Aggregate').style.position = 'absolute';
  197. document.getElementById('query_Aggregate').style.zIndex = '9';
  198. document.getElementById('query_Aggregate').style.visibility = 'visible';
  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. {
  209. if (type == "Rename") {
  210. if (document.getElementById('e_rename').value !== "") {
  211. history_array[g_index].get_obj().setrename_to(document.getElementById('e_rename').value);
  212. document.getElementById('e_rename').value = "";
  213. }
  214. document.getElementById('query_rename_to').style.visibility = 'hidden';
  215. }
  216. if (type == "Aggregate") {
  217. if (document.getElementById('e_operator').value != '---') {
  218. history_array[g_index].get_obj().set_operator(document.getElementById('e_operator').value);
  219. document.getElementById('e_operator').value = '---';
  220. }
  221. document.getElementById('query_Aggregate').style.visibility = 'hidden';
  222. }
  223. if (type == "Where") {
  224. if (document.getElementById('erel_opt').value != '--' && document.getElementById('eQuery').value !== "") {
  225. history_array[g_index].get_obj().setquery(document.getElementById('eQuery').value);
  226. history_array[g_index].get_obj().setrelation_operator(document.getElementById('erel_opt').value);
  227. }
  228. document.getElementById('query_where').style.visibility = 'hidden';
  229. }
  230. if (type == "Having") {
  231. if (document.getElementById('hrel_opt').value != '--' && document.getElementById('hQuery').value !== "") {
  232. history_array[g_index].get_obj().setquery(document.getElementById('hQuery').value);
  233. history_array[g_index].get_obj().setrelation_operator(document.getElementById('hrel_opt').value);
  234. history_array[g_index].get_obj().set_operator(document.getElementById('hoperator').value);
  235. }
  236. document.getElementById('query_having').style.visibility = 'hidden';
  237. }
  238. var existingDiv = document.getElementById('ab');
  239. existingDiv.innerHTML = display(0, 0);
  240. panel(1);
  241. }
  242. /**
  243. * history object closure
  244. *
  245. * @param ncolumn_name name of the column on which conditions are put
  246. * @param nobj object details(where,rename,orderby,groupby,aggregate)
  247. * @param ntab table name of the column on which conditions are applied
  248. * @param nobj_no object no used for inner join
  249. * @param ntype type of object
  250. *
  251. **/
  252. function history(ncolumn_name, nobj, ntab, nobj_no, ntype)
  253. {
  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. * Having object closure, makes an object with all information of where
  333. *
  334. * @param nrelation_operator type of relation operator to be applied
  335. * @param nquery stores value of value/sub-query
  336. *
  337. **/
  338. var having = function (nrelation_operator, nquery, noperator) {
  339. var relation_operator;
  340. var query;
  341. var operator;
  342. this.set_operator = function (noperator) {
  343. operator = noperator;
  344. };
  345. this.setrelation_operator = function (nrelation_operator) {
  346. relation_operator = nrelation_operator;
  347. };
  348. this.setquery = function (nquery) {
  349. query = nquery;
  350. };
  351. this.getquery = function () {
  352. return query;
  353. };
  354. this.getrelation_operator = function () {
  355. return relation_operator;
  356. };
  357. this.get_operator = function () {
  358. return operator;
  359. };
  360. this.setquery(nquery);
  361. this.setrelation_operator(nrelation_operator);
  362. this.set_operator(noperator);
  363. };
  364. /**
  365. * rename object closure,makes an object with all information of rename
  366. *
  367. * @param nrename_to new name information
  368. *
  369. **/
  370. var rename = function (nrename_to) {
  371. var rename_to;
  372. this.setrename_to = function (nrename_to) {
  373. rename_to = nrename_to;
  374. };
  375. this.getrename_to = function () {
  376. return rename_to;
  377. };
  378. this.setrename_to(nrename_to);
  379. };
  380. /**
  381. * aggregate object closure
  382. *
  383. * @param noperator aggregte operator
  384. *
  385. **/
  386. var aggregate = function (noperator) {
  387. var operator;
  388. this.set_operator = function (noperator) {
  389. operator = noperator;
  390. };
  391. this.get_operator = function () {
  392. return operator;
  393. };
  394. this.set_operator(noperator);
  395. };
  396. /**
  397. * This function returns unique element from an array
  398. *
  399. * @param arraName array from which duplicate elem are to be removed.
  400. * @return unique array
  401. */
  402. function unique(arrayName)
  403. {
  404. var newArray = [];
  405. uniquetop:
  406. for (var i = 0; i < arrayName.length; i++) {
  407. for (var j = 0; j < newArray.length; j++) {
  408. if (newArray[j] == arrayName[i]) {
  409. continue uniquetop;
  410. }
  411. }
  412. newArray[newArray.length] = arrayName[i];
  413. }
  414. return newArray;
  415. }
  416. /**
  417. * This function takes in array and a value as input and returns 1 if values is present in array
  418. * else returns -1
  419. *
  420. * @param arrayName array
  421. * @param value value which is to be searched in the array
  422. */
  423. function found(arrayName, value)
  424. {
  425. for (var i = 0; i < arrayName.length; i++) {
  426. if (arrayName[i] == value) {
  427. return 1;
  428. }
  429. }
  430. return -1;
  431. }
  432. /**
  433. * This function concatenates two array
  434. *
  435. * @params add array elements of which are pushed in
  436. * @params arr array in which elemnets are added
  437. */
  438. function add_array(add, arr)
  439. {
  440. for (var i = 0; i < add.length; i++) {
  441. arr.push(add[i]);
  442. }
  443. return arr;
  444. }
  445. /* This fucntion removes all elements present in one array from the other.
  446. *
  447. * @params rem array from which each element is removed from other array.
  448. * @params arr array from which elements are removed.
  449. *
  450. */
  451. function remove_array(rem, arr)
  452. {
  453. for (var i = 0; i < rem.length; i++) {
  454. for (var j = 0; j < arr.length; j++) {
  455. if (rem[i] == arr[j]) {
  456. arr.splice(j, 1);
  457. }
  458. }
  459. }
  460. return arr;
  461. }
  462. /**
  463. * This function builds the groupby clause from history object
  464. *
  465. */
  466. function query_groupby()
  467. {
  468. var i;
  469. var str = "";
  470. for (i = 0; i < history_array.length;i++) {
  471. if (history_array[i].get_type() == "GroupBy") {
  472. str += history_array[i].get_column_name() + ", ";
  473. }
  474. }
  475. str = str.substr(0, str.length - 1);
  476. return str;
  477. }
  478. /**
  479. * This function builds the Having clause from the history object.
  480. *
  481. */
  482. function query_having()
  483. {
  484. var i;
  485. var and = "(";
  486. for (i = 0; i < history_array.length;i++) {
  487. if (history_array[i].get_type() == "Having") {
  488. if (history_array[i].get_obj().get_operator() != 'None') {
  489. and += history_array[i].get_obj().get_operator() + "(" + history_array[i].get_column_name() + " ) " + history_array[i].get_obj().getrelation_operator();
  490. and += " " + history_array[i].get_obj().getquery() + ", ";
  491. } else {
  492. and += history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() + ", ";
  493. }
  494. }
  495. }
  496. if (and == "(") {
  497. and = "";
  498. } else {
  499. and = and.substr(0, and.length - 2) + ")";
  500. }
  501. return and;
  502. }
  503. /**
  504. * This function builds the orderby clause from the history object.
  505. *
  506. */
  507. function query_orderby()
  508. {
  509. var i;
  510. var str = "";
  511. for (i = 0; i < history_array.length;i++) {
  512. if (history_array[i].get_type() == "OrderBy") { str += history_array[i].get_column_name() + " , "; }
  513. }
  514. str = str.substr(0, str.length - 1);
  515. return str;
  516. }
  517. /**
  518. * This function builds the Where clause from the history object.
  519. *
  520. */
  521. function query_where()
  522. {
  523. var i;
  524. var and = "(";
  525. var or = "(";
  526. for (i = 0; i < history_array.length;i++) {
  527. if (history_array[i].get_type() == "Where") {
  528. if (history_array[i].get_and_or() === 0) {
  529. and += "( " + history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() + ")";
  530. and += " AND ";
  531. } else {
  532. or += "( " + history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() + ")";
  533. or += " OR ";
  534. }
  535. }
  536. }
  537. if (or != "(") {
  538. or = or.substring(0, (or.length - 4)) + ")";
  539. } else {
  540. or = "";
  541. }
  542. if (and != "(") {
  543. and = and.substring(0, (and.length - 5)) + ")";
  544. } else {
  545. and = "";
  546. }
  547. if (or !== "") {
  548. and = and + " OR " + or + " )";
  549. }
  550. return and;
  551. }
  552. function check_aggregate(id_this)
  553. {
  554. var i;
  555. for (i = 0; i < history_array.length; i++) {
  556. var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
  557. if (temp == id_this && history_array[i].get_type() == "Aggregate") {
  558. return history_array[i].get_obj().get_operator() + '(' + id_this + ')';
  559. }
  560. }
  561. return "";
  562. }
  563. function check_rename(id_this)
  564. {
  565. var i;
  566. for (i = 0; i < history_array.length; i++) {
  567. var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
  568. if (temp == id_this && history_array[i].get_type() == "Rename") {
  569. return " AS `" + history_array[i].get_obj().getrename_to() + "`";
  570. }
  571. }
  572. return "";
  573. }
  574. function gradient(id, level)
  575. {
  576. var box = document.getElementById(id);
  577. box.style.opacity = level;
  578. box.style.MozOpacity = level;
  579. box.style.KhtmlOpacity = level;
  580. box.style.filter = "alpha(opacity=" + level * 100 + ")";
  581. box.style.display = "block";
  582. return;
  583. }
  584. function fadein(id)
  585. {
  586. var level = 0;
  587. while (level <= 1) {
  588. setTimeout("gradient('" + id + "'," + level + ")", (level * 1000) + 10);
  589. level += 0.01;
  590. }
  591. }
  592. /**
  593. * This function builds from clause of query
  594. * makes automatic joins.
  595. *
  596. *
  597. */
  598. function query_from()
  599. {
  600. var i;
  601. var tab_left = [];
  602. var tab_used = [];
  603. var t_tab_used = [];
  604. var t_tab_left = [];
  605. var temp;
  606. var query = "";
  607. var quer = "";
  608. var parts = [];
  609. var t_array = [];
  610. t_array = from_array;
  611. var K = 0;
  612. var k;
  613. var key;
  614. var key2;
  615. var key3;
  616. var parts1;
  617. for (i = 0; i < history_array.length; i++) {
  618. from_array.push(history_array[i].get_tab());
  619. }
  620. from_array = unique(from_array);
  621. tab_left = from_array;
  622. temp = tab_left.shift();
  623. quer = temp;
  624. tab_used.push(temp);
  625. // if master table (key2) matches with tab used get all keys and check if tab_left matches
  626. // after this check if master table (key2) matches with tab left then check if any foreign matches with master .
  627. for (i = 0; i < 2; i++) {
  628. for (K in contr) {
  629. for (key in contr[K]) {// contr name
  630. for (key2 in contr[K][key]) {// table name
  631. parts = key2.split(".");
  632. if (found(tab_used, parts[1]) > 0) {
  633. for (key3 in contr[K][key][key2]) {
  634. parts1 = contr[K][key][key2][key3][0].split(".");
  635. if (found(tab_left, parts1[1]) > 0) {
  636. query += "\n" + 'LEFT JOIN ';
  637. query += '`' + parts1[0] + '`.`' + parts1[1] + '` ON ';
  638. query += '`' + parts[1] + '`.`' + key3 + '` = ';
  639. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
  640. t_tab_left.push(parts1[1]);
  641. }
  642. }
  643. }
  644. }
  645. }
  646. }
  647. K = 0;
  648. t_tab_left = unique(t_tab_left);
  649. tab_used = add_array(t_tab_left, tab_used);
  650. tab_left = remove_array(t_tab_left, tab_left);
  651. t_tab_left = [];
  652. for (K in contr) {
  653. for (key in contr[K]) {
  654. for (key2 in contr[K][key]) {// table name
  655. parts = key2.split(".");
  656. if (found(tab_left, parts[1]) > 0) {
  657. for (key3 in contr[K][key][key2]) {
  658. parts1 = contr[K][key][key2][key3][0].split(".");
  659. if (found(tab_used, parts1[1]) > 0) {
  660. query += "\n" + 'LEFT JOIN ';
  661. query += '`' + parts[0] + '`.`' + parts[1] + '` ON ';
  662. query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
  663. query += '`' + parts[1] + '`.`' + key3 + '` ';
  664. t_tab_left.push(parts[1]);
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. t_tab_left = unique(t_tab_left);
  672. tab_used = add_array(t_tab_left, tab_used);
  673. tab_left = remove_array(t_tab_left, tab_left);
  674. t_tab_left = [];
  675. }
  676. for (k in tab_left) {
  677. quer += " , `" + tab_left[k] + "`";
  678. }
  679. query = quer + query;
  680. from_array = t_array;
  681. return query;
  682. }
  683. /**
  684. * This function is the main function for query building.
  685. * uses history object details for this.
  686. *
  687. * @ uses query_where()
  688. * @ uses query_groupby()
  689. * @ uses query_having()
  690. * @ uses query_orderby()
  691. *
  692. * @param formtitle title for the form
  693. * @param fadin
  694. */
  695. function build_query(formtitle, fadin)
  696. {
  697. var q_select = "SELECT ";
  698. var temp;
  699. for (var i = 0;i < select_field.length; i++) {
  700. temp = check_aggregate(select_field[i]);
  701. if (temp !== "") {
  702. q_select += temp;
  703. temp = check_rename(select_field[i]);
  704. q_select += temp + ",";
  705. } else {
  706. temp = check_rename(select_field[i]);
  707. q_select += select_field[i] + temp + ",";
  708. }
  709. }
  710. q_select = q_select.substring(0, q_select.length - 1);
  711. q_select += " FROM " + query_from();
  712. if (query_where() !== "") {
  713. q_select += "\n WHERE";
  714. q_select += query_where();
  715. }
  716. if (query_groupby() !== "") { q_select += "\nGROUP BY " + query_groupby(); }
  717. if (query_having() !== "") { q_select += "\nHAVING " + query_having(); }
  718. if (query_orderby() !== "") { q_select += "\nORDER BY " + query_orderby(); }
  719. var box = document.getElementById('box');
  720. document.getElementById('filter').style.display = 'block';
  721. var btitle = document.getElementById('boxtitle');
  722. btitle.innerHTML = 'SELECT';//formtitle;
  723. if (fadin) {
  724. gradient("box", 0);
  725. fadein("box");
  726. } else {
  727. box.style.display = 'block';
  728. }
  729. document.getElementById('textSqlquery').innerHTML = q_select;
  730. }
  731. function closebox()
  732. {
  733. document.getElementById('box').style.display = 'none';
  734. document.getElementById('filter').style.display = 'none';
  735. }