|
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
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* abstract Class DbExtractorAbstract |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class DbExtractorAbstract extends ExtractorBatchLimitAbstract |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The SQL query |
|
21
|
|
|
* |
|
22
|
|
|
* @var mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $extractQuery; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var iterable |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $extracted; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Instantiate a DB extractor |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed $extractQuery |
|
35
|
|
|
* |
|
36
|
|
|
* @throws NodalFlowException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($extractQuery = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($extractQuery !== null) { |
|
41
|
|
|
$this->setExtractQuery($extractQuery); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Trigger a batch extract |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $param |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function extract($param = null): bool |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->isLimitReached()) { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->enforceBatchSize(); |
|
61
|
|
|
if ($this->fetchRecords()) { |
|
62
|
|
|
$this->incrementOffset(); |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set the Extract SQL query |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $extractQuery |
|
74
|
|
|
* |
|
75
|
|
|
* @return static |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setExtractQuery($extractQuery): self |
|
78
|
|
|
{ |
|
79
|
|
|
$this->extractQuery = $extractQuery; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Build the LIMIT...OFFSET bit of the query |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getLimitOffsetBit(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return ' ' . \implode('', [ |
|
92
|
|
|
' LIMIT ' . (int) $this->batchSize, |
|
93
|
|
|
$this->offset ? ' OFFSET ' . (int) $this->offset : '', |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Execute query and store results calling this->setExtracted |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool true if there are records fetched |
|
101
|
|
|
*/ |
|
102
|
|
|
abstract protected function fetchRecords(): bool; |
|
103
|
|
|
} |
|
104
|
|
|
|