From 17d6f115cd7fc4b738ecc7916bebb43b8c6e9aa6 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sat, 21 Apr 2018 15:51:58 +0100 Subject: [PATCH 01/11] Adding Doctrine examples --- README.md | 42 ++++++++++++++- .../05_default_doctrine_statement_console.php | 30 +++++++++++ fixtures/mysql/books.sql | 6 +++ fixtures/mysql/db-create.sql | 12 +++++ .../Extractor/Doctrine/QueryExtractor.php | 52 +++++++++++++++++++ 5 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 examples/05_default_doctrine_statement_console.php create mode 100644 fixtures/mysql/books.sql create mode 100644 fixtures/mysql/db-create.sql create mode 100644 src/Extraload/Extractor/Doctrine/QueryExtractor.php diff --git a/README.md b/README.md index 487c83f..795610d 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Powerful ETL library. -## Example +## Examples -Dumping csv data to console. +### Dumping CSV data into the console Input data is given in csv format: ```csv @@ -55,6 +55,44 @@ It can be dumped as table to console: ``` In this example `NoopTransformer` is used, but various transformations can be applied. Transformers can also be chained using `TransformerChain`. +### Dumping a Doctrine query into the console + +Make sure to load the fixtures into a database -- this example works with MySQL: + + mysql> source /home/standard/projects/Extraload/fixtures/mysql/db-create.sql + Query OK, 1 row affected (0.00 sec) + + Database changed + Query OK, 0 rows affected, 1 warning (0.00 sec) + + Query OK, 0 rows affected (0.02 sec) + + mysql> source /home/standard/projects/Extraload/fixtures/mysql/books.sql + Query OK, 4 rows affected (0.01 sec) + Records: 4 Duplicates: 0 Warnings: 0 + +The following code: + +```php +(new DefaultPipeline( + new QueryExtractor($conn, 'SELECT * FROM books'), + new NoopTransformer(), + new ConsoleLoader( + new Table($output = new ConsoleOutput()) + ) +))->process(); +``` + +Will dump the results to the console: + + +---------------+--------------------------+------------------+ + | 99921-58-10-7 | Divine Comedy | Dante Alighieri | + | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | + | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | + | 80-902734-1-6 | And Then There Were None | Agatha Christie | + +---------------+--------------------------+------------------+ + + See more [examples](https://github.com/umpirsky/Extraload/tree/master/examples). ## Inspiration diff --git a/examples/05_default_doctrine_statement_console.php b/examples/05_default_doctrine_statement_console.php new file mode 100644 index 0000000..63cce4f --- /dev/null +++ b/examples/05_default_doctrine_statement_console.php @@ -0,0 +1,30 @@ +#!/usr/bin/env php + 'extraload_fixtures', + 'user' => 'extraload_fixtures', + 'password' => 'password', + 'host' => 'localhost', + 'driver' => 'pdo_mysql', +); +$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); + +(new DefaultPipeline( + new QueryExtractor($conn, 'SELECT * FROM books'), + new NoopTransformer(), + new ConsoleLoader( + new Table($output = new ConsoleOutput()) + ) +))->process(); diff --git a/fixtures/mysql/books.sql b/fixtures/mysql/books.sql new file mode 100644 index 0000000..24f92a2 --- /dev/null +++ b/fixtures/mysql/books.sql @@ -0,0 +1,6 @@ +INSERT INTO books (isbn, title, author) +VALUES +('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), +('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), +('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), +('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'); diff --git a/fixtures/mysql/db-create.sql b/fixtures/mysql/db-create.sql new file mode 100644 index 0000000..afbb783 --- /dev/null +++ b/fixtures/mysql/db-create.sql @@ -0,0 +1,12 @@ +CREATE DATABASE extraload_fixtures; + +USE extraload_fixtures; + +GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES +ON extraload_fixtures.* TO 'extraload_fixtures'@'localhost' IDENTIFIED BY 'password'; + +CREATE TABLE books ( + isbn VARCHAR(16) NOT NULL, + title VARCHAR(128) NOT NULL, + author VARCHAR(128) NOT NULL +); diff --git a/src/Extraload/Extractor/Doctrine/QueryExtractor.php b/src/Extraload/Extractor/Doctrine/QueryExtractor.php new file mode 100644 index 0000000..0ec990a --- /dev/null +++ b/src/Extraload/Extractor/Doctrine/QueryExtractor.php @@ -0,0 +1,52 @@ +position = 0; + $this->data = $conn->query($sql)->fetchAll(); + } + + public function extract() + { + $data = $this->current(); + $this->next(); + + return $data; + } + + public function current() + { + return $this->data[$this->position]; + } + + public function key() + { + return $this->position; + } + + public function next() + { + $this->position += 1; + } + + public function rewind() + { + $this->position = 0; + } + + public function valid() + { + return isset($this->data[$this->position]); + } +} From 8b3203c3bd35235d0c2514508e3a83d35fb18e41 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sat, 21 Apr 2018 19:29:00 +0100 Subject: [PATCH 02/11] offset fix --- src/Extraload/Extractor/Doctrine/QueryExtractor.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Extraload/Extractor/Doctrine/QueryExtractor.php b/src/Extraload/Extractor/Doctrine/QueryExtractor.php index 0ec990a..0a3d707 100644 --- a/src/Extraload/Extractor/Doctrine/QueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/QueryExtractor.php @@ -19,7 +19,12 @@ public function __construct(Connection $conn, string $sql) public function extract() { + if ($this->position >= count($this->data)) { + return; + } + $data = $this->current(); + $this->next(); return $data; From 6cd4dec2b577f2938733f87b49f8e30b4d5a9445 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sat, 21 Apr 2018 20:33:02 +0100 Subject: [PATCH 03/11] PreparedQueryExtractor is added --- README.md | 48 +++++++++++-- ...lt_doctrine_prepared_statement_console.php | 39 +++++++++++ fixtures/mysql/books.sql | 1 + .../Doctrine/PreparedQueryExtractor.php | 70 +++++++++++++++++++ 4 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 examples/06_default_doctrine_prepared_statement_console.php create mode 100644 src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php diff --git a/README.md b/README.md index 795610d..e0eef20 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Powerful ETL library. -## Examples +## 1. Examples -### Dumping CSV data into the console +### 1.1. Dumping CSV data into the console Input data is given in csv format: ```csv @@ -55,9 +55,9 @@ It can be dumped as table to console: ``` In this example `NoopTransformer` is used, but various transformations can be applied. Transformers can also be chained using `TransformerChain`. -### Dumping a Doctrine query into the console +### 1.2. Dumping a Doctrine query into the console -Make sure to load the fixtures into a database -- this example works with MySQL: +First of all make sure to load the fixtures into a database -- this example works with MySQL: mysql> source /home/standard/projects/Extraload/fixtures/mysql/db-create.sql Query OK, 1 row affected (0.00 sec) @@ -71,7 +71,7 @@ Make sure to load the fixtures into a database -- this example works with MySQL: Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 -The following code: +So the following code: ```php (new DefaultPipeline( @@ -83,18 +83,52 @@ The following code: ))->process(); ``` -Will dump the results to the console: +Will dump these results to the console: +---------------+--------------------------+------------------+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri | + | 9781847493583 | La Vita Nuova | Dante Alighieri | | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+--------------------------+------------------+ +### 1.3. Dumping a Doctrine prepared query into the console + +Again, make sure to load the fixtures into the database. + +The following code: + +```php +// ... + +$sql = "SELECT * FROM books WHERE author = :author"; +$values = [ + [ + 'parameter' => ':author', + 'value' => 'Dante Alighieri', + 'data_type' => PDO::PARAM_STR // optional + ] +]; + +(new DefaultPipeline( + new PreparedQueryExtractor($conn, $sql, $values), + new NoopTransformer(), + new ConsoleLoader( + new Table($output = new ConsoleOutput()) + ) +))->process(); +``` + +Will dump the results to the console: + + +---------------+---------------+-----------------+ + | 99921-58-10-7 | Divine Comedy | Dante Alighieri | + | 9781847493583 | La Vita Nuova | Dante Alighieri | + +---------------+---------------+-----------------+ See more [examples](https://github.com/umpirsky/Extraload/tree/master/examples). -## Inspiration +## 2. Inspiration Inspired by [php-etl](https://github.com/docteurklein/php-etl) and [petl](https://github.com/alimanfoo/petl). diff --git a/examples/06_default_doctrine_prepared_statement_console.php b/examples/06_default_doctrine_prepared_statement_console.php new file mode 100644 index 0000000..88a310b --- /dev/null +++ b/examples/06_default_doctrine_prepared_statement_console.php @@ -0,0 +1,39 @@ +#!/usr/bin/env php + 'extraload_fixtures', + 'user' => 'extraload_fixtures', + 'password' => 'password', + 'host' => 'localhost', + 'driver' => 'pdo_mysql', +); +$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); + +$sql = "SELECT * FROM books WHERE author = :author"; +$values = [ + [ + 'parameter' => ':author', + 'value' => 'Dante Alighieri', + 'data_type' => PDO::PARAM_STR // optional + ] +]; + +(new DefaultPipeline( + new PreparedQueryExtractor($conn, $sql, $values), + new NoopTransformer(), + new ConsoleLoader( + new Table($output = new ConsoleOutput()) + ) +))->process(); diff --git a/fixtures/mysql/books.sql b/fixtures/mysql/books.sql index 24f92a2..ca064ce 100644 --- a/fixtures/mysql/books.sql +++ b/fixtures/mysql/books.sql @@ -1,6 +1,7 @@ INSERT INTO books (isbn, title, author) VALUES ('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), +('9781847493583', 'La Vita Nuova', 'Dante Alighieri'), ('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), ('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), ('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'); diff --git a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php new file mode 100644 index 0000000..b88aae5 --- /dev/null +++ b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php @@ -0,0 +1,70 @@ +position = 0; + + $stmt = $conn->prepare($sql); + + foreach ($values as $value) { + $stmt->bindValue( + $value['parameter'], + $value['value'], + $value['data_type'] ?? null + ); + } + + $stmt->execute(); + + $this->data = $stmt->fetchAll(); + } + + public function extract() + { + if ($this->position >= count($this->data)) { + return; + } + + $data = $this->current(); + + $this->next(); + + return $data; + } + + public function current() + { + return $this->data[$this->position]; + } + + public function key() + { + return $this->position; + } + + public function next() + { + $this->position += 1; + } + + public function rewind() + { + $this->position = 0; + } + + public function valid() + { + return isset($this->data[$this->position]); + } +} From a59527716166a79252c26fe6676c9496153bfa64 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sat, 21 Apr 2018 23:08:44 +0100 Subject: [PATCH 04/11] Dumping a Doctrine query into a table --- README.md | 44 +++++++++++++------ ...default_doctrine_statement_dbal_loader.php | 28 ++++++++++++ fixtures/mysql/books.sql | 24 ++++++++++ fixtures/mysql/db-create.sql | 12 ----- 4 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 examples/07_default_doctrine_statement_dbal_loader.php delete mode 100644 fixtures/mysql/db-create.sql diff --git a/README.md b/README.md index e0eef20..fe50e0c 100644 --- a/README.md +++ b/README.md @@ -59,17 +59,7 @@ In this example `NoopTransformer` is used, but various transformations can be ap First of all make sure to load the fixtures into a database -- this example works with MySQL: - mysql> source /home/standard/projects/Extraload/fixtures/mysql/db-create.sql - Query OK, 1 row affected (0.00 sec) - - Database changed - Query OK, 0 rows affected, 1 warning (0.00 sec) - - Query OK, 0 rows affected (0.02 sec) - mysql> source /home/standard/projects/Extraload/fixtures/mysql/books.sql - Query OK, 4 rows affected (0.01 sec) - Records: 4 Duplicates: 0 Warnings: 0 So the following code: @@ -95,8 +85,6 @@ Will dump these results to the console: ### 1.3. Dumping a Doctrine prepared query into the console -Again, make sure to load the fixtures into the database. - The following code: ```php @@ -120,13 +108,43 @@ $values = [ ))->process(); ``` -Will dump the results to the console: +Will dump these results to the console: +---------------+---------------+-----------------+ | 99921-58-10-7 | Divine Comedy | Dante Alighieri | | 9781847493583 | La Vita Nuova | Dante Alighieri | +---------------+---------------+-----------------+ +### 1.4. Dumping a Doctrine query into a table + +The following code: + +```php +// ... + +(new DefaultPipeline( + new QueryExtractor($conn, 'SELECT * FROM books'), + new NoopTransformer(), + new DbalLoader($conn, 'my_books') +))->process(); +``` + +Will dump the results into the `my_books` table: + + mysql> select * from my_books; + +----------------+--------------------------+----------------------------+ + | isbn | title | author | + +----------------+--------------------------+----------------------------+ + | 9781503262140 | Faust | Johann Wolfgang von Goethe | + | 978-0156949606 | The Waves | Virgina Woolf | + | 99921-58-10-7 | Divine Comedy | Dante Alighieri | + | 9781847493583 | La Vita Nuova | Dante Alighieri | + | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | + | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | + | 80-902734-1-6 | And Then There Were None | Agatha Christie | + +----------------+--------------------------+----------------------------+ + 7 rows in set (0.00 sec) + See more [examples](https://github.com/umpirsky/Extraload/tree/master/examples). ## 2. Inspiration diff --git a/examples/07_default_doctrine_statement_dbal_loader.php b/examples/07_default_doctrine_statement_dbal_loader.php new file mode 100644 index 0000000..adffe4e --- /dev/null +++ b/examples/07_default_doctrine_statement_dbal_loader.php @@ -0,0 +1,28 @@ +#!/usr/bin/env php + 'extraload_fixtures', + 'user' => 'extraload_fixtures', + 'password' => 'password', + 'host' => 'localhost', + 'driver' => 'pdo_mysql', +); +$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); + +(new DefaultPipeline( + new QueryExtractor($conn, 'SELECT * FROM books'), + new NoopTransformer(), + new DbalLoader($conn, 'my_books') +))->process(); diff --git a/fixtures/mysql/books.sql b/fixtures/mysql/books.sql index ca064ce..bd5f1a5 100644 --- a/fixtures/mysql/books.sql +++ b/fixtures/mysql/books.sql @@ -1,3 +1,22 @@ +CREATE DATABASE extraload_fixtures; + +USE extraload_fixtures; + +GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES +ON extraload_fixtures.* TO 'extraload_fixtures'@'localhost' IDENTIFIED BY 'password'; + +CREATE TABLE books ( + isbn VARCHAR(16) NOT NULL, + title VARCHAR(128) NOT NULL, + author VARCHAR(128) NOT NULL +); + +CREATE TABLE my_books ( + isbn VARCHAR(16) NOT NULL, + title VARCHAR(128) NOT NULL, + author VARCHAR(128) NOT NULL +); + INSERT INTO books (isbn, title, author) VALUES ('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), @@ -5,3 +24,8 @@ VALUES ('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), ('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), ('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'); + +INSERT INTO my_books (isbn, title, author) +VALUES +('9781503262140', 'Faust', 'Johann Wolfgang von Goethe'), +('978-0156949606', 'The Waves', 'Virgina Woolf'); diff --git a/fixtures/mysql/db-create.sql b/fixtures/mysql/db-create.sql deleted file mode 100644 index afbb783..0000000 --- a/fixtures/mysql/db-create.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE DATABASE extraload_fixtures; - -USE extraload_fixtures; - -GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, CREATE TEMPORARY TABLES -ON extraload_fixtures.* TO 'extraload_fixtures'@'localhost' IDENTIFIED BY 'password'; - -CREATE TABLE books ( - isbn VARCHAR(16) NOT NULL, - title VARCHAR(128) NOT NULL, - author VARCHAR(128) NOT NULL -); From 7781deb51701ba901b542da5a3eb2fe20239e057 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sat, 21 Apr 2018 23:14:13 +0100 Subject: [PATCH 05/11] examples renamed --- ...tatement_console.php => 05_default_doctrine_query_console.php} | 0 ...console.php => 06_default_doctrine_prepared_query_console.php} | 0 ..._dbal_loader.php => 07_default_doctrine_query_dbal_loader.php} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename examples/{05_default_doctrine_statement_console.php => 05_default_doctrine_query_console.php} (100%) rename examples/{06_default_doctrine_prepared_statement_console.php => 06_default_doctrine_prepared_query_console.php} (100%) rename examples/{07_default_doctrine_statement_dbal_loader.php => 07_default_doctrine_query_dbal_loader.php} (100%) diff --git a/examples/05_default_doctrine_statement_console.php b/examples/05_default_doctrine_query_console.php similarity index 100% rename from examples/05_default_doctrine_statement_console.php rename to examples/05_default_doctrine_query_console.php diff --git a/examples/06_default_doctrine_prepared_statement_console.php b/examples/06_default_doctrine_prepared_query_console.php similarity index 100% rename from examples/06_default_doctrine_prepared_statement_console.php rename to examples/06_default_doctrine_prepared_query_console.php diff --git a/examples/07_default_doctrine_statement_dbal_loader.php b/examples/07_default_doctrine_query_dbal_loader.php similarity index 100% rename from examples/07_default_doctrine_statement_dbal_loader.php rename to examples/07_default_doctrine_query_dbal_loader.php From 26a696ab7777859b0d1a5ac2ade72e094b88e7a8 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sun, 22 Apr 2018 11:00:55 +0100 Subject: [PATCH 06/11] README update --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fe50e0c..3b202cc 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Powerful ETL library. -## 1. Examples +## Examples -### 1.1. Dumping CSV data into the console +### Dumping CSV data into the console Input data is given in csv format: ```csv @@ -55,7 +55,7 @@ It can be dumped as table to console: ``` In this example `NoopTransformer` is used, but various transformations can be applied. Transformers can also be chained using `TransformerChain`. -### 1.2. Dumping a Doctrine query into the console +### Dumping a Doctrine query into the console First of all make sure to load the fixtures into a database -- this example works with MySQL: @@ -83,7 +83,7 @@ Will dump these results to the console: | 80-902734-1-6 | And Then There Were None | Agatha Christie | +---------------+--------------------------+------------------+ -### 1.3. Dumping a Doctrine prepared query into the console +### Dumping a Doctrine prepared query into the console The following code: @@ -115,7 +115,7 @@ Will dump these results to the console: | 9781847493583 | La Vita Nuova | Dante Alighieri | +---------------+---------------+-----------------+ -### 1.4. Dumping a Doctrine query into a table +### Dumping a Doctrine query into a table The following code: From 173476498690538f350cf919f714ea46949b6881 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sun, 22 Apr 2018 11:18:08 +0100 Subject: [PATCH 07/11] the doctrine examples are bootstrapped --- .../01_default_doctrine_query_console.php} | 14 ++------------ ..._default_doctrine_prepared_query_console.php} | 14 ++------------ .../03_default_doctrine_query_dbal_loader.php} | 14 ++------------ examples/doctrine/mysql-bootstrap.php | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 36 deletions(-) rename examples/{05_default_doctrine_query_console.php => doctrine/01_default_doctrine_query_console.php} (55%) rename examples/{06_default_doctrine_prepared_query_console.php => doctrine/02_default_doctrine_prepared_query_console.php} (64%) rename examples/{07_default_doctrine_query_dbal_loader.php => doctrine/03_default_doctrine_query_dbal_loader.php} (53%) create mode 100644 examples/doctrine/mysql-bootstrap.php diff --git a/examples/05_default_doctrine_query_console.php b/examples/doctrine/01_default_doctrine_query_console.php similarity index 55% rename from examples/05_default_doctrine_query_console.php rename to examples/doctrine/01_default_doctrine_query_console.php index 63cce4f..d8afb15 100644 --- a/examples/05_default_doctrine_query_console.php +++ b/examples/doctrine/01_default_doctrine_query_console.php @@ -1,9 +1,9 @@ #!/usr/bin/env php 'extraload_fixtures', - 'user' => 'extraload_fixtures', - 'password' => 'password', - 'host' => 'localhost', - 'driver' => 'pdo_mysql', -); -$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); - (new DefaultPipeline( new QueryExtractor($conn, 'SELECT * FROM books'), new NoopTransformer(), diff --git a/examples/06_default_doctrine_prepared_query_console.php b/examples/doctrine/02_default_doctrine_prepared_query_console.php similarity index 64% rename from examples/06_default_doctrine_prepared_query_console.php rename to examples/doctrine/02_default_doctrine_prepared_query_console.php index 88a310b..c097f0f 100644 --- a/examples/06_default_doctrine_prepared_query_console.php +++ b/examples/doctrine/02_default_doctrine_prepared_query_console.php @@ -1,9 +1,9 @@ #!/usr/bin/env php 'extraload_fixtures', - 'user' => 'extraload_fixtures', - 'password' => 'password', - 'host' => 'localhost', - 'driver' => 'pdo_mysql', -); -$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); - $sql = "SELECT * FROM books WHERE author = :author"; $values = [ [ diff --git a/examples/07_default_doctrine_query_dbal_loader.php b/examples/doctrine/03_default_doctrine_query_dbal_loader.php similarity index 53% rename from examples/07_default_doctrine_query_dbal_loader.php rename to examples/doctrine/03_default_doctrine_query_dbal_loader.php index adffe4e..d2fddf2 100644 --- a/examples/07_default_doctrine_query_dbal_loader.php +++ b/examples/doctrine/03_default_doctrine_query_dbal_loader.php @@ -1,9 +1,9 @@ #!/usr/bin/env php 'extraload_fixtures', - 'user' => 'extraload_fixtures', - 'password' => 'password', - 'host' => 'localhost', - 'driver' => 'pdo_mysql', -); -$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); - (new DefaultPipeline( new QueryExtractor($conn, 'SELECT * FROM books'), new NoopTransformer(), diff --git a/examples/doctrine/mysql-bootstrap.php b/examples/doctrine/mysql-bootstrap.php new file mode 100644 index 0000000..45a3ffd --- /dev/null +++ b/examples/doctrine/mysql-bootstrap.php @@ -0,0 +1,16 @@ +#!/usr/bin/env php + 'extraload_fixtures', + 'user' => 'extraload_fixtures', + 'password' => 'password', + 'host' => 'localhost', + 'driver' => 'pdo_mysql', +); +$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); From 7c8dd72b2d62d9fcb333e45427ece48977b1efd2 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sun, 22 Apr 2018 12:29:18 +0100 Subject: [PATCH 08/11] QueryExtractor row by row --- src/Extraload/Extractor/Doctrine/QueryExtractor.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Extraload/Extractor/Doctrine/QueryExtractor.php b/src/Extraload/Extractor/Doctrine/QueryExtractor.php index 0a3d707..9ec9182 100644 --- a/src/Extraload/Extractor/Doctrine/QueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/QueryExtractor.php @@ -7,22 +7,27 @@ class QueryExtractor implements ExtractorInterface { - private $position = 0; + private $stmt; + + private $position; private $data; public function __construct(Connection $conn, string $sql) { + $this->stmt = $conn->query($sql); $this->position = 0; - $this->data = $conn->query($sql)->fetchAll(); + $this->data = []; } public function extract() { - if ($this->position >= count($this->data)) { + if (count($this->data) >= $this->stmt->rowCount()) { return; } + $this->data[$this->position] = $this->stmt->fetch(); + $data = $this->current(); $this->next(); From d418d819104d5a17dccbbf51e68ecd090caad1c3 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sun, 22 Apr 2018 13:00:04 +0100 Subject: [PATCH 09/11] PreparedQueryExtractor row by row also (AbstractExtractor is created) --- .../Extractor/Doctrine/AbstractExtractor.php | 51 +++++++++++++++++ .../Doctrine/PreparedQueryExtractor.php | 56 ++----------------- .../Extractor/Doctrine/QueryExtractor.php | 50 +---------------- 3 files changed, 58 insertions(+), 99 deletions(-) create mode 100644 src/Extraload/Extractor/Doctrine/AbstractExtractor.php diff --git a/src/Extraload/Extractor/Doctrine/AbstractExtractor.php b/src/Extraload/Extractor/Doctrine/AbstractExtractor.php new file mode 100644 index 0000000..f27d4df --- /dev/null +++ b/src/Extraload/Extractor/Doctrine/AbstractExtractor.php @@ -0,0 +1,51 @@ +data) >= $this->stmt->rowCount()) { + return; + } + $this->data[$this->position] = $this->stmt->fetch(); + $data = $this->current(); + $this->next(); + + return $data; + } + + public function current() + { + return $this->data[$this->position]; + } + + public function key() + { + return $this->position; + } + + public function next() + { + $this->position += 1; + } + + public function rewind() + { + $this->position = 0; + } + + public function valid() + { + return isset($this->data[$this->position]); + } +} diff --git a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php index b88aae5..daa04a5 100644 --- a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php @@ -3,68 +3,22 @@ namespace Extraload\Extractor\Doctrine; use Doctrine\DBAL\Connection; -use Extraload\Extractor\ExtractorInterface; +use Extraload\Extractor\Doctrine\AbstractExtractor; -class PreparedQueryExtractor implements ExtractorInterface +class PreparedQueryExtractor extends AbstractExtractor { - private $position = 0; - - private $data; - public function __construct(Connection $conn, string $sql, array $values) { - $this->position = 0; - - $stmt = $conn->prepare($sql); - + $this->stmt = $conn->prepare($sql); foreach ($values as $value) { - $stmt->bindValue( + $this->stmt->bindValue( $value['parameter'], $value['value'], $value['data_type'] ?? null ); } + $this->stmt->execute(); - $stmt->execute(); - - $this->data = $stmt->fetchAll(); - } - - public function extract() - { - if ($this->position >= count($this->data)) { - return; - } - - $data = $this->current(); - - $this->next(); - - return $data; - } - - public function current() - { - return $this->data[$this->position]; - } - - public function key() - { - return $this->position; - } - - public function next() - { - $this->position += 1; - } - - public function rewind() - { $this->position = 0; } - - public function valid() - { - return isset($this->data[$this->position]); - } } diff --git a/src/Extraload/Extractor/Doctrine/QueryExtractor.php b/src/Extraload/Extractor/Doctrine/QueryExtractor.php index 9ec9182..5fadf0e 100644 --- a/src/Extraload/Extractor/Doctrine/QueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/QueryExtractor.php @@ -3,60 +3,14 @@ namespace Extraload\Extractor\Doctrine; use Doctrine\DBAL\Connection; -use Extraload\Extractor\ExtractorInterface; +use Extraload\Extractor\Doctrine\AbstractExtractor; -class QueryExtractor implements ExtractorInterface +class QueryExtractor extends AbstractExtractor { - private $stmt; - - private $position; - - private $data; - public function __construct(Connection $conn, string $sql) { $this->stmt = $conn->query($sql); $this->position = 0; $this->data = []; } - - public function extract() - { - if (count($this->data) >= $this->stmt->rowCount()) { - return; - } - - $this->data[$this->position] = $this->stmt->fetch(); - - $data = $this->current(); - - $this->next(); - - return $data; - } - - public function current() - { - return $this->data[$this->position]; - } - - public function key() - { - return $this->position; - } - - public function next() - { - $this->position += 1; - } - - public function rewind() - { - $this->position = 0; - } - - public function valid() - { - return isset($this->data[$this->position]); - } } From 7e35bbff36ee6e3cd7542fe32ffec6800f987ec7 Mon Sep 17 00:00:00 2001 From: programarivm Date: Sun, 22 Apr 2018 13:26:34 +0100 Subject: [PATCH 10/11] PreparedQueryExtractor cleanup --- src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php index daa04a5..9f4ebbf 100644 --- a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php @@ -20,5 +20,7 @@ public function __construct(Connection $conn, string $sql, array $values) $this->stmt->execute(); $this->position = 0; + + $this->data = []; } } From 7d9e39590e3f2443a0a43f20a7dd3f02acbffb7d Mon Sep 17 00:00:00 2001 From: programarivm Date: Mon, 23 Apr 2018 10:33:04 +0100 Subject: [PATCH 11/11] PR review --- README.md | 2 +- ...efault_doctrine_prepared_query_console.php | 4 +- .../Extractor/Doctrine/AbstractExtractor.php | 51 ---------------- .../Doctrine/PreparedQueryExtractor.php | 26 -------- .../Extractor/Doctrine/QueryExtractor.php | 61 +++++++++++++++++-- 5 files changed, 60 insertions(+), 84 deletions(-) delete mode 100644 src/Extraload/Extractor/Doctrine/AbstractExtractor.php delete mode 100644 src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php diff --git a/README.md b/README.md index 3b202cc..7e1c490 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ $values = [ ]; (new DefaultPipeline( - new PreparedQueryExtractor($conn, $sql, $values), + new QueryExtractor($conn, $sql, $values), new NoopTransformer(), new ConsoleLoader( new Table($output = new ConsoleOutput()) diff --git a/examples/doctrine/02_default_doctrine_prepared_query_console.php b/examples/doctrine/02_default_doctrine_prepared_query_console.php index c097f0f..f822e4d 100644 --- a/examples/doctrine/02_default_doctrine_prepared_query_console.php +++ b/examples/doctrine/02_default_doctrine_prepared_query_console.php @@ -4,7 +4,7 @@ require __DIR__.'/../../vendor/autoload.php'; require './mysql-bootstrap.php'; -use Extraload\Extractor\Doctrine\PreparedQueryExtractor; +use Extraload\Extractor\Doctrine\QueryExtractor; use Extraload\Loader\ConsoleLoader; use Extraload\Pipeline\DefaultPipeline; use Extraload\Transformer\NoopTransformer; @@ -21,7 +21,7 @@ ]; (new DefaultPipeline( - new PreparedQueryExtractor($conn, $sql, $values), + new QueryExtractor($conn, $sql, $values), new NoopTransformer(), new ConsoleLoader( new Table($output = new ConsoleOutput()) diff --git a/src/Extraload/Extractor/Doctrine/AbstractExtractor.php b/src/Extraload/Extractor/Doctrine/AbstractExtractor.php deleted file mode 100644 index f27d4df..0000000 --- a/src/Extraload/Extractor/Doctrine/AbstractExtractor.php +++ /dev/null @@ -1,51 +0,0 @@ -data) >= $this->stmt->rowCount()) { - return; - } - $this->data[$this->position] = $this->stmt->fetch(); - $data = $this->current(); - $this->next(); - - return $data; - } - - public function current() - { - return $this->data[$this->position]; - } - - public function key() - { - return $this->position; - } - - public function next() - { - $this->position += 1; - } - - public function rewind() - { - $this->position = 0; - } - - public function valid() - { - return isset($this->data[$this->position]); - } -} diff --git a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php b/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php deleted file mode 100644 index 9f4ebbf..0000000 --- a/src/Extraload/Extractor/Doctrine/PreparedQueryExtractor.php +++ /dev/null @@ -1,26 +0,0 @@ -stmt = $conn->prepare($sql); - foreach ($values as $value) { - $this->stmt->bindValue( - $value['parameter'], - $value['value'], - $value['data_type'] ?? null - ); - } - $this->stmt->execute(); - - $this->position = 0; - - $this->data = []; - } -} diff --git a/src/Extraload/Extractor/Doctrine/QueryExtractor.php b/src/Extraload/Extractor/Doctrine/QueryExtractor.php index 5fadf0e..1ab14a7 100644 --- a/src/Extraload/Extractor/Doctrine/QueryExtractor.php +++ b/src/Extraload/Extractor/Doctrine/QueryExtractor.php @@ -3,14 +3,67 @@ namespace Extraload\Extractor\Doctrine; use Doctrine\DBAL\Connection; -use Extraload\Extractor\Doctrine\AbstractExtractor; +use Extraload\Extractor\ExtractorInterface; -class QueryExtractor extends AbstractExtractor +class QueryExtractor implements ExtractorInterface { - public function __construct(Connection $conn, string $sql) + protected $stmt; + + protected $position; + + protected $data; + + public function __construct(Connection $conn, string $sql, array $values = []) { - $this->stmt = $conn->query($sql); + $this->stmt = $conn->prepare($sql); + foreach ($values as $value) { + $this->stmt->bindValue( + $value['parameter'], + $value['value'], + $value['data_type'] ?? null + ); + } + $this->stmt->execute(); + $this->position = 0; + $this->data = []; } + + public function extract() + { + if (count($this->data) >= $this->stmt->rowCount()) { + return; + } + $this->data[$this->position] = $this->stmt->fetch(); + $data = $this->current(); + $this->next(); + + return $data; + } + + public function current() + { + return $this->data[$this->position]; + } + + public function key() + { + return $this->position; + } + + public function next() + { + $this->position += 1; + } + + public function rewind() + { + $this->position = 0; + } + + public function valid() + { + return isset($this->data[$this->position]); + } }