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\PaginatedQueryInterface; |
14
|
|
|
use fab2s\YaEtl\Extractors\PdoUniqueKeyExtractor; |
15
|
|
|
use fab2s\YaEtl\Extractors\UniqueKeyExtractorAbstract; |
16
|
|
|
use fab2s\YaEtl\YaEtlException; |
17
|
|
|
use Illuminate\Database\Query\Builder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class UniqueKeyExtractor |
21
|
|
|
*/ |
22
|
|
|
class UniqueKeyExtractor extends PdoUniqueKeyExtractor implements PaginatedQueryInterface |
23
|
|
|
{ |
24
|
|
|
use DelayedExtractQueryTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Generic extraction from tables with unique (composite) key |
28
|
|
|
* |
29
|
|
|
* @param ?Builder $extractQuery |
30
|
|
|
* @param array|string $uniqueKey can be either a unique key name as |
31
|
|
|
* string ('id' by default, will be ordered asc) or an associative array : |
32
|
|
|
* [ |
33
|
|
|
* 'uniqueKeyName' => 'order', // eg 'asc' or 'desc' |
34
|
|
|
* ] |
35
|
|
|
* or, for a unique composite key : |
36
|
|
|
* [ |
37
|
|
|
* 'compositeKey1' => 'asc', |
38
|
|
|
* 'compositeKey2' => 'desc', |
39
|
|
|
* // ... |
40
|
|
|
* ] |
41
|
|
|
* |
42
|
|
|
* @throws YaEtlException |
43
|
|
|
* @throws NodalFlowException |
44
|
|
|
*/ |
45
|
|
|
public function __construct(?Builder $extractQuery = null, $uniqueKey = 'id') |
46
|
|
|
{ |
47
|
|
|
if ($extractQuery !== null) { |
48
|
|
|
$this->setExtractQuery($extractQuery); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// delay configuring pdo to flow start |
52
|
|
|
UniqueKeyExtractorAbstract::__construct(null, $uniqueKey); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* This method sets offset and limit in the query |
57
|
|
|
* |
58
|
|
|
* @return string the paginated query with current offset and limit |
59
|
|
|
*/ |
60
|
|
|
public function getPaginatedQuery(): string |
61
|
|
|
{ |
62
|
|
|
if ($this->joinFrom) { |
63
|
|
|
$extractQuery = $this->extractQuery |
64
|
|
|
->whereIn($this->uniqueKeyName, $this->uniqueKeyValues); |
65
|
|
|
} else { |
66
|
|
|
$extractQuery = $this->extractQuery |
67
|
|
|
->offset($this->offset) |
68
|
|
|
->limit($this->batchSize); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->queryBindings = $extractQuery->getRawBindings(); |
72
|
|
|
$this->queryBindings = $this->queryBindings['where']; |
73
|
|
|
|
74
|
|
|
return $extractQuery->toSql(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|