db_structure.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used on the database structure page
  4. * @name Database Structure
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @required js/functions.js
  9. */
  10. /**
  11. * AJAX scripts for db_structure.php
  12. *
  13. * Actions ajaxified here:
  14. * Drop Database
  15. * Truncate Table
  16. * Drop Table
  17. *
  18. */
  19. /**
  20. * Unbind all event handlers before tearing down a page
  21. */
  22. AJAX.registerTeardown('db_structure.js', function() {
  23. $("span.fkc_switch").unbind('click');
  24. $('#fkc_checkbox').unbind('change');
  25. $("a.truncate_table_anchor.ajax").die('click');
  26. $("a.drop_table_anchor.ajax").die('click');
  27. $('a.drop_tracking_anchor.ajax').die('click');
  28. $('#real_end_input').die('click');
  29. });
  30. /**
  31. * Adjust number of rows and total size in the summary
  32. * when truncating, creating, dropping or inserting into a table
  33. */
  34. function PMA_adjustTotals() {
  35. var byteUnits = new Array(
  36. PMA_messages['strB'],
  37. PMA_messages['strKiB'],
  38. PMA_messages['strMiB'],
  39. PMA_messages['strGiB'],
  40. PMA_messages['strTiB'],
  41. PMA_messages['strPiB'],
  42. PMA_messages['strEiB']
  43. );
  44. /**
  45. * @var $allTr jQuery object that references all the rows in the list of tables
  46. */
  47. var $allTr = $("#tablesForm table.data tbody:first tr");
  48. // New summary values for the table
  49. var tableSum = $allTr.size();
  50. var rowsSum = 0;
  51. var sizeSum = 0;
  52. var overheadSum = 0;
  53. var rowSumApproximated = false;
  54. $allTr.each(function () {
  55. var $this = $(this);
  56. // Get the number of rows for this SQL table
  57. var strRows = $this.find('.tbl_rows').text();
  58. // If the value is approximated
  59. if (strRows.indexOf('~') == 0) {
  60. rowSumApproximated = true;
  61. // The approximated value contains a preceding ~ and a following 2 (Eg 100 --> ~1002)
  62. strRows = strRows.substring(1, strRows.length - 1);
  63. }
  64. strRows = strRows.replace(/[,.]/g , '');
  65. var intRow = parseInt(strRows, 10);
  66. if (! isNaN(intRow)) {
  67. rowsSum += intRow;
  68. }
  69. // Extract the size and overhead
  70. var valSize = 0;
  71. var valOverhead = 0;
  72. var strSize = $.trim($this.find('.tbl_size span:not(.unit)').text());
  73. var strSizeUnit = $.trim($this.find('.tbl_size span.unit').text());
  74. var strOverhead = $.trim($this.find('.tbl_overhead span:not(.unit)').text());
  75. var strOverheadUnit = $.trim($this.find('.tbl_overhead span.unit').text());
  76. // Given a value and a unit, such as 100 and KiB, for the table size
  77. // and overhead calculate their numeric values in bytes, such as 102400
  78. for (var i = 0; i < byteUnits.length; i++) {
  79. if (strSizeUnit == byteUnits[i]) {
  80. var tmpVal = parseFloat(strSize);
  81. valSize = tmpVal * Math.pow(1024, i);
  82. break;
  83. }
  84. }
  85. for (var i = 0; i < byteUnits.length; i++) {
  86. if (strOverheadUnit == byteUnits[i]) {
  87. var tmpVal = parseFloat(strOverhead);
  88. valOverhead = tmpVal * Math.pow(1024, i);
  89. break;
  90. }
  91. }
  92. sizeSum += valSize;
  93. overheadSum += valOverhead;
  94. });
  95. // Add some commas for readablility:
  96. // 1000000 becomes 1,000,000
  97. var strRowSum = rowsSum + "";
  98. var regex = /(\d+)(\d{3})/;
  99. while (regex.test(strRowSum)) {
  100. strRowSum = strRowSum.replace(regex, '$1' + ',' + '$2');
  101. }
  102. // If approximated total value add ~ in front
  103. if (rowSumApproximated) {
  104. strRowSum = "~" + strRowSum;
  105. }
  106. // Calculate the magnitude for the size and overhead values
  107. var size_magnitude = 0, overhead_magnitude = 0;
  108. while (sizeSum >= 1024) {
  109. sizeSum /= 1024;
  110. size_magnitude++;
  111. }
  112. while (overheadSum >= 1024) {
  113. overheadSum /= 1024;
  114. overhead_magnitude++;
  115. }
  116. sizeSum = Math.round(sizeSum * 10) / 10;
  117. overheadSum = Math.round(overheadSum * 10) / 10;
  118. // Update summary with new data
  119. var $summary = $("#tbl_summary_row");
  120. $summary.find('.tbl_num').text($.sprintf(PMA_messages['strTables'], tableSum));
  121. $summary.find('.tbl_rows').text(strRowSum);
  122. $summary.find('.tbl_size').text(sizeSum + " " + byteUnits[size_magnitude]);
  123. $summary.find('.tbl_overhead').text(overheadSum + " " + byteUnits[overhead_magnitude]);
  124. }
  125. AJAX.registerOnload('db_structure.js', function() {
  126. /**
  127. * Handler for the print view multisubmit.
  128. * All other multi submits can be handled via ajax, but this one needs
  129. * special treatment as the results need to open in another browser window
  130. */
  131. $('#tablesForm').submit(function (event) {
  132. var $form = $(this);
  133. if ($form.find('select[name=submit_mult]').val() === 'print') {
  134. event.preventDefault();
  135. event.stopPropagation();
  136. $('form#clone').remove();
  137. var $clone = $form
  138. .clone()
  139. .hide()
  140. .appendTo('body');
  141. $clone
  142. .find('select[name=submit_mult]')
  143. .val('print');
  144. $clone
  145. .attr('target', 'printview')
  146. .attr('id', 'clone')
  147. .submit();
  148. }
  149. });
  150. /**
  151. * Event handler for 'Foreign Key Checks' disabling option
  152. * in the drop table confirmation form
  153. */
  154. $("span.fkc_switch").click(function(event){
  155. if ($("#fkc_checkbox").prop('checked')) {
  156. $("#fkc_checkbox").prop('checked', false);
  157. $("#fkc_status").html(PMA_messages['strForeignKeyCheckDisabled']);
  158. return;
  159. }
  160. $("#fkc_checkbox").prop('checked', true);
  161. $("#fkc_status").html(PMA_messages['strForeignKeyCheckEnabled']);
  162. });
  163. $('#fkc_checkbox').change(function () {
  164. if ($(this).prop("checked")) {
  165. $("#fkc_status").html(PMA_messages['strForeignKeyCheckEnabled']);
  166. return;
  167. }
  168. $("#fkc_status").html(PMA_messages['strForeignKeyCheckDisabled']);
  169. }); // End of event handler for 'Foreign Key Check'
  170. /**
  171. * Ajax Event handler for 'Truncate Table'
  172. */
  173. $("a.truncate_table_anchor.ajax").live('click', function(event) {
  174. event.preventDefault();
  175. /**
  176. * @var $this_anchor Object referring to the anchor clicked
  177. */
  178. var $this_anchor = $(this);
  179. //extract current table name and build the question string
  180. /**
  181. * @var curr_table_name String containing the name of the table to be truncated
  182. */
  183. var curr_table_name = $this_anchor.parents('tr').children('th').children('a').text();
  184. /**
  185. * @var question String containing the question to be asked for confirmation
  186. */
  187. var question =
  188. PMA_messages.strTruncateTableStrongWarning + ' '
  189. + $.sprintf(PMA_messages.strDoYouReally, 'TRUNCATE ' + escapeHtml(curr_table_name));
  190. $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
  191. PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
  192. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
  193. if (data.success == true) {
  194. PMA_ajaxShowMessage(data.message);
  195. // Adjust table statistics
  196. var $tr = $this_anchor.closest('tr');
  197. $tr.find('.tbl_rows').text('0');
  198. $tr.find('.tbl_size, .tbl_overhead').text('-');
  199. //Fetch inner span of this anchor
  200. //and replace the icon with its disabled version
  201. var span = $this_anchor.html().replace(/b_empty/, 'bd_empty');
  202. //To disable further attempts to truncate the table,
  203. //replace the a element with its inner span (modified)
  204. $this_anchor
  205. .replaceWith(span)
  206. .removeClass('truncate_table_anchor');
  207. PMA_adjustTotals();
  208. } else {
  209. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  210. }
  211. }); // end $.get()
  212. }); //end $.PMA_confirm()
  213. }); //end of Truncate Table Ajax action
  214. /**
  215. * Ajax Event handler for 'Drop Table' or 'Drop View'
  216. */
  217. $("a.drop_table_anchor.ajax").live('click', function(event) {
  218. event.preventDefault();
  219. var $this_anchor = $(this);
  220. //extract current table name and build the question string
  221. /**
  222. * @var $curr_row Object containing reference to the current row
  223. */
  224. var $curr_row = $this_anchor.parents('tr');
  225. /**
  226. * @var curr_table_name String containing the name of the table to be truncated
  227. */
  228. var curr_table_name = $curr_row.children('th').children('a').text();
  229. /**
  230. * @var is_view Boolean telling if we have a view
  231. */
  232. var is_view = $curr_row.hasClass('is_view') || $this_anchor.hasClass('view');
  233. /**
  234. * @var question String containing the question to be asked for confirmation
  235. */
  236. var question;
  237. if (! is_view) {
  238. question =
  239. PMA_messages.strDropTableStrongWarning + ' '
  240. + $.sprintf(PMA_messages.strDoYouReally, 'DROP TABLE ' + escapeHtml(curr_table_name));
  241. } else {
  242. question =
  243. $.sprintf(PMA_messages.strDoYouReally, 'DROP VIEW ' + escapeHtml(curr_table_name));
  244. }
  245. $this_anchor.PMA_confirm(question, $this_anchor.attr('href'), function(url) {
  246. var $msg = PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
  247. $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true}, function(data) {
  248. if (data.success == true) {
  249. PMA_ajaxShowMessage(data.message);
  250. toggleRowColors($curr_row.next());
  251. $curr_row.hide("medium").remove();
  252. PMA_adjustTotals();
  253. PMA_reloadNavigation();
  254. PMA_ajaxRemoveMessage($msg);
  255. } else {
  256. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  257. }
  258. }); // end $.get()
  259. }); // end $.PMA_confirm()
  260. }); //end of Drop Table Ajax action
  261. /**
  262. * Ajax Event handler for 'Drop tracking'
  263. */
  264. $('a.drop_tracking_anchor.ajax').live('click', function(event) {
  265. event.preventDefault();
  266. var $anchor = $(this);
  267. /**
  268. * @var curr_tracking_row Object containing reference to the current tracked table's row
  269. */
  270. var $curr_tracking_row = $anchor.parents('tr');
  271. /**
  272. * @var question String containing the question to be asked for confirmation
  273. */
  274. var question = PMA_messages['strDeleteTrackingData'];
  275. $anchor.PMA_confirm(question, $anchor.attr('href'), function(url) {
  276. PMA_ajaxShowMessage(PMA_messages['strDeletingTrackingData']);
  277. $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function(data) {
  278. if (data.success == true) {
  279. var $tracked_table = $curr_tracking_row.parents('table');
  280. var table_name = $curr_tracking_row.find('td:nth-child(2)').text();
  281. // Check how many rows will be left after we remove
  282. if ($tracked_table.find('tbody tr').length === 1) {
  283. // We are removing the only row it has
  284. $('#tracked_tables').hide("slow").remove();
  285. } else {
  286. // There are more rows left after the deletion
  287. toggleRowColors($curr_tracking_row.next());
  288. $curr_tracking_row.hide("slow", function() {
  289. $(this).remove();
  290. });
  291. }
  292. // Make the removed table visible in the list of 'Untracked tables'.
  293. var $untracked_table = $('table#noversions');
  294. // This won't work if no untracked tables are there.
  295. if ($untracked_table.length > 0) {
  296. var $rows = $untracked_table.find('tbody tr');
  297. $rows.each(function(index) {
  298. var $row = $(this);
  299. var tmp_tbl_name = $row.find('td:first-child').text();
  300. var is_last_iteration = (index == ($rows.length - 1));
  301. if (tmp_tbl_name > table_name || is_last_iteration) {
  302. var $cloned = $row.clone();
  303. // Change the table name of the cloned row.
  304. $cloned.find('td:first-child').text(table_name);
  305. // Change the link of the cloned row.
  306. var new_url = $cloned
  307. .find('td:nth-child(2) a')
  308. .attr('href')
  309. .replace('table=' + tmp_tbl_name, 'table=' + encodeURIComponent(table_name));
  310. $cloned.find('td:nth-child(2) a').attr('href', new_url);
  311. // Insert the cloned row in an appropriate location.
  312. if (tmp_tbl_name > table_name) {
  313. $cloned.insertBefore($row);
  314. toggleRowColors($row);
  315. return false;
  316. } else {
  317. $cloned.insertAfter($row);
  318. toggleRowColors($cloned);
  319. }
  320. }
  321. });
  322. }
  323. PMA_ajaxShowMessage(data.message);
  324. } else {
  325. PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : " + data.error, false);
  326. }
  327. }); // end $.get()
  328. }); // end $.PMA_confirm()
  329. }); //end Drop Tracking
  330. //Calculate Real End for InnoDB
  331. /**
  332. * Ajax Event handler for calculatig the real end for a InnoDB table
  333. *
  334. */
  335. $('#real_end_input').live('click', function(event) {
  336. event.preventDefault();
  337. /**
  338. * @var question String containing the question to be asked for confirmation
  339. */
  340. var question = PMA_messages['strOperationTakesLongTime'];
  341. $(this).PMA_confirm(question, '', function() {
  342. return true;
  343. });
  344. return false;
  345. }); //end Calculate Real End for InnoDB
  346. }); // end $()