DB_Dump.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. class DB_Dump {
  3. function get_db_structure_by_table_prefix( &$db, $table_prefix ) {
  4. $structure = '';
  5. $res=$db->show_tables();
  6. // $res = $db->query('SHOW TABLES');
  7. if ($db->num_rows($res) == 0) {
  8. return false;
  9. }
  10. while ($record = $db->fetch_row($res)) {
  11. // DEBUG_S(-3,'res',$record,__FILE__,__FUNCTION__,__LINE__);
  12. if ($table_prefix == '' || $table_prefix == '%' || $table_prefix == substr($record[0], 0, strlen($table_prefix))) {
  13. $table_struct = self::get_table_structure($db, $record[0]);
  14. if ($table_struct === false) {
  15. // TODO: get_table_structure fail
  16. } else {
  17. $structure .= $table_struct;
  18. }
  19. }
  20. }
  21. return $structure;
  22. }
  23. function get_db_structure( &$db, $tables = array() ) {
  24. $structure = '';
  25. //$res = $db->query('SHOW TABLES');
  26. $res=$db->show_tables();
  27. if ($db->num_rows($res) == 0) {
  28. return false;
  29. }
  30. while ($record = $db->fetch_row($res)) {
  31. if (empty($tables) || in_array($record[0], $tables)) {
  32. $table_struct = self::get_table_structure($db, $record[0]);
  33. if ($table_struct === false) {
  34. // TODO: get_table_structure fail
  35. } else {
  36. $structure .= $table_struct;
  37. }
  38. }
  39. }
  40. return $structure;
  41. }
  42. function get_table_structure( &$db, $table ) {
  43. // Structure Header
  44. $structure = "-- \n";
  45. $structure .= "-- Table structure for table `{$table}` \n";
  46. $structure .= "-- \n\n";
  47. // Dump Structure
  48. //$structure .= 'DROP TABLE IF EXISTS `'.$table.'`;'."\n";
  49. // Create sql
  50. $structure .= "CREATE TABLE `".$table."` (\n";
  51. $sql_table_fields = array();
  52. // $records = $db->query('SHOW FIELDS FROM `'.$table.'`');
  53. $records = $db->describe_table($table);
  54. if ($db->num_rows($records) == 0) {
  55. return false;
  56. }
  57. while ($record = $db->fetch_assoc($records)) {
  58. $curr_sql_field = "";
  59. $curr_sql_field .= "`" . $record['Field'] . "` " . $record['Type'];
  60. if (!empty($record['Default'])) {
  61. if (substr($record['Default'], 0, strlen('CURRENT_TIMESTAMP')) == 'CURRENT_TIMESTAMP') {
  62. $curr_sql_field .= " DEFAULT " . $record['Default'];
  63. } else {
  64. $curr_sql_field .= " DEFAULT '" . $record['Default'] . "'";
  65. }
  66. }
  67. if (@strcmp($record['Null'], 'YES') != 0) {
  68. $curr_sql_field .= " NOT NULL";
  69. }
  70. if (!empty($record['Extra'])) {
  71. $curr_sql_field .= " " . $record['Extra'];
  72. }
  73. $sql_table_fields []= $curr_sql_field;
  74. }
  75. $structure .= (!empty($sql_table_fields))? implode(",\n", $sql_table_fields) : "";
  76. //$structure = @ereg_replace(",\n$", null, $structure);
  77. // Save all Column Indexes
  78. $table_keys = self::get_sql_keys_table($db, $table);
  79. if ($table_keys === false) {
  80. // TODO: get_sql_keys_table fail
  81. } else {
  82. $structure .= ",\n" . $table_keys;
  83. }
  84. $structure .= "\n)";
  85. //Save table engine
  86. //$records = $db->query("SHOW TABLE STATUS LIKE '".$table."'");
  87. $record = $db->SHOW_TABLE_STATUS_LIKE($table);
  88. //if ($record = $db->fetch_assoc($records)) {
  89. if ($record) {
  90. if (!empty($record['Engine'])) {
  91. $structure .= ' ENGINE='.$record['Engine'];
  92. }
  93. if (!empty($record['Auto_increment'])) {
  94. $structure .= ' AUTO_INCREMENT='.$record['Auto_increment'];
  95. }
  96. }
  97. $structure .= ";\n\n-- --------------------------------------------------------\n\n";
  98. return $structure;
  99. }
  100. function get_sql_keys_table( &$db, $table ) {
  101. $ret_keys = array();
  102. $primary = "";
  103. $unique = array();
  104. $index = array();
  105. $fulltext = array();
  106. $res = $db->query("SHOW KEYS FROM `" . $table . "`");
  107. if ($db->num_rows($res) == 0) {
  108. return false;
  109. }
  110. while ($row = $db->fetch($res)) {
  111. if ($row->Key_name == 'PRIMARY') {
  112. if ($row->Index_type == 'BTREE') {
  113. $ret_keys ["PRIMARY KEY"] []= "`" . $row->Column_name . "`";
  114. }
  115. }
  116. else {
  117. if ($row->Non_unique == '0' && $row->Index_type == 'BTREE') {
  118. $ret_keys ["UNIQUE KEY `" . $row->Key_name . "`"] []= "`" . $row->Column_name . "`";
  119. }
  120. if ($row->Non_unique == '1' && $row->Index_type == 'BTREE') {
  121. $ret_keys ["KEY `" . $row->Key_name . "`"] []= "`" . $row->Column_name . "`";
  122. }
  123. if ($row->Non_unique == '1' && $row->Index_type == 'FULLTEXT') {
  124. $ret_keys ["FULLTEXT `" . $row->Key_name . "`"] []= "`" . $row->Column_name . "`";
  125. }
  126. }
  127. }
  128. $out_keys = array();
  129. if (!empty($ret_keys)) {
  130. foreach ($ret_keys as $key => $val) {
  131. $out_keys []= $key . " (" . implode(", ", $val) . ")";
  132. }
  133. }
  134. $out_keys = (!empty($out_keys))? implode(",\n", $out_keys) : '';
  135. return $out_keys;
  136. }
  137. }