sql.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. "use strict";
  2. /**
  3. * @fileoverview functions used wherever an sql query form is used
  4. *
  5. * @requires jQuery
  6. * @requires js/functions.js
  7. *
  8. * @test-module Sql
  9. */
  10. /* global Stickyfill */
  11. /* global isStorageSupported */
  12. // js/config.js
  13. /* global codeMirrorEditor */
  14. // js/functions.js
  15. /* global MicroHistory */
  16. // js/microhistory.js
  17. /* global makeGrid */
  18. // js/makegrid.js
  19. var Sql = {};
  20. /**
  21. * decode a string URL_encoded
  22. *
  23. * @param string str
  24. * @return string the URL-decoded string
  25. */
  26. Sql.urlDecode = function (str) {
  27. if (typeof str !== 'undefined') {
  28. return decodeURIComponent(str.replace(/\+/g, '%20'));
  29. }
  30. };
  31. /**
  32. * encode a string URL_decoded
  33. *
  34. * @param string str
  35. * @return string the URL-encoded string
  36. */
  37. Sql.urlEncode = function (str) {
  38. if (typeof str !== 'undefined') {
  39. return encodeURIComponent(str).replace(/%20/g, '+');
  40. }
  41. };
  42. /**
  43. * Saves SQL query in local storage or cookie
  44. *
  45. * @param string SQL query
  46. * @return void
  47. */
  48. Sql.autoSave = function (query) {
  49. if (query) {
  50. var key = Sql.getAutoSavedKey();
  51. if (isStorageSupported('localStorage')) {
  52. window.localStorage.setItem(key, query);
  53. } else {
  54. Cookies.set(key, query);
  55. }
  56. }
  57. };
  58. /**
  59. * Saves SQL query in local storage or cookie
  60. *
  61. * @param string database name
  62. * @param string table name
  63. * @param string SQL query
  64. * @return void
  65. */
  66. Sql.showThisQuery = function (db, table, query) {
  67. var showThisQueryObject = {
  68. 'db': db,
  69. 'table': table,
  70. 'query': query
  71. };
  72. if (isStorageSupported('localStorage')) {
  73. window.localStorage.showThisQuery = 1;
  74. window.localStorage.showThisQueryObject = JSON.stringify(showThisQueryObject);
  75. } else {
  76. Cookies.set('showThisQuery', 1);
  77. Cookies.set('showThisQueryObject', JSON.stringify(showThisQueryObject));
  78. }
  79. };
  80. /**
  81. * Set query to codemirror if show this query is
  82. * checked and query for the db and table pair exists
  83. */
  84. Sql.setShowThisQuery = function () {
  85. var db = $('input[name="db"]').val();
  86. var table = $('input[name="table"]').val();
  87. if (isStorageSupported('localStorage')) {
  88. if (window.localStorage.showThisQueryObject !== undefined) {
  89. var storedDb = JSON.parse(window.localStorage.showThisQueryObject).db;
  90. var storedTable = JSON.parse(window.localStorage.showThisQueryObject).table;
  91. var storedQuery = JSON.parse(window.localStorage.showThisQueryObject).query;
  92. }
  93. if (window.localStorage.showThisQuery !== undefined && window.localStorage.showThisQuery === '1') {
  94. $('input[name="show_query"]').prop('checked', true);
  95. if (db === storedDb && table === storedTable) {
  96. if (codeMirrorEditor) {
  97. codeMirrorEditor.setValue(storedQuery);
  98. } else if (document.sqlform) {
  99. document.sqlform.sql_query.value = storedQuery;
  100. }
  101. }
  102. } else {
  103. $('input[name="show_query"]').prop('checked', false);
  104. }
  105. }
  106. };
  107. /**
  108. * Saves SQL query with sort in local storage or cookie
  109. *
  110. * @param {String} query SQL query
  111. * @return void
  112. */
  113. Sql.autoSaveWithSort = function (query) {
  114. if (query) {
  115. if (isStorageSupported('localStorage')) {
  116. window.localStorage.setItem('autoSavedSqlSort', query);
  117. } else {
  118. Cookies.set('autoSavedSqlSort', query);
  119. }
  120. }
  121. };
  122. /**
  123. * Clear saved SQL query with sort in local storage or cookie
  124. *
  125. * @return void
  126. */
  127. Sql.clearAutoSavedSort = function () {
  128. if (isStorageSupported('localStorage')) {
  129. window.localStorage.removeItem('autoSavedSqlSort');
  130. } else {
  131. Cookies.set('autoSavedSqlSort', '');
  132. }
  133. };
  134. /**
  135. * Get the field name for the current field. Required to construct the query
  136. * for grid editing
  137. *
  138. * @param $tableResults enclosing results table
  139. * @param $thisField jQuery object that points to the current field's tr
  140. */
  141. Sql.getFieldName = function ($tableResults, $thisField) {
  142. var thisFieldIndex = $thisField.index(); // ltr or rtl direction does not impact how the DOM was generated
  143. // check if the action column in the left exist
  144. var leftActionExist = !$tableResults.find('th').first().hasClass('draggable'); // number of column span for checkbox and Actions
  145. var leftActionSkip = leftActionExist ? $tableResults.find('th').first().attr('colspan') - 1 : 0; // If this column was sorted, the text of the a element contains something
  146. // like <small>1</small> that is useful to indicate the order in case
  147. // of a sort on multiple columns; however, we dont want this as part
  148. // of the column name so we strip it ( .clone() to .end() )
  149. var fieldName = $tableResults.find('thead').find('th').eq(thisFieldIndex - leftActionSkip).find('a').clone() // clone the element
  150. .children() // select all the children
  151. .remove() // remove all of them
  152. .end() // go back to the selected element
  153. .text(); // grab the text
  154. // happens when just one row (headings contain no a)
  155. if (fieldName === '') {
  156. var $heading = $tableResults.find('thead').find('th').eq(thisFieldIndex - leftActionSkip).children('span'); // may contain column comment enclosed in a span - detach it temporarily to read the column name
  157. var $tempColComment = $heading.children().detach();
  158. fieldName = $heading.text(); // re-attach the column comment
  159. $heading.append($tempColComment);
  160. }
  161. fieldName = fieldName.trim();
  162. return fieldName;
  163. };
  164. /**
  165. * Unbind all event handlers before tearing down a page
  166. */
  167. AJAX.registerTeardown('sql.js', function () {
  168. $(document).off('click', 'a.delete_row.ajax');
  169. $(document).off('submit', '.bookmarkQueryForm');
  170. $('input#bkm_label').off('input');
  171. $(document).off('makegrid', '.sqlqueryresults');
  172. $('#togglequerybox').off('click');
  173. $(document).off('click', '#button_submit_query');
  174. $(document).off('change', '#id_bookmark');
  175. $('input[name=\'bookmark_variable\']').off('keypress');
  176. $(document).off('submit', '#sqlqueryform.ajax');
  177. $(document).off('click', 'input[name=navig].ajax');
  178. $(document).off('submit', 'form[name=\'displayOptionsForm\'].ajax');
  179. $(document).off('mouseenter', 'th.column_heading.pointer');
  180. $(document).off('mouseleave', 'th.column_heading.pointer');
  181. $(document).off('click', 'th.column_heading.marker');
  182. $(document).off('scroll', window);
  183. $(document).off('keyup', '.filter_rows');
  184. $(document).off('click', '#printView');
  185. if (codeMirrorEditor) {
  186. codeMirrorEditor.off('change');
  187. } else {
  188. $('#sqlquery').off('input propertychange');
  189. }
  190. $('body').off('click', '.navigation .showAllRows');
  191. $('body').off('click', 'a.browse_foreign');
  192. $('body').off('click', '#simulate_dml');
  193. $('body').off('keyup', '#sqlqueryform');
  194. $('body').off('click', 'form[name="resultsForm"].ajax button[name="submit_mult"], form[name="resultsForm"].ajax input[name="submit_mult"]');
  195. $(document).off('submit', '#maxRowsForm');
  196. });
  197. /**
  198. * @description <p>Ajax scripts for sql and browse pages</p>
  199. *
  200. * Actions ajaxified here:
  201. * <ul>
  202. * <li>Retrieve results of an SQL query</li>
  203. * <li>Paginate the results table</li>
  204. * <li>Sort the results table</li>
  205. * <li>Change table according to display options</li>
  206. * <li>Grid editing of data</li>
  207. * <li>Saving a bookmark</li>
  208. * </ul>
  209. *
  210. * @name document.ready
  211. * @memberOf jQuery
  212. */
  213. AJAX.registerOnload('sql.js', function () {
  214. if (codeMirrorEditor || document.sqlform) {
  215. Sql.setShowThisQuery();
  216. }
  217. $(function () {
  218. if (codeMirrorEditor) {
  219. codeMirrorEditor.on('change', function () {
  220. Sql.autoSave(codeMirrorEditor.getValue());
  221. });
  222. } else {
  223. $('#sqlquery').on('input propertychange', function () {
  224. Sql.autoSave($('#sqlquery').val());
  225. });
  226. var useLocalStorageValue = isStorageSupported('localStorage') && typeof window.localStorage.autoSavedSqlSort !== 'undefined'; // Save sql query with sort
  227. if ($('#RememberSorting') !== undefined && $('#RememberSorting').is(':checked')) {
  228. $('select[name="sql_query"]').on('change', function () {
  229. Sql.autoSaveWithSort($(this).val());
  230. });
  231. $('.sortlink').on('click', function () {
  232. Sql.clearAutoSavedSort();
  233. });
  234. } else {
  235. Sql.clearAutoSavedSort();
  236. } // If sql query with sort for current table is stored, change sort by key select value
  237. var sortStoredQuery = useLocalStorageValue ? window.localStorage.autoSavedSqlSort : Cookies.get('autoSavedSqlSort');
  238. if (typeof sortStoredQuery !== 'undefined' && sortStoredQuery !== $('select[name="sql_query"]').val() && $('select[name="sql_query"] option[value="' + sortStoredQuery + '"]').length !== 0) {
  239. $('select[name="sql_query"]').val(sortStoredQuery).trigger('change');
  240. }
  241. }
  242. }); // Delete row from SQL results
  243. $(document).on('click', 'a.delete_row.ajax', function (e) {
  244. e.preventDefault();
  245. var question = Functions.sprintf(Messages.strDoYouReally, Functions.escapeHtml($(this).closest('td').find('div').text()));
  246. var $link = $(this);
  247. $link.confirm(question, $link.attr('href'), function (url) {
  248. Functions.ajaxShowMessage();
  249. var argsep = CommonParams.get('arg_separator');
  250. var params = 'ajax_request=1' + argsep + 'is_js_confirmed=1';
  251. var postData = $link.getPostData();
  252. if (postData) {
  253. params += argsep + postData;
  254. }
  255. $.post(url, params, function (data) {
  256. if (data.success) {
  257. Functions.ajaxShowMessage(data.message);
  258. $link.closest('tr').remove();
  259. } else {
  260. Functions.ajaxShowMessage(data.error, false);
  261. }
  262. });
  263. });
  264. }); // Ajaxification for 'Bookmark this SQL query'
  265. $(document).on('submit', '.bookmarkQueryForm', function (e) {
  266. e.preventDefault();
  267. Functions.ajaxShowMessage();
  268. var argsep = CommonParams.get('arg_separator');
  269. $.post($(this).attr('action'), 'ajax_request=1' + argsep + $(this).serialize(), function (data) {
  270. if (data.success) {
  271. Functions.ajaxShowMessage(data.message);
  272. } else {
  273. Functions.ajaxShowMessage(data.error, false);
  274. }
  275. });
  276. });
  277. /* Hides the bookmarkoptions checkboxes when the bookmark label is empty */
  278. $('input#bkm_label').on('input', function () {
  279. $('input#id_bkm_all_users, input#id_bkm_replace').parent().toggle($(this).val().length > 0);
  280. }).trigger('input');
  281. /**
  282. * Attach Event Handler for 'Copy to clipboard'
  283. */
  284. $(document).on('click', '#copyToClipBoard', function (event) {
  285. event.preventDefault();
  286. var textArea = document.createElement('textarea'); //
  287. // *** This styling is an extra step which is likely not required. ***
  288. //
  289. // Why is it here? To ensure:
  290. // 1. the element is able to have focus and selection.
  291. // 2. if element was to flash render it has minimal visual impact.
  292. // 3. less flakyness with selection and copying which **might** occur if
  293. // the textarea element is not visible.
  294. //
  295. // The likelihood is the element won't even render, not even a flash,
  296. // so some of these are just precautions. However in IE the element
  297. // is visible whilst the popup box asking the user for permission for
  298. // the web page to copy to the clipboard.
  299. //
  300. // Place in top-left corner of screen regardless of scroll position.
  301. textArea.style.position = 'fixed';
  302. textArea.style.top = 0;
  303. textArea.style.left = 0; // Ensure it has a small width and height. Setting to 1px / 1em
  304. // doesn't work as this gives a negative w/h on some browsers.
  305. textArea.style.width = '2em';
  306. textArea.style.height = '2em'; // We don't need padding, reducing the size if it does flash render.
  307. textArea.style.padding = 0; // Clean up any borders.
  308. textArea.style.border = 'none';
  309. textArea.style.outline = 'none';
  310. textArea.style.boxShadow = 'none'; // Avoid flash of white box if rendered for any reason.
  311. textArea.style.background = 'transparent';
  312. textArea.value = '';
  313. $('#server-breadcrumb a').each(function () {
  314. textArea.value += $(this).data('raw-text') + '/';
  315. });
  316. textArea.value += '\t\t' + window.location.href;
  317. textArea.value += '\n';
  318. $('.alert-success').each(function () {
  319. textArea.value += $(this).text() + '\n\n';
  320. });
  321. $('.sql pre').each(function () {
  322. textArea.value += $(this).text() + '\n\n';
  323. });
  324. $('.table_results .column_heading a').each(function () {
  325. // Don't copy ordering number text within <small> tag
  326. textArea.value += $(this).clone().find('small').remove().end().text() + '\t';
  327. });
  328. textArea.value += '\n';
  329. $('.table_results tbody tr').each(function () {
  330. $(this).find('.data span').each(function () {
  331. textArea.value += $(this).text() + '\t';
  332. });
  333. textArea.value += '\n';
  334. });
  335. document.body.appendChild(textArea);
  336. textArea.select();
  337. try {
  338. document.execCommand('copy');
  339. } catch (err) {
  340. alert('Sorry! Unable to copy');
  341. }
  342. document.body.removeChild(textArea);
  343. }); // end of Copy to Clipboard action
  344. /**
  345. * Attach Event Handler for 'Print' link
  346. */
  347. $(document).on('click', '#printView', function (event) {
  348. event.preventDefault(); // Take to preview mode
  349. Functions.printPreview();
  350. }); // end of 'Print' action
  351. /**
  352. * Attach the {@link makegrid} function to a custom event, which will be
  353. * triggered manually everytime the table of results is reloaded
  354. * @memberOf jQuery
  355. */
  356. $(document).on('makegrid', '.sqlqueryresults', function () {
  357. $('.table_results').each(function () {
  358. makeGrid(this);
  359. });
  360. });
  361. /**
  362. * Append the "Show/Hide query box" message to the query input form
  363. *
  364. * @memberOf jQuery
  365. * @name appendToggleSpan
  366. */
  367. // do not add this link more than once
  368. if (!$('#sqlqueryform').find('button').is('#togglequerybox')) {
  369. $('<button class="btn btn-secondary" id="togglequerybox"></button>').html(Messages.strHideQueryBox).appendTo('#sqlqueryform') // initially hidden because at this point, nothing else
  370. // appears under the link
  371. .hide(); // Attach the toggling of the query box visibility to a click
  372. $('#togglequerybox').on('click', function () {
  373. var $link = $(this);
  374. $link.siblings().slideToggle('fast');
  375. if ($link.text() === Messages.strHideQueryBox) {
  376. $link.text(Messages.strShowQueryBox); // cheap trick to add a spacer between the menu tabs
  377. // and "Show query box"; feel free to improve!
  378. $('#togglequerybox_spacer').remove();
  379. $link.before('<br id="togglequerybox_spacer">');
  380. } else {
  381. $link.text(Messages.strHideQueryBox);
  382. } // avoid default click action
  383. return false;
  384. });
  385. }
  386. /**
  387. * Event handler for sqlqueryform.ajax button_submit_query
  388. *
  389. * @memberOf jQuery
  390. */
  391. $(document).on('click', '#button_submit_query', function () {
  392. $('.alert-success,.alert-danger').hide(); // hide already existing error or success message
  393. var $form = $(this).closest('form'); // the Go button related to query submission was clicked,
  394. // instead of the one related to Bookmarks, so empty the
  395. // id_bookmark selector to avoid misinterpretation in
  396. // /import about what needs to be done
  397. $form.find('select[name=id_bookmark]').val('');
  398. var isShowQuery = $('input[name="show_query"]').is(':checked');
  399. if (isShowQuery) {
  400. window.localStorage.showThisQuery = '1';
  401. var db = $('input[name="db"]').val();
  402. var table = $('input[name="table"]').val();
  403. var query;
  404. if (codeMirrorEditor) {
  405. query = codeMirrorEditor.getValue();
  406. } else {
  407. query = $('#sqlquery').val();
  408. }
  409. Sql.showThisQuery(db, table, query);
  410. } else {
  411. window.localStorage.showThisQuery = '0';
  412. }
  413. });
  414. /**
  415. * Event handler to show appropriate number of variable boxes
  416. * based on the bookmarked query
  417. */
  418. $(document).on('change', '#id_bookmark', function () {
  419. var varCount = $(this).find('option:selected').data('varcount');
  420. if (typeof varCount === 'undefined') {
  421. varCount = 0;
  422. }
  423. var $varDiv = $('#bookmarkVariables');
  424. $varDiv.empty();
  425. for (var i = 1; i <= varCount; i++) {
  426. $varDiv.append($('<div class="form-group">'));
  427. $varDiv.append($('<label for="bookmarkVariable' + i + '">' + Functions.sprintf(Messages.strBookmarkVariable, i) + '</label>'));
  428. $varDiv.append($('<input class="form-control" type="text" size="10" name="bookmark_variable[' + i + ']" id="bookmarkVariable' + i + '">'));
  429. $varDiv.append($('</div>'));
  430. }
  431. if (varCount === 0) {
  432. $varDiv.parent().hide();
  433. } else {
  434. $varDiv.parent().show();
  435. }
  436. });
  437. /**
  438. * Event handler for hitting enter on sqlqueryform bookmark_variable
  439. * (the Variable textfield in Bookmarked SQL query section)
  440. *
  441. * @memberOf jQuery
  442. */
  443. $('input[name=bookmark_variable]').on('keypress', function (event) {
  444. // force the 'Enter Key' to implicitly click the #button_submit_bookmark
  445. var keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  446. if (keycode === 13) {
  447. // keycode for enter key
  448. // When you press enter in the sqlqueryform, which
  449. // has 2 submit buttons, the default is to run the
  450. // #button_submit_query, because of the tabindex
  451. // attribute.
  452. // This submits #button_submit_bookmark instead,
  453. // because when you are in the Bookmarked SQL query
  454. // section and hit enter, you expect it to do the
  455. // same action as the Go button in that section.
  456. $('#button_submit_bookmark').trigger('click');
  457. return false;
  458. } else {
  459. return true;
  460. }
  461. });
  462. /**
  463. * Ajax Event handler for 'SQL Query Submit'
  464. *
  465. * @see Functions.ajaxShowMessage()
  466. * @memberOf jQuery
  467. * @name sqlqueryform_submit
  468. */
  469. $(document).on('submit', '#sqlqueryform.ajax', function (event) {
  470. event.preventDefault();
  471. var $form = $(this);
  472. if (codeMirrorEditor) {
  473. $form[0].elements.sql_query.value = codeMirrorEditor.getValue();
  474. }
  475. if (!Functions.checkSqlQuery($form[0])) {
  476. return false;
  477. } // remove any div containing a previous error message
  478. $('.alert-danger').remove();
  479. var $msgbox = Functions.ajaxShowMessage();
  480. var $sqlqueryresultsouter = $('#sqlqueryresultsouter');
  481. Functions.prepareForAjaxRequest($form);
  482. var argsep = CommonParams.get('arg_separator');
  483. $.post($form.attr('action'), $form.serialize() + argsep + 'ajax_page_request=true', function (data) {
  484. if (typeof data !== 'undefined' && data.success === true) {
  485. // success happens if the query returns rows or not
  486. // show a message that stays on screen
  487. if (typeof data.action_bookmark !== 'undefined') {
  488. // view only
  489. if ('1' === data.action_bookmark) {
  490. $('#sqlquery').text(data.sql_query); // send to codemirror if possible
  491. Functions.setQuery(data.sql_query);
  492. } // delete
  493. if ('2' === data.action_bookmark) {
  494. $('#id_bookmark option[value=\'' + data.id_bookmark + '\']').remove(); // if there are no bookmarked queries now (only the empty option),
  495. // remove the bookmark section
  496. if ($('#id_bookmark option').length === 1) {
  497. $('#fieldsetBookmarkOptions').hide();
  498. $('#fieldsetBookmarkOptionsFooter').hide();
  499. }
  500. }
  501. }
  502. $sqlqueryresultsouter.show().html(data.message);
  503. Functions.highlightSql($sqlqueryresultsouter);
  504. if (data.menu) {
  505. if (history && history.pushState) {
  506. history.replaceState({
  507. menu: data.menu
  508. }, null);
  509. AJAX.handleMenu.replace(data.menu);
  510. } else {
  511. MicroHistory.menus.replace(data.menu);
  512. MicroHistory.menus.add(data.menuHash, data.menu);
  513. }
  514. } else if (data.menuHash) {
  515. if (!(history && history.pushState)) {
  516. MicroHistory.menus.replace(MicroHistory.menus.get(data.menuHash));
  517. }
  518. }
  519. if (data.params) {
  520. CommonParams.setAll(data.params);
  521. }
  522. if (typeof data.ajax_reload !== 'undefined') {
  523. if (data.ajax_reload.reload) {
  524. if (data.ajax_reload.table_name) {
  525. CommonParams.set('table', data.ajax_reload.table_name);
  526. CommonActions.refreshMain();
  527. } else {
  528. Navigation.reload();
  529. }
  530. }
  531. } else if (typeof data.reload !== 'undefined') {
  532. // this happens if a USE or DROP command was typed
  533. CommonActions.setDb(data.db);
  534. var url;
  535. if (data.db) {
  536. if (data.table) {
  537. url = 'index.php?route=/table/sql';
  538. } else {
  539. url = 'index.php?route=/database/sql';
  540. }
  541. } else {
  542. url = 'index.php?route=/server/sql';
  543. }
  544. CommonActions.refreshMain(url, function () {
  545. $('#sqlqueryresultsouter').show().html(data.message);
  546. Functions.highlightSql($('#sqlqueryresultsouter'));
  547. });
  548. }
  549. $('.sqlqueryresults').trigger('makegrid');
  550. $('#togglequerybox').show();
  551. Functions.initSlider();
  552. if (typeof data.action_bookmark === 'undefined') {
  553. if ($('#sqlqueryform input[name="retain_query_box"]').is(':checked') !== true) {
  554. if ($('#togglequerybox').siblings(':visible').length > 0) {
  555. $('#togglequerybox').trigger('click');
  556. }
  557. }
  558. }
  559. } else if (typeof data !== 'undefined' && data.success === false) {
  560. // show an error message that stays on screen
  561. $sqlqueryresultsouter.show().html(data.error);
  562. $('html, body').animate({
  563. scrollTop: $(document).height()
  564. }, 200);
  565. }
  566. Functions.ajaxRemoveMessage($msgbox);
  567. }); // end $.post()
  568. }); // end SQL Query submit
  569. /**
  570. * Ajax Event handler for the display options
  571. * @memberOf jQuery
  572. * @name displayOptionsForm_submit
  573. */
  574. $(document).on('submit', 'form[name=\'displayOptionsForm\'].ajax', function (event) {
  575. event.preventDefault();
  576. var $form = $(this);
  577. var $msgbox = Functions.ajaxShowMessage();
  578. var argsep = CommonParams.get('arg_separator');
  579. $.post($form.attr('action'), $form.serialize() + argsep + 'ajax_request=true', function (data) {
  580. Functions.ajaxRemoveMessage($msgbox);
  581. var $sqlqueryresults = $form.parents('.sqlqueryresults');
  582. $sqlqueryresults.html(data.message).trigger('makegrid');
  583. Functions.initSlider();
  584. Functions.highlightSql($sqlqueryresults);
  585. }); // end $.post()
  586. }); // end displayOptionsForm handler
  587. // Filter row handling. --STARTS--
  588. $(document).on('keyup', '.filter_rows', function () {
  589. var uniqueId = $(this).data('for');
  590. var $targetTable = $('.table_results[data-uniqueId=\'' + uniqueId + '\']');
  591. var $headerCells = $targetTable.find('th[data-column]');
  592. var targetColumns = []; // To handle colspan=4, in case of edit,copy etc options.
  593. var dummyTh = $('.edit_row_anchor').length !== 0 ? '<th class="hide dummy_th"></th><th class="hide dummy_th"></th><th class="hide dummy_th"></th>' : ''; // Selecting columns that will be considered for filtering and searching.
  594. $headerCells.each(function () {
  595. targetColumns.push($(this).text().trim());
  596. });
  597. var phrase = $(this).val(); // Set same value to both Filter rows fields.
  598. $('.filter_rows[data-for=\'' + uniqueId + '\']').not(this).val(phrase); // Handle colspan.
  599. $targetTable.find('thead > tr').prepend(dummyTh);
  600. $.uiTableFilter($targetTable, phrase, targetColumns);
  601. $targetTable.find('th.dummy_th').remove();
  602. }); // Filter row handling. --ENDS--
  603. // Prompt to confirm on Show All
  604. $('body').on('click', '.navigation .showAllRows', function (e) {
  605. e.preventDefault();
  606. var $form = $(this).parents('form');
  607. Sql.submitShowAllForm = function () {
  608. var argsep = CommonParams.get('arg_separator');
  609. var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  610. Functions.ajaxShowMessage();
  611. AJAX.source = $form;
  612. $.post($form.attr('action'), submitData, AJAX.responseHandler);
  613. };
  614. if (!$(this).is(':checked')) {
  615. // already showing all rows
  616. Sql.submitShowAllForm();
  617. } else {
  618. $form.confirm(Messages.strShowAllRowsWarning, $form.attr('action'), function () {
  619. Sql.submitShowAllForm();
  620. });
  621. }
  622. });
  623. $('body').on('keyup', '#sqlqueryform', function () {
  624. Functions.handleSimulateQueryButton();
  625. });
  626. /**
  627. * Ajax event handler for 'Simulate DML'.
  628. */
  629. $('body').on('click', '#simulate_dml', function () {
  630. var $form = $('#sqlqueryform');
  631. var query = '';
  632. var delimiter = $('#id_sql_delimiter').val();
  633. var dbName = $form.find('input[name="db"]').val();
  634. if (codeMirrorEditor) {
  635. query = codeMirrorEditor.getValue();
  636. } else {
  637. query = $('#sqlquery').val();
  638. }
  639. if (query.length === 0) {
  640. alert(Messages.strFormEmpty);
  641. $('#sqlquery').trigger('focus');
  642. return false;
  643. }
  644. var $msgbox = Functions.ajaxShowMessage();
  645. $.ajax({
  646. type: 'POST',
  647. url: $form.attr('action'),
  648. data: {
  649. 'server': CommonParams.get('server'),
  650. 'db': dbName,
  651. 'ajax_request': '1',
  652. 'simulate_dml': '1',
  653. 'sql_query': query,
  654. 'sql_delimiter': delimiter
  655. },
  656. success: function success(response) {
  657. Functions.ajaxRemoveMessage($msgbox);
  658. if (response.success) {
  659. var dialogContent = '<div class="preview_sql">';
  660. if (response.sql_data) {
  661. var len = response.sql_data.length;
  662. for (var i = 0; i < len; i++) {
  663. dialogContent += '<strong>' + Messages.strSQLQuery + '</strong>' + response.sql_data[i].sql_query + Messages.strMatchedRows + ' <a href="' + response.sql_data[i].matched_rows_url + '">' + response.sql_data[i].matched_rows + '</a><br>';
  664. if (i < len - 1) {
  665. dialogContent += '<hr>';
  666. }
  667. }
  668. } else {
  669. dialogContent += response.message;
  670. }
  671. dialogContent += '</div>';
  672. var $dialogContent = $(dialogContent);
  673. var buttonOptions = {};
  674. buttonOptions[Messages.strClose] = function () {
  675. $(this).dialog('close');
  676. };
  677. $('<div></div>').append($dialogContent).dialog({
  678. minWidth: 540,
  679. maxHeight: 400,
  680. modal: true,
  681. buttons: buttonOptions,
  682. title: Messages.strSimulateDML,
  683. open: function open() {
  684. Functions.highlightSql($(this));
  685. },
  686. close: function close() {
  687. $(this).remove();
  688. }
  689. });
  690. } else {
  691. Functions.ajaxShowMessage(response.error);
  692. }
  693. },
  694. error: function error() {
  695. Functions.ajaxShowMessage(Messages.strErrorProcessingRequest);
  696. }
  697. });
  698. });
  699. /**
  700. * Handles multi submits of results browsing page such as edit, delete and export
  701. */
  702. $('body').on('click', 'form[name="resultsForm"].ajax button[name="submit_mult"], form[name="resultsForm"].ajax input[name="submit_mult"]', function (e) {
  703. e.preventDefault();
  704. var $button = $(this);
  705. var action = $button.val();
  706. var $form = $button.closest('form');
  707. var argsep = CommonParams.get('arg_separator');
  708. var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true' + argsep;
  709. Functions.ajaxShowMessage();
  710. AJAX.source = $form;
  711. var url;
  712. if (action === 'edit') {
  713. submitData = submitData + argsep + 'default_action=update';
  714. url = 'index.php?route=/table/change/rows';
  715. } else if (action === 'copy') {
  716. submitData = submitData + argsep + 'default_action=insert';
  717. url = 'index.php?route=/table/change/rows';
  718. } else if (action === 'export') {
  719. url = 'index.php?route=/table/export/rows';
  720. } else if (action === 'delete') {
  721. url = 'index.php?route=/table/delete/confirm';
  722. } else {
  723. return;
  724. }
  725. $.post(url, submitData, AJAX.responseHandler);
  726. });
  727. $(document).on('submit', '#maxRowsForm', function () {
  728. var unlimNumRows = $(this).find('input[name="unlim_num_rows"]').val();
  729. var maxRowsCheck = Functions.checkFormElementInRange(this, 'session_max_rows', Messages.strNotValidRowNumber, 1);
  730. var posCheck = Functions.checkFormElementInRange(this, 'pos', Messages.strNotValidRowNumber, 0, unlimNumRows > 0 ? unlimNumRows - 1 : null);
  731. return maxRowsCheck && posCheck;
  732. });
  733. $('#insertBtn').on('click', function () {
  734. Functions.insertValueQuery();
  735. });
  736. }); // end $()
  737. /**
  738. * Starting from some th, change the class of all td under it.
  739. * If isAddClass is specified, it will be used to determine whether to add or remove the class.
  740. */
  741. Sql.changeClassForColumn = function ($thisTh, newClass, isAddClass) {
  742. // index 0 is the th containing the big T
  743. var thIndex = $thisTh.index();
  744. var hasBigT = $thisTh.closest('tr').children().first().hasClass('column_action'); // .eq() is zero-based
  745. if (hasBigT) {
  746. thIndex--;
  747. }
  748. var $table = $thisTh.parents('.table_results');
  749. if (!$table.length) {
  750. $table = $thisTh.parents('table').siblings('.table_results');
  751. }
  752. var $tds = $table.find('tbody tr').find('td.data').eq(thIndex);
  753. if (isAddClass === undefined) {
  754. $tds.toggleClass(newClass);
  755. } else {
  756. $tds.toggleClass(newClass, isAddClass);
  757. }
  758. };
  759. /**
  760. * Handles browse foreign values modal dialog
  761. *
  762. * @param object $this_a reference to the browse foreign value link
  763. */
  764. Sql.browseForeignDialog = function ($thisA) {
  765. var formId = '#browse_foreign_form';
  766. var showAllId = '#foreign_showAll';
  767. var tableId = '#browse_foreign_table';
  768. var filterId = '#input_foreign_filter';
  769. var $dialog = null;
  770. var argSep = CommonParams.get('arg_separator');
  771. var params = $thisA.getPostData();
  772. params += argSep + 'ajax_request=true';
  773. $.post($thisA.attr('href'), params, function (data) {
  774. // Creates browse foreign value dialog
  775. $dialog = $('<div>').append(data.message).dialog({
  776. title: Messages.strBrowseForeignValues,
  777. width: Math.min($(window).width() - 100, 700),
  778. maxHeight: $(window).height() - 100,
  779. dialogClass: 'browse_foreign_modal',
  780. close: function close() {
  781. // remove event handlers attached to elements related to dialog
  782. $(tableId).off('click', 'td a.foreign_value');
  783. $(formId).off('click', showAllId);
  784. $(formId).off('submit'); // remove dialog itself
  785. $(this).remove();
  786. },
  787. modal: true
  788. });
  789. }).done(function () {
  790. var showAll = false;
  791. $(tableId).on('click', 'td a.foreign_value', function (e) {
  792. e.preventDefault();
  793. var $input = $thisA.prev('input[type=text]'); // Check if input exists or get CEdit edit_box
  794. if ($input.length === 0) {
  795. $input = $thisA.closest('.edit_area').prev('.edit_box');
  796. } // Set selected value as input value
  797. $input.val($(this).data('key'));
  798. $dialog.dialog('close');
  799. });
  800. $(formId).on('click', showAllId, function () {
  801. showAll = true;
  802. });
  803. $(formId).on('submit', function (e) {
  804. e.preventDefault(); // if filter value is not equal to old value
  805. // then reset page number to 1
  806. if ($(filterId).val() !== $(filterId).data('old')) {
  807. $(formId).find('select[name=pos]').val('0');
  808. }
  809. var postParams = $(this).serializeArray(); // if showAll button was clicked to submit form then
  810. // add showAll button parameter to form
  811. if (showAll) {
  812. postParams.push({
  813. name: $(showAllId).attr('name'),
  814. value: $(showAllId).val()
  815. });
  816. } // updates values in dialog
  817. $.post($(this).attr('action') + '&ajax_request=1', postParams, function (data) {
  818. var $obj = $('<div>').html(data.message);
  819. $(formId).html($obj.find(formId).html());
  820. $(tableId).html($obj.find(tableId).html());
  821. });
  822. showAll = false;
  823. });
  824. });
  825. };
  826. /**
  827. * Get the auto saved query key
  828. * @return {String}
  829. */
  830. Sql.getAutoSavedKey = function () {
  831. var db = $('input[name="db"]').val();
  832. var table = $('input[name="table"]').val();
  833. var key = db;
  834. if (table !== undefined) {
  835. key += '.' + table;
  836. }
  837. return 'autoSavedSql_' + key;
  838. };
  839. Sql.checkSavedQuery = function () {
  840. var key = Sql.getAutoSavedKey();
  841. if (isStorageSupported('localStorage') && typeof window.localStorage.getItem(key) === 'string') {
  842. Functions.ajaxShowMessage(Messages.strPreviousSaveQuery);
  843. } else if (Cookies.get(key)) {
  844. Functions.ajaxShowMessage(Messages.strPreviousSaveQuery);
  845. }
  846. };
  847. AJAX.registerOnload('sql.js', function () {
  848. $('body').on('click', 'a.browse_foreign', function (e) {
  849. e.preventDefault();
  850. Sql.browseForeignDialog($(this));
  851. });
  852. /**
  853. * vertical column highlighting in horizontal mode when hovering over the column header
  854. */
  855. $(document).on('mouseenter', 'th.column_heading.pointer', function () {
  856. Sql.changeClassForColumn($(this), 'hover', true);
  857. });
  858. $(document).on('mouseleave', 'th.column_heading.pointer', function () {
  859. Sql.changeClassForColumn($(this), 'hover', false);
  860. });
  861. /**
  862. * vertical column marking in horizontal mode when clicking the column header
  863. */
  864. $(document).on('click', 'th.column_heading.marker', function () {
  865. Sql.changeClassForColumn($(this), 'marked');
  866. });
  867. /**
  868. * create resizable table
  869. */
  870. $('.sqlqueryresults').trigger('makegrid');
  871. /**
  872. * Check if there is any saved query
  873. */
  874. if (codeMirrorEditor || document.sqlform) {
  875. Sql.checkSavedQuery();
  876. }
  877. });
  878. /**
  879. * Profiling Chart
  880. */
  881. Sql.makeProfilingChart = function () {
  882. if ($('#profilingchart').length === 0 || $('#profilingchart').html().length !== 0 || !$.jqplot || !$.jqplot.Highlighter || !$.jqplot.PieRenderer) {
  883. return;
  884. }
  885. var data = [];
  886. $.each(JSON.parse($('#profilingChartData').html()), function (key, value) {
  887. data.push([key, parseFloat(value)]);
  888. }); // Remove chart and data divs contents
  889. $('#profilingchart').html('').show();
  890. $('#profilingChartData').html('');
  891. Functions.createProfilingChart('profilingchart', data);
  892. };
  893. /**
  894. * initialize profiling data tables
  895. */
  896. Sql.initProfilingTables = function () {
  897. if (!$.tablesorter) {
  898. return;
  899. } // Added to allow two direction sorting
  900. $('#profiletable').find('thead th').off('click mousedown');
  901. $('#profiletable').tablesorter({
  902. widgets: ['zebra'],
  903. sortList: [[0, 0]],
  904. textExtraction: function textExtraction(node) {
  905. if (node.children.length > 0) {
  906. return node.children[0].innerHTML;
  907. } else {
  908. return node.innerHTML;
  909. }
  910. }
  911. }); // Added to allow two direction sorting
  912. $('#profilesummarytable').find('thead th').off('click mousedown');
  913. $('#profilesummarytable').tablesorter({
  914. widgets: ['zebra'],
  915. sortList: [[1, 1]],
  916. textExtraction: function textExtraction(node) {
  917. if (node.children.length > 0) {
  918. return node.children[0].innerHTML;
  919. } else {
  920. return node.innerHTML;
  921. }
  922. }
  923. });
  924. };
  925. AJAX.registerOnload('sql.js', function () {
  926. Sql.makeProfilingChart();
  927. Sql.initProfilingTables();
  928. });
  929. /**
  930. * Polyfill to make table headers sticky.
  931. */
  932. var elements = $('.sticky');
  933. Stickyfill.add(elements);