PHPSQLParser\PHPSQLParser::parse PHP Method

parse() public method

It parses the given SQL statement and generates a detailled output array for every part of the statement. The method can also generate [position] fields within the output, which hold the character position for every statement part. The calculation of the positions needs some time, if you don't need positions in your application, set the parameter to false.
public parse ( String $sql, boolean $calcPositions = false ) : array
$sql String The SQL statement.
$calcPositions boolean True, if the output should contain [position], false otherwise.
return array An associative array with all meta information about the SQL statement.
    public function parse($sql, $calcPositions = false)
    {
        $processor = new DefaultProcessor($this->options);
        $queries = $processor->process($sql);
        // calc the positions of some important tokens
        if ($calcPositions) {
            $calculator = new PositionCalculator();
            $queries = $calculator->setPositionsWithinSQL($sql, $queries);
        }
        // store the parsed queries
        $this->parsed = $queries;
        return $this->parsed;
    }

Usage Example

Esempio n. 1
0
 /**
  * @throws \Exception
  *
  * @return string
  */
 public function convert()
 {
     $parsed = $this->sqlParser->parse($this->sql);
     if (false === $parsed) {
         throw new \Exception('SQL query is not valid');
     }
     $results = [];
     foreach ($parsed as $section => $data) {
         if ($this->converterFactory->canCreate($section)) {
             $converter = $this->converterFactory->create($section);
             $results = array_merge($results, $converter->convert($data));
         }
     }
     $table = current(array_filter($results, function ($item) {
         return 'table' === $item['name'];
     }));
     unset($results[array_search($table, $results)]);
     array_unshift($results, $table);
     foreach ($results as $function) {
         $args = isset($function['args']) ? $function['args'] : [];
         $this->generator->addFunction($function['name'], $args);
     }
     $this->generator->addFunction('get');
     return $this->generator->generate();
 }
All Usage Examples Of PHPSQLParser\PHPSQLParser::parse