1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace fuitad\LaravelCassandra; |
4
|
|
|
|
5
|
|
|
use Cassandra; |
6
|
|
|
use Cassandra\BatchStatement; |
7
|
|
|
use Cassandra\ExecutionOptions; |
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 cluster |
20
|
|
|
* |
21
|
|
|
* @var \Cassandra\Cluster |
22
|
|
|
*/ |
23
|
|
|
protected $cluster; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The Cassandra connection handler. |
27
|
|
|
* |
28
|
|
|
* @var \Cassandra\Session |
29
|
|
|
*/ |
30
|
|
|
protected $session; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new database connection instance. |
34
|
|
|
* |
35
|
|
|
* @param array $config |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $config) |
38
|
|
|
{ |
39
|
|
|
$this->config = $config; |
40
|
|
|
|
41
|
|
|
// You can pass options directly to the Cassandra constructor |
42
|
|
|
$options = array_get($config, 'options', []); |
43
|
|
|
|
44
|
|
|
// Create the connection |
45
|
|
|
$this->cluster = $this->createCluster(null, $config, $options); |
46
|
|
|
|
47
|
|
|
if (isset($options['keyspace']) || isset($config['keyspace'])) { |
48
|
|
|
$this->keyspace = $config['keyspace']; |
49
|
|
|
$this->session = $this->cluster->connect($config['keyspace']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->useDefaultPostProcessor(); |
53
|
|
|
|
54
|
|
|
$this->useDefaultSchemaGrammar(); |
55
|
|
|
|
56
|
|
|
$this->setQueryGrammar($this->getDefaultQueryGrammar()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Begin a fluent query against a database table. |
61
|
|
|
* |
62
|
|
|
* @param string $table |
63
|
|
|
* @return Query\Builder |
64
|
|
|
*/ |
65
|
|
|
public function table($table) |
66
|
|
|
{ |
67
|
|
|
$processor = $this->getPostProcessor(); |
68
|
|
|
|
69
|
|
|
$query = new Query\Builder($this, $processor); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $query->from($table); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a schema builder instance for the connection. |
76
|
|
|
* |
77
|
|
|
* @return Schema\Builder |
78
|
|
|
*/ |
79
|
|
|
public function getSchemaBuilder() |
80
|
|
|
{ |
81
|
|
|
return new Schema\Builder($this); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* return Cassandra cluster. |
86
|
|
|
* |
87
|
|
|
* @return \Cassandra\Cluster |
88
|
|
|
*/ |
89
|
|
|
public function getCassandraCluster() |
90
|
|
|
{ |
91
|
|
|
return $this->cluster; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* return Cassandra Session. |
96
|
|
|
* |
97
|
|
|
* @return \Cassandra\Session |
98
|
|
|
*/ |
99
|
|
|
public function getCassandraSession() |
100
|
|
|
{ |
101
|
|
|
return $this->session; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the Cassandra keyspace |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getKeyspace() |
110
|
|
|
{ |
111
|
|
|
return $this->keyspace; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create a new Cassandra cluster object. |
116
|
|
|
* |
117
|
|
|
* @param string $dsn |
118
|
|
|
* @param array $config |
119
|
|
|
* @param array $options |
120
|
|
|
* @return \Cassandra\Cluster |
121
|
|
|
*/ |
122
|
|
|
protected function createCluster($dsn, array $config, array $options) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
// By default driver options is an empty array. |
125
|
|
|
$driverOptions = []; |
|
|
|
|
126
|
|
|
|
127
|
|
|
if (isset($config['driver_options']) && is_array($config['driver_options'])) { |
128
|
|
|
$driverOptions = $config['driver_options']; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$cluster = Cassandra::cluster(); |
132
|
|
|
|
133
|
|
|
// Check if the credentials are not already set in the options |
134
|
|
View Code Duplication |
if (!isset($options['username']) && !empty($config['username'])) { |
|
|
|
|
135
|
|
|
$options['username'] = $config['username']; |
136
|
|
|
} |
137
|
|
View Code Duplication |
if (!isset($options['password']) && !empty($config['password'])) { |
|
|
|
|
138
|
|
|
$options['password'] = $config['password']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Authentication |
142
|
|
|
if (isset($options['username']) && isset($options['password'])) { |
143
|
|
|
$cluster->withCredentials($options['username'], $options['password']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Contact Points/Host |
147
|
|
|
if (isset($options['contactpoints']) || (isset($config['host']) && !empty($config['host']))) { |
148
|
|
|
$contactPoints = $config['host']; |
149
|
|
|
|
150
|
|
|
if (isset($options['contactpoints'])) { |
151
|
|
|
$contactPoints = $options['contactpoints']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$cluster->withContactPoints($contactPoints); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!isset($options['port']) && !empty($config['port'])) { |
158
|
|
|
$cluster->withPort((int) $config['port']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $cluster->build(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Disconnect from the underlying Cassandra connection. |
166
|
|
|
*/ |
167
|
|
|
public function disconnect() |
168
|
|
|
{ |
169
|
|
|
unset($this->connection); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the PDO driver name. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getDriverName() |
178
|
|
|
{ |
179
|
|
|
return 'cassandra'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Run a select statement against the database. |
184
|
|
|
* |
185
|
|
|
* @param string $query |
186
|
|
|
* @param array $bindings |
187
|
|
|
* @param bool $useReadPdo |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
public function select($query, $bindings = [], $useReadPdo = true) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) { |
193
|
|
|
if ($this->pretending()) { |
194
|
|
|
return []; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$preparedStatement = $this->session->prepare($query); |
198
|
|
|
|
199
|
|
|
return $this->session->execute($preparedStatement, new ExecutionOptions(['arguments' => $bindings])); |
200
|
|
|
}); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Run an bulk insert statement against the database. |
205
|
|
|
* |
206
|
|
|
* @param array $queries |
207
|
|
|
* @param array $bindings |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
public function insertBulk($queries, $bindings = [], $type = Cassandra::BATCH_LOGGED) |
211
|
|
|
{ |
212
|
|
|
return $this->batchStatement($queries, $bindings, $type); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Execute a group of queries inside a batch statement against the database. |
217
|
|
|
* |
218
|
|
|
* @param array $queries |
219
|
|
|
* @param array $bindings |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
return $this->run($queries, $bindings, function ($queries, $bindings) { |
|
|
|
|
225
|
|
|
if ($this->pretending()) { |
226
|
|
|
return []; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$batch = new BatchStatement(Cassandra::BATCH_LOGGED); |
230
|
|
|
|
231
|
|
|
foreach ($queries as $k => $query) { |
232
|
|
|
$preparedStatement = $this->session->prepare($query); |
233
|
|
|
$batch->add($preparedStatement, $bindings[$k]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->session->execute($batch); |
237
|
|
|
}); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Execute an SQL statement and return the boolean result. |
242
|
|
|
* |
243
|
|
|
* @param string $query |
244
|
|
|
* @param array $bindings |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function statement($query, $bindings = []) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
return $this->run($query, $bindings, function ($query, $bindings) { |
250
|
|
|
if ($this->pretending()) { |
251
|
|
|
return []; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$preparedStatement = $this->session->prepare($query); |
255
|
|
|
|
256
|
|
|
return $this->session->execute($preparedStatement, new ExecutionOptions(['arguments' => $bindings])); |
257
|
|
|
}); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Because Cassandra is an eventually consistent database, it's not possible to obtain |
262
|
|
|
* the affected count for statements so we're just going to return 0, based on the idea |
263
|
|
|
* that if the query fails somehow, an exception will be thrown |
264
|
|
|
* |
265
|
|
|
* @param string $query |
266
|
|
|
* @param array $bindings |
267
|
|
|
* @return int |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function affectingStatement($query, $bindings = []) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
return $this->run($query, $bindings, function ($query, $bindings) { |
272
|
|
|
if ($this->pretending()) { |
273
|
|
|
return 0; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$preparedStatement = $this->session->prepare($query); |
277
|
|
|
|
278
|
|
|
$this->session->execute($preparedStatement, new ExecutionOptions(['arguments' => $bindings])); |
279
|
|
|
|
280
|
|
|
return 1; |
281
|
|
|
}); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @inheritdoc |
286
|
|
|
*/ |
287
|
|
|
protected function getDefaultPostProcessor() |
288
|
|
|
{ |
289
|
|
|
return new Query\Processor(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @inheritdoc |
294
|
|
|
*/ |
295
|
|
|
protected function getDefaultQueryGrammar() |
296
|
|
|
{ |
297
|
|
|
return new Query\Grammar(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @inheritdoc |
302
|
|
|
*/ |
303
|
|
|
protected function getDefaultSchemaGrammar() |
304
|
|
|
{ |
305
|
|
|
//return new Schema\Grammar(); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Dynamically pass methods to the connection. |
310
|
|
|
* |
311
|
|
|
* @param string $method |
312
|
|
|
* @param array $parameters |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
public function __call($method, $parameters) |
316
|
|
|
{ |
317
|
|
|
return call_user_func_array([$this->cluster, $method], $parameters); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.