Skip to content

Commit b61f333

Browse files
committed
PHP-152 - Add duration support to composite types
1 parent 99dc72d commit b61f333

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

ext/src/Duration.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "php_driver_globals.h"
33
#include "php_driver_types.h"
44

5+
#include "util/hash.h"
56
#include "util/math.h"
67
#include "util/types.h"
78

@@ -215,7 +216,7 @@ static zend_function_entry php_driver_duration_methods[] = {
215216
PHP_FE_END
216217
};
217218

218-
static zend_object_handlers php_driver_duration_handlers;
219+
static php_driver_value_handlers php_driver_duration_handlers;
219220

220221
static HashTable *
221222
php_driver_duration_properties(zval *object TSRMLS_DC)
@@ -270,6 +271,19 @@ php_driver_duration_compare(zval *obj1, zval *obj2 TSRMLS_DC)
270271
return (left->nanos == right->nanos) ? 0 : 1;
271272
}
272273

274+
static unsigned
275+
php_driver_duration_hash_value(zval *obj TSRMLS_DC)
276+
{
277+
php_driver_duration *self = PHP_DRIVER_GET_DURATION(obj);
278+
unsigned hashv = 0;
279+
280+
hashv = php_driver_combine_hash(hashv, (unsigned) self->months);
281+
hashv = php_driver_combine_hash(hashv, (unsigned) self->days);
282+
hashv = php_driver_combine_hash(hashv, (unsigned) self->nanos);
283+
284+
return hashv;
285+
}
286+
273287
static void
274288
php_driver_duration_free(php5to7_zend_object_free *object TSRMLS_DC)
275289
{
@@ -300,7 +314,9 @@ void php_driver_define_Duration(TSRMLS_D)
300314
php_driver_duration_ce->create_object = php_driver_duration_new;
301315

302316
memcpy(&php_driver_duration_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
303-
php_driver_duration_handlers.get_properties = php_driver_duration_properties;
304-
php_driver_duration_handlers.compare_objects = php_driver_duration_compare;
305-
php_driver_duration_handlers.clone_obj = NULL;
317+
php_driver_duration_handlers.std.get_properties = php_driver_duration_properties;
318+
php_driver_duration_handlers.std.compare_objects = php_driver_duration_compare;
319+
320+
php_driver_duration_handlers.hash_value = php_driver_duration_hash_value;
321+
php_driver_duration_handlers.std.clone_obj = NULL;
306322
}

ext/util/collections.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ php_driver_validate_object(zval *object, zval *ztype TSRMLS_DC)
108108
EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Decimal");
109109
}
110110

111+
return 1;
112+
case CASS_VALUE_TYPE_DURATION:
113+
if (!INSTANCE_OF(php_driver_duration_ce)) {
114+
EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Duration");
115+
}
116+
111117
return 1;
112118
case CASS_VALUE_TYPE_TIMESTAMP:
113119
if (!INSTANCE_OF(php_driver_timestamp_ce)) {
@@ -237,6 +243,8 @@ php_driver_value_type(char *type, CassValueType *value_type TSRMLS_DC)
237243
*value_type = CASS_VALUE_TYPE_COUNTER;
238244
} else if (strcmp("decimal", type) == 0) {
239245
*value_type = CASS_VALUE_TYPE_DECIMAL;
246+
} else if (strcmp("duration", type) == 0) {
247+
*value_type = CASS_VALUE_TYPE_DURATION;
240248
} else if (strcmp("double", type) == 0) {
241249
*value_type = CASS_VALUE_TYPE_DOUBLE;
242250
} else if (strcmp("float", type) == 0) {
@@ -281,6 +289,7 @@ php_driver_collection_append(CassCollection *collection, zval *value, CassValueT
281289
php_driver_time *time;
282290
php_driver_uuid *uuid;
283291
php_driver_inet *inet;
292+
php_driver_duration *duration;
284293
size_t size;
285294
cass_byte_t *data;
286295
php_driver_collection *coll;
@@ -361,6 +370,10 @@ php_driver_collection_append(CassCollection *collection, zval *value, CassValueT
361370
CHECK_ERROR(cass_collection_append_decimal(collection, data, size, numeric->data.decimal.scale));
362371
free(data);
363372
break;
373+
case CASS_VALUE_TYPE_DURATION:
374+
duration = PHP_DRIVER_GET_DURATION(value);
375+
CHECK_ERROR(cass_collection_append_duration(collection, duration->months, duration->days, duration->nanos));
376+
break;
364377
case CASS_VALUE_TYPE_INET:
365378
inet = PHP_DRIVER_GET_INET(value);
366379
CHECK_ERROR(cass_collection_append_inet(collection, inet->inet));
@@ -414,6 +427,7 @@ php_driver_tuple_set(CassTuple *tuple, php5to7_ulong index, zval *value, CassVal
414427
php_driver_time *time;
415428
php_driver_uuid *uuid;
416429
php_driver_inet *inet;
430+
php_driver_duration *duration;
417431
size_t size;
418432
cass_byte_t *data;
419433
php_driver_collection *coll;
@@ -499,6 +513,10 @@ php_driver_tuple_set(CassTuple *tuple, php5to7_ulong index, zval *value, CassVal
499513
CHECK_ERROR(cass_tuple_set_decimal(tuple, index, data, size, numeric->data.decimal.scale));
500514
free(data);
501515
break;
516+
case CASS_VALUE_TYPE_DURATION:
517+
duration = PHP_DRIVER_GET_DURATION(value);
518+
CHECK_ERROR(cass_tuple_set_duration(tuple, index, duration->months, duration->days, duration->nanos));
519+
break;
502520
case CASS_VALUE_TYPE_INET:
503521
inet = PHP_DRIVER_GET_INET(value);
504522
CHECK_ERROR(cass_tuple_set_inet(tuple, index, inet->inet));
@@ -554,6 +572,7 @@ php_driver_user_type_set(CassUserType *ut,
554572
php_driver_time *time;
555573
php_driver_uuid *uuid;
556574
php_driver_inet *inet;
575+
php_driver_duration *duration;
557576
size_t size;
558577
cass_byte_t *data;
559578
php_driver_collection *coll;
@@ -639,6 +658,10 @@ php_driver_user_type_set(CassUserType *ut,
639658
CHECK_ERROR(cass_user_type_set_decimal_by_name(ut, name, data, size, numeric->data.decimal.scale));
640659
free(data);
641660
break;
661+
case CASS_VALUE_TYPE_DURATION:
662+
duration = PHP_DRIVER_GET_DURATION(value);
663+
CHECK_ERROR(cass_user_type_set_duration_by_name(ut, name, duration->months, duration->days, duration->nanos));
664+
break;
642665
case CASS_VALUE_TYPE_INET:
643666
inet = PHP_DRIVER_GET_INET(value);
644667
CHECK_ERROR(cass_user_type_set_inet_by_name(ut, name, inet->inet));

0 commit comments

Comments
 (0)