diff --git a/application/controllers/SqlController.php b/application/controllers/SqlController.php index 08cb989..4fc3b22 100644 --- a/application/controllers/SqlController.php +++ b/application/controllers/SqlController.php @@ -358,7 +358,7 @@ class SqlController extends Zend_Controller_Action if ($this->_request->isPost() && !empty($tables)) { foreach ($tables as $tableName) { - $res = $this->_db->truncateTable($tableName); + $this->_db->truncateTable($tableName); } $this->view->actionResult = $truncateResults; } @@ -387,14 +387,17 @@ class SqlController extends Zend_Controller_Action $config->set('dynamic.sqlboxQuery', $query); $query = trim($query); if ($query > '') { - $parser = new Msd_Sqlparser($query); - $query = $parser->parse(); + $parser = new Msd_Sql_Parser($query, true); + $parser->parse(); + $statements = $parser->getParsedStatements(); $this->_db->selectDb($config->get('dynamic.dbActual')); - try { - $res = $this->_db->query($query, Msd_Db::ARRAY_ASSOC); - $this->view->resultset = $res; - } catch (Exception $e) { - $this->view->errorMessage = $e->getMessage(); + foreach ($statements as $statement) { + try { + $res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC); + $this->view->resultset = $res; + } catch (Exception $e) { + $this->view->errorMessage = $e->getMessage(); + } } } } diff --git a/library/Msd/Sql/Parser.php b/library/Msd/Sql/Parser.php index 4d40869..f54cdb8 100644 --- a/library/Msd/Sql/Parser.php +++ b/library/Msd/Sql/Parser.php @@ -83,20 +83,27 @@ class Msd_Sql_Parser implements Iterator * @var array */ protected $_sqlComments = array( - '--' => "\n", '/*' => '*/' + '--' => "\n", '/*' => '*/', '/*!' => '*/' ); + /** + * @var bool + */ + private $_debug = false; + /** * 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 + * @param bool $debug If turned on, detection of queries is logged */ - public function __construct($sqlQuery = null) + public function __construct($sqlQuery = null, $debug = false) { if ($sqlQuery !== null) { $this->_rawQuery = $sqlQuery; } + $this->_debug = $debug; } /** @@ -124,6 +131,9 @@ class Msd_Sql_Parser implements Iterator $statementCounter = 0; $startPos = 0; + if ($this->_debug) { + ob_start(); + } while ($startPos < strlen($sqlQuery)) { $statementCounter++; $firstSpace = strpos($sqlQuery, ' ', $startPos); @@ -136,8 +146,9 @@ class Msd_Sql_Parser implements Iterator $startPos = $startPos + 1; continue; } + // check for comments or conditional comments $commentCheck = substr($statement, 0, 2); - if (array_key_exists($commentCheck, $this->_sqlComments)) { + if (isset($this->_sqlComments[$commentCheck]) || substr($statement, 0, 3) == '/*!') { $commentEnd = $this->_sqlComments[$commentCheck]; $endPos = strpos($sqlQuery, $commentEnd, $startPos) + strlen($commentEnd); $comment = substr($sqlQuery, $startPos, $endPos - $startPos); @@ -145,6 +156,7 @@ class Msd_Sql_Parser implements Iterator $startPos = $endPos; continue; } + $statementLength = strlen($statement); if ( !isset($this->_sqlStatements[$statementLength]) || @@ -164,6 +176,14 @@ class Msd_Sql_Parser implements Iterator } $this->_parsingSummary[$statement]++; } + + if ($this->_debug != false) { + $buffer = ob_get_contents(); + ob_end_clean(); + echo "
".$buffer."
"; + } else { + ob_get_clean(); + } } /** diff --git a/library/Msd/Sql/Parser/Statement/Comment.php b/library/Msd/Sql/Parser/Statement/Comment.php index 5215da0..c000cfb 100644 --- a/library/Msd/Sql/Parser/Statement/Comment.php +++ b/library/Msd/Sql/Parser/Statement/Comment.php @@ -9,13 +9,12 @@ * @author $Author$ */ -require_once "Msd/Sql/Parser/Interface.php"; /** * Class to parse MySQL comments. * This enables you to analyze and modify MySQL queries, which the user has entered. * * @package MySQLDumper - * @subpackage SQL-Browser + * @subpackage SQL-Parser */ class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface { @@ -29,5 +28,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 cb6ef4e..006155d 100644 --- a/library/Msd/Sql/Parser/Statement/Create.php +++ b/library/Msd/Sql/Parser/Statement/Create.php @@ -9,13 +9,12 @@ * @author $Author$ */ -require_once "Msd/Sql/Parser/Interface.php"; /** * Class to parse MySQL CREATE statements. * This enables you to analyze and modify MySQL queries, which the user has entered. * * @package MySQLDumper - * @subpackage SQL-Browser + * @subpackage SQL-Parser */ class Msd_Sql_Parser_Statement_Create implements Msd_Sql_Parser_Interface { @@ -28,6 +27,7 @@ class Msd_Sql_Parser_Statement_Create implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "$statement\n"; + 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 4ad55ef..8c4c526 100644 --- a/library/Msd/Sql/Parser/Statement/Drop.php +++ b/library/Msd/Sql/Parser/Statement/Drop.php @@ -9,13 +9,12 @@ * @author $Author$ */ -require_once "Msd/Sql/Parser/Interface.php"; /** * Class to parse MySQL DROP statements. * This enables you to analyze and modify MySQL queries, which the user has entered. * * @package MySQLDumper - * @subpackage SQL-Browser + * @subpackage SQL-Parser */ class Msd_Sql_Parser_Statement_Drop implements Msd_Sql_Parser_Interface { @@ -28,6 +27,7 @@ class Msd_Sql_Parser_Statement_Drop implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "$statement\n"; + 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 9274942..4ac5937 100644 --- a/library/Msd/Sql/Parser/Statement/Insert.php +++ b/library/Msd/Sql/Parser/Statement/Insert.php @@ -9,13 +9,12 @@ * @author $Author$ */ -require_once "Msd/Sql/Parser/Interface.php"; /** * Class to parse MySQL INSERT statements. * This enables you to analyze and modify MySQL queries, which the user has entered. * * @package MySQLDumper - * @subpackage SQL-Browser + * @subpackage SQL-Parser */ class Msd_Sql_Parser_Statement_Insert implements Msd_Sql_Parser_Interface { @@ -28,6 +27,7 @@ class Msd_Sql_Parser_Statement_Insert implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "$statement\n"; + 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 9593d54..61955c9 100644 --- a/library/Msd/Sql/Parser/Statement/Lock.php +++ b/library/Msd/Sql/Parser/Statement/Lock.php @@ -9,13 +9,12 @@ * @author $Author$ */ -require_once "Msd/Sql/Parser/Interface.php"; /** * Class to parse MySQL LOCK statements. * This enables you to analyze and modify MySQL queries, which the user has entered. * * @package MySQLDumper - * @subpackage SQL-Browser + * @subpackage SQL-Parser */ class Msd_Sql_Parser_Statement_Lock implements Msd_Sql_Parser_Interface { @@ -28,6 +27,7 @@ class Msd_Sql_Parser_Statement_Lock implements Msd_Sql_Parser_Interface */ public function parse($statement) { - echo "$statement\n"; + echo "Lock: $statement\n"; + return $statement; } } diff --git a/library/Msd/Sql/Parser/Statement/Select.php b/library/Msd/Sql/Parser/Statement/Select.php new file mode 100644 index 0000000..1271280 --- /dev/null +++ b/library/Msd/Sql/Parser/Statement/Select.php @@ -0,0 +1,33 @@ +