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
|
|
|
* trait PdoExtractorTrait |
14
|
|
|
*/ |
15
|
|
|
trait PdoExtractorTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* This is hte default limit applied when no limit |
19
|
|
|
* is set and an offset is set. |
20
|
|
|
* It's 2^31 – 1 = 2147483647 = max 32bit |
21
|
|
|
* Use 2^63 − 1 = 9223372036854775807 = max 64bit |
22
|
|
|
* if your os and dbms are 64bit |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $maxdefaultLimit = 2147483647; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \PDO |
30
|
|
|
*/ |
31
|
|
|
protected $pdo; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $driverBufferedQuery; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $supportedDrivers = [ |
42
|
|
|
'mysql' => 'mysql', |
43
|
|
|
'sqlite' => 'sqlite', |
44
|
|
|
'pgsql' => 'pgsql', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $dbDriverName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $queryBindings; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* leave no trace |
59
|
|
|
*/ |
60
|
|
|
public function __destruct() |
61
|
|
|
{ |
62
|
|
|
if ($this->dbDriverName === 'mysql' && $this->driverBufferedQuery) { |
63
|
|
|
// set driver state back to where we met |
64
|
|
|
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \PDO $pdo |
70
|
|
|
*/ |
71
|
|
|
public function configurePdo(\PDO $pdo) |
72
|
|
|
{ |
73
|
|
|
$this->pdo = $pdo; |
74
|
|
|
$this->dbDriverName = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
75
|
|
|
|
76
|
|
|
if (!isset($this->supportedDrivers[$this->dbDriverName])) { |
77
|
|
|
throw new \Exception('[YaEtl] Pdo driver not supported, must be one of: ' . \implode(', ', \array_keys($this->supportedDrivers))); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->dbDriverName === 'mysql') { |
81
|
|
|
// buffered wueries can have great performance impact |
82
|
|
|
// with large data sets |
83
|
|
|
$this->driverBufferedQuery = $this->pdo->getAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY); |
84
|
|
|
|
85
|
|
|
if ($this->driverBufferedQuery) { |
86
|
|
|
// disable buffered queries as we should be querying by a lot |
87
|
|
|
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $extractQuery which MUST NOT contain the LIMIT/OFFSET bit |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setExtractQuery($extractQuery) |
98
|
|
|
{ |
99
|
|
|
if (empty($this->pdo)) { |
100
|
|
|
throw new \Exception('[YaEtl] Pdo must be set'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
parent::setExtractQuery($extractQuery); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getLimitOffsetBit() |
112
|
|
|
{ |
113
|
|
|
return ' ' . \implode('', [ |
114
|
|
|
$this->limit ? ' LIMIT ' . (int) $this->limit : ($this->offset ? ' LIMIT ' . $this->maxdefaultLimit : ''), |
115
|
|
|
$this->offset ? ' OFFSET ' . (int) $this->offset : '', |
116
|
|
|
';', |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|