From 55aa7889e52c7db63b714f238f8d4a2769f0303e Mon Sep 17 00:00:00 2001 From: D4rk4ng3l Date: Sat, 18 Jun 2011 11:10:40 +0000 Subject: [PATCH] Added basic structure of MySQL statement parsers. --- library/Msd/Sql/Parser.php | 81 ++++++++++++++++++++- library/Msd/Sql/Parser/Exception.php | 16 ++++ library/Msd/Sql/Parser/Interface.php | 30 ++++++++ library/Msd/Sql/Parser/Statement/Create.php | 33 +++++++++ library/Msd/Sql/Parser/Statement/Drop.php | 33 +++++++++ library/Msd/Sql/Parser/Statement/Insert.php | 33 +++++++++ library/Msd/Sql/Parser/Statement/Lock.php | 33 +++++++++ library/Msd/Sql/Parser/Statement/Unlock.php | 33 +++++++++ 8 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 library/Msd/Sql/Parser/Interface.php create mode 100644 library/Msd/Sql/Parser/Statement/Create.php create mode 100644 library/Msd/Sql/Parser/Statement/Drop.php create mode 100644 library/Msd/Sql/Parser/Statement/Insert.php create mode 100644 library/Msd/Sql/Parser/Statement/Lock.php create mode 100644 library/Msd/Sql/Parser/Statement/Unlock.php diff --git a/library/Msd/Sql/Parser.php b/library/Msd/Sql/Parser.php index 7a08b40..9a73ed2 100644 --- a/library/Msd/Sql/Parser.php +++ b/library/Msd/Sql/Parser.php @@ -1,9 +1,36 @@ array( 'do', 'xa' @@ -35,10 +62,21 @@ class Msd_Sql_Parser ), ); + /** + * MySQL comment types. + * + * @var array + */ protected $_sqlComments = array( '--' => "\n", '/*' => '*/' ); + /** + * 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 + */ public function __construct($sqlQuery = null) { if ($sqlQuery !== null) { @@ -46,6 +84,16 @@ class Msd_Sql_Parser } } + /** + * 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 + */ public function parse($sqlQuery = null) { if ($sqlQuery === null) { @@ -87,13 +135,23 @@ class Msd_Sql_Parser include_once 'Msd/Sql/Parser/Exception.php'; throw new Msd_Sql_Parser_Exception("Unknown MySQL statement is found: '$statement'"); } - $parserClass = 'Msd_Sql_Parser_' . ucwords($statement); + $parserClass = 'Msd_Sql_Parser_Statement_' . ucwords($statement); $endPos = $this->_getStatementEndPos($sqlQuery, $startPos); $completeStatement = trim(substr($sqlQuery, $startPos, $endPos - $startPos)); $startPos = $endPos + 1; + + $this->_parseStatement($completeStatement, $parserClass); } } + /** + * 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 + */ private function _getStatementEndPos($sqlQuery, $startPos = 0) { $nextString = strpos($sqlQuery, "'", $startPos); @@ -117,4 +175,25 @@ class Msd_Sql_Parser return $nextSemicolon; } + + /** + * Creates an instance of a statement parser class and invokes statement parsing. + * + * @param string $statement MySQL statement to parse + * @param string $parserClass Parser class to use + * + * @return void + */ + private function _parseStatement($statement, $parserClass) + { + $classFilename = str_replace('_', '/', $parserClass) . '.php'; + include_once $classFilename; + $parserObject = new $parserClass; + + if (!$parserObject instanceof Msd_Sql_Parser_Interface) { + throw new Msd_Sql_Parser_Exception('The given parser class must implement Msd_Sql_Parser_Interface!'); + } + + $parserObject->parse($statement); + } } diff --git a/library/Msd/Sql/Parser/Exception.php b/library/Msd/Sql/Parser/Exception.php index eb803e4..e4c9700 100644 --- a/library/Msd/Sql/Parser/Exception.php +++ b/library/Msd/Sql/Parser/Exception.php @@ -1,4 +1,20 @@