<?php


class DB_Dump {


	function get_db_structure_by_table_prefix(&$db, $table_prefix) {
		$structure = '';
		$res=$db->show_tables();
//		$res = $db->query('SHOW TABLES');

		if ($db->num_rows($res) == 0) {
			return false;
		}
		while ($record = $db->fetch_row($res)) {
				//	DEBUG_S(-3,'res',$record,__FILE__,__FUNCTION__,__LINE__);

			if ($table_prefix == '' || $table_prefix == '%' || $table_prefix == substr($record[0], 0, strlen($table_prefix))) {
				$table_struct = self::get_table_structure($db, $record[0]);
				if ($table_struct === false) {
					// TODO: get_table_structure fail
				} else {
					$structure .= $table_struct;
				}
			}
		}

		return $structure;
	}

	function get_db_structure(&$db, $tables = array()) {
		$structure = '';
		//$res = $db->query('SHOW TABLES');
		$res=$db->show_tables();

		if ($db->num_rows($res) == 0) {
			return false;
		}
		while ($record = $db->fetch_row($res)) {
			if (empty($tables) || in_array($record[0], $tables)) {
				$table_struct = self::get_table_structure($db, $record[0]);
				if ($table_struct === false) {
					// TODO: get_table_structure fail
				} else {
					$structure .= $table_struct;
				}
			}
		}
		return $structure;
  }

  function get_table_structure(&$db, $table) {
		// Structure Header
		$structure = "-- \n";
		$structure .= "-- Table structure for table `{$table}` \n";
		$structure .= "-- \n\n";
		// Dump Structure
		//$structure .= 'DROP TABLE IF EXISTS `'.$table.'`;'."\n";
		// Create sql
		$structure .= "CREATE TABLE `".$table."` (\n";
		$sql_table_fields = array();
//		$records = $db->query('SHOW FIELDS FROM `'.$table.'`');
		$records = $db->describe_table($table);
		
		if ($db->num_rows($records) == 0) {
			return false;
		}
		while ($record = $db->fetch_assoc($records)) {
			$curr_sql_field = "";
			$curr_sql_field .= "`" . $record['Field'] . "` " . $record['Type'];
			if (!empty($record['Default'])) {
				if (substr($record['Default'], 0, strlen('CURRENT_TIMESTAMP')) == 'CURRENT_TIMESTAMP') {
					$curr_sql_field .= " DEFAULT " . $record['Default'];
				} else {
					$curr_sql_field .= " DEFAULT '" . $record['Default'] . "'";
				}
			}
			if (@strcmp($record['Null'], 'YES') != 0) {
				$curr_sql_field .= " NOT NULL";
			}
			if (!empty($record['Extra'])) {
				$curr_sql_field .= " " . $record['Extra'];
			}
			$sql_table_fields[] = $curr_sql_field;
		}
		$structure .= (!empty($sql_table_fields))? implode(",\n", $sql_table_fields) : "";
		//$structure = @ereg_replace(",\n$", null, $structure);

		// Save all Column Indexes
		$table_keys = self::get_sql_keys_table($db, $table);
		if ($table_keys === false) {
			// TODO: get_sql_keys_table fail
		} else {
			$structure .= ",\n" . $table_keys;
		}
		$structure .= "\n)";

		//Save table engine
		//$records = $db->query("SHOW TABLE STATUS LIKE '".$table."'");
		$record = $db->SHOW_TABLE_STATUS_LIKE($table);
		//if ($record = $db->fetch_assoc($records)) {
		if ($record) {
			if (!empty($record['Engine'])) {
				$structure .= ' ENGINE='.$record['Engine'];
			}
			if (!empty($record['Auto_increment'])) {
				$structure .= ' AUTO_INCREMENT='.$record['Auto_increment'];
			}
		}

		$structure .= ";\n\n-- --------------------------------------------------------\n\n";
		return $structure;
	}

	function get_sql_keys_table(&$db, $table) {
		$ret_keys = array();
		$primary = "";
		$unique = array();
		$index = array();
		$fulltext = array();
		$res = $db->query("SHOW KEYS FROM `" . $table . "`");
		if ($db->num_rows($res) == 0) {
			return false;
		}
		while ($row = $db->fetch($res)) {
			if ($row->Key_name == 'PRIMARY') {
				if ($row->Index_type == 'BTREE') {
					$ret_keys["PRIMARY KEY"][] = "`" . $row->Column_name . "`";
				}
			}
			else {
				if ($row->Non_unique == '0' && $row->Index_type == 'BTREE') {
					$ret_keys["UNIQUE KEY `" . $row->Key_name . "`"][] = "`" . $row->Column_name . "`";
				}
				if ($row->Non_unique == '1' && $row->Index_type == 'BTREE') {
					$ret_keys["KEY `" . $row->Key_name . "`"][] = "`" . $row->Column_name . "`";
				}
				if ($row->Non_unique == '1' && $row->Index_type == 'FULLTEXT') {
					$ret_keys["FULLTEXT `" . $row->Key_name . "`"][] = "`" . $row->Column_name . "`";
				}
			}
		}
		$out_keys = array();
		if (!empty($ret_keys)) {
			foreach ($ret_keys as $key => $val) {
				$out_keys[] = $key . " (" . implode(", ", $val) . ")";
			}
		}
		$out_keys = (!empty($out_keys))? implode(",\n", $out_keys) : '';
		return $out_keys;
	}

}
