|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of YaEtl |
|
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
|
6
|
|
|
* This source file is licensed under the MIT license which you will |
|
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Extractors; |
|
11
|
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
|
13
|
|
|
use fab2s\YaEtl\YaEtlException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* class PdoUniqueKeyExtractor |
|
17
|
|
|
*/ |
|
18
|
|
|
class PdoUniqueKeyExtractor extends UniqueKeyExtractorAbstract |
|
19
|
|
|
{ |
|
20
|
|
|
use PdoExtractorTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Generic extraction from tables with unique (composite) key |
|
24
|
|
|
* |
|
25
|
|
|
* @param \PDO $pdo |
|
26
|
|
|
* @param string|null $extractQuery |
|
27
|
|
|
* @param array|string $uniqueKey can be either a unique key name as |
|
28
|
|
|
* string |
|
29
|
|
|
* `'(table.)compositeKeyName' // ('id' by default)` |
|
30
|
|
|
* |
|
31
|
|
|
* or an array : |
|
32
|
|
|
* `['(table.)compositeKey1'] // single unique key` |
|
33
|
|
|
* `['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key` |
|
34
|
|
|
* |
|
35
|
|
|
* or an associative array in case you are using aliases : |
|
36
|
|
|
* `[ |
|
37
|
|
|
* '(table.)compositeKey1' => 'aliasNameAsInRecord', |
|
38
|
|
|
* ]` |
|
39
|
|
|
* |
|
40
|
|
|
* and : |
|
41
|
|
|
* `[ |
|
42
|
|
|
* '(table.)compositeKey1' => 'aliasNameAsInRecord1', |
|
43
|
|
|
* '(table.)compositeKey2' => 'aliasNameAsInRecord2', |
|
44
|
|
|
* // ... |
|
45
|
|
|
* ]` |
|
46
|
|
|
* |
|
47
|
|
|
* @throws YaEtlException |
|
48
|
|
|
* @throws NodalFlowException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(\PDO $pdo, ?string $extractQuery = null, $uniqueKey = 'id') |
|
51
|
|
|
{ |
|
52
|
|
|
$this->configurePdo($pdo); |
|
53
|
|
|
|
|
54
|
|
|
parent::__construct($extractQuery, $uniqueKey); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Leave no trace |
|
59
|
|
|
* implement here to allow easier overriding |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __destruct() |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->driverBufferedQuery) { |
|
64
|
|
|
// set driver state back to where we met |
|
65
|
|
|
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* This method sets offset and limit in the query |
|
71
|
|
|
* WARNING : if you set an offset without limit, |
|
72
|
|
|
* the limit will be set to $this->maxdefaultLimit |
|
73
|
|
|
* |
|
74
|
|
|
* @return string the paginated query with current offset and limit |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getPaginatedQuery(): string |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->joinFrom) { |
|
79
|
|
|
$this->queryBindings = array_values($this->uniqueKeyValues); |
|
80
|
|
|
|
|
81
|
|
|
$whereOrAndStr = stripos($this->extractQuery, 'WHERE') !== false ? 'AND' : 'WHERE'; |
|
82
|
|
|
|
|
83
|
|
|
return $this->extractQuery . " $whereOrAndStr $this->uniqueKeyName IN (" . implode(',', array_fill(0, count($this->uniqueKeyValues), '?')) . ')'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->extractQuery . $this->getLimitOffsetBit(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function fetchJoinedRecords(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
$extractQuery = $this->getPaginatedQuery(); |
|
95
|
|
|
$statement = $this->pdo->prepare($extractQuery); |
|
96
|
|
|
if (!$statement->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->joinedRecords = []; |
|
101
|
|
|
$joinKey = $this->onClose->getJoinKeyAlias(); |
|
|
|
|
|
|
102
|
|
|
while ($record = $statement->fetch(\PDO::FETCH_ASSOC)) { |
|
103
|
|
|
$this->joinedRecords[$record[$joinKey]] = $record; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$statement->closeCursor(); |
|
107
|
|
|
unset($statement); |
|
108
|
|
|
// still set this as extracted as we build |
|
109
|
|
|
// record map in both from and join context |
|
110
|
|
|
$this->setExtractedCollection($this->joinedRecords); |
|
111
|
|
|
|
|
112
|
|
|
return !empty($this->joinedRecords); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.