1
0
Fork 0
- changes and fixes for parsing statements
- QA

SQl-Browser / SQL-Box:
-  added semicolon to table select box
Dieser Commit ist enthalten in:
DSB 2011-06-20 13:39:03 +00:00
Ursprung 2a46539fe5
Commit e95ab08945
13 geänderte Dateien mit 98 neuen und 56 gelöschten Zeilen

Datei anzeigen

@ -394,12 +394,11 @@ class SqlController extends Zend_Controller_Action
if ($sqlObject->hasErrors()) {
$this->view->errorMessage = implode('<br />', $sqlObject->getErrors());
}
//echo $parser->getDebugOutput();
$statements = $parser->getParsedStatements();
foreach ($statements as $statement) {
echo "<br>- ".$statement;
//echo "<br>Extracted statement: ".$statement;
try {
$res = array(); //$this->_db->query($statement, Msd_Db::ARRAY_ASSOC);
$res = $this->_db->query($statement, Msd_Db::ARRAY_ASSOC);
$this->view->resultset = $res;
} catch (Exception $e) {
$this->view->errorMessage = $e->getMessage();

Datei anzeigen

@ -43,7 +43,7 @@ if (isset($this->errorMessage)) {
function setShowTableQuery()
{
query = $('#selectTable').attr('value');
query = 'SELECT * FROM `' + query + '`';
query = 'SELECT * FROM `' + query + '`;';
$('#sqltextarea').val(query);
$('#myForm').submit();
}

Datei anzeigen

@ -44,12 +44,11 @@ class Msd_Html
* Returns a new array containing the different prefixes. Used for building
* filter select boxes (e.g. sqlserver/show.variables).
*
* @param array $array Array to scan for prefixes
* @param boolean $addNoneOption Whether to add a first entry '---'
* @param array $array Array to scan for prefixes
*
* @return $prefix_array array The array conatining the unique prefixes
* @return array The array conatining the unique prefixes
*/
public static function getPrefixArray($array, $addNoneOption = true)
public static function getPrefixArray($array)
{
$prefixes = array();
$keys = array_keys($array);

Datei anzeigen

@ -115,33 +115,48 @@ class Msd_Sql_Object
{
$pointer = $this->getPointer();
$dataSize = strlen($this->_data);
$skip = array(';', ' ', "\n", "\r");
$skip = array(' ', "\n", "\r", "\t");
if ($this->_state !== 'Comment') {
$skip[] = "\n";
}
if (in_array($this->_data[$pointer], $skip)) {
while ($pointer < $dataSize && in_array($this->_data[$pointer], $skip)) {
$pointer++;
}
}
$this->setPointer($pointer);
if ($pointer == $this->getLength()) {
if ($pointer >= $this->getLength()) {
$pointer = false;
}
return $pointer;
}
/**
* Move pointer forward by $positions positions.
*
* @param integer $positions Move pointer forward by $positions
*
* @return void
*/
public function movePointerForward($positions)
{
$this->setPointer($this->getPointer() + $positions);
}
/**
* Get some characters of data.
* Get data from actual pointer to given position.
*
* @param int $nrOfCharacters Number of characters to get
* @param int $endPosition End position of pointer
* @param bool $movePointer Move pointer behind fetched data
*
* @return string Sql data from the pointer position to end or to the nr of chars to fetch
*/
public function getData($nrOfCharacters = null)
public function getData($endPosition, $movePointer = true)
{
if ($nrOfCharacters > 0) {
return substr($this->_data, $this->_pointer, $nrOfCharacters);
} else {
return substr($this->_data, $this->_pointer);
$data = substr($this->_data, $this->_pointer, ($endPosition - $this->_pointer));
if ($movePointer === true) {
$this->setPointer($endPosition +1);
}
return $data;
}
/**
@ -185,8 +200,8 @@ class Msd_Sql_Object
*
* Begins to search at the actual postion of the pointer.
*
* @param string $match The string to find
* @param bool $includeMatch Whether to add length of $match to position
* @param string $match The string to find
* @param bool $includeMatch Whether to add length of $match to position
*
* @return int
*/
@ -195,12 +210,20 @@ class Msd_Sql_Object
$pointer = $this->getPointer();
$offset = $pointer;
$notFound = true;
$nextHit = 0;
$length = $this->getLength() - 1;
$nextHit = false;
$length = $this->getLength() - 1; // zero-based
while ($notFound && $offset < $length) {
$nextHit = strpos($this->_data, $match, $offset);
//echo "<br>getPosition: Search for '".$match."' P: ".$offset."-> Hit at :".$nextHit;
if ($nextHit === false) {
$lang = Msd_Language::getInstance()->getTranslator();
// check special case for comments
if ($this->getState() == 'Comment' && strpos($this->_data, "\n", $pointer) === false) {
// there is no next line - return statement "as is"
$this->setPointer($this->getLength());
return $this->getLength();
}
// we haven't found the correct end of the query - inform user
$lang = Msd_Language::getInstance()->getTranslator();
$msg = sprintf(
$lang->_('L_SQL_INCOMPLETE_STATEMENT_DETECTED'),
$this->getState(),
@ -211,25 +234,51 @@ class Msd_Sql_Object
$this->setPointer($this->getLength());
return false;
}
// now check if we found an escaped occurance
$string = substr($this->_data, $pointer, $nextHit);
$string = str_replace('\\\\', '', trim($string));
$quotes = substr_count($string, '\'');
$escapedQuotes = substr_count($string, '\\\'');
if (($quotes - $escapedQuotes) % 2 == 0) {
$data = $this->getData($nextHit, false);
if (!$this->isEscaped($data)) {
// hit was not escaped - we found the match
$notFound = false;
if ($includeMatch) {
$nextHit += strlen($match);
$nextHit += strlen($match)-1;
}
} else {
// keep on looking, this was escaped
$offset = $pointer + $nextHit;
// keep on looking, this one was escaped
$offset = $nextHit+1;
}
}
return $nextHit;
}
/**
* Get data upto the next new line
*
* @return string
*/
public function getDataUntilNewLine()
{
}
/**
* Check if hit is escaped.
*
* @param string $string String to analyse
*
* @return bool
*/
private function isEscaped($string)
{
$string = str_replace('\\\\', '', $string);
$quotes = substr_count($string, '\'');
$escapedQuotes = substr_count($string, '\\\'');
if (($quotes - $escapedQuotes) % 2 == 0) {
return false;
} else {
return true;
}
}
/**
* Set an error message
*

Datei anzeigen

@ -82,30 +82,31 @@ class Msd_Sql_Parser implements Iterator
{
$statementCounter = 0;
while ($this->_sql->hasMoreToProcess() && $this->_sql->movePointerToNextCommand()!==false) {
$startPosition = $this->_sql->getPointer();
// get first "word" of query to extract the kind we have to process
$endOfFirstWord = $this->_sql->getPosition(' ', false);
// get substring from actual position to found position
$sqlQuery = $this->_sql->getData($endOfFirstWord - $startPosition);
$statement = strtolower($sqlQuery);
// check for comments or conditional comments
$commentCheck = substr($sqlQuery, 0, 2);
if (isset($this->_sqlComments[$commentCheck]) || substr($statement, 0, 3) == '/*!') {
$commentCheck = $this->_sql->getData($this->_sql->getPointer() + 3, false);
if (substr($commentCheck,0 , 2) == '--' || substr($commentCheck, 0, 2) == '/*') {
$statement = 'Comment';
} else {
//$this->_sql->movePointerToNextCommand();
// get first "word" of query to extract the kind we have to process
$endOfFirstWord = $this->_sql->getPosition(' ', false);
$sqlQuery = $this->_sql->getData($endOfFirstWord, false);
$statement = strtolower($sqlQuery);
}
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();
$this->_sql->setPointer($this->_sql->getLength());
}
if ($this->_debug) {
$this->_debugOutput .= '<br />Extracted statement: '.$foundStatement;
}
$this->_parsedStatements[] = $foundStatement;
$statementCounter++;
if ($foundStatement > '') {
$this->_parsedStatements[] = $foundStatement;
$statementCounter++;
}
// increment query type counter
if (!isset($this->_parsingSummary[$statement])) {
$this->_parsingSummary[$statement] = 0;

Datei anzeigen

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

Datei anzeigen

@ -21,18 +21,20 @@ class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface
/**
* Parse the statement.
*
* @param Msd_Sql_Object $statement MySQL comment.
* @param Msd_Sql_Object $sql MySQL comment.
*
* @return void
* @return string
*/
public function parse(Msd_Sql_Object $sql)
{
$sql->setState('Comment');
$firstChars = $sql->getData(3);
$firstChars = $sql->getData($sql->getPointer() + 3, false);
$includeMatch = true;
$returnStatement = false;
if (substr($firstChars, 0, 2) == '--' || substr($firstChars, 0, 1) == '#') {
// one line comment -> match new line
$match = "\n";
$includeMatch = false;
} else {
if ($firstChars == '/*!') {
// conditionial statement
@ -43,13 +45,12 @@ class Msd_Sql_Parser_Statement_Comment implements Msd_Sql_Parser_Interface
$match = '*/';
}
}
$endOfStatement = $sql->getPosition($match);
$endOfStatement = $sql->getPosition($match, $includeMatch);
$statement = $sql->getData($endOfStatement);
$sql->setPointer($endOfStatement);
if ($returnStatement === true) {
return $statement;
} else {
return false;
return '';
}
}
}

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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