Skip to content

Commit 5b755df

Browse files
committed
FIxed events, date/time types, updated readme
1 parent c80990e commit 5b755df

22 files changed

+740
-165
lines changed

README.md

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ $nodes = [
7777
];
7878

7979
// Create a connection.
80-
$connection = new Cassandra\Connection($nodes, 'my_keyspace');
80+
$connection = new \\Cassandra\Connection($nodes, 'my_keyspace');
8181

8282
//Connect
8383
try
8484
{
8585
$connection->connect();
8686
}
87-
catch (Cassandra\Exception $e)
87+
catch (\Cassandra\Exception $e)
8888
{
8989
echo 'Caught exception: ', $e->getMessage(), "\n";
9090
}
@@ -96,9 +96,9 @@ $connection->setConsistency(Request::CONSISTENCY_QUORUM);
9696
// Run query synchronously.
9797
try
9898
{
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')]);
100100
}
101-
catch (Cassandra\Exception $e)
101+
catch (\Cassandra\Exception $e)
102102
{
103103
}
104104
```
@@ -107,26 +107,26 @@ catch (Cassandra\Exception $e)
107107

108108
```php
109109
// Return a SplFixedArray containing all of the result set.
110-
$rows = $response->fetchAll(); // SplFixedArray
110+
$rows = $result->fetchAll(); // SplFixedArray
111111

112112
// Return a SplFixedArray containing a specified index column from the result set.
113-
$col = $response->fetchCol(); // SplFixedArray
113+
$col = $result->fetchCol(); // SplFixedArray
114114

115115
// 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
117117

118118
// Return the first row of the result set.
119-
$row = $response->fetchRow(); // ArrayObject
119+
$row = $result->fetchRow(); // ArrayObject
120120

121121
// Return the first column of the first row of the result set.
122-
$value = $response->fetchOne(); // mixed
122+
$value = $result->fetchOne(); // mixed
123123
```
124124

125125
## Iterate over result
126126
```php
127127
// 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)
130130
{
131131
echo $rowContent['role']."\n";
132132
}
@@ -141,15 +141,15 @@ try
141141
$statement1 = $connection->queryAsync($cql1);
142142
$statement2 = $connection->queryAsync($cql2);
143143

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();
147147

148148

149-
$rows1 = $response1->fetchAll();
150-
$rows2 = $response2->fetchAll();
149+
$rows1 = $result1->fetchAll();
150+
$rows2 = $result2->fetchAll();
151151
}
152-
catch (Cassandra\Exception $e)
152+
catch (\Cassandra\Exception $e)
153153
{
154154
}
155155
```
@@ -159,53 +159,53 @@ catch (Cassandra\Exception $e)
159159
```php
160160
$preparedData = $connection->prepare('SELECT * FROM "users" WHERE "id" = :id');
161161

