multi_table_query.js 7.1 KB

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