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\Laravel\Extractors; |
11
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
13
|
|
|
use fab2s\YaEtl\Extractors\DbExtractorAbstract; |
14
|
|
|
use fab2s\YaEtl\Extractors\PaginatedQueryInterface; |
15
|
|
|
use fab2s\YaEtl\Extractors\PdoExtractor; |
16
|
|
|
use fab2s\YaEtl\YaEtlException; |
17
|
|
|
use Illuminate\Database\Query\Builder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DbExtractor |
21
|
|
|
*/ |
22
|
|
|
class DbExtractor extends PdoExtractor implements PaginatedQueryInterface |
23
|
|
|
{ |
24
|
|
|
use DelayedExtractQueryTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The record collection structure |
28
|
|
|
* |
29
|
|
|
* @var iterable |
30
|
|
|
*/ |
31
|
|
|
protected $extracted; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The query object |
35
|
|
|
* |
36
|
|
|
* @var Builder |
37
|
|
|
*/ |
38
|
|
|
protected $extractQuery; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Instantiate the DbExtractor |
42
|
|
|
* |
43
|
|
|
* @param Builder|null $extractQuery |
44
|
|
|
* |
45
|
|
|
* @throws NodalFlowException |
46
|
|
|
* @throws YaEtlException |
47
|
|
|
*/ |
48
|
|
|
public function __construct(?Builder $extractQuery = null) |
49
|
|
|
{ |
50
|
|
|
if ($extractQuery !== null) { |
51
|
|
|
$this->setExtractQuery($extractQuery); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// delay configuring pdo to flow start |
55
|
|
|
DbExtractorAbstract::__construct(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This method sets offset and limit in the query |
60
|
|
|
* |
61
|
|
|
* @return string the paginated query with current offset and limit |
62
|
|
|
*/ |
63
|
|
|
public function getPaginatedQuery(): string |
64
|
|
|
{ |
65
|
|
|
$extractQuery = $this->extractQuery |
66
|
|
|
->offset($this->offset) |
67
|
|
|
->limit($this->batchSize); |
68
|
|
|
$this->queryBindings = $extractQuery->getRawBindings(); |
69
|
|
|
$this->queryBindings = $this->queryBindings['where']; |
70
|
|
|
|
71
|
|
|
return $extractQuery->toSql(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|