Skip to content

Commit 7e644f1

Browse files
PHP-127 - Duration type (#96)
PHP-127 - Add Duration type support
1 parent 9345b40 commit 7e644f1

14 files changed

+579
-0
lines changed

ext/config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ if test "$PHP_CASSANDRA" != "no"; then
4444
src/DefaultSchema.c \
4545
src/DefaultSession.c \
4646
src/DefaultTable.c \
47+
src/Duration.c \
4748
src/Exception.c \
4849
src/ExecutionOptions.c \
4950
src/Float.c \

ext/config.w32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ if (PHP_CASSANDRA != "no") {
118118
"DefaultSchema.c " +
119119
"DefaultSession.c " +
120120
"DefaultTable.c " +
121+
"Duration.c " +
121122
"Exception.c " +
122123
"ExecutionOptions.c " +
123124
"Float.c " +

ext/doc/Cassandra/Duration.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2016 DataStax, Inc.
5+
This software can be used solely with DataStax Enterprise. Please consult the
6+
license at http://www.datastax.com/terms/datastax-Cassandra-driver-license-terms
7+
*/
8+
9+
namespace Cassandra;
10+
11+
/**
12+
* A PHP representation of the CQL `duration` datatype
13+
*/
14+
final class Duration implements Value {
15+
16+
/**
17+
* @param long|double|string|Bigint $months Months attribute of the duration.
18+
* @param long|double|string|Bigint $days Days attribute of the duration.
19+
* @param long|double|string|Bigint $nanos Nanos attribute of the duration.
20+
*/
21+
public function __construct($months, $days, $nanos) { }
22+
23+
/**
24+
* @return Type the Cassandra type for Duration
25+
*/
26+
public function type() { }
27+
28+
/**
29+
* @return string the months attribute of this Duration
30+
*/
31+
public function months() { }
32+
33+
/**
34+
* @return string the days attribute of this Duration
35+
*/
36+
public function days() { }
37+
38+
/**
39+
* @return string the nanoseconds attribute of this Duration
40+
*/
41+
public function nanos() { }
42+
43+
/**
44+
* @return string string representation of this Duration; may be used as a literal parameter in CQL queries.
45+
*/
46+
public function __toString() { }
47+
48+
}

ext/php_driver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ PHP_MINIT_FUNCTION(php_driver)
493493
php_driver_define_Uuid(TSRMLS_C);
494494
php_driver_define_Varint(TSRMLS_C);
495495
php_driver_define_Custom(TSRMLS_C);
496+
php_driver_define_Duration(TSRMLS_C);
496497

497498
php_driver_define_Set(TSRMLS_C);
498499
php_driver_define_Map(TSRMLS_C);

ext/php_driver_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_driver)
2626
php5to7_zval type_timeuuid;
2727
php5to7_zval type_smallint;
2828
php5to7_zval type_tinyint;
29+
php5to7_zval type_duration;
2930
ZEND_END_MODULE_GLOBALS(php_driver)
3031

3132
ZEND_EXTERN_MODULE_GLOBALS(php_driver)

ext/php_driver_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#define PHP_DRIVER_GET_TYPE(obj) php_driver_type_object_fetch(Z_OBJ_P(obj))
7474
#define PHP_DRIVER_GET_RETRY_POLICY(obj) php_driver_retry_policy_object_fetch(Z_OBJ_P(obj))
7575
#define PHP_DRIVER_GET_TIMESTAMP_GEN(obj) php_driver_timestamp_gen_object_fetch(Z_OBJ_P(obj))
76+
#define PHP_DRIVER_GET_DURATION(obj) php_driver_duration_object_fetch(Z_OBJ_P(obj))
7677
#else
7778
#define PHP_DRIVER_GET_NUMERIC(obj) ((php_driver_numeric *)zend_object_store_get_object((obj) TSRMLS_CC))
7879
#define PHP_DRIVER_GET_BLOB(obj) ((php_driver_blob *)zend_object_store_get_object((obj) TSRMLS_CC))
@@ -110,6 +111,7 @@
110111
#define PHP_DRIVER_GET_TYPE(obj) ((php_driver_type *)zend_object_store_get_object((obj) TSRMLS_CC))
111112
#define PHP_DRIVER_GET_RETRY_POLICY(obj) ((php_driver_retry_policy *)zend_object_store_get_object((obj) TSRMLS_CC))
112113
#define PHP_DRIVER_GET_TIMESTAMP_GEN(obj) ((php_driver_timestamp_gen *)zend_object_store_get_object((obj) TSRMLS_CC))
114+
#define PHP_DRIVER_GET_DURATION(obj) ((php_driver_duration *)zend_object_store_get_object((obj) TSRMLS_CC))
113115
#endif
114116

115117
typedef enum {
@@ -171,6 +173,12 @@ PHP_DRIVER_BEGIN_OBJECT_TYPE(inet)
171173
CassInet inet;
172174
PHP_DRIVER_END_OBJECT_TYPE(inet)
173175

176+
PHP_DRIVER_BEGIN_OBJECT_TYPE(duration)
177+
cass_int64_t months;
178+
cass_int64_t days;
179+
cass_int64_t nanos;
180+
PHP_DRIVER_END_OBJECT_TYPE(duration)
181+
174182
PHP_DRIVER_BEGIN_OBJECT_TYPE(collection)
175183
php5to7_zval type;
176184
HashTable values;
@@ -521,6 +529,7 @@ extern PHP_DRIVER_API zend_class_entry *php_driver_uuid_ce;
521529
extern PHP_DRIVER_API zend_class_entry *php_driver_timeuuid_ce;
522530
extern PHP_DRIVER_API zend_class_entry *php_driver_varint_ce;
523531
extern PHP_DRIVER_API zend_class_entry *php_driver_custom_ce;
532+
extern PHP_DRIVER_API zend_class_entry *php_driver_duration_ce;
524533

525534
extern PHP_DRIVER_API zend_class_entry *php_driver_set_ce;
526535
extern PHP_DRIVER_API zend_class_entry *php_driver_map_ce;
@@ -578,6 +587,7 @@ void php_driver_define_Uuid(TSRMLS_D);
578587
void php_driver_define_Timeuuid(TSRMLS_D);
579588
void php_driver_define_Varint(TSRMLS_D);
580589
void php_driver_define_Custom(TSRMLS_D);
590+
void php_driver_define_Duration(TSRMLS_D);
581591

582592
/* Classes */
583593
extern PHP_DRIVER_API zend_class_entry *php_driver_core_ce;

ext/src/DefaultSession.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ bind_argument_by_index(CassStatement *statement, size_t index, zval *value TSRML
139139
CHECK_RESULT(cass_statement_bind_inet(statement, index, inet->inet));
140140
}
141141

142+
if (instanceof_function(Z_OBJCE_P(value), php_driver_duration_ce TSRMLS_CC)) {
143+
php_driver_duration *duration = PHP_DRIVER_GET_DURATION(value);
144+
CHECK_RESULT(cass_statement_bind_duration(statement,
145+
index,
146+
duration->months, duration->days, duration->nanos));
147+
}
148+
142149
if (instanceof_function(Z_OBJCE_P(value), php_driver_set_ce TSRMLS_CC)) {
143150
CassError rc;
144151
CassCollection *collection;
@@ -295,6 +302,13 @@ bind_argument_by_name(CassStatement *statement, const char *name,
295302
CHECK_RESULT(cass_statement_bind_inet_by_name(statement, name, inet->inet));
296303
}
297304

305+
if (instanceof_function(Z_OBJCE_P(value), php_driver_duration_ce TSRMLS_CC)) {
306+
php_driver_duration *duration = PHP_DRIVER_GET_DURATION(value);
307+
CHECK_RESULT(cass_statement_bind_duration_by_name(statement,
308+
name,
309+
duration->months, duration->days, duration->nanos));
310+
}
311+
298312
if (instanceof_function(Z_OBJCE_P(value), php_driver_set_ce TSRMLS_CC)) {
299313
CassError rc;
300314
CassCollection *collection;

0 commit comments

Comments
 (0)