PageRenderTime 44ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 1ms

/phpMyAdmin/libraries/export/sql.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 1290 lines | 897 code | 134 blank | 259 comment | 233 complexity | acc9355f2efbfd67e1f2c516cc056ee9 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build SQL dumps of tables
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage SQL
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /**
  13. *
  14. */
  15. if (isset($plugin_list)) {
  16. $hide_sql = false;
  17. $hide_structure = false;
  18. if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
  19. $hide_structure = true;
  20. $hide_sql = true;
  21. }
  22. if (!$hide_sql) {
  23. $plugin_list['sql'] = array(
  24. 'text' => __('SQL'),
  25. 'extension' => 'sql',
  26. 'mime_type' => 'text/x-sql',
  27. 'options' => array());
  28. $plugin_list['sql']['options'][] = array(
  29. 'type' => 'begin_group',
  30. 'name' => 'general_opts');
  31. /* comments */
  32. $plugin_list['sql']['options'][] = array(
  33. 'type' => 'begin_subgroup',
  34. 'subgroup_header' => array(
  35. 'type' => 'bool',
  36. 'name' => 'include_comments',
  37. 'text' => __('Display comments <i>(includes info such as export timestamp, PHP version, and server version)</i>')
  38. ));
  39. $plugin_list['sql']['options'][] = array(
  40. 'type' => 'text',
  41. 'name' => 'header_comment',
  42. 'text' => __('Additional custom header comment (\n splits lines):')
  43. );
  44. $plugin_list['sql']['options'][] = array(
  45. 'type' => 'bool',
  46. 'name' => 'dates',
  47. 'text' => __('Include a timestamp of when databases were created, last updated, and last checked')
  48. );
  49. if (!empty($GLOBALS['cfgRelation']['relation'])) {
  50. $plugin_list['sql']['options'][] = array(
  51. 'type' => 'bool',
  52. 'name' => 'relation',
  53. 'text' => __('Display foreign key relationships')
  54. );
  55. }
  56. if (!empty($GLOBALS['cfgRelation']['mimework'])) {
  57. $plugin_list['sql']['options'][] = array(
  58. 'type' => 'bool',
  59. 'name' => 'mime',
  60. 'text' => __('Display MIME types')
  61. );
  62. }
  63. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  64. /* end comments */
  65. /* enclose in a transaction */
  66. $plugin_list['sql']['options'][] = array(
  67. 'type' => 'bool',
  68. 'name' => 'use_transaction',
  69. 'text' => __('Enclose export in a transaction'),
  70. 'doc' => array('programs', 'mysqldump', 'option_mysqldump_single-transaction')
  71. );
  72. /* disable foreign key checks */
  73. $plugin_list['sql']['options'][] = array(
  74. 'type' => 'bool',
  75. 'name' => 'disable_fk',
  76. 'text' => __('Disable foreign key checks'),
  77. 'doc' => array(
  78. 'manual_MySQL_Database_Administration',
  79. 'server-system-variables',
  80. 'sysvar_foreign_key_checks')
  81. );
  82. $plugin_list['sql']['options_text'] = __('Options');
  83. /* compatibility maximization */
  84. $compats = PMA_DBI_getCompatibilities();
  85. if (count($compats) > 0) {
  86. $values = array();
  87. foreach ($compats as $val) {
  88. $values[$val] = $val;
  89. }
  90. $plugin_list['sql']['options'][] = array(
  91. 'type' => 'select',
  92. 'name' => 'compatibility',
  93. 'text' => __('Database system or older MySQL server to maximize output compatibility with:'),
  94. 'values' => $values,
  95. 'doc' => array(
  96. 'manual_MySQL_Database_Administration',
  97. 'Server_SQL_mode')
  98. );
  99. unset($values);
  100. }
  101. /* server export options */
  102. if ($plugin_param['export_type'] == 'server') {
  103. $plugin_list['sql']['options'][] = array(
  104. 'type' => 'bool',
  105. 'name' => 'drop_database',
  106. 'text' => sprintf(__('Add %s statement'), '<code>DROP DATABASE</code>')
  107. );
  108. }
  109. /* what to dump (structure/data/both) */
  110. $plugin_list['sql']['options'][] = array(
  111. 'type' => 'begin_subgroup',
  112. 'subgroup_header' => array(
  113. 'type' => 'message_only',
  114. 'text' => __('Dump table')
  115. ));
  116. $plugin_list['sql']['options'][] = array(
  117. 'type' => 'radio',
  118. 'name' => 'structure_or_data',
  119. 'values' => array(
  120. 'structure' => __('structure'),
  121. 'data' => __('data'),
  122. 'structure_and_data' => __('structure and data')
  123. ));
  124. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  125. $plugin_list['sql']['options'][] = array('type' => 'end_group');
  126. /* begin Structure options */
  127. if (!$hide_structure) {
  128. $plugin_list['sql']['options'][] = array(
  129. 'type' => 'begin_group',
  130. 'name' => 'structure',
  131. 'text' => __('Object creation options'),
  132. 'force' => 'data'
  133. );
  134. /* begin SQL Statements */
  135. $plugin_list['sql']['options'][] = array(
  136. 'type' => 'begin_subgroup',
  137. 'subgroup_header' => array(
  138. 'type' => 'message_only',
  139. 'name' => 'add_statements',
  140. 'text' => __('Add statements:')
  141. ));
  142. if ($plugin_param['export_type'] == 'table') {
  143. if (PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])) {
  144. $drop_clause = '<code>DROP VIEW</code>';
  145. } else {
  146. $drop_clause = '<code>DROP TABLE</code>';
  147. }
  148. } else {
  149. if (PMA_DRIZZLE) {
  150. $drop_clause = '<code>DROP TABLE</code>';
  151. } else {
  152. $drop_clause = '<code>DROP TABLE / VIEW / PROCEDURE / FUNCTION</code>';
  153. if (PMA_MYSQL_INT_VERSION > 50100) {
  154. $drop_clause .= '<code> / EVENT</code>';
  155. }
  156. }
  157. }
  158. $plugin_list['sql']['options'][] = array(
  159. 'type' => 'bool',
  160. 'name' => 'drop_table',
  161. 'text' => sprintf(__('Add %s statement'), $drop_clause)
  162. );
  163. // Drizzle doesn't support procedures and functions
  164. if (!PMA_DRIZZLE) {
  165. $plugin_list['sql']['options'][] = array(
  166. 'type' => 'bool',
  167. 'name' => 'procedure_function',
  168. 'text' => sprintf(__('Add %s statement'), '<code>CREATE PROCEDURE / FUNCTION' . (PMA_MYSQL_INT_VERSION > 50100 ? ' / EVENT</code>' : '</code>'))
  169. );
  170. }
  171. /* begin CREATE TABLE statements*/
  172. $plugin_list['sql']['options'][] = array(
  173. 'type' => 'begin_subgroup',
  174. 'subgroup_header' => array(
  175. 'type' => 'bool',
  176. 'name' => 'create_table_statements',
  177. 'text' => __('<code>CREATE TABLE</code> options:')
  178. ));
  179. $plugin_list['sql']['options'][] = array(
  180. 'type' => 'bool',
  181. 'name' => 'if_not_exists',
  182. 'text' => '<code>IF NOT EXISTS</code>'
  183. );
  184. $plugin_list['sql']['options'][] = array(
  185. 'type' => 'bool',
  186. 'name' => 'auto_increment',
  187. 'text' => '<code>AUTO_INCREMENT</code>'
  188. );
  189. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  190. /* end CREATE TABLE statements */
  191. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  192. /* end SQL statements */
  193. $plugin_list['sql']['options'][] = array(
  194. 'type' => 'bool',
  195. 'name' => 'backquotes',
  196. 'text' => __('Enclose table and column names with backquotes <i>(Protects column and table names formed with special characters or keywords)</i>')
  197. );
  198. $plugin_list['sql']['options'][] = array('type' => 'end_group');
  199. }
  200. /* end Structure options */
  201. /* begin Data options */
  202. $plugin_list['sql']['options'][] = array(
  203. 'type' => 'begin_group',
  204. 'name' => 'data',
  205. 'text' => __('Data dump options'),
  206. 'force' => 'structure'
  207. );
  208. /* begin SQL statements */
  209. $plugin_list['sql']['options'][] = array(
  210. 'type' => 'begin_subgroup',
  211. 'subgroup_header' => array(
  212. 'type' => 'message_only',
  213. 'text' => __('Instead of <code>INSERT</code> statements, use:')
  214. ));
  215. // Not supported in Drizzle
  216. if (!PMA_DRIZZLE) {
  217. $plugin_list['sql']['options'][] = array(
  218. 'type' => 'bool',
  219. 'name' => 'delayed',
  220. 'text' => __('<code>INSERT DELAYED</code> statements'),
  221. 'doc' => array('manual_MySQL_Database_Administration', 'insert_delayed')
  222. );
  223. }
  224. $plugin_list['sql']['options'][] = array(
  225. 'type' => 'bool',
  226. 'name' => 'ignore',
  227. 'text' => __('<code>INSERT IGNORE</code> statements'),
  228. 'doc' => array('manual_MySQL_Database_Administration', 'insert')
  229. );
  230. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  231. /* end SQL statements */
  232. /* Function to use when dumping data */
  233. $plugin_list['sql']['options'][] = array(
  234. 'type' => 'select',
  235. 'name' => 'type',
  236. 'text' => __('Function to use when dumping data:'),
  237. 'values' => array(
  238. 'INSERT' => 'INSERT',
  239. 'UPDATE' => 'UPDATE',
  240. 'REPLACE' => 'REPLACE'
  241. )
  242. );
  243. /* Syntax to use when inserting data */
  244. $plugin_list['sql']['options'][] = array(
  245. 'type' => 'begin_subgroup',
  246. 'subgroup_header' => array(
  247. 'type' => 'message_only',
  248. 'text' => __('Syntax to use when inserting data:')
  249. ));
  250. $plugin_list['sql']['options'][] = array(
  251. 'type' => 'radio',
  252. 'name' => 'insert_syntax',
  253. 'values' => array(
  254. 'complete' => __('include column names in every <code>INSERT</code> statement <br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO tbl_name (col_A,col_B,col_C) VALUES (1,2,3)</code>'),
  255. 'extended' => __('insert multiple rows in every <code>INSERT</code> statement<br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO tbl_name VALUES (1,2,3), (4,5,6), (7,8,9)</code>'),
  256. 'both' => __('both of the above<br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO tbl_name (col_A,col_B) VALUES (1,2,3), (4,5,6), (7,8,9)</code>'),
  257. 'none' => __('neither of the above<br /> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO tbl_name VALUES (1,2,3)</code>')
  258. ));
  259. $plugin_list['sql']['options'][] = array('type' => 'end_subgroup');
  260. /* Max length of query */
  261. $plugin_list['sql']['options'][] = array(
  262. 'type' => 'text',
  263. 'name' => 'max_query_size',
  264. 'text' => __('Maximal length of created query')
  265. );
  266. /* Dump binary columns in hexadecimal */
  267. $plugin_list['sql']['options'][] = array(
  268. 'type' => 'bool',
  269. 'name' => 'hex_for_blob',
  270. 'text' => __('Dump binary columns in hexadecimal notation <i>(for example, "abc" becomes 0x616263)</i>')
  271. );
  272. // Drizzle works only with UTC timezone
  273. if (!PMA_DRIZZLE) {
  274. /* Dump time in UTC */
  275. $plugin_list['sql']['options'][] = array(
  276. 'type' => 'bool',
  277. 'name' => 'utc_time',
  278. 'text' => __('Dump TIMESTAMP columns in UTC <i>(enables TIMESTAMP columns to be dumped and reloaded between servers in different time zones)</i>')
  279. );
  280. }
  281. $plugin_list['sql']['options'][] = array('type' => 'end_group');
  282. /* end Data options */
  283. }
  284. } else {
  285. /**
  286. * Avoids undefined variables, use NULL so isset() returns false
  287. */
  288. if (! isset($GLOBALS['sql_backquotes'])) {
  289. $GLOBALS['sql_backquotes'] = null;
  290. }
  291. /**
  292. * Exports routines (procedures and functions)
  293. *
  294. * @param string $db
  295. * @return bool Whether it succeeded
  296. *
  297. * @access public
  298. */
  299. function PMA_exportRoutines($db) {
  300. global $crlf;
  301. $text = '';
  302. $delimiter = '$$';
  303. $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
  304. $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
  305. if ($procedure_names || $function_names) {
  306. $text .= $crlf
  307. . 'DELIMITER ' . $delimiter . $crlf;
  308. }
  309. if ($procedure_names) {
  310. $text .=
  311. PMA_exportComment()
  312. . PMA_exportComment(__('Procedures'))
  313. . PMA_exportComment();
  314. foreach ($procedure_names as $procedure_name) {
  315. if (! empty($GLOBALS['sql_drop_table'])) {
  316. $text .= 'DROP PROCEDURE IF EXISTS '
  317. . PMA_backquote($procedure_name)
  318. . $delimiter . $crlf;
  319. }
  320. $text .= PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name)
  321. . $delimiter . $crlf . $crlf;
  322. }
  323. }
  324. if ($function_names) {
  325. $text .=
  326. PMA_exportComment()
  327. . PMA_exportComment(__('Functions'))
  328. . PMA_exportComment();
  329. foreach ($function_names as $function_name) {
  330. if (! empty($GLOBALS['sql_drop_table'])) {
  331. $text .= 'DROP FUNCTION IF EXISTS '
  332. . PMA_backquote($function_name)
  333. . $delimiter . $crlf;
  334. }
  335. $text .= PMA_DBI_get_definition($db, 'FUNCTION', $function_name)
  336. . $delimiter . $crlf . $crlf;
  337. }
  338. }
  339. if ($procedure_names || $function_names) {
  340. $text .= 'DELIMITER ;' . $crlf;
  341. }
  342. if (! empty($text)) {
  343. return PMA_exportOutputHandler($text);
  344. } else {
  345. return false;
  346. }
  347. }
  348. /**
  349. * Possibly outputs comment
  350. *
  351. * @param string $text Text of comment
  352. * @return string The formatted comment
  353. *
  354. * @access private
  355. */
  356. function PMA_exportComment($text = '')
  357. {
  358. if (isset($GLOBALS['sql_include_comments']) && $GLOBALS['sql_include_comments']) {
  359. // see http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html
  360. return '--' . (empty($text) ? '' : ' ') . $text . $GLOBALS['crlf'];
  361. } else {
  362. return '';
  363. }
  364. }
  365. /**
  366. * Possibly outputs CRLF
  367. *
  368. * @return string $crlf or nothing
  369. *
  370. * @access private
  371. */
  372. function PMA_possibleCRLF()
  373. {
  374. if (isset($GLOBALS['sql_include_comments']) && $GLOBALS['sql_include_comments']) {
  375. return $GLOBALS['crlf'];
  376. } else {
  377. return '';
  378. }
  379. }
  380. /**
  381. * Outputs export footer
  382. *
  383. * @return bool Whether it succeeded
  384. *
  385. * @access public
  386. */
  387. function PMA_exportFooter()
  388. {
  389. global $crlf;
  390. global $mysql_charset_map;
  391. $foot = '';
  392. if (isset($GLOBALS['sql_disable_fk'])) {
  393. $foot .= 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
  394. }
  395. if (isset($GLOBALS['sql_use_transaction'])) {
  396. $foot .= 'COMMIT;' . $crlf;
  397. }
  398. // restore connection settings
  399. $charset_of_file = isset($GLOBALS['charset_of_file']) ? $GLOBALS['charset_of_file'] : '';
  400. if (!empty($GLOBALS['asfile']) && isset($mysql_charset_map[$charset_of_file]) && !PMA_DRIZZLE) {
  401. $foot .= $crlf
  402. . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' . $crlf
  403. . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' . $crlf
  404. . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' . $crlf;
  405. }
  406. /* Restore timezone */
  407. if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
  408. PMA_DBI_query('SET time_zone = "' . $GLOBALS['old_tz'] . '"');
  409. }
  410. return PMA_exportOutputHandler($foot);
  411. }
  412. /**
  413. * Outputs export header
  414. *
  415. * @return bool Whether it succeeded
  416. *
  417. * @access public
  418. */
  419. function PMA_exportHeader()
  420. {
  421. global $crlf;
  422. global $cfg;
  423. global $mysql_charset_map;
  424. if (isset($GLOBALS['sql_compatibility'])) {
  425. $tmp_compat = $GLOBALS['sql_compatibility'];
  426. if ($tmp_compat == 'NONE') {
  427. $tmp_compat = '';
  428. }
  429. PMA_DBI_try_query('SET SQL_MODE="' . $tmp_compat . '"');
  430. unset($tmp_compat);
  431. }
  432. $head = PMA_exportComment('phpMyAdmin SQL Dump')
  433. . PMA_exportComment('version ' . PMA_VERSION)
  434. . PMA_exportComment('http://www.phpmyadmin.net')
  435. . PMA_exportComment();
  436. $host_string = __('Host') . ': ' . $cfg['Server']['host'];
  437. if (!empty($cfg['Server']['port'])) {
  438. $host_string .= ':' . $cfg['Server']['port'];
  439. }
  440. $head .= PMA_exportComment($host_string);
  441. $head .= PMA_exportComment(__('Generation Time')
  442. . ': ' . PMA_localisedDate())
  443. . PMA_exportComment(__('Server version') . ': ' . PMA_MYSQL_STR_VERSION)
  444. . PMA_exportComment(__('PHP Version') . ': ' . phpversion())
  445. . PMA_possibleCRLF();
  446. if (isset($GLOBALS['sql_header_comment']) && !empty($GLOBALS['sql_header_comment'])) {
  447. // '\n' is not a newline (like "\n" would be), it's the characters
  448. // backslash and n, as explained on the export interface
  449. $lines = explode('\n', $GLOBALS['sql_header_comment']);
  450. $head .= PMA_exportComment();
  451. foreach ($lines as $one_line) {
  452. $head .= PMA_exportComment($one_line);
  453. }
  454. $head .= PMA_exportComment();
  455. }
  456. if (isset($GLOBALS['sql_disable_fk'])) {
  457. $head .= 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
  458. }
  459. /* We want exported AUTO_INCREMENT columns to have still same value, do this only for recent MySQL exports */
  460. if ((!isset($GLOBALS['sql_compatibility']) || $GLOBALS['sql_compatibility'] == 'NONE')
  461. && !PMA_DRIZZLE) {
  462. $head .= 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";' . $crlf;
  463. }
  464. if (isset($GLOBALS['sql_use_transaction'])) {
  465. $head .= 'SET AUTOCOMMIT=0;' . $crlf
  466. . 'START TRANSACTION;' . $crlf;
  467. }
  468. /* Change timezone if we should export timestamps in UTC */
  469. if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
  470. $head .= 'SET time_zone = "+00:00";' . $crlf;
  471. $GLOBALS['old_tz'] = PMA_DBI_fetch_value('SELECT @@session.time_zone');
  472. PMA_DBI_query('SET time_zone = "+00:00"');
  473. }
  474. $head .= PMA_possibleCRLF();
  475. if (! empty($GLOBALS['asfile']) && !PMA_DRIZZLE) {
  476. // we are saving as file, therefore we provide charset information
  477. // so that a utility like the mysql client can interpret
  478. // the file correctly
  479. if (isset($GLOBALS['charset_of_file']) && isset($mysql_charset_map[$GLOBALS['charset_of_file']])) {
  480. // we got a charset from the export dialog
  481. $set_names = $mysql_charset_map[$GLOBALS['charset_of_file']];
  482. } else {
  483. // by default we use the connection charset
  484. $set_names = $mysql_charset_map['utf-8'];
  485. }
  486. $head .= $crlf
  487. . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;' . $crlf
  488. . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;' . $crlf
  489. . '/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;' . $crlf
  490. . '/*!40101 SET NAMES ' . $set_names . ' */;' . $crlf . $crlf;
  491. }
  492. return PMA_exportOutputHandler($head);
  493. }
  494. /**
  495. * Outputs CREATE DATABASE statement
  496. *
  497. * @param string $db Database name
  498. * @return bool Whether it succeeded
  499. *
  500. * @access public
  501. */
  502. function PMA_exportDBCreate($db)
  503. {
  504. global $crlf;
  505. if (isset($GLOBALS['sql_drop_database'])) {
  506. if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['sql_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) {
  507. return false;
  508. }
  509. }
  510. $create_query = 'CREATE DATABASE ' . (isset($GLOBALS['sql_backquotes']) ? PMA_backquote($db) : $db);
  511. $collation = PMA_getDbCollation($db);
  512. if (PMA_DRIZZLE) {
  513. $create_query .= ' COLLATE ' . $collation;
  514. } else {
  515. if (strpos($collation, '_')) {
  516. $create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation;
  517. } else {
  518. $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
  519. }
  520. }
  521. $create_query .= ';' . $crlf;
  522. if (!PMA_exportOutputHandler($create_query)) {
  523. return false;
  524. }
  525. if (isset($GLOBALS['sql_backquotes'])
  526. && ((isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'NONE') || PMA_DRIZZLE)) {
  527. $result = PMA_exportOutputHandler('USE ' . PMA_backquote($db) . ';' . $crlf);
  528. } else {
  529. $result = PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
  530. }
  531. return $result;
  532. }
  533. /**
  534. * Outputs database header
  535. *
  536. * @param string $db Database name
  537. * @return bool Whether it succeeded
  538. *
  539. * @access public
  540. */
  541. function PMA_exportDBHeader($db)
  542. {
  543. $head = PMA_exportComment()
  544. . PMA_exportComment(__('Database') . ': ' . (isset($GLOBALS['sql_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''))
  545. . PMA_exportComment();
  546. return PMA_exportOutputHandler($head);
  547. }
  548. /**
  549. * Outputs database footer
  550. *
  551. * @param string $db Database name
  552. * @return bool Whether it succeeded
  553. *
  554. * @access public
  555. */
  556. function PMA_exportDBFooter($db)
  557. {
  558. global $crlf;
  559. $result = true;
  560. if (isset($GLOBALS['sql_constraints'])) {
  561. $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']);
  562. unset($GLOBALS['sql_constraints']);
  563. }
  564. if (($GLOBALS['sql_structure_or_data'] == 'structure' || $GLOBALS['sql_structure_or_data'] == 'structure_and_data') && isset($GLOBALS['sql_procedure_function'])) {
  565. $text = '';
  566. $delimiter = '$$';
  567. if (PMA_MYSQL_INT_VERSION > 50100) {
  568. $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db, true) . '\';');
  569. } else {
  570. $event_names = array();
  571. }
  572. if ($event_names) {
  573. $text .= $crlf
  574. . 'DELIMITER ' . $delimiter . $crlf;
  575. $text .=
  576. PMA_exportComment()
  577. . PMA_exportComment(__('Events'))
  578. . PMA_exportComment();
  579. foreach ($event_names as $event_name) {
  580. if (! empty($GLOBALS['sql_drop_table'])) {
  581. $text .= 'DROP EVENT ' . PMA_backquote($event_name) . $delimiter . $crlf;
  582. }
  583. $text .= PMA_DBI_get_definition($db, 'EVENT', $event_name) . $delimiter . $crlf . $crlf;
  584. }
  585. $text .= 'DELIMITER ;' . $crlf;
  586. }
  587. if (! empty($text)) {
  588. $result = PMA_exportOutputHandler($text);
  589. }
  590. }
  591. return $result;
  592. }
  593. /**
  594. * Returns a stand-in CREATE definition to resolve view dependencies
  595. *
  596. * @param string $db the database name
  597. * @param string $view the view name
  598. * @param string $crlf the end of line sequence
  599. * @return string resulting definition
  600. *
  601. * @access public
  602. */
  603. function PMA_getTableDefStandIn($db, $view, $crlf) {
  604. $create_query = '';
  605. if (! empty($GLOBALS['sql_drop_table'])) {
  606. $create_query .= 'DROP VIEW IF EXISTS ' . PMA_backquote($view) . ';' . $crlf;
  607. }
  608. $create_query .= 'CREATE TABLE ';
  609. if (isset($GLOBALS['sql_if_not_exists']) && $GLOBALS['sql_if_not_exists']) {
  610. $create_query .= 'IF NOT EXISTS ';
  611. }
  612. $create_query .= PMA_backquote($view) . ' (' . $crlf;
  613. $tmp = array();
  614. $columns = PMA_DBI_get_columns_full($db, $view);
  615. foreach ($columns as $column_name => $definition) {
  616. $tmp[] = PMA_backquote($column_name) . ' ' . $definition['Type'] . $crlf;
  617. }
  618. $create_query .= implode(',', $tmp) . ');';
  619. return($create_query);
  620. }
  621. /**
  622. * Returns $table's CREATE definition
  623. *
  624. * @param string $db the database name
  625. * @param string $table the table name
  626. * @param string $crlf the end of line sequence
  627. * @param string $error_url the url to go back in case of error
  628. * @param bool $show_dates whether to include creation/update/check dates
  629. * @param bool $add_semicolon whether to add semicolon and end-of-line at the end
  630. * @param bool $view whether we're handling a view
  631. * @return string resulting schema
  632. *
  633. * @access public
  634. */
  635. function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false, $add_semicolon = true, $view = false)
  636. {
  637. global $sql_drop_table;
  638. global $sql_backquotes;
  639. global $sql_constraints;
  640. global $sql_constraints_query; // just the text of the query
  641. global $sql_drop_foreign_keys;
  642. $schema_create = '';
  643. $auto_increment = '';
  644. $new_crlf = $crlf;
  645. // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
  646. $result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddSlashes($table, true) . '\'', null, PMA_DBI_QUERY_STORE);
  647. if ($result != false) {
  648. if (PMA_DBI_num_rows($result) > 0) {
  649. $tmpres = PMA_DBI_fetch_assoc($result);
  650. if (PMA_DRIZZLE && $show_dates) {
  651. // Drizzle doesn't give Create_time and Update_time in SHOW TABLE STATUS, add it
  652. $sql ="SELECT
  653. TABLE_CREATION_TIME AS Create_time,
  654. TABLE_UPDATE_TIME AS Update_time
  655. FROM data_dictionary.TABLES
  656. WHERE TABLE_SCHEMA = '" . PMA_sqlAddSlashes($db) . "'
  657. AND TABLE_NAME = '" . PMA_sqlAddSlashes($table) . "'";
  658. $tmpres = array_merge(PMA_DBI_fetch_single_row($sql), $tmpres);
  659. }
  660. // Here we optionally add the AUTO_INCREMENT next value,
  661. // but starting with MySQL 5.0.24, the clause is already included
  662. // in SHOW CREATE TABLE so we'll remove it below
  663. // It's required for Drizzle because SHOW CREATE TABLE uses
  664. // the value from table's creation time
  665. if (isset($GLOBALS['sql_auto_increment']) && !empty($tmpres['Auto_increment'])) {
  666. $auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' ';
  667. }
  668. if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) {
  669. $schema_create .= PMA_exportComment(__('Creation') . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])));
  670. $new_crlf = PMA_exportComment() . $crlf;
  671. }
  672. if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) {
  673. $schema_create .= PMA_exportComment(__('Last update') . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])));
  674. $new_crlf = PMA_exportComment() . $crlf;
  675. }
  676. if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) {
  677. $schema_create .= PMA_exportComment(__('Last check') . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])));
  678. $new_crlf = PMA_exportComment() . $crlf;
  679. }
  680. }
  681. PMA_DBI_free_result($result);
  682. }
  683. $schema_create .= $new_crlf;
  684. // no need to generate a DROP VIEW here, it was done earlier
  685. if (! empty($sql_drop_table) && ! PMA_Table::isView($db, $table)) {
  686. $schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table, $sql_backquotes) . ';' . $crlf;
  687. }
  688. // Complete table dump,
  689. // Whether to quote table and column names or not
  690. // Drizzle always quotes names
  691. if (!PMA_DRIZZLE) {
  692. if ($sql_backquotes) {
  693. PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1');
  694. } else {
  695. PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0');
  696. }
  697. }
  698. // I don't see the reason why this unbuffered query could cause problems,
  699. // because SHOW CREATE TABLE returns only one row, and we free the
  700. // results below. Nonetheless, we got 2 user reports about this
  701. // (see bug 1562533) so I remove the unbuffered mode.
  702. //$result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), null, PMA_DBI_QUERY_UNBUFFERED);
  703. //
  704. // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not
  705. // produce a displayable result for the default value of a BIT
  706. // column, nor does the mysqldump command. See MySQL bug 35796
  707. $result = PMA_DBI_try_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table));
  708. // an error can happen, for example the table is crashed
  709. $tmp_error = PMA_DBI_getError();
  710. if ($tmp_error) {
  711. return PMA_exportComment(__('in use') . '(' . $tmp_error . ')');
  712. }
  713. if ($result != false && ($row = PMA_DBI_fetch_row($result))) {
  714. $create_query = $row[1];
  715. unset($row);
  716. // Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases)
  717. if (strpos($create_query, "(\r\n ")) {
  718. $create_query = str_replace("\r\n", $crlf, $create_query);
  719. } elseif (strpos($create_query, "(\n ")) {
  720. $create_query = str_replace("\n", $crlf, $create_query);
  721. } elseif (strpos($create_query, "(\r ")) {
  722. $create_query = str_replace("\r", $crlf, $create_query);
  723. }
  724. /*
  725. * Drop database name from VIEW creation.
  726. *
  727. * This is a bit tricky, but we need to issue SHOW CREATE TABLE with
  728. * database name, but we don't want name to show up in CREATE VIEW
  729. * statement.
  730. */
  731. if ($view) {
  732. $create_query = preg_replace('/' . PMA_backquote($db) . '\./', '', $create_query);
  733. }
  734. // Should we use IF NOT EXISTS?
  735. if (isset($GLOBALS['sql_if_not_exists'])) {
  736. $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
  737. }
  738. // Drizzle (checked on 2011.03.13) returns ROW_FORMAT surrounded with quotes, which is not accepted by parser
  739. if (PMA_DRIZZLE) {
  740. $create_query = preg_replace('/ROW_FORMAT=\'(\S+)\'/', 'ROW_FORMAT=$1', $create_query);
  741. }
  742. // are there any constraints to cut out?
  743. if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) {
  744. // Split the query into lines, so we can easily handle it. We know lines are separated by $crlf (done few lines above).
  745. $sql_lines = explode($crlf, $create_query);
  746. $sql_count = count($sql_lines);
  747. // lets find first line with constraints
  748. for ($i = 0; $i < $sql_count; $i++) {
  749. if (preg_match('@^[\s]*(CONSTRAINT|FOREIGN[\s]+KEY)@', $sql_lines[$i])) {
  750. break;
  751. }
  752. }
  753. // If we really found a constraint
  754. if ($i != $sql_count) {
  755. // remove , from the end of create statement
  756. $sql_lines[$i - 1] = preg_replace('@,$@', '', $sql_lines[$i - 1]);
  757. // prepare variable for constraints
  758. if (!isset($sql_constraints)) {
  759. if (isset($GLOBALS['no_constraints_comments'])) {
  760. $sql_constraints = '';
  761. } else {
  762. $sql_constraints = $crlf
  763. . PMA_exportComment()
  764. . PMA_exportComment(__('Constraints for dumped tables'))
  765. . PMA_exportComment();
  766. }
  767. }
  768. // comments for current table
  769. if (!isset($GLOBALS['no_constraints_comments'])) {
  770. $sql_constraints .= $crlf
  771. . PMA_exportComment()
  772. . PMA_exportComment(__('Constraints for table') . ' ' . PMA_backquote($table))
  773. . PMA_exportComment();
  774. }
  775. // let's do the work
  776. $sql_constraints_query .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;
  777. $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;
  778. $sql_drop_foreign_keys .= 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $crlf;
  779. $first = true;
  780. for ($j = $i; $j < $sql_count; $j++) {
  781. if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j])) {
  782. if (!$first) {
  783. $sql_constraints .= $crlf;
  784. }
  785. if (strpos($sql_lines[$j], 'CONSTRAINT') === false) {
  786. $tmp_str = preg_replace('/(FOREIGN[\s]+KEY)/', 'ADD \1', $sql_lines[$j]);
  787. $sql_constraints_query .= $tmp_str;
  788. $sql_constraints .= $tmp_str;
  789. } else {
  790. $tmp_str = preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]);
  791. $sql_constraints_query .= $tmp_str;
  792. $sql_constraints .= $tmp_str;
  793. preg_match('/(CONSTRAINT)([\s])([\S]*)([\s])/', $sql_lines[$j], $matches);
  794. if (! $first) {
  795. $sql_drop_foreign_keys .= ', ';
  796. }
  797. $sql_drop_foreign_keys .= 'DROP FOREIGN KEY ' . $matches[3];
  798. }
  799. $first = false;
  800. } else {
  801. break;
  802. }
  803. }
  804. $sql_constraints .= ';' . $crlf;
  805. $sql_constraints_query .= ';';
  806. $create_query = implode($crlf, array_slice($sql_lines, 0, $i)) . $crlf . implode($crlf, array_slice($sql_lines, $j, $sql_count - 1));
  807. unset($sql_lines);
  808. }
  809. }
  810. $schema_create .= $create_query;
  811. }
  812. // remove a possible "AUTO_INCREMENT = value" clause
  813. // that could be there starting with MySQL 5.0.24
  814. // in Drizzle it's useless as it contains the value given at table creation time
  815. $schema_create = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $schema_create);
  816. $schema_create .= $auto_increment;
  817. PMA_DBI_free_result($result);
  818. return $schema_create . ($add_semicolon ? ';' . $crlf : '');
  819. } // end of the 'PMA_getTableDef()' function
  820. /**
  821. * Returns $table's comments, relations etc.
  822. *
  823. * @param string $db database name
  824. * @param string $table table name
  825. * @param string $crlf end of line sequence
  826. * @param bool $do_relation whether to include relation comments
  827. * @param bool $do_mime whether to include mime comments
  828. * @return string resulting comments
  829. *
  830. * @access private
  831. */
  832. function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_mime = false)
  833. {
  834. global $cfgRelation;
  835. global $sql_backquotes;
  836. global $sql_constraints;
  837. $schema_create = '';
  838. // Check if we can use Relations
  839. if ($do_relation && !empty($cfgRelation['relation'])) {
  840. // Find which tables are related with the current one and write it in
  841. // an array
  842. $res_rel = PMA_getForeigners($db, $table);
  843. if ($res_rel && count($res_rel) > 0) {
  844. $have_rel = true;
  845. } else {
  846. $have_rel = false;
  847. }
  848. } else {
  849. $have_rel = false;
  850. } // end if
  851. if ($do_mime && $cfgRelation['mimework']) {
  852. if (!($mime_map = PMA_getMIME($db, $table, true))) {
  853. unset($mime_map);
  854. }
  855. }
  856. if (isset($mime_map) && count($mime_map) > 0) {
  857. $schema_create .= PMA_possibleCRLF()
  858. . PMA_exportComment()
  859. . PMA_exportComment(__('MIME TYPES FOR TABLE'). ' ' . PMA_backquote($table, $sql_backquotes) . ':');
  860. @reset($mime_map);
  861. foreach ($mime_map AS $mime_field => $mime) {
  862. $schema_create .= PMA_exportComment(' ' . PMA_backquote($mime_field, $sql_backquotes))
  863. . PMA_exportComment(' ' . PMA_backquote($mime['mimetype'], $sql_backquotes));
  864. }
  865. $schema_create .= PMA_exportComment();
  866. }
  867. if ($have_rel) {
  868. $schema_create .= PMA_possibleCRLF()
  869. . PMA_exportComment()
  870. . PMA_exportComment(__('RELATIONS FOR TABLE'). ' ' . PMA_backquote($table, $sql_backquotes) . ':');
  871. foreach ($res_rel AS $rel_field => $rel) {
  872. $schema_create .= PMA_exportComment(' ' . PMA_backquote($rel_field, $sql_backquotes))
  873. . PMA_exportComment(' ' . PMA_backquote($rel['foreign_table'], $sql_backquotes)
  874. . ' -> ' . PMA_backquote($rel['foreign_field'], $sql_backquotes));
  875. }
  876. $schema_create .= PMA_exportComment();
  877. }
  878. return $schema_create;
  879. } // end of the 'PMA_getTableComments()' function
  880. /**
  881. * Outputs table's structure
  882. *
  883. * @param string $db database name
  884. * @param string $table table name
  885. * @param string $crlf the end of line sequence
  886. * @param string $error_url the url to go back in case of error
  887. * @param bool $relation whether to include relation comments
  888. * @param bool $comments whether to include the pmadb-style column comments
  889. * as comments in the structure; this is deprecated
  890. * but the parameter is left here because export.php
  891. * calls PMA_exportStructure() also for other export
  892. * types which use this parameter
  893. * @param bool $mime whether to include mime comments
  894. * @param bool $dates whether to include creation/update/check dates
  895. * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
  896. * @param string $export_type 'server', 'database', 'table'
  897. * @return bool Whether it succeeded
  898. *
  899. * @access public
  900. */
  901. function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = false, $comments = false, $mime = false, $dates = false, $export_mode, $export_type)
  902. {
  903. $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
  904. ? PMA_backquote($table)
  905. : '\'' . $table . '\'';
  906. $dump = PMA_possibleCRLF()
  907. . PMA_exportComment(str_repeat('-', 56))
  908. . PMA_possibleCRLF()
  909. . PMA_exportComment();
  910. switch($export_mode) {
  911. case 'create_table':
  912. $dump .= PMA_exportComment(__('Table structure for table') . ' ' . $formatted_table_name);
  913. $dump .= PMA_exportComment();
  914. $dump .= PMA_getTableDef($db, $table, $crlf, $error_url, $dates);
  915. $dump .= PMA_getTableComments($db, $table, $crlf, $relation, $mime);
  916. break;
  917. case 'triggers':
  918. $dump = '';
  919. $triggers = PMA_DBI_get_triggers($db, $table);
  920. if ($triggers) {
  921. $dump .= PMA_possibleCRLF()
  922. . PMA_exportComment()
  923. . PMA_exportComment(__('Triggers') . ' ' . $formatted_table_name)
  924. . PMA_exportComment();
  925. $delimiter = '//';
  926. foreach ($triggers as $trigger) {
  927. $dump .= $trigger['drop'] . ';' . $crlf;
  928. $dump .= 'DELIMITER ' . $delimiter . $crlf;
  929. $dump .= $trigger['create'];
  930. $dump .= 'DELIMITER ;' . $crlf;
  931. }
  932. }
  933. break;
  934. case 'create_view':
  935. $dump .= PMA_exportComment(__('Structure for view') . ' ' . $formatted_table_name)
  936. . PMA_exportComment();
  937. // delete the stand-in table previously created (if any)
  938. if ($export_type != 'table') {
  939. $dump .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table) . ';' . $crlf;
  940. }
  941. $dump .= PMA_getTableDef($db, $table, $crlf, $error_url, $dates, true, true);
  942. break;
  943. case 'stand_in':
  944. $dump .= PMA_exportComment(__('Stand-in structure for view') . ' ' . $formatted_table_name)
  945. . PMA_exportComment();
  946. // export a stand-in definition to resolve view dependencies
  947. $dump .= PMA_getTableDefStandIn($db, $table, $crlf);
  948. } // end switch
  949. // this one is built by PMA_getTableDef() to use in table copy/move
  950. // but not in the case of export
  951. unset($GLOBALS['sql_constraints_query']);
  952. return PMA_exportOutputHandler($dump);
  953. }
  954. /**
  955. * Outputs the content of a table in SQL format
  956. *
  957. * @param string $db database name
  958. * @param string $table table name
  959. * @param string $crlf the end of line sequence
  960. * @param string $error_url the url to go back in case of error
  961. * @param string $sql_query SQL query for obtaining data
  962. * @return bool Whether it succeeded
  963. *
  964. * @access public
  965. */
  966. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  967. {
  968. global $sql_backquotes;
  969. global $current_row;
  970. $formatted_table_name = (isset($GLOBALS['sql_backquotes']))
  971. ? PMA_backquote($table)
  972. : '\'' . $table . '\'';
  973. // Do not export data for a VIEW
  974. // (For a VIEW, this is called only when exporting a single VIEW)
  975. if (PMA_Table::isView($db, $table)) {
  976. $head = PMA_possibleCRLF()
  977. . PMA_exportComment()
  978. . PMA_exportComment('VIEW ' . ' ' . $formatted_table_name)
  979. . PMA_exportComment(__('Data') . ': ' . __('None'))
  980. . PMA_exportComment()
  981. . PMA_possibleCRLF();
  982. if (! PMA_exportOutputHandler($head)) {
  983. return false;
  984. }
  985. return true;
  986. }
  987. // analyze the query to get the true column names, not the aliases
  988. // (this fixes an undefined index, also if Complete inserts
  989. // are used, we did not get the true column name in case of aliases)
  990. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query));
  991. $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  992. // a possible error: the table has crashed
  993. $tmp_error = PMA_DBI_getError();
  994. if ($tmp_error) {
  995. return PMA_exportOutputHandler(PMA_exportComment(__('Error reading data:') . ' (' . $tmp_error . ')'));
  996. }
  997. if ($result != false) {
  998. $fields_cnt = PMA_DBI_num_fields($result);
  999. // Get field information
  1000. $fields_meta = PMA_DBI_get_fields_meta($result);
  1001. $field_flags = array();
  1002. for ($j = 0; $j < $fields_cnt; $j++) {
  1003. $field_flags[$j] = PMA_DBI_field_flags($result, $j);
  1004. }
  1005. for ($j = 0; $j < $fields_cnt; $j++) {
  1006. if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
  1007. $field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $sql_backquotes);
  1008. } else {
  1009. $field_set[$j] = PMA_backquote($fields_meta[$j]->name, $sql_backquotes);
  1010. }
  1011. }
  1012. if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') {
  1013. // update
  1014. $schema_insert = 'UPDATE ';
  1015. if (isset($GLOBALS['sql_ignore'])) {
  1016. $schema_insert .= 'IGNORE ';
  1017. }
  1018. // avoid EOL blank
  1019. $schema_insert .= PMA_backquote($table, $sql_backquotes) . ' SET';
  1020. } else {
  1021. // insert or replace
  1022. if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'REPLACE') {
  1023. $sql_command = 'REPLACE';
  1024. } else {
  1025. $sql_command = 'INSERT';
  1026. }
  1027. // delayed inserts?
  1028. if (isset($GLOBALS['sql_delayed'])) {
  1029. $insert_delayed = ' DELAYED';
  1030. } else {
  1031. $insert_delayed = '';
  1032. }
  1033. // insert ignore?
  1034. if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'INSERT' && isset($GLOBALS['sql_ignore'])) {
  1035. $insert_delayed .= ' IGNORE';
  1036. }
  1037. // scheme for inserting fields
  1038. if ($GLOBALS['sql_insert_syntax'] == 'complete' || $GLOBALS['sql_insert_syntax'] == 'both') {
  1039. $fields = implode(', ', $field_set);
  1040. $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_backquotes)
  1041. // avoid EOL blank
  1042. . ' (' . $fields . ') VALUES';
  1043. } else {
  1044. $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $sql_backquotes)
  1045. . ' VALUES';
  1046. }
  1047. }
  1048. $search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
  1049. $replace = array('\0', '\n', '\r', '\Z');
  1050. $current_row = 0;
  1051. $query_size = 0;
  1052. if (($GLOBALS['sql_insert_syntax'] == 'extended' || $GLOBALS['sql_insert_syntax'] == 'both') && (!isset($GLOBALS['sql_type']) || $GLOBALS['sql_type'] != 'UPDATE')) {
  1053. $separator = ',';
  1054. $schema_insert .= $crlf;
  1055. } else {
  1056. $separator = ';';
  1057. }
  1058. while ($row = PMA_DBI_fetch_row($result)) {
  1059. if ($current_row == 0) {
  1060. $head = PMA_possibleCRLF()
  1061. . PMA_exportComment()
  1062. . PMA_exportComment(__('Dumping data for table') . ' ' . $formatted_table_name)
  1063. . PMA_exportComment()
  1064. . $crlf;
  1065. if (! PMA_exportOutputHandler($head)) {
  1066. return false;
  1067. }
  1068. }
  1069. $current_row++;
  1070. for ($j = 0; $j < $fields_cnt; $j++) {
  1071. // NULL
  1072. if (!isset($row[$j]) || is_null($row[$j])) {
  1073. $values[] = 'NULL';
  1074. // a number
  1075. // timestamp is numeric on some MySQL 4.1, BLOBs are sometimes numeric

Large files files are truncated, but you can click here to view the full file