SQL-Box:
- prepared parsing of more than one query
Dieser Commit ist enthalten in:
Ursprung
1d1da03450
Commit
2d8e59b0b2
9 geänderte Dateien mit 41 neuen und 32 gelöschten Zeilen
|
@ -387,10 +387,11 @@ 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 > '') {
|
||||||
|
$this->_db->selectDb($config->get('dynamic.dbActual'));
|
||||||
$parser = new Msd_Sql_Parser($query, true);
|
$parser = new Msd_Sql_Parser($query, true);
|
||||||
$parser->parse();
|
$parser->parse();
|
||||||
|
print_r($parser->getDebugOutput());
|
||||||
$statements = $parser->getParsedStatements();
|
$statements = $parser->getParsedStatements();
|
||||||
$this->_db->selectDb($config->get('dynamic.dbActual'));
|
|
||||||
foreach ($statements as $statement) {
|
foreach ($statements as $statement) {
|
||||||
try {
|
try {
|
||||||
$res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC);
|
$res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC);
|
||||||
|
|
|
@ -87,10 +87,20 @@ class Msd_Sql_Parser implements Iterator
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Whether to save debug output
|
||||||
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_debug = false;
|
private $_debug = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debug output buffer
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $_debugOutput = '';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -120,10 +130,8 @@ class Msd_Sql_Parser implements Iterator
|
||||||
{
|
{
|
||||||
if ($sqlQuery === null) {
|
if ($sqlQuery === null) {
|
||||||
if ($this->_rawQuery === 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!');
|
throw new Msd_Sql_Parser_Exception('You must specify a MySQL query for parsing!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$sqlQuery = $this->_rawQuery;
|
$sqlQuery = $this->_rawQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,11 +139,14 @@ class Msd_Sql_Parser implements Iterator
|
||||||
|
|
||||||
$statementCounter = 0;
|
$statementCounter = 0;
|
||||||
$startPos = 0;
|
$startPos = 0;
|
||||||
if ($this->_debug) {
|
$queryLength = strlen($sqlQuery);
|
||||||
ob_start();
|
while ($startPos < $queryLength) {
|
||||||
}
|
|
||||||
while ($startPos < strlen($sqlQuery)) {
|
|
||||||
$statementCounter++;
|
$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);
|
$firstSpace = strpos($sqlQuery, ' ', $startPos);
|
||||||
$statement = trim(strtolower(substr($sqlQuery, $startPos, $firstSpace - $startPos)));
|
$statement = trim(strtolower(substr($sqlQuery, $startPos, $firstSpace - $startPos)));
|
||||||
$lengthCheck = strlen($statement);
|
$lengthCheck = strlen($statement);
|
||||||
|
@ -153,7 +164,7 @@ class Msd_Sql_Parser implements Iterator
|
||||||
$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);
|
||||||
$this->_parseStatement($comment, 'Msd_Sql_Parser_Statement_Comment');
|
$this->_parseStatement($comment, 'Msd_Sql_Parser_Statement_Comment');
|
||||||
$startPos = $endPos;
|
$startPos = $endPos+1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,28 +173,23 @@ class Msd_Sql_Parser implements Iterator
|
||||||
!isset($this->_sqlStatements[$statementLength]) ||
|
!isset($this->_sqlStatements[$statementLength]) ||
|
||||||
!in_array($statement, $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'");
|
throw new Msd_Sql_Parser_Exception("Unknown MySQL statement is found: '$statement'");
|
||||||
}
|
}
|
||||||
$parserClass = 'Msd_Sql_Parser_Statement_' . ucwords($statement);
|
$parserClass = 'Msd_Sql_Parser_Statement_' . ucwords($statement);
|
||||||
$endPos = $this->_getStatementEndPos($sqlQuery, $startPos);
|
$endPos = $this->_getStatementEndPos($sqlQuery, $startPos);
|
||||||
$completeStatement = trim(substr($sqlQuery, $startPos, $endPos - $startPos));
|
$completeStatement = trim(substr($sqlQuery, $startPos, $endPos - $startPos));
|
||||||
$startPos = $endPos + 1;
|
$startPos = $endPos + 1;
|
||||||
|
$foundStatement = $this->_parseStatement($completeStatement, $parserClass);
|
||||||
$this->_parsedStatements[] = $this->_parseStatement($completeStatement, $parserClass);
|
if ($this->_debug) {
|
||||||
|
$this->_debugOutput .= '<br />Extracted statement: '.$foundStatement;
|
||||||
|
}
|
||||||
|
$this->_parsedStatements[] = $foundStatement;
|
||||||
|
// increment query type counter
|
||||||
if (!isset($this->_parsingSummary[$statement])) {
|
if (!isset($this->_parsingSummary[$statement])) {
|
||||||
$this->_parsingSummary[$statement] = 0;
|
$this->_parsingSummary[$statement] = 0;
|
||||||
}
|
}
|
||||||
$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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -203,7 +209,7 @@ class Msd_Sql_Parser implements Iterator
|
||||||
return strlen($sqlQuery);
|
return strlen($sqlQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $nextSemicolon;
|
return $nextSemicolon+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ($nextString < $nextSemicolon) {
|
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.
|
* Creates an instance of a statement parser class and invokes statement parsing.
|
||||||
*
|
*
|
||||||
|
* @throws Exception
|
||||||
* @param string $statement MySQL statement to parse
|
* @param string $statement MySQL statement to parse
|
||||||
* @param string $parserClass Parser class to use
|
* @param string $parserClass Parser class to use
|
||||||
*
|
*
|
||||||
|
@ -228,11 +235,9 @@ class Msd_Sql_Parser implements Iterator
|
||||||
*/
|
*/
|
||||||
private function _parseStatement($statement, $parserClass)
|
private function _parseStatement($statement, $parserClass)
|
||||||
{
|
{
|
||||||
$classFilename = str_replace('_', '/', $parserClass) . '.php';
|
try {
|
||||||
include_once $classFilename;
|
$parserObject = new $parserClass;
|
||||||
$parserObject = new $parserClass;
|
} catch (Exception $e) {
|
||||||
|
|
||||||
if (!$parserObject instanceof Msd_Sql_Parser_Interface) {
|
|
||||||
throw new Msd_Sql_Parser_Exception('The given parser class must implement Msd_Sql_Parser_Interface!');
|
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;
|
return key($this->_parsedStatements) !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get debug output buffer
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getDebugOutput()
|
||||||
|
{
|
||||||
|
return $this->_debugOutput;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Comment: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Create implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Create: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Drop implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Drop: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Insert implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Insert: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Lock implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Lock: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Select implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "Select: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ class Msd_Sql_Parser_Statement_Unlock implements Msd_Sql_Parser_Interface
|
||||||
*/
|
*/
|
||||||
public function parse($statement)
|
public function parse($statement)
|
||||||
{
|
{
|
||||||
echo "UNLOCK: $statement\n";
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Laden …
In neuem Issue referenzieren