162-
$strictValues = Cassandra\Request\Request::strictTypeValues(
162+
$strictValues = \Cassandra\Request\Request::strictTypeValues(
163163
[
164164
'id' => 'c5420d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
165165
],
166166
$preparedData['metadata']['columns']
167167
);
168168

169-
$response = $connection->executeSync(
169+
$result = $connection->executeSync(
170170
$preparedData['id'],
171171
$strictValues,
172-
Cassandra\Request\Request::CONSISTENCY_QUORUM,
172+
\Cassandra\Request\Request::CONSISTENCY_QUORUM,
173173
[
174174
'page_size' => 100,
175175
'names_for_values' => true,
176176
'skip_metadata' => true,
177177
]
178178
);
179179

180-
$response->setMetadata($preparedData['result_metadata']);
181-
$rows = $response->fetchAll();
180+
$result->setMetadata($preparedData['result_metadata']);
181+
$rows = $result->fetchAll();
182182
```
183183

184184
## Using Batch
185185

186186
```php
187-
$batchRequest = new Cassandra\Request\Batch();
187+
$batchRequest = new \Cassandra\Request\Batch();
188188

189189
// Append a prepared query
190190
$preparedData = $connection->prepare('UPDATE "students" SET "age" = :age WHERE "id" = :id');
191191
$values = [
192192
'age' => 21,
193193
'id' => 'c5419d81-499e-4c9c-ac0c-fa6ba3ebc2bc',
194194
];
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']));
196196

197197
// Append a query string
198198
$batchRequest->appendQuery(
199199
'INSERT INTO "students" ("id", "name", "age") VALUES (:id, :name, :age)',
200200
[
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'),
203203
'age' => 20,
204204
]
205205
);
206206

207-
$response = $connection->syncRequest($batchRequest);
208-
$rows = $response->fetchAll();
207+
$result = $connection->batchSync($batchRequest);
208+
$rows = $result->fetchAll();
209209
```
210210

211211
## Supported datatypes
@@ -214,97 +214,97 @@ All types are supported.
214214

215215
```php
216216
// Ascii
217-
new Cassandra\Type\Ascii('string');
217+
new \Cassandra\Type\Ascii('string');
218218

219219
// Bigint
220-
new Cassandra\Type\Bigint(10000000000);
220+
new \Cassandra\Type\Bigint(10000000000);
221221

222222
// Blob
223-
new Cassandra\Type\Blob('string');
223+
new \Cassandra\Type\Blob('string');
224224

225225
// Boolean
226-
new Cassandra\Type\Boolean(true);
226+
new \Cassandra\Type\Boolean(true);
227227

228228
// Counter
229-
new Cassandra\Type\Counter(1000);
229+
new \Cassandra\Type\Counter(1000);
230230

231231
// 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);
234234

235235
// Decimal
236-
new Cassandra\Type\Decimal('0.0123');
236+
new \Cassandra\Type\Decimal('0.0123');
237237

238238
// Double
239-
new Cassandra\Type\Double(2.718281828459);
239+
new \Cassandra\Type\Double(2.718281828459);
240240

241241
// Duration
242-
new Cassandra\Type\Duration(['months' => 1, 'days' => 2, 'nanoseconds'=> 3]);
242+
new \Cassandra\Type\Duration(['months' => 1, 'days' => 2, 'nanoseconds'=> 3]);
243243

244244
// Float
245-
new Cassandra\Type\PhpFloat(2.718);
245+
new \Cassandra\Type\PhpFloat(2.718);
246246

247247
// Inet
248-
new Cassandra\Type\Inet('127.0.0.1');
248+
new \Cassandra\Type\Inet('127.0.0.1');
249249

250250
// Int
251-
new Cassandra\Type\PhpInt(12345678);
251+
new \Cassandra\Type\PhpInt(12345678);
252252

253253
// Smallint
254-
new Cassandra\Type\Smallint(2048);
254+
new \Cassandra\Type\Smallint(2048);
255255

256256
// Tinyint
257-
new Cassandra\Type\Tinyint(122);
257+
new \Cassandra\Type\Tinyint(122);
258258

259259
// 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]);
261261

262262
// 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]);
264264

265265
// 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]);
267267

268268
// Time (nanoseconds since midnight)
269-
new Cassandra\Type\Time(18000000000000);
269+
new \Cassandra\Type\Time(18000000000000);
270270

271271
// 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);
274274

275275
// Uuid
276-
new Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');
276+
new \Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');
277277

278278
// Timeuuid
279-
new Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509');
279+
new \Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509');
280280

281281
// Varchar
282-
new Cassandra\Type\Varchar('string');
282+
new \Cassandra\Type\Varchar('string');
283283

284284
// Varint
285-
new Cassandra\Type\Varint(10000000000);
285+
new \Cassandra\Type\Varint(10000000000);
286286

287287
// Custom
288-
new Cassandra\Type\Custom('string', 'var_name');
288+
new \Cassandra\Type\Custom('string', 'var_name');
289289

290290
// 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]);
292292

293293
// UDT
294-
new Cassandra\Type\UDT([
294+
new \Cassandra\Type\UDT([
295295
'intField' => 1,
296296
'textField' => '2'
297297
], [
298-
'intField' => Cassandra\Type\Base::INT,
299-
'textField' => Cassandra\Type\Base::VARCHAR
298+
'intField' => \Cassandra\Type\Base::INT,
299+
'textField' => \Cassandra\Type\Base::VARCHAR
300300
]); // in the order defined by the type
301301
```
302302

303303
## Using nested datatypes
304304

305305
```php
306306
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean, CollectionList<Text>, CollectionList<UDT>
307-
new Cassandra\Type\CollectionSet([
307+
new \Cassandra\Type\CollectionSet([
308308
[
309309
'id' => 1,
310310
'name' => 'string',
@@ -320,22 +320,22 @@ new Cassandra\Type\CollectionSet([
320320
]
321321
], [
322322
[
323-
'type' => Cassandra\Type\Base::UDT,
323+
'type' => \Cassandra\Type\Base::UDT,
324324
'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,
328328
'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
331331
],
332332
'drinks' => [
333-
'type' => Cassandra\Type\Base::COLLECTION_LIST,
333+
'type' => \Cassandra\Type\Base::COLLECTION_LIST,
334334
'value' => [
335-
'type' => Cassandra\Type\Base::UDT,
335+
'type' => \Cassandra\Type\Base::UDT,
336336
'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
339339
]
340340
]
341341
]
@@ -344,6 +344,29 @@ new Cassandra\Type\CollectionSet([
344344
]);
345345
```
346346

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+
347370
## Inspired by
348371
* [duoshuo/php-cassandra](https://github.com/duoshuo/php-cassandra)
349372

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Todo
22
=====
33

44
* Test basic functions
5-
* Add string support for date/time data types
5+
* Refactor type data validation

0 commit comments

Comments
 (0)