PMA\libraries\Tracker::handleQuery PHP Method

handleQuery() public static method

Analyzes a given SQL statement and saves tracking data.
public static handleQuery ( string $query ) : void
$query string a SQL query
return void
    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);
            }
        }
    }

Usage Example

 /**
  * runs a query and returns the result
  *
  * @param string  $query               query to run
  * @param object  $link                mysql link resource
  * @param integer $options             query options
  * @param bool    $cache_affected_rows whether to cache affected row
  *
  * @return mixed
  */
 public function tryQuery($query, $link = null, $options = 0, $cache_affected_rows = true)
 {
     $link = $this->getLink($link);
     if ($link === false) {
         return false;
     }
     if ($GLOBALS['cfg']['DBG']['sql']) {
         $time = microtime(true);
     }
     $result = $this->_extension->realQuery($query, $link, $options);
     if ($cache_affected_rows) {
         $GLOBALS['cached_affected_rows'] = $this->affectedRows($link, false);
     }
     if ($GLOBALS['cfg']['DBG']['sql']) {
         $time = microtime(true) - $time;
         $this->_dbgQuery($query, $link, $result, $time);
     }
     if (!empty($result) && Tracker::isActive()) {
         Tracker::handleQuery($query);
     }
     return $result;
 }
All Usage Examples Of PMA\libraries\Tracker::handleQuery