private function extractTablesToConvert($query, &$tables)
{
// Normalize the whitespace of the query
$query = trim($query);
$query = str_replace(array("\r\n", "\r", "\n"), ' ', $query);
while (strstr($query, ' ') !== false) {
$query = str_replace(' ', ' ', $query);
}
// Is it a create table query?
$queryStart = substr($query, 0, 12);
$queryStart = strtoupper($queryStart);
if ($queryStart != 'CREATE TABLE') {
return;
}
// Remove the CREATE TABLE keyword. Also, If there's an IF NOT EXISTS clause remove it.
$query = substr($query, 12);
$query = str_ireplace('IF NOT EXISTS', '', $query);
$query = trim($query);
// Make sure there is a space between the table name and its definition, denoted by an open parenthesis
$query = str_replace('(', ' (', $query);
// Now we should have the name of the table, a space and the rest of the query. Extract the table name.
$parts = explode(' ', $query, 2);
$tableName = $parts[0];
/**
* The table name may be quoted. Since UTF8MB4 is only supported in MySQL, the table name can only be
* quoted with surrounding backticks. Therefore we can trim backquotes from the table name to unquote it!
**/
$tableName = trim($tableName, '`');
// Finally, add the table name to $tables if it doesn't already exist.
if (!in_array($tableName, $tables)) {
$tables[] = $tableName;
}
}