From 9144e553561c99705fb455973d86338a75655742 Mon Sep 17 00:00:00 2001 From: Andrew Stilliard Date: Thu, 24 Apr 2014 09:37:07 +0100 Subject: [PATCH] Added new method to split_query used by execute method, complete with test that it splits properly when semicolons exist inside strings. ref pull #120 and issue #123 on github --- lib/Ruckusing/Migration/Base.php | 28 +++++++++++++++++++++++++++- tests/unit/BaseMigrationTest.php | 20 +++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/Ruckusing/Migration/Base.php b/lib/Ruckusing/Migration/Base.php index 9128c310..9e783fff 100644 --- a/lib/Ruckusing/Migration/Base.php +++ b/lib/Ruckusing/Migration/Base.php @@ -238,6 +238,32 @@ public function create_table($table_name, $options = array()) return $this->_adapter->create_table($table_name, $options); } + /** + * Split up multiple sql queries from a single query string + * @link http://stackoverflow.com/questions/4001797/how-to-break-queries-using-regex-in-php + * @param string $query The query to be split + * @return array Each query string found + */ + protected function split_query($query) + { + $open = false; + $buffer = null; + $parts = array(); + for($i = 0, $l = strlen($query); $i < $l; $i++) { + if ($query[$i] == ';' && !$open) { + $parts[] = trim($buffer); + $buffer = null; + continue; + } + if ($query[$i] == "'") { + $open = ($open) ? false: true; + } + $buffer .= $query[$i]; + } + if ($buffer) $parts[] = trim($buffer); + return $parts; + } + /** * Execute a query * @@ -248,7 +274,7 @@ public function create_table($table_name, $options = array()) public function execute($query) { $result = null; - $queries = explode(';', $query); + $queries = $this->split_query($query); foreach($queries as $query_s) { $query_s = trim($query_s); if (!empty($query_s)) { diff --git a/tests/unit/BaseMigrationTest.php b/tests/unit/BaseMigrationTest.php index 63b2e4c9..83557dc0 100644 --- a/tests/unit/BaseMigrationTest.php +++ b/tests/unit/BaseMigrationTest.php @@ -101,16 +101,30 @@ public function test_execute_multiple_queries() ) engine=innodb default charset=utf8 collate=utf8_unicode_ci; "); - // test first table + // test first table created ok $col = $this->adapter->column_info("admin", "email"); $this->assertEquals("email", $col['field']); - // test second table + // test second table created ok $col = $this->adapter->column_info("adminsession", "admin_id"); $this->assertEquals("admin_id", $col['field']); + // test multiple queries with a semicolon inside quotes + $base->execute(" + DROP TABLE IF EXISTS `demo`; + CREATE TABLE `demo` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `name` VARCHAR(100) NOT NULL, + PRIMARY KEY (`id`) + ); + INSERT INTO demo(id, name) VALUES(1,'A;A'); + INSERT INTO demo(id, name) VALUES(2,'b;b'); + "); + $rows = $this->adapter->select_all('SELECT * FROM demo'); + $this->assertEquals(2, count($rows)); + // cleanup - $base->execute("drop table `admin`; drop table `adminsession`;"); + $base->execute("DROP TABLE `admin`; DROP TABLE `adminsession`; DROP TABLE `demo`;"); } }