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\YaEtl\Extractors\PdoExtractorTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* class PdoUniqueKeyExtractor |
16
|
|
|
*/ |
17
|
|
|
class PdoUniqueKeyExtractor extends UniqueKeyExtractorAbstract |
18
|
|
|
{ |
19
|
|
|
use PdoExtractorTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* generic extraction from tables with unique (composite) key |
23
|
|
|
* |
24
|
|
|
* @param \PDO $pdo |
25
|
|
|
* @param string $extractQuery |
26
|
|
|
* @param array|string $uniqueKey can be either a unique key name as |
27
|
|
|
* string |
28
|
|
|
* '(table.)compositeKeyName' // ('id' by default) |
29
|
|
|
* |
30
|
|
|
* or an array : |
31
|
|
|
* ['(table.)compositeKey1'] // single unique key |
32
|
|
|
* ['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key |
33
|
|
|
* |
34
|
|
|
* or an associative array in case you are using aliases : |
35
|
|
|
* [ |
36
|
|
|
* '(table.)compositeKey1' => 'aliasNameAsInRecord', |
37
|
|
|
* ] |
38
|
|
|
* |
39
|
|
|
* and : |
40
|
|
|
* [ |
41
|
|
|
* '(table.)compositeKey1' => 'aliasNameAsInRecord1', |
42
|
|
|
* '(table.)compositeKey2' => 'aliasNameAsInRecord2', |
43
|
|
|
* // ... |
44
|
|
|
* ] |
45
|
|
|
*/ |
46
|
|
|
public function __construct(\PDO $pdo, $extractQuery = null, $uniqueKey = 'id') |
47
|
|
|
{ |
48
|
|
|
$this->configurePdo($pdo); |
49
|
|
|
parent::__construct($extractQuery, $uniqueKey); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* leave no trace |
54
|
|
|
* implement here to allow easier overidding |
55
|
|
|
*/ |
56
|
|
|
public function __destruct() |
57
|
|
|
{ |
58
|
|
|
if ($this->driverBufferedQuery) { |
59
|
|
|
// set driver state back to where we met |
60
|
|
|
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function fetchRecords() |
68
|
|
|
{ |
69
|
|
|
$extractQuery = $this->getPaginatedQuery(); |
70
|
|
|
|
71
|
|
|
$query = $this->pdo->prepare($extractQuery); |
72
|
|
View Code Duplication |
if (!$query->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->extracted = isset($this->joinFrom) ? [] : new \SplDoublyLinkedList; |
77
|
|
|
$hasRecord = false; |
78
|
|
|
while ($record = $query->fetch(\PDO::FETCH_ASSOC)) { |
79
|
|
|
if (isset($this->joinFrom)) { |
80
|
|
|
$this->extracted[$record[$this->uniqueKeyName]] = $record; |
81
|
|
|
$hasRecord = true; |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->extracted->push($record); |
86
|
|
|
$hasRecord = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$query->closeCursor(); |
90
|
|
|
unset($query); |
91
|
|
|
|
92
|
|
|
return $hasRecord; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This method sets offset and limit in the query |
97
|
|
|
* WARNING : if you set an offset without limit, |
98
|
|
|
* the limit will be set to $this->maxdefaultLimit |
99
|
|
|
* |
100
|
|
|
* @return string the paginated query with current offset and limit |
101
|
|
|
*/ |
102
|
|
|
protected function getPaginatedQuery() |
103
|
|
|
{ |
104
|
|
|
if ($this->joinFrom) { |
105
|
|
|
$this->queryBindings = array_values($this->uniqueKeyValues); |
106
|
|
|
|
107
|
|
|
$whereOrAndStr = stripos($this->extractQuery, 'WHERE') !== false ? 'AND' : 'WHERE'; |
108
|
|
|
|
109
|
|
|
return $this->extractQuery . " $whereOrAndStr $this->uniqueKeyName IN (" . implode(',', array_fill(0, count($this->uniqueKeyValues), '?')) . ')'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->extractQuery . $this->getLimitOffsetBit(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|