1
0
Fork 0
- fix if statement is unknown
Dieser Commit ist enthalten in:
DSB 2011-06-20 08:07:13 +00:00
Ursprung 78323875f2
Commit 76aca62376
3 geänderte Dateien mit 27 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -109,10 +109,15 @@ class Msd_Sql_Object
$pointer = $this->getPointer(); $pointer = $this->getPointer();
$dataSize = strlen($this->_data); $dataSize = strlen($this->_data);
$skip = array(';', ' ', "\n", "\r"); $skip = array(';', ' ', "\n", "\r");
while ($pointer < $dataSize && in_array($this->_data[$pointer], $skip)) { if (in_array($this->_data[$pointer], $skip)) {
$pointer++; while ($pointer < $dataSize && in_array($this->_data[$pointer], $skip)) {
$pointer++;
}
} }
$this->setPointer($pointer); $this->setPointer($pointer);
if ($pointer == $this->getLength()) {
$pointer = false;
}
return $pointer; return $pointer;
} }
@ -182,13 +187,12 @@ class Msd_Sql_Object
$pointer = $this->getPointer(); $pointer = $this->getPointer();
$offset = $pointer; $offset = $pointer;
$notFound = true; $notFound = true;
$nextHit = 0;
$length = $this->getLength() - 1; $length = $this->getLength() - 1;
while ($notFound && $offset < $length) { while ($notFound && $offset < $length) {
//echo "<br>Checking: ". substr($this->_data, $offset);
$nextHit = strpos($this->_data, $match, $offset); $nextHit = strpos($this->_data, $match, $offset);
//echo "<br>Next hit is :".intval($nextHit);
if ($nextHit === false) { if ($nextHit === false) {
$nextHit = $this->getLength() - $pointer; return $this->getLength() - $pointer;
} }
// now check if we found an escaped occurance // now check if we found an escaped occurance
$string = substr($this->_data, $pointer, $nextHit); $string = substr($this->_data, $pointer, $nextHit);
@ -200,7 +204,7 @@ class Msd_Sql_Object
$notFound = false; $notFound = false;
} else { } else {
// keep on looking, this was escaped // keep on looking, this was escaped
$offset = $pointer + $nextHit + 1; $offset = $pointer + $nextHit;
} }
} }
return $nextHit; return $nextHit;

Datei anzeigen

@ -18,13 +18,6 @@
*/ */
class Msd_Sql_Parser implements Iterator class Msd_Sql_Parser implements Iterator
{ {
/**
* Saves the raw MySQL Query.
*
* @var string
*/
private $_rawQuery = null;
/** /**
* Parsed MySQL statements. * Parsed MySQL statements.
* *
@ -73,7 +66,7 @@ class Msd_Sql_Parser implements Iterator
*/ */
public function __construct(Msd_Sql_Object $sqlObject, $debug = false) public function __construct(Msd_Sql_Object $sqlObject, $debug = false)
{ {
$this->_sql = $sqlObject; $this->_sql = $sqlObject;
$this->_debug = $debug; $this->_debug = $debug;
} }
@ -87,23 +80,27 @@ class Msd_Sql_Parser implements Iterator
*/ */
public function parse() public function parse()
{ {
// get first characters to extract the kind of query we have to process
$statementCounter = 0; $statementCounter = 0;
while ($this->_sql->hasMoreToProcess()) { while ($this->_sql->hasMoreToProcess() && $this->_sql->movePointerToNextCommand()!==false) {
$this->_sql->movePointerToNextCommand(); $startPosition = $this->_sql->getPointer();
$endOfCommand = $this->_sql->getPosition(' '); // get first "word" of query to extract the kind we have to process
$sqlQuery = $this->_sql->getData($endOfCommand); $endOfFirstWord = $this->_sql->getPosition(' ');
//echo "<br>Query beginn: ".$sqlQuery; // get substring from actual position to found position
$sqlQuery = $this->_sql->getData($endOfFirstWord - $startPosition);
$parts = explode(' ', $sqlQuery); $statement = strtolower($sqlQuery);
$statement = strtolower($parts[0]);
// check for comments or conditional comments // check for comments or conditional comments
$commentCheck = substr($sqlQuery, 0, 2); $commentCheck = substr($sqlQuery, 0, 2);
if (isset($this->_sqlComments[$commentCheck]) || substr($statement, 0, 3) == '/*!') { if (isset($this->_sqlComments[$commentCheck]) || substr($statement, 0, 3) == '/*!') {
$statement = 'Comment'; $statement = 'Comment';
} }
$foundStatement = $this->_parseStatement($this->_sql, ucwords($statement)); try {
$foundStatement = $this->_parseStatement($this->_sql, ucfirst($statement));
} catch (Msd_Sql_Parser_Exception $e) {
// stop parsing by setting pointer to the end
$this->_sql->setPointer($this->_sql->getLength()-1);
//echo "<br>Error: ".$e->getMessage();
}
if ($this->_debug) { if ($this->_debug) {
$this->_debugOutput .= '<br />Extracted statement: '.$foundStatement; $this->_debugOutput .= '<br />Extracted statement: '.$foundStatement;
} }
@ -130,12 +127,11 @@ class Msd_Sql_Parser implements Iterator
private function _parseStatement(Msd_Sql_Object $sqlObject, $statement) private function _parseStatement(Msd_Sql_Object $sqlObject, $statement)
{ {
$statementPath = '/Msd/Sql/Parser/Statement/' . $statement; $statementPath = '/Msd/Sql/Parser/Statement/' . $statement;
if ($statement !== 'Select') die("Not implemented yet: ".$statement);
if (!file_exists(LIBRARY_PATH . $statementPath . '.php')) { if (!file_exists(LIBRARY_PATH . $statementPath . '.php')) {
throw new Msd_Sql_Parser_Exception("Can't find statement class for statement: " . $statement); throw new Msd_Sql_Parser_Exception("Can't find statement class for statement: " . $statement);
} }
$statementClass = 'Msd_Sql_Parser_Statement_' . $statement; $statementClass = 'Msd_Sql_Parser_Statement_' . $statement;
$parserObject = new $statementClass; $parserObject = new $statementClass();
return $parserObject->parse($sqlObject); return $parserObject->parse($sqlObject);
} }

Datei anzeigen

@ -30,7 +30,7 @@ class Msd_Sql_Parser_Statement_Select implements Msd_Sql_Parser_Interface
$sql->setState('Select'); $sql->setState('Select');
$endOfStatement = $sql->getPosition(';'); $endOfStatement = $sql->getPosition(';');
$statement = $sql->getData($endOfStatement); $statement = $sql->getData($endOfStatement);
$sql->setPointer($endOfStatement+1); $sql->setPointer($endOfStatement);
return $statement; return $statement;
} }
} }