From: Chuck Hagenbuch Date: Tue, 16 Jun 2009 02:36:11 +0000 (-0400) Subject: Add Horde_Db_StatementParser, for looping through SQL files - a better implementation... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=b034e693de43c27369f3fdb87929e42a1110baf9;p=horde.git Add Horde_Db_StatementParser, for looping through SQL files - a better implementation of what's in horde-sql-shell from admintools --- diff --git a/framework/Db/lib/Horde/Db/StatementParser.php b/framework/Db/lib/Horde/Db/StatementParser.php new file mode 100644 index 000000000..84885e734 --- /dev/null +++ b/framework/Db/lib/Horde/Db/StatementParser.php @@ -0,0 +1,89 @@ + + * @author James Pepin + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Db + */ + +/** + * Class for parsing a stream into individual SQL statements. + * + * @author Chuck Hagenbuch + * @author James Pepin + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_Db + */ +class Horde_Db_StatementParser implements Iterator +{ + private $_count; + private $_currentStatement; + + public function __construct(SplFileObject $file) + { + $this->_file = $file; + $this->_count = 0; + } + + public function current() + { + return $this->_currentStatement; + } + + public function key() + { + return $this->_count; + } + + public function next() + { + if ($statement = $this->_getNextStatement()) { + $this->_count++; + return $statement; + } + return null; + } + + public function rewind() + { + $this->_count = 0; + return $this->_file->rewind(); + } + + public function valid() + { + return !$this->_file->eof() && $this->_file->isReadable(); + } + + /** + * Read the next sql statement from our file. Statements are terminated by + * semicolons. + * + * @return string The next SQL statement in the file. + */ + protected function _getNextStatement() + { + $this->_currentStatement = ''; + while (!$this->_file->eof()) { + $line = $this->_file->fgets(); + if (!trim($line)) { continue; } + if (!$this->_currentStatement && substr($line, 0, 2) == '--') { continue; } + + $trimmedline = rtrim($line); + if (substr($trimmedline, -1) == ';') { + // Leave off the ending ; + $this->_currentStatement .= substr($trimmedline, 0, -1); + return $this->_currentStatement; + } + + $this->_currentStatement .= $line; + } + + return $this->_currentStatement; + } + +} diff --git a/framework/Db/package.xml b/framework/Db/package.xml index 0b0f7acdd..64fb9dd75 100644 --- a/framework/Db/package.xml +++ b/framework/Db/package.xml @@ -80,6 +80,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/framework/Db/test/Horde/Db/StatementParserTest.php b/framework/Db/test/Horde/Db/StatementParserTest.php new file mode 100644 index 000000000..2434fae86 --- /dev/null +++ b/framework/Db/test/Horde/Db/StatementParserTest.php @@ -0,0 +1,34 @@ +assertParser($expected, 'drop_create_table.sql'); + } + + public function assertParser(array $expectedStatements, $filename) + { + $file = new SplFileObject(dirname(__FILE__) . '/fixtures/' . $filename, 'r'); + $parser = new Horde_Db_StatementParser($file); + + foreach ($expectedStatements as $i => $expected) { + // Strip any whitespace before comparing the strings. + $this->assertEquals(preg_replace('/\s/', '', $expected), + preg_replace('/\s/', '', $parser->next()), + "Parser differs on statement #$i"); + } + } + +} diff --git a/framework/Db/test/Horde/Db/fixtures/drop_create_table.sql b/framework/Db/test/Horde/Db/fixtures/drop_create_table.sql new file mode 100644 index 000000000..fa12dd62a --- /dev/null +++ b/framework/Db/test/Horde/Db/fixtures/drop_create_table.sql @@ -0,0 +1,20 @@ +-- MySQL dump 10.11 +-- +-- Host: localhost Database: eemaster +-- ------------------------------------------------------ +-- Server version 5.0.27-standard + +-- +-- Table structure for table `exp_actions` +-- + +DROP TABLE IF EXISTS `exp_actions`; +SET @saved_cs_client = @@character_set_client; +SET character_set_client = utf8; +CREATE TABLE `exp_actions` ( + `action_id` int(4) unsigned NOT NULL auto_increment, + `class` varchar(50) NOT NULL, + `method` varchar(50) NOT NULL, + PRIMARY KEY (`action_id`) +) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=latin1; +SET character_set_client = @saved_cs_client;