-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from programarivm/feature/doctrine-examples
Adding Doctrine examples
- Loading branch information
Showing
7 changed files
with
276 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
examples/doctrine/02_default_doctrine_prepared_query_console.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
examples/doctrine/03_default_doctrine_query_dbal_loader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |