|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lroman242\LaravelCassandra\Query; |
|
4
|
|
|
|
|
5
|
|
|
use lroman242\LaravelCassandra\Collection; |
|
6
|
|
|
use lroman242\LaravelCassandra\Connection; |
|
7
|
|
|
use Illuminate\Database\Query\Builder as BaseBuilder; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
|
|
10
|
|
|
class Builder extends BaseBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Use cassandra filtering |
|
14
|
|
|
* |
|
15
|
|
|
* @var bool |
|
16
|
|
|
*/ |
|
17
|
|
|
public $allowFiltering = false; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Size of fetched page |
|
21
|
|
|
* |
|
22
|
|
|
* @var null|int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $pageSize = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Pagination state token |
|
28
|
|
|
* |
|
29
|
|
|
* @var null|string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $paginationStateToken = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Indicate what amount of pages should be fetched |
|
35
|
|
|
* all or single |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $fetchAllResults = true; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
23 |
|
public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null) |
|
45
|
|
|
{ |
|
46
|
23 |
|
$this->connection = $connection; |
|
47
|
23 |
|
$this->grammar = $grammar ?: $connection->getQueryGrammar(); |
|
48
|
23 |
|
$this->processor = $processor ?: $connection->getPostProcessor(); |
|
49
|
23 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Support "allow filtering" |
|
53
|
|
|
*/ |
|
54
|
|
|
public function allowFiltering($bool = true) { |
|
55
|
|
|
$this->allowFiltering = (bool) $bool; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Insert a new record into the database. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $values |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
16 |
|
public function insert(array $values) |
|
67
|
|
|
{ |
|
68
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
|
69
|
|
|
// bindings are structured in a way that is convenient when building these |
|
70
|
|
|
// inserts statements by verifying these elements are actually an array. |
|
71
|
16 |
|
if (empty($values)) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
16 |
|
if (!is_array(reset($values))) { |
|
76
|
13 |
|
$values = [$values]; |
|
77
|
|
|
|
|
78
|
13 |
|
return $this->connection->insert( |
|
79
|
13 |
|
$this->grammar->compileInsert($this, $values), |
|
80
|
13 |
|
$this->cleanBindings(Arr::flatten($values, 1)) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Here, we'll generate the insert queries for every record and send those |
|
85
|
|
|
// for a batch query |
|
86
|
|
|
else { |
|
87
|
3 |
|
$queries = []; |
|
88
|
3 |
|
$bindings = []; |
|
89
|
|
|
|
|
90
|
3 |
|
foreach ($values as $key => $value) { |
|
91
|
3 |
|
ksort($value); |
|
92
|
|
|
|
|
93
|
3 |
|
$queries[] = $this->grammar->compileInsert($this, $value); |
|
94
|
3 |
|
$bindings[] = $this->cleanBindings(Arr::flatten($value, 1)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
return $this->connection->insertBulk($queries, $bindings); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Execute the query as a "select" statement. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $columns |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Illuminate\Support\Collection |
|
107
|
|
|
*/ |
|
108
|
19 |
|
public function get($columns = ['*']) |
|
109
|
|
|
{ |
|
110
|
19 |
|
$original = $this->columns; |
|
111
|
|
|
|
|
112
|
19 |
|
if (is_null($original)) { |
|
113
|
19 |
|
$this->columns = $columns; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
//Set up custom options |
|
117
|
19 |
|
$options = []; |
|
118
|
19 |
|
if ($this->pageSize !== null && (int) $this->pageSize > 0) { |
|
119
|
|
|
$options['page_size'] = (int) $this->pageSize; |
|
120
|
|
|
} |
|
121
|
19 |
|
if ($this->paginationStateToken !== null) { |
|
122
|
|
|
$options['paging_state_token'] = $this->paginationStateToken; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Process select with custom options |
|
126
|
19 |
|
$results = $this->processor->processSelect($this, $this->runSelect($options)); |
|
127
|
|
|
|
|
128
|
|
|
// Get results from all pages |
|
129
|
19 |
|
$collection = new Collection($results); |
|
130
|
|
|
|
|
131
|
19 |
|
if ($this->fetchAllResults) { |
|
132
|
19 |
|
while (!$collection->isLastPage()) { |
|
133
|
|
|
$collection = $collection->appendNextPage(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
19 |
|
$this->columns = $original; |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
19 |
|
return $collection; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Run the query as a "select" statement against the connection. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $options |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
19 |
|
protected function runSelect(array $options = []) |
|
150
|
|
|
{ |
|
151
|
19 |
|
return $this->connection->select( |
|
152
|
19 |
|
$this->toSql(), $this->getBindings(), !$this->useWritePdo, $options |
|
|
|
|
|
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Set pagination state token to fetch |
|
158
|
|
|
* next page |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $token |
|
161
|
|
|
* |
|
162
|
|
|
* @return Builder |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setPaginationStateToken($token = null) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->paginationStateToken = $token; |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set page size |
|
173
|
|
|
* |
|
174
|
|
|
* @param int $pageSize |
|
175
|
|
|
* |
|
176
|
|
|
* @return Builder |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setPageSize($pageSize = null) |
|
179
|
|
|
{ |
|
180
|
|
|
if ($pageSize !== null) { |
|
181
|
|
|
$this->pageSize = (int) $pageSize; |
|
182
|
|
|
} else { |
|
183
|
|
|
$this->pageSize = $pageSize; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get collection with single page results |
|
191
|
|
|
* |
|
192
|
|
|
* @param $columns array |
|
193
|
|
|
* |
|
194
|
|
|
* @return \Illuminate\Support\Collection |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getPage($columns = ['*']) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->fetchAllResults = false; |
|
199
|
|
|
|
|
200
|
|
|
$result = $this->get($columns); |
|
201
|
|
|
|
|
202
|
|
|
$this->fetchAllResults = true; |
|
203
|
|
|
|
|
204
|
|
|
return $result; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.