-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCassandra.php
281 lines (242 loc) · 6.69 KB
/
Cassandra.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php /** @noinspection PhpComposerExtensionStubsInspection */
namespace Aternos\Model\Driver\Cassandra;
use Aternos\Model\Driver\Driver;
use Aternos\Model\Driver\Features\CRUDAbleInterface;
use Aternos\Model\Driver\Features\QueryableInterface;
use Aternos\Model\ModelInterface;
use Aternos\Model\Query\Generator\SQL;
use Aternos\Model\Query\Query;
use Aternos\Model\Query\QueryResult;
use Cassandra\Exception;
use Cassandra\Rows;
use Cassandra\Session;
use Cassandra\SimpleStatement;
/**
* Class Cassandra
*
* Inherit this class, overwrite the connect function
* and/or the protected connection specific properties
* and register the new class in the driver factory
* for other credentials or connect specifics
*
* @package Aternos\Model\Driver
*/
class Cassandra extends Driver implements CRUDAbleInterface, QueryableInterface
{
public const ID = "cassandra";
protected string $id = self::ID;
/**
* Host address (localhost by default)
*
* Can actually be one or multiple comma separated hosts (contact points)
*
* @var null|string
*/
protected ?string $host = null;
/**
* Host port
*
* @var int|null
*/
protected ?int $port = null;
/**
* Authentication username
*
* @var null|string
*/
protected ?string $user = null;
/**
* Authentication password
*
* @var null|string
*/
protected ?string $password = null;
/**
* Keyspace name
*
* @var string
*/
protected string $keyspace = "data";
/**
* @var Session|null
*/
protected ?Session $connection = null;
/**
* Cassandra constructor.
*
* @param string|null $host
* @param int|null $port
* @param string|null $user
* @param string|null $password
* @param string $keyspace
*/
public function __construct(?string $host = null, ?int $port = null, ?string $user = null, ?string $password = null, string $keyspace = "data")
{
$this->host = $host ?? $this->host;
$this->port = $port ?? $this->port;
$this->user = $user ?? $this->user;
$this->password = $password ?? $this->password;
$this->keyspace = $keyspace ?? $this->keyspace;
}
/**
* Connect to cassandra database
*/
protected function connect(): void
{
if ($this->connection) {
return;
}
$builder = \Cassandra::cluster();
if ($this->host) {
$builder->withContactPoints($this->host);
}
if ($this->port) {
$builder->withPort($this->port);
}
if ($this->user && $this->password) {
$builder->withCredentials($this->user, $this->password);
}
$cluster = $builder->build();
$this->connection = $cluster->connect($this->keyspace);
}
/**
* Execute a cassandra query
*
* @param string $query
* @return Rows
* @throws Exception
*/
protected function rawQuery(string $query): Rows
{
$this->connect();
$statement = new SimpleStatement($query);
return $this->connection->execute($statement, null);
}
/**
* Save the model
*
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function save(ModelInterface $model): bool
{
$table = $model::getName();
$data = json_encode($model);
$data = str_replace("'", "''", $data);
$query = "INSERT INTO " . $table . " JSON '" . $data . "';";
$this->rawQuery($query);
return true;
}
/**
* Get the model
*
* @param class-string<ModelInterface> $modelClass
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws Exception
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
$table = $modelClass::getName();
$id = str_replace("'", "''", $id);
$query = "SELECT * FROM " . $table . " WHERE " . $modelClass::getIdField() . " = '" . $id . "'";
$rows = $this->rawQuery($query);
if ($rows->count() === 0) {
return null;
}
$current = $rows->current();
if ($model) {
return $model->applyData($current);
}
return $modelClass::getModelFromData($current);
}
/**
* Delete the model
*
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function delete(ModelInterface $model): bool
{
$table = $model::getName();
$id = str_replace("'", "''", $model->getId());
$query = "DELETE FROM " . $table . " WHERE " . $model->getIdField() . " = '" . $id . "'";
$this->rawQuery($query);
return true;
}
/**
* Execute a SELECT, UPDATE or DELETE query
*
* @param Query $query
* @return QueryResult
* @throws Exception
*/
public function query(Query $query): QueryResult
{
$this->connect();
$generator = new SQL(function ($value) {
return str_replace("'", "''", $value);
});
$generator->columnEnclosure = "";
$generator->tableEnclosure = "";
$queryString = $generator->generate($query);
$rawQueryResult = $this->rawQuery($queryString);
$result = new QueryResult((bool)$rawQueryResult);
$result->setQueryString($queryString);
foreach ($rawQueryResult as $resultRow) {
/** @var class-string<ModelInterface> $modelClass */
$modelClass = $query->modelClassName;
$model = $modelClass::getModelFromData($resultRow);
$result->add($model);
}
return $result;
}
/**
* @param string|null $host
* @return Cassandra
*/
public function setHost(?string $host): Cassandra
{
$this->host = $host;
return $this;
}
/**
* @param int|null $port
* @return Cassandra
*/
public function setPort(?int $port): Cassandra
{
$this->port = $port;
return $this;
}
/**
* @param string|null $user
* @return Cassandra
*/
public function setUser(?string $user): Cassandra
{
$this->user = $user;
return $this;
}
/**
* @param string|null $password
* @return Cassandra
*/
public function setPassword(?string $password): Cassandra
{
$this->password = $password;
return $this;
}
/**
* @param string $keyspace
* @return Cassandra
*/
public function setKeyspace(string $keyspace): Cassandra
{
$this->keyspace = $keyspace;
return $this;
}
}