2011-06-17 21:06:54 +00:00
|
|
|
<?php
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
|
|
|
* http://www.mysqldumper.net
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage SQL-Browser
|
2011-06-19 13:47:44 +00:00
|
|
|
* @version SVN: $Rev$
|
|
|
|
* @author $Author$
|
2011-06-18 11:10:40 +00:00
|
|
|
*/
|
2011-06-17 21:06:54 +00:00
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
require_once "Msd/Sql/Parser/Interface.php";
|
|
|
|
/**
|
|
|
|
* Class to parse MySQL queries.
|
|
|
|
* This enables you to analyze and modify MySQL queries, which the user has entered.
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage SQL-Browser
|
|
|
|
*/
|
2011-06-19 13:57:40 +00:00
|
|
|
class Msd_Sql_Parser implements Iterator
|
2011-06-17 21:06:54 +00:00
|
|
|
{
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* Saves the raw MySQL Query.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-06-17 21:06:54 +00:00
|
|
|
private $_rawQuery = null;
|
|
|
|
|
2011-06-19 13:47:44 +00:00
|
|
|
/**
|
|
|
|
* Parsed MySQL statements.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_parsedStatements = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the summary of the parsing process.
|
|
|
|
* The summary contains the count of each statement in the query.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_parsingSummary = array();
|
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* MySQL comment types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-06-17 21:06:54 +00:00
|
|
|
protected $_sqlComments = array(
|
2011-06-19 14:42:30 +00:00
|
|
|
'--' => "\n", '/*' => '*/', '/*!' => '*/'
|
2011-06-17 21:06:54 +00:00
|
|
|
);
|
|
|
|
|
2011-06-19 14:42:30 +00:00
|
|
|
/**
|
2011-06-19 15:43:36 +00:00
|
|
|
* Whether to save debug output
|
|
|
|
*
|
2011-06-19 14:42:30 +00:00
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $_debug = false;
|
|
|
|
|
2011-06-19 15:43:36 +00:00
|
|
|
/**
|
|
|
|
* Debug output buffer
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $_debugOutput = '';
|
|
|
|
|
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* Class constructor.
|
|
|
|
* Creates a new instance of the MySQL parser and optionally assign the raw MySQL query.
|
|
|
|
*
|
|
|
|
* @param string $sqlQuery Raw MySQL query to parse
|
2011-06-19 14:42:30 +00:00
|
|
|
* @param bool $debug If turned on, detection of queries is logged
|
2011-06-18 11:10:40 +00:00
|
|
|
*/
|
2011-06-19 14:42:30 +00:00
|
|
|
public function __construct($sqlQuery = null, $debug = false)
|
2011-06-17 21:06:54 +00:00
|
|
|
{
|
|
|
|
if ($sqlQuery !== null) {
|
|
|
|
$this->_rawQuery = $sqlQuery;
|
|
|
|
}
|
2011-06-19 14:42:30 +00:00
|
|
|
$this->_debug = $debug;
|
2011-06-17 21:06:54 +00:00
|
|
|
}
|
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* Parses a raw MySQL query.
|
|
|
|
* This could include more than one MySQL statement.
|
|
|
|
*
|
|
|
|
* @throws Msd_Sql_Parser_Exception
|
|
|
|
*
|
|
|
|
* @param string $sqlQuery Raw MySQL query to parse
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-06-17 21:06:54 +00:00
|
|
|
public function parse($sqlQuery = null)
|
|
|
|
{
|
|
|
|
if ($sqlQuery === null) {
|
|
|
|
if ($this->_rawQuery === null) {
|
|
|
|
throw new Msd_Sql_Parser_Exception('You must specify a MySQL query for parsing!');
|
|
|
|
}
|
|
|
|
$sqlQuery = $this->_rawQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sqlQuery = trim($sqlQuery);
|
|
|
|
|
|
|
|
$statementCounter = 0;
|
|
|
|
$startPos = 0;
|
2011-06-19 15:43:36 +00:00
|
|
|
$queryLength = strlen($sqlQuery);
|
|
|
|
while ($startPos < $queryLength) {
|
2011-06-17 21:06:54 +00:00
|
|
|
$statementCounter++;
|
2011-06-19 15:43:36 +00:00
|
|
|
// move pointer to the next character we can interprete
|
2011-06-19 16:10:32 +00:00
|
|
|
while ($startPos < $queryLength && in_array($sqlQuery[$startPos], array(' ', "\n", "\r"))) {
|
2011-06-19 15:43:36 +00:00
|
|
|
$startPos++;
|
|
|
|
}
|
|
|
|
|
2011-06-17 21:06:54 +00:00
|
|
|
$firstSpace = strpos($sqlQuery, ' ', $startPos);
|
|
|
|
$statement = trim(strtolower(substr($sqlQuery, $startPos, $firstSpace - $startPos)));
|
|
|
|
$lengthCheck = strlen($statement);
|
|
|
|
if ($lengthCheck == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($lengthCheck == 1 || $statement{1} == ';' || $statement{1} == "\n") {
|
|
|
|
$startPos = $startPos + 1;
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-19 14:42:30 +00:00
|
|
|
// check for comments or conditional comments
|
2011-06-17 21:06:54 +00:00
|
|
|
$commentCheck = substr($statement, 0, 2);
|
2011-06-19 14:42:30 +00:00
|
|
|
if (isset($this->_sqlComments[$commentCheck]) || substr($statement, 0, 3) == '/*!') {
|
2011-06-17 21:06:54 +00:00
|
|
|
$commentEnd = $this->_sqlComments[$commentCheck];
|
2011-06-19 13:47:44 +00:00
|
|
|
$endPos = strpos($sqlQuery, $commentEnd, $startPos) + strlen($commentEnd);
|
|
|
|
$comment = substr($sqlQuery, $startPos, $endPos - $startPos);
|
2011-06-19 13:57:40 +00:00
|
|
|
$this->_parseStatement($comment, 'Msd_Sql_Parser_Statement_Comment');
|
2011-06-19 15:43:36 +00:00
|
|
|
$startPos = $endPos+1;
|
2011-06-17 21:06:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-06-19 14:42:30 +00:00
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
$parserClass = 'Msd_Sql_Parser_Statement_' . ucwords($statement);
|
2011-06-17 21:06:54 +00:00
|
|
|
$endPos = $this->_getStatementEndPos($sqlQuery, $startPos);
|
|
|
|
$completeStatement = trim(substr($sqlQuery, $startPos, $endPos - $startPos));
|
|
|
|
$startPos = $endPos + 1;
|
2011-06-19 15:43:36 +00:00
|
|
|
$foundStatement = $this->_parseStatement($completeStatement, $parserClass);
|
|
|
|
if ($this->_debug) {
|
|
|
|
$this->_debugOutput .= '<br />Extracted statement: '.$foundStatement;
|
|
|
|
}
|
|
|
|
$this->_parsedStatements[] = $foundStatement;
|
|
|
|
// increment query type counter
|
2011-06-19 13:47:44 +00:00
|
|
|
if (!isset($this->_parsingSummary[$statement])) {
|
|
|
|
$this->_parsingSummary[$statement] = 0;
|
|
|
|
}
|
|
|
|
$this->_parsingSummary[$statement]++;
|
2011-06-17 21:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-18 11:10:40 +00:00
|
|
|
/**
|
|
|
|
* Searches the end of a MySQL statement and returns its position.
|
|
|
|
*
|
|
|
|
* @param string $sqlQuery The complete MySQL query
|
|
|
|
* @param int $startPos Index, where to start the search.
|
|
|
|
*
|
|
|
|
* @return int End position of the statement
|
|
|
|
*/
|
2011-06-17 21:06:54 +00:00
|
|
|
private function _getStatementEndPos($sqlQuery, $startPos = 0)
|
|
|
|
{
|
|
|
|
$nextString = strpos($sqlQuery, "'", $startPos);
|
|
|
|
$nextSemicolon = strpos($sqlQuery, ';', $startPos);
|
|
|
|
if ($nextString === false) {
|
|
|
|
if ($nextSemicolon === false) {
|
|
|
|
return strlen($sqlQuery);
|
|
|
|
}
|
|
|
|
|
2011-06-19 15:43:36 +00:00
|
|
|
return $nextSemicolon+1;
|
2011-06-17 21:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ($nextString < $nextSemicolon) {
|
|
|
|
$nextString = strpos($sqlQuery, "'", $nextString + 1);
|
|
|
|
$nextSemicolon = strpos($sqlQuery, ';', $nextString + 1);
|
|
|
|
$nextString = strpos($sqlQuery, "'", $nextString + 1);
|
|
|
|
if ($nextString === false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nextSemicolon;
|
|
|
|
}
|
2011-06-18 11:10:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of a statement parser class and invokes statement parsing.
|
|
|
|
*
|
2011-06-19 16:10:32 +00:00
|
|
|
* @throws Msd_Sql_Parser_Exception
|
|
|
|
*
|
2011-06-18 11:10:40 +00:00
|
|
|
* @param string $statement MySQL statement to parse
|
|
|
|
* @param string $parserClass Parser class to use
|
|
|
|
*
|
2011-06-19 13:47:44 +00:00
|
|
|
* @return array
|
2011-06-18 11:10:40 +00:00
|
|
|
*/
|
|
|
|
private function _parseStatement($statement, $parserClass)
|
|
|
|
{
|
2011-06-19 15:43:36 +00:00
|
|
|
try {
|
|
|
|
$parserObject = new $parserClass;
|
|
|
|
} catch (Exception $e) {
|
2011-06-19 16:10:32 +00:00
|
|
|
throw new Msd_Sql_Parser_Exception("Can't suitable parser for the statement!");
|
2011-06-18 11:10:40 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 13:47:44 +00:00
|
|
|
return $parserObject->parse($statement);
|
|
|
|
}
|
|
|
|
|
2011-06-19 13:57:40 +00:00
|
|
|
/**
|
|
|
|
* Returns the array with the parsed statements.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getParsedStatements()
|
|
|
|
{
|
|
|
|
return $this->_parsedStatements;
|
|
|
|
}
|
|
|
|
|
2011-06-19 13:47:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the parsing summary.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSummary()
|
|
|
|
{
|
|
|
|
return $this->_parsingSummary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewind (reset) the internal pointer position af the parsed statements array.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
return reset($this->_parsedStatements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current value af the parsed statements array.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return current($this->_parsedStatements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current key af the parsed statements array.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return key($this->_parsedStatements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the internal pointer af the parsed statements array to the next position.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
return next($this->_parsedStatements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the internal pointer position af the parsed statements array.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function valid()
|
|
|
|
{
|
|
|
|
return key($this->_parsedStatements) !== null;
|
2011-06-18 11:10:40 +00:00
|
|
|
}
|
2011-06-19 15:43:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get debug output buffer
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDebugOutput()
|
|
|
|
{
|
|
|
|
return $this->_debugOutput;
|
|
|
|
}
|
2011-06-17 21:06:54 +00:00
|
|
|
}
|