function read_sql($infile) { $create_list = array(); $change_info = array(); $file_content = file_get_contents($infile, false, null); // Strip out MySQL style comments $file_content = preg_replace('/#.+/', '', $file_content); $file_content = preg_replace('/--.+/', '', $file_content); $ddl_full = trim($file_content); $ddl_list = preg_split("/;/", $ddl_full); foreach ($ddl_list as $ddl) { $ddl = trim($ddl); if ($ddl == '') { continue; } $res = array(); if (!preg_match("/^(?P<mode>create|alter) table `?(?P<name>\\w+)`?/i", $ddl, $res)) { print "ERROR: {$infile} does not contain ALTER or CREATE statement:\n" . "{$create}\n"; return false; } if (strtolower($res['mode']) == 'create') { $ddl = standardize_create($ddl); } $change_info[] = array('table_name' => $res['name'], 'ddl' => $ddl, 'mode' => strtolower($res['mode'])); } return $change_info; }
function show_table_schema($host, $db, $table, $verbose = false) { global $db_admin_user, $db_admin_pass; $max_tries = 10; $counter = 0; while ($counter < $max_tries) { $conn = mysql_connect($host, $db_admin_user, $db_admin_pass); if (!$conn) { if ($verbose) { echo "Could not connect to {$host}\n - " . mysql_error() . "\n"; } $counter++; continue; } $result = mysql_select_db($db, $conn); if (!$result) { if ($verbose) { echo "Could not select {$db} on {$host}\n - " . mysql_error() . "\n"; } $counter++; continue; } $sql = "SELECT COUNT(*) AS cnt\n FROM information_schema.TABLES\n WHERE TABLE_SCHEMA = '{$db}' AND\n TABLE_NAME = '{$table}'"; $result = mysql_query($sql, $conn); if (!$result) { if ($verbose) { echo "Could not query i_s on {$host}\n - " . mysql_error() . "\n"; } $counter++; continue; } $row = mysql_fetch_array($result); if (0 == $row['cnt']) { if ($verbose) { echo "Table {$db}.{$table} does not exist on {$host}\n"; } return false; } $sql = "SHOW CREATE TABLE " . $table; $result = mysql_query($sql, $conn); if (!$result) { if ($verbose) { echo "Could not run '" . $sql . "' in {$db} on {$host}\n - " . mysql_error() . "\n"; } $counter++; continue; } else { while ($row = mysql_fetch_array($result)) { $table_info['name'] = $row['Table']; $table_info['schema'] = standardize_create($row['Create Table']); } return $table_info; } } return false; }