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
|
|
|
/** |
13
|
|
|
* Class PdoExtractor |
14
|
|
|
*/ |
15
|
|
|
class PdoExtractor extends DbExtractorAbstract |
16
|
|
|
{ |
17
|
|
|
use PdoExtractorTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \PDO $pdo |
21
|
|
|
* @param string|null $extractQuery |
22
|
|
|
*/ |
23
|
|
|
public function __construct(\PDO $pdo, $extractQuery = null) |
24
|
|
|
{ |
25
|
|
|
$this->configurePdo($pdo); |
26
|
|
|
|
27
|
|
|
parent::__construct($extractQuery); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* leave no trace |
32
|
|
|
* implement here to allow easier overidding |
33
|
|
|
*/ |
34
|
|
|
public function __destruct() |
35
|
|
|
{ |
36
|
|
|
if ($this->driverBufferedQuery) { |
37
|
|
|
// set driver state back to where we met |
38
|
|
|
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function fetchRecords() |
46
|
|
|
{ |
47
|
|
|
$extractQuery = $this->getPaginatedQuery(); |
48
|
|
|
|
49
|
|
|
$query = $this->pdo->prepare($extractQuery); |
50
|
|
View Code Duplication |
if (!$query->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->extracted = new \SplDoublyLinkedList; |
55
|
|
|
$hasRecord = false; |
56
|
|
|
while ($record = $query->fetch(\PDO::FETCH_ASSOC)) { |
57
|
|
|
$this->extracted->push($record); |
58
|
|
|
$hasRecord = true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$query->closeCursor(); |
62
|
|
|
unset($query); |
63
|
|
|
$this->extracted->rewind(); |
64
|
|
|
|
65
|
|
|
return $hasRecord; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* This method sets offset and limit in the query |
70
|
|
|
* WARNING : if you set an offset without limit, |
71
|
|
|
* the limit will be set to $this->maxdefaultLimit |
72
|
|
|
* |
73
|
|
|
* @return string the paginated query with current offset and limit |
74
|
|
|
*/ |
75
|
|
|
protected function getPaginatedQuery() |
76
|
|
|
{ |
77
|
|
|
return $this->extractQuery . $this->getLimitOffsetBit(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|