Skip to content

Commit

Permalink
Merge pull request #17 from programarivm/feature/doctrine-examples
Browse files Browse the repository at this point in the history
Adding Doctrine examples
  • Loading branch information
umpirsky committed Apr 23, 2018
2 parents 87ea4b4 + 7d9e395 commit 6fe47da
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 3 deletions.
96 changes: 93 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,8 +55,98 @@ 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

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/books.sql

So the following code:

```php
(new DefaultPipeline(
new QueryExtractor($conn, 'SELECT * FROM books'),
new NoopTransformer(),
new ConsoleLoader(
new Table($output = new ConsoleOutput())
)
))->process();
```

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 |
+---------------+--------------------------+------------------+

### Dumping a Doctrine prepared query into the console

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 QueryExtractor($conn, $sql, $values),
new NoopTransformer(),
new ConsoleLoader(
new Table($output = new ConsoleOutput())
)
))->process();
```

Will dump these results to the console:

+---------------+---------------+-----------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
| 9781847493583 | La Vita Nuova | Dante Alighieri |
+---------------+---------------+-----------------+

### 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).

## Inspiration
## 2. Inspiration

Inspired by [php-etl](https://github.com/docteurklein/php-etl) and [petl](https://github.com/alimanfoo/petl).
20 changes: 20 additions & 0 deletions examples/doctrine/01_default_doctrine_query_console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../../vendor/autoload.php';
require './mysql-bootstrap.php';

use Extraload\Extractor\Doctrine\QueryExtractor;
use Extraload\Loader\ConsoleLoader;
use Extraload\Pipeline\DefaultPipeline;
use Extraload\Transformer\NoopTransformer;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;

(new DefaultPipeline(
new QueryExtractor($conn, 'SELECT * FROM books'),
new NoopTransformer(),
new ConsoleLoader(
new Table($output = new ConsoleOutput())
)
))->process();
29 changes: 29 additions & 0 deletions examples/doctrine/02_default_doctrine_prepared_query_console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../../vendor/autoload.php';
require './mysql-bootstrap.php';

use Extraload\Extractor\Doctrine\QueryExtractor;
use Extraload\Loader\ConsoleLoader;
use Extraload\Pipeline\DefaultPipeline;
use Extraload\Transformer\NoopTransformer;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;

$sql = "SELECT * FROM books WHERE author = :author";
$values = [
[
'parameter' => ':author',
'value' => 'Dante Alighieri',
'data_type' => PDO::PARAM_STR // optional
]
];

(new DefaultPipeline(
new QueryExtractor($conn, $sql, $values),
new NoopTransformer(),
new ConsoleLoader(
new Table($output = new ConsoleOutput())
)
))->process();
18 changes: 18 additions & 0 deletions examples/doctrine/03_default_doctrine_query_dbal_loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../../vendor/autoload.php';
require './mysql-bootstrap.php';

use Extraload\Extractor\Doctrine\QueryExtractor;
use Extraload\Loader\Doctrine\DbalLoader;
use Extraload\Pipeline\DefaultPipeline;
use Extraload\Transformer\NoopTransformer;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;

(new DefaultPipeline(
new QueryExtractor($conn, 'SELECT * FROM books'),
new NoopTransformer(),
new DbalLoader($conn, 'my_books')
))->process();
16 changes: 16 additions & 0 deletions examples/doctrine/mysql-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../../vendor/autoload.php';

use Doctrine\DBAL\Connection;

$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'extraload_fixtures',
'user' => 'extraload_fixtures',
'password' => 'password',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
31 changes: 31 additions & 0 deletions fixtures/mysql/books.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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'),
('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');

INSERT INTO my_books (isbn, title, author)
VALUES
('9781503262140', 'Faust', 'Johann Wolfgang von Goethe'),
('978-0156949606', 'The Waves', 'Virgina Woolf');
69 changes: 69 additions & 0 deletions src/Extraload/Extractor/Doctrine/QueryExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Extraload\Extractor\Doctrine;

use Doctrine\DBAL\Connection;
use Extraload\Extractor\ExtractorInterface;

class QueryExtractor implements ExtractorInterface
{
protected $stmt;

protected $position;

protected $data;

public function __construct(Connection $conn, string $sql, array $values = [])
{
$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]);
}
}

0 comments on commit 6fe47da

Please sign in to comment.