@@ -77,14 +77,14 @@ $nodes = [
77
77
];
78
78
79
79
// Create a connection.
80
- $connection = new Cassandra\Connection($nodes, 'my_keyspace');
80
+ $connection = new \\ Cassandra\Connection($nodes, 'my_keyspace');
81
81
82
82
//Connect
83
83
try
84
84
{
85
85
$connection->connect();
86
86
}
87
- catch (Cassandra\Exception $e)
87
+ catch (\ Cassandra\Exception $e)
88
88
{
89
89
echo 'Caught exception: ', $e->getMessage(), "\n";
90
90
}
@@ -96,9 +96,9 @@ $connection->setConsistency(Request::CONSISTENCY_QUORUM);
96
96
// Run query synchronously.
97
97
try
98
98
{
99
- $response = $connection->querySync('SELECT * FROM "users" WHERE "id" = ?', [new Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc')]);
99
+ $result = $connection->querySync('SELECT * FROM "users" WHERE "id" = ?', [new \ Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc')]);
100
100
}
101
- catch (Cassandra\Exception $e)
101
+ catch (\ Cassandra\Exception $e)
102
102
{
103
103
}
104
104
```
@@ -107,26 +107,26 @@ catch (Cassandra\Exception $e)
107
107
108
108
``` php
109
109
// Return a SplFixedArray containing all of the result set.
110
- $rows = $response ->fetchAll(); // SplFixedArray
110
+ $rows = $result ->fetchAll(); // SplFixedArray
111
111
112
112
// Return a SplFixedArray containing a specified index column from the result set.
113
- $col = $response ->fetchCol(); // SplFixedArray
113
+ $col = $result ->fetchCol(); // SplFixedArray
114
114
115
115
// Return a assoc array with key-value pairs, the key is the first column, the value is the second column.
116
- $col = $response ->fetchPairs(); // assoc array
116
+ $col = $result ->fetchPairs(); // assoc array
117
117
118
118
// Return the first row of the result set.
119
- $row = $response ->fetchRow(); // ArrayObject
119
+ $row = $result ->fetchRow(); // ArrayObject
120
120
121
121
// Return the first column of the first row of the result set.
122
- $value = $response ->fetchOne(); // mixed
122
+ $value = $result ->fetchOne(); // mixed
123
123
```
124
124
125
125
## Iterate over result
126
126
``` php
127
127
// Print all roles
128
- $response = $connection->querySync("SELECT role FROM system_auth.roles");
129
- foreach($response AS $rowNo => $rowContent)
128
+ $result = $connection->querySync("SELECT role FROM system_auth.roles");
129
+ foreach($result AS $rowNo => $rowContent)
130
130
{
131
131
echo $rowContent['role']."\n";
132
132
}
@@ -141,15 +141,15 @@ try
141
141
$statement1 = $connection->queryAsync($cql1);
142
142
$statement2 = $connection->queryAsync($cql2);
143
143
144
- // Wait until received the response , can be reversed order
145
- $response2 = $statement2->getResponse ();
146
- $response1 = $statement1->getResponse ();
144
+ // Wait until received the result , can be reversed order
145
+ $result2 = $statement2->getResult ();
146
+ $result1 = $statement1->getResult ();
147
147
148
148
149
- $rows1 = $response1 ->fetchAll();
150
- $rows2 = $response2 ->fetchAll();
149
+ $rows1 = $result1 ->fetchAll();
150
+ $rows2 = $result2 ->fetchAll();
151
151
}
152
- catch (Cassandra\Exception $e)
152
+ catch (\ Cassandra\Exception $e)
153
153
{
154
154
}
155
155
```
@@ -159,53 +159,53 @@ catch (Cassandra\Exception $e)
159
159
``` php
160
160
$preparedData = $connection->prepare('SELECT * FROM "users" WHERE "id" = :id');
161
161
162
- $strictValues = Cassandra\Request\Request::strictTypeValues(
162
+ $strictValues = \ Cassandra\Request\Request::strictTypeValues(
163
163
[
164
164
'id' => 'c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
165
165
],
166
166
$preparedData['metadata']['columns']
167
167
);
168
168
169
- $response = $connection->executeSync(
169
+ $result = $connection->executeSync(
170
170
$preparedData['id'],
171
171
$strictValues,
172
- Cassandra\Request\Request::CONSISTENCY_QUORUM,
172
+ \ Cassandra\Request\Request::CONSISTENCY_QUORUM,
173
173
[
174
174
'page_size' => 100,
175
175
'names_for_values' => true,
176
176
'skip_metadata' => true,
177
177
]
178
178
);
179
179
180
- $response ->setMetadata($preparedData['result_metadata']);
181
- $rows = $response ->fetchAll();
180
+ $result ->setMetadata($preparedData['result_metadata']);
181
+ $rows = $result ->fetchAll();
182
182
```
183
183
184
184
## Using Batch
185
185
186
186
``` php
187
- $batchRequest = new Cassandra\Request\Batch();
187
+ $batchRequest = new \ Cassandra\Request\Batch();
188
188
189
189
// Append a prepared query
190
190
$preparedData = $connection->prepare('UPDATE "students" SET "age" = :age WHERE "id" = :id');
191
191
$values = [
192
192
'age' => 21,
193
193
'id' => 'c5419d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
194
194
];
195
- $batchRequest->appendQueryId($preparedData['id'], Cassandra\Request\Request::strictTypeValues($values, $preparedData['metadata']['columns']));
195
+ $batchRequest->appendQueryId($preparedData['id'], \ Cassandra\Request\Request::strictTypeValues($values, $preparedData['metadata']['columns']));
196
196
197
197
// Append a query string
198
198
$batchRequest->appendQuery(
199
199
'INSERT INTO "students" ("id", "name", "age") VALUES (:id, :name, :age)',
200
200
[
201
- 'id' => new Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc'),
202
- 'name' => new Cassandra\Type\Varchar('Mark'),
201
+ 'id' => new \ Cassandra\Type\Uuid('c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc'),
202
+ 'name' => new \ Cassandra\Type\Varchar('Mark'),
203
203
'age' => 20,
204
204
]
205
205
);
206
206
207
- $response = $connection->syncRequest ($batchRequest);
208
- $rows = $response ->fetchAll();
207
+ $result = $connection->batchSync ($batchRequest);
208
+ $rows = $result ->fetchAll();
209
209
```
210
210
211
211
## Supported datatypes
@@ -214,97 +214,97 @@ All types are supported.
214
214
215
215
``` php
216
216
// Ascii
217
- new Cassandra\Type\Ascii('string');
217
+ new \ Cassandra\Type\Ascii('string');
218
218
219
219
// Bigint
220
- new Cassandra\Type\Bigint(10000000000);
220
+ new \ Cassandra\Type\Bigint(10000000000);
221
221
222
222
// Blob
223
- new Cassandra\Type\Blob('string');
223
+ new \ Cassandra\Type\Blob('string');
224
224
225
225
// Boolean
226
- new Cassandra\Type\Boolean(true);
226
+ new \ Cassandra\Type\Boolean(true);
227
227
228
228
// Counter
229
- new Cassandra\Type\Counter(1000);
229
+ new \ Cassandra\Type\Counter(1000);
230
230
231
231
// Date (a number of days +- since January 1st, 1970)
232
- new Cassandra\Type\Date(19435);
233
- new Cassandra\Type\Date(-2000);
232
+ new \ Cassandra\Type\Date(19435);
233
+ new \ Cassandra\Type\Date(-2000);
234
234
235
235
// Decimal
236
- new Cassandra\Type\Decimal('0.0123');
236
+ new \ Cassandra\Type\Decimal('0.0123');
237
237
238
238
// Double
239
- new Cassandra\Type\Double(2.718281828459);
239
+ new \ Cassandra\Type\Double(2.718281828459);
240
240
241
241
// Duration
242
- new Cassandra\Type\Duration(['months' => 1, 'days' => 2, 'nanoseconds'=> 3]);
242
+ new \ Cassandra\Type\Duration(['months' => 1, 'days' => 2, 'nanoseconds'=> 3]);
243
243
244
244
// Float
245
- new Cassandra\Type\PhpFloat(2.718);
245
+ new \ Cassandra\Type\PhpFloat(2.718);
246
246
247
247
// Inet
248
- new Cassandra\Type\Inet('127.0.0.1');
248
+ new \ Cassandra\Type\Inet('127.0.0.1');
249
249
250
250
// Int
251
- new Cassandra\Type\PhpInt(12345678);
251
+ new \ Cassandra\Type\PhpInt(12345678);
252
252
253
253
// Smallint
254
- new Cassandra\Type\Smallint(2048);
254
+ new \ Cassandra\Type\Smallint(2048);
255
255
256
256
// Tinyint
257
- new Cassandra\Type\Tinyint(122);
257
+ new \ Cassandra\Type\Tinyint(122);
258
258
259
259
// CollectionList
260
- new Cassandra\Type\CollectionList([1, 1, 1], [Cassandra\Type\Base::INT]);
260
+ new \ Cassandra\Type\CollectionList([1, 1, 1], [\ Cassandra\Type\Base::INT]);
261
261
262
262
// CollectionMap
263
- new Cassandra\Type\CollectionMap(['a' => 1, 'b' => 2], [Cassandra\Type\Base::ASCII, Cassandra\Type\Base::INT]);
263
+ new \ Cassandra\Type\CollectionMap(['a' => 1, 'b' => 2], [\ Cassandra\Type\Base::ASCII, \ Cassandra\Type\Base::INT]);
264
264
265
265
// CollectionSet
266
- new Cassandra\Type\CollectionSet([1, 2, 3], [Cassandra\Type\Base::INT]);
266
+ new \ Cassandra\Type\CollectionSet([1, 2, 3], [\ Cassandra\Type\Base::INT]);
267
267
268
268
// Time (nanoseconds since midnight)
269
- new Cassandra\Type\Time(18000000000000);
269
+ new \ Cassandra\Type\Time(18000000000000);
270
270
271
271
// Timestamp (unit: millisecond)
272
- new Cassandra\Type\Timestamp((int) (microtime(true) * 1000));
273
- new Cassandra\Type\Timestamp(1409830696263);
272
+ new \ Cassandra\Type\Timestamp((int) (microtime(true) * 1000));
273
+ new \ Cassandra\Type\Timestamp(1409830696263);
274
274
275
275
// Uuid
276
- new Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');
276
+ new \ Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');
277
277
278
278
// Timeuuid
279
- new Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509');
279
+ new \ Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509');
280
280
281
281
// Varchar
282
- new Cassandra\Type\Varchar('string');
282
+ new \ Cassandra\Type\Varchar('string');
283
283
284
284
// Varint
285
- new Cassandra\Type\Varint(10000000000);
285
+ new \ Cassandra\Type\Varint(10000000000);
286
286
287
287
// Custom
288
- new Cassandra\Type\Custom('string', 'var_name');
288
+ new \ Cassandra\Type\Custom('string', 'var_name');
289
289
290
290
// Tuple
291
- new Cassandra\Type\Tuple([1, '2'], [Cassandra\Type\Base::INT, Cassandra\Type\Base::VARCHAR]);
291
+ new \ Cassandra\Type\Tuple([1, '2'], [\ Cassandra\Type\Base::INT, \ Cassandra\Type\Base::VARCHAR]);
292
292
293
293
// UDT
294
- new Cassandra\Type\UDT([
294
+ new \ Cassandra\Type\UDT([
295
295
'intField' => 1,
296
296
'textField' => '2'
297
297
], [
298
- 'intField' => Cassandra\Type\Base::INT,
299
- 'textField' => Cassandra\Type\Base::VARCHAR
298
+ 'intField' => \ Cassandra\Type\Base::INT,
299
+ 'textField' => \ Cassandra\Type\Base::VARCHAR
300
300
]); // in the order defined by the type
301
301
```
302
302
303
303
## Using nested datatypes
304
304
305
305
``` php
306
306
// CollectionSet<UDT >, where UDT contains: Int, Text, Boolean, CollectionList<Text >, CollectionList<UDT >
307
- new Cassandra\Type\CollectionSet([
307
+ new \ Cassandra\Type\CollectionSet([
308
308
[
309
309
'id' => 1,
310
310
'name' => 'string',
@@ -320,22 +320,22 @@ new Cassandra\Type\CollectionSet([
320
320
]
321
321
], [
322
322
[
323
- 'type' => Cassandra\Type\Base::UDT,
323
+ 'type' => \ Cassandra\Type\Base::UDT,
324
324
'definition' => [
325
- 'id' => Cassandra\Type\Base::INT,
326
- 'name' => Cassandra\Type\Base::VARCHAR,
327
- 'active' => Cassandra\Type\Base::BOOLEAN,
325
+ 'id' => \ Cassandra\Type\Base::INT,
326
+ 'name' => \ Cassandra\Type\Base::VARCHAR,
327
+ 'active' => \ Cassandra\Type\Base::BOOLEAN,
328
328
'friends' => [
329
- 'type' => Cassandra\Type\Base::COLLECTION_LIST,
330
- 'value' => Cassandra\Type\Base::VARCHAR
329
+ 'type' => \ Cassandra\Type\Base::COLLECTION_LIST,
330
+ 'value' => \ Cassandra\Type\Base::VARCHAR
331
331
],
332
332
'drinks' => [
333
- 'type' => Cassandra\Type\Base::COLLECTION_LIST,
333
+ 'type' => \ Cassandra\Type\Base::COLLECTION_LIST,
334
334
'value' => [
335
- 'type' => Cassandra\Type\Base::UDT,
335
+ 'type' => \ Cassandra\Type\Base::UDT,
336
336
'typeMap' => [
337
- 'qty' => Cassandra\Type\Base::INT,
338
- 'brand' => Cassandra\Type\Base::VARCHAR
337
+ 'qty' => \ Cassandra\Type\Base::INT,
338
+ 'brand' => \ Cassandra\Type\Base::VARCHAR
339
339
]
340
340
]
341
341
]
@@ -344,6 +344,29 @@ new Cassandra\Type\CollectionSet([
344
344
]);
345
345
```
346
346
347
+ ## Listening for events
348
+
349
+ ``` php
350
+ $connection->addEventListener(new class () implements \Cassandra\EventListener {
351
+ public function onEvent(\Cassandra\Response\Event $event): void
352
+ {
353
+ var_dump($event->getData());
354
+ }
355
+ });
356
+
357
+ $register = new \Cassandra\Request\Register([
358
+ \Cassandra\Response\Event::TOPOLOGY_CHANGE,
359
+ \Cassandra\Response\Event::STATUS_CHANGE,
360
+ \Cassandra\Response\Event::SCHEMA_CHANGE,
361
+ ]);
362
+
363
+ $connection->syncRequest($register);
364
+
365
+ while ($connection->getResponse()) {
366
+ sleep(1);
367
+ }
368
+ ```
369
+
347
370
## Inspired by
348
371
* [ duoshuo/php-cassandra] ( https://github.com/duoshuo/php-cassandra )
349
372
0 commit comments