1
0
Fork 0

Added new parser to SQL-Browser / SQL-Box

Removed require_once in parser classes
Added debug possibility to parser
Dieser Commit ist enthalten in:
DSB 2011-06-19 14:42:30 +00:00
Ursprung 50f5a5aa14
Commit e7bc0d0b0a
9 geänderte Dateien mit 84 neuen und 28 gelöschten Zeilen

Datei anzeigen

@ -358,7 +358,7 @@ class SqlController extends Zend_Controller_Action
if ($this->_request->isPost() && !empty($tables)) { if ($this->_request->isPost() && !empty($tables)) {
foreach ($tables as $tableName) { foreach ($tables as $tableName) {
$res = $this->_db->truncateTable($tableName); $this->_db->truncateTable($tableName);
} }
$this->view->actionResult = $truncateResults; $this->view->actionResult = $truncateResults;
} }
@ -387,17 +387,20 @@ class SqlController extends Zend_Controller_Action
$config->set('dynamic.sqlboxQuery', $query); $config->set('dynamic.sqlboxQuery', $query);
$query = trim($query); $query = trim($query);
if ($query > '') { if ($query > '') {
$parser = new Msd_Sqlparser($query); $parser = new Msd_Sql_Parser($query, true);
$query = $parser->parse(); $parser->parse();
$statements = $parser->getParsedStatements();
$this->_db->selectDb($config->get('dynamic.dbActual')); $this->_db->selectDb($config->get('dynamic.dbActual'));
foreach ($statements as $statement) {
try { try {
$res = $this->_db->query($query, Msd_Db::ARRAY_ASSOC); $res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC);
$this->view->resultset = $res; $this->view->resultset = $res;
} catch (Exception $e) { } catch (Exception $e) {
$this->view->errorMessage = $e->getMessage(); $this->view->errorMessage = $e->getMessage();
} }
} }
} }
}
$this->_setDynamicParams(); $this->_setDynamicParams();
$this->view->boxcontent = $query; $this->view->boxcontent = $query;
} }

Datei anzeigen

@ -83,20 +83,27 @@ class Msd_Sql_Parser implements Iterator
* @var array * @var array
*/ */
protected $_sqlComments = array( protected $_sqlComments = array(
'--' => "\n", '/*' => '*/' '--' => "\n", '/*' => '*/', '/*!' => '*/'
); );
/**
* @var bool
*/
private $_debug = false;
/** /**
* Class constructor. * Class constructor.
* Creates a new instance of the MySQL parser and optionally assign the raw MySQL query. * Creates a new instance of the MySQL parser and optionally assign the raw MySQL query.
* *
* @param string $sqlQuery Raw MySQL query to parse * @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) { if ($sqlQuery !== null) {
$this->_rawQuery = $sqlQuery; $this->_rawQuery = $sqlQuery;
} }
$this->_debug = $debug;
} }
/** /**
@ -124,6 +131,9 @@ class Msd_Sql_Parser implements Iterator
$statementCounter = 0; $statementCounter = 0;
$startPos = 0; $startPos = 0;
if ($this->_debug) {
ob_start();
}
while ($startPos < strlen($sqlQuery)) { while ($startPos < strlen($sqlQuery)) {
$statementCounter++; $statementCounter++;
$firstSpace = strpos($sqlQuery, ' ', $startPos); $firstSpace = strpos($sqlQuery, ' ', $startPos);
@ -136,8 +146,9 @@ class Msd_Sql_Parser implements Iterator
$startPos = $startPos + 1; $startPos = $startPos + 1;
continue; continue;
} }
// check for comments or conditional comments
$commentCheck = substr($statement, 0, 2); $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]; $commentEnd = $this->_sqlComments[$commentCheck];
$endPos = strpos($sqlQuery, $commentEnd, $startPos) + strlen($commentEnd); $endPos = strpos($sqlQuery, $commentEnd, $startPos) + strlen($commentEnd);
$comment = substr($sqlQuery, $startPos, $endPos - $startPos); $comment = substr($sqlQuery, $startPos, $endPos - $startPos);
@ -145,6 +156,7 @@ class Msd_Sql_Parser implements Iterator
$startPos = $endPos; $startPos = $endPos;
continue; continue;
} }
$statementLength = strlen($statement); $statementLength = strlen($statement);
if ( if (
!isset($this->_sqlStatements[$statementLength]) || !isset($this->_sqlStatements[$statementLength]) ||
@ -164,6 +176,14 @@ class Msd_Sql_Parser implements Iterator
} }
$this->_parsingSummary[$statement]++; $this->_parsingSummary[$statement]++;
} }
if ($this->_debug != false) {
$buffer = ob_get_contents();
ob_end_clean();
echo "<br />".$buffer."<br />";
} else {
ob_get_clean();
}
} }
/** /**

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL comments. * Class to parse MySQL comments.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface 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) public function parse($statement)
{ {
echo "Comment: $statement\n"; echo "Comment: $statement\n";
return $statement;
} }
} }

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL CREATE statements. * Class to parse MySQL CREATE statements.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Create implements Msd_Sql_Parser_Interface 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) public function parse($statement)
{ {
echo "$statement\n"; echo "Create: $statement\n";
return $statement;
} }
} }

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL DROP statements. * Class to parse MySQL DROP statements.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Drop implements Msd_Sql_Parser_Interface 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) public function parse($statement)
{ {
echo "$statement\n"; echo "Drop: $statement\n";
return $statement;
} }
} }

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL INSERT statements. * Class to parse MySQL INSERT statements.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Insert implements Msd_Sql_Parser_Interface 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) public function parse($statement)
{ {
echo "$statement\n"; echo "Insert: $statement\n";
return $statement;
} }
} }

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL LOCK statements. * Class to parse MySQL LOCK statements.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Lock implements Msd_Sql_Parser_Interface 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) public function parse($statement)
{ {
echo "$statement\n"; echo "Lock: $statement\n";
return $statement;
} }
} }

Datei anzeigen

@ -0,0 +1,33 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage SQL-Browser
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Class to parse MySQL comments.
* This enables you to analyze and modify MySQL queries, which the user has entered.
*
* @package MySQLDumper
* @subpackage SQL-Parser
*/
class Msd_Sql_Parser_Statement_Select implements Msd_Sql_Parser_Interface
{
/**
* Parse the statement.
*
* @param string $statement MySQL comment.
*
* @return void
*/
public function parse($statement)
{
echo "Select: $statement\n";
return $statement;
}
}

Datei anzeigen

@ -9,13 +9,12 @@
* @author $Author$ * @author $Author$
*/ */
require_once "Msd/Sql/Parser/Interface.php";
/** /**
* Class to parse MySQL UNLOCK statements. * Class to parse MySQL UNLOCK statements.
* This enables you to analyze and modify MySQL queries, which the user has entered. * This enables you to analyze and modify MySQL queries, which the user has entered.
* *
* @package MySQLDumper * @package MySQLDumper
* @subpackage SQL-Browser * @subpackage SQL-Parser
*/ */
class Msd_Sql_Parser_Statement_Unlock implements Msd_Sql_Parser_Interface class Msd_Sql_Parser_Statement_Unlock implements Msd_Sql_Parser_Interface
{ {
@ -28,6 +27,7 @@ class Msd_Sql_Parser_Statement_Unlock implements Msd_Sql_Parser_Interface
*/ */
public function parse($statement) public function parse($statement)
{ {
echo "$statement\n"; echo "UNLOCK: $statement\n";
return $statement;
} }
} }