Add Horde_Db_StatementParser, for looping through SQL files - a better implementation...
authorChuck Hagenbuch <chuck@horde.org>
Tue, 16 Jun 2009 02:36:11 +0000 (22:36 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 16 Jun 2009 02:36:11 +0000 (22:36 -0400)
framework/Db/lib/Horde/Db/StatementParser.php [new file with mode: 0644]
framework/Db/package.xml
framework/Db/test/Horde/Db/StatementParserTest.php [new file with mode: 0644]
framework/Db/test/Horde/Db/fixtures/drop_create_table.sql [new file with mode: 0644]

diff --git a/framework/Db/lib/Horde/Db/StatementParser.php b/framework/Db/lib/Horde/Db/StatementParser.php
new file mode 100644 (file)
index 0000000..84885e7
--- /dev/null
@@ -0,0 +1,89 @@
+<?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;
+    }
+
+}
index 0b0f7ac..64fb9dd 100644 (file)
@@ -80,6 +80,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       </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 -->
diff --git a/framework/Db/test/Horde/Db/StatementParserTest.php b/framework/Db/test/Horde/Db/StatementParserTest.php
new file mode 100644 (file)
index 0000000..2434fae
--- /dev/null
@@ -0,0 +1,34 @@
+<?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");
+        }
+    }
+
+}
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 (file)
index 0000000..fa12dd6
--- /dev/null
@@ -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;