-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunker.php
142 lines (119 loc) · 3.04 KB
/
Chunker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Chunker;
use DB;
use Illuminate\Database\Eloquent\Builder;
/**
* Class Chunker
*
* Allows you to chunk incrementing models, make modifications, add records, and delete records without
* affecting the chunkable dataset.
*
* @author Devon Bessemer
* @package App\Classes
*/
class Chunker
{
/**
* @var Builder
*/
protected $query;
/**
* @var int
*/
protected $count;
/**
* @var callable
*/
protected $callback;
/**
* @var \Illuminate\Database\Eloquent\Model
*/
protected $model;
/**
* @var string
*/
protected $connection;
/**
* @var string
*/
protected $primaryKey;
/**
* @var int
*/
protected $maxId;
/**
* @var int
*/
protected $minId;
/**
* @var int
*/
protected $lastId = 0;
/**
* @var bool
*/
protected $queryLogEnabled = false;
/**
* ChunkAndEdit constructor.
* @param Builder $query
* @param $count
* @param callable $callback
* @throws \Exception
*/
public function __construct(Builder $query, $count, callable $callback)
{
$this->query = $query;
$this->count = $count;
$this->callback = $callback;
$this->retreiveModelInformation();
$this->setMinId();
$this->setMaxId();
$this->process();
}
protected function retreiveModelInformation() {
$this->model = $this->query->getModel();
$this->primaryKey = $this->model->getKeyName();
$this->connection = $this->model->getConnectionName();
$this->queryLogEnabled = DB::connection($this->connection)->logging();
if (!$this->model->getIncrementing()) {
throw new \Exception('ChunkAndEdit Exception: Model must have an auto incrementing primary key.');
}
}
protected function setMinId() {
$this->minId = with(clone $this->query)->min($this->primaryKey);
}
protected function setMaxId() {
$this->maxId = with(clone $this->query)->max($this->primaryKey);
}
protected function process() {
if (!$this->maxId) {
return;
}
if ($this->queryLogEnabled) {
$this->disableQueryLog();
}
while($this->lastId !== null && $this->lastId < $this->maxId) {
$results = $this->getNextChunk();
$this->lastId = $results->max($this->primaryKey);
call_user_func($this->callback, $results);
}
if ($this->queryLogEnabled) {
$this->enableQueryLog();
}
}
protected function getNextChunk() {
return with(clone $this->query)
->where($this->primaryKey, '>', $this->lastId)
->limit($this->count)
->get();
}
/**
* Reduces memory usage
*/
protected function disableQueryLog() {
DB::connection($this->connection)->disableQueryLog();
}
protected function enableQueryLog() {
DB::connection($this->connection)->enableQueryLog();
}
}