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