1
0
Fork 0
MySQLDumper/library/Msd/Sql/Object.php

213 Zeilen
5,1 KiB
PHP

<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage SQL-Parser
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Class to represent sql data to be parsed.
* Object is intended to be handed over to the parser classes so that they can work on it.
*
* @package MySQLDumper
* @subpackage SQL-Parser
*/
class Msd_Sql_Object
{
/**
* Holds string data to be parsed.
*
* @var string
*/
private $_data = '';
/**
* Holdes a pointer to the actual examined part as offset.
*
* @var string
*/
private $_pointer = 0;
/**
* Holds the parsing state.
* This is set by the data examining methods of the parser and reflects the parser mode (select, insert, ...)
*
* @var string
*/
private $_state = '';
/**
* Constructor
*
* @param null $sqlData SQl data to be parsed
*/
public function __construct($sqlData = null)
{
$this->_data = $sqlData;
}
/**
* Sets data of object
*
* @param string $sqlData The queries
*
* @return void
*/
public function setData($sqlData = '')
{
$this->_data = $sqlData;
}
/**
* Append data to already given data.
*
* @param string $sqlData The sql-string to be appended
*
* @return void
*/
private function appendData($sqlData)
{
$this->_data .= $sqlData;
}
/**
* Get the actual position of the pointer.
*
* @return int Pointer position
*/
public function getPointer()
{
return $this->_pointer;
}
/**
* Set the actual position of the pointer.
*
* @param int $position Position of pointer
*
* @return void
*/
public function setPointer($position)
{
$this->_pointer = $position;
}
/**
* Move pointer to the beginning of the next command.
*
* Skip all spaces and line-breaks until a character is found that might be the beginning
* of a new sql command.
*
* @return int New pointer position
*/
public function movePointerToNextCommand()
{
$pointer = $this->getPointer();
$dataSize = strlen($this->_data);
$skip = array(';', ' ', "\n", "\r");
if (in_array($this->_data[$pointer], $skip)) {
while ($pointer < $dataSize && in_array($this->_data[$pointer], $skip)) {
$pointer++;
}
}
$this->setPointer($pointer);
if ($pointer == $this->getLength()) {
$pointer = false;
}
return $pointer;
}
/**
* Get some characters of data.
*
* @param int $nrOfCharacters Number of characters to get
*
* @return string Sql data from the pointer position to end or to the nr of chars to fetch
*/
public function getData($nrOfCharacters = null)
{
if ($nrOfCharacters > 0) {
return substr($this->_data, $this->_pointer, $nrOfCharacters);
} else {
return substr($this->_data, $this->_pointer);
}
}
/**
* Get length of data string.
*
* @return int Length of data string
*/
public function getLength()
{
return strlen($this->_data);
}
/**
* Check if pointer has reached the end of the data string.
*
* @return bool
*/
public function hasMoreToProcess()
{
if ($this->_pointer < $this->getLength()) {
return true;
} else {
return false;
}
}
/**
* Set the parser state.
*
* @param string $state The parsing state we are actually in.
*
* @return void
*/
public function setState($state)
{
$this->_state = $state;
}
/**
* Find the next unescaped occurance of $match.
*
* Begins to search at the actual postion of the pointer.
*
* @param string $match
*
* @return int
*/
public function getPosition($match = ';')
{
$pointer = $this->getPointer();
$offset = $pointer;
$notFound = true;
$nextHit = 0;
2011-06-19 22:14:49 +00:00
$length = $this->getLength() - 1;
while ($notFound && $offset < $length) {
$nextHit = strpos($this->_data, $match, $offset);
if ($nextHit === false) {
return $this->getLength() - $pointer;
}
// now check if we found an escaped occurance
$string = substr($this->_data, $pointer, $nextHit);
2011-06-19 22:14:49 +00:00
$string = str_replace('\\\\', '', trim($string));
$quotes = substr_count($string, '\'');
2011-06-19 22:16:17 +00:00
$escapedQuotes = substr_count($string, '\\\'');
if (($quotes - $escapedQuotes) % 2 == 0) {
// hit was not escaped - we found the match
$notFound = false;
} else {
// keep on looking, this was escaped
$offset = $pointer + $nextHit;
}
}
return $nextHit;
}
}