db_multi_table_query.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview function used in QBE for DB
  4. * @name Database Operations
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @requires js/functions.js
  9. * @requires js/db_query_generator.js
  10. *
  11. */
  12. /**
  13. * js file for handling AJAX and other events in db_multi_table_query.php
  14. */
  15. /**
  16. * Unbind all event handlers before tearing down a page
  17. */
  18. AJAX.registerTeardown('db_multi_table_query.js', function () {
  19. $('.tableNameSelect').each(function () {
  20. $(this).off('change');
  21. });
  22. $('#update_query_button').off('click');
  23. $('#add_column_button').off('click');
  24. });
  25. AJAX.registerOnload('db_multi_table_query.js', function () {
  26. var editor = PMA_getSQLEditor($('#MultiSqlquery'), {}, 'both');
  27. $('.CodeMirror-line').css('text-align', 'left');
  28. editor.setSize(-1, 50);
  29. var column_count = 3;
  30. PMA_init_slider();
  31. addNewColumnCallbacks();
  32. $('#update_query_button').on('click', function () {
  33. var columns = [];
  34. var tableAliases = {};
  35. $('.tableNameSelect').each(function () {
  36. $show = $(this).siblings('.show_col').first();
  37. if ($(this).val() !== '' && $show.prop('checked')) {
  38. var tableAlias = $(this).siblings('.table_alias').first().val();
  39. var columnAlias = $(this).siblings('.col_alias').first().val();
  40. if (tableAlias !== '') {
  41. columns.push([tableAlias, $(this).siblings('.columnNameSelect').first().val()]);
  42. } else {
  43. columns.push([$(this).val(), $(this).siblings('.columnNameSelect').first().val()]);
  44. }
  45. columns[columns.length - 1].push(columnAlias);
  46. if ($(this).val() in tableAliases) {
  47. if (!(tableAliases[$(this).val()].includes(tableAlias))) {
  48. tableAliases[$(this).val()].push(tableAlias);
  49. }
  50. } else {
  51. tableAliases[$(this).val()] = [tableAlias];
  52. }
  53. }
  54. });
  55. if (Object.keys(tableAliases).length === 0) {
  56. PMA_ajaxShowMessage('Nothing selected', false, 'error');
  57. return;
  58. }
  59. var foreignKeys;
  60. $.ajax({
  61. type: 'GET',
  62. async: false,
  63. url: 'db_multi_table_query.php',
  64. data: {
  65. 'server': sessionStorage.server,
  66. 'db': $('#db_name').val(),
  67. 'tables': Object.keys(tableAliases),
  68. 'ajax_request': '1',
  69. 'token': PMA_commonParams.get('token')
  70. },
  71. success: function (response) {
  72. foreignKeys = response.foreignKeyConstrains;
  73. }
  74. });
  75. query = 'SELECT ' + '`' + escapeBacktick(columns[0][0]) + '`.';
  76. if (columns[0][1] === '*') {
  77. query += '*';
  78. } else {
  79. query += '`' + escapeBacktick(columns[0][1]) + '`';
  80. }
  81. if (columns[0][2] !== '') {
  82. query += ' AS ' + columns[0][2];
  83. }
  84. for (var i = 1; i < columns.length; i++) {
  85. query += ', `' + escapeBacktick(columns[i][0]) + '`.';
  86. if (columns[i][1] === '*') {
  87. query += '*';
  88. } else {
  89. query += '`' + escapeBacktick(columns[i][1]) + '`';
  90. }
  91. if (columns[i][2] !== '') {
  92. query += ' AS `' + escapeBacktick(columns[0][2]) + '`';
  93. }
  94. }
  95. query += '\nFROM ';
  96. query += generateFromBlock(tableAliases, foreignKeys);
  97. $criteria_col_count = $('.criteria_col:checked').length;
  98. if ($criteria_col_count > 0) {
  99. query += '\nWHERE ';
  100. query += generateWhereBlock();
  101. }
  102. query += ';';
  103. editor.getDoc().setValue(query);
  104. });
  105. $('#submit_query').on('click', function () {
  106. var query = editor.getDoc().getValue();
  107. // Verifying that the query is not empty
  108. if (query === '') {
  109. PMA_ajaxShowMessage(PMA_messages.strEmptyQuery, false, 'error');
  110. return;
  111. }
  112. var data = {
  113. 'db': $('#db_name').val(),
  114. 'sql_query': query,
  115. 'ajax_request': '1',
  116. 'token': PMA_commonParams.get('token')
  117. };
  118. $.ajax({
  119. type: 'POST',
  120. url: 'db_multi_table_query.php',
  121. data: data,
  122. success: function (data) {
  123. $results_dom = $(data.message);
  124. $results_dom.find('.ajax:not(.pageselector)').each(function () {
  125. $(this).on('click', function (event) {
  126. event.preventDefault();
  127. });
  128. });
  129. $results_dom.find('.autosubmit, .pageselector, .showAllRows, .filter_rows').each(function () {
  130. $(this).on('change click select focus', function (event) {
  131. event.preventDefault();
  132. });
  133. });
  134. $('#sql_results').html($results_dom);
  135. $('#page_content').find('a').first().click();
  136. }
  137. });
  138. });
  139. $('#add_column_button').on('click', function () {
  140. column_count++;
  141. $new_column_dom = $($('#new_column_layout').html()).clone();
  142. $new_column_dom.find('div').first().find('div').first().attr('id', column_count.toString());
  143. $new_column_dom.find('a').first().remove();
  144. $new_column_dom.find('.pma_auto_slider').first().unwrap();
  145. $new_column_dom.find('.pma_auto_slider').first().attr('title', 'criteria');
  146. $('#add_column_button').parent().before($new_column_dom);
  147. PMA_init_slider();
  148. addNewColumnCallbacks();
  149. });
  150. function addNewColumnCallbacks () {
  151. $('.tableNameSelect').each(function () {
  152. $(this).on('change', function () {
  153. $sibs = $(this).siblings('.columnNameSelect');
  154. if ($sibs.length === 0) {
  155. $sibs = $(this).parent().parent().find('.columnNameSelect');
  156. }
  157. $sibs.first().html($('#' + $.md5($(this).val())).html());
  158. });
  159. });
  160. $('.removeColumn').each(function () {
  161. $(this).on('click', function () {
  162. $(this).parent().remove();
  163. });
  164. });
  165. $('a.ajax').each(function () {
  166. $(this).on('click', function (event, from) {
  167. if (from === null) {
  168. $checkbox = $(this).siblings('.criteria_col').first();
  169. $checkbox.prop('checked', !$checkbox.prop('checked'));
  170. }
  171. $criteria_col_count = $('.criteria_col:checked').length;
  172. if ($criteria_col_count > 1) {
  173. $(this).siblings('.slide-wrapper').first().find('.logical_operator').first().css('display','table-row');
  174. }
  175. });
  176. });
  177. $('.criteria_col').each(function () {
  178. $(this).on('change', function () {
  179. $anchor = $(this).siblings('a.ajax').first();
  180. $anchor.trigger('click', ['Trigger']);
  181. });
  182. });
  183. $('.criteria_rhs').each(function () {
  184. $(this).on('change', function () {
  185. $rhs_col = $(this).parent().parent().siblings('.rhs_table').first();
  186. $rhs_text = $(this).parent().parent().siblings('.rhs_text').first();
  187. if ($(this).val() === 'text') {
  188. $rhs_col.css('display', 'none');
  189. $rhs_text.css('display', 'table-row');
  190. } else if ($(this).val() === 'anotherColumn') {
  191. $rhs_text.css('display', 'none');
  192. $rhs_col.css('display', 'table-row');
  193. } else {
  194. $rhs_text.css('display', 'none');
  195. $rhs_col.css('display', 'none');
  196. }
  197. });
  198. });
  199. }
  200. });