public static function handleQuery($query)
{
// If query is marked as untouchable, leave
if (mb_strstr($query, "/*NOTRACK*/")) {
return;
}
if (!(substr($query, -1) == ';')) {
$query = $query . ";\n";
}
// Get some information about query
$result = self::parseQuery($query);
// Get database name
$dbname = trim(isset($GLOBALS['db']) ? $GLOBALS['db'] : '', '`');
// $dbname can be empty, for example when coming from Synchronize
// and this is a query for the remote server
if (empty($dbname)) {
return;
}
// If we found a valid statement
if (isset($result['identifier'])) {
$version = self::getVersion($dbname, $result['tablename'], $result['identifier']);
// If version not exists and auto-creation is enabled
if ($GLOBALS['cfg']['Server']['tracking_version_auto_create'] == true && self::isTracked($dbname, $result['tablename']) == false && $version == -1) {
// Create the version
switch ($result['identifier']) {
case 'CREATE TABLE':
self::createVersion($dbname, $result['tablename'], '1');
break;
case 'CREATE VIEW':
self::createVersion($dbname, $result['tablename'], '1', '', true);
break;
case 'CREATE DATABASE':
self::createDatabaseVersion($dbname, '1', $query);
break;
}
// end switch
}
// If version exists
if (self::isTracked($dbname, $result['tablename']) && $version != -1) {
if ($result['type'] == 'DDL') {
$save_to = 'schema_sql';
} elseif ($result['type'] == 'DML') {
$save_to = 'data_sql';
} else {
$save_to = '';
}
$date = date('Y-m-d H:i:s');
// Cut off `dbname`. from query
$query = preg_replace('/`' . preg_quote($dbname, '/') . '`\\s?\\./', '', $query);
// Add log information
$query = self::getLogComment() . $query;
// Mark it as untouchable
$sql_query = " /*NOTRACK*/\n" . " UPDATE " . self::_getTrackingTable() . " SET " . Util::backquote($save_to) . " = CONCAT( " . Util::backquote($save_to) . ",'\n" . $GLOBALS['dbi']->escapeString($query) . "') ," . " `date_updated` = '" . $date . "' ";
// If table was renamed we have to change
// the tablename attribute in pma_tracking too
if ($result['identifier'] == 'RENAME TABLE') {
$sql_query .= ', `table_name` = \'' . $GLOBALS['dbi']->escapeString($result['tablename_after_rename']) . '\' ';
}
// Save the tracking information only for
// 1. the database
// 2. the table / view
// 3. the statements
// we want to track
$sql_query .= " WHERE FIND_IN_SET('" . $result['identifier'] . "',tracking) > 0" . " AND `db_name` = '" . $GLOBALS['dbi']->escapeString($dbname) . "' " . " AND `table_name` = '" . $GLOBALS['dbi']->escapeString($result['tablename']) . "' " . " AND `version` = '" . $GLOBALS['dbi']->escapeString($version) . "' ";
PMA_queryAsControlUser($sql_query);
}
}
}