1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlow\Provider; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DBALException; |
8
|
|
|
use SlayerBirden\DataFlow\Data\SimpleBag; |
9
|
|
|
use SlayerBirden\DataFlow\DataBagInterface; |
10
|
|
|
use SlayerBirden\DataFlow\IdentificationTrait; |
11
|
|
|
use SlayerBirden\DataFlow\Provider\Exception\ProviderException; |
12
|
|
|
use SlayerBirden\DataFlow\ProviderInterface; |
13
|
|
|
|
14
|
|
|
class Dbal implements ProviderInterface |
15
|
|
|
{ |
16
|
|
|
use IdentificationTrait; |
17
|
|
|
|
18
|
|
|
const LIMIT = 100; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $id; |
23
|
|
|
/** |
24
|
|
|
* @var Connection |
25
|
|
|
*/ |
26
|
|
|
private $connection; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $table; |
31
|
|
|
|
32
|
2 |
|
public function __construct(string $id, Connection $connection, string $table) |
33
|
|
|
{ |
34
|
2 |
|
$this->id = $id; |
35
|
2 |
|
$this->connection = $connection; |
36
|
2 |
|
$this->table = $table; |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return \Generator|DataBagInterface[] |
41
|
|
|
*/ |
42
|
1 |
|
public function getCask(): \Generator |
43
|
|
|
{ |
44
|
1 |
|
$offset = 0; |
45
|
1 |
|
$qb = $this->connection->createQueryBuilder(); |
46
|
|
|
do { |
47
|
1 |
|
$qb->select('*') |
48
|
1 |
|
->from($this->table) |
49
|
1 |
|
->setFirstResult($offset) |
50
|
1 |
|
->setMaxResults(self::LIMIT); |
51
|
|
|
try { |
52
|
1 |
|
$stmt = $this->connection->query($qb->getSQL()); |
53
|
|
|
} catch (DBALException $e) { |
54
|
|
|
yield new ProviderException($e->getMessage(), $e->getCode(), $e); |
55
|
|
|
} |
56
|
1 |
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
57
|
1 |
|
$count = count($result); |
58
|
1 |
|
if ($count) { |
59
|
1 |
|
foreach ($result as $row) { |
60
|
1 |
|
yield new SimpleBag($row); |
61
|
|
|
} |
62
|
1 |
|
$offset += self::LIMIT; |
63
|
|
|
} |
64
|
1 |
|
} while ($count > 0); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
* @throws \Doctrine\DBAL\DBALException |
70
|
|
|
*/ |
71
|
1 |
|
public function getEstimatedSize(): int |
72
|
|
|
{ |
73
|
1 |
|
$qb = $this->connection->createQueryBuilder(); |
74
|
1 |
|
$qb->select('count(*)')->from($this->table); |
75
|
|
|
|
76
|
1 |
|
$stmt = $this->connection->query($qb->getSQL()); |
77
|
1 |
|
return (int)$stmt->fetchColumn(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|