--- /dev/null
+<?php
+/**
+ * Copyright 2006-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @author James Pepin <james@bluestatedigital.com>
+ * @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 <chuck@horde.org>
+ * @author James Pepin <james@bluestatedigital.com>
+ * @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;
+ }
+
+}
</dir> <!-- /lib/Horde/Db/Adapter -->
<file name="Adapter.php" role="php" />
<file name="Exception.php" role="php" />
+ <file name="StatementParser.php" role="php" />
</dir> <!-- /lib/Horde/Db -->
<file name="Db.php" role="php" />
</dir> <!-- /lib/Horde -->
--- /dev/null
+<?php
+class Horde_Db_StatementParserTest extends Horde_Test_Case
+{
+ public function testParserFindsMultilineCreateStatement()
+ {
+ $expected = array(
+ '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',
+ );
+ $this->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");
+ }
+ }
+
+}
--- /dev/null
+-- 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;