|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lroman242\LaravelCassandra; |
|
4
|
|
|
|
|
5
|
|
|
use Cassandra; |
|
6
|
|
|
use Cassandra\BatchStatement; |
|
7
|
|
|
use \lroman242\LaravelCassandra\Exceptions\CassandraNotSupportedException; |
|
8
|
|
|
|
|
9
|
|
|
class Connection extends \Illuminate\Database\Connection |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The Cassandra keyspace |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $keyspace; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The Cassandra connection handler. |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Cassandra\Session |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $session; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The config |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $config; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The Cassandra cluster |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Cassandra\Cluster |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $cluster; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Connection constructor. |
|
41
|
|
|
* @param Cassandra\Cluster $cluster |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
*/ |
|
44
|
49 |
|
public function __construct(\Cassandra\Cluster $cluster, array $config) |
|
45
|
|
|
{ |
|
46
|
49 |
|
$this->config = $config; |
|
47
|
|
|
|
|
48
|
49 |
|
$this->cluster = $cluster; |
|
49
|
|
|
|
|
50
|
49 |
|
$this->keyspace = $this->getDatabase($config); |
|
51
|
|
|
|
|
52
|
49 |
|
$this->session = $this->cluster->connect($this->keyspace); |
|
53
|
|
|
|
|
54
|
49 |
|
$this->useDefaultPostProcessor(); |
|
55
|
|
|
|
|
56
|
49 |
|
$this->useDefaultSchemaGrammar(); |
|
57
|
|
|
|
|
58
|
49 |
|
$this->setQueryGrammar($this->getDefaultQueryGrammar()); |
|
59
|
49 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get keyspace name from config |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $config |
|
65
|
|
|
* |
|
66
|
|
|
* @return string|null |
|
67
|
|
|
*/ |
|
68
|
49 |
|
protected function getDatabase(array $config) |
|
69
|
|
|
{ |
|
70
|
49 |
|
$keyspaceName = null; |
|
71
|
|
|
|
|
72
|
49 |
|
if (isset($config['keyspace'])) { |
|
73
|
49 |
|
$keyspaceName = $config['keyspace']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
49 |
|
return $keyspaceName; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Begin a fluent query against a database table. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $table |
|
83
|
|
|
* @return Query\Builder |
|
84
|
|
|
*/ |
|
85
|
5 |
|
public function table($table) |
|
86
|
|
|
{ |
|
87
|
5 |
|
$processor = $this->getPostProcessor(); |
|
88
|
|
|
|
|
89
|
5 |
|
$query = new Query\Builder($this, null, $processor); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
5 |
|
return $query->from($table); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* return Cassandra cluster. |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Cassandra\Cluster |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getCassandraCluster() |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->cluster; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* return Cassandra Session. |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Cassandra\Session |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getCassandraSession() |
|
110
|
|
|
{ |
|
111
|
3 |
|
return $this->session; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Return the Cassandra keyspace |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function getKeyspace() |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->keyspace; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Disconnect from the underlying Cassandra connection. |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function disconnect() |
|
128
|
|
|
{ |
|
129
|
3 |
|
$this->session->close(); |
|
130
|
3 |
|
$this->session = null; |
|
131
|
3 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the PDO driver name. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function getDriverName() |
|
139
|
|
|
{ |
|
140
|
1 |
|
return 'cassandra'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Run a select statement against the database. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $query |
|
147
|
|
|
* @param array $bindings |
|
148
|
|
|
* @param bool $useReadPdo |
|
149
|
|
|
* @param array $customOptions |
|
150
|
|
|
* |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
49 |
|
public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
|
154
|
|
|
{ |
|
155
|
49 |
|
return $this->statement($query, $bindings, $customOptions); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Run an bulk insert statement against the database. |
|
160
|
|
|
* |
|
161
|
|
|
* @param array $queries |
|
162
|
|
|
* @param array $bindings |
|
163
|
|
|
* @param int $type |
|
164
|
|
|
* @param array $customOptions |
|
165
|
|
|
* |
|
166
|
|
|
* @return bool |
|
167
|
|
|
*/ |
|
168
|
7 |
|
public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
|
169
|
|
|
{ |
|
170
|
7 |
|
return $this->batchStatement($queries, $bindings, $type, $customOptions); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Execute a group of queries inside a batch statement against the database. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $queries |
|
177
|
|
|
* @param array $bindings |
|
178
|
|
|
* @param int $type |
|
179
|
|
|
* @param array $customOptions |
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
|
|
public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
|
184
|
|
|
{ |
|
185
|
8 |
|
return $this->run($queries, $bindings, function ($queries, $bindings) use ($type, $customOptions) { |
|
|
|
|
|
|
186
|
8 |
|
if ($this->pretending()) { |
|
187
|
1 |
|
return []; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
7 |
|
$batch = new BatchStatement($type); |
|
191
|
|
|
|
|
192
|
7 |
|
foreach ($queries as $k => $query) { |
|
193
|
7 |
|
$preparedStatement = $this->session->prepare($query); |
|
194
|
7 |
|
$batch->add($preparedStatement, $bindings[$k]); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
7 |
|
return $this->session->execute($batch, $customOptions); |
|
198
|
8 |
|
}); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Execute an CQL statement and return the boolean result. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $query |
|
205
|
|
|
* @param array $bindings |
|
206
|
|
|
* @param array $customOptions |
|
207
|
|
|
* |
|
208
|
|
|
* @return mixed |
|
209
|
|
|
*/ |
|
210
|
49 |
|
public function statement($query, $bindings = [], array $customOptions = []) |
|
211
|
|
|
{ |
|
212
|
49 |
|
return $this->runStatement($query, $bindings, $customOptions); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Because Cassandra is an eventually consistent database, it's not possible to obtain |
|
217
|
|
|
* the affected count for statements so we're just going to return 0, based on the idea |
|
218
|
|
|
* that if the query fails somehow, an exception will be thrown |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $query |
|
221
|
|
|
* @param array $bindings |
|
222
|
|
|
* @param array $customOptions |
|
223
|
|
|
* |
|
224
|
|
|
* @return int |
|
225
|
|
|
*/ |
|
226
|
10 |
|
public function affectingStatement($query, $bindings = [], array $customOptions = []) |
|
227
|
|
|
{ |
|
228
|
10 |
|
return $this->runStatement($query, $bindings, $customOptions, 0, 1); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @inheritdoc |
|
233
|
|
|
*/ |
|
234
|
49 |
|
protected function getDefaultPostProcessor() |
|
235
|
|
|
{ |
|
236
|
49 |
|
return new Query\Processor(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @inheritdoc |
|
241
|
|
|
*/ |
|
242
|
49 |
|
protected function getDefaultQueryGrammar() |
|
243
|
|
|
{ |
|
244
|
49 |
|
return new Query\Grammar(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @inheritdoc |
|
249
|
|
|
*/ |
|
250
|
49 |
|
protected function getDefaultSchemaGrammar() |
|
251
|
|
|
{ |
|
252
|
|
|
//return new Schema\Grammar(); |
|
253
|
49 |
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Reconnect to the database if connection is missing. |
|
257
|
|
|
* |
|
258
|
|
|
* @return void |
|
259
|
|
|
*/ |
|
260
|
49 |
|
protected function reconnectIfMissingConnection() |
|
261
|
|
|
{ |
|
262
|
49 |
|
if (is_null($this->session)) { |
|
263
|
1 |
|
$this->session = $this->cluster->connect($this->keyspace); |
|
264
|
|
|
} |
|
265
|
49 |
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Dynamically pass methods to the connection. |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $method |
|
271
|
|
|
* @param array $parameters |
|
272
|
|
|
* @return mixed |
|
273
|
|
|
*/ |
|
274
|
1 |
|
public function __call($method, $parameters) |
|
275
|
|
|
{ |
|
276
|
1 |
|
return call_user_func_array([$this->session, $method], $parameters); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Execute an CQL statement and return the boolean result. |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $query |
|
283
|
|
|
* @param array $bindings |
|
284
|
|
|
* @param array $customOptions |
|
285
|
|
|
* @param mixed $defaultFailed |
|
286
|
|
|
* @param mixed $defaultSuccess |
|
287
|
|
|
* |
|
288
|
|
|
* @return mixed |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
|
291
|
|
|
{ |
|
292
|
49 |
|
return $this->run($query, $bindings, function ($query, $bindings) use ($customOptions, $defaultFailed, $defaultSuccess) { |
|
293
|
49 |
|
if ($this->pretending()) { |
|
294
|
2 |
|
return $defaultFailed; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
49 |
|
$preparedStatement = $this->session->prepare($query); |
|
298
|
|
|
|
|
299
|
|
|
//Add bindings |
|
300
|
49 |
|
$customOptions['arguments'] = $bindings; |
|
301
|
|
|
|
|
302
|
49 |
|
$result = $this->session->execute($preparedStatement, $customOptions); |
|
303
|
|
|
|
|
304
|
49 |
|
return $defaultSuccess === null ? $result : $defaultSuccess; |
|
305
|
49 |
|
}); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @inheritDoc |
|
310
|
|
|
*/ |
|
311
|
1 |
|
public function transaction(\Closure $callback, $attempts = 1) |
|
312
|
|
|
{ |
|
313
|
1 |
|
throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @inheritDoc |
|
318
|
|
|
*/ |
|
319
|
1 |
|
public function beginTransaction() |
|
320
|
|
|
{ |
|
321
|
1 |
|
throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @inheritDoc |
|
326
|
|
|
*/ |
|
327
|
1 |
|
public function commit() |
|
328
|
|
|
{ |
|
329
|
1 |
|
throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @inheritDoc |
|
334
|
|
|
*/ |
|
335
|
1 |
|
public function rollBack() |
|
336
|
|
|
{ |
|
337
|
1 |
|
throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @inheritDoc |
|
342
|
|
|
*/ |
|
343
|
1 |
|
public function transactionLevel() |
|
344
|
|
|
{ |
|
345
|
1 |
|
throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
//TODO: override isDoctrineAvailable method |
|
349
|
|
|
//TODO: override getDoctrineColumn method |
|
350
|
|
|
//TODO: override getDoctrineSchemaManager method |
|
351
|
|
|
//TODO: override getDoctrineConnection method |
|
352
|
|
|
//TODO: override getPdo method |
|
353
|
|
|
//TODO: override getReadPdo method |
|
354
|
|
|
//TODO: override setPdo method |
|
355
|
|
|
//TODO: override setReadPdo method |
|
356
|
|
|
//TODO: override setReconnector method |
|
357
|
|
|
//TODO: override reconnect method |
|
358
|
|
|
//TODO: override query method |
|
359
|
|
|
|
|
360
|
|
|
//TODO: override bindValues method |
|
361
|
|
|
//TODO: override cursor method |
|
362
|
|
|
//TODO: override unprepared method |
|
363
|
|
|
|
|
364
|
|
|
//TODO: check prepareBindings method |
|
365
|
|
|
|
|
366
|
|
|
//TODO: provide interface for $this->session->executeAsync |
|
367
|
|
|
//TODO: provide interface for $this->session->prepareAsync |
|
368
|
|
|
//TODO: provide interface for $this->session->closeAsync |
|
369
|
|
|
//TODO: provide interface for $this->session->schema |
|
370
|
|
|
} |
|
371
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: