Author: Dan Ungureanu (udan1107@gmail.com)
示例#1
0
 /**
  * Get url sql query without conditions to shorten URLs
  *
  * @param array $analyzed_sql_results analyzed sql results
  *
  * @return  string  $url_sql        analyzed sql query
  *
  * @access  private
  *
  * @see     _getTableBody()
  */
 private function _getUrlSqlQuery($analyzed_sql_results)
 {
     if ($analyzed_sql_results['querytype'] != 'SELECT' || mb_strlen($this->__get('sql_query')) < 200) {
         return $this->__get('sql_query');
     }
     $query = 'SELECT ' . Query::getClause($analyzed_sql_results['statement'], $analyzed_sql_results['parser']->list, 'SELECT');
     $from_clause = Query::getClause($analyzed_sql_results['statement'], $analyzed_sql_results['parser']->list, 'FROM');
     if (!empty($from_clause)) {
         $query .= ' FROM ' . $from_clause;
     }
     return $query;
 }
示例#2
0
 public function testGetFirstStatement()
 {
     $query = 'USE saki';
     $delimiter = null;
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals(null, $statement);
     $this->assertEquals('USE saki', $query);
     $query = 'USE sakila; ' . '/*test comment*/' . 'SELECT * FROM actor; ' . 'DELIMITER $$ ' . 'UPDATE actor SET last_name = "abc"$$' . '/*!SELECT * FROM actor WHERE last_name = "abc"*/$$';
     $delimiter = null;
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals('USE sakila;', $statement);
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals('SELECT * FROM actor;', $statement);
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals('DELIMITER $$', $statement);
     $this->assertEquals('$$', $delimiter);
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals('UPDATE actor SET last_name = "abc"$$', $statement);
     list($statement, $query, $delimiter) = Query::getFirstStatement($query, $delimiter);
     $this->assertEquals('SELECT * FROM actor WHERE last_name = "abc"$$', $statement);
 }
示例#3
0
 public function testReplaceClauseOnlyKeyword()
 {
     $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10');
     $this->assertEquals(' SELECT SQL_CALC_FOUND_ROWS *, (SELECT 1) FROM film LIMIT 0, 10', Query::replaceClause($parser->statements[0], $parser->list, 'SELECT SQL_CALC_FOUND_ROWS', true));
 }
示例#4
0
 /**
  * Validates the order of the clauses in parsed statement
  * Ideally this should be called after successfully
  * completing the parsing of each statement
  *
  * @param Parser     $parser The instance that requests parsing.
  * @param TokensList $list   The list of tokens to be parsed.
  *
  * @return boolean
  */
 public function validateClauseOrder($parser, $list)
 {
     $clauses = array_flip(array_keys($this->getClauses()));
     if (empty($clauses) || count($clauses) == 0) {
         return true;
     }
     $minIdx = -1;
     /**
      * For tracking JOIN clauses in a query
      *   0 - JOIN not found till now
      *   1 - JOIN has been found
      *   2 - A Non-JOIN clause has been found
      *       after a previously found JOIN clause
      *
      * @var int $joinStart
      */
     $joinStart = 0;
     $error = 0;
     foreach ($clauses as $clauseType => $index) {
         $clauseStartIdx = Utils\Query::getClauseStartOffset($this, $list, $clauseType);
         // Handle ordering of Multiple Joins in a query
         if ($clauseStartIdx != -1) {
             if ($joinStart == 0 && stripos($clauseType, 'JOIN')) {
                 $joinStart = 1;
             } elseif ($joinStart == 1 && !stripos($clauseType, 'JOIN')) {
                 $joinStart = 2;
             } elseif ($joinStart == 2 && stripos($clauseType, 'JOIN')) {
                 $error = 1;
             }
         }
         if ($clauseStartIdx != -1 && $clauseStartIdx < $minIdx) {
             if ($joinStart == 0 || $joinStart == 2 && ($error = 1)) {
                 $token = $list->tokens[$clauseStartIdx];
                 $parser->error(__('Unexpected ordering of clauses.'), $token);
                 return false;
             } else {
                 $minIdx = $clauseStartIdx;
             }
         } elseif ($clauseStartIdx != -1) {
             $minIdx = $clauseStartIdx;
         }
     }
     return true;
 }