-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuilder.php
197 lines (168 loc) · 4.33 KB
/
Builder.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace WPLibs\Database;
class Builder extends \Database\Query\Builder {
/**
* Alias to set the "offset" value of the query.
*
* @param int $value
* @return $this
*/
public function skip( $value ) {
return $this->offset( $value );
}
/**
* Alias to set the "limit" value of the query.
*
* @param int $value
* @return $this
*/
public function take( $value ) {
return $this->limit( $value );
}
/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return array
*/
public function get( $columns = [ '*' ] ) {
return $this->onceWithColumns( $columns, function () {
return $this->connection->fetchAll( $this->toSql(), $this->getBindings() );
} );
}
/**
* Insert a new record into the database.
*
* @param array $values
* @return bool
*/
public function insert( array $values ) {
if ( empty( $values ) ) {
return false;
}
return (bool) parent::insert( $values );
}
/**
* Update a record in the database.
*
* @param array $values
* @return int
*/
public function update( array $values ) {
return (int) parent::update( $values );
}
/**
* Increment a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public function increment( $column, $amount = 1, array $extra = [] ) {
if ( ! is_numeric( $amount ) ) {
throw new \InvalidArgumentException( 'Non-numeric value passed to increment method.' );
}
return (int) parent::increment( $column, $amount, $extra );
}
/**
* Decrement a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public function decrement( $column, $amount = 1, array $extra = [] ) {
if ( ! is_numeric( $amount ) ) {
throw new \InvalidArgumentException( 'Non-numeric value passed to decrement method.' );
}
return (int) parent::decrement( $column, $amount, $extra );
}
/**
* Delete a record from the database.
*
* @param mixed $id
* @return int
*/
public function delete( $id = null ) {
return (int) parent::delete( $id );
}
/**
* Execute a callback over each item while chunking.
*
* @param callable $callback
* @param int $count
* @return bool
*/
public function each( callable $callback, $count = 1000 ) {
return $this->chunk( $count, function ( $results ) use ( $callback ) {
foreach ( $results as $key => $value ) {
if ( $callback( $value, $key ) === false ) {
return false;
}
}
return true;
} );
}
/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @return bool
*/
public function chunk( $count, callable $callback ) {
$this->enforceOrderBy();
$page = 1;
do {
// We'll execute the query for the given page and get the results. If there are
// no results we can just break and return from here. When there are results
// we will call the callback with the current chunk of these results here.
$results = $this->forPage( $page, $count )->get();
$countResults = count( $results );
if ( 0 === $countResults ) {
break;
}
// On each chunk result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.
if ( $callback( $results, $page ) === false ) {
return false;
}
unset( $results );
$page ++;
} while ( $countResults === $count );
return true;
}
/**
* Throw an exception if the query doesn't have an orderBy clause.
*
* @return void
* @throws \RuntimeException
*/
protected function enforceOrderBy() {
if ( empty( $this->orders ) ) {
throw new \RuntimeException( 'You must specify an orderBy clause when using this function.' );
}
}
/**
* Execute the given callback while selecting the given columns.
*
* After running the callback, the columns are reset to the original value.
*
* @param array $columns
* @param callable $callback
*
* @return mixed
*/
protected function onceWithColumns( $columns, $callback ) {
$original = $this->columns;
if ( is_null( $original ) ) {
$this->columns = $columns;
}
$result = $callback();
$this->columns = $original;
return $result;
}
}