_sql = $sqlObject; $this->_debug = $debug; } /** * Parses a raw MySQL query. * This could include more than one MySQL statement. * * @throws Msd_Sql_Parser_Exception * * @return void */ public function parse() { while ($this->_sql->hasMoreToProcess() && $this->_sql->movePointerToNextCommand() !== false) { // check for comments or conditional comments $commentCheck = $this->_sql->getData($this->_sql->getPointer() + 3, false); if (substr($commentCheck, 0, 2) == '--' || substr($commentCheck, 0, 2) == '/*') { $queryType = 'Comment'; } else { // get first "word" of query to get the kind we have to process $endOfFirstWord = $this->_sql->getPosition(' ', false); $sqlQuery = $this->_sql->getData($endOfFirstWord, false); $queryType = strtolower($sqlQuery); } try { $foundStatement = $this->_parseStatement($this->_sql, ucfirst($queryType)); } catch (Msd_Sql_Parser_Exception $e) { $this->_sql->setError($e->getMessage()); // stop parsing by setting pointer to the end $this->_sql->setPointer($this->_sql->getLength() + 1); } if ($foundStatement > '') { $this->_parsedStatements[] = $foundStatement; // increment query type counter if (!isset($this->_parsingSummary[$queryType])) { $this->_parsingSummary[$queryType] = 0; } $this->_parsingSummary[$queryType]++; } } } /** * Creates an instance of a statement parser class and invokes statement parsing. * * @throws Msd_Sql_Parser_Exception * * @param Msd_Sql_Object $sqlObject MySQL statement to parse * @param string $statement Parser class to use * * @return array */ private function _parseStatement(Msd_Sql_Object $sqlObject, $statement) { $statementPath = '/Msd/Sql/Parser/Statement/' . $statement; if (!file_exists(LIBRARY_PATH . $statementPath . '.php')) { throw new Msd_Sql_Parser_Exception("Unknown statement: '" . $statement . "'"); } $statementClass = 'Msd_Sql_Parser_Statement_' . $statement; $parserObject = new $statementClass(); return $parserObject->parse($sqlObject); } /** * Returns the array with the parsed statements. * * @return array */ public function getParsedStatements() { return $this->_parsedStatements; } /** * Returns the parsing summary. * * @return array */ public function getSummary() { return $this->_parsingSummary; } /** * Rewind (reset) the internal pointer position af the parsed statements array. * * @return mixed */ public function rewind() { return reset($this->_parsedStatements); } /** * Return the current value af the parsed statements array. * * @return mixed */ public function current() { return current($this->_parsedStatements); } /** * Return the current key af the parsed statements array. * * @return mixed */ public function key() { return key($this->_parsedStatements); } /** * Move the internal pointer af the parsed statements array to the next position. * * @return mixed */ public function next() { return next($this->_parsedStatements); } /** * Validates the internal pointer position af the parsed statements array. * * @return bool */ public function valid() { return key($this->_parsedStatements) !== null; } /** * Get debug output buffer * * @return array */ public function getDebugOutput() { return $this->_debugOutput; } }