From 6dd16de9f72d072394aa7dc27c11c780d17bc74f Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Mon, 1 Dec 2014 10:06:26 +1300 Subject: [PATCH 1/6] Add PdoReader and tests --- src/Ddeboer/DataImport/Reader/PdoReader.php | 126 ++++++++++++++++++ .../DataImport/Tests/Reader/PdoReaderTest.php | 91 +++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/Ddeboer/DataImport/Reader/PdoReader.php create mode 100644 tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php diff --git a/src/Ddeboer/DataImport/Reader/PdoReader.php b/src/Ddeboer/DataImport/Reader/PdoReader.php new file mode 100644 index 00000000..ed79c58b --- /dev/null +++ b/src/Ddeboer/DataImport/Reader/PdoReader.php @@ -0,0 +1,126 @@ +pdo = $pdo; + $this->statement = $this->pdo->prepare($sql); + + foreach ($params as $key => $value) { + $this->statement->bindValue($key, $value); + } + } + + /** + * {@inheritdoc} + */ + public function getFields() + { + if ($this->statement->execute()) { + // Statement executed successfully + // Grab the first row to find keys + $row = $this->statement->fetch(\PDO::FETCH_ASSOC); + // Return field keys, or empty array no rows remain + return array_keys($row ? $row : []); + } else { + // If the statement errors return empty + return []; + } + } + + /** + * {@inheritdoc} + */ + public function current() + { + return current($this->data); + } + + /** + * {@inheritdoc} + */ + public function next() + { + next($this->data); + } + + /** + * {@inheritdoc} + */ + public function key() + { + return key($this->data); + } + + /** + * {@inheritdoc} + */ + public function valid() + { + $key = key($this->data); + + return ($key !== null && $key !== false); + } + + /** + * {@inheritdoc} + */ + public function rewind() + { + $this->loadData(); + reset($this->data); + } + + /** + * {@inheritdoc} + */ + public function count() + { + $this->loadData(); + + return count($this->data); + } + + /** + * Load data if it hasn't been loaded yet + */ + protected function loadData() + { + if (null === $this->data) { + $this->statement->execute(); + $this->data = $this->statement->fetchAll(\PDO::FETCH_ASSOC); + } + } +} diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php new file mode 100644 index 00000000..a50ab16c --- /dev/null +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -0,0 +1,91 @@ +getReader()->getFields(); + $this->assertInternalType('array', $fields); + $this->assertEquals(array('id', 'username', 'name'), $fields); + } + + public function testCount() + { + $this->assertEquals(100, $this->getReader()->count()); + } + + public function testIterate() + { + $i=1; + foreach ($this->getReader() as $row) { + $this->assertInternalType('array', $row); + $this->assertEquals('user-'.$i, $row['username']); + $i++; + } + } + + public function testReaderRewindWorksCorrectly() + { + $reader = $this->getReader(); + foreach ($reader as $row) { + } + + foreach ($reader as $row) { + } + } + + public function getConnection() + { + $connection = new \PDO('sqlite::memory:'); + // Set error mode = exception for easy debugging + $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + // Build schema + $connection->query('CREATE TABLE groups ( + id INTEGER PRIMARY KEY, + name VARCHAR(45) + )'); + + $connection->query('CREATE TABLE user ( + id INTEGER PRIMARY KEY, + username VARCHAR(32), + group_id INTEGER, + FOREIGN KEY(group_id) REFERENCES groups(id) + )'); + $connection->query('CREATE UNIQUE INDEX user_username ON user(username)'); + + return $connection; + } + + protected function getReader() + { + $connection = $this->getConnection(); + + $group_insert = $connection->prepare('INSERT INTO groups (name) VALUES (:name)'); + $user_insert = $connection->prepare('INSERT INTO user (username, group_id) VALUES (:username, :group)'); + + $counter = 1; + for ($i = 1; $i <= 10; $i++) { + $group_insert->execute([':name' => "name {$i}"]); + $id = $connection->lastInsertId(); + + for ($j = 1; $j <= 10; $j++) { + $user_insert->execute([ + ':username' => "user-{$counter}", + ':group' => $id + ]); + + $counter++; + } + } + + return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `user` u INNER JOIN groups g ON u.group_id = g.id'); + } +} From ef9bf0294bd1a6c0869035687b3a7e501bb25157 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Mon, 8 Dec 2014 12:03:48 +1300 Subject: [PATCH 2/6] Update code to fix PHP 5.3 errors --- src/Ddeboer/DataImport/Reader/PdoReader.php | 4 ++-- tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Ddeboer/DataImport/Reader/PdoReader.php b/src/Ddeboer/DataImport/Reader/PdoReader.php index ed79c58b..93270aff 100644 --- a/src/Ddeboer/DataImport/Reader/PdoReader.php +++ b/src/Ddeboer/DataImport/Reader/PdoReader.php @@ -53,10 +53,10 @@ public function getFields() // Grab the first row to find keys $row = $this->statement->fetch(\PDO::FETCH_ASSOC); // Return field keys, or empty array no rows remain - return array_keys($row ? $row : []); + return array_keys($row ? $row : array()); } else { // If the statement errors return empty - return []; + return array(); } } diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php index a50ab16c..ed0a7a59 100644 --- a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -73,14 +73,14 @@ protected function getReader() $counter = 1; for ($i = 1; $i <= 10; $i++) { - $group_insert->execute([':name' => "name {$i}"]); + $group_insert->execute(array(':name' => "name {$i}")); $id = $connection->lastInsertId(); for ($j = 1; $j <= 10; $j++) { - $user_insert->execute([ + $user_insert->execute(array( ':username' => "user-{$counter}", ':group' => $id - ]); + )); $counter++; } From df871d1a0fa13c01878ea65cf6085347c0c489da Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Mon, 8 Dec 2014 12:28:26 +1300 Subject: [PATCH 3/6] Prefix tables for PDOReader test, otherwise they clash with DbalReader --- .../DataImport/Tests/Reader/PdoReaderTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php index ed0a7a59..cc4961da 100644 --- a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -48,18 +48,18 @@ public function getConnection() $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Build schema - $connection->query('CREATE TABLE groups ( + $connection->query('CREATE TABLE pdo_group ( id INTEGER PRIMARY KEY, name VARCHAR(45) )'); - $connection->query('CREATE TABLE user ( + $connection->query('CREATE TABLE pdo_user ( id INTEGER PRIMARY KEY, username VARCHAR(32), group_id INTEGER, - FOREIGN KEY(group_id) REFERENCES groups(id) + FOREIGN KEY(group_id) REFERENCES pdo_group(id) )'); - $connection->query('CREATE UNIQUE INDEX user_username ON user(username)'); + $connection->query('CREATE UNIQUE INDEX user_username ON pdo_user(username)'); return $connection; } @@ -68,8 +68,8 @@ protected function getReader() { $connection = $this->getConnection(); - $group_insert = $connection->prepare('INSERT INTO groups (name) VALUES (:name)'); - $user_insert = $connection->prepare('INSERT INTO user (username, group_id) VALUES (:username, :group)'); + $group_insert = $connection->prepare('INSERT INTO pdo_group (name) VALUES (:name)'); + $user_insert = $connection->prepare('INSERT INTO pdo_user (username, group_id) VALUES (:username, :group)'); $counter = 1; for ($i = 1; $i <= 10; $i++) { @@ -86,6 +86,6 @@ protected function getReader() } } - return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `user` u INNER JOIN groups g ON u.group_id = g.id'); + return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `pdo_user` u INNER JOIN `pdo_group` g ON u.group_id = g.id'); } } From f210df89f357f6181349e8176edcbbc15932d003 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Tue, 9 Dec 2014 10:46:49 +1300 Subject: [PATCH 4/6] Revert "Prefix tables for PDOReader test, otherwise they clash with DbalReader" This reverts commit df871d1a0fa13c01878ea65cf6085347c0c489da. --- .../DataImport/Tests/Reader/PdoReaderTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php index cc4961da..ed0a7a59 100644 --- a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -48,18 +48,18 @@ public function getConnection() $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Build schema - $connection->query('CREATE TABLE pdo_group ( + $connection->query('CREATE TABLE groups ( id INTEGER PRIMARY KEY, name VARCHAR(45) )'); - $connection->query('CREATE TABLE pdo_user ( + $connection->query('CREATE TABLE user ( id INTEGER PRIMARY KEY, username VARCHAR(32), group_id INTEGER, - FOREIGN KEY(group_id) REFERENCES pdo_group(id) + FOREIGN KEY(group_id) REFERENCES groups(id) )'); - $connection->query('CREATE UNIQUE INDEX user_username ON pdo_user(username)'); + $connection->query('CREATE UNIQUE INDEX user_username ON user(username)'); return $connection; } @@ -68,8 +68,8 @@ protected function getReader() { $connection = $this->getConnection(); - $group_insert = $connection->prepare('INSERT INTO pdo_group (name) VALUES (:name)'); - $user_insert = $connection->prepare('INSERT INTO pdo_user (username, group_id) VALUES (:username, :group)'); + $group_insert = $connection->prepare('INSERT INTO groups (name) VALUES (:name)'); + $user_insert = $connection->prepare('INSERT INTO user (username, group_id) VALUES (:username, :group)'); $counter = 1; for ($i = 1; $i <= 10; $i++) { @@ -86,6 +86,6 @@ protected function getReader() } } - return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `pdo_user` u INNER JOIN `pdo_group` g ON u.group_id = g.id'); + return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `user` u INNER JOIN groups g ON u.group_id = g.id'); } } From b873a2545c0bbebb3b72ab08e9a287ec08eb5e61 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Tue, 9 Dec 2014 10:47:31 +1300 Subject: [PATCH 5/6] Auto increment primary ids in pdoreader tests --- tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php index ed0a7a59..684b4b94 100644 --- a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -49,12 +49,12 @@ public function getConnection() // Build schema $connection->query('CREATE TABLE groups ( - id INTEGER PRIMARY KEY, + id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(45) )'); $connection->query('CREATE TABLE user ( - id INTEGER PRIMARY KEY, + id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(32), group_id INTEGER, FOREIGN KEY(group_id) REFERENCES groups(id) From 694cf2198e31bc8825cd47a7ddef3663635e86ea Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Mon, 8 Dec 2014 12:28:26 +1300 Subject: [PATCH 6/6] Prefix tables for PDOReader test, otherwise they clash with DbalReader Conflicts: tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php --- .../DataImport/Tests/Reader/PdoReaderTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php index 684b4b94..4e55732c 100644 --- a/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php +++ b/tests/Ddeboer/DataImport/Tests/Reader/PdoReaderTest.php @@ -48,18 +48,18 @@ public function getConnection() $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // Build schema - $connection->query('CREATE TABLE groups ( + $connection->query('CREATE TABLE pdo_group ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(45) )'); - $connection->query('CREATE TABLE user ( + $connection->query('CREATE TABLE pdo_user ( id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(32), group_id INTEGER, - FOREIGN KEY(group_id) REFERENCES groups(id) + FOREIGN KEY(group_id) REFERENCES pdo_group(id) )'); - $connection->query('CREATE UNIQUE INDEX user_username ON user(username)'); + $connection->query('CREATE UNIQUE INDEX user_username ON pdo_user(username)'); return $connection; } @@ -68,8 +68,8 @@ protected function getReader() { $connection = $this->getConnection(); - $group_insert = $connection->prepare('INSERT INTO groups (name) VALUES (:name)'); - $user_insert = $connection->prepare('INSERT INTO user (username, group_id) VALUES (:username, :group)'); + $group_insert = $connection->prepare('INSERT INTO pdo_group (name) VALUES (:name)'); + $user_insert = $connection->prepare('INSERT INTO pdo_user (username, group_id) VALUES (:username, :group)'); $counter = 1; for ($i = 1; $i <= 10; $i++) { @@ -86,6 +86,6 @@ protected function getReader() } } - return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `user` u INNER JOIN groups g ON u.group_id = g.id'); + return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `pdo_user` u INNER JOIN `pdo_group` g ON u.group_id = g.id'); } }