From 2d8e59b0b2f169dc49bcabb9e6ca2edf34f1e5d7 Mon Sep 17 00:00:00 2001 From: DSB Date: Sun, 19 Jun 2011 15:43:36 +0000 Subject: [PATCH] SQL-Box: - prepared parsing of more than one query --- application/controllers/SqlController.php | 3 +- library/Msd/Sql/Parser.php | 63 ++++++++++++-------- library/Msd/Sql/Parser/Statement/Comment.php | 1 - library/Msd/Sql/Parser/Statement/Create.php | 1 - library/Msd/Sql/Parser/Statement/Drop.php | 1 - library/Msd/Sql/Parser/Statement/Insert.php | 1 - library/Msd/Sql/Parser/Statement/Lock.php | 1 - library/Msd/Sql/Parser/Statement/Select.php | 1 - library/Msd/Sql/Parser/Statement/Unlock.php | 1 - 9 files changed, 41 insertions(+), 32 deletions(-) diff --git a/application/controllers/SqlController.php b/application/controllers/SqlController.php index 4fc3b22..7d85945 100644 --- a/application/controllers/SqlController.php +++ b/application/controllers/SqlController.php @@ -387,10 +387,11 @@ class SqlController extends Zend_Controller_Action $config->set('dynamic.sqlboxQuery', $query); $query = trim($query); if ($query > '') { + $this->_db->selectDb($config->get('dynamic.dbActual')); $parser = new Msd_Sql_Parser($query, true); $parser->parse(); + print_r($parser->getDebugOutput()); $statements = $parser->getParsedStatements(); - $this->_db->selectDb($config->get('dynamic.dbActual')); foreach ($statements as $statement) { try { $res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC); diff --git a/library/Msd/Sql/Parser.php b/library/Msd/Sql/Parser.php index f54cdb8..09fdb41 100644 --- a/library/Msd/Sql/Parser.php +++ b/library/Msd/Sql/Parser.php @@ -87,10 +87,20 @@ class Msd_Sql_Parser implements Iterator ); /** + * Whether to save debug output + * * @var bool */ private $_debug = false; + /** + * Debug output buffer + * + * @var string + */ + private $_debugOutput = ''; + + /** * Class constructor. * Creates a new instance of the MySQL parser and optionally assign the raw MySQL query. @@ -120,10 +130,8 @@ class Msd_Sql_Parser implements Iterator { if ($sqlQuery === null) { if ($this->_rawQuery === null) { - include_once 'Msd/Sql/Parser/Exception.php'; throw new Msd_Sql_Parser_Exception('You must specify a MySQL query for parsing!'); } - $sqlQuery = $this->_rawQuery; } @@ -131,11 +139,14 @@ class Msd_Sql_Parser implements Iterator $statementCounter = 0; $startPos = 0; - if ($this->_debug) { - ob_start(); - } - while ($startPos < strlen($sqlQuery)) { + $queryLength = strlen($sqlQuery); + while ($startPos < $queryLength) { $statementCounter++; + // move pointer to the next character we can interprete + WHILE ($startPos < $queryLength && in_array($sqlQuery[$startPos], array(' ', "\n", "\r"))) { + $startPos++; + } + $firstSpace = strpos($sqlQuery, ' ', $startPos); $statement = trim(strtolower(substr($sqlQuery, $startPos, $firstSpace - $startPos))); $lengthCheck = strlen($statement); @@ -153,7 +164,7 @@ class Msd_Sql_Parser implements Iterator $endPos = strpos($sqlQuery, $commentEnd, $startPos) + strlen($commentEnd); $comment = substr($sqlQuery, $startPos, $endPos - $startPos); $this->_parseStatement($comment, 'Msd_Sql_Parser_Statement_Comment'); - $startPos = $endPos; + $startPos = $endPos+1; continue; } @@ -162,28 +173,23 @@ class Msd_Sql_Parser implements Iterator !isset($this->_sqlStatements[$statementLength]) || !in_array($statement, $this->_sqlStatements[$statementLength]) ) { - include_once 'Msd/Sql/Parser/Exception.php'; throw new Msd_Sql_Parser_Exception("Unknown MySQL statement is found: '$statement'"); } $parserClass = 'Msd_Sql_Parser_Statement_' . ucwords($statement); $endPos = $this->_getStatementEndPos($sqlQuery, $startPos); $completeStatement = trim(substr($sqlQuery, $startPos, $endPos - $startPos)); $startPos = $endPos + 1; - - $this->_parsedStatements[] = $this->_parseStatement($completeStatement, $parserClass); + $foundStatement = $this->_parseStatement($completeStatement, $parserClass); + if ($this->_debug) { + $this->_debugOutput .= '
Extracted statement: '.$foundStatement; + } + $this->_parsedStatements[] = $foundStatement; + // increment query type counter if (!isset($this->_parsingSummary[$statement])) { $this->_parsingSummary[$statement] = 0; } $this->_parsingSummary[$statement]++; } - - if ($this->_debug != false) { - $buffer = ob_get_contents(); - ob_end_clean(); - echo "
".$buffer."
"; - } else { - ob_get_clean(); - } } /** @@ -203,7 +209,7 @@ class Msd_Sql_Parser implements Iterator return strlen($sqlQuery); } - return $nextSemicolon; + return $nextSemicolon+1; } while ($nextString < $nextSemicolon) { @@ -221,6 +227,7 @@ class Msd_Sql_Parser implements Iterator /** * Creates an instance of a statement parser class and invokes statement parsing. * + * @throws Exception * @param string $statement MySQL statement to parse * @param string $parserClass Parser class to use * @@ -228,11 +235,9 @@ class Msd_Sql_Parser implements Iterator */ private function _parseStatement($statement, $parserClass) { - $classFilename = str_replace('_', '/', $parserClass) . '.php'; - include_once $classFilename; - $parserObject = new $parserClass; - - if (!$parserObject instanceof Msd_Sql_Parser_Interface) { + try { + $parserObject = new $parserClass; + } catch (Exception $e) { throw new Msd_Sql_Parser_Exception('The given parser class must implement Msd_Sql_Parser_Interface!'); } @@ -308,4 +313,14 @@ class Msd_Sql_Parser implements Iterator { return key($this->_parsedStatements) !== null; } + + /** + * Get debug output buffer + * + * @return array + */ + public function getDebugOutput() + { + return $this->_debugOutput; + } } diff --git a/library/Msd/Sql/Parser/Statement/Comment.php b/library/Msd/Sql/Parser/Statement/Comment.php index c000cfb..9d56d3b 100644 --- a/library/Msd/Sql/Parser/Statement/Comment.php +++ b/library/Msd/Sql/Parser/Statement/Comment.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Comment: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Create.php b/library/Msd/Sql/Parser/Statement/Create.php index 006155d..7ac296c 100644 --- a/library/Msd/Sql/Parser/Statement/Create.php +++ b/library/Msd/Sql/Parser/Statement/Create.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Create implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Create: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Drop.php b/library/Msd/Sql/Parser/Statement/Drop.php index 8c4c526..d1bb8a0 100644 --- a/library/Msd/Sql/Parser/Statement/Drop.php +++ b/library/Msd/Sql/Parser/Statement/Drop.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Drop implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Drop: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Insert.php b/library/Msd/Sql/Parser/Statement/Insert.php index 4ac5937..b27e06b 100644 --- a/library/Msd/Sql/Parser/Statement/Insert.php +++ b/library/Msd/Sql/Parser/Statement/Insert.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Insert implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Insert: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Lock.php b/library/Msd/Sql/Parser/Statement/Lock.php index 61955c9..f6d1b82 100644 --- a/library/Msd/Sql/Parser/Statement/Lock.php +++ b/library/Msd/Sql/Parser/Statement/Lock.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Lock implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Lock: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Select.php b/library/Msd/Sql/Parser/Statement/Select.php index 1271280..ba38217 100644 --- a/library/Msd/Sql/Parser/Statement/Select.php +++ b/library/Msd/Sql/Parser/Statement/Select.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Select implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "Select: $statement\n"; return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Unlock.php b/library/Msd/Sql/Parser/Statement/Unlock.php index 664dfdb..fc9cc6a 100644 --- a/library/Msd/Sql/Parser/Statement/Unlock.php +++ b/library/Msd/Sql/Parser/Statement/Unlock.php @@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Unlock implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "UNLOCK: $statement\n"; return $statement; } }