package.xml0000644000076500000240000006607713062515666013704 0ustar michaelpenickstaff cassandra pecl.php.net DataStax PHP Driver for Apache Cassandra A modern, feature-rich and highly tunable PHP client library for Apache Cassandra and DataStax Enterprise using exclusively Cassandra's binary protocol and Cassandra Query Language v3. Michael Penick mpenick michael.penick@datastax.com yes 2017-03-16 1.3.0 1.3.0 stable stable Apache License 2.0 # 1.3.0 Features: * Added support for the `duration` type * Added support to pass a query string directly to `Session::execute()` and `Session::executeAsync()` * `ExecutionOptions` has been deprecated in favor of using an array Bug Fixes: * [PHP-86] Decimal __toString method * [PHP-109] Cassandra solr_query not working after upgrading to DSE 5 * [PHP-114] pecl install compiler output is dominated by C90 warnings * [PHP-119] Driver crashes upon getting type of column * [PHP-143] Memory leak in `FutureSession` * [PHP-144] Fix persistent sessions reference counting issue * [PHP-147] Unable to create Bigint with PHP_INT_MIN 5.6.0 7.0.99 1.4.8 cassandra cassandra-1.3.0/config.m40000644000076500000240000002010713062515664015650 0ustar michaelpenickstaffPHP_ARG_WITH(cassandra, Enable Cassandra extension, [ --with-cassandra[=DIR] Enable the Cassandra extension.]) AC_MSG_CHECKING([for supported PHP version]) PHP_CASSANDRA_FOUND_PHP_VERSION=`${PHP_CONFIG} --version` PHP_CASSANDRA_FOUND_PHP_VERSION_NUMBER=`echo "${PHP_CASSANDRA_FOUND_PHP_VERSION}" | $AWK 'BEGIN { FS = "."; } { printf "%d", ([$]1 * 100 + [$]2) * 100 + [$]3;}'` if test "$PHP_CASSANDRA_FOUND_PHP_VERSION_NUMBER" -lt "50600"; then AC_MSG_ERROR([not supported. PHP version 5.6.0+ required (found $PHP_CASSANDRA_FOUND_PHP_VERSION)]) else AC_MSG_RESULT([supported ($PHP_CASSANDRA_FOUND_PHP_VERSION)]) fi if test -z "$PHP_UV"; then PHP_ARG_WITH(uv, libuv install dir, [ --with-uv=DIR Specify libuv install dir.]) fi if test -z "$PHP_GMP"; then PHP_ARG_WITH(gmp, GNU MP install dir, [ --with-gmp=DIR Specify GNU MP install dir.]) fi if test "$PHP_CASSANDRA" != "no"; then CASSANDRA_CLASSES="\ src/Core.c \ src/Aggregate.c \ src/BatchStatement.c \ src/Bigint.c \ src/Blob.c \ src/Cluster.c \ src/Cluster/Builder.c \ src/Collection.c \ src/Column.c \ src/Custom.c \ src/Date.c \ src/Decimal.c \ src/DefaultAggregate.c \ src/DefaultCluster.c \ src/DefaultColumn.c \ src/DefaultFunction.c \ src/DefaultIndex.c \ src/DefaultKeyspace.c \ src/DefaultMaterializedView.c \ src/DefaultSchema.c \ src/DefaultSession.c \ src/DefaultTable.c \ src/Duration.c \ src/Exception.c \ src/ExecutionOptions.c \ src/Float.c \ src/Function.c \ src/Future.c \ src/FutureClose.c \ src/FuturePreparedStatement.c \ src/FutureRows.c \ src/FutureSession.c \ src/FutureValue.c \ src/Index.c \ src/Inet.c \ src/Keyspace.c \ src/Map.c \ src/MaterializedView.c \ src/Numeric.c \ src/PreparedStatement.c \ src/RetryPolicy.c \ src/Rows.c \ src/Schema.c \ src/Session.c \ src/Set.c \ src/SimpleStatement.c \ src/Smallint.c \ src/SSLOptions.c \ src/SSLOptions/Builder.c \ src/Statement.c \ src/Table.c \ src/Time.c \ src/Timestamp.c \ src/TimestampGenerator.c \ src/TimestampGenerator/Monotonic.c \ src/TimestampGenerator/ServerSide.c \ src/Timeuuid.c \ src/Tinyint.c \ src/Tuple.c \ src/Type.c \ src/UserTypeValue.c \ src/Uuid.c \ src/UuidInterface.c \ src/Value.c \ src/Varint.c \ "; CASSANDRA_EXCEPTIONS="\ src/Exception/AlreadyExistsException.c \ src/Exception/AuthenticationException.c \ src/Exception/ConfigurationException.c \ src/Exception/DivideByZeroException.c \ src/Exception/DomainException.c \ src/Exception/ExecutionException.c \ src/Exception/InvalidArgumentException.c \ src/Exception/InvalidQueryException.c \ src/Exception/InvalidSyntaxException.c \ src/Exception/IsBootstrappingException.c \ src/Exception/LogicException.c \ src/Exception/OverloadedException.c \ src/Exception/ProtocolException.c \ src/Exception/RangeException.c \ src/Exception/ReadTimeoutException.c \ src/Exception/RuntimeException.c \ src/Exception/ServerException.c \ src/Exception/TimeoutException.c \ src/Exception/TruncateException.c \ src/Exception/UnauthorizedException.c \ src/Exception/UnavailableException.c \ src/Exception/UnpreparedException.c \ src/Exception/ValidationException.c \ src/Exception/WriteTimeoutException.c "; CASSANDRA_RETRY_POLICIES="\ src/RetryPolicy/DefaultPolicy.c \ src/RetryPolicy/DowngradingConsistency.c \ src/RetryPolicy/Fallthrough.c \ src/RetryPolicy/Logging.c "; CASSANDRA_TYPES="\ src/Type/Collection.c \ src/Type/Custom.c \ src/Type/Map.c \ src/Type/Scalar.c \ src/Type/Set.c \ src/Type/Tuple.c \ src/Type/UserType.c "; CASSANDRA_UTIL="\ util/bytes.c \ util/collections.c \ util/consistency.c \ util/future.c \ util/hash.c \ util/inet.c \ util/math.c \ util/ref.c \ util/result.c \ util/types.c \ util/uuid_gen.c \ "; case $(uname -s) in Linux) CASSANDRA_CFLAGS="-Wall -Wextra -Wno-long-long -Wno-deprecated-declarations -Wno-unused-parameter -Wno-unused-result -Wno-variadic-macros -Wno-extra-semi -pthread" ;; Darwin) CASSANDRA_CFLAGS="-Wall -Wextra -Wno-long-long -Wno-deprecated-declarations -Wno-unused-parameter -Wno-unused-result -Wno-variadic-macros -Wno-extra-semi" ;; esac PHP_NEW_EXTENSION(cassandra, php_driver.c \ $CASSANDRA_CLASSES \ $CASSANDRA_EXCEPTIONS \ $CASSANDRA_RETRY_POLICIES \ $CASSANDRA_TYPES \ $CASSANDRA_UTIL, $ext_shared, , $CASSANDRA_CFLAGS) PHP_ADD_BUILD_DIR($ext_builddir/src) PHP_ADD_BUILD_DIR($ext_builddir/src/Cluster) PHP_ADD_BUILD_DIR($ext_builddir/src/Exception) PHP_ADD_BUILD_DIR($ext_builddir/src/SSLOptions) PHP_ADD_BUILD_DIR($ext_builddir/src/Type) PHP_ADD_BUILD_DIR($ext_builddir/src/RetryPolicy) PHP_ADD_BUILD_DIR($ext_builddir/src/TimestampGenerator) PHP_ADD_BUILD_DIR($ext_builddir/util) PHP_SUBST(CASSANDRA_SHARED_LIBADD) PHP_SUBST(CASSANDRA_CFLAGS) ifdef([PHP_ADD_EXTENSION_DEP], [ PHP_ADD_EXTENSION_DEP(cassandra, spl) ]) if test "$PHP_GMP" != "no"; then if test -f $PHP_GMP/include/gmp.h; then GMP_DIR=$PHP_GMP fi else for i in /usr/local /usr; do if test -f $i/include/gmp.h; then GMP_DIR=$i fi done fi if test -z "$GMP_DIR"; then ac_extra= else ac_extra=-L$GMP_DIR/$PHP_LIBDIR fi PHP_CHECK_LIBRARY(gmp, __gmp_version, [ AC_DEFINE(HAVE_GMPLIB,1,[ ]) ], [ AC_MSG_ERROR([Unable to load libgmp]) ], [ $ac_extra ] ) if test -n "$GMP_DIR"; then PHP_ADD_LIBPATH($GMP_DIR/$PHP_LIBDIR, CASSANDRA_SHARED_LIBADD) PHP_ADD_INCLUDE($GMP_DIR/include) fi PHP_ADD_LIBRARY(gmp,, CASSANDRA_SHARED_LIBADD) if test "$PHP_UV" != "no"; then if test -f $PHP_UV/include/uv.h; then UV_DIR=$PHP_UV fi else for i in /usr/local /usr; do if test -f $i/include/uv.h; then UV_DIR=$i fi done fi if test -z "$UV_DIR"; then ac_extra= else ac_extra=-L$UV_DIR/$PHP_LIBDIR fi PHP_CHECK_LIBRARY(uv, uv_version, [ AC_DEFINE(HAVE_UVLIB,1,[ ]) ], [ AC_MSG_ERROR([Unable to load libuv]) ], [ $ac_extra ] ) if test -n "$UV_DIR"; then PHP_ADD_LIBPATH($UV_DIR/$PHP_LIBDIR, CASSANDRA_SHARED_LIBADD) PHP_ADD_INCLUDE($UV_DIR/include) fi PHP_ADD_LIBRARY(uv,, CASSANDRA_SHARED_LIBADD) PHP_ADD_LIBRARY(m,, CASSANDRA_SHARED_LIBADD) if test "$PHP_CASSANDRA" != "yes"; then if test -f $PHP_CASSANDRA/include/cassandra.h; then CPP_DRIVER_DIR=$PHP_CASSANDRA fi else for i in /usr/local /usr; do if test -f $i/include/cassandra.h; then CPP_DRIVER_DIR=$i fi done fi AC_MSG_CHECKING([for supported DataStax C/C++ driver version]) PHP_CASSANDRA_FOUND_CASSANDRA_VERSION=`$AWK '/CASS_VERSION_MAJOR/ {printf $3"."} /CASS_VERSION_MINOR/ {printf $3"."} /CASS_VERSION_PATCH/ {printf $3}' $CPP_DRIVER_DIR/include/cassandra.h` PHP_CASSANDRA_FOUND_CASSANDRA_VERSION_NUMBER=`echo "${PHP_CASSANDRA_FOUND_CASSANDRA_VERSION}" | $AWK 'BEGIN { FS = "."; } { printf "%d", ([$]1 * 100 + [$]2) * 100 + [$]3;}'` if test "$PHP_CASSANDRA_FOUND_CASSANDRA_VERSION_NUMBER" -lt "20600"; then AC_MSG_ERROR([not supported. Driver version 2.6.0+ required (found $PHP_CASSANDRA_FOUND_CASSANDRA_VERSION)]) else AC_MSG_RESULT([supported ($PHP_CASSANDRA_FOUND_CASSANDRA_VERSION)]) fi if test -z "$CPP_DRIVER_DIR"; then ac_extra= else ac_extra=-L$CPP_DRIVER_DIR/$PHP_LIBDIR fi PHP_CHECK_LIBRARY(cassandra, cass_cluster_new, [ AC_DEFINE(HAVE_CASSANDRALIB,1,[ ]) ], [ AC_MSG_ERROR([Unable to load libcassandra]) ], [ $ac_extra ] ) if test -n "$CPP_DRIVER_DIR"; then PHP_ADD_LIBPATH($CPP_DRIVER_DIR/$PHP_LIBDIR, CASSANDRA_SHARED_LIBADD) PHP_ADD_INCLUDE($CPP_DRIVER_DIR/include) fi PHP_ADD_LIBRARY(cassandra,, CASSANDRA_SHARED_LIBADD) CASSANDRA_SHARED_LIBADD="$CASSANDRA_SHARED_LIBADD $LIBS" fi cassandra-1.3.0/config.w320000644000076500000240000002423513062515664015751 0ustar michaelpenickstaff// vim:ft=javascript ARG_ENABLE("cassandra", "Enable DataStax PHP Cassandra extension", "no"); // Establish the minimum Cassandra C/C++ driver and PHP version var driver_minimum_major = 2; var driver_minimum_minor = 6; var driver_minimum_patch = 0; var driver_minimum_version = driver_minimum_major + "." + driver_minimum_minor + "." + driver_minimum_patch; var php_minimum_major = 5; var php_minimum_minor = 6; var php_minimum_patch = 0; var php_minimum_version = php_minimum_major + "." + php_minimum_minor + "." + php_minimum_patch; function compare_minimum_version(major, minor, patch, minimum_major, minimum_minor, minimum_patch) { // Validate major version if (major < minimum_major) return -1; if (major > minimum_major) return 1; // Validate minor version if (minor < minimum_minor) return -1; if (minor > minimum_minor) return 1; // Validate patch version if (patch < minimum_patch) return -1; if (patch > minimum_patch) return 1; return 0; } function check_cassandra_cpp_driver_version(header_filename) { // Parse the Cassandra C/C++ driver version from the header var major = 0; var minor = 0; var patch = 0; var contents = file_get_contents(header_filename); if (contents.match(new RegExp(".*CASS_VERSION_MAJOR.*(\\d+)"))) { major = RegExp.$1; } if (contents.match(new RegExp(".*CASS_VERSION_MINOR.*(\\d+)"))) { minor = RegExp.$1; } if (contents.match(new RegExp(".*CASS_VERSION_PATCH.*(\\d+)"))) { patch = RegExp.$1; } var version = major + "." + minor + "." + patch; // Ensure the Cassandra C/C++ driver version is valid if (compare_minimum_version(major, minor, patch, driver_minimum_major, driver_minimum_minor, driver_minimum_patch) < 0) { ERROR("Invalid Cassandra C/C++ Driver Version (v" + version + "): v" + driver_minimum_version + "+ is required"); } MESSAGE("\tUsing Cassandra C/C++ driver v" + version); } function check_php_version() { var version = PHP_VERSION + "." + PHP_MINOR_VERSION + "." + PHP_RELEASE_VERSION if (compare_minimum_version(PHP_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, php_minimum_major, php_minimum_minor, php_minimum_patch) < 0) { ERROR("Invalid PHP Version (v" + version + "): v" + php_minimum_version + "+ is required"); } MESSAGE("\tUsing PHP v" + version); } if (PHP_CASSANDRA != "no") { // Allow for DataStax C/C++ and libuv default locations to be overridden ARG_WITH("cassandra-cpp-driver", "Where to find DataStax C/C++ driver", "yes"); ARG_WITH("libuv", "Where to find libuv libraries", "yes"); check_php_version(); if (CHECK_LIB("cassandra_static.lib", "cassandra", PHP_CASSANDRA_CPP_DRIVER) && (PHP_CASSANDRA_CPP_DRIVER_INCLUDE = CHECK_HEADER_ADD_INCLUDE("cassandra.h", "CFLAGS_CASSANDRA", PHP_CASSANDRA_CPP_DRIVER + "/include"))) { check_cassandra_cpp_driver_version(PHP_CASSANDRA_CPP_DRIVER_INCLUDE + "/cassandra.h"); if (CHECK_LIB("libuv.lib", "cassandra", PHP_LIBUV) && CHECK_LIB("iphlpapi.lib", "cassandra") && CHECK_LIB("psapi.lib", "cassandra") && CHECK_LIB("userenv.lib", "cassandra") && // Required for libuv v1.6.0+ CHECK_LIB("ws2_32.lib", "cassandra") && CHECK_LIB("wsock32.lib", "cassandra") && CHECK_HEADER_ADD_INCLUDE("uv.h", "CFLAGS_CASSANDRA", PHP_LIBUV + "/include")) { // Indicate the DataStax C/C++ driver and dependencies are available AC_DEFINE("HAVE_CASSANDRA_CPP_DRIVER", 1, "Have DataStax C/C++ driver"); AC_DEFINE("HAVE_LIBUV", 1, "Have libuv"); if (CHECK_LIB("libeay32.lib", "cassandra") && CHECK_LIB("ssleay32.lib", "cassandra")) { if (!CHECK_LIB("zlib_a.lib", "cassandra")) { WARNING("Unable to Locate zlib Library: Cassandra extension may not link correctly"); } } else { ERROR("Unable to Locate OpenSSL Libraries: https://wiki.php.net/internals/windows/stepbystepbuild"); } if (CHECK_LIB("mpir_a.lib", "cassandra") && CHECK_HEADER_ADD_INCLUDE("gmp.h", "CFLAGS_CASSANDRA", PHP_GMP + ";" + PHP_PHP_BUILD + "/include/mpir")) { if (ADD_EXTENSION_DEP("cassandra", "spl")) { EXTENSION("cassandra", "php_driver.c"); ADD_SOURCES(configure_module_dirname + "/src", "Aggregate.c " + "BatchStatement.c " + "Bigint.c " + "Blob.c " + "Cluster.c " + "Collection.c " + "Column.c " + "Core.c " + "Custom.c " + "Date.c " + "Decimal.c " + "DefaultAggregate.c " + "DefaultCluster.c " + "DefaultColumn.c " + "DefaultFunction.c " + "DefaultIndex.c " + "DefaultKeyspace.c " + "DefaultMaterializedView.c " + "DefaultSchema.c " + "DefaultSession.c " + "DefaultTable.c " + "Duration.c " + "Exception.c " + "ExecutionOptions.c " + "Float.c " + "Function.c " + "Future.c " + "FutureClose.c " + "FuturePreparedStatement.c " + "FutureRows.c " + "FutureSession.c " + "FutureValue.c " + "Index.c " + "Inet.c " + "Keyspace.c " + "Map.c " + "MaterializedView.c " + "Numeric.c " + "PreparedStatement.c " + "RetryPolicy.c " + "Rows.c " + "Schema.c " + "Session.c " + "Set.c " + "SimpleStatement.c " + "Smallint.c " + "SSLOptions.c " + "Statement.c " + "Table.c " + "Time.c " + "Timestamp.c " + "TimestampGenerator.c " + "Timeuuid.c " + "Tinyint.c " + "Tuple.c " + "Type.c " + "UserTypeValue.c " + "Uuid.c " + "UuidInterface.c " + "Value.c " + "Varint.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/Cluster", "Builder.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/Exception", "AlreadyExistsException.c " + "AuthenticationException.c " + "ConfigurationException.c " + "DivideByZeroException.c " + "DomainException.c " + "ExecutionException.c " + "InvalidArgumentException.c " + "InvalidQueryException.c " + "InvalidSyntaxException.c " + "IsBootstrappingException.c " + "LogicException.c " + "OverloadedException.c " + "ProtocolException.c " + "RangeException.c " + "ReadTimeoutException.c " + "RuntimeException.c " + "ServerException.c " + "TimeoutException.c " + "TruncateException.c " + "UnauthorizedException.c " + "UnavailableException.c " + "UnpreparedException.c " + "ValidationException.c " + "WriteTimeoutException.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/RetryPolicy", "DefaultPolicy.c " + "DowngradingConsistency.c " + "Fallthrough.c " + "Logging.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/SSLOptions", "Builder.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/TimestampGenerator", "Monotonic.c " + "ServerSide.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/src/Type", "Collection.c " + "Custom.c " + "Map.c " + "Scalar.c " + "Set.c " + "Tuple.c " + "UserType.c", "cassandra"); ADD_SOURCES(configure_module_dirname + "/util", "bytes.c " + "collections.c " + "consistency.c " + "future.c " + "hash.c " + "inet.c " + "math.c " + "ref.c " + "result.c " + "types.c " + "uuid_gen.c", "cassandra"); ADD_FLAG("LDFLAGS_CASSANDRA", "/INCREMENTAL:NO " + "/LTCG " + "/NODEFAULTLIB:LIBCMT.LIB " + "/NODEFAULTLIB:LIBCMTD.LIB"); CHECK_HEADER_ADD_INCLUDE("timelib_config.h", "CFLAGS_CASSANDRA", "ext/date/lib"); ADD_FLAG("CFLAGS_CASSANDRA", "/I " + configure_module_dirname + " " + "/I " + PHP_CASSANDRA_CPP_DRIVER + "/include " + "/I " + PHP_LIBUV + "/include " + "/DCASS_STATIC " + "/wd4267"); // Determine if MSVC++ stdint definitions should be disabled if (PHP_VERSION == "5" && PHP_MINOR_VERSION <= 4) { ADD_FLAG("CFLAGS_CASSANDRA", "/DDISABLE_MSVC_STDINT"); } // Determine if library deconfliction should occur (DEBUG) if (PHP_DEBUG == "yes" && VCVERS == "1700") { ADD_FLAG("LDFLAGS", "/NODEFAULTLIB:LIBCMT.LIB " + "/NODEFAULTLIB:LIBCMTD.LIB"); // This is needed for vc_build.bat } // Ensure gdi32.lib is linked for all dependency extensions ADD_FLAG("LIBS", "gdi32.lib"); // This is needed for vc_build.bat AC_DEFINE("HAVE_CASSANDRA_EXT", PHP_CASSANDRA_SHARED ? 0 : 1, "Have DataStax PHP driver extension"); } else { ERROR("Unable to Add Dependency SPL: This should never happen (SPL is part of PHP core)"); } } else { ERROR("Unable to Locate MPIR (GMP) Library/Headers: https://wiki.php.net/internals/windows/stepbystepbuild"); } } else { ERROR("Unable to Locate libuv Library/Header: https://wiki.php.net/internals/windows/stepbystepbuild"); } } else { ERROR("Unable to Locate DataStax C/C++ driver Library/Header: https://wiki.php.net/internals/windows/stepbystepbuild"); } } cassandra-1.3.0/php_driver.c0000644000076500000240000004617513062515664016464 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "version.h" #include "util/types.h" #include "util/ref.h" #include #include #ifndef _WIN32 #include #else #pragma message("syslog will be disabled on Windows") #endif #include #include #include #include /* Resources */ #define PHP_DRIVER_CLUSTER_RES_NAME PHP_DRIVER_NAMESPACE " Cluster" #define PHP_DRIVER_SESSION_RES_NAME PHP_DRIVER_NAMESPACE " Session" static uv_once_t log_once = UV_ONCE_INIT; static char *log_location = NULL; static uv_rwlock_t log_lock; #if CURRENT_CPP_DRIVER_VERSION < CPP_DRIVER_VERSION(2, 6, 0) #error C/C++ driver version 2.6.0 or greater required #endif ZEND_DECLARE_MODULE_GLOBALS(php_driver) static PHP_GINIT_FUNCTION(php_driver); static PHP_GSHUTDOWN_FUNCTION(php_driver); const zend_function_entry php_driver_functions[] = { PHP_FE_END /* Must be the last line in php_driver_functions[] */ }; #if ZEND_MODULE_API_NO >= 20050617 static zend_module_dep php_driver_deps[] = { ZEND_MOD_REQUIRED("spl") ZEND_MOD_END }; #endif zend_module_entry php_driver_module_entry = { #if ZEND_MODULE_API_NO >= 20050617 STANDARD_MODULE_HEADER_EX, NULL, php_driver_deps, #elif ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif PHP_DRIVER_NAME, php_driver_functions, /* Functions */ PHP_MINIT(php_driver), /* MINIT */ PHP_MSHUTDOWN(php_driver), /* MSHUTDOWN */ PHP_RINIT(php_driver), /* RINIT */ PHP_RSHUTDOWN(php_driver), /* RSHUTDOWN */ PHP_MINFO(php_driver), /* MINFO */ #if ZEND_MODULE_API_NO >= 20010901 PHP_DRIVER_VERSION, #endif PHP_MODULE_GLOBALS(php_driver), PHP_GINIT(php_driver), PHP_GSHUTDOWN(php_driver), NULL, STANDARD_MODULE_PROPERTIES_EX }; #ifdef COMPILE_DL_CASSANDRA ZEND_GET_MODULE(php_driver) #endif PHP_INI_BEGIN() PHP_DRIVER_INI_ENTRY_LOG PHP_DRIVER_INI_ENTRY_LOG_LEVEL PHP_INI_END() static int le_php_driver_cluster_res; int php_le_php_driver_cluster() { return le_php_driver_cluster_res; } static void php_driver_cluster_dtor(php5to7_zend_resource rsrc TSRMLS_DC) { CassCluster *cluster = (CassCluster*) rsrc->ptr; if (cluster) { cass_cluster_free(cluster); PHP_DRIVER_G(persistent_clusters)--; rsrc->ptr = NULL; } } static int le_php_driver_session_res; int php_le_php_driver_session() { return le_php_driver_session_res; } static void php_driver_session_dtor(php5to7_zend_resource rsrc TSRMLS_DC) { php_driver_psession *psession = (php_driver_psession*) rsrc->ptr; if (psession) { cass_future_free(psession->future); php_driver_del_peref(&psession->session, 1); pefree(psession, 1); PHP_DRIVER_G(persistent_sessions)--; rsrc->ptr = NULL; } } static void php_driver_log(const CassLogMessage *message, void *data); static void php_driver_log_cleanup() { cass_log_cleanup(); uv_rwlock_destroy(&log_lock); if (log_location) { free(log_location); log_location = NULL; } } static void php_driver_log_initialize() { uv_rwlock_init(&log_lock); cass_log_set_level(CASS_LOG_ERROR); cass_log_set_callback(php_driver_log, NULL); } static void php_driver_log(const CassLogMessage *message, void *data) { char log[MAXPATHLEN + 1]; uint log_length = 0; /* Making a copy here because location could be updated by a PHP thread. */ uv_rwlock_rdlock(&log_lock); if (log_location) { log_length = MIN(strlen(log_location), MAXPATHLEN); memcpy(log, log_location, log_length); } uv_rwlock_rdunlock(&log_lock); log[log_length] = '\0'; if (log_length > 0) { FILE *fd = NULL; #ifndef _WIN32 if (!strcmp(log, "syslog")) { php_syslog(LOG_NOTICE, PHP_DRIVER_NAME " | [%s] %s (%s:%d)", cass_log_level_string(message->severity), message->message, message->file, message->line); return; } #endif fd = fopen(log, "a"); if (fd) { time_t log_time; struct tm log_tm; char log_time_str[64]; size_t needed = 0; char *tmp = NULL; time(&log_time); php_localtime_r(&log_time, &log_tm); strftime(log_time_str, sizeof(log_time_str), "%d-%m-%Y %H:%M:%S %Z", &log_tm); needed = snprintf(NULL, 0, "%s [%s] %s (%s:%d)%s", log_time_str, cass_log_level_string(message->severity), message->message, message->file, message->line, PHP_EOL); tmp = malloc(needed + 1); sprintf(tmp, "%s [%s] %s (%s:%d)%s", log_time_str, cass_log_level_string(message->severity), message->message, message->file, message->line, PHP_EOL); fwrite(tmp, 1, needed, fd); free(tmp); fclose(fd); return; } } /* This defaults to using "stderr" instead of "sapi_module.log_message" * because there are no guarantees that all implementations of the SAPI * logging function are thread-safe. */ fprintf(stderr, PHP_DRIVER_NAME " | [%s] %s (%s:%d)%s", cass_log_level_string(message->severity), message->message, message->file, message->line, PHP_EOL); } zend_class_entry* exception_class(CassError rc) { switch (rc) { case CASS_ERROR_LIB_BAD_PARAMS: case CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS: case CASS_ERROR_LIB_INVALID_ITEM_COUNT: case CASS_ERROR_LIB_INVALID_VALUE_TYPE: case CASS_ERROR_LIB_INVALID_STATEMENT_TYPE: case CASS_ERROR_LIB_NAME_DOES_NOT_EXIST: case CASS_ERROR_LIB_NULL_VALUE: case CASS_ERROR_SSL_INVALID_CERT: case CASS_ERROR_SSL_INVALID_PRIVATE_KEY: case CASS_ERROR_SSL_NO_PEER_CERT: case CASS_ERROR_SSL_INVALID_PEER_CERT: case CASS_ERROR_SSL_IDENTITY_MISMATCH: return php_driver_invalid_argument_exception_ce; case CASS_ERROR_LIB_NO_STREAMS: case CASS_ERROR_LIB_UNABLE_TO_INIT: case CASS_ERROR_LIB_MESSAGE_ENCODE: case CASS_ERROR_LIB_HOST_RESOLUTION: case CASS_ERROR_LIB_UNEXPECTED_RESPONSE: case CASS_ERROR_LIB_REQUEST_QUEUE_FULL: case CASS_ERROR_LIB_NO_AVAILABLE_IO_THREAD: case CASS_ERROR_LIB_WRITE_ERROR: case CASS_ERROR_LIB_NO_HOSTS_AVAILABLE: case CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE: case CASS_ERROR_LIB_UNABLE_TO_DETERMINE_PROTOCOL: case CASS_ERROR_LIB_UNABLE_TO_CONNECT: case CASS_ERROR_LIB_UNABLE_TO_CLOSE: return php_driver_runtime_exception_ce; case CASS_ERROR_LIB_REQUEST_TIMED_OUT: return php_driver_timeout_exception_ce; case CASS_ERROR_LIB_CALLBACK_ALREADY_SET: case CASS_ERROR_LIB_NOT_IMPLEMENTED: return php_driver_logic_exception_ce; case CASS_ERROR_SERVER_SERVER_ERROR: return php_driver_server_exception_ce; case CASS_ERROR_SERVER_PROTOCOL_ERROR: return php_driver_protocol_exception_ce; case CASS_ERROR_SERVER_BAD_CREDENTIALS: return php_driver_authentication_exception_ce; case CASS_ERROR_SERVER_UNAVAILABLE: return php_driver_unavailable_exception_ce; case CASS_ERROR_SERVER_OVERLOADED: return php_driver_overloaded_exception_ce; case CASS_ERROR_SERVER_IS_BOOTSTRAPPING: return php_driver_is_bootstrapping_exception_ce; case CASS_ERROR_SERVER_TRUNCATE_ERROR: return php_driver_truncate_exception_ce; case CASS_ERROR_SERVER_WRITE_TIMEOUT: return php_driver_write_timeout_exception_ce; case CASS_ERROR_SERVER_READ_TIMEOUT: return php_driver_read_timeout_exception_ce; case CASS_ERROR_SERVER_SYNTAX_ERROR: return php_driver_invalid_syntax_exception_ce; case CASS_ERROR_SERVER_UNAUTHORIZED: return php_driver_unauthorized_exception_ce; case CASS_ERROR_SERVER_INVALID_QUERY: return php_driver_invalid_query_exception_ce; case CASS_ERROR_SERVER_CONFIG_ERROR: return php_driver_configuration_exception_ce; case CASS_ERROR_SERVER_ALREADY_EXISTS: return php_driver_already_exists_exception_ce; case CASS_ERROR_SERVER_UNPREPARED: return php_driver_unprepared_exception_ce; default: return php_driver_runtime_exception_ce; } } void throw_invalid_argument(zval *object, const char *object_name, const char *expected_type TSRMLS_DC) { if (Z_TYPE_P(object) == IS_OBJECT) { #if ZEND_MODULE_API_NO >= 20100525 const char* cls_name = NULL; #else char* cls_name = NULL; #endif #if PHP_MAJOR_VERSION >= 7 size_t cls_len; #else zend_uint cls_len; #endif #if PHP_MAJOR_VERSION >= 7 zend_string* str = Z_OBJ_HANDLER_P(object, get_class_name)(Z_OBJ_P(object) TSRMLS_CC); cls_name = str->val; cls_len = str->len; #else Z_OBJ_HANDLER_P(object, get_class_name)(object, &cls_name, &cls_len, 0 TSRMLS_CC); #endif if (cls_name) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "%s must be %s, an instance of %.*s given", object_name, expected_type, cls_len, cls_name); #if PHP_MAJOR_VERSION >= 7 zend_string_release(str); #else efree((void*) cls_name); #endif } else { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "%s must be %s, an instance of Unknown Class given", object_name, expected_type); } } else if (Z_TYPE_P(object) == IS_STRING) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "%s must be %s, '%Z' given", object_name, expected_type, object); } else { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "%s must be %s, %Z given", object_name, expected_type, object); } } PHP_INI_MH(OnUpdateLogLevel) { /* If TSRM is enabled then the last thread to update this wins */ if (new_value) { if (PHP5TO7_STRCMP(new_value, "CRITICAL") == 0) { cass_log_set_level(CASS_LOG_DISABLED); } else if (PHP5TO7_STRCMP(new_value, "ERROR") == 0) { cass_log_set_level(CASS_LOG_ERROR); } else if (PHP5TO7_STRCMP(new_value, "WARN") == 0) { cass_log_set_level(CASS_LOG_WARN); } else if (PHP5TO7_STRCMP(new_value, "INFO") == 0) { cass_log_set_level(CASS_LOG_INFO); } else if (PHP5TO7_STRCMP(new_value, "DEBUG") == 0) { cass_log_set_level(CASS_LOG_DEBUG); } else if (PHP5TO7_STRCMP(new_value, "TRACE") == 0) { cass_log_set_level(CASS_LOG_TRACE); } else { php_error_docref(NULL TSRMLS_CC, E_NOTICE, PHP_DRIVER_NAME " | Unknown log level '%s', using 'ERROR'", new_value); cass_log_set_level(CASS_LOG_ERROR); } } return SUCCESS; } PHP_INI_MH(OnUpdateLog) { /* If TSRM is enabled then the last thread to update this wins */ uv_rwlock_wrlock(&log_lock); if (log_location) { free(log_location); log_location = NULL; } if (new_value) { if (PHP5TO7_STRCMP(new_value, "syslog") != 0) { char realpath[MAXPATHLEN + 1]; if (VCWD_REALPATH(PHP5TO7_STRVAL(new_value), realpath)) { log_location = strdup(realpath); } else { log_location = strdup(PHP5TO7_STRVAL(new_value)); } } else { log_location = strdup(PHP5TO7_STRVAL(new_value)); } } uv_rwlock_wrunlock(&log_lock); return SUCCESS; } static PHP_GINIT_FUNCTION(php_driver) { uv_once(&log_once, php_driver_log_initialize); php_driver_globals->uuid_gen = NULL; php_driver_globals->uuid_gen_pid = 0; php_driver_globals->persistent_clusters = 0; php_driver_globals->persistent_sessions = 0; PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_varchar); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_text); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_blob); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_ascii); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_bigint); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_smallint); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_counter); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_int); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_varint); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_boolean); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_decimal); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_double); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_float); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_inet); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_timestamp); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_uuid); PHP5TO7_ZVAL_UNDEF(php_driver_globals->type_timeuuid); } static PHP_GSHUTDOWN_FUNCTION(php_driver) { if (php_driver_globals->uuid_gen) { cass_uuid_gen_free(php_driver_globals->uuid_gen); } php_driver_log_cleanup(); } PHP_MINIT_FUNCTION(php_driver) { REGISTER_INI_ENTRIES(); le_php_driver_cluster_res = zend_register_list_destructors_ex(NULL, php_driver_cluster_dtor, PHP_DRIVER_CLUSTER_RES_NAME, module_number); le_php_driver_session_res = zend_register_list_destructors_ex(NULL, php_driver_session_dtor, PHP_DRIVER_SESSION_RES_NAME, module_number); php_driver_define_Exception(TSRMLS_C); php_driver_define_InvalidArgumentException(TSRMLS_C); php_driver_define_DomainException(TSRMLS_C); php_driver_define_RuntimeException(TSRMLS_C); php_driver_define_TimeoutException(TSRMLS_C); php_driver_define_LogicException(TSRMLS_C); php_driver_define_ExecutionException(TSRMLS_C); php_driver_define_ReadTimeoutException(TSRMLS_C); php_driver_define_WriteTimeoutException(TSRMLS_C); php_driver_define_UnavailableException(TSRMLS_C); php_driver_define_TruncateException(TSRMLS_C); php_driver_define_ValidationException(TSRMLS_C); php_driver_define_InvalidQueryException(TSRMLS_C); php_driver_define_InvalidSyntaxException(TSRMLS_C); php_driver_define_UnauthorizedException(TSRMLS_C); php_driver_define_UnpreparedException(TSRMLS_C); php_driver_define_ConfigurationException(TSRMLS_C); php_driver_define_AlreadyExistsException(TSRMLS_C); php_driver_define_AuthenticationException(TSRMLS_C); php_driver_define_ProtocolException(TSRMLS_C); php_driver_define_ServerException(TSRMLS_C); php_driver_define_IsBootstrappingException(TSRMLS_C); php_driver_define_OverloadedException(TSRMLS_C); php_driver_define_RangeException(TSRMLS_C); php_driver_define_DivideByZeroException(TSRMLS_C); php_driver_define_Value(TSRMLS_C); php_driver_define_Numeric(TSRMLS_C); php_driver_define_Bigint(TSRMLS_C); php_driver_define_Smallint(TSRMLS_C); php_driver_define_Tinyint(TSRMLS_C); php_driver_define_Blob(TSRMLS_C); php_driver_define_Decimal(TSRMLS_C); php_driver_define_Float(TSRMLS_C); php_driver_define_Inet(TSRMLS_C); php_driver_define_Timestamp(TSRMLS_C); php_driver_define_Date(TSRMLS_C); php_driver_define_Time(TSRMLS_C); php_driver_define_UuidInterface(TSRMLS_C); php_driver_define_Timeuuid(TSRMLS_C); php_driver_define_Uuid(TSRMLS_C); php_driver_define_Varint(TSRMLS_C); php_driver_define_Custom(TSRMLS_C); php_driver_define_Duration(TSRMLS_C); php_driver_define_Set(TSRMLS_C); php_driver_define_Map(TSRMLS_C); php_driver_define_Collection(TSRMLS_C); php_driver_define_Tuple(TSRMLS_C); php_driver_define_UserTypeValue(TSRMLS_C); php_driver_define_Core(TSRMLS_C); php_driver_define_Cluster(TSRMLS_C); php_driver_define_DefaultCluster(TSRMLS_C); php_driver_define_ClusterBuilder(TSRMLS_C); php_driver_define_Future(TSRMLS_C); php_driver_define_FuturePreparedStatement(TSRMLS_C); php_driver_define_FutureRows(TSRMLS_C); php_driver_define_FutureSession(TSRMLS_C); php_driver_define_FutureValue(TSRMLS_C); php_driver_define_FutureClose(TSRMLS_C); php_driver_define_Session(TSRMLS_C); php_driver_define_DefaultSession(TSRMLS_C); php_driver_define_SSLOptions(TSRMLS_C); php_driver_define_SSLOptionsBuilder(TSRMLS_C); php_driver_define_Statement(TSRMLS_C); php_driver_define_SimpleStatement(TSRMLS_C); php_driver_define_PreparedStatement(TSRMLS_C); php_driver_define_BatchStatement(TSRMLS_C); php_driver_define_ExecutionOptions(TSRMLS_C); php_driver_define_Rows(TSRMLS_C); php_driver_define_Schema(TSRMLS_C); php_driver_define_DefaultSchema(TSRMLS_C); php_driver_define_Keyspace(TSRMLS_C); php_driver_define_DefaultKeyspace(TSRMLS_C); php_driver_define_Table(TSRMLS_C); php_driver_define_DefaultTable(TSRMLS_C); php_driver_define_Column(TSRMLS_C); php_driver_define_DefaultColumn(TSRMLS_C); php_driver_define_Index(TSRMLS_C); php_driver_define_DefaultIndex(TSRMLS_C); php_driver_define_MaterializedView(TSRMLS_C); php_driver_define_DefaultMaterializedView(TSRMLS_C); php_driver_define_Function(TSRMLS_C); php_driver_define_DefaultFunction(TSRMLS_C); php_driver_define_Aggregate(TSRMLS_C); php_driver_define_DefaultAggregate(TSRMLS_C); php_driver_define_Type(TSRMLS_C); php_driver_define_TypeScalar(TSRMLS_C); php_driver_define_TypeCollection(TSRMLS_C); php_driver_define_TypeSet(TSRMLS_C); php_driver_define_TypeMap(TSRMLS_C); php_driver_define_TypeTuple(TSRMLS_C); php_driver_define_TypeUserType(TSRMLS_C); php_driver_define_TypeCustom(TSRMLS_C); php_driver_define_RetryPolicy(TSRMLS_C); php_driver_define_RetryPolicyDefault(TSRMLS_C); php_driver_define_RetryPolicyDowngradingConsistency(TSRMLS_C); php_driver_define_RetryPolicyFallthrough(TSRMLS_C); php_driver_define_RetryPolicyLogging(TSRMLS_C); php_driver_define_TimestampGenerator(TSRMLS_C); php_driver_define_TimestampGeneratorMonotonic(TSRMLS_C); php_driver_define_TimestampGeneratorServerSide(TSRMLS_C); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(php_driver) { /* UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } PHP_RINIT_FUNCTION(php_driver) { #define XX_SCALAR(name, value) \ PHP5TO7_ZVAL_UNDEF(PHP_DRIVER_G(type_##name)); PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(php_driver) { #define XX_SCALAR(name, value) \ PHP5TO7_ZVAL_MAYBE_DESTROY(PHP_DRIVER_G(type_##name)); PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR return SUCCESS; } PHP_MINFO_FUNCTION(php_driver) { char buf[256]; php_info_print_table_start(); php_info_print_table_header(2, PHP_DRIVER_NAMESPACE " support", "enabled"); snprintf(buf, sizeof(buf), "%d.%d.%d%s", CASS_VERSION_MAJOR, CASS_VERSION_MINOR, CASS_VERSION_PATCH, strlen(CASS_VERSION_SUFFIX) > 0 ? "-" CASS_VERSION_SUFFIX : ""); php_info_print_table_row(2, "C/C++ driver version", buf); snprintf(buf, sizeof(buf), "%d", PHP_DRIVER_G(persistent_clusters)); php_info_print_table_row(2, "Persistent Clusters", buf); snprintf(buf, sizeof(buf), "%d", PHP_DRIVER_G(persistent_sessions)); php_info_print_table_row(2, "Persistent Sessions", buf); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } cassandra-1.3.0/php_driver.h0000644000076500000240000004411213062515664016456 0ustar michaelpenickstaff#ifndef PHP_DRIVER_H #define PHP_DRIVER_H #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include /* Ensure Visual Studio 2010 does not load MSVC++ stdint definitions */ #ifdef _WIN32 # ifdef DISABLE_MSVC_STDINT # pragma once # ifndef _STDINT # define _STDINT # endif # endif #endif #include #include #include #include #ifdef HAVE_SYS_TYPES_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef PHP_WIN32 typedef int pid_t; #include #endif #ifdef WIN32 # define LL_FORMAT "%I64d" #else # define LL_FORMAT "%lld" #endif #if PHP_VERSION_ID < 50600 # error PHP 5.6.0 or later is required in order to build the driver #endif #if HAVE_SPL # include # include #else # error SPL must be enabled in order to build the driver #endif #include "version.h" #ifdef PHP_WIN32 # define PHP_DRIVER_API __declspec(dllexport) #elif defined(__GNUC__) && __GNUC__ >= 4 # define PHP_DRIVER_API __attribute__ ((visibility("default"))) #else # define PHP_DRIVER_API #endif #define PHP_DRIVER_NAMESPACE "Cassandra" #define PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(pass_by_ref, name, classname, allow_null) \ ZEND_ARG_OBJ_INFO(pass_by_ref, name, Cassandra\\classname, allow_null) #define PHP_DRIVER_CORE_METHOD(name) \ PHP_METHOD(Cassandra, name) #define PHP_DRIVER_CORE_ME(name, arg_info, flags) \ PHP_ME(Cassandra, name, arg_info, flags) #ifndef ZEND_MOD_END # define ZEND_MOD_END {NULL, NULL, NULL} #endif #ifndef PHP_FE_END # define PHP_FE_END { NULL, NULL, NULL, 0, 0 } #endif #if ZEND_MODULE_API_NO < 20100525 # define object_properties_init(value, class_entry) \ zend_hash_copy(*value.properties, &class_entry->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); #endif #define SAFE_STR(a) ((a)?a:"") #ifdef ZTS # include "TSRM.h" #endif #ifdef ZTS # define PHP_DRIVER_G(v) TSRMG(php_driver_globals_id, zend_php_driver_globals *, v) #else # define PHP_DRIVER_G(v) (php_driver_globals.v) #endif #define CPP_DRIVER_VERSION(major, minor, patch) \ (((major) << 16) + ((minor) << 8) + (patch)) #define CURRENT_CPP_DRIVER_VERSION \ CPP_DRIVER_VERSION(CASS_VERSION_MAJOR, CASS_VERSION_MINOR, CASS_VERSION_PATCH) #if PHP_MAJOR_VERSION >= 7 #define php5to7_zend_register_internal_class_ex(ce, parent_ce) zend_register_internal_class_ex((ce), (parent_ce) TSRMLS_CC); typedef zval php5to7_zval; typedef zval *php5to7_zval_args; typedef zval *php5to7_zval_arg; typedef zend_string *php5to7_string; typedef zend_long php5to7_long; typedef zend_ulong php5to7_ulong; typedef zval php5to7_zend_resource_le; typedef zend_resource* php5to7_zend_resource; typedef zend_object *php5to7_zend_object; typedef zend_object php5to7_zend_object_free; typedef zval **php5to7_zval_gc; typedef zval *php5to7_dtor; typedef size_t php5to7_size; static inline int php5to7_string_compare(php5to7_string s1, php5to7_string s2) { if (s1->len != s2->len) { return s1->len < s2->len ? -1 : 1; } return memcmp(s1->val, s2->val, s1->len); } #define PHP5TO7_ZEND_OBJECT_GET(type_name, object) \ php_driver_##type_name##_object_fetch(object); #define PHP5TO7_SMART_STR_INIT { NULL, 0 } #define PHP5TO7_SMART_STR_VAL(ss) ((ss).s ? (ss).s->val : NULL) #define PHP5TO7_SMART_STR_LEN(ss) ((ss).s ? (ss).s->len : 0) #define PHP5TO7_STRCMP(s, c) strcmp((s)->val, (c)) #define PHP5TO7_STRVAL(s) ((s)->val) #define PHP5TO7_ZEND_ACC_FINAL ZEND_ACC_FINAL #define PHP5TO7_ZEND_OBJECT_ECALLOC(type_name, ce) \ (php_driver_##type_name *) ecalloc(1, sizeof(php_driver_##type_name) + zend_object_properties_size(ce)) #define PHP5TO7_ZEND_OBJECT_INIT(type_name, self, ce) \ PHP5TO7_ZEND_OBJECT_INIT_EX(type_name, type_name, self, ce) #define PHP5TO7_ZEND_OBJECT_INIT_EX(type_name, name, self, ce) do { \ zend_object_std_init(&self->zval, ce TSRMLS_CC); \ ((zend_object_handlers *) &php_driver_##name##_handlers)->offset = \ XtOffsetOf(php_driver_##type_name, zval); \ ((zend_object_handlers *) &php_driver_##name##_handlers)->free_obj = \ php_driver_##name##_free; \ self->zval.handlers = (zend_object_handlers *) &php_driver_##name##_handlers; \ return &self->zval; \ } while(0) #define PHP5TO7_MAYBE_EFREE(p) ((void)0) #define PHP5TO7_ADD_ASSOC_ZVAL_EX(zv, key, len, val) \ add_assoc_zval_ex((zv), (key), (size_t)(len - 1), val) #define PHP5TO7_ADD_ASSOC_STRINGL_EX(zv, key, key_len, str, str_len) \ add_assoc_stringl_ex((zv), (key), (size_t)(key_len - 1), (char *)(str), (size_t)(str_len)) #define PHP5TO7_ADD_NEXT_INDEX_STRING(zv, str) \ add_next_index_string((zv), (char*)(str)); #define PHP5TO7_ZEND_HASH_FOREACH_VAL(ht, _val) \ ZEND_HASH_FOREACH_VAL(ht, _val) #define PHP5TO7_ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \ ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) #define PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \ ZEND_HASH_FOREACH(ht, 0); \ if (_p->key) { \ (_key) = _p->key->val; \ } else { \ (_key) = NULL; \ } \ _val = _z; #define PHP5TO7_ZEND_HASH_FOREACH_END(ht) ZEND_HASH_FOREACH_END() #define PHP5TO7_ZEND_HASH_GET_CURRENT_DATA(ht, res) \ ((res = zend_hash_get_current_data((ht))) != NULL) #define PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(ht, res, pos) \ ((res = zend_hash_get_current_data_ex((ht), (pos))) != NULL) #define PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(ht, res, pos) \ ((res = zend_hash_get_current_data_ex((ht), (pos))) != NULL) #define PHP5TO7_ZEND_HASH_GET_CURRENT_KEY(ht, str_index, num_index) \ zend_hash_get_current_key((ht), (str_index), (num_index)) #define PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(ht, str_index, num_index, pos) \ zend_hash_get_current_key_ex((ht), (str_index), (num_index), pos) #define PHP5TO7_ZEND_HASH_EXISTS(ht, key, len) \ zend_hash_str_exists((ht), (key), (size_t)(len - 1)) #define PHP5TO7_ZEND_HASH_FIND(ht, key, len, res) \ ((res = zend_hash_str_find((ht), (key), (size_t)(len - 1))) != NULL) #define PHP5TO7_ZEND_HASH_INDEX_FIND(ht, index, res) \ ((res = zend_hash_index_find((ht), (php5to7_ulong) (index))) != NULL) #define PHP5TO7_ZEND_HASH_NEXT_INDEX_INSERT(ht, val, val_size) \ ((void) zend_hash_next_index_insert((ht), (val))) #define PHP5TO7_ZEND_HASH_UPDATE(ht, key, len, val, val_size) \ ((void) zend_hash_str_update((ht), (key), (size_t)(len - 1), (val))) #define PHP5TO7_ZEND_HASH_INDEX_UPDATE(ht, index, val, val_size) \ ((void) zend_hash_index_update((ht), (index), (val))) #define PHP5TO7_ZEND_HASH_ADD(ht, key, len, val, val_size) \ ((void) zend_hash_str_add((ht), (key), (size_t)(len - 1), (val))) #define PHP5TO7_ZEND_HASH_DEL(ht, key, len) \ ((zend_hash_str_del((ht), (key), (size_t)(len - 1))) == SUCCESS) #define PHP5TO7_ZEND_HASH_ZVAL_COPY(dst, src) \ zend_hash_copy((dst), (src), (copy_ctor_func_t) zval_add_ref); #define PHP5TO7_ZEND_HASH_SORT(ht, compare_func, renumber) \ zend_hash_sort(ht, compare_func, renumber TSRMLS_CC) #define PHP5TO7_ZEND_STRING_VAL(str) (str)->val #define PHP5TO7_ZEND_STRING_LEN(str) (str)->len #define PHP5TO7_ZVAL_COPY(zv1, zv2) ZVAL_COPY(zv1, zv2) #define PHP5TO7_ZVAL_IS_UNDEF(zv) Z_ISUNDEF(zv) #define PHP5TO7_ZVAL_IS_UNDEF_P(zv) Z_ISUNDEF_P(zv) #define PHP5TO7_ZVAL_IS_BOOL_P(zv) \ (Z_TYPE_P(zv) == IS_TRUE || Z_TYPE_P(zv) == IS_FALSE) #define PHP5TO7_ZVAL_IS_FALSE_P(zv) (Z_TYPE_P(zv) == IS_FALSE) #define PHP5TO7_ZVAL_IS_TRUE_P(zv) (Z_TYPE_P(zv) == IS_TRUE) #define PHP5TO7_ZVAL_UNDEF(zv) ZVAL_UNDEF(&(zv)); #define PHP5TO7_ZVAL_MAYBE_MAKE(zv) ((void)0) #define PHP5TO7_ZVAL_MAYBE_DESTROY(zv) do { \ if (!Z_ISUNDEF(zv)) { \ zval_ptr_dtor(&(zv)); \ ZVAL_UNDEF(&(zv)); \ } \ } while(0) #define PHP5TO7_ZVAL_STRING(zv, s) ZVAL_STRING(zv, s) #define PHP5TO7_ZVAL_STRINGL(zv, s, len) ZVAL_STRINGL(zv, s, len) #define PHP5TO7_RETVAL_STRING(s) RETVAL_STRING(s) #define PHP5TO7_RETURN_STRING(s) RETURN_STRING(s) #define PHP5TO7_RETVAL_STRINGL(s, len) RETVAL_STRINGL(s, len) #define PHP5TO7_RETURN_STRINGL(s, len) RETURN_STRINGL(s, len) #define PHP5TO7_ZVAL_ARG(zv) &(zv) #define PHP5TO7_ZVAL_MAYBE_DEREF(zv) (zv) #define PHP5TO7_ZVAL_MAYBE_ADDR_OF(zv) (zv) #define PHP5TO7_ZVAL_MAYBE_P(zv) &(zv) #define PHP5TO7_Z_TYPE_MAYBE_P(zv) Z_TYPE(zv) #define PHP5TO7_Z_ARRVAL_MAYBE_P(zv) Z_ARRVAL(zv) #define PHP5TO7_Z_OBJCE_MAYBE_P(zv) Z_OBJCE(zv) #define PHP5TO7_Z_LVAL_MAYBE_P(zv) Z_LVAL(zv) #define PHP5TO7_Z_DVAL_MAYBE_P(zv) Z_DVAL(zv) #define PHP5TO7_Z_STRVAL_MAYBE_P(zv) Z_STRVAL(zv) #define PHP5TO7_Z_STRLEN_MAYBE_P(zv) Z_STRLEN(zv) #define PHP5TO7_ZEND_LONG_MAX ZEND_LONG_MAX #define PHP5TO7_ZEND_LONG_MIN ZEND_LONG_MIN #else typedef zval *php5to7_zval; typedef zval ***php5to7_zval_args; typedef char *php5to7_string; typedef long php5to7_long; typedef ulong php5to7_ulong; typedef zend_rsrc_list_entry php5to7_zend_resource_le; typedef zend_rsrc_list_entry *php5to7_zend_resource; typedef zend_object_value php5to7_zend_object; typedef void php5to7_zend_object_free; typedef zval ***php5to7_zval_gc; typedef void **php5to7_dtor; typedef int php5to7_size; static inline int php5to7_string_compare(php5to7_string s1, php5to7_string s2) { return strcmp(s1, s2); } #define PHP5TO7_ZEND_OBJECT_GET(type_name, object) \ (php_driver_##type_name *) object #define Z_RES_P(zv) (zv) #define Z_RES(zv) (&(zv)) #define Z_TRY_ADDREF_P(zv) Z_ADDREF_P(zv) #define PHP5TO7_SMART_STR_INIT { NULL, 0, 0 } #define PHP5TO7_SMART_STR_VAL(ss) (ss).c #define PHP5TO7_SMART_STR_LEN(ss) (ss).len #define PHP5TO7_STRCMP(s, c) strcmp((s), (c)) #define PHP5TO7_STRVAL(s) (s) #define PHP5TO7_ZEND_ACC_FINAL ZEND_ACC_FINAL_CLASS #define PHP5TO7_ZEND_OBJECT_ECALLOC(type_name, ce) (php_driver_##type_name *) \ ecalloc(1, sizeof(php_driver_##type_name)) #define PHP5TO7_ZEND_OBJECT_INIT(type_name, self, ce) \ PHP5TO7_ZEND_OBJECT_INIT_EX(type_name, type_name, self, ce) #define PHP5TO7_ZEND_OBJECT_INIT_EX(type_name, name, self, ce) do { \ zend_object_value retval; \ zend_object_std_init(&self->zval, ce TSRMLS_CC); \ object_properties_init(&self->zval, ce); \ retval.handle = zend_objects_store_put(self, \ (zend_objects_store_dtor_t) zend_objects_destroy_object, \ php_driver_##name##_free, NULL TSRMLS_CC); \ retval.handlers = (zend_object_handlers *) &php_driver_##name##_handlers; \ return retval; \ } while(0) #define PHP5TO7_MAYBE_EFREE(p) efree(p) #define PHP5TO7_ADD_ASSOC_ZVAL_EX(zv, key, len, val) \ add_assoc_zval_ex((zv), (key), (uint)(len), val) #define PHP5TO7_ADD_ASSOC_STRINGL_EX(zv, key, key_len, str, str_len) \ add_assoc_stringl_ex((zv), (key), (uint)(key_len), (char*)(str), (uint)(str_len), 1) #define PHP5TO7_ADD_NEXT_INDEX_STRING(zv, str) \ add_next_index_string((zv), (char*)(str), 1); #define PHP5TO7_ZEND_HASH_FOREACH_VAL(ht, _val) do { \ HashPosition _pos; \ zend_hash_internal_pointer_reset_ex((ht), &_pos); \ while (zend_hash_get_current_data_ex((ht), (void **)&(_val), &_pos) == SUCCESS) { #define PHP5TO7_ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _key_len, _val) \ PHP5TO7_ZEND_HASH_FOREACH_VAL(ht, _val) \ (_key) = NULL; \ zend_hash_get_current_key_ex((ht), &(_key), &(_key_len), &(_h), 0, &_pos); #define PHP5TO7_ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \ PHP5TO7_ZEND_HASH_FOREACH_VAL(ht, _val) \ char *_key; \ uint _len; \ zend_hash_get_current_key_ex((ht), &_key, &_len, &(_h), 0, &_pos); #define PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \ PHP5TO7_ZEND_HASH_FOREACH_VAL(ht, _val) \ ulong _h; \ (_key) = NULL; \ zend_hash_get_current_key_ex((ht), &(_key), NULL, &_h, 0, &_pos); #define PHP5TO7_ZEND_HASH_FOREACH_END(ht) \ zend_hash_move_forward_ex((ht), &_pos); \ } \ } while(0) #define PHP5TO7_ZEND_HASH_GET_CURRENT_DATA(ht, res) \ (zend_hash_get_current_data((ht), (void **) &(res)) == SUCCESS) #define PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(ht, res, pos) \ (zend_hash_get_current_data_ex((ht), (void **) &(res), (pos)) == SUCCESS) #define PHP5TO7_ZEND_HASH_GET_CURRENT_KEY(ht, str_index, num_index) \ zend_hash_get_current_key((ht), (str_index), (num_index), 0) #define PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(ht, str_index, num_index, pos) \ zend_hash_get_current_key_ex((ht), (str_index), NULL, (num_index), 0, pos) #define PHP5TO7_ZEND_HASH_EXISTS(ht, key, len) \ zend_hash_exists((ht), (key), (len)) #define PHP5TO7_ZEND_HASH_FIND(ht, key, len, res) \ (zend_hash_find((ht), (key), (uint)(len), (void **)&(res)) == SUCCESS) #define PHP5TO7_ZEND_HASH_INDEX_FIND(ht, index, res) \ (zend_hash_index_find((ht), (php5to7_ulong) (index), (void **) &res) == SUCCESS) #define PHP5TO7_ZEND_HASH_NEXT_INDEX_INSERT(ht, val, val_size) \ ((void) zend_hash_next_index_insert((ht), (void*) &(val), (uint) (val_size), NULL)) #define PHP5TO7_ZEND_HASH_UPDATE(ht, key, len, val, val_size) \ ((void) zend_hash_update((ht), (key), (uint)(len), (void *) &(val), (uint)(val_size), NULL)) #define PHP5TO7_ZEND_HASH_INDEX_UPDATE(ht, index, val, val_size) \ ((void) zend_hash_index_update((ht), (index), (void *) &(val), (uint)(val_size), NULL)) #define PHP5TO7_ZEND_HASH_ADD(ht, key, len, val, val_size) \ ((void) zend_hash_add((ht), (key), (len), (void *) &(val), (uint)(val_size), NULL)) #define PHP5TO7_ZEND_HASH_DEL(ht, key, len) \ ((zend_hash_del((ht), (key), (uint)(len))) == SUCCESS) #define PHP5TO7_ZEND_HASH_ZVAL_COPY(dst, src) do { \ zval *_tmp; \ zend_hash_copy((dst), (src), \ (copy_ctor_func_t) zval_add_ref, \ (void *) &_tmp, sizeof(zval *)); \ } while (0) #define PHP5TO7_ZEND_HASH_SORT(ht, compare_func, renumber) \ zend_hash_sort(ht, zend_qsort, compare_func, renumber TSRMLS_CC); #define php5to7_zend_register_internal_class_ex(ce, parent_ce) zend_register_internal_class_ex((ce), (parent_ce), NULL TSRMLS_CC); #define PHP5TO7_ZVAL_COPY(zv1, zv2) do { \ zv1 = zv2; \ if(zv1) Z_TRY_ADDREF_P(zv1); \ } while(0) #define PHP5TO7_ZVAL_IS_UNDEF(zv) ((zv) == NULL) #define PHP5TO7_ZVAL_IS_UNDEF_P(zv) ((zv) == NULL) #define PHP5TO7_ZVAL_IS_BOOL_P(zv) (Z_TYPE_P(zv) == IS_BOOL) #define PHP5TO7_ZVAL_IS_FALSE_P(zv) (Z_TYPE_P(zv) == IS_BOOL && !Z_BVAL_P(zv)) #define PHP5TO7_ZVAL_IS_TRUE_P(zv) (Z_TYPE_P(zv) == IS_BOOL && Z_BVAL_P(zv)) #define PHP5TO7_ZVAL_UNDEF(zv) (zv) = NULL; #define PHP5TO7_ZVAL_MAYBE_MAKE(zv) MAKE_STD_ZVAL(zv) #define PHP5TO7_ZVAL_MAYBE_DESTROY(zv) do { \ if ((zv) != NULL) { \ zval_ptr_dtor(&(zv)); \ (zv) = NULL; \ } \ } while(0) #define PHP5TO7_ZVAL_STRING(zv, s) ZVAL_STRING(zv, s, 1) #define PHP5TO7_ZVAL_STRINGL(zv, s, len) ZVAL_STRINGL(zv, s, len, 1) #define PHP5TO7_RETVAL_STRING(s) RETVAL_STRING(s, 1) #define PHP5TO7_RETURN_STRING(s) RETURN_STRING(s, 1) #define PHP5TO7_RETVAL_STRINGL(s, len) RETVAL_STRINGL(s, len, 1) #define PHP5TO7_RETURN_STRINGL(s, len) RETURN_STRINGL(s, len, 1) #define PHP5TO7_ZVAL_ARG(zv) *(zv) #define PHP5TO7_ZVAL_MAYBE_DEREF(zv) *(zv) #define PHP5TO7_ZVAL_MAYBE_ADDR_OF(zv) &(zv) #define PHP5TO7_ZVAL_MAYBE_P(zv) (zv) #define PHP5TO7_Z_TYPE_MAYBE_P(zv) Z_TYPE_P(zv) #define PHP5TO7_Z_ARRVAL_MAYBE_P(zv) Z_ARRVAL_P(zv) #define PHP5TO7_Z_OBJCE_MAYBE_P(zv) Z_OBJCE_P(zv) #define PHP5TO7_Z_LVAL_MAYBE_P(zv) Z_LVAL_P(zv) #define PHP5TO7_Z_DVAL_MAYBE_P(zv) Z_DVAL_P(zv) #define PHP5TO7_Z_STRVAL_MAYBE_P(zv) Z_STRVAL_P(zv) #define PHP5TO7_Z_STRLEN_MAYBE_P(zv) Z_STRLEN_P(zv) #define PHP5TO7_ZEND_LONG_MAX LONG_MAX #define PHP5TO7_ZEND_LONG_MIN LONG_MIN #endif /* PHP_MAJOR_VERSION >= 7 */ extern zend_module_entry php_driver_module_entry; PHP_MINIT_FUNCTION(php_driver); PHP_MSHUTDOWN_FUNCTION(php_driver); PHP_RINIT_FUNCTION(php_driver); PHP_RSHUTDOWN_FUNCTION(php_driver); PHP_MINFO_FUNCTION(php_driver); zend_class_entry *exception_class(CassError rc); void throw_invalid_argument(zval *object, const char *object_name, const char *expected_type TSRMLS_DC); #define INVALID_ARGUMENT(object, expected) \ { \ throw_invalid_argument(object, #object, expected TSRMLS_CC); \ return; \ } #define INVALID_ARGUMENT_VALUE(object, expected, failed_value) \ { \ throw_invalid_argument(object, #object, expected TSRMLS_CC); \ return failed_value; \ } #define ASSERT_SUCCESS_BLOCK(rc, block) \ { \ if (rc != CASS_OK) { \ zend_throw_exception_ex(exception_class(rc), rc TSRMLS_CC, \ "%s", cass_error_desc(rc)); \ block \ } \ } #define ASSERT_SUCCESS(rc) ASSERT_SUCCESS_BLOCK(rc, return;) #define ASSERT_SUCCESS_VALUE(rc, value) ASSERT_SUCCESS_BLOCK(rc, return value;) #define PHP_DRIVER_DEFAULT_CONSISTENCY CASS_CONSISTENCY_LOCAL_ONE #define PHP_DRIVER_DEFAULT_LOG PHP_DRIVER_NAME ".log" #define PHP_DRIVER_DEFAULT_LOG_LEVEL "ERROR" #define PHP_DRIVER_INI_ENTRY_LOG \ PHP_INI_ENTRY(PHP_DRIVER_NAME ".log", PHP_DRIVER_DEFAULT_LOG, PHP_INI_ALL, OnUpdateLog) #define PHP_DRIVER_INI_ENTRY_LOG_LEVEL \ PHP_INI_ENTRY(PHP_DRIVER_NAME ".log_level", PHP_DRIVER_DEFAULT_LOG_LEVEL, PHP_INI_ALL, OnUpdateLogLevel) PHP_INI_MH(OnUpdateLogLevel); PHP_INI_MH(OnUpdateLog); #endif /* PHP_DRIVER_H */ cassandra-1.3.0/php_driver_globals.h0000644000076500000240000000166013062515664020162 0ustar michaelpenickstaff#ifndef PHP_DRIVER_GLOBALS_H #define PHP_DRIVER_GLOBALS_H ZEND_BEGIN_MODULE_GLOBALS(php_driver) CassUuidGen *uuid_gen; pid_t uuid_gen_pid; unsigned int persistent_clusters; unsigned int persistent_sessions; php5to7_zval type_varchar; php5to7_zval type_text; php5to7_zval type_blob; php5to7_zval type_ascii; php5to7_zval type_bigint; php5to7_zval type_counter; php5to7_zval type_int; php5to7_zval type_varint; php5to7_zval type_boolean; php5to7_zval type_decimal; php5to7_zval type_double; php5to7_zval type_float; php5to7_zval type_inet; php5to7_zval type_timestamp; php5to7_zval type_date; php5to7_zval type_time; php5to7_zval type_uuid; php5to7_zval type_timeuuid; php5to7_zval type_smallint; php5to7_zval type_tinyint; php5to7_zval type_duration; ZEND_END_MODULE_GLOBALS(php_driver) ZEND_EXTERN_MODULE_GLOBALS(php_driver) #endif /* PHP_DRIVER_GLOBALS_H */ cassandra-1.3.0/php_driver_types.h0000644000076500000240000007343513062515664017714 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TYPES_H #define PHP_DRIVER_TYPES_H #if PHP_MAJOR_VERSION >= 7 #define PHP_DRIVER_BEGIN_OBJECT_TYPE(type_name) \ typedef struct php_driver_##type_name##_ { #define PHP_DRIVER_END_OBJECT_TYPE(type_name) \ zend_object zval; \ } php_driver_##type_name; \ static inline php_driver_##type_name *php_driver_##type_name##_object_fetch(zend_object *obj) { \ return (php_driver_##type_name *)((char *)obj - XtOffsetOf(php_driver_##type_name, zval)); \ } #else #define PHP_DRIVER_BEGIN_OBJECT_TYPE(type_name) \ typedef struct php_driver_##type_name##_ { \ zend_object zval; #define PHP_DRIVER_END_OBJECT_TYPE(type_name) \ } php_driver_##type_name; #endif #if PHP_MAJOR_VERSION >= 7 #define PHP_DRIVER_GET_NUMERIC(obj) php_driver_numeric_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_BLOB(obj) php_driver_blob_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TIMESTAMP(obj) php_driver_timestamp_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_DATE(obj) php_driver_date_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TIME(obj) php_driver_time_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_UUID(obj) php_driver_uuid_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_INET(obj) php_driver_inet_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_COLLECTION(obj) php_driver_collection_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_MAP(obj) php_driver_map_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_SET(obj) php_driver_set_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TUPLE(obj) php_driver_tuple_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_USER_TYPE_VALUE(obj) php_driver_user_type_value_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_CLUSTER(obj) php_driver_cluster_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_STATEMENT(obj) php_driver_statement_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_EXECUTION_OPTIONS(obj) php_driver_execution_options_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_ROWS(obj) php_driver_rows_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUTURE_ROWS(obj) php_driver_future_rows_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_CLUSTER_BUILDER(obj) php_driver_cluster_builder_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUTURE_PREPARED_STATEMENT(obj) php_driver_future_prepared_statement_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUTURE_VALUE(obj) php_driver_future_value_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUTURE_CLOSE(obj) php_driver_future_close_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUTURE_SESSION(obj) php_driver_future_session_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_SESSION(obj) php_driver_session_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_SSL(obj) php_driver_ssl_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_SSL_BUILDER(obj) php_driver_ssl_builder_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_SCHEMA(obj) php_driver_schema_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_KEYSPACE(obj) php_driver_keyspace_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TABLE(obj) php_driver_table_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_COLUMN(obj) php_driver_column_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_INDEX(obj) php_driver_index_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_MATERIALIZED_VIEW(obj) php_driver_materialized_view_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_FUNCTION(obj) php_driver_function_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_AGGREGATE(obj) php_driver_aggregate_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TYPE(obj) php_driver_type_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_RETRY_POLICY(obj) php_driver_retry_policy_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_TIMESTAMP_GEN(obj) php_driver_timestamp_gen_object_fetch(Z_OBJ_P(obj)) #define PHP_DRIVER_GET_DURATION(obj) php_driver_duration_object_fetch(Z_OBJ_P(obj)) #else #define PHP_DRIVER_GET_NUMERIC(obj) ((php_driver_numeric *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_BLOB(obj) ((php_driver_blob *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TIMESTAMP(obj) ((php_driver_timestamp *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_DATE(obj) ((php_driver_date *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TIME(obj) ((php_driver_time *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_UUID(obj) ((php_driver_uuid *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_INET(obj) ((php_driver_inet *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_COLLECTION(obj) ((php_driver_collection *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_MAP(obj) ((php_driver_map *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_SET(obj) ((php_driver_set *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TUPLE(obj) ((php_driver_tuple *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_USER_TYPE_VALUE(obj) ((php_driver_user_type_value *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_CLUSTER(obj) ((php_driver_cluster *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_STATEMENT(obj) ((php_driver_statement *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_EXECUTION_OPTIONS(obj) ((php_driver_execution_options *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_ROWS(obj) ((php_driver_rows *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUTURE_ROWS(obj) ((php_driver_future_rows *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_CLUSTER_BUILDER(obj) ((php_driver_cluster_builder *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUTURE_PREPARED_STATEMENT(obj) ((php_driver_future_prepared_statement *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUTURE_VALUE(obj) ((php_driver_future_value *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUTURE_CLOSE(obj) ((php_driver_future_close *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUTURE_SESSION(obj) ((php_driver_future_session *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_SESSION(obj) ((php_driver_session *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_SSL(obj) ((php_driver_ssl *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_SSL_BUILDER(obj) ((php_driver_ssl_builder *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_SCHEMA(obj) ((php_driver_schema *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_KEYSPACE(obj) ((php_driver_keyspace *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TABLE(obj) ((php_driver_table *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_COLUMN(obj) ((php_driver_column *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_INDEX(obj) ((php_driver_index *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_MATERIALIZED_VIEW(obj) ((php_driver_materialized_view *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_FUNCTION(obj) ((php_driver_function *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_AGGREGATE(obj) ((php_driver_aggregate *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TYPE(obj) ((php_driver_type *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_RETRY_POLICY(obj) ((php_driver_retry_policy *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_TIMESTAMP_GEN(obj) ((php_driver_timestamp_gen *)zend_object_store_get_object((obj) TSRMLS_CC)) #define PHP_DRIVER_GET_DURATION(obj) ((php_driver_duration *)zend_object_store_get_object((obj) TSRMLS_CC)) #endif typedef enum { PHP_DRIVER_BIGINT, PHP_DRIVER_DECIMAL, PHP_DRIVER_FLOAT, PHP_DRIVER_VARINT, PHP_DRIVER_SMALLINT, PHP_DRIVER_TINYINT } php_driver_numeric_type; PHP_DRIVER_BEGIN_OBJECT_TYPE(numeric) php_driver_numeric_type type; union { struct { cass_int8_t value; } tinyint; struct { cass_int16_t value; } smallint; struct { cass_int64_t value; } bigint; struct { cass_float_t value; } floating; struct { mpz_t value; } varint; struct { mpz_t value; long scale; } decimal; } data; PHP_DRIVER_END_OBJECT_TYPE(numeric) PHP_DRIVER_BEGIN_OBJECT_TYPE(timestamp) cass_int64_t timestamp; PHP_DRIVER_END_OBJECT_TYPE(timestamp) PHP_DRIVER_BEGIN_OBJECT_TYPE(date) cass_uint32_t date; PHP_DRIVER_END_OBJECT_TYPE(date) PHP_DRIVER_BEGIN_OBJECT_TYPE(time) cass_int64_t time; PHP_DRIVER_END_OBJECT_TYPE(time) PHP_DRIVER_BEGIN_OBJECT_TYPE(blob) cass_byte_t *data; size_t size; PHP_DRIVER_END_OBJECT_TYPE(blob) PHP_DRIVER_BEGIN_OBJECT_TYPE(uuid) CassUuid uuid; PHP_DRIVER_END_OBJECT_TYPE(uuid) PHP_DRIVER_BEGIN_OBJECT_TYPE(inet) CassInet inet; PHP_DRIVER_END_OBJECT_TYPE(inet) PHP_DRIVER_BEGIN_OBJECT_TYPE(duration) cass_int32_t months; cass_int32_t days; cass_int32_t nanos; PHP_DRIVER_END_OBJECT_TYPE(duration) PHP_DRIVER_BEGIN_OBJECT_TYPE(collection) php5to7_zval type; HashTable values; unsigned hashv; int dirty; PHP_DRIVER_END_OBJECT_TYPE(collection) typedef struct php_driver_map_entry_ php_driver_map_entry; PHP_DRIVER_BEGIN_OBJECT_TYPE(map) php5to7_zval type; php_driver_map_entry *entries; unsigned hashv; int dirty; php_driver_map_entry *iter_curr; php_driver_map_entry *iter_temp; PHP_DRIVER_END_OBJECT_TYPE(map) typedef struct php_driver_set_entry_ php_driver_set_entry; PHP_DRIVER_BEGIN_OBJECT_TYPE(set) php5to7_zval type; php_driver_set_entry *entries; unsigned hashv; int dirty; php_driver_set_entry *iter_curr; php_driver_set_entry *iter_temp; int iter_index; PHP_DRIVER_END_OBJECT_TYPE(set) PHP_DRIVER_BEGIN_OBJECT_TYPE(tuple) php5to7_zval type; HashTable values; HashPosition pos; unsigned hashv; int dirty; PHP_DRIVER_END_OBJECT_TYPE(tuple) PHP_DRIVER_BEGIN_OBJECT_TYPE(user_type_value) php5to7_zval type; HashTable values; HashPosition pos; unsigned hashv; int dirty; PHP_DRIVER_END_OBJECT_TYPE(user_type_value) PHP_DRIVER_BEGIN_OBJECT_TYPE(cluster) cass_byte_t *data; CassCluster *cluster; long default_consistency; int default_page_size; php5to7_zval default_timeout; cass_bool_t persist; char *hash_key; int hash_key_len; PHP_DRIVER_END_OBJECT_TYPE(cluster) typedef enum { PHP_DRIVER_SIMPLE_STATEMENT, PHP_DRIVER_PREPARED_STATEMENT, PHP_DRIVER_BATCH_STATEMENT } php_driver_statement_type; PHP_DRIVER_BEGIN_OBJECT_TYPE(statement) php_driver_statement_type type; union { struct { char *cql; } simple; struct { const CassPrepared *prepared; } prepared; struct { CassBatchType type; HashTable statements; } batch; } data; PHP_DRIVER_END_OBJECT_TYPE(statement) typedef struct { php5to7_zval statement; php5to7_zval arguments; } php_driver_batch_statement_entry; PHP_DRIVER_BEGIN_OBJECT_TYPE(execution_options) long consistency; long serial_consistency; int page_size; char *paging_state_token; size_t paging_state_token_size; php5to7_zval timeout; php5to7_zval arguments; php5to7_zval retry_policy; cass_int64_t timestamp; PHP_DRIVER_END_OBJECT_TYPE(execution_options) typedef enum { LOAD_BALANCING_DEFAULT = 0, LOAD_BALANCING_ROUND_ROBIN, LOAD_BALANCING_DC_AWARE_ROUND_ROBIN } php_driver_load_balancing; typedef void (*php_driver_free_function)(void *data); typedef struct { size_t count; php_driver_free_function destruct; void *data; } php_driver_ref; PHP_DRIVER_BEGIN_OBJECT_TYPE(rows) php_driver_ref *statement; php_driver_ref *session; php5to7_zval rows; php5to7_zval next_rows; php_driver_ref *result; php_driver_ref *next_result; php5to7_zval future_next_page; PHP_DRIVER_END_OBJECT_TYPE(rows) PHP_DRIVER_BEGIN_OBJECT_TYPE(future_rows) php_driver_ref *statement; php_driver_ref *session; php5to7_zval rows; php_driver_ref *result; CassFuture *future; PHP_DRIVER_END_OBJECT_TYPE(future_rows) PHP_DRIVER_BEGIN_OBJECT_TYPE(cluster_builder) char *contact_points; int port; php_driver_load_balancing load_balancing_policy; char *local_dc; unsigned int used_hosts_per_remote_dc; cass_bool_t allow_remote_dcs_for_local_cl; cass_bool_t use_token_aware_routing; char *username; char *password; unsigned int connect_timeout; unsigned int request_timeout; php5to7_zval ssl_options; long default_consistency; int default_page_size; php5to7_zval default_timeout; cass_bool_t persist; int protocol_version; int io_threads; int core_connections_per_host; int max_connections_per_host; unsigned int reconnect_interval; cass_bool_t enable_latency_aware_routing; cass_bool_t enable_tcp_nodelay; cass_bool_t enable_tcp_keepalive; unsigned int tcp_keepalive_delay; php5to7_zval retry_policy; php5to7_zval timestamp_gen; cass_bool_t enable_schema; char *blacklist_hosts; char *whitelist_hosts; char *blacklist_dcs; char *whitelist_dcs; cass_bool_t enable_hostname_resolution; cass_bool_t enable_randomized_contact_points; unsigned int connection_heartbeat_interval; PHP_DRIVER_END_OBJECT_TYPE(cluster_builder) PHP_DRIVER_BEGIN_OBJECT_TYPE(future_prepared_statement) CassFuture *future; php5to7_zval prepared_statement; PHP_DRIVER_END_OBJECT_TYPE(future_prepared_statement) PHP_DRIVER_BEGIN_OBJECT_TYPE(future_value) php5to7_zval value; PHP_DRIVER_END_OBJECT_TYPE(future_value) PHP_DRIVER_BEGIN_OBJECT_TYPE(future_close) CassFuture *future; PHP_DRIVER_END_OBJECT_TYPE(future_close) PHP_DRIVER_BEGIN_OBJECT_TYPE(future_session) CassFuture *future; php_driver_ref *session; php5to7_zval default_session; cass_bool_t persist; char *hash_key; int hash_key_len; char *exception_message; CassError exception_code; PHP_DRIVER_END_OBJECT_TYPE(future_session) typedef struct { CassFuture *future; php_driver_ref *session; } php_driver_psession; PHP_DRIVER_BEGIN_OBJECT_TYPE(session) php_driver_ref *session; long default_consistency; int default_page_size; php5to7_zval default_timeout; cass_bool_t persist; PHP_DRIVER_END_OBJECT_TYPE(session) PHP_DRIVER_BEGIN_OBJECT_TYPE(ssl) CassSsl *ssl; PHP_DRIVER_END_OBJECT_TYPE(ssl) PHP_DRIVER_BEGIN_OBJECT_TYPE(ssl_builder) int flags; char **trusted_certs; int trusted_certs_cnt; char *client_cert; char *private_key; char *passphrase; PHP_DRIVER_END_OBJECT_TYPE(ssl_builder) PHP_DRIVER_BEGIN_OBJECT_TYPE(schema) php_driver_ref *schema; PHP_DRIVER_END_OBJECT_TYPE(schema) PHP_DRIVER_BEGIN_OBJECT_TYPE(keyspace) php_driver_ref *schema; const CassKeyspaceMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(keyspace) PHP_DRIVER_BEGIN_OBJECT_TYPE(table) php5to7_zval name; php5to7_zval options; php5to7_zval partition_key; php5to7_zval primary_key; php5to7_zval clustering_key; php5to7_zval clustering_order; php_driver_ref *schema; const CassTableMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(table) PHP_DRIVER_BEGIN_OBJECT_TYPE(materialized_view) php5to7_zval name; php5to7_zval options; php5to7_zval partition_key; php5to7_zval primary_key; php5to7_zval clustering_key; php5to7_zval clustering_order; php5to7_zval base_table; php_driver_ref *schema; const CassMaterializedViewMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(materialized_view) PHP_DRIVER_BEGIN_OBJECT_TYPE(column) php5to7_zval name; php5to7_zval type; int reversed; int frozen; php_driver_ref *schema; const CassColumnMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(column) PHP_DRIVER_BEGIN_OBJECT_TYPE(index) php5to7_zval name; php5to7_zval kind; php5to7_zval target; php5to7_zval options; php_driver_ref *schema; const CassIndexMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(index) PHP_DRIVER_BEGIN_OBJECT_TYPE(function) php5to7_zval simple_name; php5to7_zval arguments; php5to7_zval return_type; php5to7_zval signature; php5to7_zval language; php5to7_zval body; php_driver_ref *schema; const CassFunctionMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(function) PHP_DRIVER_BEGIN_OBJECT_TYPE(aggregate) php5to7_zval simple_name; php5to7_zval argument_types; php5to7_zval state_function; php5to7_zval final_function; php5to7_zval initial_condition; php5to7_zval state_type; php5to7_zval return_type; php5to7_zval signature; php_driver_ref *schema; const CassAggregateMeta *meta; PHP_DRIVER_END_OBJECT_TYPE(aggregate) PHP_DRIVER_BEGIN_OBJECT_TYPE(type) CassValueType type; CassDataType *data_type; union { struct { php5to7_zval value_type; } collection; struct { php5to7_zval value_type; } set; struct { php5to7_zval key_type; php5to7_zval value_type; } map; struct { char *class_name; } custom; struct { char *keyspace; char *type_name; HashTable types; } udt; struct { HashTable types; } tuple; } data; PHP_DRIVER_END_OBJECT_TYPE(type) PHP_DRIVER_BEGIN_OBJECT_TYPE(retry_policy) CassRetryPolicy *policy; PHP_DRIVER_END_OBJECT_TYPE(retry_policy) PHP_DRIVER_BEGIN_OBJECT_TYPE(timestamp_gen) CassTimestampGen *gen; PHP_DRIVER_END_OBJECT_TYPE(timestamp_gen) typedef unsigned (*php_driver_value_hash_t)(zval *obj TSRMLS_DC); typedef struct { zend_object_handlers std; php_driver_value_hash_t hash_value; } php_driver_value_handlers; extern PHP_DRIVER_API zend_class_entry *php_driver_value_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_numeric_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_bigint_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_smallint_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_tinyint_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_blob_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_decimal_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_float_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_inet_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_timestamp_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_date_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_time_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_uuid_interface_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_uuid_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_timeuuid_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_varint_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_custom_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_duration_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_set_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_map_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_collection_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_tuple_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_user_type_value_ce; /* Exceptions */ void php_driver_define_Exception(TSRMLS_D); void php_driver_define_InvalidArgumentException(TSRMLS_D); void php_driver_define_DomainException(TSRMLS_D); void php_driver_define_LogicException(TSRMLS_D); void php_driver_define_RuntimeException(TSRMLS_D); void php_driver_define_TimeoutException(TSRMLS_D); void php_driver_define_ExecutionException(TSRMLS_D); void php_driver_define_ReadTimeoutException(TSRMLS_D); void php_driver_define_WriteTimeoutException(TSRMLS_D); void php_driver_define_UnavailableException(TSRMLS_D); void php_driver_define_TruncateException(TSRMLS_D); void php_driver_define_ValidationException(TSRMLS_D); void php_driver_define_InvalidQueryException(TSRMLS_D); void php_driver_define_InvalidSyntaxException(TSRMLS_D); void php_driver_define_UnauthorizedException(TSRMLS_D); void php_driver_define_UnpreparedException(TSRMLS_D); void php_driver_define_ConfigurationException(TSRMLS_D); void php_driver_define_AlreadyExistsException(TSRMLS_D); void php_driver_define_AuthenticationException(TSRMLS_D); void php_driver_define_ProtocolException(TSRMLS_D); void php_driver_define_ServerException(TSRMLS_D); void php_driver_define_IsBootstrappingException(TSRMLS_D); void php_driver_define_OverloadedException(TSRMLS_D); void php_driver_define_DivideByZeroException(TSRMLS_D); void php_driver_define_RangeException(TSRMLS_D); /* Types */ void php_driver_define_Value(TSRMLS_D); void php_driver_define_Numeric(TSRMLS_D); void php_driver_define_Bigint(TSRMLS_D); void php_driver_define_Smallint(TSRMLS_D); void php_driver_define_Tinyint(TSRMLS_D); void php_driver_define_Blob(TSRMLS_D); void php_driver_define_Collection(TSRMLS_D); void php_driver_define_Decimal(TSRMLS_D); void php_driver_define_Float(TSRMLS_D); void php_driver_define_Inet(TSRMLS_D); void php_driver_define_Map(TSRMLS_D); void php_driver_define_Set(TSRMLS_D); void php_driver_define_Timestamp(TSRMLS_D); void php_driver_define_Date(TSRMLS_D); void php_driver_define_Time(TSRMLS_D); void php_driver_define_Tuple(TSRMLS_D); void php_driver_define_UserTypeValue(TSRMLS_D); void php_driver_define_UuidInterface(TSRMLS_D); void php_driver_define_Uuid(TSRMLS_D); void php_driver_define_Timeuuid(TSRMLS_D); void php_driver_define_Varint(TSRMLS_D); void php_driver_define_Custom(TSRMLS_D); void php_driver_define_Duration(TSRMLS_D); /* Classes */ extern PHP_DRIVER_API zend_class_entry *php_driver_core_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_cluster_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_cluster_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_cluster_builder_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_cluster_builder_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_ssl_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_ssl_builder_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_prepared_statement_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_rows_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_session_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_value_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_future_close_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_session_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_session_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_runtime_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_timeout_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_logic_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_domain_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_invalid_argument_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_server_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_overloaded_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_is_bootstrapping_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_execution_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_truncate_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_write_timeout_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_read_timeout_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_truncate_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_unavailable_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_validation_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_invalid_syntax_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_unauthorized_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_invalid_query_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_configuration_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_already_exists_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_unprepared_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_protocol_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_authentication_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_divide_by_zero_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_range_exception_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_statement_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_simple_statement_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_prepared_statement_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_batch_statement_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_execution_options_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_rows_ce; void php_driver_define_Core(TSRMLS_D); void php_driver_define_Cluster(TSRMLS_D); void php_driver_define_ClusterBuilder(TSRMLS_D); void php_driver_define_DefaultCluster(TSRMLS_D); void php_driver_define_Future(TSRMLS_D); void php_driver_define_FuturePreparedStatement(TSRMLS_D); void php_driver_define_FutureRows(TSRMLS_D); void php_driver_define_FutureSession(TSRMLS_D); void php_driver_define_FutureValue(TSRMLS_D); void php_driver_define_FutureClose(TSRMLS_D); void php_driver_define_Session(TSRMLS_D); void php_driver_define_DefaultSession(TSRMLS_D); void php_driver_define_SSLOptions(TSRMLS_D); void php_driver_define_SSLOptionsBuilder(TSRMLS_D); void php_driver_define_Statement(TSRMLS_D); void php_driver_define_SimpleStatement(TSRMLS_D); void php_driver_define_PreparedStatement(TSRMLS_D); void php_driver_define_BatchStatement(TSRMLS_D); void php_driver_define_ExecutionOptions(TSRMLS_D); void php_driver_define_Rows(TSRMLS_D); extern PHP_DRIVER_API zend_class_entry *php_driver_schema_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_schema_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_keyspace_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_keyspace_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_table_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_table_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_column_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_column_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_index_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_index_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_materialized_view_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_materialized_view_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_function_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_function_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_aggregate_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_default_aggregate_ce; void php_driver_define_Schema(TSRMLS_D); void php_driver_define_DefaultSchema(TSRMLS_D); void php_driver_define_Keyspace(TSRMLS_D); void php_driver_define_DefaultKeyspace(TSRMLS_D); void php_driver_define_Table(TSRMLS_D); void php_driver_define_DefaultTable(TSRMLS_D); void php_driver_define_Column(TSRMLS_D); void php_driver_define_DefaultColumn(TSRMLS_D); void php_driver_define_Index(TSRMLS_D); void php_driver_define_DefaultIndex(TSRMLS_D); void php_driver_define_MaterializedView(TSRMLS_D); void php_driver_define_DefaultMaterializedView(TSRMLS_D); void php_driver_define_Function(TSRMLS_D); void php_driver_define_DefaultFunction(TSRMLS_D); void php_driver_define_Aggregate(TSRMLS_D); void php_driver_define_DefaultAggregate(TSRMLS_D); extern PHP_DRIVER_API zend_class_entry *php_driver_type_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_scalar_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_collection_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_set_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_map_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_tuple_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_user_type_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_type_custom_ce; void php_driver_define_Type(TSRMLS_D); void php_driver_define_TypeScalar(TSRMLS_D); void php_driver_define_TypeCollection(TSRMLS_D); void php_driver_define_TypeSet(TSRMLS_D); void php_driver_define_TypeMap(TSRMLS_D); void php_driver_define_TypeTuple(TSRMLS_D); void php_driver_define_TypeUserType(TSRMLS_D); void php_driver_define_TypeCustom(TSRMLS_D); extern PHP_DRIVER_API zend_class_entry *php_driver_retry_policy_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_retry_policy_default_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_retry_policy_downgrading_consistency_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_retry_policy_fallthrough_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_retry_policy_logging_ce; void php_driver_define_RetryPolicy(TSRMLS_D); void php_driver_define_RetryPolicyDefault(TSRMLS_D); void php_driver_define_RetryPolicyDowngradingConsistency(TSRMLS_D); void php_driver_define_RetryPolicyFallthrough(TSRMLS_D); void php_driver_define_RetryPolicyLogging(TSRMLS_D); extern PHP_DRIVER_API zend_class_entry *php_driver_timestamp_gen_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_timestamp_gen_monotonic_ce; extern PHP_DRIVER_API zend_class_entry *php_driver_timestamp_gen_server_side_ce; void php_driver_define_TimestampGenerator(TSRMLS_D); void php_driver_define_TimestampGeneratorMonotonic(TSRMLS_D); void php_driver_define_TimestampGeneratorServerSide(TSRMLS_D); extern int php_le_php_driver_cluster(); extern int php_le_php_driver_session(); #endif /* PHP_DRIVER_TYPES_H */ cassandra-1.3.0/src/Aggregate.c0000644000076500000240000000314213062515664016762 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_aggregate_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_aggregate_methods[] = { PHP_ABSTRACT_ME(Aggregate, name, arginfo_none) PHP_ABSTRACT_ME(Aggregate, simpleName, arginfo_none) PHP_ABSTRACT_ME(Aggregate, argumentTypes, arginfo_none) PHP_ABSTRACT_ME(Aggregate, finalFunction, arginfo_none) PHP_ABSTRACT_ME(Aggregate, stateFunction, arginfo_none) PHP_ABSTRACT_ME(Aggregate, initialCondition, arginfo_none) PHP_ABSTRACT_ME(Aggregate, returnType, arginfo_none) PHP_ABSTRACT_ME(Aggregate, stateType, arginfo_none) PHP_ABSTRACT_ME(Aggregate, signature, arginfo_none) PHP_FE_END }; void php_driver_define_Aggregate(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Aggregate", php_driver_aggregate_methods); php_driver_aggregate_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_aggregate_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/BatchStatement.c0000644000076500000240000001354013062515664020005 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_batch_statement_ce = NULL; void php_driver_batch_statement_entry_dtor(php5to7_dtor dest) { #if PHP_MAJOR_VERSION >= 7 php_driver_batch_statement_entry *batch_statement_entry = Z_PTR_P(dest); #else php_driver_batch_statement_entry *batch_statement_entry = *((php_driver_batch_statement_entry **) dest); #endif zval_ptr_dtor(&batch_statement_entry->statement); PHP5TO7_ZVAL_MAYBE_DESTROY(batch_statement_entry->arguments); efree(batch_statement_entry); } PHP_METHOD(BatchStatement, __construct) { zval *type = NULL; php_driver_statement *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &type) == FAILURE) { return; } self = PHP_DRIVER_GET_STATEMENT(getThis()); if (type) { if (Z_TYPE_P(type) != IS_LONG) { INVALID_ARGUMENT(type, "one of " PHP_DRIVER_NAMESPACE "::BATCH_TYPE_*"); } switch (Z_LVAL_P(type)) { case CASS_BATCH_TYPE_LOGGED: case CASS_BATCH_TYPE_UNLOGGED: case CASS_BATCH_TYPE_COUNTER: self->data.batch.type = (CassBatchType) Z_LVAL_P(type); break; default: INVALID_ARGUMENT(type, "one of " PHP_DRIVER_NAMESPACE "::BATCH_TYPE_*"); } } } PHP_METHOD(BatchStatement, add) { zval *statement = NULL; zval *arguments = NULL; php_driver_batch_statement_entry *batch_statement_entry = NULL; php_driver_statement *self = NULL; #if PHP_MAJOR_VERSION >= 7 zval entry; #endif if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &statement, &arguments) == FAILURE) { return; } if (Z_TYPE_P(statement) != IS_STRING && (Z_TYPE_P(statement) != IS_OBJECT || (!instanceof_function(Z_OBJCE_P(statement), php_driver_simple_statement_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(statement), php_driver_prepared_statement_ce TSRMLS_CC)))) { INVALID_ARGUMENT(statement, "a string, an instance of " PHP_DRIVER_NAMESPACE "\\SimpleStatement or an instance of " PHP_DRIVER_NAMESPACE "\\PreparedStatement"); } self = PHP_DRIVER_GET_STATEMENT(getThis()); batch_statement_entry = (php_driver_batch_statement_entry *) ecalloc(1, sizeof(php_driver_batch_statement_entry)); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(batch_statement_entry->statement), statement); if (arguments) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(batch_statement_entry->arguments), arguments); } #if PHP_MAJOR_VERSION >= 7 ZVAL_PTR(&entry, batch_statement_entry); zend_hash_next_index_insert(&self->data.batch.statements, &entry); #else zend_hash_next_index_insert(&self->data.batch.statements, &batch_statement_entry, sizeof(php_driver_batch_statement_entry *), NULL); #endif } ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, type) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_add, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, statement) ZEND_ARG_ARRAY_INFO(0, arguments, 1) ZEND_END_ARG_INFO() static zend_function_entry php_driver_batch_statement_methods[] = { PHP_ME(BatchStatement, __construct, arginfo__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(BatchStatement, add, arginfo_add, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_batch_statement_handlers; static HashTable * php_driver_batch_statement_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_batch_statement_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_batch_statement_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_GET(statement, object); zend_hash_destroy(&self->data.batch.statements); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_batch_statement_new(zend_class_entry *ce TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_ECALLOC(statement, ce); self->type = PHP_DRIVER_BATCH_STATEMENT; self->data.batch.type = CASS_BATCH_TYPE_LOGGED; zend_hash_init(&self->data.batch.statements, 0, NULL, (dtor_func_t) php_driver_batch_statement_entry_dtor, 0); PHP5TO7_ZEND_OBJECT_INIT_EX(statement, batch_statement, self, ce); } void php_driver_define_BatchStatement(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\BatchStatement", php_driver_batch_statement_methods); php_driver_batch_statement_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_batch_statement_ce TSRMLS_CC, 1, php_driver_statement_ce); php_driver_batch_statement_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_batch_statement_ce->create_object = php_driver_batch_statement_new; memcpy(&php_driver_batch_statement_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_batch_statement_handlers.get_properties = php_driver_batch_statement_properties; php_driver_batch_statement_handlers.compare_objects = php_driver_batch_statement_compare; php_driver_batch_statement_handlers.clone_obj = NULL; } cassandra-1.3.0/src/Bigint.c0000644000076500000240000003557213062515664016324 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #if !defined(HAVE_STDINT_H) && !defined(_MSC_STDINT_H_) # define INT64_MAX 9223372036854775807LL # define INT64_MIN (-INT64_MAX-1) #endif zend_class_entry *php_driver_bigint_ce = NULL; static int to_double(zval *result, php_driver_numeric *bigint TSRMLS_DC) { ZVAL_DOUBLE(result, (double) bigint->data.bigint.value); return SUCCESS; } static int to_long(zval *result, php_driver_numeric *bigint TSRMLS_DC) { if (bigint->data.bigint.value < (cass_int64_t) PHP5TO7_ZEND_LONG_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small"); return FAILURE; } if (bigint->data.bigint.value > (cass_int64_t) PHP5TO7_ZEND_LONG_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too big"); return FAILURE; } ZVAL_LONG(result, (php5to7_long) bigint->data.bigint.value); return SUCCESS; } static int to_string(zval *result, php_driver_numeric *bigint TSRMLS_DC) { char *string; spprintf(&string, 0, LL_FORMAT, (long long int) bigint->data.bigint.value); PHP5TO7_ZVAL_STRING(result, string); efree(string); return SUCCESS; } void php_driver_bigint_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_numeric *self; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_bigint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_bigint_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(value) == IS_LONG) { self->data.bigint.value = (cass_int64_t) Z_LVAL_P(value); } else if (Z_TYPE_P(value) == IS_DOUBLE) { double double_value = Z_DVAL_P(value); if (double_value > INT64_MAX || double_value < INT64_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between " LL_FORMAT " and " LL_FORMAT ", %g given", INT64_MIN, INT64_MAX, double_value); return; } self->data.bigint.value = (cass_int64_t) Z_DVAL_P(value); } else if (Z_TYPE_P(value) == IS_STRING) { if (!php_driver_parse_bigint(Z_STRVAL_P(value), Z_STRLEN_P(value), &self->data.bigint.value TSRMLS_CC)) { return; } } else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(value); self->data.bigint.value = bigint->data.bigint.value; } else { INVALID_ARGUMENT(value, "a long, a double, a numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* {{{ Bigint::__construct(string) */ PHP_METHOD(Bigint, __construct) { php_driver_bigint_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Bigint::__toString() */ PHP_METHOD(Bigint, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Bigint::type() */ PHP_METHOD(Bigint, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_BIGINT TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Bigint::value() */ PHP_METHOD(Bigint, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Bigint::add() */ PHP_METHOD(Bigint, add) { zval *num; php_driver_numeric *self; php_driver_numeric *bigint; php_driver_numeric *result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_bigint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); bigint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = self->data.bigint.value + bigint->data.bigint.value; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* }}} */ /* {{{ Bigint::sub() */ PHP_METHOD(Bigint, sub) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = self->data.bigint.value - bigint->data.bigint.value; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* }}} */ /* {{{ Bigint::mul() */ PHP_METHOD(Bigint, mul) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = self->data.bigint.value * bigint->data.bigint.value; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* }}} */ /* {{{ Bigint::div() */ PHP_METHOD(Bigint, div) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (bigint->data.bigint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } result->data.bigint.value = self->data.bigint.value / bigint->data.bigint.value; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* }}} */ /* {{{ Bigint::mod() */ PHP_METHOD(Bigint, mod) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (bigint->data.bigint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot modulo by zero"); return; } result->data.bigint.value = self->data.bigint.value % bigint->data.bigint.value; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Bigint"); } } /* }}} */ /* {{{ Bigint::abs() */ PHP_METHOD(Bigint, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.bigint.value == INT64_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value doesn't exist"); return; } object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = self->data.bigint.value < 0 ? -self->data.bigint.value : self->data.bigint.value; } /* }}} */ /* {{{ Bigint::neg() */ PHP_METHOD(Bigint, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = -self->data.bigint.value; } /* }}} */ /* {{{ Bigint::sqrt() */ PHP_METHOD(Bigint, sqrt) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.bigint.value < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Cannot take a square root of a negative number"); } object_init_ex(return_value, php_driver_bigint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.bigint.value = (cass_int64_t) sqrt((long double) self->data.bigint.value); } /* }}} */ /* {{{ Bigint::toInt() */ PHP_METHOD(Bigint, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Bigint::toDouble() */ PHP_METHOD(Bigint, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_double(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Bigint::min() */ PHP_METHOD(Bigint, min) { php_driver_numeric *bigint = NULL; object_init_ex(return_value, php_driver_bigint_ce); bigint = PHP_DRIVER_GET_NUMERIC(return_value); bigint->data.bigint.value = INT64_MIN; } /* }}} */ /* {{{ Bigint::max() */ PHP_METHOD(Bigint, max) { php_driver_numeric *bigint = NULL; object_init_ex(return_value, php_driver_bigint_ce); bigint = PHP_DRIVER_GET_NUMERIC(return_value); bigint->data.bigint.value = INT64_MAX; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_bigint_methods[] = { PHP_ME(Bigint, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Bigint, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Bigint, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Bigint, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Bigint, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Bigint, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Bigint, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Bigint, min, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Bigint, max, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; static php_driver_value_handlers php_driver_bigint_handlers; static HashTable * php_driver_bigint_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_bigint_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval value; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_BIGINT TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(value); to_string(PHP5TO7_ZVAL_MAYBE_P(value), self TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); return props; } static int php_driver_bigint_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_numeric *bigint1 = NULL; php_driver_numeric *bigint2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ bigint1 = PHP_DRIVER_GET_NUMERIC(obj1); bigint2 = PHP_DRIVER_GET_NUMERIC(obj2); if (bigint1->data.bigint.value == bigint2->data.bigint.value) return 0; else if (bigint1->data.bigint.value < bigint2->data.bigint.value) return -1; else return 1; } static unsigned php_driver_bigint_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return (unsigned)(self->data.bigint.value ^ (self->data.bigint.value >> 32)); } static int php_driver_bigint_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: return to_long(retval, self TSRMLS_CC); case IS_DOUBLE: return to_double(retval, self TSRMLS_CC); case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_bigint_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_bigint_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); self->type = PHP_DRIVER_BIGINT; PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, bigint, self, ce); } void php_driver_define_Bigint(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Bigint", php_driver_bigint_methods); php_driver_bigint_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_bigint_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_bigint_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_bigint_ce->create_object = php_driver_bigint_new; memcpy(&php_driver_bigint_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_bigint_handlers.std.get_properties = php_driver_bigint_properties; #if PHP_VERSION_ID >= 50400 php_driver_bigint_handlers.std.get_gc = php_driver_bigint_gc; #endif php_driver_bigint_handlers.std.compare_objects = php_driver_bigint_compare; php_driver_bigint_handlers.std.cast_object = php_driver_bigint_cast; php_driver_bigint_handlers.hash_value = php_driver_bigint_hash_value; php_driver_bigint_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Bigint.h0000644000076500000240000000135713062515664016323 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_BIGINT_H #define PHP_DRIVER_BIGINT_H void php_driver_bigint_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_BIGINT_H */ cassandra-1.3.0/src/Blob.c0000644000076500000240000001373513062515664015763 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/bytes.h" #include "util/types.h" zend_class_entry *php_driver_blob_ce = NULL; void php_driver_blob_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_blob *self; char *string; php5to7_size string_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_blob_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_BLOB(getThis()); } else { object_init_ex(return_value, php_driver_blob_ce); self = PHP_DRIVER_GET_BLOB(return_value); } self->data = emalloc(string_len * sizeof(cass_byte_t)); self->size = string_len; memcpy(self->data, string, string_len); } /* {{{ Blob::__construct(string) */ PHP_METHOD(Blob, __construct) { php_driver_blob_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Blob::__toString() */ PHP_METHOD(Blob, __toString) { php_driver_blob *self = PHP_DRIVER_GET_BLOB(getThis()); char *hex; int hex_len; php_driver_bytes_to_hex((const char *) self->data, self->size, &hex, &hex_len); PHP5TO7_RETVAL_STRINGL(hex, hex_len); efree(hex); } /* }}} */ /* {{{ Blob::type() */ PHP_METHOD(Blob, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_BLOB TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Blob::bytes() */ PHP_METHOD(Blob, bytes) { php_driver_blob *self = PHP_DRIVER_GET_BLOB(getThis()); char *hex; int hex_len; php_driver_bytes_to_hex((const char *) self->data, self->size, &hex, &hex_len); PHP5TO7_RETVAL_STRINGL(hex, hex_len); efree(hex); } /* }}} */ /* {{{ Blob::toBinaryString() */ PHP_METHOD(Blob, toBinaryString) { php_driver_blob *blob = PHP_DRIVER_GET_BLOB(getThis()); PHP5TO7_RETVAL_STRINGL((const char *)blob->data, blob->size); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, bytes) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_blob_methods[] = { PHP_ME(Blob, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Blob, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Blob, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Blob, bytes, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Blob, toBinaryString, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_blob_handlers; static HashTable * php_driver_blob_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_blob_properties(zval *object TSRMLS_DC) { char *hex; int hex_len; php5to7_zval type; php5to7_zval bytes; php_driver_blob *self = PHP_DRIVER_GET_BLOB(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_BLOB TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); php_driver_bytes_to_hex((const char *) self->data, self->size, &hex, &hex_len); PHP5TO7_ZVAL_MAYBE_MAKE(bytes); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(bytes), hex, hex_len); efree(hex); PHP5TO7_ZEND_HASH_UPDATE(props, "bytes", sizeof("bytes"), PHP5TO7_ZVAL_MAYBE_P(bytes), sizeof(zval)); return props; } static int php_driver_blob_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_blob *blob1 = NULL; php_driver_blob *blob2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ blob1 = PHP_DRIVER_GET_BLOB(obj1); blob2 = PHP_DRIVER_GET_BLOB(obj2); if (blob1->size == blob2->size) { return memcmp((const char *) blob1->data, (const char *) blob2->data, blob1->size); } else if (blob1->size < blob2->size) { return -1; } else { return 1; } } static unsigned php_driver_blob_hash_value(zval *obj TSRMLS_DC) { php_driver_blob *self = PHP_DRIVER_GET_BLOB(obj); return zend_inline_hash_func((const char *) self->data, self->size); } static void php_driver_blob_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_blob *self = PHP5TO7_ZEND_OBJECT_GET(blob, object); if (self->data) { efree(self->data); } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_blob_new(zend_class_entry *ce TSRMLS_DC) { php_driver_blob *self = PHP5TO7_ZEND_OBJECT_ECALLOC(blob, ce); PHP5TO7_ZEND_OBJECT_INIT(blob, self, ce); } void php_driver_define_Blob(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Blob", php_driver_blob_methods); php_driver_blob_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_blob_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_blob_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_blob_handlers.std.get_properties = php_driver_blob_properties; #if PHP_VERSION_ID >= 50400 php_driver_blob_handlers.std.get_gc = php_driver_blob_gc; #endif php_driver_blob_handlers.std.compare_objects = php_driver_blob_compare; php_driver_blob_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_blob_ce->create_object = php_driver_blob_new; php_driver_blob_handlers.hash_value = php_driver_blob_hash_value; php_driver_blob_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Blob.h0000644000076500000240000000134713062515664015764 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_BLOB_H #define PHP_DRIVER_BLOB_H void php_driver_blob_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_BLOB_H */ cassandra-1.3.0/src/Cluster.c0000644000076500000240000000236013062515664016516 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_cluster_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_keyspace, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, keyspace) ZEND_END_ARG_INFO() static zend_function_entry php_driver_cluster_methods[] = { PHP_ABSTRACT_ME(Cluster, connect, arginfo_keyspace) PHP_ABSTRACT_ME(Cluster, connectAsync, arginfo_keyspace) PHP_FE_END }; void php_driver_define_Cluster(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Cluster", php_driver_cluster_methods); php_driver_cluster_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_cluster_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Cluster/Builder.c0000644000076500000240000013712113062515664020110 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "util/consistency.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif zend_class_entry *php_driver_cluster_builder_ce = NULL; PHP_METHOD(ClusterBuilder, build) { php_driver_cluster* cluster; php_driver_cluster_builder *self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); object_init_ex(return_value, php_driver_default_cluster_ce); cluster = PHP_DRIVER_GET_CLUSTER(return_value); cluster->persist = self->persist; cluster->default_consistency = self->default_consistency; cluster->default_page_size = self->default_page_size; PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(cluster->default_timeout), PHP5TO7_ZVAL_MAYBE_P(self->default_timeout)); if (self->persist) { cluster->hash_key_len = spprintf(&cluster->hash_key, 0, PHP_DRIVER_NAME ":%s:%d:%d:%s:%d:%d:%d:%s:%s:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%s:%s:%s:%s", self->contact_points, self->port, self->load_balancing_policy, SAFE_STR(self->local_dc), self->used_hosts_per_remote_dc, self->allow_remote_dcs_for_local_cl, self->use_token_aware_routing, SAFE_STR(self->username), SAFE_STR(self->password), self->connect_timeout, self->request_timeout, self->protocol_version, self->io_threads, self->core_connections_per_host, self->max_connections_per_host, self->reconnect_interval, self->enable_latency_aware_routing, self->enable_tcp_nodelay, self->enable_tcp_keepalive, self->tcp_keepalive_delay, self->enable_schema, self->enable_hostname_resolution, self->enable_randomized_contact_points, self->connection_heartbeat_interval, SAFE_STR(self->whitelist_hosts), SAFE_STR(self->whitelist_dcs), SAFE_STR(self->blacklist_hosts), SAFE_STR(self->blacklist_dcs)); if (self->persist) { php5to7_zend_resource_le *le; if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), cluster->hash_key, cluster->hash_key_len + 1, le)) { if (Z_TYPE_P(le) == php_le_php_driver_cluster()) { cluster->cluster = (CassCluster*) Z_RES_P(le)->ptr; return; /* Return cached version */ } } } } cluster->cluster = cass_cluster_new(); if (self->load_balancing_policy == LOAD_BALANCING_ROUND_ROBIN) { cass_cluster_set_load_balance_round_robin(cluster->cluster); } if (self->load_balancing_policy == LOAD_BALANCING_DC_AWARE_ROUND_ROBIN) { ASSERT_SUCCESS(cass_cluster_set_load_balance_dc_aware(cluster->cluster, self->local_dc, self->used_hosts_per_remote_dc, self->allow_remote_dcs_for_local_cl)); } if (self->blacklist_hosts != NULL) { cass_cluster_set_blacklist_filtering(cluster->cluster, self->blacklist_hosts); } if (self->whitelist_hosts != NULL) { cass_cluster_set_whitelist_filtering(cluster->cluster, self->whitelist_hosts); } if (self->blacklist_dcs != NULL) { cass_cluster_set_blacklist_dc_filtering(cluster->cluster, self->blacklist_dcs); } if (self->whitelist_dcs != NULL) { cass_cluster_set_whitelist_dc_filtering(cluster->cluster, self->whitelist_dcs); } cass_cluster_set_token_aware_routing(cluster->cluster, self->use_token_aware_routing); if (self->username) { cass_cluster_set_credentials(cluster->cluster, self->username, self->password); } cass_cluster_set_connect_timeout(cluster->cluster, self->connect_timeout); cass_cluster_set_request_timeout(cluster->cluster, self->request_timeout); if (!PHP5TO7_ZVAL_IS_UNDEF(self->ssl_options)) { php_driver_ssl *options = PHP_DRIVER_GET_SSL(PHP5TO7_ZVAL_MAYBE_P(self->ssl_options)); cass_cluster_set_ssl(cluster->cluster, options->ssl); } ASSERT_SUCCESS(cass_cluster_set_contact_points(cluster->cluster, self->contact_points)); ASSERT_SUCCESS(cass_cluster_set_port(cluster->cluster, self->port)); ASSERT_SUCCESS(cass_cluster_set_protocol_version(cluster->cluster, self->protocol_version)); ASSERT_SUCCESS(cass_cluster_set_num_threads_io(cluster->cluster, self->io_threads)); ASSERT_SUCCESS(cass_cluster_set_core_connections_per_host(cluster->cluster, self->core_connections_per_host)); ASSERT_SUCCESS(cass_cluster_set_max_connections_per_host(cluster->cluster, self->max_connections_per_host)); cass_cluster_set_reconnect_wait_time(cluster->cluster, self->reconnect_interval); cass_cluster_set_latency_aware_routing(cluster->cluster, self->enable_latency_aware_routing); cass_cluster_set_tcp_nodelay(cluster->cluster, self->enable_tcp_nodelay); cass_cluster_set_tcp_keepalive(cluster->cluster, self->enable_tcp_keepalive, self->tcp_keepalive_delay); cass_cluster_set_use_schema(cluster->cluster, self->enable_schema); ASSERT_SUCCESS(cass_cluster_set_use_hostname_resolution(cluster->cluster, self->enable_hostname_resolution)); ASSERT_SUCCESS(cass_cluster_set_use_randomized_contact_points(cluster->cluster, self->enable_randomized_contact_points)); cass_cluster_set_connection_heartbeat_interval(cluster->cluster, self->connection_heartbeat_interval); if (!PHP5TO7_ZVAL_IS_UNDEF(self->timestamp_gen)) { php_driver_timestamp_gen *timestamp_gen = PHP_DRIVER_GET_TIMESTAMP_GEN(PHP5TO7_ZVAL_MAYBE_P(self->timestamp_gen)); cass_cluster_set_timestamp_gen(cluster->cluster, timestamp_gen->gen); } if (!PHP5TO7_ZVAL_IS_UNDEF(self->retry_policy)) { php_driver_retry_policy *retry_policy = PHP_DRIVER_GET_RETRY_POLICY(PHP5TO7_ZVAL_MAYBE_P(self->retry_policy)); cass_cluster_set_retry_policy(cluster->cluster, retry_policy->policy); } if (self->persist) { php5to7_zend_resource_le resource; #if PHP_MAJOR_VERSION >= 7 ZVAL_NEW_PERSISTENT_RES(&resource, 0, cluster->cluster, php_le_php_driver_cluster()); PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), cluster->hash_key, cluster->hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_clusters)++; #else resource.type = php_le_php_driver_cluster(); resource.ptr = cluster->cluster; PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), cluster->hash_key, cluster->hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_clusters)++; #endif } } PHP_METHOD(ClusterBuilder, withDefaultConsistency) { zval *consistency = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &consistency) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (php_driver_get_consistency(consistency, &self->default_consistency TSRMLS_CC) == FAILURE) { return; } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withDefaultPageSize) { zval *pageSize = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &pageSize) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(pageSize) == IS_NULL) { self->default_page_size = -1; } else if (Z_TYPE_P(pageSize) == IS_LONG && Z_LVAL_P(pageSize) > 0) { self->default_page_size = Z_LVAL_P(pageSize); } else { INVALID_ARGUMENT(pageSize, "a positive integer or null"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withDefaultTimeout) { zval *timeout = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(timeout) == IS_NULL) { PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_timeout); PHP5TO7_ZVAL_UNDEF(self->default_timeout); } else if ((Z_TYPE_P(timeout) == IS_LONG && Z_LVAL_P(timeout) > 0) || (Z_TYPE_P(timeout) == IS_DOUBLE && Z_LVAL_P(timeout) > 0)) { PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_timeout); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->default_timeout), timeout); } else { INVALID_ARGUMENT(timeout, "a number of seconds greater than zero or null"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withContactPoints) { zval *host = NULL; php5to7_zval_args args = NULL; int argc = 0, i; smart_str contactPoints = PHP5TO7_SMART_STR_INIT; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); for (i = 0; i < argc; i++) { host = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(host) != IS_STRING) { smart_str_free(&contactPoints); throw_invalid_argument(host, "host", "a string ip address or hostname" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); return; } if (i > 0) { smart_str_appendl(&contactPoints, ",", 1); } smart_str_appendl(&contactPoints, Z_STRVAL_P(host), Z_STRLEN_P(host)); } PHP5TO7_MAYBE_EFREE(args); smart_str_0(&contactPoints); efree(self->contact_points); #if PHP_MAJOR_VERSION >= 7 self->contact_points = estrndup(contactPoints.s->val, contactPoints.s->len); smart_str_free(&contactPoints); #else self->contact_points = contactPoints.c; #endif RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withPort) { zval *port = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &port) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(port) == IS_LONG && Z_LVAL_P(port) > 0 && Z_LVAL_P(port) < 65536) { self->port = Z_LVAL_P(port); } else { INVALID_ARGUMENT(port, "an integer between 1 and 65535"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withRoundRobinLoadBalancingPolicy) { php_driver_cluster_builder *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (self->local_dc) { efree(self->local_dc); self->local_dc = NULL; } self->load_balancing_policy = LOAD_BALANCING_ROUND_ROBIN; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withDatacenterAwareRoundRobinLoadBalancingPolicy) { char *local_dc; php5to7_size local_dc_len; zval *hostPerRemoteDatacenter = NULL; zend_bool allow_remote_dcs_for_local_cl; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szb", &local_dc, &local_dc_len, &hostPerRemoteDatacenter, &allow_remote_dcs_for_local_cl) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(hostPerRemoteDatacenter) != IS_LONG || Z_LVAL_P(hostPerRemoteDatacenter) < 0) { INVALID_ARGUMENT(hostPerRemoteDatacenter, "a positive integer"); } if (self->local_dc) { efree(self->local_dc); self->local_dc = NULL; } self->load_balancing_policy = LOAD_BALANCING_DC_AWARE_ROUND_ROBIN; self->local_dc = estrndup(local_dc, local_dc_len); self->used_hosts_per_remote_dc = Z_LVAL_P(hostPerRemoteDatacenter); self->allow_remote_dcs_for_local_cl = allow_remote_dcs_for_local_cl; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withBlackListHosts) { zval *hosts = NULL; php5to7_zval_args args = NULL; int argc = 0, i; smart_str blacklist_hosts = PHP5TO7_SMART_STR_INIT; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); for (i = 0; i < argc; i++) { hosts = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(hosts) != IS_STRING) { smart_str_free(&blacklist_hosts); throw_invalid_argument(hosts, "hosts", "a string ip address or hostname" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); return; } if (i > 0) { smart_str_appendl(&blacklist_hosts, ",", 1); } smart_str_appendl(&blacklist_hosts, Z_STRVAL_P(hosts), Z_STRLEN_P(hosts)); } PHP5TO7_MAYBE_EFREE(args); smart_str_0(&blacklist_hosts); efree(self->blacklist_hosts); #if PHP_MAJOR_VERSION >= 7 self->blacklist_hosts = estrndup(blacklist_hosts.s->val, blacklist_hosts.s->len); smart_str_free(&blacklist_hosts); #else self->blacklist_hosts = blacklist_hosts.c; #endif RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withWhiteListHosts) { zval *hosts = NULL; php5to7_zval_args args = NULL; int argc = 0, i; smart_str whitelist_hosts = PHP5TO7_SMART_STR_INIT; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); for (i = 0; i < argc; i++) { hosts = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(hosts) != IS_STRING) { smart_str_free(&whitelist_hosts); throw_invalid_argument(hosts, "hosts", "a string ip address or hostname" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); return; } if (i > 0) { smart_str_appendl(&whitelist_hosts, ",", 1); } smart_str_appendl(&whitelist_hosts, Z_STRVAL_P(hosts), Z_STRLEN_P(hosts)); } PHP5TO7_MAYBE_EFREE(args); smart_str_0(&whitelist_hosts); efree(self->whitelist_hosts); #if PHP_MAJOR_VERSION >= 7 self->whitelist_hosts = estrndup(whitelist_hosts.s->val, whitelist_hosts.s->len); smart_str_free(&whitelist_hosts); #else self->whitelist_hosts = whitelist_hosts.c; #endif RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withBlackListDCs) { zval *dcs = NULL; php5to7_zval_args args = NULL; int argc = 0, i; smart_str blacklist_dcs = PHP5TO7_SMART_STR_INIT; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); for (i = 0; i < argc; i++) { dcs = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(dcs) != IS_STRING) { smart_str_free(&blacklist_dcs); throw_invalid_argument(dcs, "dcs", "a string" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); return; } if (i > 0) { smart_str_appendl(&blacklist_dcs, ",", 1); } smart_str_appendl(&blacklist_dcs, Z_STRVAL_P(dcs), Z_STRLEN_P(dcs)); } PHP5TO7_MAYBE_EFREE(args); smart_str_0(&blacklist_dcs); efree(self->blacklist_dcs); #if PHP_MAJOR_VERSION >= 7 self->blacklist_dcs = estrndup(blacklist_dcs.s->val, blacklist_dcs.s->len); smart_str_free(&blacklist_dcs); #else self->blacklist_dcs = blacklist_dcs.c; #endif RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withWhiteListDCs) { zval *dcs = NULL; php5to7_zval_args args = NULL; int argc = 0, i; smart_str whitelist_dcs = PHP5TO7_SMART_STR_INIT; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); for (i = 0; i < argc; i++) { dcs = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(dcs) != IS_STRING) { smart_str_free(&whitelist_dcs); throw_invalid_argument(dcs, "dcs", "a string" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); return; } if (i > 0) { smart_str_appendl(&whitelist_dcs, ",", 1); } smart_str_appendl(&whitelist_dcs, Z_STRVAL_P(dcs), Z_STRLEN_P(dcs)); } PHP5TO7_MAYBE_EFREE(args); smart_str_0(&whitelist_dcs); efree(self->whitelist_dcs); #if PHP_MAJOR_VERSION >= 7 self->whitelist_dcs = estrndup(whitelist_dcs.s->val, whitelist_dcs.s->len); smart_str_free(&whitelist_dcs); #else self->whitelist_dcs = whitelist_dcs.c; #endif RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withTokenAwareRouting) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->use_token_aware_routing = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withCredentials) { zval *username = NULL; zval *password = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &username, &password) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(username) != IS_STRING) { INVALID_ARGUMENT(username, "a string"); } if (Z_TYPE_P(password) != IS_STRING) { INVALID_ARGUMENT(password, "a string"); } if (self->username) { efree(self->username); efree(self->password); } self->username = estrndup(Z_STRVAL_P(username), Z_STRLEN_P(username)); self->password = estrndup(Z_STRVAL_P(password), Z_STRLEN_P(password)); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withConnectTimeout) { zval *timeout = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(timeout) == IS_LONG && Z_LVAL_P(timeout) > 0) { self->connect_timeout = Z_LVAL_P(timeout) * 1000; } else if (Z_TYPE_P(timeout) == IS_DOUBLE && Z_DVAL_P(timeout) > 0) { self->connect_timeout = ceil(Z_DVAL_P(timeout) * 1000); } else { INVALID_ARGUMENT(timeout, "a positive number"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withRequestTimeout) { double timeout; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->request_timeout = ceil(timeout * 1000); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withSSL) { zval *ssl_options = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &ssl_options, php_driver_ssl_ce) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->ssl_options)) zval_ptr_dtor(&self->ssl_options); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->ssl_options), ssl_options); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withPersistentSessions) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->persist = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withProtocolVersion) { zval *version = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &version) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(version) == IS_LONG && Z_LVAL_P(version) >= 1) { self->protocol_version = Z_LVAL_P(version); } else { INVALID_ARGUMENT(version, "must be >= 1"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withIOThreads) { zval *count = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &count) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(count) == IS_LONG && Z_LVAL_P(count) < 129 && Z_LVAL_P(count) > 0) { self->io_threads = Z_LVAL_P(count); } else { INVALID_ARGUMENT(count, "a number between 1 and 128"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withConnectionsPerHost) { zval *core = NULL; zval *max = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &core, &max) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(core) == IS_LONG && Z_LVAL_P(core) < 129 && Z_LVAL_P(core) > 0) { self->core_connections_per_host = Z_LVAL_P(core); } else { INVALID_ARGUMENT(core, "a number between 1 and 128"); } if (max == NULL || Z_TYPE_P(max) == IS_NULL) { self->max_connections_per_host = Z_LVAL_P(core); } else if (Z_TYPE_P(max) == IS_LONG) { if (Z_LVAL_P(max) < Z_LVAL_P(core)) { INVALID_ARGUMENT(max, "greater than core"); } else if (Z_LVAL_P(max) > 128) { INVALID_ARGUMENT(max, "less than 128"); } else { self->max_connections_per_host = Z_LVAL_P(max); } } else { INVALID_ARGUMENT(max, "a number between 1 and 128"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withReconnectInterval) { zval *interval = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &interval) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(interval) == IS_LONG && Z_LVAL_P(interval) > 0) { self->reconnect_interval = Z_LVAL_P(interval) * 1000; } else if (Z_TYPE_P(interval) == IS_DOUBLE && Z_DVAL_P(interval) > 0) { self->reconnect_interval = ceil(Z_DVAL_P(interval) * 1000); } else { INVALID_ARGUMENT(interval, "a positive number"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withLatencyAwareRouting) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->enable_latency_aware_routing = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withTCPNodelay) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->enable_tcp_nodelay = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withTCPKeepalive) { zval *delay = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &delay) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(delay) == IS_NULL) { self->enable_tcp_keepalive = 0; self->tcp_keepalive_delay = 0; } else if (Z_TYPE_P(delay) == IS_LONG && Z_LVAL_P(delay) > 0) { self->enable_tcp_keepalive = 1; self->tcp_keepalive_delay = Z_LVAL_P(delay) * 1000; } else if (Z_TYPE_P(delay) == IS_DOUBLE && Z_DVAL_P(delay) > 0) { self->enable_tcp_keepalive = 1; self->tcp_keepalive_delay = ceil(Z_DVAL_P(delay) * 1000); } else { INVALID_ARGUMENT(delay, "a positive number or null"); } RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withRetryPolicy) { zval *retry_policy = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &retry_policy, php_driver_retry_policy_ce) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->retry_policy)) zval_ptr_dtor(&self->retry_policy); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->retry_policy), retry_policy); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withTimestampGenerator) { zval *timestamp_gen = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", ×tamp_gen, php_driver_timestamp_gen_ce) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->timestamp_gen)) zval_ptr_dtor(&self->timestamp_gen); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->timestamp_gen), timestamp_gen); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withSchemaMetadata) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->enable_schema = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withHostnameResolution) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->enable_hostname_resolution = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withRandomizedContactPoints) { zend_bool enabled = 1; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enabled) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); self->enable_randomized_contact_points = enabled; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(ClusterBuilder, withConnectionHeartbeatInterval) { zval *interval = NULL; php_driver_cluster_builder *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &interval) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER_BUILDER(getThis()); if (Z_TYPE_P(interval) == IS_LONG && Z_LVAL_P(interval) >= 0) { self->connection_heartbeat_interval = Z_LVAL_P(interval); } else if (Z_TYPE_P(interval) == IS_DOUBLE && Z_DVAL_P(interval) >= 0) { self->connection_heartbeat_interval = ceil(Z_DVAL_P(interval)); } else { INVALID_ARGUMENT(interval, "a positive number (or 0 to disable)"); } RETURN_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_consistency, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, consistency) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_page_size, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, pageSize) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_contact_points, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, host) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_port, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, port) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_dc_aware, 0, ZEND_RETURN_VALUE, 3) ZEND_ARG_INFO(0, localDatacenter) ZEND_ARG_INFO(0, hostPerRemoteDatacenter) ZEND_ARG_INFO(0, useRemoteDatacenterForLocalConsistencies) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_blacklist_nodes, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, hosts) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_whitelist_nodes, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, hosts) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_blacklist_dcs, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, dcs) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_whitelist_dcs, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, dcs) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_enabled, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, enabled) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_credentials, 0, ZEND_RETURN_VALUE, 2) ZEND_ARG_INFO(0, username) ZEND_ARG_INFO(0, password) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_ssl, 0, ZEND_RETURN_VALUE, 1) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, options, SSLOptions, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_version, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, version) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_count, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, count) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_connections, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, core) ZEND_ARG_INFO(0, max) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_interval, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, interval) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_delay, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, delay) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_retry_policy, 0, ZEND_RETURN_VALUE, 1) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, policy, RetryPolicy, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_timestamp_gen, 0, ZEND_RETURN_VALUE, 1) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, generator, TimestampGenerator, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_cluster_builder_methods[] = { PHP_ME(ClusterBuilder, build, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withDefaultConsistency, arginfo_consistency, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withDefaultPageSize, arginfo_page_size, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withDefaultTimeout, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withContactPoints, arginfo_contact_points, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withPort, arginfo_port, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withRoundRobinLoadBalancingPolicy, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withDatacenterAwareRoundRobinLoadBalancingPolicy, arginfo_dc_aware, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withBlackListHosts, arginfo_blacklist_nodes, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withWhiteListHosts, arginfo_whitelist_nodes, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withBlackListDCs, arginfo_blacklist_dcs, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withWhiteListDCs, arginfo_whitelist_dcs, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withTokenAwareRouting, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withCredentials, arginfo_credentials, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withConnectTimeout, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withRequestTimeout, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withSSL, arginfo_ssl, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withPersistentSessions, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withProtocolVersion, arginfo_version, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withIOThreads, arginfo_count, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withConnectionsPerHost, arginfo_connections, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withReconnectInterval, arginfo_interval, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withLatencyAwareRouting, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withTCPNodelay, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withTCPKeepalive, arginfo_delay, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withRetryPolicy, arginfo_retry_policy, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withTimestampGenerator, arginfo_timestamp_gen, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withSchemaMetadata, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withHostnameResolution, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withRandomizedContactPoints, arginfo_enabled, ZEND_ACC_PUBLIC) PHP_ME(ClusterBuilder, withConnectionHeartbeatInterval, arginfo_interval, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_cluster_builder_handlers; static HashTable* php_driver_cluster_builder_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable* php_driver_cluster_builder_properties(zval *object TSRMLS_DC) { php5to7_zval contactPoints; php5to7_zval loadBalancingPolicy; php5to7_zval localDatacenter; php5to7_zval hostPerRemoteDatacenter; php5to7_zval useRemoteDatacenterForLocalConsistencies; php5to7_zval useTokenAwareRouting; php5to7_zval username; php5to7_zval password; php5to7_zval connectTimeout; php5to7_zval requestTimeout; php5to7_zval sslOptions; php5to7_zval defaultConsistency; php5to7_zval defaultPageSize; php5to7_zval defaultTimeout; php5to7_zval usePersistentSessions; php5to7_zval protocolVersion; php5to7_zval ioThreads; php5to7_zval coreConnectionPerHost; php5to7_zval maxConnectionsPerHost; php5to7_zval reconnectInterval; php5to7_zval latencyAwareRouting; php5to7_zval tcpNodelay; php5to7_zval tcpKeepalive; php5to7_zval retryPolicy; php5to7_zval blacklistHosts; php5to7_zval whitelistHosts; php5to7_zval blacklistDCs; php5to7_zval whitelistDCs; php5to7_zval timestampGen; php5to7_zval schemaMetadata; php5to7_zval hostnameResolution; php5to7_zval randomizedContactPoints; php5to7_zval connectionHeartbeatInterval; php_driver_cluster_builder *self = PHP_DRIVER_GET_CLUSTER_BUILDER(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZVAL_MAYBE_MAKE(contactPoints); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(contactPoints), self->contact_points); PHP5TO7_ZVAL_MAYBE_MAKE(loadBalancingPolicy); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(loadBalancingPolicy), self->load_balancing_policy); PHP5TO7_ZVAL_MAYBE_MAKE(localDatacenter); PHP5TO7_ZVAL_MAYBE_MAKE(hostPerRemoteDatacenter); PHP5TO7_ZVAL_MAYBE_MAKE(useRemoteDatacenterForLocalConsistencies); if (self->load_balancing_policy == LOAD_BALANCING_DC_AWARE_ROUND_ROBIN) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(localDatacenter), self->local_dc); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(hostPerRemoteDatacenter), self->used_hosts_per_remote_dc); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(useRemoteDatacenterForLocalConsistencies), self->allow_remote_dcs_for_local_cl); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(localDatacenter)); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(hostPerRemoteDatacenter)); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(useRemoteDatacenterForLocalConsistencies)); } PHP5TO7_ZVAL_MAYBE_MAKE(useTokenAwareRouting); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(useTokenAwareRouting), self->use_token_aware_routing); PHP5TO7_ZVAL_MAYBE_MAKE(username); PHP5TO7_ZVAL_MAYBE_MAKE(password); if (self->username) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(username), self->username); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(password), self->password); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(username)); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(password)); } PHP5TO7_ZVAL_MAYBE_MAKE(connectTimeout); ZVAL_DOUBLE(PHP5TO7_ZVAL_MAYBE_P(connectTimeout), (double) self->connect_timeout / 1000); PHP5TO7_ZVAL_MAYBE_MAKE(requestTimeout); ZVAL_DOUBLE(PHP5TO7_ZVAL_MAYBE_P(requestTimeout), (double) self->request_timeout / 1000); PHP5TO7_ZVAL_MAYBE_MAKE(sslOptions); if (!PHP5TO7_ZVAL_IS_UNDEF(self->ssl_options)) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(sslOptions), PHP5TO7_ZVAL_MAYBE_P(self->ssl_options)); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(sslOptions)); } PHP5TO7_ZVAL_MAYBE_MAKE(defaultConsistency); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(defaultConsistency), self->default_consistency); PHP5TO7_ZVAL_MAYBE_MAKE(defaultPageSize); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(defaultPageSize), self->default_page_size); PHP5TO7_ZVAL_MAYBE_MAKE(defaultTimeout); if (!PHP5TO7_ZVAL_IS_UNDEF(self->default_timeout)) { ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(defaultTimeout), PHP5TO7_Z_LVAL_MAYBE_P(self->default_timeout)); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(defaultTimeout)); } PHP5TO7_ZVAL_MAYBE_MAKE(usePersistentSessions); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(usePersistentSessions), self->persist); PHP5TO7_ZVAL_MAYBE_MAKE(protocolVersion); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(protocolVersion), self->protocol_version); PHP5TO7_ZVAL_MAYBE_MAKE(ioThreads); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(ioThreads), self->io_threads); PHP5TO7_ZVAL_MAYBE_MAKE(coreConnectionPerHost); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(coreConnectionPerHost), self->core_connections_per_host); PHP5TO7_ZVAL_MAYBE_MAKE(maxConnectionsPerHost); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(maxConnectionsPerHost), self->max_connections_per_host); PHP5TO7_ZVAL_MAYBE_MAKE(reconnectInterval); ZVAL_DOUBLE(PHP5TO7_ZVAL_MAYBE_P(reconnectInterval), (double) self->reconnect_interval / 1000); PHP5TO7_ZVAL_MAYBE_MAKE(latencyAwareRouting); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(latencyAwareRouting), self->enable_latency_aware_routing); PHP5TO7_ZVAL_MAYBE_MAKE(tcpNodelay); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(tcpNodelay), self->enable_tcp_nodelay); PHP5TO7_ZVAL_MAYBE_MAKE(tcpKeepalive); if (self->enable_tcp_keepalive) { ZVAL_DOUBLE(PHP5TO7_ZVAL_MAYBE_P(tcpKeepalive), (double) self->tcp_keepalive_delay / 1000); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(tcpKeepalive)); } PHP5TO7_ZVAL_MAYBE_MAKE(retryPolicy); if (!PHP5TO7_ZVAL_IS_UNDEF(self->retry_policy)) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(retryPolicy), PHP5TO7_ZVAL_MAYBE_P(self->retry_policy)); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(retryPolicy)); } PHP5TO7_ZVAL_MAYBE_MAKE(blacklistHosts); if (self->blacklist_hosts) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(blacklistHosts), self->blacklist_hosts); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(blacklistHosts)); } PHP5TO7_ZVAL_MAYBE_MAKE(whitelistHosts); if (self->whitelist_hosts) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(whitelistHosts), self->whitelist_hosts); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(whitelistHosts)); } PHP5TO7_ZVAL_MAYBE_MAKE(blacklistDCs); if (self->blacklist_dcs) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(blacklistDCs), self->blacklist_dcs); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(blacklistDCs)); } PHP5TO7_ZVAL_MAYBE_MAKE(whitelistDCs); if (self->whitelist_dcs) { PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(whitelistDCs), self->whitelist_dcs); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(whitelistDCs)); } PHP5TO7_ZVAL_MAYBE_MAKE(timestampGen); if (!PHP5TO7_ZVAL_IS_UNDEF(self->timestamp_gen)) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(timestampGen), PHP5TO7_ZVAL_MAYBE_P(self->timestamp_gen)); } else { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(timestampGen)); } PHP5TO7_ZVAL_MAYBE_MAKE(schemaMetadata); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(schemaMetadata), self->enable_schema); PHP5TO7_ZVAL_MAYBE_MAKE(hostnameResolution); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(hostnameResolution), self->enable_hostname_resolution); PHP5TO7_ZVAL_MAYBE_MAKE(randomizedContactPoints); ZVAL_BOOL(PHP5TO7_ZVAL_MAYBE_P(randomizedContactPoints), self->enable_randomized_contact_points); PHP5TO7_ZVAL_MAYBE_MAKE(connectionHeartbeatInterval); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(connectionHeartbeatInterval), self->connection_heartbeat_interval); PHP5TO7_ZEND_HASH_UPDATE(props, "contactPoints", sizeof("contactPoints"), PHP5TO7_ZVAL_MAYBE_P(contactPoints), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "loadBalancingPolicy", sizeof("loadBalancingPolicy"), PHP5TO7_ZVAL_MAYBE_P(loadBalancingPolicy), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "localDatacenter", sizeof("localDatacenter"), PHP5TO7_ZVAL_MAYBE_P(localDatacenter), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "hostPerRemoteDatacenter", sizeof("hostPerRemoteDatacenter"), PHP5TO7_ZVAL_MAYBE_P(hostPerRemoteDatacenter), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "useRemoteDatacenterForLocalConsistencies", sizeof("useRemoteDatacenterForLocalConsistencies"), PHP5TO7_ZVAL_MAYBE_P(useRemoteDatacenterForLocalConsistencies), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "useTokenAwareRouting", sizeof("useTokenAwareRouting"), PHP5TO7_ZVAL_MAYBE_P(useTokenAwareRouting), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "username", sizeof("username"), PHP5TO7_ZVAL_MAYBE_P(username), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "password", sizeof("password"), PHP5TO7_ZVAL_MAYBE_P(password), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "connectTimeout", sizeof("connectTimeout"), PHP5TO7_ZVAL_MAYBE_P(connectTimeout), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "requestTimeout", sizeof("requestTimeout"), PHP5TO7_ZVAL_MAYBE_P(requestTimeout), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "sslOptions", sizeof("sslOptions"), PHP5TO7_ZVAL_MAYBE_P(sslOptions), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "defaultConsistency", sizeof("defaultConsistency"), PHP5TO7_ZVAL_MAYBE_P(defaultConsistency), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "defaultPageSize", sizeof("defaultPageSize"), PHP5TO7_ZVAL_MAYBE_P(defaultPageSize), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "defaultTimeout", sizeof("defaultTimeout"), PHP5TO7_ZVAL_MAYBE_P(defaultTimeout), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "usePersistentSessions", sizeof("usePersistentSessions"), PHP5TO7_ZVAL_MAYBE_P(usePersistentSessions), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "protocolVersion", sizeof("protocolVersion"), PHP5TO7_ZVAL_MAYBE_P(protocolVersion), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "ioThreads", sizeof("ioThreads"), PHP5TO7_ZVAL_MAYBE_P(ioThreads), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "coreConnectionPerHost", sizeof("coreConnectionPerHost"), PHP5TO7_ZVAL_MAYBE_P(coreConnectionPerHost), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "maxConnectionsPerHost", sizeof("maxConnectionsPerHost"), PHP5TO7_ZVAL_MAYBE_P(maxConnectionsPerHost), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "reconnectInterval", sizeof("reconnectInterval"), PHP5TO7_ZVAL_MAYBE_P(reconnectInterval), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "latencyAwareRouting", sizeof("latencyAwareRouting"), PHP5TO7_ZVAL_MAYBE_P(latencyAwareRouting), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "tcpNodelay", sizeof("tcpNodelay"), PHP5TO7_ZVAL_MAYBE_P(tcpNodelay), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "tcpKeepalive", sizeof("tcpKeepalive"), PHP5TO7_ZVAL_MAYBE_P(tcpKeepalive), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "retryPolicy", sizeof("retryPolicy"), PHP5TO7_ZVAL_MAYBE_P(retryPolicy), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "timestampGenerator", sizeof("timestampGenerator"), PHP5TO7_ZVAL_MAYBE_P(timestampGen), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "schemaMetadata", sizeof("schemaMetadata"), PHP5TO7_ZVAL_MAYBE_P(schemaMetadata), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "blacklist_hosts", sizeof("blacklist_hosts"), PHP5TO7_ZVAL_MAYBE_P(blacklistHosts), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "whitelist_hosts", sizeof("whitelist_hosts"), PHP5TO7_ZVAL_MAYBE_P(whitelistHosts), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "blacklist_dcs", sizeof("blacklist_dcs"), PHP5TO7_ZVAL_MAYBE_P(blacklistDCs), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "whitelist_dcs", sizeof("whitelist_dcs"), PHP5TO7_ZVAL_MAYBE_P(whitelistDCs), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "hostnameResolution", sizeof("hostnameResolution"), PHP5TO7_ZVAL_MAYBE_P(hostnameResolution), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "randomizedContactPoints", sizeof("randomizedContactPoints"), PHP5TO7_ZVAL_MAYBE_P(randomizedContactPoints), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "connectionHeartbeatInterval", sizeof("connectionHeartbeatInterval"), PHP5TO7_ZVAL_MAYBE_P(connectionHeartbeatInterval), sizeof(zval)); return props; } static int php_driver_cluster_builder_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_cluster_builder_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_cluster_builder *self = PHP5TO7_ZEND_OBJECT_GET(cluster_builder, object); efree(self->contact_points); self->contact_points = NULL; if (self->local_dc) { efree(self->local_dc); self->local_dc = NULL; } if (self->username) { efree(self->username); self->username = NULL; } if (self->password) { efree(self->password); self->password = NULL; } if (self->whitelist_hosts) { efree(self->whitelist_hosts); self->whitelist_hosts = NULL; } if (self->blacklist_hosts) { efree(self->blacklist_hosts); self->blacklist_hosts = NULL; } if (self->whitelist_dcs) { efree(self->whitelist_dcs); self->whitelist_dcs = NULL; } if (self->blacklist_dcs) { efree(self->blacklist_dcs); self->whitelist_dcs = NULL; } PHP5TO7_ZVAL_MAYBE_DESTROY(self->ssl_options); PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_timeout); PHP5TO7_ZVAL_MAYBE_DESTROY(self->retry_policy); PHP5TO7_ZVAL_MAYBE_DESTROY(self->timestamp_gen); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_cluster_builder_new(zend_class_entry *ce TSRMLS_DC) { php_driver_cluster_builder *self = PHP5TO7_ZEND_OBJECT_ECALLOC(cluster_builder, ce); self->contact_points = estrdup("127.0.0.1"); self->port = 9042; self->load_balancing_policy = LOAD_BALANCING_DEFAULT; self->local_dc = NULL; self->used_hosts_per_remote_dc = 0; self->allow_remote_dcs_for_local_cl = 0; self->use_token_aware_routing = 1; self->username = NULL; self->password = NULL; self->connect_timeout = 5000; self->request_timeout = 12000; self->default_consistency = PHP_DRIVER_DEFAULT_CONSISTENCY; self->default_page_size = 5000; self->persist = 1; self->protocol_version = 4; self->io_threads = 1; self->core_connections_per_host = 1; self->max_connections_per_host = 2; self->reconnect_interval = 2000; self->enable_latency_aware_routing = 1; self->enable_tcp_nodelay = 1; self->enable_tcp_keepalive = 0; self->tcp_keepalive_delay = 0; self->enable_schema = 1; self->blacklist_hosts = NULL; self->whitelist_hosts = NULL; self->blacklist_dcs = NULL; self->whitelist_dcs = NULL; self->enable_hostname_resolution = 0; self->enable_randomized_contact_points = 1; self->connection_heartbeat_interval = 30; PHP5TO7_ZVAL_UNDEF(self->ssl_options); PHP5TO7_ZVAL_UNDEF(self->default_timeout); PHP5TO7_ZVAL_UNDEF(self->retry_policy); PHP5TO7_ZVAL_UNDEF(self->timestamp_gen); PHP5TO7_ZEND_OBJECT_INIT(cluster_builder, self, ce); } void php_driver_define_ClusterBuilder(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Cluster\\Builder", php_driver_cluster_builder_methods); php_driver_cluster_builder_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_cluster_builder_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_cluster_builder_ce->create_object = php_driver_cluster_builder_new; memcpy(&php_driver_cluster_builder_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_cluster_builder_handlers.get_properties = php_driver_cluster_builder_properties; #if PHP_VERSION_ID >= 50400 php_driver_cluster_builder_handlers.get_gc = php_driver_cluster_builder_gc; #endif php_driver_cluster_builder_handlers.compare_objects = php_driver_cluster_builder_compare; } cassandra-1.3.0/src/Collection.c0000644000076500000240000003275713062515664017205 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/collections.h" #include "util/hash.h" #include "util/types.h" #include "src/Collection.h" zend_class_entry *php_driver_collection_ce = NULL; void php_driver_collection_add(php_driver_collection *collection, zval *object TSRMLS_DC) { PHP5TO7_ZEND_HASH_NEXT_INDEX_INSERT(&collection->values, object, sizeof(zval *)); Z_TRY_ADDREF_P(object); collection->dirty = 1; } static int php_driver_collection_del(php_driver_collection *collection, ulong index) { if (zend_hash_index_del(&collection->values, index) == SUCCESS) { collection->dirty = 1; return 1; } return 0; } static int php_driver_collection_get(php_driver_collection *collection, ulong index, php5to7_zval *zvalue) { php5to7_zval *value; if (PHP5TO7_ZEND_HASH_INDEX_FIND(&collection->values, index, value)) { *zvalue = *value; return 1; } return 0; } static int php_driver_collection_find(php_driver_collection *collection, zval *object, long *index TSRMLS_DC) { php5to7_ulong num_key; php5to7_zval *current; PHP5TO7_ZEND_HASH_FOREACH_NUM_KEY_VAL(&collection->values, num_key, current) { zval compare; is_equal_function(&compare, object, PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC); if (PHP5TO7_ZVAL_IS_TRUE_P(&compare)) { *index = (long) num_key; return 1; } } PHP5TO7_ZEND_HASH_FOREACH_END(&collection->values); return 0; } static void php_driver_collection_populate(php_driver_collection *collection, zval *array) { php5to7_zval *current; PHP5TO7_ZEND_HASH_FOREACH_VAL(&collection->values, current) { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_DEREF(current)) == SUCCESS) Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_DEREF(current)); else break; } PHP5TO7_ZEND_HASH_FOREACH_END(&collection->values); } /* {{{ Collection::__construct(type) */ PHP_METHOD(Collection, __construct) { php_driver_collection *self; zval *type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &type) == FAILURE) return; self = PHP_DRIVER_GET_COLLECTION(getThis()); if (Z_TYPE_P(type) == IS_STRING) { CassValueType value_type; if (!php_driver_value_type(Z_STRVAL_P(type), &value_type TSRMLS_CC)) return; self->type = php_driver_type_set_from_value_type(value_type TSRMLS_CC); } else if (Z_TYPE_P(type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(type, "type" TSRMLS_CC)) { return; } self->type = php_driver_type_collection(type TSRMLS_CC); Z_ADDREF_P(type); } else { INVALID_ARGUMENT(type, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type"); } } /* }}} */ /* {{{ Collection::type() */ PHP_METHOD(Collection, type) { php_driver_collection *self = PHP_DRIVER_GET_COLLECTION(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } /* {{{ Collection::values() */ PHP_METHOD(Collection, values) { php_driver_collection *collection = NULL; array_init(return_value); collection = PHP_DRIVER_GET_COLLECTION(getThis()); php_driver_collection_populate(collection, return_value); } /* }}} */ /* {{{ Collection::add(mixed) */ PHP_METHOD(Collection, add) { php_driver_collection *self = NULL; php5to7_zval_args args = NULL; int argc = 0, i; php_driver_type *type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) return; self = PHP_DRIVER_GET_COLLECTION(getThis()); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); for (i = 0; i < argc; i++) { if (Z_TYPE_P(PHP5TO7_ZVAL_ARG(args[i])) == IS_NULL) { PHP5TO7_MAYBE_EFREE(args); zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid value: null is not supported inside collections"); RETURN_FALSE; } if (!php_driver_validate_object(PHP5TO7_ZVAL_ARG(args[i]), PHP5TO7_ZVAL_MAYBE_P(type->data.collection.value_type) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); RETURN_FALSE; } } for (i = 0; i < argc; i++) { php_driver_collection_add(self, PHP5TO7_ZVAL_ARG(args[i]) TSRMLS_CC); } PHP5TO7_MAYBE_EFREE(args); RETVAL_LONG(zend_hash_num_elements(&self->values)); } /* }}} */ /* {{{ Collection::get(int) */ PHP_METHOD(Collection, get) { long key; php_driver_collection *self = NULL; php5to7_zval value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &key) == FAILURE) return; self = PHP_DRIVER_GET_COLLECTION(getThis()); if (php_driver_collection_get(self, (ulong) key, &value)) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 1, 0); } /* }}} */ /* {{{ Collection::find(mixed) */ PHP_METHOD(Collection, find) { zval *object; php_driver_collection *collection = NULL; long index; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &object) == FAILURE) return; collection = PHP_DRIVER_GET_COLLECTION(getThis()); if (php_driver_collection_find(collection, object, &index TSRMLS_CC)) RETURN_LONG(index); } /* }}} */ /* {{{ Collection::count() */ PHP_METHOD(Collection, count) { php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); RETURN_LONG(zend_hash_num_elements(&collection->values)); } /* }}} */ /* {{{ Collection::current() */ PHP_METHOD(Collection, current) { php5to7_zval *current; php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); if (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA(&collection->values, current)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(current), 1, 0); } } /* }}} */ /* {{{ Collection::key() */ PHP_METHOD(Collection, key) { php5to7_ulong num_key; php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY(&collection->values, NULL, &num_key) == HASH_KEY_IS_LONG) { RETURN_LONG(num_key); } } /* }}} */ /* {{{ Collection::next() */ PHP_METHOD(Collection, next) { php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); zend_hash_move_forward(&collection->values); } /* }}} */ /* {{{ Collection::valid() */ PHP_METHOD(Collection, valid) { php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); RETURN_BOOL(zend_hash_has_more_elements(&collection->values) == SUCCESS); } /* }}} */ /* {{{ Collection::rewind() */ PHP_METHOD(Collection, rewind) { php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(getThis()); zend_hash_internal_pointer_reset(&collection->values); } /* }}} */ /* {{{ Collection::remove(key) */ PHP_METHOD(Collection, remove) { long index; php_driver_collection *collection = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) { return; } collection = PHP_DRIVER_GET_COLLECTION(getThis()); if (php_driver_collection_del(collection, (ulong) index)) { RETURN_TRUE; } RETURN_FALSE; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, type) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_index, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, index) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_collection_methods[] = { PHP_ME(Collection, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Collection, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, values, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, add, arginfo_value, ZEND_ACC_PUBLIC) PHP_ME(Collection, get, arginfo_index, ZEND_ACC_PUBLIC) PHP_ME(Collection, find, arginfo_value, ZEND_ACC_PUBLIC) /* Countable */ PHP_ME(Collection, count, arginfo_none, ZEND_ACC_PUBLIC) /* Iterator */ PHP_ME(Collection, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, rewind, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Collection, remove, arginfo_index, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_collection_handlers; static HashTable * php_driver_collection_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_collection_properties(zval *object TSRMLS_DC) { php5to7_zval values; php_driver_collection *self = PHP_DRIVER_GET_COLLECTION(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(self->type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZVAL_MAYBE_MAKE(values); array_init(PHP5TO7_ZVAL_MAYBE_P(values)); php_driver_collection_populate(self, PHP5TO7_ZVAL_MAYBE_P(values)); PHP5TO7_ZEND_HASH_UPDATE(props, "values", sizeof("values"), PHP5TO7_ZVAL_MAYBE_P(values), sizeof(zval)); return props; } static int php_driver_collection_compare(zval *obj1, zval *obj2 TSRMLS_DC) { HashPosition pos1; HashPosition pos2; php5to7_zval *current1; php5to7_zval *current2; php_driver_collection *collection1; php_driver_collection *collection2; php_driver_type *type1; php_driver_type *type2; int result; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ collection1 = PHP_DRIVER_GET_COLLECTION(obj1); collection2 = PHP_DRIVER_GET_COLLECTION(obj2); type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(collection1->type)); type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(collection2->type)); result = php_driver_type_compare(type1, type2 TSRMLS_CC); if (result != 0) return result; if (zend_hash_num_elements(&collection1->values) != zend_hash_num_elements(&collection2->values)) { return zend_hash_num_elements(&collection1->values) < zend_hash_num_elements(&collection2->values) ? -1 : 1; } zend_hash_internal_pointer_reset_ex(&collection1->values, &pos1); zend_hash_internal_pointer_reset_ex(&collection2->values, &pos2); while (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&collection1->values, current1, &pos1) && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&collection2->values, current2, &pos2)) { result = php_driver_value_compare(PHP5TO7_ZVAL_MAYBE_DEREF(current1), PHP5TO7_ZVAL_MAYBE_DEREF(current2) TSRMLS_CC); if (result != 0) return result; zend_hash_move_forward_ex(&collection1->values, &pos1); zend_hash_move_forward_ex(&collection2->values, &pos2); } return 0; } static unsigned php_driver_collection_hash_value(zval *obj TSRMLS_DC) { php5to7_zval *current; unsigned hashv = 0; php_driver_collection *self = PHP_DRIVER_GET_COLLECTION(obj); if (!self->dirty) return self->hashv; PHP5TO7_ZEND_HASH_FOREACH_VAL(&self->values, current) { hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC)); } PHP5TO7_ZEND_HASH_FOREACH_END(&self->values); self->hashv = hashv; self->dirty = 0; return hashv; } static void php_driver_collection_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_collection *self = PHP5TO7_ZEND_OBJECT_GET(collection, object); zend_hash_destroy(&self->values); PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_collection_new(zend_class_entry *ce TSRMLS_DC) { php_driver_collection *self = PHP5TO7_ZEND_OBJECT_ECALLOC(collection, ce); zend_hash_init(&self->values, 0, NULL, ZVAL_PTR_DTOR, 0); self->dirty = 1; PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT(collection, self, ce); } void php_driver_define_Collection(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Collection", php_driver_collection_methods); php_driver_collection_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_collection_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_collection_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_collection_handlers.std.get_properties = php_driver_collection_properties; #if PHP_VERSION_ID >= 50400 php_driver_collection_handlers.std.get_gc = php_driver_collection_gc; #endif php_driver_collection_handlers.std.compare_objects = php_driver_collection_compare; php_driver_collection_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_collection_ce->create_object = php_driver_collection_new; zend_class_implements(php_driver_collection_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); php_driver_collection_handlers.hash_value = php_driver_collection_hash_value; php_driver_collection_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Collection.h0000644000076500000240000000143313062515664017175 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_COLLECTION_H #define PHP_DRIVER_COLLECTION_H void php_driver_collection_add(php_driver_collection* collection, zval* object TSRMLS_DC); #endif /* PHP_DRIVER_COLLECTION_H */ cassandra-1.3.0/src/Column.c0000644000076500000240000000266413062515664016341 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_column_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_column_methods[] = { PHP_ABSTRACT_ME(Column, name, arginfo_none) PHP_ABSTRACT_ME(Column, type, arginfo_none) PHP_ABSTRACT_ME(Column, isReversed, arginfo_none) PHP_ABSTRACT_ME(Column, isStatic, arginfo_none) PHP_ABSTRACT_ME(Column, isFrozen, arginfo_none) PHP_ABSTRACT_ME(Column, indexName, arginfo_none) PHP_ABSTRACT_ME(Column, indexOptions, arginfo_none) PHP_FE_END }; void php_driver_define_Column(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Column", php_driver_column_methods); php_driver_column_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_column_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Core.c0000644000076500000240000001513213062515664015766 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_core_ce = NULL; PHP_DRIVER_CORE_METHOD(cluster) { object_init_ex(return_value, php_driver_cluster_builder_ce); } PHP_DRIVER_CORE_METHOD(ssl) { object_init_ex(return_value, php_driver_ssl_builder_ce); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry core_methods[] = { PHP_DRIVER_CORE_ME(cluster, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_DRIVER_CORE_ME(ssl, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; void php_driver_define_Core(TSRMLS_D) { zend_class_entry ce; char buf[64]; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE, core_methods); php_driver_core_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_core_ce->ce_flags |= ZEND_ACC_FINAL; zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_ANY"), CASS_CONSISTENCY_ANY TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_ONE"), CASS_CONSISTENCY_ONE TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_TWO"), CASS_CONSISTENCY_TWO TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_THREE"), CASS_CONSISTENCY_THREE TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_QUORUM"), CASS_CONSISTENCY_QUORUM TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_ALL"), CASS_CONSISTENCY_ALL TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_LOCAL_QUORUM"), CASS_CONSISTENCY_LOCAL_QUORUM TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_EACH_QUORUM"), CASS_CONSISTENCY_EACH_QUORUM TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_SERIAL"), CASS_CONSISTENCY_SERIAL TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_LOCAL_SERIAL"), CASS_CONSISTENCY_LOCAL_SERIAL TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("CONSISTENCY_LOCAL_ONE"), CASS_CONSISTENCY_LOCAL_ONE TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("VERIFY_NONE"), CASS_SSL_VERIFY_NONE TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("VERIFY_PEER_CERT"), CASS_SSL_VERIFY_PEER_CERT TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("VERIFY_PEER_IDENTITY"), CASS_SSL_VERIFY_PEER_IDENTITY TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("BATCH_LOGGED"), CASS_BATCH_TYPE_LOGGED TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("BATCH_UNLOGGED"), CASS_BATCH_TYPE_UNLOGGED TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("BATCH_COUNTER"), CASS_BATCH_TYPE_COUNTER TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_DISABLED"), CASS_LOG_DISABLED TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_CRITICAL"), CASS_LOG_CRITICAL TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_ERROR"), CASS_LOG_ERROR TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_WARN"), CASS_LOG_WARN TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_INFO"), CASS_LOG_INFO TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_DEBUG"), CASS_LOG_DEBUG TSRMLS_CC); zend_declare_class_constant_long(php_driver_core_ce, ZEND_STRL("LOG_TRACE"), CASS_LOG_TRACE TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_TEXT"), "text" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_ASCII"), "ascii" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_VARCHAR"), "varchar" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_BIGINT"), "bigint" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_SMALLINT"), "smallint" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_TINYINT"), "tinyint" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_BLOB"), "blob" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_BOOLEAN"), "boolean" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_COUNTER"), "counter" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_DECIMAL"), "decimal" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_DOUBLE"), "double" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_FLOAT"), "float" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_INT"), "int" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_TIMESTAMP"), "timestamp" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_UUID"), "uuid" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_VARINT"), "varint" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_TIMEUUID"), "timeuuid" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("TYPE_INET"), "inet" TSRMLS_CC); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("VERSION"), PHP_DRIVER_VERSION_FULL TSRMLS_CC); snprintf(buf, sizeof(buf), "%d.%d.%d%s", CASS_VERSION_MAJOR, CASS_VERSION_MINOR, CASS_VERSION_PATCH, strlen(CASS_VERSION_SUFFIX) > 0 ? "-" CASS_VERSION_SUFFIX : ""); zend_declare_class_constant_string(php_driver_core_ce, ZEND_STRL("CPP_DRIVER_VERSION"), buf TSRMLS_CC); } cassandra-1.3.0/src/Custom.c0000644000076500000240000000216213062515664016347 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry* php_driver_custom_ce = NULL; static zend_function_entry php_driver_custom_methods[] = { PHP_FE_END }; void php_driver_define_Custom(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Custom", php_driver_custom_methods); php_driver_custom_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_custom_ce TSRMLS_CC, 1, php_driver_value_ce); php_driver_custom_ce->ce_flags |= ZEND_ACC_ABSTRACT; } cassandra-1.3.0/src/Date.c0000644000076500000240000001736313062515664015763 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/types.h" #include #include zend_class_entry *php_driver_date_ce = NULL; void php_driver_date_init(INTERNAL_FUNCTION_PARAMETERS) { zval *seconds = NULL; php_driver_date *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &seconds) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_date_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_DATE(getThis()); } else { object_init_ex(return_value, php_driver_date_ce); self = PHP_DRIVER_GET_DATE(return_value); } if (seconds == NULL) { self->date = cass_date_from_epoch(time(NULL)); } else { if (Z_TYPE_P(seconds) != IS_LONG) { INVALID_ARGUMENT(seconds, "a number of seconds since the Unix Epoch"); } self->date = cass_date_from_epoch(Z_LVAL_P(seconds)); } } /* {{{ Date::__construct(string) */ PHP_METHOD(Date, __construct) { php_driver_date_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Date::type() */ PHP_METHOD(Date, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_DATE TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Date::seconds() */ PHP_METHOD(Date, seconds) { php_driver_date *self = PHP_DRIVER_GET_DATE(getThis()); RETURN_LONG(cass_date_time_to_epoch(self->date, 0)); } /* }}} */ /* {{{ Date::toDateTime() */ PHP_METHOD(Date, toDateTime) { php_driver_date *self; zval *ztime = NULL; php_driver_time* time_obj = NULL; php5to7_zval datetime; php_date_obj *datetime_obj = NULL; char *str; int str_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &ztime) == FAILURE) { return; } if (ztime != NULL) { time_obj = PHP_DRIVER_GET_TIME(ztime); } self = PHP_DRIVER_GET_DATE(getThis()); PHP5TO7_ZVAL_MAYBE_MAKE(datetime); php_date_instantiate(php_date_get_date_ce(), PHP5TO7_ZVAL_MAYBE_P(datetime) TSRMLS_CC); #if PHP_MAJOR_VERSION >= 7 datetime_obj = php_date_obj_from_obj(Z_OBJ(datetime)); #else datetime_obj = zend_object_store_get_object(datetime TSRMLS_CC); #endif str_len = spprintf(&str, 0, "%lld", cass_date_time_to_epoch(self->date, time_obj != NULL ? time_obj->time : 0)); php_date_initialize(datetime_obj, str, str_len, "U", NULL, 0 TSRMLS_CC); efree(str); RETVAL_ZVAL(PHP5TO7_ZVAL_MAYBE_P(datetime), 0, 1); } /* }}} */ /* {{{ Date::fromDateTime() */ PHP_METHOD(Date, fromDateTime) { php_driver_date *self; zval *zdatetime; php5to7_zval retval; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdatetime) == FAILURE) { return; } zend_call_method_with_0_params(PHP5TO7_ZVAL_MAYBE_ADDR_OF(zdatetime), php_date_get_date_ce(), NULL, "gettimestamp", &retval); if (!PHP5TO7_ZVAL_IS_UNDEF(retval) && Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_P(retval)) == IS_LONG) { object_init_ex(return_value, php_driver_date_ce); self = PHP_DRIVER_GET_DATE(return_value); self->date = cass_date_from_epoch(PHP5TO7_Z_LVAL_MAYBE_P(retval)); zval_ptr_dtor(&retval); return; } } /* }}} */ /* {{{ Date::__toString() */ PHP_METHOD(Date, __toString) { php_driver_date *self; char *ret = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_DATE(getThis()); spprintf(&ret, 0, PHP_DRIVER_NAMESPACE "\\Date(seconds=%lld)", cass_date_time_to_epoch(self->date, 0)); PHP5TO7_RETVAL_STRING(ret); efree(ret); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, seconds) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_time, 0, ZEND_RETURN_VALUE, 0) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, time, Time, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_datetime, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_OBJ_INFO(0, datetime, DateTime, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_date_methods[] = { PHP_ME(Date, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Date, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Date, seconds, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Date, toDateTime, arginfo_time, ZEND_ACC_PUBLIC) PHP_ME(Date, fromDateTime, arginfo_datetime, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Date, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_date_handlers; static HashTable * php_driver_date_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_date_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval seconds; php_driver_date *self = PHP_DRIVER_GET_DATE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_DATE TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(seconds); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(seconds), cass_date_time_to_epoch(self->date, 0)); PHP5TO7_ZEND_HASH_UPDATE(props, "seconds", sizeof("seconds"), PHP5TO7_ZVAL_MAYBE_P(seconds), sizeof(zval)); return props; } static int php_driver_date_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_date *date1 = NULL; php_driver_date *date2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ date1 = PHP_DRIVER_GET_DATE(obj1); date2 = PHP_DRIVER_GET_DATE(obj2); return PHP_DRIVER_COMPARE(date1->date, date2->date); } static unsigned php_driver_date_hash_value(zval *obj TSRMLS_DC) { php_driver_date *self = PHP_DRIVER_GET_DATE(obj); return 31 * 17 + self->date; } static void php_driver_date_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_date *self = PHP5TO7_ZEND_OBJECT_GET(date, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_date_new(zend_class_entry *ce TSRMLS_DC) { php_driver_date *self = PHP5TO7_ZEND_OBJECT_ECALLOC(date, ce); self->date = 0; PHP5TO7_ZEND_OBJECT_INIT(date, self, ce); } void php_driver_define_Date(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Date", php_driver_date_methods); php_driver_date_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_date_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_date_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_date_handlers.std.get_properties = php_driver_date_properties; #if PHP_VERSION_ID >= 50400 php_driver_date_handlers.std.get_gc = php_driver_date_gc; #endif php_driver_date_handlers.std.compare_objects = php_driver_date_compare; php_driver_date_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_date_ce->create_object = php_driver_date_new; php_driver_date_handlers.hash_value = php_driver_date_hash_value; } cassandra-1.3.0/src/Date.h0000644000076500000240000000134713062515664015763 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DATE_H #define PHP_DRIVER_DATE_H void php_driver_date_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_DATE_H */ cassandra-1.3.0/src/Decimal.c0000644000076500000240000004527313062515664016445 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #include #include #include #include zend_class_entry *php_driver_decimal_ce = NULL; static void to_mpf(mpf_t result, php_driver_numeric *decimal) { mpf_t scale_factor; long scale; /* result = unscaled * pow(10, -scale) */ mpf_set_z(result, decimal->data.decimal.value); scale = decimal->data.decimal.scale; mpf_init_set_si(scale_factor, 10); mpf_pow_ui(scale_factor, scale_factor, scale < 0 ? -scale : scale); if (scale > 0) { mpf_ui_div(scale_factor, 1, scale_factor); } mpf_mul(result, result, scale_factor); mpf_clear(scale_factor); } /* * IEEE 754 double precision floating point representation: * * S EEEEEEEEEEE MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM * [63][ 62 - 52 ][ 51 - 0 ] * * S = sign bit * E = exponent * M = mantissa */ #define DOUBLE_MANTISSA_BITS 52 #define DOUBLE_MANTISSA_MASK (cass_int64_t) ((1LL << DOUBLE_MANTISSA_BITS) - 1) #define DOUBLE_EXPONENT_BITS 11 #define DOUBLE_EXPONENT_MASK (cass_int64_t) ((1LL << DOUBLE_EXPONENT_BITS) - 1) static void from_double(php_driver_numeric *result, double value) { int denormal; char mantissa_str[32]; cass_int64_t raw, mantissa, exponent; // Copy the bits of value into an int64 so that we can do bit manipulations on it. memcpy(&raw, &value, 8); mantissa = raw & DOUBLE_MANTISSA_MASK; exponent = (raw >> DOUBLE_MANTISSA_BITS) & DOUBLE_EXPONENT_MASK; /* This exponent is offset using 1023 unless it's a denormal value then its value * is the minimum value -1022 */ if (exponent == 0) { /* If the exponent is a zero then we have a denormal (subnormal) number. These are numbers * that represent small values around 0.0. The mantissa has the form of 0.xxxxxxxx... * * http://en.wikipedia.org/wiki/Denormal_number */ denormal = 1; exponent = -1022; } else { /* Normal number The mantissa has the form of 1.xxxxxxx... */ denormal = 0; exponent -= 1023; } /* Move the factional parts in the mantissa to the exponent. The significand * represents fractional parts: * * S = 1 + B51 * 2^-51 + B50 * 2^-52 ... + B0 * */ exponent -= DOUBLE_MANTISSA_BITS; if (!denormal) { /* Normal numbers have an implied one i.e. 1.xxxxxx... */ mantissa |= (1LL << DOUBLE_MANTISSA_BITS); } /* Remove trailing zeros and move them to the exponent */ while (exponent < 0 && (mantissa & 1) == 0) { ++exponent; mantissa >>= 1; } /* There isn't any "long long" setter method */ #ifdef _WIN32 sprintf(mantissa_str, "%I64d", mantissa); #else sprintf(mantissa_str, "%lld", mantissa); #endif mpz_set_str(result->data.decimal.value, mantissa_str, 10); /* Change the sign if negative */ if (raw < 0) { mpz_neg(result->data.decimal.value, result->data.decimal.value); } if (exponent < 0) { /* Convert from pow(2, exponent) to pow(10, exponent): * * mantissa * pow(2, exponent) equals * mantissa * (pow(10, exponent) / pow(5, exponent)) */ mpz_t pow_5; mpz_init(pow_5); mpz_ui_pow_ui(pow_5, 5, -exponent); mpz_mul(result->data.decimal.value, result->data.decimal.value, pow_5); mpz_clear(pow_5); result->data.decimal.scale = -exponent; } else { mpz_mul_2exp(result->data.decimal.value, result->data.decimal.value, exponent); result->data.decimal.scale = 0; } } static int to_double(zval* result, php_driver_numeric *decimal TSRMLS_DC) { mpf_t value; mpf_init(value); to_mpf(value, decimal); if (mpf_cmp_d(value, -DBL_MAX) < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small"); mpf_clear(value); return FAILURE; } if (mpf_cmp_d(value, DBL_MAX) > 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too big"); mpf_clear(value); return FAILURE; } ZVAL_DOUBLE(result, mpf_get_d(value)); mpf_clear(value); return SUCCESS; } static int to_long(zval* result, php_driver_numeric *decimal TSRMLS_DC) { mpf_t value; mpf_init(value); to_mpf(value, decimal); if (mpf_cmp_si(value, LONG_MIN) < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small"); mpf_clear(value); return FAILURE; } if (mpf_cmp_si(value, LONG_MAX) > 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too big"); mpf_clear(value); return FAILURE; } ZVAL_LONG(result, mpf_get_si(value)); mpf_clear(value); return SUCCESS; } static int to_string(zval* result, php_driver_numeric *decimal TSRMLS_DC) { char* string; int string_len; php_driver_format_decimal(decimal->data.decimal.value, decimal->data.decimal.scale, &string, &string_len); PHP5TO7_ZVAL_STRINGL(result, string, string_len); efree(string); return SUCCESS; } static void align_decimals(php_driver_numeric *lhs, php_driver_numeric *rhs) { mpz_t pow_10; mpz_init(pow_10); if (lhs->data.decimal.scale < rhs->data.decimal.scale) { mpz_ui_pow_ui(pow_10, 10, rhs->data.decimal.scale - lhs->data.decimal.scale); mpz_mul(lhs->data.decimal.value, lhs->data.decimal.value, pow_10); } else if (lhs->data.decimal.scale > rhs->data.decimal.scale) { mpz_ui_pow_ui(pow_10, 10, lhs->data.decimal.scale - rhs->data.decimal.scale); mpz_mul(rhs->data.decimal.value, rhs->data.decimal.value, pow_10); } mpz_clear(pow_10); } void php_driver_decimal_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_numeric *self; zval* value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_decimal_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_decimal_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(value) == IS_LONG) { mpz_set_si(self->data.decimal.value, Z_LVAL_P(value)); self->data.decimal.scale = 0; } else if (Z_TYPE_P(value) == IS_DOUBLE) { double val = Z_DVAL_P(value); if (zend_isnan(val) || zend_isinf(val)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Value of NaN or +/- infinity is not supported"); return; } from_double(self, val); } else if (Z_TYPE_P(value) == IS_STRING) { if (!php_driver_parse_decimal(Z_STRVAL_P(value), Z_STRLEN_P(value), &self->data.decimal.value, &self->data.decimal.scale TSRMLS_CC)) { return; } } else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(value); mpz_set(self->data.decimal.value, decimal->data.decimal.value); self->data.decimal.scale = decimal->data.decimal.scale; } else { INVALID_ARGUMENT(value, "a long, a double, a numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Decimal"); } } /* {{{ Decimal::__construct(string) */ PHP_METHOD(Decimal, __construct) { php_driver_decimal_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Decimal::__toString() */ PHP_METHOD(Decimal, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Decimal::type() */ PHP_METHOD(Decimal, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_DECIMAL TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Decimal::value() */ PHP_METHOD(Decimal, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); char* string; int string_len; php_driver_format_integer(self->data.decimal.value, &string, &string_len); PHP5TO7_RETVAL_STRINGL(string, string_len); efree(string); } /* }}} */ PHP_METHOD(Decimal, scale) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_LONG(self->data.decimal.scale); } /* {{{ Decimal::add() */ PHP_METHOD(Decimal, add) { zval* num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_decimal_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); align_decimals(self, decimal); mpz_add(result->data.decimal.value, self->data.decimal.value, decimal->data.decimal.value); result->data.decimal.scale = MAX(self->data.decimal.scale, decimal->data.decimal.scale); } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Decimal"); } } /* }}} */ /* {{{ Decimal::sub() */ PHP_METHOD(Decimal, sub) { zval* num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_decimal_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); align_decimals(self, decimal); mpz_sub(result->data.decimal.value, self->data.decimal.value, decimal->data.decimal.value); result->data.decimal.scale = MAX(self->data.decimal.scale, decimal->data.decimal.scale); } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Decimal"); } } /* }}} */ /* {{{ Decimal::mul() */ PHP_METHOD(Decimal, mul) { zval* num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_decimal_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_mul(result->data.decimal.value, self->data.decimal.value, decimal->data.decimal.value); result->data.decimal.scale = self->data.decimal.scale + decimal->data.decimal.scale; } else { INVALID_ARGUMENT(num, "a " PHP_DRIVER_NAMESPACE "\\Decimal"); } } /* }}} */ /* {{{ Decimal::div() */ PHP_METHOD(Decimal, div) { /* TODO: Implementation of this a bit more difficult than anticipated. */ zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Not implemented"); } /* }}} */ /* {{{ Decimal::mod() */ PHP_METHOD(Decimal, mod) { /* TODO: We could implement a remainder method */ zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Not implemented"); } /* {{{ Decimal::abs() */ PHP_METHOD(Decimal, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_decimal_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_abs(result->data.decimal.value, self->data.decimal.value); result->data.decimal.scale = self->data.decimal.scale; } /* }}} */ /* {{{ Decimal::neg() */ PHP_METHOD(Decimal, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_decimal_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_neg(result->data.decimal.value, self->data.decimal.value); result->data.decimal.scale = self->data.decimal.scale; } /* }}} */ /* {{{ Decimal::sqrt() */ PHP_METHOD(Decimal, sqrt) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Not implemented"); #if 0 php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); mpf_t value; mpf_init(value); to_mpf(value, self); mpf_sqrt(value, value); mp_exp_t exponent; char* mantissa = mpf_get_str(NULL, &exponent, 10, 0, value); object_init_ex(return_value, php_driver_decimal_ce); php_driver_numeric *result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_set_str(result->value.decimal_value, mantissa, 10); mp_bitcnt_t prec = mpf_get_prec(value); exponent -= prec; result->value.decimal_scale = -exponent; free(mantissa); mpf_clear(value); #endif } /* }}} */ /* {{{ Decimal::toInt() */ PHP_METHOD(Decimal, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Decimal::toDouble() */ PHP_METHOD(Decimal, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_double(return_value, self TSRMLS_CC); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_decimal_methods[] = { PHP_ME(Decimal, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Decimal, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, scale, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Decimal, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Decimal, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Decimal, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Decimal, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Decimal, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Decimal, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_decimal_handlers; static HashTable* php_driver_decimal_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable* php_driver_decimal_properties(zval *object TSRMLS_DC) { char* string; int string_len; php5to7_zval type; php5to7_zval value; php5to7_zval scale; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_DECIMAL TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); php_driver_format_integer(self->data.decimal.value, &string, &string_len); PHP5TO7_ZVAL_MAYBE_MAKE(PHP5TO7_ZVAL_MAYBE_P(value)); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(value), string, string_len); efree(string); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(scale); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(scale), self->data.decimal.scale); PHP5TO7_ZEND_HASH_UPDATE(props, "scale", sizeof("scale"), PHP5TO7_ZVAL_MAYBE_P(scale), sizeof(zval)); return props; } static int php_driver_decimal_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_numeric *decimal1 = NULL; php_driver_numeric *decimal2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ decimal1 = PHP_DRIVER_GET_NUMERIC(obj1); decimal2 = PHP_DRIVER_GET_NUMERIC(obj2); if (decimal1->data.decimal.scale == decimal2->data.decimal.scale) { return mpz_cmp(decimal1->data.decimal.value, decimal2->data.decimal.value); } else if (decimal1->data.decimal.scale < decimal2->data.decimal.scale) { return -1; } else { return 1; } } static unsigned php_driver_decimal_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return php_driver_mpz_hash((unsigned)self->data.decimal.scale, self->data.decimal.value); } static int php_driver_decimal_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: return to_long(retval, self TSRMLS_CC); case IS_DOUBLE: return to_double(retval, self TSRMLS_CC); case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_decimal_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); mpz_clear(self->data.decimal.value); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_decimal_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); self->type = PHP_DRIVER_DECIMAL; self->data.decimal.scale = 0; mpz_init(self->data.decimal.value); PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, decimal, self, ce); } void php_driver_define_Decimal(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Decimal", php_driver_decimal_methods); php_driver_decimal_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_decimal_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_decimal_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_decimal_ce->create_object = php_driver_decimal_new; memcpy(&php_driver_decimal_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_decimal_handlers.std.get_properties = php_driver_decimal_properties; #if PHP_VERSION_ID >= 50400 php_driver_decimal_handlers.std.get_gc = php_driver_decimal_gc; #endif php_driver_decimal_handlers.std.compare_objects = php_driver_decimal_compare; php_driver_decimal_handlers.std.cast_object = php_driver_decimal_cast; php_driver_decimal_handlers.hash_value = php_driver_decimal_hash_value; php_driver_decimal_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Decimal.h0000644000076500000240000000136313062515664016442 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DECIMAL_H #define PHP_DRIVER_DECIMAL_H void php_driver_decimal_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_DECIMAL_H */ cassandra-1.3.0/src/DefaultAggregate.c0000644000076500000240000002215313062515664020272 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultFunction.h" zend_class_entry *php_driver_default_aggregate_ce = NULL; PHP_METHOD(DefaultAggregate, name) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->signature), 1, 0); } PHP_METHOD(DefaultAggregate, simpleName) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->simple_name)) { const char *name; size_t name_length; cass_aggregate_meta_name(self->meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(self->simple_name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(self->simple_name), name, name_length); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->simple_name), 1, 0); } PHP_METHOD(DefaultAggregate, argumentTypes) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->argument_types)) { size_t i, count = cass_aggregate_meta_argument_count(self->meta); PHP5TO7_ZVAL_MAYBE_MAKE(self->argument_types); array_init(PHP5TO7_ZVAL_MAYBE_P(self->argument_types)); for (i = 0; i < count; ++i) { const CassDataType* data_type = cass_aggregate_meta_argument_type(self->meta, i); if (data_type) { php5to7_zval type = php_driver_type_from_data_type(data_type TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(type)) { add_next_index_zval(PHP5TO7_ZVAL_MAYBE_P(self->argument_types), PHP5TO7_ZVAL_MAYBE_P(type)); } } } } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->argument_types), 1, 0); } PHP_METHOD(DefaultAggregate, stateFunction) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->state_function)) { const CassFunctionMeta* function = cass_aggregate_meta_state_func(self->meta); if (!function) { return; } self->state_function = php_driver_create_function(self->schema, function TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->state_function), 1, 0); } PHP_METHOD(DefaultAggregate, finalFunction) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->final_function)) { const CassFunctionMeta* function = cass_aggregate_meta_final_func(self->meta); if (!function) { return; } self->final_function = php_driver_create_function(self->schema, function TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->final_function), 1, 0); } PHP_METHOD(DefaultAggregate, initialCondition) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->initial_condition)) { const CassValue *value = cass_aggregate_meta_init_cond(self->meta); const CassDataType *data_type = NULL; if (!value) { return; } data_type = cass_value_data_type(value); if (!data_type) { return; } php_driver_value(value, data_type, &self->initial_condition TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->initial_condition), 1, 0); } PHP_METHOD(DefaultAggregate, stateType) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->state_type)) { const CassDataType* data_type = cass_aggregate_meta_state_type(self->meta); if (!data_type) { return; } self->state_type = php_driver_type_from_data_type(data_type TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->state_type), 1, 0); } PHP_METHOD(DefaultAggregate, returnType) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->return_type)) { const CassDataType* data_type = cass_aggregate_meta_return_type(self->meta); if (!data_type) { return; } self->return_type = php_driver_type_from_data_type(data_type TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->return_type), 1, 0); } PHP_METHOD(DefaultAggregate, signature) { php_driver_aggregate *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_AGGREGATE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->signature), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_aggregate_methods[] = { PHP_ME(DefaultAggregate, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, simpleName, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, argumentTypes, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, stateFunction, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, finalFunction, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, initialCondition, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, stateType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, returnType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultAggregate, signature, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_aggregate_handlers; static HashTable * php_driver_type_default_aggregate_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_aggregate_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_aggregate_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_aggregate_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_aggregate *self = PHP5TO7_ZEND_OBJECT_GET(aggregate, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->simple_name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->argument_types); PHP5TO7_ZVAL_MAYBE_DESTROY(self->state_function); PHP5TO7_ZVAL_MAYBE_DESTROY(self->final_function); PHP5TO7_ZVAL_MAYBE_DESTROY(self->initial_condition); PHP5TO7_ZVAL_MAYBE_DESTROY(self->state_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->return_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->signature); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_aggregate_new(zend_class_entry *ce TSRMLS_DC) { php_driver_aggregate *self = PHP5TO7_ZEND_OBJECT_ECALLOC(aggregate, ce); PHP5TO7_ZVAL_UNDEF(self->simple_name); PHP5TO7_ZVAL_UNDEF(self->argument_types); PHP5TO7_ZVAL_UNDEF(self->state_function); PHP5TO7_ZVAL_UNDEF(self->final_function); PHP5TO7_ZVAL_UNDEF(self->initial_condition); PHP5TO7_ZVAL_UNDEF(self->state_type); PHP5TO7_ZVAL_UNDEF(self->return_type); PHP5TO7_ZVAL_UNDEF(self->signature); self->schema = NULL; self->meta = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(aggregate, default_aggregate, self, ce); } void php_driver_define_DefaultAggregate(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultAggregate", php_driver_default_aggregate_methods); php_driver_default_aggregate_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_aggregate_ce TSRMLS_CC, 1, php_driver_aggregate_ce); php_driver_default_aggregate_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_aggregate_ce->create_object = php_driver_default_aggregate_new; memcpy(&php_driver_default_aggregate_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_aggregate_handlers.get_properties = php_driver_default_aggregate_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_aggregate_handlers.get_gc = php_driver_type_default_aggregate_gc; #endif php_driver_default_aggregate_handlers.compare_objects = php_driver_default_aggregate_compare; php_driver_default_aggregate_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultCluster.c0000644000076500000240000002207713062515664020032 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "util/future.h" #include "util/ref.h" zend_class_entry *php_driver_default_cluster_ce = NULL; static void free_session(void *session) { cass_session_free((CassSession*) session); } PHP_METHOD(DefaultCluster, connect) { char *keyspace = NULL; php5to7_size keyspace_len; zval *timeout = NULL; php_driver_cluster *self = NULL; php_driver_session *session = NULL; CassFuture *future = NULL; char *hash_key; php5to7_size hash_key_len = 0; php_driver_psession *psession; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sz", &keyspace, &keyspace_len, &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER(getThis()); object_init_ex(return_value, php_driver_default_session_ce); session = PHP_DRIVER_GET_SESSION(return_value); session->default_consistency = self->default_consistency; session->default_page_size = self->default_page_size; session->persist = self->persist; if (!PHP5TO7_ZVAL_IS_UNDEF(session->default_timeout)) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(session->default_timeout), PHP5TO7_ZVAL_MAYBE_P(self->default_timeout)); } if (session->persist) { php5to7_zend_resource_le *le; hash_key_len = spprintf(&hash_key, 0, "%s:session:%s", self->hash_key, SAFE_STR(keyspace)); if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), hash_key, hash_key_len + 1, le) && Z_RES_P(le)->type == php_le_php_driver_session()) { psession = (php_driver_psession *) Z_RES_P(le)->ptr; session->session = php_driver_add_ref(psession->session); future = psession->future; } } if (future == NULL) { php5to7_zend_resource_le resource; session->session = php_driver_new_peref(cass_session_new(), free_session, 1); if (keyspace) { future = cass_session_connect_keyspace((CassSession *) session->session->data, self->cluster, keyspace); } else { future = cass_session_connect((CassSession *) session->session->data, self->cluster); } if (session->persist) { psession = (php_driver_psession *) pecalloc(1, sizeof(php_driver_psession), 1); psession->session = php_driver_add_ref(session->session); psession->future = future; #if PHP_MAJOR_VERSION >= 7 ZVAL_NEW_PERSISTENT_RES(&resource, 0, psession, php_le_php_driver_session()); PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_sessions)++; #else resource.type = php_le_php_driver_session(); resource.ptr = psession; PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_sessions)++; #endif } } if (php_driver_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) { if (session->persist) { efree(hash_key); } else { cass_future_free(future); } return; } if (php_driver_future_is_error(future TSRMLS_CC) == FAILURE) { if (session->persist) { (void) PHP5TO7_ZEND_HASH_DEL(&EG(persistent_list), hash_key, hash_key_len + 1); efree(hash_key); } else { cass_future_free(future); } return; } if (session->persist) efree(hash_key); } PHP_METHOD(DefaultCluster, connectAsync) { char *hash_key = NULL; php5to7_size hash_key_len = 0; char *keyspace = NULL; php5to7_size keyspace_len; php_driver_cluster *self = NULL; php_driver_future_session *future = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &keyspace, &keyspace_len) == FAILURE) { return; } self = PHP_DRIVER_GET_CLUSTER(getThis()); object_init_ex(return_value, php_driver_future_session_ce); future = PHP_DRIVER_GET_FUTURE_SESSION(return_value); future->persist = self->persist; if (self->persist) { php5to7_zend_resource_le *le; hash_key_len = spprintf(&hash_key, 0, "%s:session:%s", self->hash_key, SAFE_STR(keyspace)); future->hash_key = hash_key; future->hash_key_len = hash_key_len; if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), hash_key, hash_key_len + 1, le) && Z_RES_P(le)->type == php_le_php_driver_session()) { php_driver_psession *psession = (php_driver_psession *) Z_RES_P(le)->ptr; future->session = php_driver_add_ref(psession->session); future->future = psession->future; return; } } future->session = php_driver_new_peref(cass_session_new(), free_session, 1); if (keyspace) { future->future = cass_session_connect_keyspace((CassSession *) future->session->data, self->cluster, keyspace); } else { future->future = cass_session_connect((CassSession *) future->session->data, self->cluster); } if (self->persist) { php5to7_zend_resource_le resource; php_driver_psession *psession = (php_driver_psession *) pecalloc(1, sizeof(php_driver_psession), 1); psession->session = php_driver_add_ref(future->session); psession->future = future->future; #if PHP_MAJOR_VERSION >= 7 ZVAL_NEW_PERSISTENT_RES(&resource, 0, psession, php_le_php_driver_session()); PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_sessions)++; #else resource.type = php_le_php_driver_session(); resource.ptr = psession; PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le)); PHP_DRIVER_G(persistent_sessions)++; #endif } } ZEND_BEGIN_ARG_INFO_EX(arginfo_connect, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, keyspace) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_connectAsync, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, keyspace) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_cluster_methods[] = { PHP_ME(DefaultCluster, connect, arginfo_connect, ZEND_ACC_PUBLIC) PHP_ME(DefaultCluster, connectAsync, arginfo_connectAsync, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_cluster_handlers; static HashTable * php_driver_default_cluster_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_cluster_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_cluster_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_cluster *self = PHP5TO7_ZEND_OBJECT_GET(cluster, object); if (self->persist) { efree(self->hash_key); } else { if (self->cluster) { cass_cluster_free(self->cluster); } } PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_timeout); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_cluster_new(zend_class_entry *ce TSRMLS_DC) { php_driver_cluster *self = PHP5TO7_ZEND_OBJECT_ECALLOC(cluster, ce); self->cluster = NULL; self->default_consistency = PHP_DRIVER_DEFAULT_CONSISTENCY; self->default_page_size = 5000; self->persist = 0; self->hash_key = NULL; PHP5TO7_ZVAL_UNDEF(self->default_timeout); PHP5TO7_ZEND_OBJECT_INIT_EX(cluster, default_cluster, self, ce); } void php_driver_define_DefaultCluster(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultCluster", php_driver_default_cluster_methods); php_driver_default_cluster_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_cluster_ce TSRMLS_CC, 1, php_driver_cluster_ce); php_driver_default_cluster_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_cluster_ce->create_object = php_driver_default_cluster_new; memcpy(&php_driver_default_cluster_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_cluster_handlers.get_properties = php_driver_default_cluster_properties; php_driver_default_cluster_handlers.compare_objects = php_driver_default_cluster_compare; } cassandra-1.3.0/src/DefaultColumn.c0000644000076500000240000002040013062515664017632 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/types.h" #include "util/ref.h" #include "DefaultColumn.h" zend_class_entry *php_driver_default_column_ce = NULL; php5to7_zval php_driver_create_column(php_driver_ref *schema, const CassColumnMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_column *column; const char *name; size_t name_length; const CassValue *value; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_column_ce); column = PHP_DRIVER_GET_COLUMN(PHP5TO7_ZVAL_MAYBE_P(result)); column->schema = php_driver_add_ref(schema); column->meta = meta; cass_column_meta_name(meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(column->name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(column->name), name, name_length); value = cass_column_meta_field_by_name(meta, "validator"); if (value) { const char *validator; size_t validator_length; ASSERT_SUCCESS_BLOCK(cass_value_get_string(value, &validator, &validator_length), zval_ptr_dtor(&result); PHP5TO7_ZVAL_UNDEF(result); return result; ); if (php_driver_parse_column_type(validator, validator_length, &column->reversed, &column->frozen, &column->type TSRMLS_CC) == FAILURE) { zval_ptr_dtor(&result); PHP5TO7_ZVAL_UNDEF(result); return result; } } else { const CassDataType *data_type = cass_column_meta_data_type(meta); if (data_type) { const char *clustering_order; size_t clustering_order_length; column->type = php_driver_type_from_data_type(data_type TSRMLS_CC); #if CURRENT_CPP_DRIVER_VERSION > CPP_DRIVER_VERSION(2, 2, 0) column->frozen = cass_data_type_is_frozen(data_type); #else column->frozen = 0; #endif value = cass_column_meta_field_by_name(meta, "clustering_order"); if (!value) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unable to get column field \"clustering_order\""); zval_ptr_dtor(&result); PHP5TO7_ZVAL_UNDEF(result); return result; } ASSERT_SUCCESS_BLOCK(cass_value_get_string(value, &clustering_order, &clustering_order_length), zval_ptr_dtor(&result); PHP5TO7_ZVAL_UNDEF(result); return result; ); column->reversed = strncmp(clustering_order, "desc", clustering_order_length) == 0 ? 1 : 0; } } return result; } PHP_METHOD(DefaultColumn, name) { php_driver_column *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->name), 1, 0); } PHP_METHOD(DefaultColumn, type) { php_driver_column *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->type)) { RETURN_NULL(); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } PHP_METHOD(DefaultColumn, isReversed) { php_driver_column *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); RETURN_BOOL(self->reversed); } PHP_METHOD(DefaultColumn, isStatic) { php_driver_column *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); RETURN_BOOL(cass_column_meta_type(self->meta) == CASS_COLUMN_TYPE_STATIC); } PHP_METHOD(DefaultColumn, isFrozen) { php_driver_column *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); RETURN_BOOL(self->frozen); } PHP_METHOD(DefaultColumn, indexName) { php_driver_column *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); php_driver_get_column_field(self->meta, "index_name", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } PHP_METHOD(DefaultColumn, indexOptions) { php_driver_column *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_COLUMN(getThis()); php_driver_get_column_field(self->meta, "index_options", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_column_methods[] = { PHP_ME(DefaultColumn, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultColumn, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultColumn, isReversed, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED) PHP_ME(DefaultColumn, isStatic, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultColumn, isFrozen, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultColumn, indexName, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED) PHP_ME(DefaultColumn, indexOptions, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED) PHP_FE_END }; static zend_object_handlers php_driver_default_column_handlers; static HashTable * php_driver_type_default_column_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_column_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_column_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_column_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_column *self = PHP5TO7_ZEND_OBJECT_GET(column, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_column_new(zend_class_entry *ce TSRMLS_DC) { php_driver_column *self = PHP5TO7_ZEND_OBJECT_ECALLOC(column, ce); self->reversed = 0; self->frozen = 0; self->schema = NULL; self->meta = NULL; PHP5TO7_ZVAL_UNDEF(self->name); PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT_EX(column, default_column, self, ce); } void php_driver_define_DefaultColumn(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultColumn", php_driver_default_column_methods); php_driver_default_column_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_column_ce TSRMLS_CC, 1, php_driver_column_ce); php_driver_default_column_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_column_ce->create_object = php_driver_default_column_new; memcpy(&php_driver_default_column_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_column_handlers.get_properties = php_driver_default_column_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_column_handlers.get_gc = php_driver_type_default_column_gc; #endif php_driver_default_column_handlers.compare_objects = php_driver_default_column_compare; php_driver_default_column_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultColumn.h0000644000076500000240000000154613062515664017651 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DEFAULT_COLUMN_H #define PHP_DRIVER_DEFAULT_COLUMN_H #include "php_driver.h" php5to7_zval php_driver_create_column(php_driver_ref* schema, const CassColumnMeta *meta TSRMLS_DC); #endif /* PHP_DRIVER_DEFAULT_COLUMN_H */ cassandra-1.3.0/src/DefaultFunction.c0000644000076500000240000002142513062515664020172 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultFunction.h" zend_class_entry *php_driver_default_function_ce = NULL; php5to7_zval php_driver_create_function(php_driver_ref* schema, const CassFunctionMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_function *function; const char *full_name; size_t full_name_length; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_function_ce); function = PHP_DRIVER_GET_FUNCTION(PHP5TO7_ZVAL_MAYBE_P(result)); function->schema = php_driver_add_ref(schema); function->meta = meta; cass_function_meta_full_name(function->meta, &full_name, &full_name_length); PHP5TO7_ZVAL_MAYBE_MAKE(function->signature); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(function->signature), full_name, full_name_length); return result; } PHP_METHOD(DefaultFunction, name) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->signature), 1, 0); } PHP_METHOD(DefaultFunction, simpleName) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->simple_name)) { const char *name; size_t name_length; cass_function_meta_name(self->meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(self->simple_name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(self->simple_name), name, name_length); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->simple_name), 1, 0); } PHP_METHOD(DefaultFunction, arguments) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->arguments)) { size_t i, count = cass_function_meta_argument_count(self->meta); PHP5TO7_ZVAL_MAYBE_MAKE(self->arguments); array_init(PHP5TO7_ZVAL_MAYBE_P(self->arguments)); for (i = 0; i < count; ++i) { const char *name; size_t name_length; const CassDataType* data_type; if (cass_function_meta_argument(self->meta, i, &name, &name_length, &data_type) == CASS_OK) { php5to7_zval type = php_driver_type_from_data_type(data_type TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(type)) { PHP5TO7_ADD_ASSOC_ZVAL_EX(PHP5TO7_ZVAL_MAYBE_P(self->arguments), name, name_length + 1, PHP5TO7_ZVAL_MAYBE_P(type)); } } } } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->arguments), 1, 0); } PHP_METHOD(DefaultFunction, returnType) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->return_type)) { const CassDataType* data_type = cass_function_meta_return_type(self->meta); if (!data_type) { return; } self->return_type = php_driver_type_from_data_type(data_type TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->return_type), 1, 0); } PHP_METHOD(DefaultFunction, signature) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->signature), 1, 0); } PHP_METHOD(DefaultFunction, language) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->language)) { const char *language; size_t language_length; cass_function_meta_language(self->meta, &language, &language_length); PHP5TO7_ZVAL_MAYBE_MAKE(self->language); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(self->language), language, language_length); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->language), 1, 0); } PHP_METHOD(DefaultFunction, body) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->body)) { const char *body; size_t body_length; cass_function_meta_body(self->meta, &body, &body_length); PHP5TO7_ZVAL_MAYBE_MAKE(self->body); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(self->body), body, body_length); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->body), 1, 0); } PHP_METHOD(DefaultFunction, isCalledOnNullInput) { php_driver_function *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_FUNCTION(getThis()); RETURN_BOOL((int)cass_function_meta_called_on_null_input(self->meta)); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_function_methods[] = { PHP_ME(DefaultFunction, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, simpleName, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, arguments, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, returnType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, signature, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, language, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, body, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultFunction, isCalledOnNullInput, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_function_handlers; static HashTable * php_driver_type_default_function_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_function_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_function_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_function_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_function *self = PHP5TO7_ZEND_OBJECT_GET(function, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->simple_name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->arguments); PHP5TO7_ZVAL_MAYBE_DESTROY(self->return_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->signature); PHP5TO7_ZVAL_MAYBE_DESTROY(self->language); PHP5TO7_ZVAL_MAYBE_DESTROY(self->body); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_function_new(zend_class_entry *ce TSRMLS_DC) { php_driver_function *self = PHP5TO7_ZEND_OBJECT_ECALLOC(function, ce); PHP5TO7_ZVAL_UNDEF(self->simple_name); PHP5TO7_ZVAL_UNDEF(self->arguments); PHP5TO7_ZVAL_UNDEF(self->return_type); PHP5TO7_ZVAL_UNDEF(self->signature); PHP5TO7_ZVAL_UNDEF(self->language); PHP5TO7_ZVAL_UNDEF(self->body); self->schema = NULL; self->meta = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(function, default_function, self, ce); } void php_driver_define_DefaultFunction(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultFunction", php_driver_default_function_methods); php_driver_default_function_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_function_ce TSRMLS_CC, 1, php_driver_function_ce); php_driver_default_function_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_function_ce->create_object = php_driver_default_function_new; memcpy(&php_driver_default_function_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_function_handlers.get_properties = php_driver_default_function_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_function_handlers.get_gc = php_driver_type_default_function_gc; #endif php_driver_default_function_handlers.compare_objects = php_driver_default_function_compare; php_driver_default_function_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultFunction.h0000644000076500000240000000153113062515664020173 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DEFAULT_FUNCTION_H #define PHP_DRIVER_DEFAULT_FUNCTION_H php5to7_zval php_driver_create_function(php_driver_ref* schema, const CassFunctionMeta *meta TSRMLS_DC); #endif /* PHP_DRIVER_DEFAULT_FUNCTION_H */ cassandra-1.3.0/src/DefaultIndex.c0000644000076500000240000002175413062515664017461 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultIndex.h" zend_class_entry *php_driver_default_index_ce = NULL; php5to7_zval php_driver_create_index(php_driver_ref *schema, const CassIndexMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_index *index; const char *name; size_t name_length; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_index_ce); index = PHP_DRIVER_GET_INDEX(PHP5TO7_ZVAL_MAYBE_P(result)); index->meta = meta; index->schema = php_driver_add_ref(schema); cass_index_meta_name(meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(index->name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(index->name), name, name_length); return result; } PHP_METHOD(DefaultIndex, name) { php_driver_index *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->name), 1, 0); } PHP_METHOD(DefaultIndex, target) { php_driver_index *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->target)) { const char *target; size_t target_length; cass_index_meta_target(self->meta, &target, &target_length); PHP5TO7_ZVAL_MAYBE_MAKE(self->target); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(self->target), target, target_length); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->target), 1, 0); } PHP_METHOD(DefaultIndex, kind) { php_driver_index *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->kind)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->kind); switch (cass_index_meta_type(self->meta)) { case CASS_INDEX_TYPE_KEYS: PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(self->kind), "keys"); break; case CASS_INDEX_TYPE_CUSTOM: PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(self->kind), "custom"); break; case CASS_INDEX_TYPE_COMPOSITES: PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(self->kind), "composites"); break; default: PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(self->kind), "unknown"); break; } } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->kind), 1, 0); } void php_driver_index_build_option(php_driver_index *index) { const CassValue* options; PHP5TO7_ZVAL_MAYBE_MAKE(index->options); array_init(PHP5TO7_ZVAL_MAYBE_P(index->options)); options = cass_index_meta_options(index->meta); if (options) { CassIterator* iterator = cass_iterator_from_map(options); while (cass_iterator_next(iterator)) { const char* key_str; size_t key_str_length; const char* value_str; size_t value_str_length; const CassValue* key = cass_iterator_get_map_key(iterator); const CassValue* value = cass_iterator_get_map_value(iterator); cass_value_get_string(key, &key_str, &key_str_length); cass_value_get_string(value, &value_str, &value_str_length); PHP5TO7_ADD_ASSOC_STRINGL_EX(PHP5TO7_ZVAL_MAYBE_P(index->options), key_str, key_str_length + 1, value_str, value_str_length); } } } PHP_METHOD(DefaultIndex, option) { char *name; php5to7_size name_len; php_driver_index *self; php5to7_zval* result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_index_build_option(self); } if (PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(self->options), name, name_len + 1, result)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(result), 1, 0); } RETURN_FALSE; } PHP_METHOD(DefaultIndex, options) { php_driver_index *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_index_build_option(self); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->options), 1, 0); } PHP_METHOD(DefaultIndex, className) { php_driver_index *self; php5to7_zval* result; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_index_build_option(self); } if (PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(self->options), "class_name", sizeof("class_name"), result)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(result), 1, 0); } RETURN_FALSE; } PHP_METHOD(DefaultIndex, isCustom) { php_driver_index *self; int is_custom; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_INDEX(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_index_build_option(self); } is_custom = PHP5TO7_ZEND_HASH_EXISTS(PHP5TO7_Z_ARRVAL_MAYBE_P(self->options), "class_name", sizeof("class_name")); RETURN_BOOL(is_custom); } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_index_methods[] = { PHP_ME(DefaultIndex, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, kind, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, target, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, option, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, options, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, className, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultIndex, isCustom, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_index_handlers; static HashTable * php_driver_type_default_index_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_index_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_index_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_index_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_index *self = PHP5TO7_ZEND_OBJECT_GET(index, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->kind); PHP5TO7_ZVAL_MAYBE_DESTROY(self->target); PHP5TO7_ZVAL_MAYBE_DESTROY(self->options); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_index_new(zend_class_entry *ce TSRMLS_DC) { php_driver_index *self = PHP5TO7_ZEND_OBJECT_ECALLOC(index, ce); PHP5TO7_ZVAL_UNDEF(self->name); PHP5TO7_ZVAL_UNDEF(self->kind); PHP5TO7_ZVAL_UNDEF(self->target); PHP5TO7_ZVAL_UNDEF(self->options); self->schema = NULL; self->meta = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(index, default_index, self, ce); } void php_driver_define_DefaultIndex(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultIndex", php_driver_default_index_methods); php_driver_default_index_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_index_ce TSRMLS_CC, 1, php_driver_index_ce); php_driver_default_index_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_index_ce->create_object = php_driver_default_index_new; memcpy(&php_driver_default_index_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_index_handlers.get_properties = php_driver_default_index_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_index_handlers.get_gc = php_driver_type_default_index_gc; #endif php_driver_default_index_handlers.compare_objects = php_driver_default_index_compare; php_driver_default_index_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultIndex.h0000644000076500000240000000154013062515664017455 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DEFAULT_INDEX_H #define PHP_DRIVER_DEFAULT_INDEX_H #include "php_driver.h" php5to7_zval php_driver_create_index(php_driver_ref* schema, const CassIndexMeta *meta TSRMLS_DC); #endif /* PHP_DRIVER_DEFAULT_INDEX_H */ cassandra-1.3.0/src/DefaultKeyspace.c0000644000076500000240000004303713062515664020154 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultFunction.h" #include "DefaultMaterializedView.h" #include "DefaultTable.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif zend_class_entry *php_driver_default_keyspace_ce = NULL; PHP_METHOD(DefaultKeyspace, name) { php_driver_keyspace *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); php_driver_get_keyspace_field(self->meta, "keyspace_name", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } PHP_METHOD(DefaultKeyspace, replicationClassName) { php_driver_keyspace *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); php_driver_get_keyspace_field(self->meta, "strategy_class", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } PHP_METHOD(DefaultKeyspace, replicationOptions) { php_driver_keyspace *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); php_driver_get_keyspace_field(self->meta, "strategy_options", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } PHP_METHOD(DefaultKeyspace, hasDurableWrites) { php_driver_keyspace *self; php5to7_zval value; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); php_driver_get_keyspace_field(self->meta, "durable_writes", &value TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 0, 1); } PHP_METHOD(DefaultKeyspace, table) { char *name; php5to7_size name_len; php_driver_keyspace *self; php5to7_zval ztable; const CassTableMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_KEYSPACE(getThis()); meta = cass_keyspace_meta_table_by_name_n(self->meta, name, name_len); if (meta == NULL) { RETURN_FALSE; } ztable = php_driver_create_table(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(ztable)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztable), 0, 1); } PHP_METHOD(DefaultKeyspace, tables) { php_driver_keyspace *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); iterator = cass_iterator_tables_from_keyspace_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassTableMeta *meta; php5to7_zval ztable; php_driver_table *table; meta = cass_iterator_get_table_meta(iterator); ztable = php_driver_create_table(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(ztable)) { zval_ptr_dtor(PHP5TO7_ZVAL_MAYBE_ADDR_OF(return_value)); cass_iterator_free(iterator); return; } else { table = PHP_DRIVER_GET_TABLE(PHP5TO7_ZVAL_MAYBE_P(ztable)); if (PHP5TO7_Z_TYPE_MAYBE_P(table->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(table->name), PHP5TO7_Z_STRLEN_MAYBE_P(table->name) + 1, PHP5TO7_ZVAL_MAYBE_P(ztable)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(ztable)); } } } cass_iterator_free(iterator); } PHP_METHOD(DefaultKeyspace, userType) { char *name; php5to7_size name_len; php_driver_keyspace *self; php5to7_zval ztype; const CassDataType *user_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_KEYSPACE(getThis()); user_type = cass_keyspace_meta_user_type_by_name_n(self->meta, name, name_len); if (user_type == NULL) { return; } ztype = php_driver_type_from_data_type(user_type TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } PHP_METHOD(DefaultKeyspace, userTypes) { php_driver_keyspace *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); iterator = cass_iterator_user_types_from_keyspace_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassDataType *user_type; php5to7_zval ztype; const char *type_name; size_t type_name_len; user_type = cass_iterator_get_user_type(iterator); ztype = php_driver_type_from_data_type(user_type TSRMLS_CC); cass_data_type_type_name(user_type, &type_name, &type_name_len); PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, type_name, type_name_len + 1, PHP5TO7_ZVAL_MAYBE_P(ztype)); } cass_iterator_free(iterator); } PHP_METHOD(DefaultKeyspace, materializedView) { php_driver_keyspace *self; char *name; php5to7_size name_len; php5to7_zval zview; const CassMaterializedViewMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_KEYSPACE(getThis()); meta = cass_keyspace_meta_materialized_view_by_name_n(self->meta, name, name_len); if (meta == NULL) { RETURN_FALSE; } zview = php_driver_create_materialized_view(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(zview)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(zview), 0, 1); } PHP_METHOD(DefaultKeyspace, materializedViews) { php_driver_keyspace *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); iterator = cass_iterator_materialized_views_from_keyspace_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassMaterializedViewMeta *meta; php5to7_zval zview; php_driver_materialized_view *view; meta = cass_iterator_get_materialized_view_meta(iterator); zview = php_driver_create_materialized_view(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(zview)) { zval_ptr_dtor(PHP5TO7_ZVAL_MAYBE_ADDR_OF(return_value)); cass_iterator_free(iterator); return; } else { view = PHP_DRIVER_GET_MATERIALIZED_VIEW(PHP5TO7_ZVAL_MAYBE_P(zview)); if (PHP5TO7_Z_TYPE_MAYBE_P(view->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(view->name), PHP5TO7_Z_STRLEN_MAYBE_P(view->name) + 1, PHP5TO7_ZVAL_MAYBE_P(zview)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zview)); } } } cass_iterator_free(iterator); } int php_driver_arguments_string(php5to7_zval_args args, int argc, smart_str *arguments TSRMLS_DC) { int i; for (i = 0; i < argc; ++i) { zval *argument_type = PHP5TO7_ZVAL_ARG(args[i]); if (i > 0) { smart_str_appendc_ex(arguments, ',', 0); } if (Z_TYPE_P(argument_type) == IS_STRING) { smart_str_appendl_ex(arguments, Z_STRVAL_P(argument_type), Z_STRLEN_P(argument_type), 0); } else if (Z_TYPE_P(argument_type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(argument_type), php_driver_type_ce TSRMLS_CC)) { php_driver_type *type = PHP_DRIVER_GET_TYPE(argument_type); php_driver_type_string(type, arguments TSRMLS_CC); } else { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Argument types must be either a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type"); return FAILURE; } } smart_str_0(arguments); return SUCCESS; } PHP_METHOD(DefaultKeyspace, function) { php_driver_keyspace *self; char *name; php5to7_size name_len; php5to7_zval_args args = NULL; smart_str arguments = PHP5TO7_SMART_STR_INIT; int argc = 0; const CassFunctionMeta *meta = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|*", &name, &name_len, &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_KEYSPACE(getThis()); if (argc > 0) { if (php_driver_arguments_string(args, argc, &arguments TSRMLS_CC) == FAILURE) { PHP5TO7_MAYBE_EFREE(args); return; } } meta = cass_keyspace_meta_function_by_name_n(self->meta, name, name_len, PHP5TO7_SMART_STR_VAL(arguments), PHP5TO7_SMART_STR_LEN(arguments)); if (meta) { php5to7_zval zfunction = php_driver_create_function(self->schema, meta TSRMLS_CC); RETVAL_ZVAL(PHP5TO7_ZVAL_MAYBE_P(zfunction), 1, 1); } else { RETVAL_FALSE; } smart_str_free(&arguments); PHP5TO7_MAYBE_EFREE(args); } PHP_METHOD(DefaultKeyspace, functions) { php_driver_keyspace *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); iterator = cass_iterator_functions_from_keyspace_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassFunctionMeta *meta = cass_iterator_get_function_meta(iterator); php5to7_zval zfunction = php_driver_create_function(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zfunction)) { php_driver_function *function = PHP_DRIVER_GET_FUNCTION(PHP5TO7_ZVAL_MAYBE_P(zfunction)); if (PHP5TO7_Z_TYPE_MAYBE_P(function->signature) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(function->signature), PHP5TO7_Z_STRLEN_MAYBE_P(function->signature) + 1, PHP5TO7_ZVAL_MAYBE_P(zfunction)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zfunction)); } } } cass_iterator_free(iterator); } static php5to7_zval php_driver_create_aggregate(php_driver_ref* schema, const CassAggregateMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_aggregate *aggregate; const char *full_name; size_t full_name_length; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_aggregate_ce); aggregate = PHP_DRIVER_GET_AGGREGATE(PHP5TO7_ZVAL_MAYBE_P(result)); aggregate->schema = php_driver_add_ref(schema); aggregate->meta = meta; cass_aggregate_meta_full_name(aggregate->meta, &full_name, &full_name_length); PHP5TO7_ZVAL_MAYBE_MAKE(aggregate->signature); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(aggregate->signature), full_name, full_name_length); return result; } PHP_METHOD(DefaultKeyspace, aggregate) { php_driver_keyspace *self; char *name; php5to7_size name_len; php5to7_zval_args args = NULL; smart_str arguments = PHP5TO7_SMART_STR_INIT; int argc = 0; const CassAggregateMeta *meta = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|*", &name, &name_len, &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_KEYSPACE(getThis()); if (argc > 0) { if (php_driver_arguments_string(args, argc, &arguments TSRMLS_CC) == FAILURE) { PHP5TO7_MAYBE_EFREE(args); return; } } meta = cass_keyspace_meta_aggregate_by_name_n(self->meta, name, name_len, PHP5TO7_SMART_STR_VAL(arguments), PHP5TO7_SMART_STR_LEN(arguments)); if (meta) { php5to7_zval zaggregate = php_driver_create_aggregate(self->schema, meta TSRMLS_CC); RETVAL_ZVAL(PHP5TO7_ZVAL_MAYBE_P(zaggregate), 1, 1); } else { RETVAL_FALSE; } smart_str_free(&arguments); PHP5TO7_MAYBE_EFREE(args); } PHP_METHOD(DefaultKeyspace, aggregates) { php_driver_keyspace *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_KEYSPACE(getThis()); iterator = cass_iterator_aggregates_from_keyspace_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassAggregateMeta *meta = cass_iterator_get_aggregate_meta(iterator); php5to7_zval zaggregate = php_driver_create_aggregate(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zaggregate)) { php_driver_aggregate *aggregate = PHP_DRIVER_GET_AGGREGATE(PHP5TO7_ZVAL_MAYBE_P(zaggregate)); if (PHP5TO7_Z_TYPE_MAYBE_P(aggregate->signature) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(aggregate->signature), PHP5TO7_Z_STRLEN_MAYBE_P(aggregate->signature) + 1, PHP5TO7_ZVAL_MAYBE_P(zaggregate)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zaggregate)); } } } cass_iterator_free(iterator); } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_signature, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_ARG_INFO(0, ...) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_keyspace_methods[] = { PHP_ME(DefaultKeyspace, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, replicationClassName, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, replicationOptions, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, hasDurableWrites, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, table, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, tables, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, userType, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, userTypes, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, materializedView, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, materializedViews, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, function, arginfo_signature, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, functions, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, aggregate, arginfo_signature, ZEND_ACC_PUBLIC) PHP_ME(DefaultKeyspace, aggregates, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_keyspace_handlers; static HashTable * php_driver_type_default_keyspace_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_keyspace_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_keyspace_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_keyspace_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_keyspace *self = PHP5TO7_ZEND_OBJECT_GET(keyspace, object); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_keyspace_new(zend_class_entry *ce TSRMLS_DC) { php_driver_keyspace *self = PHP5TO7_ZEND_OBJECT_ECALLOC(keyspace, ce); self->meta = NULL; self->schema = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(keyspace, default_keyspace, self, ce); } void php_driver_define_DefaultKeyspace(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultKeyspace", php_driver_default_keyspace_methods); php_driver_default_keyspace_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_keyspace_ce TSRMLS_CC, 1, php_driver_keyspace_ce); php_driver_default_keyspace_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_keyspace_ce->create_object = php_driver_default_keyspace_new; memcpy(&php_driver_default_keyspace_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_keyspace_handlers.get_properties = php_driver_default_keyspace_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_keyspace_handlers.get_gc = php_driver_type_default_keyspace_gc; #endif php_driver_default_keyspace_handlers.compare_objects = php_driver_default_keyspace_compare; php_driver_default_keyspace_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultMaterializedView.c0000644000076500000240000005111013062515664021644 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultColumn.h" #include "DefaultIndex.h" #include "DefaultTable.h" #include "Table.h" zend_class_entry *php_driver_default_materialized_view_ce = NULL; static void populate_partition_key(php_driver_materialized_view *view, zval *result TSRMLS_DC) { size_t i, count = cass_materialized_view_meta_partition_key_count(view->meta); for (i = 0; i < count; ++i) { const CassColumnMeta *column = cass_materialized_view_meta_partition_key(view->meta, i); if (column) { php5to7_zval zcolumn = php_driver_create_column(view->schema, column TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { add_next_index_zval(result, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } } static void populate_clustering_key(php_driver_materialized_view *view, zval *result TSRMLS_DC) { size_t i, count = cass_materialized_view_meta_clustering_key_count(view->meta); for (i = 0; i < count; ++i) { const CassColumnMeta *column = cass_materialized_view_meta_clustering_key(view->meta, i); if (column) { php5to7_zval zcolumn = php_driver_create_column(view->schema, column TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { add_next_index_zval(result, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } } php5to7_zval php_driver_create_materialized_view(php_driver_ref* schema, const CassMaterializedViewMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_materialized_view *view; const char *name; size_t name_length; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_materialized_view_ce); view = PHP_DRIVER_GET_MATERIALIZED_VIEW(PHP5TO7_ZVAL_MAYBE_P(result)); view->schema = php_driver_add_ref(schema); view->meta = meta; cass_materialized_view_meta_name(meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(view->name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(view->name), name, name_length); return result; } void php_driver_default_materialized_view_build_options(php_driver_materialized_view *view TSRMLS_DC) { CassIterator *iterator = cass_iterator_fields_from_materialized_view_meta(view->meta); view->options = php_driver_table_build_options(iterator TSRMLS_CC); cass_iterator_free(iterator); } void php_driver_materialized_view_get_option(php_driver_materialized_view *view, const char *name, zval *result TSRMLS_DC) { zval *zvalue; if (PHP5TO7_ZVAL_IS_UNDEF(view->options)) { php_driver_default_materialized_view_build_options(view TSRMLS_CC); } if (!PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(view->options), name, strlen(name) + 1, zvalue)) { ZVAL_FALSE(result); return; } PHP5TO7_ZVAL_COPY(result, zvalue); } PHP_METHOD(DefaultMaterializedView, name) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->name), 1, 0); } PHP_METHOD(DefaultMaterializedView, option) { char *name; php5to7_size name_len; php_driver_materialized_view *self; php5to7_zval* result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_default_materialized_view_build_options(self TSRMLS_CC); } if (PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(self->options), name, name_len + 1, result)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(result), 1, 0); } RETURN_FALSE; } PHP_METHOD(DefaultMaterializedView, options) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_default_materialized_view_build_options(self TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->options), 1, 0); } PHP_METHOD(DefaultMaterializedView, comment) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "comment", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, readRepairChance) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "read_repair_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, localReadRepairChance) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "local_read_repair_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, gcGraceSeconds) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "gc_grace_seconds", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, caching) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "caching", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, bloomFilterFPChance) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "bloom_filter_fp_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, memtableFlushPeriodMs) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "memtable_flush_period_in_ms", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, defaultTTL) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "default_time_to_live", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, speculativeRetry) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "speculative_retry", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, indexInterval) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, compactionStrategyClassName) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "compaction_strategy_class", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, compactionStrategyOptions) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "compaction_strategy_options", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, compressionParameters) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "compression_parameters", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, populateIOCacheOnFlush) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "populate_io_cache_on_flush", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, replicateOnWrite) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "replicate_on_write", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, maxIndexInterval) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "max_index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, minIndexInterval) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); php_driver_materialized_view_get_option(self, "min_index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultMaterializedView, column) { php_driver_materialized_view *self; char *name; php5to7_size name_len; php5to7_zval column; const CassColumnMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); meta = cass_materialized_view_meta_column_by_name(self->meta, name); if (meta == NULL) { RETURN_FALSE; } column = php_driver_create_column(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(column)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(column), 0, 1); } PHP_METHOD(DefaultMaterializedView, columns) { php_driver_materialized_view *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); iterator = cass_iterator_columns_from_materialized_view_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassColumnMeta *meta; php5to7_zval zcolumn; php_driver_column *column; meta = cass_iterator_get_column_meta(iterator); zcolumn = php_driver_create_column(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { column = PHP_DRIVER_GET_COLUMN(PHP5TO7_ZVAL_MAYBE_P(zcolumn)); if (PHP5TO7_Z_TYPE_MAYBE_P(column->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(column->name), PHP5TO7_Z_STRLEN_MAYBE_P(column->name) + 1, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } cass_iterator_free(iterator); } PHP_METHOD(DefaultMaterializedView, partitionKey) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->partition_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->partition_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->partition_key)); populate_partition_key(self, PHP5TO7_ZVAL_MAYBE_P(self->partition_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->partition_key), 1, 0); } PHP_METHOD(DefaultMaterializedView, primaryKey) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->primary_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->primary_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->primary_key)); populate_partition_key(self, PHP5TO7_ZVAL_MAYBE_P(self->primary_key) TSRMLS_CC); populate_clustering_key(self, PHP5TO7_ZVAL_MAYBE_P(self->primary_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->primary_key), 1, 0); } PHP_METHOD(DefaultMaterializedView, clusteringKey) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->clustering_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->clustering_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->clustering_key)); populate_clustering_key(self, PHP5TO7_ZVAL_MAYBE_P(self->clustering_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->clustering_key), 1, 0); } PHP_METHOD(DefaultMaterializedView, clusteringOrder) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->clustering_order)) { size_t i, count = cass_materialized_view_meta_clustering_key_count(self->meta); PHP5TO7_ZVAL_MAYBE_MAKE(self->clustering_order); array_init(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order)); for (i = 0; i < count; ++i) { CassClusteringOrder order = cass_materialized_view_meta_clustering_key_order(self->meta, i); switch (order) { case CASS_CLUSTERING_ORDER_ASC: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "asc"); break; case CASS_CLUSTERING_ORDER_DESC: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "desc"); break; case CASS_CLUSTERING_ORDER_NONE: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "none"); break; } } } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), 1, 0); } PHP_METHOD(DefaultMaterializedView, baseTable) { php_driver_materialized_view *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_MATERIALIZED_VIEW(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->base_table)) { const CassTableMeta *table = cass_materialized_view_meta_base_table(self->meta); if (!table) { return; } self->base_table = php_driver_create_table(self->schema, table TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->base_table), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_materialized_view_methods[] = { PHP_ME(DefaultMaterializedView, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, option, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, options, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, comment, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, readRepairChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, localReadRepairChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, gcGraceSeconds, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, caching, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, bloomFilterFPChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, memtableFlushPeriodMs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, defaultTTL, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, speculativeRetry, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, indexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, compactionStrategyClassName, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, compactionStrategyOptions, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, compressionParameters, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, populateIOCacheOnFlush, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, replicateOnWrite, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, maxIndexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, minIndexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, column, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, columns, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, partitionKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, primaryKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, clusteringKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, clusteringOrder, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultMaterializedView, baseTable, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_materialized_view_handlers; static HashTable * php_driver_type_default_materialized_view_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_materialized_view_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_materialized_view_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_materialized_view_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_materialized_view *self = PHP5TO7_ZEND_OBJECT_GET(materialized_view, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->options); PHP5TO7_ZVAL_MAYBE_DESTROY(self->partition_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->primary_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->clustering_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->clustering_order); PHP5TO7_ZVAL_MAYBE_DESTROY(self->base_table); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_materialized_view_new(zend_class_entry *ce TSRMLS_DC) { php_driver_materialized_view *self = PHP5TO7_ZEND_OBJECT_ECALLOC(materialized_view, ce); PHP5TO7_ZVAL_UNDEF(self->name); PHP5TO7_ZVAL_UNDEF(self->options); PHP5TO7_ZVAL_UNDEF(self->partition_key); PHP5TO7_ZVAL_UNDEF(self->primary_key); PHP5TO7_ZVAL_UNDEF(self->clustering_key); PHP5TO7_ZVAL_UNDEF(self->clustering_order); PHP5TO7_ZVAL_UNDEF(self->base_table); self->meta = NULL; self->schema = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(materialized_view, default_materialized_view, self, ce); } void php_driver_define_DefaultMaterializedView(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultMaterializedView", php_driver_default_materialized_view_methods); php_driver_default_materialized_view_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_materialized_view_ce); php_driver_default_materialized_view_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_materialized_view_ce->create_object = php_driver_default_materialized_view_new; memcpy(&php_driver_default_materialized_view_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_materialized_view_handlers.get_properties = php_driver_default_materialized_view_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_materialized_view_handlers.get_gc = php_driver_type_default_materialized_view_gc; #endif php_driver_default_materialized_view_handlers.compare_objects = php_driver_default_materialized_view_compare; php_driver_default_materialized_view_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultMaterializedView.h0000644000076500000240000000164713062515664021663 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DEFAULT_MATERIALIZED_VIEW_H #define PHP_DRIVER_DEFAULT_MATERIALIZED_VIEW_H #include "php_driver.h" php5to7_zval php_driver_create_materialized_view(php_driver_ref* schema, const CassMaterializedViewMeta *meta TSRMLS_DC); #endif /* PHP_DRIVER_DEFAULT_MATERIALIZED_VIEW_H */ cassandra-1.3.0/src/DefaultSchema.c0000644000076500000240000001246113062515664017605 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/ref.h" zend_class_entry *php_driver_default_schema_ce = NULL; PHP_METHOD(DefaultSchema, keyspace) { char *name; php5to7_size name_len; php_driver_schema *self; php_driver_keyspace *keyspace; const CassKeyspaceMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_SCHEMA(getThis()); meta = cass_schema_meta_keyspace_by_name_n((CassSchemaMeta *) self->schema->data, name, name_len); if (meta == NULL) { RETURN_FALSE; } object_init_ex(return_value, php_driver_default_keyspace_ce); keyspace = PHP_DRIVER_GET_KEYSPACE(return_value); keyspace->schema = php_driver_add_ref(self->schema); keyspace->meta = meta; } PHP_METHOD(DefaultSchema, keyspaces) { php_driver_schema *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_SCHEMA(getThis()); iterator = cass_iterator_keyspaces_from_schema_meta((CassSchemaMeta *) self->schema->data); array_init(return_value); while (cass_iterator_next(iterator)) { const CassKeyspaceMeta *meta; const CassValue *value; const char *keyspace_name; size_t keyspace_name_len; php5to7_zval zkeyspace; php_driver_keyspace *keyspace; meta = cass_iterator_get_keyspace_meta(iterator); value = cass_keyspace_meta_field_by_name(meta, "keyspace_name"); ASSERT_SUCCESS_BLOCK(cass_value_get_string(value, &keyspace_name, &keyspace_name_len), zval_ptr_dtor(PHP5TO7_ZVAL_MAYBE_ADDR_OF(return_value)); return; ); PHP5TO7_ZVAL_MAYBE_MAKE(zkeyspace); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(zkeyspace), php_driver_default_keyspace_ce); keyspace = PHP_DRIVER_GET_KEYSPACE(PHP5TO7_ZVAL_MAYBE_P(zkeyspace)); keyspace->schema = php_driver_add_ref(self->schema); keyspace->meta = meta; PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, keyspace_name, keyspace_name_len + 1, PHP5TO7_ZVAL_MAYBE_P(zkeyspace)); } cass_iterator_free(iterator); } PHP_METHOD(DefaultSchema, version) { php_driver_schema *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_SCHEMA(getThis()); RETURN_LONG(cass_schema_meta_snapshot_version((CassSchemaMeta *) self->schema->data)); } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_schema_methods[] = { PHP_ME(DefaultSchema, keyspace, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultSchema, keyspaces, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultSchema, version, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_schema_handlers; static HashTable * php_driver_default_schema_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_schema_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_schema_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_schema *self = PHP5TO7_ZEND_OBJECT_GET(schema, object); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_schema_new(zend_class_entry *ce TSRMLS_DC) { php_driver_schema *self = PHP5TO7_ZEND_OBJECT_ECALLOC(schema, ce); self->schema = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(schema, default_schema, self, ce); } void php_driver_define_DefaultSchema(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultSchema", php_driver_default_schema_methods); php_driver_default_schema_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_schema_ce TSRMLS_CC, 1, php_driver_schema_ce); php_driver_default_schema_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_schema_ce->create_object = php_driver_default_schema_new; memcpy(&php_driver_default_schema_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_schema_handlers.get_properties = php_driver_default_schema_properties; php_driver_default_schema_handlers.compare_objects = php_driver_default_schema_compare; php_driver_default_schema_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultSession.c0000644000076500000240000011521113062515664020025 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/bytes.h" #include "util/future.h" #include "util/result.h" #include "util/ref.h" #include "util/math.h" #include "util/collections.h" #include "ExecutionOptions.h" zend_class_entry *php_driver_default_session_ce = NULL; #define CHECK_RESULT(rc) \ { \ ASSERT_SUCCESS_VALUE(rc, FAILURE) \ return SUCCESS; \ } static void free_result(void *result) { cass_result_free((CassResult *) result); } static void free_statement(void *statement) { cass_statement_free((CassStatement *) statement); } static void free_schema(void *schema) { cass_schema_meta_free((CassSchemaMeta *) schema); } static int bind_argument_by_index(CassStatement *statement, size_t index, zval *value TSRMLS_DC) { if (Z_TYPE_P(value) == IS_NULL) CHECK_RESULT(cass_statement_bind_null(statement, index)); if (Z_TYPE_P(value) == IS_STRING) CHECK_RESULT(cass_statement_bind_string(statement, index, Z_STRVAL_P(value))); if (Z_TYPE_P(value) == IS_DOUBLE) CHECK_RESULT(cass_statement_bind_double(statement, index, Z_DVAL_P(value))); if (Z_TYPE_P(value) == IS_LONG) CHECK_RESULT(cass_statement_bind_int32(statement, index, Z_LVAL_P(value))); if (PHP5TO7_ZVAL_IS_TRUE_P(value)) CHECK_RESULT(cass_statement_bind_bool(statement, index, cass_true)); if (PHP5TO7_ZVAL_IS_FALSE_P(value)) CHECK_RESULT(cass_statement_bind_bool(statement, index, cass_false)); if (Z_TYPE_P(value) == IS_OBJECT) { if (instanceof_function(Z_OBJCE_P(value), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *float_number = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_float(statement, index, float_number->data.floating.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int64(statement, index, bigint->data.bigint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int16(statement, index, smallint->data.smallint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int8(statement, index, tinyint->data.tinyint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_timestamp_ce TSRMLS_CC)) { php_driver_timestamp *timestamp = PHP_DRIVER_GET_TIMESTAMP(value); CHECK_RESULT(cass_statement_bind_int64(statement, index, timestamp->timestamp)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_date_ce TSRMLS_CC)) { php_driver_date *date = PHP_DRIVER_GET_DATE(value); CHECK_RESULT(cass_statement_bind_uint32(statement, index, date->date)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_time_ce TSRMLS_CC)) { php_driver_time *time = PHP_DRIVER_GET_TIME(value); CHECK_RESULT(cass_statement_bind_int64(statement, index, time->time)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_blob_ce TSRMLS_CC)) { php_driver_blob *blob = PHP_DRIVER_GET_BLOB(value); CHECK_RESULT(cass_statement_bind_bytes(statement, index, blob->data, blob->size)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(value); size_t size; cass_byte_t *data = export_twos_complement(varint->data.varint.value, &size); CassError rc = cass_statement_bind_bytes(statement, index, data, size); free(data); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(value); size_t size; cass_byte_t *data = (cass_byte_t *) export_twos_complement(decimal->data.decimal.value, &size); CassError rc = cass_statement_bind_decimal(statement, index, data, size, decimal->data.decimal.scale); free(data); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_uuid_interface_ce TSRMLS_CC)) { php_driver_uuid *uuid = PHP_DRIVER_GET_UUID(value); CHECK_RESULT(cass_statement_bind_uuid(statement, index, uuid->uuid)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_inet_ce TSRMLS_CC)) { php_driver_inet *inet = PHP_DRIVER_GET_INET(value); CHECK_RESULT(cass_statement_bind_inet(statement, index, inet->inet)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_duration_ce TSRMLS_CC)) { php_driver_duration *duration = PHP_DRIVER_GET_DURATION(value); CHECK_RESULT(cass_statement_bind_duration(statement, index, duration->months, duration->days, duration->nanos)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_set_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_set *set = PHP_DRIVER_GET_SET(value); if (!php_driver_collection_from_set(set, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection(statement, index, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_map_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_map *map = PHP_DRIVER_GET_MAP(value); if (!php_driver_collection_from_map(map, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection(statement, index, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_collection_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_collection *coll = PHP_DRIVER_GET_COLLECTION(value); if (!php_driver_collection_from_collection(coll, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection(statement, index, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_tuple_ce TSRMLS_CC)) { CassError rc; CassTuple *tup; php_driver_tuple *tuple = PHP_DRIVER_GET_TUPLE(value); if (!php_driver_tuple_from_tuple(tuple, &tup TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_tuple(statement, index, tup); cass_tuple_free(tup); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_user_type_value_ce TSRMLS_CC)) { CassError rc; CassUserType *ut; php_driver_user_type_value *user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(value); if (!php_driver_user_type_from_user_type_value(user_type_value, &ut TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_user_type(statement, index, ut); cass_user_type_free(ut); CHECK_RESULT(rc); } } return FAILURE; } static int bind_argument_by_name(CassStatement *statement, const char *name, zval *value TSRMLS_DC) { if (Z_TYPE_P(value) == IS_NULL) { CHECK_RESULT(cass_statement_bind_null_by_name(statement, name)); } if (Z_TYPE_P(value) == IS_STRING) CHECK_RESULT(cass_statement_bind_string_by_name(statement, name, Z_STRVAL_P(value))); if (Z_TYPE_P(value) == IS_DOUBLE) CHECK_RESULT(cass_statement_bind_double_by_name(statement, name, Z_DVAL_P(value))); if (Z_TYPE_P(value) == IS_LONG) CHECK_RESULT(cass_statement_bind_int32_by_name(statement, name, Z_LVAL_P(value))); if (PHP5TO7_ZVAL_IS_TRUE_P(value)) CHECK_RESULT(cass_statement_bind_bool_by_name(statement, name, cass_true)); if (PHP5TO7_ZVAL_IS_FALSE_P(value)) CHECK_RESULT(cass_statement_bind_bool_by_name(statement, name, cass_false)); if (Z_TYPE_P(value) == IS_OBJECT) { if (instanceof_function(Z_OBJCE_P(value), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *float_number = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_float_by_name(statement, name, float_number->data.floating.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int64_by_name(statement, name, bigint->data.bigint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int16_by_name(statement, name, smallint->data.smallint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(value); CHECK_RESULT(cass_statement_bind_int8_by_name(statement, name, tinyint->data.tinyint.value)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_timestamp_ce TSRMLS_CC)) { php_driver_timestamp *timestamp = PHP_DRIVER_GET_TIMESTAMP(value); CHECK_RESULT(cass_statement_bind_int64_by_name(statement, name, timestamp->timestamp)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_date_ce TSRMLS_CC)) { php_driver_date *date = PHP_DRIVER_GET_DATE(value); CHECK_RESULT(cass_statement_bind_uint32_by_name(statement, name, date->date)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_time_ce TSRMLS_CC)) { php_driver_time *time = PHP_DRIVER_GET_TIME(value); CHECK_RESULT(cass_statement_bind_int64_by_name(statement, name, time->time)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_blob_ce TSRMLS_CC)) { php_driver_blob *blob = PHP_DRIVER_GET_BLOB(value); CHECK_RESULT(cass_statement_bind_bytes_by_name(statement, name, blob->data, blob->size)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(value); size_t size; cass_byte_t *data = (cass_byte_t *) export_twos_complement(varint->data.varint.value, &size); CassError rc = cass_statement_bind_bytes_by_name(statement, name, data, size); free(data); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_decimal_ce TSRMLS_CC)) { php_driver_numeric *decimal = PHP_DRIVER_GET_NUMERIC(value); size_t size; cass_byte_t *data = (cass_byte_t *) export_twos_complement(decimal->data.decimal.value, &size); CassError rc = cass_statement_bind_decimal_by_name(statement, name, data, size, decimal->data.decimal.scale); free(data); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_uuid_interface_ce TSRMLS_CC)) { php_driver_uuid *uuid = PHP_DRIVER_GET_UUID(value); CHECK_RESULT(cass_statement_bind_uuid_by_name(statement, name, uuid->uuid)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_inet_ce TSRMLS_CC)) { php_driver_inet *inet = PHP_DRIVER_GET_INET(value); CHECK_RESULT(cass_statement_bind_inet_by_name(statement, name, inet->inet)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_duration_ce TSRMLS_CC)) { php_driver_duration *duration = PHP_DRIVER_GET_DURATION(value); CHECK_RESULT(cass_statement_bind_duration_by_name(statement, name, duration->months, duration->days, duration->nanos)); } if (instanceof_function(Z_OBJCE_P(value), php_driver_set_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_set *set = PHP_DRIVER_GET_SET(value); if (!php_driver_collection_from_set(set, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection_by_name(statement, name, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_map_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_map *map = PHP_DRIVER_GET_MAP(value); if (!php_driver_collection_from_map(map, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection_by_name(statement, name, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_collection_ce TSRMLS_CC)) { CassError rc; CassCollection *collection; php_driver_collection *coll = PHP_DRIVER_GET_COLLECTION(value); if (!php_driver_collection_from_collection(coll, &collection TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_collection_by_name(statement, name, collection); cass_collection_free(collection); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_tuple_ce TSRMLS_CC)) { CassError rc; CassTuple *tup; php_driver_tuple *tuple = PHP_DRIVER_GET_TUPLE(value); if (!php_driver_tuple_from_tuple(tuple, &tup TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_tuple_by_name(statement, name, tup); cass_tuple_free(tup); CHECK_RESULT(rc); } if (instanceof_function(Z_OBJCE_P(value), php_driver_user_type_value_ce TSRMLS_CC)) { CassError rc; CassUserType *ut; php_driver_user_type_value *user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(value); if (!php_driver_user_type_from_user_type_value(user_type_value, &ut TSRMLS_CC)) return FAILURE; rc = cass_statement_bind_user_type_by_name(statement, name, ut); cass_user_type_free(ut); CHECK_RESULT(rc); } } return FAILURE; } static int bind_arguments(CassStatement *statement, HashTable *arguments TSRMLS_DC) { int rc = SUCCESS; php5to7_zval *current; ulong num_key; #if PHP_MAJOR_VERSION >= 7 zend_string *key; ZEND_HASH_FOREACH_KEY_VAL(arguments, num_key, key, current) { if (key) { rc = bind_argument_by_name(statement, key->val, PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC); #else char *str_key; uint str_len; PHP5TO7_ZEND_HASH_FOREACH_KEY_VAL(arguments, num_key, str_key, str_len, current) { if (str_key) { rc = bind_argument_by_name(statement, str_key, PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC); #endif } else { rc = bind_argument_by_index(statement, num_key, PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC); } if (rc == FAILURE) break; } PHP5TO7_ZEND_HASH_FOREACH_END(arguments); return rc; } static CassStatement * create_statement(php_driver_statement *statement, HashTable *arguments TSRMLS_DC) { CassStatement *stmt; uint32_t count; switch (statement->type) { case PHP_DRIVER_SIMPLE_STATEMENT: count = 0; if (arguments) count = zend_hash_num_elements(arguments); stmt = cass_statement_new(statement->data.simple.cql, count); break; case PHP_DRIVER_PREPARED_STATEMENT: stmt = cass_prepared_bind(statement->data.prepared.prepared); break; default: zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unsupported statement type."); return NULL; } if (arguments && bind_arguments(stmt, arguments TSRMLS_CC) == FAILURE) { cass_statement_free(stmt); return NULL; } return stmt; } static CassBatch * create_batch(php_driver_statement *batch, CassConsistency consistency, CassRetryPolicy *retry_policy, cass_int64_t timestamp TSRMLS_DC) { CassBatch *cass_batch = cass_batch_new(batch->data.batch.type); CassError rc = CASS_OK; php5to7_zval *current; PHP5TO7_ZEND_HASH_FOREACH_VAL(&batch->data.batch.statements, current) { php_driver_statement *statement; php_driver_statement simple_statement; HashTable *arguments; CassStatement *stmt; #if PHP_MAJOR_VERSION >= 7 php_driver_batch_statement_entry *batch_statement_entry = (php_driver_batch_statement_entry *)Z_PTR_P(current); #else php_driver_batch_statement_entry *batch_statement_entry = *((php_driver_batch_statement_entry **)current); #endif if (PHP5TO7_Z_TYPE_MAYBE_P(batch_statement_entry->statement) == IS_STRING) { simple_statement.type = PHP_DRIVER_SIMPLE_STATEMENT; simple_statement.data.simple.cql = PHP5TO7_Z_STRVAL_MAYBE_P(batch_statement_entry->statement); statement = &simple_statement; } else { statement = PHP_DRIVER_GET_STATEMENT(PHP5TO7_ZVAL_MAYBE_P(batch_statement_entry->statement)); } arguments = !PHP5TO7_ZVAL_IS_UNDEF(batch_statement_entry->arguments) ? Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(batch_statement_entry->arguments)) : NULL; stmt = create_statement(statement, arguments TSRMLS_CC); if (!stmt) { cass_batch_free(cass_batch); return NULL; } cass_batch_add_statement(cass_batch, stmt); cass_statement_free(stmt); } PHP5TO7_ZEND_HASH_FOREACH_END(&batch->data.batch.statements); rc = cass_batch_set_consistency(cass_batch, consistency); ASSERT_SUCCESS_BLOCK(rc, cass_batch_free(cass_batch); return NULL; ) rc = cass_batch_set_retry_policy(cass_batch, retry_policy); ASSERT_SUCCESS_BLOCK(rc, cass_batch_free(cass_batch); return NULL; ) rc = cass_batch_set_timestamp(cass_batch, timestamp); ASSERT_SUCCESS_BLOCK(rc, cass_batch_free(cass_batch); return NULL; ) return cass_batch; } static CassStatement * create_single(php_driver_statement *statement, HashTable *arguments, CassConsistency consistency, long serial_consistency, int page_size, const char* paging_state_token, size_t paging_state_token_size, CassRetryPolicy *retry_policy, cass_int64_t timestamp TSRMLS_DC) { CassError rc = CASS_OK; CassStatement *stmt = create_statement(statement, arguments TSRMLS_CC); if (!stmt) return NULL; rc = cass_statement_set_consistency(stmt, consistency); if (rc == CASS_OK && serial_consistency >= 0) rc = cass_statement_set_serial_consistency(stmt, serial_consistency); if (rc == CASS_OK && page_size >= 0) rc = cass_statement_set_paging_size(stmt, page_size); if (rc == CASS_OK && paging_state_token) { rc = cass_statement_set_paging_state_token(stmt, paging_state_token, paging_state_token_size); } if (rc == CASS_OK && retry_policy) rc = cass_statement_set_retry_policy(stmt, retry_policy); if (rc == CASS_OK) rc = cass_statement_set_timestamp(stmt, timestamp); if (rc != CASS_OK) { cass_statement_free(stmt); zend_throw_exception_ex(exception_class(rc), rc TSRMLS_CC, "%s", cass_error_desc(rc)); return NULL; } return stmt; } PHP_METHOD(DefaultSession, execute) { zval *statement = NULL; zval *options = NULL; php_driver_session *self = NULL; php_driver_statement *stmt = NULL; php_driver_statement simple_statement; HashTable *arguments = NULL; CassConsistency consistency = PHP_DRIVER_DEFAULT_CONSISTENCY; int page_size = -1; char *paging_state_token = NULL; size_t paging_state_token_size = 0; zval *timeout = NULL; long serial_consistency = -1; CassRetryPolicy *retry_policy = NULL; cass_int64_t timestamp = INT64_MIN; php_driver_execution_options *opts = NULL; php_driver_execution_options local_opts; CassFuture *future = NULL; CassStatement *single = NULL; CassBatch *batch = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &statement, &options) == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); if (Z_TYPE_P(statement) == IS_STRING) { simple_statement.type = PHP_DRIVER_SIMPLE_STATEMENT; simple_statement.data.simple.cql = Z_STRVAL_P(statement); stmt = &simple_statement; } else if (Z_TYPE_P(statement) == IS_OBJECT && instanceof_function(Z_OBJCE_P(statement), php_driver_statement_ce TSRMLS_CC)) { stmt = PHP_DRIVER_GET_STATEMENT(statement); } else { INVALID_ARGUMENT(statement, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Statement"); } consistency = self->default_consistency; page_size = self->default_page_size; timeout = PHP5TO7_ZVAL_MAYBE_P(self->default_timeout); if (options) { if (Z_TYPE_P(options) != IS_ARRAY && (Z_TYPE_P(options) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce TSRMLS_CC))) { INVALID_ARGUMENT(options, "an instance of " PHP_DRIVER_NAMESPACE "\\ExecutionOptions or an array or null"); } if (Z_TYPE_P(options) == IS_OBJECT) { opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options); } else { if (php_driver_execution_options_build_local_from_array(&local_opts, options TSRMLS_CC) == FAILURE) { return; } opts = &local_opts; } if (!PHP5TO7_ZVAL_IS_UNDEF(opts->arguments)) arguments = PHP5TO7_Z_ARRVAL_MAYBE_P(opts->arguments); if (opts->consistency >= 0) consistency = (CassConsistency) opts->consistency; if (opts->page_size >= 0) page_size = opts->page_size; if (opts->paging_state_token) { paging_state_token = opts->paging_state_token; paging_state_token_size = opts->paging_state_token_size; } if (!PHP5TO7_ZVAL_IS_UNDEF(opts->timeout)) timeout = PHP5TO7_ZVAL_MAYBE_P(opts->timeout); if (opts->serial_consistency >= 0) serial_consistency = opts->serial_consistency; if (!PHP5TO7_ZVAL_IS_UNDEF(opts->retry_policy)) retry_policy = (PHP_DRIVER_GET_RETRY_POLICY(PHP5TO7_ZVAL_MAYBE_P(opts->retry_policy)))->policy; timestamp = opts->timestamp; } switch (stmt->type) { case PHP_DRIVER_SIMPLE_STATEMENT: case PHP_DRIVER_PREPARED_STATEMENT: single = create_single(stmt, arguments, consistency, serial_consistency, page_size, paging_state_token, paging_state_token_size, retry_policy, timestamp TSRMLS_CC); if (!single) return; future = cass_session_execute((CassSession *) self->session->data, single); break; case PHP_DRIVER_BATCH_STATEMENT: batch = create_batch(stmt, consistency, retry_policy, timestamp TSRMLS_CC); if (!batch) return; future = cass_session_execute_batch((CassSession *) self->session->data, batch); break; default: INVALID_ARGUMENT(statement, "an instance of " PHP_DRIVER_NAMESPACE "\\SimpleStatement, " \ PHP_DRIVER_NAMESPACE "\\PreparedStatement or " PHP_DRIVER_NAMESPACE "\\BatchStatement" ); return; } do { const CassResult *result = NULL; php_driver_rows *rows = NULL; if (php_driver_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE || php_driver_future_is_error(future TSRMLS_CC) == FAILURE) break; result = cass_future_get_result(future); cass_future_free(future); if (!result) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Future doesn't contain a result."); break; } object_init_ex(return_value, php_driver_rows_ce); rows = PHP_DRIVER_GET_ROWS(return_value); if (php_driver_get_result(result, &rows->rows TSRMLS_CC) == FAILURE) { cass_result_free(result); break; } if (single && cass_result_has_more_pages(result)) { rows->statement = php_driver_new_ref(single, free_statement); rows->result = php_driver_new_ref((void *)result, free_result); rows->session = php_driver_add_ref(self->session); return; } cass_result_free(result); } while (0); if (batch) cass_batch_free(batch); if (single) cass_statement_free(single); } PHP_METHOD(DefaultSession, executeAsync) { zval *statement = NULL; zval *options = NULL; php_driver_session *self = NULL; php_driver_statement *stmt = NULL; php_driver_statement simple_statement; HashTable *arguments = NULL; CassConsistency consistency = PHP_DRIVER_DEFAULT_CONSISTENCY; int page_size = -1; char *paging_state_token = NULL; size_t paging_state_token_size = 0; long serial_consistency = -1; CassRetryPolicy *retry_policy = NULL; cass_int64_t timestamp = INT64_MIN; php_driver_execution_options *opts = NULL; php_driver_execution_options local_opts; php_driver_future_rows *future_rows = NULL; CassStatement *single = NULL; CassBatch *batch = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &statement, &options) == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); if (Z_TYPE_P(statement) == IS_STRING) { simple_statement.type = PHP_DRIVER_SIMPLE_STATEMENT; simple_statement.data.simple.cql = Z_STRVAL_P(statement); stmt = &simple_statement; } else if (Z_TYPE_P(statement) == IS_OBJECT && instanceof_function(Z_OBJCE_P(statement), php_driver_statement_ce TSRMLS_CC)) { stmt = PHP_DRIVER_GET_STATEMENT(statement); } else { INVALID_ARGUMENT(statement, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Statement"); } consistency = self->default_consistency; page_size = self->default_page_size; if (options) { if (Z_TYPE_P(options) != IS_ARRAY && (Z_TYPE_P(options) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce TSRMLS_CC))) { INVALID_ARGUMENT(options, "an instance of " PHP_DRIVER_NAMESPACE "\\ExecutionOptions or an array or null"); } if (Z_TYPE_P(options) == IS_OBJECT) { opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options); } else { if (php_driver_execution_options_build_local_from_array(&local_opts, options TSRMLS_CC) == FAILURE) { return; } opts = &local_opts; } if (!PHP5TO7_ZVAL_IS_UNDEF(opts->arguments)) arguments = PHP5TO7_Z_ARRVAL_MAYBE_P(opts->arguments); if (opts->consistency >= 0) consistency = (CassConsistency) opts->consistency; if (opts->page_size >= 0) page_size = opts->page_size; if (opts->paging_state_token) { paging_state_token = opts->paging_state_token; paging_state_token_size = opts->paging_state_token_size; } if (opts->serial_consistency >= 0) serial_consistency = opts->serial_consistency; if (!PHP5TO7_ZVAL_IS_UNDEF(opts->retry_policy)) retry_policy = (PHP_DRIVER_GET_RETRY_POLICY(PHP5TO7_ZVAL_MAYBE_P(opts->retry_policy)))->policy; timestamp = opts->timestamp; } object_init_ex(return_value, php_driver_future_rows_ce); future_rows = PHP_DRIVER_GET_FUTURE_ROWS(return_value); switch (stmt->type) { case PHP_DRIVER_SIMPLE_STATEMENT: case PHP_DRIVER_PREPARED_STATEMENT: single = create_single(stmt, arguments, consistency, serial_consistency, page_size, paging_state_token, paging_state_token_size, retry_policy, timestamp TSRMLS_CC); if (!single) return; future_rows->statement = php_driver_new_ref(single, free_statement); future_rows->future = cass_session_execute((CassSession *) self->session->data, single); future_rows->session = php_driver_add_ref(self->session); break; case PHP_DRIVER_BATCH_STATEMENT: batch = create_batch(stmt, consistency, retry_policy, timestamp TSRMLS_CC); if (!batch) return; future_rows->future = cass_session_execute_batch((CassSession *) self->session->data, batch); cass_batch_free(batch); break; default: INVALID_ARGUMENT(statement, "an instance of " PHP_DRIVER_NAMESPACE "\\SimpleStatement, " \ PHP_DRIVER_NAMESPACE "\\PreparedStatement or " PHP_DRIVER_NAMESPACE "\\BatchStatement" ); return; } } PHP_METHOD(DefaultSession, prepare) { zval *cql = NULL; zval *options = NULL; php_driver_session *self = NULL; php_driver_execution_options *opts = NULL; php_driver_execution_options local_opts; CassFuture *future = NULL; zval *timeout = NULL; php_driver_statement *prepared_statement = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &cql, &options) == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); if (options) { if (Z_TYPE_P(options) != IS_ARRAY && (Z_TYPE_P(options) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(options), php_driver_execution_options_ce TSRMLS_CC))) { INVALID_ARGUMENT(options, "an instance of " PHP_DRIVER_NAMESPACE "\\ExecutionOptions or an array or null"); } if (Z_TYPE_P(options) == IS_OBJECT) { opts = PHP_DRIVER_GET_EXECUTION_OPTIONS(options); } else { if (php_driver_execution_options_build_local_from_array(&local_opts, options TSRMLS_CC) == FAILURE) { return; } opts = &local_opts; } timeout = PHP5TO7_ZVAL_MAYBE_P(opts->timeout); } future = cass_session_prepare_n((CassSession *)self->session->data, Z_STRVAL_P(cql), Z_STRLEN_P(cql)); if (php_driver_future_wait_timed(future, timeout TSRMLS_CC) == SUCCESS && php_driver_future_is_error(future TSRMLS_CC) == SUCCESS) { object_init_ex(return_value, php_driver_prepared_statement_ce); prepared_statement = PHP_DRIVER_GET_STATEMENT(return_value); prepared_statement->data.prepared.prepared = cass_future_get_prepared(future); } cass_future_free(future); } PHP_METHOD(DefaultSession, prepareAsync) { zval *cql = NULL; zval *options = NULL; php_driver_session *self = NULL; CassFuture *future = NULL; php_driver_future_prepared_statement *future_prepared = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &cql, &options) == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); future = cass_session_prepare_n((CassSession *)self->session->data, Z_STRVAL_P(cql), Z_STRLEN_P(cql)); object_init_ex(return_value, php_driver_future_prepared_statement_ce); future_prepared = PHP_DRIVER_GET_FUTURE_PREPARED_STATEMENT(return_value); future_prepared->future = future; } PHP_METHOD(DefaultSession, close) { zval *timeout = NULL; CassFuture *future = NULL; php_driver_session *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); if (self->persist) return; future = cass_session_close((CassSession *) self->session->data); if (php_driver_future_wait_timed(future, timeout TSRMLS_CC) == SUCCESS) php_driver_future_is_error(future TSRMLS_CC); cass_future_free(future); } PHP_METHOD(DefaultSession, closeAsync) { php_driver_session *self; php_driver_future_close *future = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_SESSION(getThis()); if (self->persist) { object_init_ex(return_value, php_driver_future_value_ce); return; } object_init_ex(return_value, php_driver_future_close_ce); future = PHP_DRIVER_GET_FUTURE_CLOSE(return_value); future->future = cass_session_close((CassSession *) self->session->data); } PHP_METHOD(DefaultSession, metrics) { CassMetrics metrics; php5to7_zval requests; php5to7_zval stats; php5to7_zval errors; php_driver_session *self = PHP_DRIVER_GET_SESSION(getThis()); if (zend_parse_parameters_none() == FAILURE) return; cass_session_get_metrics((CassSession *)self->session->data, &metrics); PHP5TO7_ZVAL_MAYBE_MAKE(requests); array_init(PHP5TO7_ZVAL_MAYBE_P(requests)); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "min", metrics.requests.min); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "max", metrics.requests.max); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "mean", metrics.requests.mean); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "stddev", metrics.requests.stddev); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "median", metrics.requests.median); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "p75", metrics.requests.percentile_75th); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "p95", metrics.requests.percentile_95th); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "p98", metrics.requests.percentile_98th); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "p99", metrics.requests.percentile_99th); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(requests), "p999", metrics.requests.percentile_999th); add_assoc_double(PHP5TO7_ZVAL_MAYBE_P(requests), "mean_rate", metrics.requests.mean_rate); add_assoc_double(PHP5TO7_ZVAL_MAYBE_P(requests), "m1_rate", metrics.requests.one_minute_rate); add_assoc_double(PHP5TO7_ZVAL_MAYBE_P(requests), "m5_rate", metrics.requests.five_minute_rate); add_assoc_double(PHP5TO7_ZVAL_MAYBE_P(requests), "m15_rate", metrics.requests.fifteen_minute_rate); PHP5TO7_ZVAL_MAYBE_MAKE(stats); array_init(PHP5TO7_ZVAL_MAYBE_P(stats)); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(stats), "total_connections", metrics.stats.total_connections); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(stats), "available_connections", metrics.stats.available_connections); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(stats), "exceeded_pending_requests_water_mark", metrics.stats.exceeded_pending_requests_water_mark); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(stats), "exceeded_write_bytes_water_mark", metrics.stats.exceeded_write_bytes_water_mark); PHP5TO7_ZVAL_MAYBE_MAKE(errors); array_init(PHP5TO7_ZVAL_MAYBE_P(errors)); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(errors), "connection_timeouts", metrics.errors.connection_timeouts); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(errors), "pending_request_timeouts", metrics.errors.pending_request_timeouts); add_assoc_long(PHP5TO7_ZVAL_MAYBE_P(errors), "request_timeouts", metrics.errors.request_timeouts); array_init(return_value); add_assoc_zval(return_value, "stats", PHP5TO7_ZVAL_MAYBE_P(stats)); add_assoc_zval(return_value, "requests", PHP5TO7_ZVAL_MAYBE_P(requests)); add_assoc_zval(return_value, "errors", PHP5TO7_ZVAL_MAYBE_P(errors)); } PHP_METHOD(DefaultSession, schema) { php_driver_session *self; php_driver_schema *schema; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_SESSION(getThis()); object_init_ex(return_value, php_driver_default_schema_ce); schema = PHP_DRIVER_GET_SCHEMA(return_value); schema->schema = php_driver_new_ref( (void *) cass_session_get_schema_meta((CassSession *) self->session->data), free_schema); } ZEND_BEGIN_ARG_INFO_EX(arginfo_execute, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, statement) ZEND_ARG_INFO(0, options) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_prepare, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, cql) ZEND_ARG_INFO(0, options) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_session_methods[] = { PHP_ME(DefaultSession, execute, arginfo_execute, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, executeAsync, arginfo_execute, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, prepare, arginfo_prepare, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, prepareAsync, arginfo_prepare, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, close, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, closeAsync, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, metrics, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultSession, schema, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_session_handlers; static HashTable * php_driver_default_session_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_session_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_session_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_session *self = PHP5TO7_ZEND_OBJECT_GET(session, object); php_driver_del_peref(&self->session, 1); PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_timeout); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_session_new(zend_class_entry *ce TSRMLS_DC) { php_driver_session *self = PHP5TO7_ZEND_OBJECT_ECALLOC(session, ce); self->session = NULL; self->persist = 0; self->default_consistency = PHP_DRIVER_DEFAULT_CONSISTENCY; self->default_page_size = 5000; PHP5TO7_ZVAL_UNDEF(self->default_timeout); PHP5TO7_ZEND_OBJECT_INIT_EX(session, default_session, self, ce); } void php_driver_define_DefaultSession(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultSession", php_driver_default_session_methods); php_driver_default_session_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_session_ce TSRMLS_CC, 1, php_driver_session_ce); php_driver_default_session_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_session_ce->create_object = php_driver_default_session_new; memcpy(&php_driver_default_session_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_session_handlers.get_properties = php_driver_default_session_properties; php_driver_default_session_handlers.compare_objects = php_driver_default_session_compare; php_driver_default_session_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultTable.c0000644000076500000240000005320413062515664017434 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/result.h" #include "util/ref.h" #include "util/types.h" #include "DefaultColumn.h" #include "DefaultMaterializedView.h" #include "DefaultIndex.h" #include "Table.h" zend_class_entry *php_driver_default_table_ce = NULL; static void populate_partition_key(php_driver_table *table, zval *result TSRMLS_DC) { size_t i, count = cass_table_meta_partition_key_count(table->meta); for (i = 0; i < count; ++i) { const CassColumnMeta *column = cass_table_meta_partition_key(table->meta, i); if (column) { php5to7_zval zcolumn = php_driver_create_column(table->schema, column TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { add_next_index_zval(result, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } } static void populate_clustering_key(php_driver_table *table, zval *result TSRMLS_DC) { size_t i, count = cass_table_meta_clustering_key_count(table->meta); for (i = 0; i < count; ++i) { const CassColumnMeta *column = cass_table_meta_clustering_key(table->meta, i); if (column) { php5to7_zval zcolumn = php_driver_create_column(table->schema, column TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { add_next_index_zval(result, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } } php5to7_zval php_driver_create_table(php_driver_ref* schema, const CassTableMeta *meta TSRMLS_DC) { php5to7_zval result; php_driver_table *table; const char *name; size_t name_length; PHP5TO7_ZVAL_UNDEF(result); PHP5TO7_ZVAL_MAYBE_MAKE(result); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(result), php_driver_default_table_ce); table = PHP_DRIVER_GET_TABLE(PHP5TO7_ZVAL_MAYBE_P(result)); table->schema = php_driver_add_ref(schema); table->meta = meta; cass_table_meta_name(meta, &name, &name_length); PHP5TO7_ZVAL_MAYBE_MAKE(table->name); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(table->name), name, name_length); return result; } void php_driver_default_table_build_options(php_driver_table *table TSRMLS_DC) { CassIterator *iterator = cass_iterator_fields_from_table_meta(table->meta); table->options = php_driver_table_build_options(iterator TSRMLS_CC); cass_iterator_free(iterator); } void php_driver_table_get_option(php_driver_table *table, const char *name, zval *result TSRMLS_DC) { zval *zvalue; if (PHP5TO7_ZVAL_IS_UNDEF(table->options)) { php_driver_default_table_build_options(table TSRMLS_CC); } if (!PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(table->options), name, strlen(name) + 1, zvalue)) { ZVAL_FALSE(result); return; } PHP5TO7_ZVAL_COPY(result, zvalue); } PHP_METHOD(DefaultTable, name) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->name), 1, 0); } PHP_METHOD(DefaultTable, option) { char *name; php5to7_size name_len; php_driver_table *self; php5to7_zval* result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_default_table_build_options(self TSRMLS_CC); } if (PHP5TO7_ZEND_HASH_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(self->options), name, name_len + 1, result)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(result), 1, 0); } RETURN_FALSE; } PHP_METHOD(DefaultTable, options) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->options)) { php_driver_default_table_build_options(self TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->options), 1, 0); } PHP_METHOD(DefaultTable, comment) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "comment", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, readRepairChance) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "read_repair_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, localReadRepairChance) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "local_read_repair_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, gcGraceSeconds) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "gc_grace_seconds", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, caching) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "caching", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, bloomFilterFPChance) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "bloom_filter_fp_chance", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, memtableFlushPeriodMs) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "memtable_flush_period_in_ms", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, defaultTTL) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "default_time_to_live", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, speculativeRetry) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "speculative_retry", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, indexInterval) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, compactionStrategyClassName) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "compaction_strategy_class", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, compactionStrategyOptions) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "compaction_strategy_options", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, compressionParameters) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "compression_parameters", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, populateIOCacheOnFlush) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "populate_io_cache_on_flush", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, replicateOnWrite) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "replicate_on_write", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, maxIndexInterval) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "max_index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, minIndexInterval) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); php_driver_table_get_option(self, "min_index_interval", return_value TSRMLS_CC); } PHP_METHOD(DefaultTable, column) { php_driver_table *self; char *name; php5to7_size name_len; php5to7_zval column; const CassColumnMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TABLE(getThis()); meta = cass_table_meta_column_by_name(self->meta, name); if (meta == NULL) { RETURN_FALSE } column = php_driver_create_column(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(column)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(column), 0, 1); } PHP_METHOD(DefaultTable, columns) { php_driver_table *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); iterator = cass_iterator_columns_from_table_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassColumnMeta *meta; php5to7_zval zcolumn; php_driver_column *column; meta = cass_iterator_get_column_meta(iterator); zcolumn = php_driver_create_column(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zcolumn)) { column = PHP_DRIVER_GET_COLUMN(PHP5TO7_ZVAL_MAYBE_P(zcolumn)); if (PHP5TO7_Z_TYPE_MAYBE_P(column->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(column->name), PHP5TO7_Z_STRLEN_MAYBE_P(column->name) + 1, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zcolumn)); } } } cass_iterator_free(iterator); } PHP_METHOD(DefaultTable, partitionKey) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->partition_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->partition_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->partition_key)); populate_partition_key(self, PHP5TO7_ZVAL_MAYBE_P(self->partition_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->partition_key), 1, 0); } PHP_METHOD(DefaultTable, primaryKey) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->primary_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->primary_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->primary_key)); populate_partition_key(self, PHP5TO7_ZVAL_MAYBE_P(self->primary_key) TSRMLS_CC); populate_clustering_key(self, PHP5TO7_ZVAL_MAYBE_P(self->primary_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->primary_key), 1, 0); } PHP_METHOD(DefaultTable, clusteringKey) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->clustering_key)) { PHP5TO7_ZVAL_MAYBE_MAKE(self->clustering_key); array_init(PHP5TO7_ZVAL_MAYBE_P(self->clustering_key)); populate_clustering_key(self, PHP5TO7_ZVAL_MAYBE_P(self->clustering_key) TSRMLS_CC); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->clustering_key), 1, 0); } PHP_METHOD(DefaultTable, clusteringOrder) { php_driver_table *self; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); if (PHP5TO7_ZVAL_IS_UNDEF(self->clustering_order)) { size_t i, count = cass_table_meta_clustering_key_count(self->meta); PHP5TO7_ZVAL_MAYBE_MAKE(self->clustering_order); array_init(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order)); for (i = 0; i < count; ++i) { CassClusteringOrder order = cass_table_meta_clustering_key_order(self->meta, i); switch (order) { case CASS_CLUSTERING_ORDER_ASC: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "asc"); break; case CASS_CLUSTERING_ORDER_DESC: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "desc"); break; case CASS_CLUSTERING_ORDER_NONE: PHP5TO7_ADD_NEXT_INDEX_STRING(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), "none"); break; } } } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->clustering_order), 1, 0); } PHP_METHOD(DefaultTable, index) { php_driver_table *self; char *name; php5to7_size name_len; php5to7_zval index; const CassIndexMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TABLE(getThis()); meta = cass_table_meta_index_by_name(self->meta, name); if (meta == NULL) { RETURN_FALSE; } index = php_driver_create_index(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(index)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(index), 0, 1); } PHP_METHOD(DefaultTable, indexes) { php_driver_table *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); iterator = cass_iterator_indexes_from_table_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassIndexMeta *meta; php5to7_zval zindex; meta = cass_iterator_get_index_meta(iterator); zindex = php_driver_create_index(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zindex)) { php_driver_index *index = PHP_DRIVER_GET_INDEX(PHP5TO7_ZVAL_MAYBE_P(zindex)); if (PHP5TO7_Z_TYPE_MAYBE_P(index->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(index->name), PHP5TO7_Z_STRLEN_MAYBE_P(index->name) + 1, PHP5TO7_ZVAL_MAYBE_P(zindex)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zindex)); } } } cass_iterator_free(iterator); } PHP_METHOD(DefaultTable, materializedView) { php_driver_table *self; char *name; php5to7_size name_len; php5to7_zval zview; const CassMaterializedViewMeta *meta; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TABLE(getThis()); meta = cass_table_meta_materialized_view_by_name_n(self->meta, name, name_len); if (meta == NULL) { RETURN_FALSE; } zview = php_driver_create_materialized_view(self->schema, meta TSRMLS_CC); if (PHP5TO7_ZVAL_IS_UNDEF(zview)) { return; } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(zview), 0, 1); } PHP_METHOD(DefaultTable, materializedViews) { php_driver_table *self; CassIterator *iterator; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_TABLE(getThis()); iterator = cass_iterator_materialized_views_from_table_meta(self->meta); array_init(return_value); while (cass_iterator_next(iterator)) { const CassMaterializedViewMeta *meta; php5to7_zval zview; php_driver_materialized_view *view; meta = cass_iterator_get_materialized_view_meta(iterator); zview = php_driver_create_materialized_view(self->schema, meta TSRMLS_CC); if (!PHP5TO7_ZVAL_IS_UNDEF(zview)) { view = PHP_DRIVER_GET_MATERIALIZED_VIEW(PHP5TO7_ZVAL_MAYBE_P(zview)); if (PHP5TO7_Z_TYPE_MAYBE_P(view->name) == IS_STRING) { PHP5TO7_ADD_ASSOC_ZVAL_EX(return_value, PHP5TO7_Z_STRVAL_MAYBE_P(view->name), PHP5TO7_Z_STRLEN_MAYBE_P(view->name) + 1, PHP5TO7_ZVAL_MAYBE_P(zview)); } else { add_next_index_zval(return_value, PHP5TO7_ZVAL_MAYBE_P(zview)); } } } cass_iterator_free(iterator); } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_default_table_methods[] = { PHP_ME(DefaultTable, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, option, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, options, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, comment, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, readRepairChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, localReadRepairChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, gcGraceSeconds, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, caching, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, bloomFilterFPChance, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, memtableFlushPeriodMs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, defaultTTL, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, speculativeRetry, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, indexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, compactionStrategyClassName, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, compactionStrategyOptions, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, compressionParameters, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, populateIOCacheOnFlush, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, replicateOnWrite, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, maxIndexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, minIndexInterval, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, column, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, columns, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, partitionKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, primaryKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, clusteringKey, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, clusteringOrder, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, index, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, indexes, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, materializedView, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(DefaultTable, materializedViews, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_default_table_handlers; static HashTable * php_driver_type_default_table_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_default_table_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_default_table_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_default_table_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_table *self = PHP5TO7_ZEND_OBJECT_GET(table, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->name); PHP5TO7_ZVAL_MAYBE_DESTROY(self->options); PHP5TO7_ZVAL_MAYBE_DESTROY(self->partition_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->primary_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->clustering_key); PHP5TO7_ZVAL_MAYBE_DESTROY(self->clustering_order); if (self->schema) { php_driver_del_ref(&self->schema); self->schema = NULL; } self->meta = NULL; zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_default_table_new(zend_class_entry *ce TSRMLS_DC) { php_driver_table *self = PHP5TO7_ZEND_OBJECT_ECALLOC(table, ce); PHP5TO7_ZVAL_UNDEF(self->name); PHP5TO7_ZVAL_UNDEF(self->options); PHP5TO7_ZVAL_UNDEF(self->partition_key); PHP5TO7_ZVAL_UNDEF(self->primary_key); PHP5TO7_ZVAL_UNDEF(self->clustering_key); PHP5TO7_ZVAL_UNDEF(self->clustering_order); self->meta = NULL; self->schema = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(table, default_table, self, ce); } void php_driver_define_DefaultTable(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\DefaultTable", php_driver_default_table_methods); php_driver_default_table_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_default_table_ce TSRMLS_CC, 1, php_driver_table_ce); php_driver_default_table_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_default_table_ce->create_object = php_driver_default_table_new; memcpy(&php_driver_default_table_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_default_table_handlers.get_properties = php_driver_default_table_properties; #if PHP_VERSION_ID >= 50400 php_driver_default_table_handlers.get_gc = php_driver_type_default_table_gc; #endif php_driver_default_table_handlers.compare_objects = php_driver_default_table_compare; php_driver_default_table_handlers.clone_obj = NULL; } cassandra-1.3.0/src/DefaultTable.h0000644000076500000240000000154013062515664017435 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_DEFAULT_TABLE_H #define PHP_DRIVER_DEFAULT_TABLE_H #include "php_driver.h" php5to7_zval php_driver_create_table(php_driver_ref* schema, const CassTableMeta *meta TSRMLS_DC); #endif /* PHP_DRIVER_DEFAULT_TABLE_H */ cassandra-1.3.0/src/Duration.c0000644000076500000240000002331413062515664016664 0ustar michaelpenickstaff#include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #include "Duration.h" #if !defined(HAVE_STDINT_H) && !defined(_MSC_STDINT_H_) # define INT32_MAX 2147483647L # define INT32_MIN (-INT32_MAX-1) #endif zend_class_entry *php_driver_duration_ce = NULL; static void to_string(zval *result, cass_int32_t value) { char *string; spprintf(&string, 0, "%d", value); PHP5TO7_ZVAL_STRING(result, string); efree(string); } static int get_int32(zval* value, cass_int32_t* destination, const char* param_name TSRMLS_DC) { // Adapted from Bigint __construct method. if (Z_TYPE_P(value) == IS_LONG) { cass_int64_t long_value = Z_LVAL_P(value); if (long_value > INT32_MAX || long_value < INT32_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "%s must be between %d and %d, " LL_FORMAT " given", param_name, INT32_MIN, INT32_MAX, long_value); return 0; } *destination = long_value; } else if (Z_TYPE_P(value) == IS_DOUBLE) { double double_value = Z_DVAL_P(value); if (double_value > INT32_MAX || double_value < INT32_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "%s must be between %d and %d, %g given", param_name, INT32_MIN, INT32_MAX, double_value); return 0; } *destination = (cass_int32_t) double_value; } else if (Z_TYPE_P(value) == IS_STRING) { cass_int64_t parsed_big_int; if (!php_driver_parse_bigint(Z_STRVAL_P(value), Z_STRLEN_P(value), &parsed_big_int TSRMLS_CC)) { return 0; } if (parsed_big_int > INT32_MAX || parsed_big_int < INT32_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "%s must be between %d and %d, " LL_FORMAT " given", param_name, INT32_MIN, INT32_MAX, parsed_big_int); return 0; } *destination = (cass_int32_t) parsed_big_int; } else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_bigint_ce TSRMLS_CC)) { php_driver_numeric *bigint = PHP_DRIVER_GET_NUMERIC(value); cass_int64_t bigint_value = bigint->data.bigint.value; if (bigint_value > INT32_MAX || bigint_value < INT32_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "%s must be between %d and %d, " LL_FORMAT " given", param_name, INT32_MIN, INT32_MAX, bigint_value); return 0; } *destination = (cass_int32_t) bigint_value; } else { throw_invalid_argument(value, param_name, "a long, a double, a numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Bigint" TSRMLS_CC); return 0; } return 1; } char *php_driver_duration_to_string(php_driver_duration *duration) { // String representation of Duration is of the form -?MmoDdNns, for int M, D, N. // Negative durations lead with a minus sign. So (-3, -2, -1) results in // -3mo2d1ns. char* rep; int is_negative = 0; cass_int32_t final_months = duration->months; cass_int32_t final_days = duration->days; cass_int32_t final_nanos = duration->nanos; is_negative = final_months < 0 || final_days < 0 || final_nanos < 0; if (final_months < 0) final_months = -final_months; if (final_days < 0) final_days = -final_days; if (final_nanos < 0) final_nanos = -final_nanos; spprintf(&rep, 0, "%s%dmo%dd%dns", is_negative ? "-" : "", final_months, final_days, final_nanos); return rep; } void php_driver_duration_init(INTERNAL_FUNCTION_PARAMETERS) { zval *months, *days, *nanos; php_driver_duration *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &months, &days, &nanos) == FAILURE) { return; } self = PHP_DRIVER_GET_DURATION(getThis()); if (!get_int32(months, &self->months, "months" TSRMLS_CC)) { return; } if (!get_int32(days, &self->days, "days" TSRMLS_CC)) { return; } // No need to check the result of nanos parsing; get_int64 sets the exception if there's // a failure, and we have no more work to do anyway. get_int32(nanos, &self->nanos, "nanos" TSRMLS_CC); // Verify that all three attributes are non-negative or non-positive. if (!(self->months <= 0 && self->days <= 0 && self->nanos <=0) && !(self->months >= 0 && self->days >= 0 && self->nanos >=0)) { zend_throw_exception_ex(spl_ce_BadFunctionCallException, 0 TSRMLS_CC, "%s", "A duration must have all non-negative or non-positive attributes" ); } } PHP_METHOD(Duration, __construct) { php_driver_duration_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } PHP_METHOD(Duration, __toString) { char* rep; php_driver_duration *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_DURATION(getThis()); // Build up string representation of this duration. rep = php_driver_duration_to_string(self); PHP5TO7_RETVAL_STRING(rep); efree(rep); } PHP_METHOD(Duration, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_DURATION TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } PHP_METHOD(Duration, months) { php_driver_duration *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_DURATION(getThis()); to_string(return_value, self->months); } PHP_METHOD(Duration, days) { php_driver_duration *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_DURATION(getThis()); to_string(return_value, self->days); } PHP_METHOD(Duration, nanos) { php_driver_duration *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_DURATION(getThis()); to_string(return_value, self->nanos); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 3) ZEND_ARG_INFO(0, months) ZEND_ARG_INFO(0, days) ZEND_ARG_INFO(0, nanos) ZEND_END_ARG_INFO() static zend_function_entry php_driver_duration_methods[] = { PHP_ME(Duration, __construct, arginfo__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Duration, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Duration, months, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Duration, days, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Duration, nanos, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Duration, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_duration_handlers; static HashTable * php_driver_duration_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); php_driver_duration *self = PHP_DRIVER_GET_DURATION(object); php5to7_zval wrapped_months, wrapped_days, wrapped_nanos; PHP5TO7_ZVAL_MAYBE_MAKE(wrapped_months); PHP5TO7_ZVAL_MAYBE_MAKE(wrapped_days); PHP5TO7_ZVAL_MAYBE_MAKE(wrapped_nanos); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(wrapped_months), self->months); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(wrapped_days), self->days); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(wrapped_nanos), self->nanos); PHP5TO7_ZEND_HASH_UPDATE(props, "months", sizeof("months"), PHP5TO7_ZVAL_MAYBE_P(wrapped_months), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "days", sizeof("days"), PHP5TO7_ZVAL_MAYBE_P(wrapped_days), sizeof(zval)); PHP5TO7_ZEND_HASH_UPDATE(props, "nanos", sizeof("nanos"), PHP5TO7_ZVAL_MAYBE_P(wrapped_nanos), sizeof(zval)); return props; } static int php_driver_duration_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_duration *left, *right; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ left = PHP_DRIVER_GET_DURATION(obj1); right = PHP_DRIVER_GET_DURATION(obj2); // Comparisons compare months, then days, then nanos. if (left->months < right->months) return -1; if (left->months > right->months) return 1; // months are the same; compare days. if (left->days < right->days) return -1; if (left->days > right->days) return 1; // days are the same; compare nanos. if (left->nanos < right->nanos) return -1; return (left->nanos == right->nanos) ? 0 : 1; } static unsigned php_driver_duration_hash_value(zval *obj TSRMLS_DC) { php_driver_duration *self = PHP_DRIVER_GET_DURATION(obj); unsigned hashv = 0; hashv = php_driver_combine_hash(hashv, (unsigned) self->months); hashv = php_driver_combine_hash(hashv, (unsigned) self->days); hashv = php_driver_combine_hash(hashv, (unsigned) self->nanos); return hashv; } static void php_driver_duration_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_duration *self = PHP5TO7_ZEND_OBJECT_GET(duration, object); /* Clean up */ zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_duration_new(zend_class_entry *ce TSRMLS_DC) { php_driver_duration *self = PHP5TO7_ZEND_OBJECT_ECALLOC(duration, ce); PHP5TO7_ZEND_OBJECT_INIT(duration, self, ce); } void php_driver_define_Duration(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Duration", php_driver_duration_methods); php_driver_duration_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_duration_ce TSRMLS_CC, 1, php_driver_value_ce); php_driver_duration_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_duration_ce->create_object = php_driver_duration_new; memcpy(&php_driver_duration_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_duration_handlers.std.get_properties = php_driver_duration_properties; php_driver_duration_handlers.std.compare_objects = php_driver_duration_compare; php_driver_duration_handlers.hash_value = php_driver_duration_hash_value; php_driver_duration_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Duration.h0000644000076500000240000000020213062515664016660 0ustar michaelpenickstaff#ifndef PHP_DRIVER_DURATION_H #define PHP_DRIVER_DURATION_H void php_driver_duration_init(INTERNAL_FUNCTION_PARAMETERS); #endif cassandra-1.3.0/src/Exception.c0000644000076500000240000000200313062515664017025 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_exception_ce = NULL; static zend_function_entry Exception_methods[] = { PHP_FE_END }; void php_driver_define_Exception(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception", Exception_methods); php_driver_exception_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_exception_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Exception/AlreadyExistsException.c0000644000076500000240000000215313062515664023473 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_already_exists_exception_ce = NULL; static zend_function_entry AlreadyExistsException_methods[] = { PHP_FE_END }; void php_driver_define_AlreadyExistsException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\AlreadyExistsException", AlreadyExistsException_methods); php_driver_already_exists_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_configuration_exception_ce); } cassandra-1.3.0/src/Exception/AuthenticationException.c0000644000076500000240000000215113062515664023667 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_authentication_exception_ce = NULL; static zend_function_entry AuthenticationException_methods[] = { PHP_FE_END }; void php_driver_define_AuthenticationException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\AuthenticationException", AuthenticationException_methods); php_driver_authentication_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/ConfigurationException.c0000644000076500000240000000214613062515664023523 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_configuration_exception_ce = NULL; static zend_function_entry ConfigurationException_methods[] = { PHP_FE_END }; void php_driver_define_ConfigurationException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ConfigurationException", ConfigurationException_methods); php_driver_configuration_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_validation_exception_ce); } cassandra-1.3.0/src/Exception/DivideByZeroException.c0000644000076500000240000000230613062515664023251 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_divide_by_zero_exception_ce = NULL; static zend_function_entry DivideByZeroException_methods[] = { PHP_FE_END }; void php_driver_define_DivideByZeroException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\DivideByZeroException", DivideByZeroException_methods); php_driver_divide_by_zero_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_range_exception_ce); zend_class_implements(php_driver_divide_by_zero_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/DomainException.c0000644000076500000240000000221713062515664022122 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_domain_exception_ce = NULL; static zend_function_entry DomainException_methods[] = { PHP_FE_END }; void php_driver_define_DomainException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\DomainException", DomainException_methods); php_driver_domain_exception_ce = php5to7_zend_register_internal_class_ex(&ce, spl_ce_DomainException); zend_class_implements(php_driver_domain_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/ExecutionException.c0000644000076500000240000000211313062515664022651 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_execution_exception_ce = NULL; static zend_function_entry ExecutionException_methods[] = { PHP_FE_END }; void php_driver_define_ExecutionException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ExecutionException", ExecutionException_methods); php_driver_execution_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/InvalidArgumentException.c0000644000076500000240000000233213062515664024002 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_invalid_argument_exception_ce = NULL; static zend_function_entry InvalidArgumentException_methods[] = { PHP_FE_END }; void php_driver_define_InvalidArgumentException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\InvalidArgumentException", InvalidArgumentException_methods); php_driver_invalid_argument_exception_ce = php5to7_zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException); zend_class_implements(php_driver_invalid_argument_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/InvalidQueryException.c0000644000076500000240000000214213062515664023324 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_invalid_query_exception_ce = NULL; static zend_function_entry InvalidQueryException_methods[] = { PHP_FE_END }; void php_driver_define_InvalidQueryException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\InvalidQueryException", InvalidQueryException_methods); php_driver_invalid_query_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_validation_exception_ce); } cassandra-1.3.0/src/Exception/InvalidSyntaxException.c0000644000076500000240000000215013062515664023504 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_invalid_syntax_exception_ce = NULL; static zend_function_entry InvalidSyntaxException_methods[] = { PHP_FE_END }; void php_driver_define_InvalidSyntaxException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\InvalidSyntaxException", InvalidSyntaxException_methods); php_driver_invalid_syntax_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_validation_exception_ce); } cassandra-1.3.0/src/Exception/IsBootstrappingException.c0000644000076500000240000000216013062515664024037 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_is_bootstrapping_exception_ce = NULL; static zend_function_entry IsBootstrappingException_methods[] = { PHP_FE_END }; void php_driver_define_IsBootstrappingException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\IsBootstrappingException", IsBootstrappingException_methods); php_driver_is_bootstrapping_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_server_exception_ce); } cassandra-1.3.0/src/Exception/LogicException.c0000644000076500000240000000220713062515664021747 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_logic_exception_ce = NULL; static zend_function_entry LogicException_methods[] = { PHP_FE_END }; void php_driver_define_LogicException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\LogicException", LogicException_methods); php_driver_logic_exception_ce = php5to7_zend_register_internal_class_ex(&ce, spl_ce_LogicException); zend_class_implements(php_driver_logic_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/OverloadedException.c0000644000076500000240000000212013062515664022770 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_overloaded_exception_ce = NULL; static zend_function_entry OverloadedException_methods[] = { PHP_FE_END }; void php_driver_define_OverloadedException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\OverloadedException", OverloadedException_methods); php_driver_overloaded_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_server_exception_ce); } cassandra-1.3.0/src/Exception/ProtocolException.c0000644000076500000240000000210513062515664022510 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_protocol_exception_ce = NULL; static zend_function_entry ProtocolException_methods[] = { PHP_FE_END }; void php_driver_define_ProtocolException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ProtocolException", ProtocolException_methods); php_driver_protocol_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/RangeException.c0000644000076500000240000000225413062515664021750 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include zend_class_entry *php_driver_range_exception_ce = NULL; static zend_function_entry RangeException_methods[] = { PHP_FE_END }; void php_driver_define_RangeException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\RangeException", RangeException_methods); php_driver_range_exception_ce = php5to7_zend_register_internal_class_ex(&ce, spl_ce_RangeException); zend_class_implements(php_driver_range_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/ReadTimeoutException.c0000644000076500000240000000213313062515664023132 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_read_timeout_exception_ce = NULL; static zend_function_entry ReadTimeoutException_methods[] = { PHP_FE_END }; void php_driver_define_ReadTimeoutException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ReadTimeoutException", ReadTimeoutException_methods); php_driver_read_timeout_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_execution_exception_ce); } cassandra-1.3.0/src/Exception/RuntimeException.c0000644000076500000240000000222713062515664022337 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_runtime_exception_ce = NULL; static zend_function_entry RuntimeException_methods[] = { PHP_FE_END }; void php_driver_define_RuntimeException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\RuntimeException", RuntimeException_methods); php_driver_runtime_exception_ce = php5to7_zend_register_internal_class_ex(&ce, spl_ce_RuntimeException); zend_class_implements(php_driver_runtime_exception_ce TSRMLS_CC, 1, php_driver_exception_ce); } cassandra-1.3.0/src/Exception/ServerException.c0000644000076500000240000000207113062515664022157 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_server_exception_ce = NULL; static zend_function_entry ServerException_methods[] = { PHP_FE_END }; void php_driver_define_ServerException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ServerException", ServerException_methods); php_driver_server_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/TimeoutException.c0000644000076500000240000000213413062515664022337 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" extern zend_class_entry* php_driver_runtime_exception_ce; zend_class_entry *php_driver_timeout_exception_ce = NULL; static zend_function_entry TimeoutException_methods[] = { PHP_FE_END }; void php_driver_define_TimeoutException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\TimeoutException", TimeoutException_methods); php_driver_timeout_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/TruncateException.c0000644000076500000240000000210713062515664022476 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_truncate_exception_ce = NULL; static zend_function_entry TruncateException_methods[] = { PHP_FE_END }; void php_driver_define_TruncateException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\TruncateException", TruncateException_methods); php_driver_truncate_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_execution_exception_ce); } cassandra-1.3.0/src/Exception/UnauthorizedException.c0000644000076500000240000000214013062515664023367 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_unauthorized_exception_ce = NULL; static zend_function_entry UnauthorizedException_methods[] = { PHP_FE_END }; void php_driver_define_UnauthorizedException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\UnauthorizedException", UnauthorizedException_methods); php_driver_unauthorized_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_validation_exception_ce); } cassandra-1.3.0/src/Exception/UnavailableException.c0000644000076500000240000000213113062515664023131 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_unavailable_exception_ce = NULL; static zend_function_entry UnavailableException_methods[] = { PHP_FE_END }; void php_driver_define_UnavailableException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\UnavailableException", UnavailableException_methods); php_driver_unavailable_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_execution_exception_ce); } cassandra-1.3.0/src/Exception/UnpreparedException.c0000644000076500000240000000212413062515664023015 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_unprepared_exception_ce = NULL; static zend_function_entry UnpreparedException_methods[] = { PHP_FE_END }; void php_driver_define_UnpreparedException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\UnpreparedException", UnpreparedException_methods); php_driver_unprepared_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_validation_exception_ce); } cassandra-1.3.0/src/Exception/ValidationException.c0000644000076500000240000000212113062515664022777 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_validation_exception_ce = NULL; static zend_function_entry ValidationException_methods[] = { PHP_FE_END }; void php_driver_define_ValidationException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\ValidationException", ValidationException_methods); php_driver_validation_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_runtime_exception_ce); } cassandra-1.3.0/src/Exception/WriteTimeoutException.c0000644000076500000240000000214113062515664023350 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_write_timeout_exception_ce = NULL; static zend_function_entry WriteTimeoutException_methods[] = { PHP_FE_END }; void php_driver_define_WriteTimeoutException(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Exception\\WriteTimeoutException", WriteTimeoutException_methods); php_driver_write_timeout_exception_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_execution_exception_ce); } cassandra-1.3.0/src/ExecutionOptions.c0000644000076500000240000002606713062515664020426 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/consistency.h" #include "util/math.h" zend_class_entry *php_driver_execution_options_ce = NULL; static void init_execution_options(php_driver_execution_options *self) { self->consistency = -1; self->serial_consistency = -1; self->page_size = -1; self->paging_state_token = NULL; self->paging_state_token_size = 0; self->timestamp = INT64_MIN; PHP5TO7_ZVAL_UNDEF(self->arguments); PHP5TO7_ZVAL_UNDEF(self->timeout); PHP5TO7_ZVAL_UNDEF(self->retry_policy); } static int build_from_array(php_driver_execution_options *self, zval *options, int copy TSRMLS_DC) { php5to7_zval *consistency = NULL; php5to7_zval *serial_consistency = NULL; php5to7_zval *page_size = NULL; php5to7_zval *paging_state_token = NULL; php5to7_zval *timeout = NULL; php5to7_zval *arguments = NULL; php5to7_zval *retry_policy = NULL; php5to7_zval *timestamp = NULL; if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "consistency", sizeof("consistency"), consistency)) { if (php_driver_get_consistency(PHP5TO7_ZVAL_MAYBE_DEREF(consistency), &self->consistency TSRMLS_CC) == FAILURE) { return FAILURE; } } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "serial_consistency", sizeof("serial_consistency"), serial_consistency)) { if (php_driver_get_serial_consistency(PHP5TO7_ZVAL_MAYBE_DEREF(serial_consistency), &self->serial_consistency TSRMLS_CC) == FAILURE) { return FAILURE; } } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "page_size", sizeof("page_size"), page_size)) { if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(page_size)) != IS_LONG || Z_LVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(page_size)) <= 0) { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(page_size), "page_size", "greater than zero" TSRMLS_CC); return FAILURE; } self->page_size = Z_LVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(page_size)); } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "paging_state_token", sizeof("paging_state_token"), paging_state_token)) { if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token)) != IS_STRING) { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token), "paging_state_token", "a string" TSRMLS_CC); return FAILURE; } if (copy) { self->paging_state_token = estrndup(Z_STRVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token)), Z_STRLEN_P(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token))); } else { self->paging_state_token = Z_STRVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token)); } self->paging_state_token_size = Z_STRLEN_P(PHP5TO7_ZVAL_MAYBE_DEREF(paging_state_token)); } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "timeout", sizeof("timeout"), timeout)) { if (!(Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(timeout)) == IS_LONG && Z_LVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(timeout)) > 0) && !(Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(timeout)) == IS_DOUBLE && Z_DVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(timeout)) > 0) && !(Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(timeout)) == IS_NULL)) { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(timeout), "timeout", "a number of seconds greater than zero or null" TSRMLS_CC); return FAILURE; } if (copy) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->timeout), PHP5TO7_ZVAL_MAYBE_DEREF(timeout)); } else { self->timeout = *timeout; } } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "arguments", sizeof("arguments"), arguments)) { if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(arguments)) != IS_ARRAY) { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(arguments), "arguments", "an array" TSRMLS_CC); return FAILURE; } if (copy) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->arguments), PHP5TO7_ZVAL_MAYBE_DEREF(arguments)); } else { self->arguments = *arguments; } } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "retry_policy", sizeof("retry_policy"), retry_policy)) { if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(retry_policy)) != IS_OBJECT && !instanceof_function(Z_OBJCE_P(PHP5TO7_ZVAL_MAYBE_DEREF(retry_policy)), php_driver_retry_policy_ce TSRMLS_CC)) { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(retry_policy), "retry_policy", "an instance of " PHP_DRIVER_NAMESPACE "\\RetryPolicy" TSRMLS_CC); return FAILURE; } if (copy) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->retry_policy), PHP5TO7_ZVAL_MAYBE_DEREF(retry_policy)); } else { self->retry_policy = *retry_policy; } } if (PHP5TO7_ZEND_HASH_FIND(Z_ARRVAL_P(options), "timestamp", sizeof("timestamp"), timestamp)) { if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp)) == IS_LONG) { self->timestamp = Z_LVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp)); } else if (Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp)) == IS_STRING) { if (!php_driver_parse_bigint(Z_STRVAL_P(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp)), Z_STRLEN_P(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp)), &self->timestamp TSRMLS_CC)) { return FAILURE; } } else { throw_invalid_argument(PHP5TO7_ZVAL_MAYBE_DEREF(timestamp), "timestamp", "an integer or integer string" TSRMLS_CC); return FAILURE; } } return SUCCESS; } int php_driver_execution_options_build_local_from_array(php_driver_execution_options *self, zval *options TSRMLS_DC) { init_execution_options(self); return build_from_array(self, options, 0 TSRMLS_CC); } PHP_METHOD(ExecutionOptions, __construct) { zval *options = NULL; php_driver_execution_options *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &options) == FAILURE) { return; } if (!options) return; if (Z_TYPE_P(options) != IS_ARRAY) { INVALID_ARGUMENT(options, "an array"); } self = PHP_DRIVER_GET_EXECUTION_OPTIONS(getThis()); build_from_array(self, options, 1 TSRMLS_CC); } PHP_METHOD(ExecutionOptions, __get) { char *name; php5to7_size name_len; php_driver_execution_options *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_EXECUTION_OPTIONS(getThis()); if (name_len == 11 && strncmp("consistency", name, name_len) == 0) { if (self->consistency == -1) { RETURN_NULL(); } RETURN_LONG(self->consistency); } else if (name_len == 17 && strncmp("serialConsistency", name, name_len) == 0) { if (self->serial_consistency == -1) { RETURN_NULL(); } RETURN_LONG(self->serial_consistency); } else if (name_len == 8 && strncmp("pageSize", name, name_len) == 0) { if (self->page_size == -1) { RETURN_NULL(); } RETURN_LONG(self->page_size); } else if (name_len == 16 && strncmp("pagingStateToken", name, name_len) == 0) { if (!self->paging_state_token) { RETURN_NULL(); } PHP5TO7_RETURN_STRINGL(self->paging_state_token, self->paging_state_token_size); } else if (name_len == 7 && strncmp("timeout", name, name_len) == 0) { if (PHP5TO7_ZVAL_IS_UNDEF(self->timeout)) { RETURN_NULL(); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->timeout), 1, 0); } else if (name_len == 9 && strncmp("arguments", name, name_len) == 0) { if (PHP5TO7_ZVAL_IS_UNDEF(self->arguments)) { RETURN_NULL(); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->arguments), 1, 0); } else if (name_len == 11 && strncmp("retryPolicy", name, name_len) == 0) { if (PHP5TO7_ZVAL_IS_UNDEF(self->retry_policy)) { RETURN_NULL(); } RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->retry_policy), 1, 0); } else if (name_len == 9 && strncmp("timestamp", name, name_len) == 0) { char *string; if (self->timestamp == INT64_MIN) { RETURN_NULL(); } #ifdef WIN32 spprintf(&string, 0, "%I64d", (long long int) self->timestamp); #else spprintf(&string, 0, "%lld", (long long int) self->timestamp); #endif PHP5TO7_RETVAL_STRING(string); efree(string); } } ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_ARRAY_INFO(0, options, 1) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo___get, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() static zend_function_entry php_driver_execution_options_methods[] = { PHP_ME(ExecutionOptions, __construct, arginfo__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR | ZEND_ACC_DEPRECATED) PHP_ME(ExecutionOptions, __get, arginfo___get, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_execution_options_handlers; static HashTable * php_driver_execution_options_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_execution_options_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_execution_options_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_execution_options *self = PHP5TO7_ZEND_OBJECT_GET(execution_options, object); if (self->paging_state_token) { efree(self->paging_state_token); } PHP5TO7_ZVAL_MAYBE_DESTROY(self->arguments); PHP5TO7_ZVAL_MAYBE_DESTROY(self->timeout); PHP5TO7_ZVAL_MAYBE_DESTROY(self->retry_policy); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_execution_options_new(zend_class_entry *ce TSRMLS_DC) { php_driver_execution_options *self = PHP5TO7_ZEND_OBJECT_ECALLOC(execution_options, ce); init_execution_options(self); PHP5TO7_ZEND_OBJECT_INIT(execution_options, self, ce); } void php_driver_define_ExecutionOptions(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\ExecutionOptions", php_driver_execution_options_methods); php_driver_execution_options_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_execution_options_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_execution_options_ce->create_object = php_driver_execution_options_new; memcpy(&php_driver_execution_options_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_execution_options_handlers.get_properties = php_driver_execution_options_properties; php_driver_execution_options_handlers.compare_objects = php_driver_execution_options_compare; php_driver_execution_options_handlers.clone_obj = NULL; } cassandra-1.3.0/src/ExecutionOptions.h0000644000076500000240000000031513062515664020417 0ustar michaelpenickstaff#ifndef PHP_DRIVER_EXECUTION_OPTIONS_H #define PHP_DRIVER_EXECUTION_OPTIONS_H int php_driver_execution_options_build_local_from_array(php_driver_execution_options *self, zval *options TSRMLS_DC); #endif cassandra-1.3.0/src/Float.c0000644000076500000240000003513513062515664016150 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/math.h" #include "util/types.h" #include zend_class_entry *php_driver_float_ce = NULL; static int to_string(zval *result, php_driver_numeric *flt TSRMLS_DC) { char *string; spprintf(&string, 0, "%.*F", (int) EG(precision), flt->data.floating.value); PHP5TO7_ZVAL_STRING(result, string); efree(string); return SUCCESS; } void php_driver_float_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_numeric *self; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_float_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_float_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(value) == IS_LONG) { self->data.floating.value = (cass_float_t) Z_LVAL_P(value); } else if (Z_TYPE_P(value) == IS_DOUBLE) { self->data.floating.value = (cass_float_t) Z_DVAL_P(value); } else if (Z_TYPE_P(value) == IS_STRING) { if (!php_driver_parse_float(Z_STRVAL_P(value), Z_STRLEN_P(value), &self->data.floating.value TSRMLS_CC)) { return; } } else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(return_value); self->data.floating.value = flt->data.floating.value; } else { INVALID_ARGUMENT(value, "a long, double, numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Float instance"); } } /* {{{ Float::__construct(string) */ PHP_METHOD(Float, __construct) { php_driver_float_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Float::__toString() */ PHP_METHOD(Float, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Float::type() */ PHP_METHOD(Float, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_FLOAT TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Float::value() */ PHP_METHOD(Float, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_DOUBLE((double) self->data.floating.value); } /* }}} */ /* {{{ Float::isInfinite() */ PHP_METHOD(Float, isInfinite) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_BOOL(zend_isinf(self->data.floating.value)); } /* }}} */ /* {{{ Float::isFinite() */ PHP_METHOD(Float, isFinite) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_BOOL(zend_finite(self->data.floating.value)); } /* }}} */ /* {{{ Float::isNaN() */ PHP_METHOD(Float, isNaN) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_BOOL(zend_isnan(self->data.floating.value)); } /* }}} */ /* {{{ Float::add() */ PHP_METHOD(Float, add) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = self->data.floating.value + flt->data.floating.value; } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } } /* }}} */ /* {{{ Float::sub() */ PHP_METHOD(Float, sub) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = self->data.floating.value - flt->data.floating.value; } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } } /* }}} */ /* {{{ Float::mul() */ PHP_METHOD(Float, mul) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = self->data.floating.value * flt->data.floating.value; } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } } /* }}} */ /* {{{ Float::div() */ PHP_METHOD(Float, div) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (flt->data.floating.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } result->data.floating.value = self->data.floating.value / flt->data.floating.value; } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } } /* }}} */ /* {{{ Float::mod() */ PHP_METHOD(Float, mod) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_float_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *flt = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (flt->data.floating.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } result->data.floating.value = fmod(self->data.floating.value, flt->data.floating.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } } /* {{{ Float::abs() */ PHP_METHOD(Float, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = fabsf(self->data.floating.value); } /* }}} */ /* {{{ Float::neg() */ PHP_METHOD(Float, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = -self->data.floating.value; } /* }}} */ /* {{{ Float::sqrt() */ PHP_METHOD(Float, sqrt) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.floating.value < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Cannot take a square root of a negative number"); } object_init_ex(return_value, php_driver_float_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.floating.value = sqrtf(self->data.floating.value); } /* }}} */ /* {{{ Float::toInt() */ PHP_METHOD(Float, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_LONG((long) self->data.floating.value); } /* }}} */ /* {{{ Float::toDouble() */ PHP_METHOD(Float, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); RETURN_DOUBLE((double) self->data.floating.value); } /* }}} */ /* {{{ Float::min() */ PHP_METHOD(Float, min) { php_driver_numeric *flt = NULL; object_init_ex(return_value, php_driver_float_ce); flt = PHP_DRIVER_GET_NUMERIC(return_value); flt->data.floating.value = FLT_MIN; } /* }}} */ /* {{{ Float::max() */ PHP_METHOD(Float, max) { php_driver_numeric *flt = NULL; object_init_ex(return_value, php_driver_float_ce); flt = PHP_DRIVER_GET_NUMERIC(return_value); flt->data.floating.value = FLT_MAX; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_float_methods[] = { PHP_ME(Float, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Float, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, isInfinite, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, isFinite, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, isNaN, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Float, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Float, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Float, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Float, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Float, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Float, min, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Float, max, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; static php_driver_value_handlers php_driver_float_handlers; static HashTable * php_driver_float_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_float_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval value; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_FLOAT TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(value); to_string(PHP5TO7_ZVAL_MAYBE_P(value), self TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); return props; } static inline cass_int32_t float_to_bits(cass_float_t value) { cass_int32_t bits; if (zend_isnan(value)) return 0x7fc00000; /* A canonical NaN value */ memcpy(&bits, &value, sizeof(cass_int32_t)); return bits; } static int php_driver_float_compare(zval *obj1, zval *obj2 TSRMLS_DC) { cass_int32_t bits1, bits2; php_driver_numeric *flt1 = NULL; php_driver_numeric *flt2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ flt1 = PHP_DRIVER_GET_NUMERIC(obj1); flt2 = PHP_DRIVER_GET_NUMERIC(obj2); if (flt1->data.floating.value < flt2->data.floating.value) return -1; if (flt1->data.floating.value > flt2->data.floating.value) return 1; bits1 = float_to_bits(flt1->data.floating.value); bits2 = float_to_bits(flt2->data.floating.value); /* Handle NaNs and negative and positive 0.0 */ return bits1 < bits2 ? -1 : bits1 > bits2; } static unsigned php_driver_float_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return float_to_bits(self->data.floating.value); } static int php_driver_float_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: ZVAL_LONG(retval, (long) self->data.floating.value); return SUCCESS; case IS_DOUBLE: ZVAL_DOUBLE(retval, (double) self->data.floating.value); return SUCCESS; case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_float_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_float_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, float, self, ce); } void php_driver_define_Float(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Float", php_driver_float_methods); php_driver_float_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_float_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_float_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_float_ce->create_object = php_driver_float_new; memcpy(&php_driver_float_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_float_handlers.std.get_properties = php_driver_float_properties; #if PHP_VERSION_ID >= 50400 php_driver_float_handlers.std.get_gc = php_driver_float_gc; #endif php_driver_float_handlers.std.compare_objects = php_driver_float_compare; php_driver_float_handlers.std.cast_object = php_driver_float_cast; php_driver_float_handlers.hash_value = php_driver_float_hash_value; php_driver_float_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Float.h0000644000076500000240000000135313062515664016150 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_FLOAT_H #define PHP_DRIVER_FLOAT_H void php_driver_float_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_FLOAT_H */ cassandra-1.3.0/src/Function.c0000644000076500000240000000301613062515664016661 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_function_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_function_methods[] = { PHP_ABSTRACT_ME(Function, name, arginfo_none) PHP_ABSTRACT_ME(Function, simpleName, arginfo_none) PHP_ABSTRACT_ME(Function, arguments, arginfo_none) PHP_ABSTRACT_ME(Function, returnType, arginfo_none) PHP_ABSTRACT_ME(Function, signature, arginfo_none) PHP_ABSTRACT_ME(Function, language, arginfo_none) PHP_ABSTRACT_ME(Function, body, arginfo_none) PHP_ABSTRACT_ME(Function, isCalledOnNullInput, arginfo_none) PHP_FE_END }; void php_driver_define_Function(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Function", php_driver_function_methods); php_driver_function_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_function_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Future.c0000644000076500000240000000224613062515664016352 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_future_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_methods[] = { PHP_ABSTRACT_ME(Future, get, arginfo_timeout) PHP_FE_END }; void php_driver_define_Future(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Future", php_driver_future_methods); php_driver_future_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_future_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/FutureClose.c0000644000076500000240000000624413062515664017342 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/future.h" zend_class_entry *php_driver_future_close_ce = NULL; PHP_METHOD(FutureClose, get) { zval *timeout = NULL; php_driver_future_close *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) return; self = PHP_DRIVER_GET_FUTURE_CLOSE(getThis()); if (php_driver_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) return; if (php_driver_future_is_error(self->future TSRMLS_CC) == FAILURE) return; } ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_close_methods[] = { PHP_ME(FutureClose, get, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_future_close_handlers; static HashTable * php_driver_future_close_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_future_close_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_future_close_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_future_close *self = PHP5TO7_ZEND_OBJECT_GET(future_close, object); if (self->future) cass_future_free(self->future); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_future_close_new(zend_class_entry *ce TSRMLS_DC) { php_driver_future_close *self = PHP5TO7_ZEND_OBJECT_ECALLOC(future_close, ce); self->future = NULL; PHP5TO7_ZEND_OBJECT_INIT(future_close, self, ce); } void php_driver_define_FutureClose(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\FutureClose", php_driver_future_close_methods); php_driver_future_close_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_future_close_ce TSRMLS_CC, 1, php_driver_future_ce); php_driver_future_close_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_future_close_ce->create_object = php_driver_future_close_new; memcpy(&php_driver_future_close_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_future_close_handlers.get_properties = php_driver_future_close_properties; php_driver_future_close_handlers.compare_objects = php_driver_future_close_compare; php_driver_future_close_handlers.clone_obj = NULL; } cassandra-1.3.0/src/FuturePreparedStatement.c0000644000076500000240000001021713062515664021717 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/future.h" zend_class_entry *php_driver_future_prepared_statement_ce = NULL; PHP_METHOD(FuturePreparedStatement, get) { zval *timeout = NULL; php_driver_statement *prepared_statement = NULL; php_driver_future_prepared_statement *self = PHP_DRIVER_GET_FUTURE_PREPARED_STATEMENT(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->prepared_statement)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->prepared_statement), 1, 0); } if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } if (php_driver_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) { return; } if (php_driver_future_is_error(self->future TSRMLS_CC) == FAILURE) { return; } object_init_ex(return_value, php_driver_statement_ce); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->prepared_statement), return_value); prepared_statement = PHP_DRIVER_GET_STATEMENT(return_value); prepared_statement->data.prepared.prepared = cass_future_get_prepared(self->future); } ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_prepared_statement_methods[] = { PHP_ME(FuturePreparedStatement, get, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_future_prepared_statement_handlers; static HashTable * php_driver_future_prepared_statement_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_future_prepared_statement_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_future_prepared_statement_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_future_prepared_statement *self = PHP5TO7_ZEND_OBJECT_GET(future_prepared_statement, object); if (self->future) { cass_future_free(self->future); self->future = NULL; } PHP5TO7_ZVAL_MAYBE_DESTROY(self->prepared_statement); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_future_prepared_statement_new(zend_class_entry *ce TSRMLS_DC) { php_driver_future_prepared_statement *self = PHP5TO7_ZEND_OBJECT_ECALLOC(future_prepared_statement, ce); self->future = NULL; PHP5TO7_ZVAL_UNDEF(self->prepared_statement); PHP5TO7_ZEND_OBJECT_INIT(future_prepared_statement, self, ce); } void php_driver_define_FuturePreparedStatement(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\FuturePreparedStatement", php_driver_future_prepared_statement_methods); php_driver_future_prepared_statement_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_future_prepared_statement_ce TSRMLS_CC, 1, php_driver_future_ce); php_driver_future_prepared_statement_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_future_prepared_statement_ce->create_object = php_driver_future_prepared_statement_new; memcpy(&php_driver_future_prepared_statement_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_future_prepared_statement_handlers.get_properties = php_driver_future_prepared_statement_properties; php_driver_future_prepared_statement_handlers.compare_objects = php_driver_future_prepared_statement_compare; php_driver_future_prepared_statement_handlers.clone_obj = NULL; } cassandra-1.3.0/src/FutureRows.c0000644000076500000240000001164213062515664017225 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/future.h" #include "util/result.h" #include "util/ref.h" zend_class_entry *php_driver_future_rows_ce = NULL; static void free_result(void *result) { cass_result_free((CassResult *) result); } int php_driver_future_rows_get_result(php_driver_future_rows *future_rows, zval *timeout TSRMLS_DC) { if (!future_rows->result) { const CassResult *result = NULL; if (php_driver_future_wait_timed(future_rows->future, timeout TSRMLS_CC) == FAILURE) { return FAILURE; } if (php_driver_future_is_error(future_rows->future TSRMLS_CC) == FAILURE) { return FAILURE; } result = cass_future_get_result(future_rows->future); if (!result) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Future doesn't contain a result."); return FAILURE; } future_rows->result = php_driver_new_ref((void *)result, free_result); } return SUCCESS; } PHP_METHOD(FutureRows, get) { zval *timeout = NULL; php_driver_rows *rows = NULL; php_driver_future_rows *self = PHP_DRIVER_GET_FUTURE_ROWS(getThis()); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } if (php_driver_future_rows_get_result(self, timeout TSRMLS_CC) == FAILURE) { return; } if (PHP5TO7_ZVAL_IS_UNDEF(self->rows)) { if (php_driver_get_result((const CassResult *) self->result->data, &self->rows TSRMLS_CC) == FAILURE) { PHP5TO7_ZVAL_MAYBE_DESTROY(self->rows); return; } } object_init_ex(return_value, php_driver_rows_ce); rows = PHP_DRIVER_GET_ROWS(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(rows->rows), PHP5TO7_ZVAL_MAYBE_P(self->rows)); if (cass_result_has_more_pages((const CassResult *)self->result->data)) { rows->session = php_driver_add_ref(self->session); rows->statement = php_driver_add_ref(self->statement); rows->result = php_driver_add_ref(self->result); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_rows_methods[] = { PHP_ME(FutureRows, get, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_future_rows_handlers; static HashTable * php_driver_future_rows_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_future_rows_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_future_rows_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_future_rows *self = PHP5TO7_ZEND_OBJECT_GET(future_rows, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->rows); php_driver_del_ref(&self->statement); php_driver_del_peref(&self->session, 1); php_driver_del_ref(&self->result); if (self->future) { cass_future_free(self->future); } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_future_rows_new(zend_class_entry *ce TSRMLS_DC) { php_driver_future_rows *self = PHP5TO7_ZEND_OBJECT_ECALLOC(future_rows, ce); self->future = NULL; self->statement = NULL; self->result = NULL; self->session = NULL; PHP5TO7_ZVAL_UNDEF(self->rows); PHP5TO7_ZEND_OBJECT_INIT(future_rows, self, ce); } void php_driver_define_FutureRows(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\FutureRows", php_driver_future_rows_methods); php_driver_future_rows_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_future_rows_ce TSRMLS_CC, 1, php_driver_future_ce); php_driver_future_rows_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_future_rows_ce->create_object = php_driver_future_rows_new; memcpy(&php_driver_future_rows_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_future_rows_handlers.get_properties = php_driver_future_rows_properties; php_driver_future_rows_handlers.compare_objects = php_driver_future_rows_compare; php_driver_future_rows_handlers.clone_obj = NULL; } cassandra-1.3.0/src/FutureRows.h0000644000076500000240000000145013062515664017226 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_FUTURE_ROWS_H #define PHP_DRIVER_FUTURE_ROWS_H int php_driver_future_rows_get_result(php_driver_future_rows *future_rows, zval *timeout TSRMLS_DC); #endif /* PHP_DRIVER_FUTURE_ROWS_H */ cassandra-1.3.0/src/FutureSession.c0000644000076500000240000001206713062515664017720 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "util/future.h" #include "util/ref.h" zend_class_entry *php_driver_future_session_ce = NULL; PHP_METHOD(FutureSession, get) { zval *timeout = NULL; CassError rc = CASS_OK; php_driver_session *session = NULL; php_driver_future_session *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } self = PHP_DRIVER_GET_FUTURE_SESSION(getThis()); if (self->exception_message) { zend_throw_exception_ex(exception_class(self->exception_code), self->exception_code TSRMLS_CC, self->exception_message); return; } if (!PHP5TO7_ZVAL_IS_UNDEF(self->default_session)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->default_session), 1, 0); } object_init_ex(return_value, php_driver_default_session_ce); session = PHP_DRIVER_GET_SESSION(return_value); session->session = php_driver_add_ref(self->session); session->persist = self->persist; if (php_driver_future_wait_timed(self->future, timeout TSRMLS_CC) == FAILURE) { return; } rc = cass_future_error_code(self->future); if (rc != CASS_OK) { const char *message; size_t message_len; cass_future_error_message(self->future, &message, &message_len); if (self->persist) { self->exception_message = estrndup(message, message_len); self->exception_code = rc; if (PHP5TO7_ZEND_HASH_DEL(&EG(persistent_list), self->hash_key, self->hash_key_len + 1)) { self->future = NULL; } zend_throw_exception_ex(exception_class(self->exception_code), self->exception_code TSRMLS_CC, self->exception_message); return; } zend_throw_exception_ex(exception_class(rc), rc TSRMLS_CC, "%.*s", (int) message_len, message); return; } PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(self->default_session), return_value); } ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_session_methods[] = { PHP_ME(FutureSession, get, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_future_session_handlers; static HashTable * php_driver_future_session_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_future_session_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_future_session_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_future_session *self = PHP5TO7_ZEND_OBJECT_GET(future_session, object); if (self->persist) { efree(self->hash_key); } else { if (self->future) { cass_future_free(self->future); } } php_driver_del_peref(&self->session, 1); if (self->exception_message) { efree(self->exception_message); } PHP5TO7_ZVAL_MAYBE_DESTROY(self->default_session); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_future_session_new(zend_class_entry *ce TSRMLS_DC) { php_driver_future_session *self = PHP5TO7_ZEND_OBJECT_ECALLOC(future_session, ce); self->session = NULL; self->future = NULL; self->exception_message = NULL; self->hash_key = NULL; self->persist = 0; PHP5TO7_ZVAL_UNDEF(self->default_session); PHP5TO7_ZEND_OBJECT_INIT(future_session, self, ce); } void php_driver_define_FutureSession(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\FutureSession", php_driver_future_session_methods); php_driver_future_session_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_future_session_ce TSRMLS_CC, 1, php_driver_future_ce); php_driver_future_session_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_future_session_ce->create_object = php_driver_future_session_new; memcpy(&php_driver_future_session_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_future_session_handlers.get_properties = php_driver_future_session_properties; php_driver_future_session_handlers.compare_objects = php_driver_future_session_compare; php_driver_future_session_handlers.clone_obj = NULL; } cassandra-1.3.0/src/FutureValue.c0000644000076500000240000000610713062515664017347 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_future_value_ce = NULL; PHP_METHOD(FutureValue, get) { zval *timeout = NULL; php_driver_future_value *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) return; self = PHP_DRIVER_GET_FUTURE_VALUE(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->value)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->value), 1, 0); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_future_value_methods[] = { PHP_ME(FutureValue, get, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_future_value_handlers; static HashTable * php_driver_future_value_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_future_value_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_future_value_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_future_value *self = PHP5TO7_ZEND_OBJECT_GET(future_value, object); PHP5TO7_ZVAL_MAYBE_DESTROY(self->value); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_future_value_new(zend_class_entry *ce TSRMLS_DC) { php_driver_future_value *self = PHP5TO7_ZEND_OBJECT_ECALLOC(future_value, ce); PHP5TO7_ZVAL_UNDEF(self->value); PHP5TO7_ZEND_OBJECT_INIT(future_value, self, ce); } void php_driver_define_FutureValue(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\FutureValue", php_driver_future_value_methods); php_driver_future_value_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_future_value_ce TSRMLS_CC, 1, php_driver_future_ce); php_driver_future_value_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_future_value_ce->create_object = php_driver_future_value_new; memcpy(&php_driver_future_value_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_future_value_handlers.get_properties = php_driver_future_value_properties; php_driver_future_value_handlers.compare_objects = php_driver_future_value_compare; php_driver_future_value_handlers.clone_obj = NULL; } cassandra-1.3.0/src/Index.c0000644000076500000240000000300713062515664016143 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_index_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_index_methods[] = { PHP_ABSTRACT_ME(Index, name, arginfo_none) PHP_ABSTRACT_ME(Index, kind, arginfo_none) PHP_ABSTRACT_ME(Index, target, arginfo_none) PHP_ABSTRACT_ME(Index, option, arginfo_name) PHP_ABSTRACT_ME(Index, options, arginfo_none) PHP_ABSTRACT_ME(Index, className, arginfo_none) PHP_ABSTRACT_ME(Index, isCustom, arginfo_none) PHP_FE_END }; void php_driver_define_Index(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Index", php_driver_index_methods); php_driver_index_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_index_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Inet.c0000644000076500000240000001311113062515664015770 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/inet.h" #include "util/types.h" zend_class_entry *php_driver_inet_ce = NULL; void php_driver_inet_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_inet *self; char *string; php5to7_size string_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_inet_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_INET(getThis()); } else { object_init_ex(return_value, php_driver_inet_ce); self = PHP_DRIVER_GET_INET(return_value); } if (!php_driver_parse_ip_address(string, &self->inet TSRMLS_CC)) { return; } } /* {{{ Inet::__construct(string) */ PHP_METHOD(Inet, __construct) { php_driver_inet_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Inet::__toString() */ PHP_METHOD(Inet, __toString) { php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis()); char *string; php_driver_format_address(inet->inet, &string); PHP5TO7_RETVAL_STRING(string); efree(string); } /* }}} */ /* {{{ Inet::type() */ PHP_METHOD(Inet, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Inet::address() */ PHP_METHOD(Inet, address) { php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis()); char *string; php_driver_format_address(inet->inet, &string); PHP5TO7_RETVAL_STRING(string); efree(string); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, address) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_inet_methods[] = { PHP_ME(Inet, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Inet, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Inet, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Inet, address, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_inet_handlers; static HashTable * php_driver_inet_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_inet_properties(zval *object TSRMLS_DC) { char *string; php5to7_zval type; php5to7_zval address; php_driver_inet *self = PHP_DRIVER_GET_INET(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); php_driver_format_address(self->inet, &string); PHP5TO7_ZVAL_MAYBE_MAKE(address); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(address), string); efree(string); PHP5TO7_ZEND_HASH_UPDATE(props, "address", sizeof("address"), PHP5TO7_ZVAL_MAYBE_P(address), sizeof(zval)); return props; } static int php_driver_inet_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_inet *inet1 = NULL; php_driver_inet *inet2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ inet1 = PHP_DRIVER_GET_INET(obj1); inet2 = PHP_DRIVER_GET_INET(obj2); if (inet1->inet.address_length != inet2->inet.address_length) { return inet1->inet.address_length < inet2->inet.address_length ? -1 : 1; } return memcmp(inet1->inet.address, inet2->inet.address, inet1->inet.address_length); } static unsigned php_driver_inet_hash_value(zval *obj TSRMLS_DC) { php_driver_inet *self = PHP_DRIVER_GET_INET(obj); return zend_inline_hash_func((const char *) self->inet.address, self->inet.address_length); } static void php_driver_inet_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_inet *self = PHP5TO7_ZEND_OBJECT_GET(inet, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_inet_new(zend_class_entry *ce TSRMLS_DC) { php_driver_inet *self = PHP5TO7_ZEND_OBJECT_ECALLOC(inet, ce); PHP5TO7_ZEND_OBJECT_INIT(inet, self, ce); } void php_driver_define_Inet(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Inet", php_driver_inet_methods); php_driver_inet_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_inet_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_inet_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_inet_handlers.std.get_properties = php_driver_inet_properties; #if PHP_VERSION_ID >= 50400 php_driver_inet_handlers.std.get_gc = php_driver_inet_gc; #endif php_driver_inet_handlers.std.compare_objects = php_driver_inet_compare; php_driver_inet_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_inet_ce->create_object = php_driver_inet_new; php_driver_inet_handlers.hash_value = php_driver_inet_hash_value; php_driver_inet_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Inet.h0000644000076500000240000000134713062515664016005 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_INET_H #define PHP_DRIVER_INET_H void php_driver_inet_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_INET_H */ cassandra-1.3.0/src/Keyspace.c0000644000076500000240000000414713062515664016646 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_keyspace_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_signature, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_ARG_INFO(0, ...) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_keyspace_methods[] = { PHP_ABSTRACT_ME(Keyspace, name, arginfo_none) PHP_ABSTRACT_ME(Keyspace, replicationClassName, arginfo_none) PHP_ABSTRACT_ME(Keyspace, replicationOptions, arginfo_none) PHP_ABSTRACT_ME(Keyspace, hasDurableWrites, arginfo_none) PHP_ABSTRACT_ME(Keyspace, table, arginfo_name) PHP_ABSTRACT_ME(Keyspace, tables, arginfo_none) PHP_ABSTRACT_ME(Keyspace, userType, arginfo_name) PHP_ABSTRACT_ME(Keyspace, userTypes, arginfo_none) PHP_ABSTRACT_ME(Keyspace, materializedView, arginfo_name) PHP_ABSTRACT_ME(Keyspace, materializedViews, arginfo_none) PHP_ABSTRACT_ME(Keyspace, function, arginfo_signature) PHP_ABSTRACT_ME(Keyspace, functions, arginfo_none) PHP_ABSTRACT_ME(Keyspace, aggregate, arginfo_signature) PHP_ABSTRACT_ME(Keyspace, aggregates, arginfo_none) PHP_FE_END }; void php_driver_define_Keyspace(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Keyspace", php_driver_keyspace_methods); php_driver_keyspace_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_keyspace_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Map.c0000644000076500000240000004071613062515664015621 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/collections.h" #include "util/hash.h" #include "util/types.h" #include "Map.h" zend_class_entry *php_driver_map_ce = NULL; int php_driver_map_set(php_driver_map *map, zval *zkey, zval *zvalue TSRMLS_DC) { php_driver_map_entry *entry; php_driver_type *type; if (Z_TYPE_P(zkey) == IS_NULL) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid key: null is not supported inside maps"); return 0; } if (Z_TYPE_P(zvalue) == IS_NULL) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid value: null is not supported inside maps"); return 0; } type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); if (!php_driver_validate_object(zkey, PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type) TSRMLS_CC)) { return 0; } if (!php_driver_validate_object(zvalue, PHP5TO7_ZVAL_MAYBE_P(type->data.map.value_type) TSRMLS_CC)) { return 0; } map->dirty = 1; HASH_FIND_ZVAL(map->entries, zkey, entry); if (entry == NULL) { entry = (php_driver_map_entry *) emalloc(sizeof(php_driver_map_entry)); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(entry->key), zkey); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(entry->value), zvalue); HASH_ADD_ZVAL(map->entries, key, entry); } else { php5to7_zval prev_value = entry->value; PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(entry->value), zvalue); zval_ptr_dtor(&prev_value); } return 1; } static int php_driver_map_get(php_driver_map *map, zval *zkey, php5to7_zval *zvalue TSRMLS_DC) { php_driver_map_entry *entry; php_driver_type *type; int result = 0; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); if (!php_driver_validate_object(zkey, PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(map->entries, zkey, entry); if (entry != NULL) { *zvalue = entry->value; result = 1; } return result; } static int php_driver_map_del(php_driver_map *map, zval *zkey TSRMLS_DC) { php_driver_map_entry *entry; php_driver_type *type; int result = 0; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); if (!php_driver_validate_object(zkey, PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(map->entries, zkey, entry); if (entry != NULL) { map->dirty = 1; if (entry == map->iter_temp) { map->iter_temp = (php_driver_map_entry *)map->iter_temp->hh.next; } HASH_DEL(map->entries, entry); zval_ptr_dtor(&entry->key); zval_ptr_dtor(&entry->value); efree(entry); result = 1; } return result; } static int php_driver_map_has(php_driver_map *map, zval *zkey TSRMLS_DC) { php_driver_map_entry *entry; php_driver_type *type; int result = 0; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); if (!php_driver_validate_object(zkey, PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(map->entries, zkey, entry); if (entry != NULL) { result = 1; } return result; } static void php_driver_map_populate_keys(const php_driver_map *map, zval *array TSRMLS_DC) { php_driver_map_entry *curr, *temp; HASH_ITER(hh, map->entries, curr, temp) { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_P(curr->key)) != SUCCESS) { break; } Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(curr->key)); } } static void php_driver_map_populate_values(const php_driver_map *map, zval *array TSRMLS_DC) { php_driver_map_entry *curr, *temp; HASH_ITER(hh, map->entries, curr, temp) { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_P(curr->value)) != SUCCESS) { break; } Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(curr->value)); } } /* {{{ Map::__construct(type, type) */ PHP_METHOD(Map, __construct) { php_driver_map *self; zval *key_type; zval *value_type; php5to7_zval scalar_key_type; php5to7_zval scalar_value_type; PHP5TO7_ZVAL_UNDEF(scalar_key_type); PHP5TO7_ZVAL_UNDEF(scalar_value_type); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &key_type, &value_type) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (Z_TYPE_P(key_type) == IS_STRING) { CassValueType type; if (!php_driver_value_type(Z_STRVAL_P(key_type), &type TSRMLS_CC)) return; scalar_key_type = php_driver_type_scalar(type TSRMLS_CC); key_type = PHP5TO7_ZVAL_MAYBE_P(scalar_key_type); } else if (Z_TYPE_P(key_type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(key_type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(key_type, "keyType" TSRMLS_CC)) { return; } Z_ADDREF_P(key_type); } else { throw_invalid_argument(key_type, "keyType", "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type" TSRMLS_CC); return; } if (Z_TYPE_P(value_type) == IS_STRING) { CassValueType type; if (!php_driver_value_type(Z_STRVAL_P(value_type), &type TSRMLS_CC)) return; scalar_value_type = php_driver_type_scalar(type TSRMLS_CC); value_type = PHP5TO7_ZVAL_MAYBE_P(scalar_value_type); } else if (Z_TYPE_P(value_type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value_type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(value_type, "valueType" TSRMLS_CC)) { return; } Z_ADDREF_P(value_type); } else { zval_ptr_dtor(PHP5TO7_ZVAL_MAYBE_ADDR_OF(key_type)); throw_invalid_argument(value_type, "valueType", "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type" TSRMLS_CC); return; } self->type = php_driver_type_map(key_type, value_type TSRMLS_CC); } /* }}} */ /* {{{ Map::type() */ PHP_METHOD(Map, type) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } /* }}} */ PHP_METHOD(Map, keys) { php_driver_map *self = NULL; array_init(return_value); self = PHP_DRIVER_GET_MAP(getThis()); php_driver_map_populate_keys(self, return_value TSRMLS_CC); } PHP_METHOD(Map, values) { php_driver_map *self = NULL; array_init(return_value); self = PHP_DRIVER_GET_MAP(getThis()); php_driver_map_populate_values(self, return_value TSRMLS_CC); } PHP_METHOD(Map, set) { zval *key; php_driver_map *self = NULL; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &key, &value) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_set(self, key, value TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } PHP_METHOD(Map, get) { zval *key; php_driver_map *self = NULL; php5to7_zval value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_get(self, key, &value TSRMLS_CC)) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 1, 0); } PHP_METHOD(Map, remove) { zval *key; php_driver_map *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_del(self, key TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } PHP_METHOD(Map, has) { zval *key; php_driver_map *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_has(self, key TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } PHP_METHOD(Map, count) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); RETURN_LONG((long)HASH_COUNT(self->entries)); } PHP_METHOD(Map, current) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); if (self->iter_curr != NULL) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->iter_curr->value), 1, 0); } PHP_METHOD(Map, key) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); if (self->iter_curr != NULL) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->iter_curr->key), 1, 0); } PHP_METHOD(Map, next) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); self->iter_curr = self->iter_temp; self->iter_temp = self->iter_temp != NULL ? (php_driver_map_entry *)self->iter_temp->hh.next : NULL; } PHP_METHOD(Map, valid) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); RETURN_BOOL(self->iter_curr != NULL); } PHP_METHOD(Map, rewind) { php_driver_map *self = PHP_DRIVER_GET_MAP(getThis()); self->iter_curr = self->entries; self->iter_temp = self->entries != NULL ? (php_driver_map_entry *)self->entries->hh.next : NULL; } PHP_METHOD(Map, offsetSet) { zval *key; php_driver_map *self = NULL; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &key, &value) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); php_driver_map_set(self, key, value TSRMLS_CC); } PHP_METHOD(Map, offsetGet) { zval *key; php_driver_map *self = NULL; php5to7_zval value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_get(self, key, &value TSRMLS_CC)) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(value), 1, 0); } PHP_METHOD(Map, offsetUnset) { zval *key; php_driver_map *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); php_driver_map_del(self, key TSRMLS_CC); } PHP_METHOD(Map, offsetExists) { zval *key; php_driver_map *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) return; self = PHP_DRIVER_GET_MAP(getThis()); if (php_driver_map_has(self, key TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 2) ZEND_ARG_INFO(0, keyType) ZEND_ARG_INFO(0, valueType) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_two, 0, ZEND_RETURN_VALUE, 2) ZEND_ARG_INFO(0, key) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_one, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, key) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_map_methods[] = { PHP_ME(Map, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Map, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, keys, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, values, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, set, arginfo_two, ZEND_ACC_PUBLIC) PHP_ME(Map, get, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Map, remove, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Map, has, arginfo_one, ZEND_ACC_PUBLIC) /* Countable */ PHP_ME(Map, count, arginfo_none, ZEND_ACC_PUBLIC) /* Iterator */ PHP_ME(Map, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Map, rewind, arginfo_none, ZEND_ACC_PUBLIC) /* ArrayAccess */ PHP_ME(Map, offsetSet, arginfo_two, ZEND_ACC_PUBLIC) PHP_ME(Map, offsetGet, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Map, offsetUnset, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Map, offsetExists, arginfo_one, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_map_handlers; static HashTable * php_driver_map_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_map_properties(zval *object TSRMLS_DC) { php5to7_zval keys; php5to7_zval values; php_driver_map *self = PHP_DRIVER_GET_MAP(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(self->type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZVAL_MAYBE_MAKE(keys); array_init(PHP5TO7_ZVAL_MAYBE_P(keys)); php_driver_map_populate_keys(self, PHP5TO7_ZVAL_MAYBE_P(keys) TSRMLS_CC); PHP5TO7_ZEND_HASH_SORT(Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(keys)), php_driver_data_compare, 1); PHP5TO7_ZEND_HASH_UPDATE(props, "keys", sizeof("keys"), PHP5TO7_ZVAL_MAYBE_P(keys), sizeof(zval *)); PHP5TO7_ZVAL_MAYBE_MAKE(values); array_init(PHP5TO7_ZVAL_MAYBE_P(values)); php_driver_map_populate_values(self, PHP5TO7_ZVAL_MAYBE_P(values) TSRMLS_CC); PHP5TO7_ZEND_HASH_SORT(Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(values)), php_driver_data_compare, 1); PHP5TO7_ZEND_HASH_UPDATE(props, "values", sizeof("values"), PHP5TO7_ZVAL_MAYBE_P(values), sizeof(zval *)); return props; } static int php_driver_map_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_map_entry *curr, *temp; php_driver_map *map1; php_driver_map *map2; php_driver_type *type1; php_driver_type *type2; int result; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ map1 = PHP_DRIVER_GET_MAP(obj1); map2 = PHP_DRIVER_GET_MAP(obj2); type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map1->type)); type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map2->type)); result = php_driver_type_compare(type1, type2 TSRMLS_CC); if (result != 0) return result; if (HASH_COUNT(map1->entries) != HASH_COUNT(map1->entries)) { return HASH_COUNT(map1->entries) < HASH_COUNT(map1->entries) ? -1 : 1; } HASH_ITER(hh, map1->entries, curr, temp) { php_driver_map_entry *entry; HASH_FIND_ZVAL(map2->entries, PHP5TO7_ZVAL_MAYBE_P(curr->key), entry); if (entry == NULL) { return 1; } } return 0; } static unsigned php_driver_map_hash_value(zval *obj TSRMLS_DC) { php_driver_map *self = PHP_DRIVER_GET_MAP(obj); php_driver_map_entry *curr, *temp; unsigned hashv = 0; if (!self->dirty) return self->hashv; HASH_ITER(hh, self->entries, curr, temp) { hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_P(curr->key) TSRMLS_CC)); hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_P(curr->value) TSRMLS_CC)); } self->hashv = hashv; self->dirty = 0; return hashv; } static void php_driver_map_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_map *self = PHP5TO7_ZEND_OBJECT_GET(map, object); php_driver_map_entry *curr, *temp; HASH_ITER(hh, self->entries, curr, temp) { zval_ptr_dtor(&curr->key); zval_ptr_dtor(&curr->value); HASH_DEL(self->entries, curr); efree(curr); } PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_map_new(zend_class_entry *ce TSRMLS_DC) { php_driver_map *self = PHP5TO7_ZEND_OBJECT_ECALLOC(map, ce); self->entries = self->iter_curr = self->iter_temp = NULL; self->dirty = 1; PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT(map, self, ce); } void php_driver_define_Map(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Map", php_driver_map_methods); php_driver_map_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_map_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_map_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_map_handlers.std.get_properties = php_driver_map_properties; #if PHP_VERSION_ID >= 50400 php_driver_map_handlers.std.get_gc = php_driver_map_gc; #endif php_driver_map_handlers.std.compare_objects = php_driver_map_compare; php_driver_map_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_map_ce->create_object = php_driver_map_new; zend_class_implements(php_driver_map_ce TSRMLS_CC, 3, spl_ce_Countable, zend_ce_iterator, zend_ce_arrayaccess); php_driver_map_handlers.hash_value = php_driver_map_hash_value; php_driver_map_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Map.h0000644000076500000240000000137413062515664015623 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_MAP_H #define PHP_DRIVER_MAP_H int php_driver_map_set(php_driver_map* map, zval* zkey, zval* zvalue TSRMLS_DC); #endif /* PHP_DRIVER_MAP_H */ cassandra-1.3.0/src/MaterializedView.c0000644000076500000240000000253013062515664020341 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_materialized_view_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_materialized_view_methods[] = { PHP_ABSTRACT_ME(MaterializedView, baseTable, arginfo_none) PHP_FE_END }; void php_driver_define_MaterializedView(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\MaterializedView", php_driver_materialized_view_methods); php_driver_materialized_view_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_materialized_view_ce TSRMLS_CC, 1, php_driver_table_ce); php_driver_materialized_view_ce->ce_flags |= ZEND_ACC_ABSTRACT; } cassandra-1.3.0/src/Numeric.c0000644000076500000240000000322713062515664016502 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_numeric_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_numeric_methods[] = { PHP_ABSTRACT_ME(Numeric, add, arginfo_num) PHP_ABSTRACT_ME(Numeric, sub, arginfo_num) PHP_ABSTRACT_ME(Numeric, mul, arginfo_num) PHP_ABSTRACT_ME(Numeric, div, arginfo_num) PHP_ABSTRACT_ME(Numeric, mod, arginfo_num) PHP_ABSTRACT_ME(Numeric, abs, arginfo_none) PHP_ABSTRACT_ME(Numeric, neg, arginfo_none) PHP_ABSTRACT_ME(Numeric, sqrt, arginfo_none) PHP_ABSTRACT_ME(Numeric, toInt, arginfo_none) PHP_ABSTRACT_ME(Numeric, toDouble, arginfo_none) PHP_FE_END }; void php_driver_define_Numeric(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Numeric", php_driver_numeric_methods); php_driver_numeric_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_numeric_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/PreparedStatement.c0000644000076500000240000000563513062515664020534 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_prepared_statement_ce = NULL; PHP_METHOD(PreparedStatement, __construct) { } static zend_function_entry php_driver_prepared_statement_methods[] = { PHP_ME(PreparedStatement, __construct, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR) PHP_FE_END }; static zend_object_handlers php_driver_prepared_statement_handlers; static HashTable * php_driver_prepared_statement_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_prepared_statement_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_prepared_statement_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_GET(statement, object); if (self->data.prepared.prepared) cass_prepared_free(self->data.prepared.prepared); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_prepared_statement_new(zend_class_entry *ce TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_ECALLOC(statement, ce); self->type = PHP_DRIVER_PREPARED_STATEMENT; self->data.prepared.prepared = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(statement, prepared_statement, self, ce); } void php_driver_define_PreparedStatement(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\PreparedStatement", php_driver_prepared_statement_methods); php_driver_prepared_statement_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_prepared_statement_ce TSRMLS_CC, 1, php_driver_statement_ce); php_driver_prepared_statement_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_prepared_statement_ce->create_object = php_driver_prepared_statement_new; memcpy(&php_driver_prepared_statement_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_prepared_statement_handlers.get_properties = php_driver_prepared_statement_properties; php_driver_prepared_statement_handlers.compare_objects = php_driver_prepared_statement_compare; php_driver_prepared_statement_handlers.clone_obj = NULL; } cassandra-1.3.0/src/RetryPolicy.c0000644000076500000240000000214213062515664017360 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_retry_policy_ce = NULL; static zend_function_entry php_driver_retry_policy_methods[] = { PHP_FE_END }; void php_driver_define_RetryPolicy(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\RetryPolicy", php_driver_retry_policy_methods); php_driver_retry_policy_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_retry_policy_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/RetryPolicy/DefaultPolicy.c0000644000076500000240000000420313062515664022124 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_retry_policy_default_ce = NULL; static zend_function_entry php_driver_retry_policy_default_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_retry_policy_default_handlers; static void php_driver_retry_policy_default_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_GET(retry_policy, object); cass_retry_policy_free(self->policy); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_retry_policy_default_new(zend_class_entry *ce TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_ECALLOC(retry_policy, ce); self->policy = cass_retry_policy_default_new(); PHP5TO7_ZEND_OBJECT_INIT_EX(retry_policy, retry_policy_default, self, ce); } void php_driver_define_RetryPolicyDefault(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\RetryPolicy\\DefaultPolicy", php_driver_retry_policy_default_methods); php_driver_retry_policy_default_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_retry_policy_default_ce TSRMLS_CC, 1, php_driver_retry_policy_ce); php_driver_retry_policy_default_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_retry_policy_default_ce->create_object = php_driver_retry_policy_default_new; memcpy(&php_driver_retry_policy_default_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/RetryPolicy/DowngradingConsistency.c0000644000076500000240000000457313062515664024057 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_retry_policy_downgrading_consistency_ce = NULL; static zend_function_entry php_driver_retry_policy_downgrading_consistency_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_retry_policy_downgrading_consistency_handlers; static void php_driver_retry_policy_downgrading_consistency_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_GET(retry_policy, object); cass_retry_policy_free(self->policy); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_retry_policy_downgrading_consistency_new(zend_class_entry *ce TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_ECALLOC(retry_policy, ce); self->policy = cass_retry_policy_downgrading_consistency_new(); PHP5TO7_ZEND_OBJECT_INIT_EX(retry_policy, retry_policy_downgrading_consistency, self, ce); } void php_driver_define_RetryPolicyDowngradingConsistency(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\RetryPolicy\\DowngradingConsistency", php_driver_retry_policy_downgrading_consistency_methods); php_driver_retry_policy_downgrading_consistency_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_retry_policy_downgrading_consistency_ce TSRMLS_CC, 1, php_driver_retry_policy_ce); php_driver_retry_policy_downgrading_consistency_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_retry_policy_downgrading_consistency_ce->create_object = php_driver_retry_policy_downgrading_consistency_new; memcpy(&php_driver_retry_policy_downgrading_consistency_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/RetryPolicy/Fallthrough.c0000644000076500000240000000427513062515664021650 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_retry_policy_fallthrough_ce = NULL; static zend_function_entry php_driver_retry_policy_fallthrough_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_retry_policy_fallthrough_handlers; static void php_driver_retry_policy_fallthrough_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_GET(retry_policy, object); cass_retry_policy_free(self->policy); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_retry_policy_fallthrough_new(zend_class_entry *ce TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_ECALLOC(retry_policy, ce); self->policy = cass_retry_policy_fallthrough_new(); PHP5TO7_ZEND_OBJECT_INIT_EX(retry_policy, retry_policy_fallthrough, self, ce); } void php_driver_define_RetryPolicyFallthrough(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\RetryPolicy\\Fallthrough", php_driver_retry_policy_fallthrough_methods); php_driver_retry_policy_fallthrough_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_retry_policy_fallthrough_ce TSRMLS_CC, 1, php_driver_retry_policy_ce); php_driver_retry_policy_fallthrough_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_retry_policy_fallthrough_ce->create_object = php_driver_retry_policy_fallthrough_new; memcpy(&php_driver_retry_policy_fallthrough_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/RetryPolicy/Logging.c0000644000076500000240000000624213062515664020753 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_retry_policy_logging_ce = NULL; PHP_METHOD(Logging, __construct) { zval *child_policy = NULL; php_driver_retry_policy *self, *retry_policy; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &child_policy, php_driver_retry_policy_ce) == FAILURE) { return; } if (instanceof_function(Z_OBJCE_P(child_policy), php_driver_retry_policy_logging_ce TSRMLS_CC)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Cannot add a " PHP_DRIVER_NAMESPACE "\\Logging as child policy of " PHP_DRIVER_NAMESPACE "\\Logging"); return; } self = PHP_DRIVER_GET_RETRY_POLICY(getThis()); retry_policy = PHP_DRIVER_GET_RETRY_POLICY(child_policy); self->policy = cass_retry_policy_logging_new(retry_policy->policy); } ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, childPolicy, RetryPolicy, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_retry_policy_logging_methods[] = { PHP_ME(Logging, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_retry_policy_logging_handlers; static void php_driver_retry_policy_logging_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_GET(retry_policy, object); if (self->policy) { cass_retry_policy_free(self->policy); } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_retry_policy_logging_new(zend_class_entry *ce TSRMLS_DC) { php_driver_retry_policy *self = PHP5TO7_ZEND_OBJECT_ECALLOC(retry_policy, ce); self->policy = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(retry_policy, retry_policy_logging, self, ce); } void php_driver_define_RetryPolicyLogging(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\RetryPolicy\\Logging", php_driver_retry_policy_logging_methods); php_driver_retry_policy_logging_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_retry_policy_logging_ce TSRMLS_CC, 1, php_driver_retry_policy_ce); php_driver_retry_policy_logging_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_retry_policy_logging_ce->create_object = php_driver_retry_policy_logging_new; memcpy(&php_driver_retry_policy_logging_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/Rows.c0000644000076500000240000003375713062515664016045 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/future.h" #include "util/ref.h" #include "util/result.h" #include "FutureRows.h" zend_class_entry *php_driver_rows_ce = NULL; static void free_result(void *result) { cass_result_free((CassResult *) result); } static void php_driver_rows_create(php_driver_rows *current, zval *result TSRMLS_DC) { php_driver_rows *rows; if (PHP5TO7_ZVAL_IS_UNDEF(current->next_rows)) { if (php_driver_get_result((const CassResult *) current->next_result->data, ¤t->next_rows TSRMLS_CC) == FAILURE) { PHP5TO7_ZVAL_MAYBE_DESTROY(current->next_rows); return; } } object_init_ex(result, php_driver_rows_ce); rows = PHP_DRIVER_GET_ROWS(result); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(rows->rows), PHP5TO7_ZVAL_MAYBE_P(current->next_rows)); if (cass_result_has_more_pages((const CassResult *) current->next_result->data)) { rows->statement = php_driver_add_ref(current->statement); rows->session = php_driver_add_ref(current->session); rows->result = php_driver_add_ref(current->next_result); } } PHP_METHOD(Rows, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Rows objects directly is not supported, " \ "call " PHP_DRIVER_NAMESPACE "\\Session::execute() or " PHP_DRIVER_NAMESPACE "\\FutureRows::get() instead." ); return; } PHP_METHOD(Rows, count) { php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(self->rows)))); } PHP_METHOD(Rows, rewind) { php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); zend_hash_internal_pointer_reset(Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(self->rows))); } PHP_METHOD(Rows, current) { php5to7_zval *entry; php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_ROWS(getThis()); if (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), entry)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(entry), 1, 0); } } PHP_METHOD(Rows, key) { php5to7_ulong num_index; php5to7_string str_index; php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), &str_index, &num_index) == HASH_KEY_IS_LONG) RETURN_LONG(num_index); } PHP_METHOD(Rows, next) { php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_ROWS(getThis()); zend_hash_move_forward(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows)); } PHP_METHOD(Rows, valid) { php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); RETURN_BOOL(zend_hash_has_more_elements(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows)) == SUCCESS); } PHP_METHOD(Rows, offsetExists) { zval *offset; php_driver_rows *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &offset) == FAILURE) return; if (Z_TYPE_P(offset) != IS_LONG || Z_LVAL_P(offset) < 0) { INVALID_ARGUMENT(offset, "a positive integer"); } self = PHP_DRIVER_GET_ROWS(getThis()); RETURN_BOOL(zend_hash_index_exists(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), (php5to7_ulong) Z_LVAL_P(offset))); } PHP_METHOD(Rows, offsetGet) { zval *offset; php5to7_zval *value; php_driver_rows *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &offset) == FAILURE) return; if (Z_TYPE_P(offset) != IS_LONG || Z_LVAL_P(offset) < 0) { INVALID_ARGUMENT(offset, "a positive integer"); } self = PHP_DRIVER_GET_ROWS(getThis()); if (PHP5TO7_ZEND_HASH_INDEX_FIND(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), Z_LVAL_P(offset), value)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(value), 1, 0); } } PHP_METHOD(Rows, offsetSet) { if (zend_parse_parameters_none() == FAILURE) return; zend_throw_exception_ex(php_driver_domain_exception_ce, 0 TSRMLS_CC, "Cannot overwrite a row at a given offset, rows are immutable." ); return; } PHP_METHOD(Rows, offsetUnset) { if (zend_parse_parameters_none() == FAILURE) return; zend_throw_exception_ex(php_driver_domain_exception_ce, 0 TSRMLS_CC, "Cannot delete a row at a given offset, rows are immutable." ); return; } PHP_METHOD(Rows, isLastPage) { php_driver_rows *self = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); if (self->result == NULL && PHP5TO7_ZVAL_IS_UNDEF(self->next_rows) && PHP5TO7_ZVAL_IS_UNDEF(self->future_next_page)) { RETURN_TRUE; } RETURN_FALSE; } PHP_METHOD(Rows, nextPage) { zval *timeout = NULL; php_driver_rows *self = PHP_DRIVER_GET_ROWS(getThis()); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &timeout) == FAILURE) { return; } if (!self->next_result) { if (!PHP5TO7_ZVAL_IS_UNDEF(self->future_next_page)) { php_driver_future_rows *future_rows = NULL; if (!instanceof_function(PHP5TO7_Z_OBJCE_MAYBE_P(self->future_next_page), php_driver_future_rows_ce TSRMLS_CC)) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unexpected future instance."); return; } future_rows = PHP_DRIVER_GET_FUTURE_ROWS(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page)); if (php_driver_future_rows_get_result(future_rows, timeout TSRMLS_CC) == FAILURE) { return; } self->next_result = php_driver_add_ref(future_rows->result); } else { const CassResult *result = NULL; CassFuture *future = NULL; if (self->result == NULL) { return; } ASSERT_SUCCESS(cass_statement_set_paging_state((CassStatement *) self->statement->data, (const CassResult *) self->result->data)); future = cass_session_execute((CassSession *) self->session->data, (CassStatement *) self->statement->data); if (php_driver_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) { return; } if (php_driver_future_is_error(future TSRMLS_CC) == FAILURE) { return; } result = cass_future_get_result(future); if (!result) { cass_future_free(future); zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Future doesn't contain a result."); return; } self->next_result = php_driver_new_ref((void *)result , free_result); cass_future_free(future); } } /* Always create a new rows object to avoid creating a linked list of * objects. */ php_driver_rows_create(self, return_value TSRMLS_CC); } PHP_METHOD(Rows, nextPageAsync) { php_driver_rows *self = NULL; php_driver_future_rows *future_rows = NULL; if (zend_parse_parameters_none() == FAILURE) return; self = PHP_DRIVER_GET_ROWS(getThis()); if (!PHP5TO7_ZVAL_IS_UNDEF(self->future_next_page)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page), 1, 0); } if (self->next_result) { php_driver_future_value *future_value; PHP5TO7_ZVAL_MAYBE_MAKE(self->future_next_page); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page), php_driver_future_value_ce); future_value = PHP_DRIVER_GET_FUTURE_VALUE(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page)); PHP5TO7_ZVAL_MAYBE_MAKE(future_value->value); php_driver_rows_create(self, PHP5TO7_ZVAL_MAYBE_P(future_value->value) TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page), 1, 0); } if (self->result == NULL) { object_init_ex(return_value, php_driver_future_value_ce); return; } ASSERT_SUCCESS(cass_statement_set_paging_state((CassStatement *) self->statement->data, (const CassResult *) self->result->data)); PHP5TO7_ZVAL_MAYBE_MAKE(self->future_next_page); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page), php_driver_future_rows_ce); future_rows = PHP_DRIVER_GET_FUTURE_ROWS(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page)); future_rows->statement = php_driver_add_ref(self->statement); future_rows->session = php_driver_add_ref(self->session); future_rows->future = cass_session_execute((CassSession *) self->session->data, (CassStatement *) self->statement->data); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->future_next_page), 1, 0); } PHP_METHOD(Rows, pagingStateToken) { const char *paging_state; size_t paging_state_size; php_driver_rows* self = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_ROWS(getThis()); if (self->result == NULL) return; ASSERT_SUCCESS(cass_result_paging_state_token((const CassResult *) self->result->data, &paging_state, &paging_state_size)); PHP5TO7_RETURN_STRINGL(paging_state, paging_state_size); } PHP_METHOD(Rows, first) { HashPosition pos; php5to7_zval *entry; php_driver_rows* self = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_ROWS(getThis()); zend_hash_internal_pointer_reset_ex(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), &pos); if (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA(PHP5TO7_Z_ARRVAL_MAYBE_P(self->rows), entry)) { RETVAL_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(entry), 1, 0); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_offset, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, offset) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_set, 0, ZEND_RETURN_VALUE, 2) ZEND_ARG_INFO(0, offset) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() static zend_function_entry php_driver_rows_methods[] = { PHP_ME(Rows, __construct, arginfo_none, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Rows, count, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, rewind, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, offsetExists, arginfo_offset, ZEND_ACC_PUBLIC) PHP_ME(Rows, offsetGet, arginfo_offset, ZEND_ACC_PUBLIC) PHP_ME(Rows, offsetSet, arginfo_set, ZEND_ACC_PUBLIC) PHP_ME(Rows, offsetUnset, arginfo_offset, ZEND_ACC_PUBLIC) PHP_ME(Rows, isLastPage, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, nextPage, arginfo_timeout, ZEND_ACC_PUBLIC) PHP_ME(Rows, nextPageAsync, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, pagingStateToken, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Rows, first, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_rows_handlers; static HashTable * php_driver_rows_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_rows_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_rows_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_rows *self = PHP5TO7_ZEND_OBJECT_GET(rows, object); php_driver_del_ref(&self->result); php_driver_del_ref(&self->statement); php_driver_del_peref(&self->session, 1); php_driver_del_ref(&self->next_result); PHP5TO7_ZVAL_MAYBE_DESTROY(self->rows); PHP5TO7_ZVAL_MAYBE_DESTROY(self->next_rows); PHP5TO7_ZVAL_MAYBE_DESTROY(self->future_next_page); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_rows_new(zend_class_entry *ce TSRMLS_DC) { php_driver_rows *self = PHP5TO7_ZEND_OBJECT_ECALLOC(rows, ce); self->statement = NULL; self->session = NULL; self->result = NULL; self->next_result = NULL; PHP5TO7_ZVAL_UNDEF(self->rows); PHP5TO7_ZVAL_UNDEF(self->next_rows); PHP5TO7_ZVAL_UNDEF(self->future_next_page); PHP5TO7_ZEND_OBJECT_INIT(rows, self, ce); } void php_driver_define_Rows(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Rows", php_driver_rows_methods); php_driver_rows_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_rows_ce TSRMLS_CC, 2, zend_ce_iterator, zend_ce_arrayaccess); php_driver_rows_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_rows_ce->create_object = php_driver_rows_new; memcpy(&php_driver_rows_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_rows_handlers.get_properties = php_driver_rows_properties; php_driver_rows_handlers.compare_objects = php_driver_rows_compare; php_driver_rows_handlers.clone_obj = NULL; } cassandra-1.3.0/src/SSLOptions.c0000644000076500000240000000436513062515664017121 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_ssl_ce = NULL; static zend_function_entry php_driver_ssl_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_ssl_handlers; static HashTable * php_driver_ssl_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_ssl_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_ssl_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_ssl *self = PHP5TO7_ZEND_OBJECT_GET(ssl, object); cass_ssl_free(self->ssl); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_ssl_new(zend_class_entry *ce TSRMLS_DC) { php_driver_ssl *self = PHP5TO7_ZEND_OBJECT_ECALLOC(ssl, ce); self->ssl = cass_ssl_new(); PHP5TO7_ZEND_OBJECT_INIT(ssl, self, ce); } void php_driver_define_SSLOptions(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\SSLOptions", php_driver_ssl_methods); php_driver_ssl_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_ssl_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_ssl_ce->create_object = php_driver_ssl_new; memcpy(&php_driver_ssl_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_ssl_handlers.get_properties = php_driver_ssl_properties; php_driver_ssl_handlers.compare_objects = php_driver_ssl_compare; php_driver_ssl_handlers.clone_obj = NULL; } cassandra-1.3.0/src/SSLOptions/Builder.c0000644000076500000240000002237113062515664020504 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include zend_class_entry *php_driver_ssl_builder_ce = NULL; static int file_get_contents(char *path, char **output, int *len TSRMLS_DC) { #if PHP_MAJOR_VERSION >= 7 zend_string *str; php_stream *stream = php_stream_open_wrapper(path, "rb", USE_PATH|REPORT_ERRORS, NULL); #else php_stream *stream = php_stream_open_wrapper(path, "rb", USE_PATH|REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); #endif if (!stream) { zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "The path '%s' doesn't exist or is not readable", path); return 0; } #if PHP_MAJOR_VERSION >= 7 str = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0); if (str) { *output = estrndup(str->val, str->len); *len = str->len; zend_string_release(str); } else { php_stream_close(stream); return 0; } #else *len = php_stream_copy_to_mem(stream, output, PHP_STREAM_COPY_ALL, 0); #endif php_stream_close(stream); return 1; } PHP_METHOD(SSLOptionsBuilder, build) { php_driver_ssl *ssl = NULL; int len; char *contents; CassError rc; php_driver_ssl_builder *builder = PHP_DRIVER_GET_SSL_BUILDER(getThis()); object_init_ex(return_value, php_driver_ssl_ce); ssl = PHP_DRIVER_GET_SSL(return_value); cass_ssl_set_verify_flags(ssl->ssl, builder->flags); if (builder->trusted_certs) { int i; char *path; for (i = 0; i < builder->trusted_certs_cnt; i++) { path = builder->trusted_certs[i]; if (!file_get_contents(path, &contents, &len TSRMLS_CC)) return; rc = cass_ssl_add_trusted_cert_n(ssl->ssl, contents, len); efree(contents); ASSERT_SUCCESS(rc); } } if (builder->client_cert) { if (!file_get_contents(builder->client_cert, &contents, &len TSRMLS_CC)) return; rc = cass_ssl_set_cert_n(ssl->ssl, contents, len); efree(contents); ASSERT_SUCCESS(rc); } if (builder->private_key) { if (!file_get_contents(builder->private_key, &contents, &len TSRMLS_CC)) return; rc = cass_ssl_set_private_key(ssl->ssl, contents, builder->passphrase); efree(contents); ASSERT_SUCCESS(rc); } } PHP_METHOD(SSLOptionsBuilder, withTrustedCerts) { zval readable; php5to7_zval_args args = NULL; int argc = 0, i; php_driver_ssl_builder *builder = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } for (i = 0; i < argc; i++) { zval *path = PHP5TO7_ZVAL_ARG(args[i]); if (Z_TYPE_P(path) != IS_STRING) { throw_invalid_argument(path, "path", "a path to a trusted cert file" TSRMLS_CC); PHP5TO7_MAYBE_EFREE(args); } php_stat(Z_STRVAL_P(path), Z_STRLEN_P(path), FS_IS_R, &readable TSRMLS_CC); if (PHP5TO7_ZVAL_IS_FALSE_P(&readable)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "The path '%s' doesn't exist or is not readable", Z_STRVAL_P(path)); PHP5TO7_MAYBE_EFREE(args); return; } } builder = PHP_DRIVER_GET_SSL_BUILDER(getThis()); if (builder->trusted_certs) { for (i = 0; i < builder->trusted_certs_cnt; i++) { efree(builder->trusted_certs[i]); } efree(builder->trusted_certs); } builder->trusted_certs_cnt = argc; builder->trusted_certs = ecalloc(argc, sizeof(char *)); for (i = 0; i < argc; i++) { zval *path = PHP5TO7_ZVAL_ARG(args[i]); builder->trusted_certs[i] = estrndup(Z_STRVAL_P(path), Z_STRLEN_P(path)); } PHP5TO7_MAYBE_EFREE(args); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(SSLOptionsBuilder, withVerifyFlags) { long flags; php_driver_ssl_builder *builder = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &flags) == FAILURE) { return; } builder = PHP_DRIVER_GET_SSL_BUILDER(getThis()); builder->flags = (int) flags; RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(SSLOptionsBuilder, withClientCert) { char *client_cert; php5to7_size client_cert_len; zval readable; php_driver_ssl_builder *builder = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &client_cert, &client_cert_len) == FAILURE) { return; } php_stat(client_cert, client_cert_len, FS_IS_R, &readable TSRMLS_CC); if (PHP5TO7_ZVAL_IS_FALSE_P(&readable)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "The path '%s' doesn't exist or is not readable", client_cert); return; } builder = PHP_DRIVER_GET_SSL_BUILDER(getThis()); if (builder->client_cert) efree(builder->client_cert); builder->client_cert = estrndup(client_cert, client_cert_len); RETURN_ZVAL(getThis(), 1, 0); } PHP_METHOD(SSLOptionsBuilder, withPrivateKey) { char *private_key; char *passphrase = NULL; php5to7_size private_key_len, passphrase_len; zval readable; php_driver_ssl_builder *builder = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &private_key, &private_key_len, &passphrase, &passphrase_len) == FAILURE) { return; } php_stat(private_key, private_key_len, FS_IS_R, &readable TSRMLS_CC); if (PHP5TO7_ZVAL_IS_FALSE_P(&readable)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "The path '%s' doesn't exist or is not readable", private_key); return; } builder = PHP_DRIVER_GET_SSL_BUILDER(getThis()); if (builder->private_key) efree(builder->private_key); builder->private_key = estrndup(private_key, private_key_len); if (builder->passphrase) { efree(builder->passphrase); builder->passphrase = NULL; } if (passphrase) builder->passphrase = estrndup(passphrase, passphrase_len); RETURN_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_path, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, path) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_flags, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_key, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, path) ZEND_ARG_INFO(0, passphrase) ZEND_END_ARG_INFO() static zend_function_entry php_driver_ssl_builder_methods[] = { PHP_ME(SSLOptionsBuilder, build, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(SSLOptionsBuilder, withTrustedCerts, arginfo_path, ZEND_ACC_PUBLIC) PHP_ME(SSLOptionsBuilder, withVerifyFlags, arginfo_flags, ZEND_ACC_PUBLIC) PHP_ME(SSLOptionsBuilder, withClientCert, arginfo_path, ZEND_ACC_PUBLIC) PHP_ME(SSLOptionsBuilder, withPrivateKey, arginfo_key, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_ssl_builder_handlers; static HashTable * php_driver_ssl_builder_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_ssl_builder_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_ssl_builder_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_ssl_builder *self = PHP5TO7_ZEND_OBJECT_GET(ssl_builder, object); if (self->trusted_certs) { int i; for (i = 0; i < self->trusted_certs_cnt; i++) efree(self->trusted_certs[i]); efree(self->trusted_certs); } if (self->client_cert) efree(self->client_cert); if (self->private_key) efree(self->private_key); if (self->passphrase) efree(self->passphrase); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_ssl_builder_new(zend_class_entry *ce TSRMLS_DC) { php_driver_ssl_builder *self = PHP5TO7_ZEND_OBJECT_ECALLOC(ssl_builder, ce); self->flags = 0; self->trusted_certs = NULL; self->trusted_certs_cnt = 0; self->client_cert = NULL; self->private_key = NULL; self->passphrase = NULL; PHP5TO7_ZEND_OBJECT_INIT(ssl_builder, self, ce); } void php_driver_define_SSLOptionsBuilder(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\SSLOptions\\Builder", php_driver_ssl_builder_methods); php_driver_ssl_builder_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_ssl_builder_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_ssl_builder_ce->create_object = php_driver_ssl_builder_new; memcpy(&php_driver_ssl_builder_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_ssl_builder_handlers.get_properties = php_driver_ssl_builder_properties; php_driver_ssl_builder_handlers.compare_objects = php_driver_ssl_builder_compare; } cassandra-1.3.0/src/Schema.c0000644000076500000240000000245013062515664016275 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_schema_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_schema_methods[] = { PHP_ABSTRACT_ME(Schema, keyspace, arginfo_name) PHP_ABSTRACT_ME(Schema, keyspaces, arginfo_none) PHP_FE_END }; void php_driver_define_Schema(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Schema", php_driver_schema_methods); php_driver_schema_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_schema_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Session.c0000644000076500000240000000362313062515664016523 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_session_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_execute, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, statement) ZEND_ARG_INFO(0, options) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_prepare, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, cql) ZEND_ARG_INFO(0, options) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_timeout, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timeout) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_session_methods[] = { PHP_ABSTRACT_ME(Session, execute, arginfo_execute) PHP_ABSTRACT_ME(Session, executeAsync, arginfo_execute) PHP_ABSTRACT_ME(Session, prepare, arginfo_prepare) PHP_ABSTRACT_ME(Session, prepareAsync, arginfo_prepare) PHP_ABSTRACT_ME(Session, close, arginfo_timeout) PHP_ABSTRACT_ME(Session, closeAsync, arginfo_none) PHP_ABSTRACT_ME(Session, metrics, arginfo_none) PHP_ABSTRACT_ME(Session, schema, arginfo_none) PHP_FE_END }; void php_driver_define_Session(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Session", php_driver_session_methods); php_driver_session_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_session_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Set.c0000644000076500000240000002702713062515664015637 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/collections.h" #include "util/hash.h" #include "util/types.h" #include "src/Set.h" zend_class_entry *php_driver_set_ce = NULL; int php_driver_set_add(php_driver_set *set, zval *object TSRMLS_DC) { php_driver_set_entry *entry; php_driver_type *type; if (Z_TYPE_P(object) == IS_NULL) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid value: null is not supported inside sets"); return 0; } type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->type)); if (!php_driver_validate_object(object, PHP5TO7_ZVAL_MAYBE_P(type->data.set.value_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(set->entries, object, entry); if (entry == NULL) { set->dirty = 1; entry = (php_driver_set_entry *) emalloc(sizeof(php_driver_set_entry)); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(entry->value), object); HASH_ADD_ZVAL(set->entries, value, entry); } return 1; } static int php_driver_set_del(php_driver_set *set, zval *object TSRMLS_DC) { php_driver_set_entry *entry; php_driver_type *type; int result = 0; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->type)); if (!php_driver_validate_object(object, PHP5TO7_ZVAL_MAYBE_P(type->data.set.value_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(set->entries, object, entry); if (entry != NULL) { set->dirty = 1; if (entry == set->iter_temp) { set->iter_temp = (php_driver_set_entry *)set->iter_temp->hh.next; } HASH_DEL(set->entries, entry); zval_ptr_dtor(&entry->value); efree(entry); result = 1; } return result; } static int php_driver_set_has(php_driver_set *set, zval *object TSRMLS_DC) { php_driver_set_entry *entry; php_driver_type *type; int result = 0; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->type)); if (!php_driver_validate_object(object, PHP5TO7_ZVAL_MAYBE_P(type->data.set.value_type) TSRMLS_CC)) { return 0; } HASH_FIND_ZVAL(set->entries, object, entry); if (entry != NULL) { result = 1; } return result; } static void php_driver_set_populate(php_driver_set *set, zval *array TSRMLS_DC) { php_driver_set_entry *curr, *temp; HASH_ITER(hh, set->entries, curr, temp) { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_P(curr->value)) != SUCCESS) { break; } Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(curr->value)); } } /* {{{ Set::__construct(type) */ PHP_METHOD(Set, __construct) { php_driver_set *self; zval *type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &type) == FAILURE) return; self = PHP_DRIVER_GET_SET(getThis()); if (Z_TYPE_P(type) == IS_STRING) { CassValueType value_type; if (!php_driver_value_type(Z_STRVAL_P(type), &value_type TSRMLS_CC)) return; self->type = php_driver_type_set_from_value_type(value_type TSRMLS_CC); } else if (Z_TYPE_P(type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(type, "type" TSRMLS_CC)) { return; } self->type = php_driver_type_set(type TSRMLS_CC); Z_ADDREF_P(type); } else { INVALID_ARGUMENT(type, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type"); } } /* }}} */ /* {{{ Set::type() */ PHP_METHOD(Set, type) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } /* }}} */ /* {{{ Set::values() */ PHP_METHOD(Set, values) { php_driver_set *set = NULL; array_init(return_value); set = PHP_DRIVER_GET_SET(getThis()); php_driver_set_populate(set, return_value TSRMLS_CC); } /* }}} */ /* {{{ Set::add(value) */ PHP_METHOD(Set, add) { php_driver_set *self = NULL; zval *object; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &object) == FAILURE) return; self = PHP_DRIVER_GET_SET(getThis()); if (php_driver_set_add(self, object TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } /* }}} */ /* {{{ Set::remove(value) */ PHP_METHOD(Set, remove) { php_driver_set *self = NULL; zval *object; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &object) == FAILURE) return; self = PHP_DRIVER_GET_SET(getThis()); if (php_driver_set_del(self, object TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } /* }}} */ /* {{{ Set::has(value) */ PHP_METHOD(Set, has) { php_driver_set *self = NULL; zval *object; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &object) == FAILURE) return; self = PHP_DRIVER_GET_SET(getThis()); if (php_driver_set_has(self, object TSRMLS_CC)) RETURN_TRUE; RETURN_FALSE; } /* }}} */ /* {{{ Set::count() */ PHP_METHOD(Set, count) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); RETURN_LONG((long)HASH_COUNT(self->entries)); } /* }}} */ /* {{{ Set::current() */ PHP_METHOD(Set, current) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); if (self->iter_curr != NULL) RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->iter_curr->value), 1, 0); } /* }}} */ /* {{{ Set::key() */ PHP_METHOD(Set, key) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); RETURN_LONG(self->iter_index); } /* }}} */ /* {{{ Set::next() */ PHP_METHOD(Set, next) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); self->iter_curr = self->iter_temp; self->iter_temp = self->iter_temp != NULL ? (php_driver_set_entry *)self->iter_temp->hh.next : NULL; self->iter_index++; } /* }}} */ /* {{{ Set::valid() */ PHP_METHOD(Set, valid) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); RETURN_BOOL(self->iter_curr != NULL); } /* }}} */ /* {{{ Set::rewind() */ PHP_METHOD(Set, rewind) { php_driver_set *self = PHP_DRIVER_GET_SET(getThis()); self->iter_curr = self->entries; self->iter_temp = self->entries != NULL ? (php_driver_set_entry *)self->entries->hh.next : NULL; self->iter_index = 0; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, type) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_one, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_set_methods[] = { PHP_ME(Set, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Set, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, values, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, add, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Set, has, arginfo_one, ZEND_ACC_PUBLIC) PHP_ME(Set, remove, arginfo_one, ZEND_ACC_PUBLIC) /* Countable */ PHP_ME(Set, count, arginfo_none, ZEND_ACC_PUBLIC) /* Iterator */ PHP_ME(Set, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Set, rewind, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_set_handlers; static HashTable * php_driver_set_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_set_properties(zval *object TSRMLS_DC) { php5to7_zval values; php_driver_set *self = PHP_DRIVER_GET_SET(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(self->type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZVAL_MAYBE_MAKE(values); array_init(PHP5TO7_ZVAL_MAYBE_P(values)); php_driver_set_populate(self , PHP5TO7_ZVAL_MAYBE_P(values) TSRMLS_CC); PHP5TO7_ZEND_HASH_SORT(Z_ARRVAL_P(PHP5TO7_ZVAL_MAYBE_P(values)), php_driver_data_compare, 1); PHP5TO7_ZEND_HASH_UPDATE(props, "values", sizeof("values"), PHP5TO7_ZVAL_MAYBE_P(values), sizeof(zval)); return props; } static int php_driver_set_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_set_entry *curr, *temp; php_driver_set *set1; php_driver_set *set2; php_driver_type *type1; php_driver_type *type2; int result; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ set1 = PHP_DRIVER_GET_SET(obj1); set2 = PHP_DRIVER_GET_SET(obj2); type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set1->type)); type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set2->type)); result = php_driver_type_compare(type1, type2 TSRMLS_CC); if (result != 0) return result; if (HASH_COUNT(set1->entries) != HASH_COUNT(set1->entries)) { return HASH_COUNT(set1->entries) < HASH_COUNT(set1->entries) ? -1 : 1; } HASH_ITER(hh, set1->entries, curr, temp) { php_driver_set_entry *entry; HASH_FIND_ZVAL(set2->entries, PHP5TO7_ZVAL_MAYBE_P(curr->value), entry); if (entry == NULL) { return 1; } } return 0; } static unsigned php_driver_set_hash_value(zval *obj TSRMLS_DC) { unsigned hashv = 0; php_driver_set_entry *curr, *temp; php_driver_set *self = PHP_DRIVER_GET_SET(obj); if (!self->dirty) return self->hashv; HASH_ITER(hh, self->entries, curr, temp) { hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_P(curr->value) TSRMLS_CC)); } self->hashv = hashv; self->dirty = 0; return hashv; } static void php_driver_set_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_set *self = PHP5TO7_ZEND_OBJECT_GET(set, object); php_driver_set_entry *curr, *temp; HASH_ITER(hh, self->entries, curr, temp) { zval_ptr_dtor(&curr->value); HASH_DEL(self->entries, curr); efree(curr); } PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_set_new(zend_class_entry *ce TSRMLS_DC) { php_driver_set *self = PHP5TO7_ZEND_OBJECT_ECALLOC(set, ce); self->entries = self->iter_curr = self->iter_temp = NULL; self->iter_index = 0; self->dirty = 1; PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT(set, self, ce); } void php_driver_define_Set(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Set", php_driver_set_methods); php_driver_set_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_set_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_set_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_set_handlers.std.get_properties = php_driver_set_properties; #if PHP_VERSION_ID >= 50400 php_driver_set_handlers.std.get_gc = php_driver_set_gc; #endif php_driver_set_handlers.std.compare_objects = php_driver_set_compare; php_driver_set_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_set_ce->create_object = php_driver_set_new; zend_class_implements(php_driver_set_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); php_driver_set_handlers.hash_value = php_driver_set_hash_value; php_driver_set_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Set.h0000644000076500000240000000136013062515664015634 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_SET_H #define PHP_DRIVER_SET_H int php_driver_set_add(php_driver_set* set, zval* object TSRMLS_DC); #endif /* PHP_DRIVER_SET_H */ cassandra-1.3.0/src/SimpleStatement.c0000644000076500000240000000652013062515664020215 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" zend_class_entry *php_driver_simple_statement_ce = NULL; PHP_METHOD(SimpleStatement, __construct) { zval *cql = NULL; php_driver_statement *self = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &cql) == FAILURE) { return; } if (Z_TYPE_P(cql) != IS_STRING) { INVALID_ARGUMENT(cql, "a string"); } self = PHP_DRIVER_GET_STATEMENT(getThis()); self->data.simple.cql = estrndup(Z_STRVAL_P(cql), Z_STRLEN_P(cql)); } ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, cql) ZEND_END_ARG_INFO() static zend_function_entry php_driver_simple_statement_methods[] = { PHP_ME(SimpleStatement, __construct, arginfo__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_FE_END }; static zend_object_handlers php_driver_simple_statement_handlers; static HashTable * php_driver_simple_statement_properties(zval *object TSRMLS_DC) { HashTable *props = zend_std_get_properties(object TSRMLS_CC); return props; } static int php_driver_simple_statement_compare(zval *obj1, zval *obj2 TSRMLS_DC) { if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ return Z_OBJ_HANDLE_P(obj1) != Z_OBJ_HANDLE_P(obj1); } static void php_driver_simple_statement_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_GET(statement, object); if (self->data.simple.cql) { efree(self->data.simple.cql); self->data.simple.cql = NULL; } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_simple_statement_new(zend_class_entry *ce TSRMLS_DC) { php_driver_statement *self = PHP5TO7_ZEND_OBJECT_ECALLOC(statement, ce); self->type = PHP_DRIVER_SIMPLE_STATEMENT; self->data.simple.cql = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(statement, simple_statement, self, ce); } void php_driver_define_SimpleStatement(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\SimpleStatement", php_driver_simple_statement_methods); php_driver_simple_statement_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_simple_statement_ce TSRMLS_CC, 1, php_driver_statement_ce); php_driver_simple_statement_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_simple_statement_ce->create_object = php_driver_simple_statement_new; memcpy(&php_driver_simple_statement_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_simple_statement_handlers.get_properties = php_driver_simple_statement_properties; php_driver_simple_statement_handlers.compare_objects = php_driver_simple_statement_compare; php_driver_simple_statement_handlers.clone_obj = NULL; } cassandra-1.3.0/src/Smallint.c0000644000076500000240000004124613062515664016666 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #if !defined(HAVE_STDINT_H) && !defined(_MSC_STDINT_H_) # define INT16_MAX 32767 # define INT16_MIN (-INT16_MAX-1) #endif zend_class_entry *php_driver_smallint_ce = NULL; static int to_double(zval *result, php_driver_numeric *smallint TSRMLS_DC) { ZVAL_DOUBLE(result, (double) smallint->data.smallint.value); return SUCCESS; } static int to_long(zval *result, php_driver_numeric *smallint TSRMLS_DC) { ZVAL_LONG(result, (php5to7_long) smallint->data.smallint.value); return SUCCESS; } static int to_string(zval *result, php_driver_numeric *smallint TSRMLS_DC) { char *string; spprintf(&string, 0, "%d", smallint->data.smallint.value); PHP5TO7_ZVAL_STRING(result, string); efree(string); return SUCCESS; } void php_driver_smallint_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_numeric *self; zval *value; cass_int32_t number; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_smallint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_smallint_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *other = PHP_DRIVER_GET_NUMERIC(value); self->data.smallint.value = other->data.smallint.value; } else { if (Z_TYPE_P(value) == IS_LONG) { number = (cass_int32_t) Z_LVAL_P(value); if (number < INT16_MIN || number > INT16_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -32768 and 32767, %ld given", Z_LVAL_P(value)); return; } } else if (Z_TYPE_P(value) == IS_DOUBLE) { number = (cass_int32_t) Z_DVAL_P(value); if (number < INT16_MIN || number > INT16_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -32768 and 32767, %g given", Z_DVAL_P(value)); return; } } else if (Z_TYPE_P(value) == IS_STRING) { if (!php_driver_parse_int(Z_STRVAL_P(value), Z_STRLEN_P(value), &number TSRMLS_CC)) { // If the parsing function fails, it would have set an exception. If it's // a range error, the error message would be wrong because the parsing // function supports all 32-bit values, so the "valid" range it reports would // be too large for Smallint. Reset the exception in that case. if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -32768 and 32767, %s given", Z_STRVAL_P(value)); } return; } if (number < INT16_MIN || number > INT16_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -32768 and 32767, %s given", Z_STRVAL_P(value)); return; } } else { INVALID_ARGUMENT(value, "a long, a double, a numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Smallint"); } self->data.smallint.value = (cass_int16_t) number; } } /* {{{ Smallint::__construct(string) */ PHP_METHOD(Smallint, __construct) { php_driver_smallint_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Smallint::__toString() */ PHP_METHOD(Smallint, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Smallint::type() */ PHP_METHOD(Smallint, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_SMALL_INT TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Smallint::value() */ PHP_METHOD(Smallint, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Smallint::add() */ PHP_METHOD(Smallint, add) { zval *addend; php_driver_numeric *self; php_driver_numeric *smallint; php_driver_numeric *result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &addend) == FAILURE) { return; } if (Z_TYPE_P(addend) == IS_OBJECT && instanceof_function(Z_OBJCE_P(addend), php_driver_smallint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); smallint = PHP_DRIVER_GET_NUMERIC(addend); object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = self->data.smallint.value + smallint->data.smallint.value; if (result->data.smallint.value - smallint->data.smallint.value != self->data.smallint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Sum is out of range"); return; } } else { INVALID_ARGUMENT(addend, "a " PHP_DRIVER_NAMESPACE "\\Smallint"); } } /* }}} */ /* {{{ Smallint::sub() */ PHP_METHOD(Smallint, sub) { zval *difference; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &difference) == FAILURE) { return; } if (Z_TYPE_P(difference) == IS_OBJECT && instanceof_function(Z_OBJCE_P(difference), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(difference); object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = self->data.smallint.value - smallint->data.smallint.value; if (result->data.smallint.value + smallint->data.smallint.value != self->data.smallint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Difference is out of range"); return; } } else { INVALID_ARGUMENT(difference, "a " PHP_DRIVER_NAMESPACE "\\Smallint"); } } /* }}} */ /* {{{ Smallint::mul() */ PHP_METHOD(Smallint, mul) { zval *multiplier; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &multiplier) == FAILURE) { return; } if (Z_TYPE_P(multiplier) == IS_OBJECT && instanceof_function(Z_OBJCE_P(multiplier), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(multiplier); object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = self->data.smallint.value * smallint->data.smallint.value; if (smallint->data.smallint.value != 0 && result->data.smallint.value / smallint->data.smallint.value != self->data.smallint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Product is out of range"); return; } } else { INVALID_ARGUMENT(multiplier, "a " PHP_DRIVER_NAMESPACE "\\Smallint"); } } /* }}} */ /* {{{ Smallint::div() */ PHP_METHOD(Smallint, div) { zval *divisor; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &divisor) == FAILURE) { return; } if (Z_TYPE_P(divisor) == IS_OBJECT && instanceof_function(Z_OBJCE_P(divisor), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(divisor); object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (smallint->data.smallint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } result->data.smallint.value = self->data.smallint.value / smallint->data.smallint.value; } else { INVALID_ARGUMENT(divisor, "a " PHP_DRIVER_NAMESPACE "\\Smallint"); } } /* }}} */ /* {{{ Smallint::mod() */ PHP_METHOD(Smallint, mod) { zval *divisor; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &divisor) == FAILURE) { return; } if (Z_TYPE_P(divisor) == IS_OBJECT && instanceof_function(Z_OBJCE_P(divisor), php_driver_smallint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *smallint = PHP_DRIVER_GET_NUMERIC(divisor); object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (smallint->data.smallint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot modulo by zero"); return; } result->data.smallint.value = self->data.smallint.value % smallint->data.smallint.value; } else { INVALID_ARGUMENT(divisor, "a " PHP_DRIVER_NAMESPACE "\\Smallint"); } } /* }}} */ /* {{{ Smallint::abs() */ PHP_METHOD(Smallint, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.smallint.value == INT16_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value doesn't exist"); return; } object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = self->data.smallint.value < 0 ? -self->data.smallint.value : self->data.smallint.value; } /* }}} */ /* {{{ Smallint::neg() */ PHP_METHOD(Smallint, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.smallint.value == INT16_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value doesn't exist"); return; } object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = -self->data.smallint.value; } /* }}} */ /* {{{ Smallint::sqrt() */ PHP_METHOD(Smallint, sqrt) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.smallint.value < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Cannot take a square root of a negative number"); } object_init_ex(return_value, php_driver_smallint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.smallint.value = (cass_int16_t) sqrt((long double) self->data.smallint.value); } /* }}} */ /* {{{ Smallint::toInt() */ PHP_METHOD(Smallint, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Smallint::toDouble() */ PHP_METHOD(Smallint, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_double(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Smallint::min() */ PHP_METHOD(Smallint, min) { php_driver_numeric *smallint = NULL; object_init_ex(return_value, php_driver_smallint_ce); smallint = PHP_DRIVER_GET_NUMERIC(return_value); smallint->data.smallint.value = INT16_MIN; } /* }}} */ /* {{{ Smallint::max() */ PHP_METHOD(Smallint, max) { php_driver_numeric *smallint = NULL; object_init_ex(return_value, php_driver_smallint_ce); smallint = PHP_DRIVER_GET_NUMERIC(return_value); smallint->data.smallint.value = INT16_MAX; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_smallint_methods[] = { PHP_ME(Smallint, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Smallint, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Smallint, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Smallint, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Smallint, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Smallint, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Smallint, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Smallint, min, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Smallint, max, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; static php_driver_value_handlers php_driver_smallint_handlers; static HashTable * php_driver_smallint_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_smallint_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval value; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_SMALL_INT TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(value); to_string(PHP5TO7_ZVAL_MAYBE_P(value), self TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); return props; } static int php_driver_smallint_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_numeric *smallint1 = NULL; php_driver_numeric *smallint2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ smallint1 = PHP_DRIVER_GET_NUMERIC(obj1); smallint2 = PHP_DRIVER_GET_NUMERIC(obj2); if (smallint1->data.smallint.value == smallint2->data.smallint.value) return 0; else if (smallint1->data.smallint.value < smallint2->data.smallint.value) return -1; else return 1; } static unsigned php_driver_smallint_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return 31 * 17 + self->data.smallint.value; } static int php_driver_smallint_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: return to_long(retval, self TSRMLS_CC); case IS_DOUBLE: return to_double(retval, self TSRMLS_CC); case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_smallint_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_smallint_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); self->type = PHP_DRIVER_SMALLINT; PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, smallint, self, ce); } void php_driver_define_Smallint(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Smallint", php_driver_smallint_methods); php_driver_smallint_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_smallint_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_smallint_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_smallint_ce->create_object = php_driver_smallint_new; memcpy(&php_driver_smallint_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_smallint_handlers.std.get_properties = php_driver_smallint_properties; #if PHP_VERSION_ID >= 50400 php_driver_smallint_handlers.std.get_gc = php_driver_smallint_gc; #endif php_driver_smallint_handlers.std.compare_objects = php_driver_smallint_compare; php_driver_smallint_handlers.std.cast_object = php_driver_smallint_cast; php_driver_smallint_handlers.hash_value = php_driver_smallint_hash_value; } cassandra-1.3.0/src/Smallint.h0000644000076500000240000000136713062515664016673 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_SMALLINT_H #define PHP_DRIVER_SMALLINT_H void php_driver_smallint_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_SMALLINT_H */ cassandra-1.3.0/src/Statement.c0000644000076500000240000000203113062515664017034 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_statement_ce = NULL; static zend_function_entry php_driver_statement_methods[] = { PHP_FE_END }; void php_driver_define_Statement(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Statement", php_driver_statement_methods); php_driver_statement_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_statement_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Table.c0000644000076500000240000000753213062515664016132 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "util/result.h" zend_class_entry *php_driver_table_ce = NULL; php5to7_zval php_driver_table_build_options(CassIterator* iterator TSRMLS_DC) { const char *name; size_t name_length; php5to7_zval zoptions; PHP5TO7_ZVAL_MAYBE_MAKE(zoptions); array_init(PHP5TO7_ZVAL_MAYBE_P(zoptions)); while (cass_iterator_next(iterator)) { const CassValue *value = NULL; if (cass_iterator_get_meta_field_name(iterator, &name, &name_length) == CASS_OK) { if (strncmp(name, "keyspace_name", name_length) == 0 || strncmp(name, "table_name", name_length) == 0 || strncmp(name, "columnfamily_name", name_length) == 0) { break; } value = cass_iterator_get_meta_field_value(iterator); if (value) { const CassDataType *data_type = cass_value_data_type(value); if (data_type) { php5to7_zval zvalue; PHP5TO7_ZVAL_UNDEF(zvalue); if (php_driver_value(value, data_type, &zvalue TSRMLS_CC) == SUCCESS) { PHP5TO7_ADD_ASSOC_ZVAL_EX(PHP5TO7_ZVAL_MAYBE_P(zoptions), name, name_length + 1, PHP5TO7_ZVAL_MAYBE_P(zvalue)); } } } } } return zoptions; } ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_table_methods[] = { PHP_ABSTRACT_ME(Table, name, arginfo_none) PHP_ABSTRACT_ME(Table, option, arginfo_name) PHP_ABSTRACT_ME(Table, options, arginfo_none) PHP_ABSTRACT_ME(Table, comment, arginfo_none) PHP_ABSTRACT_ME(Table, readRepairChance, arginfo_none) PHP_ABSTRACT_ME(Table, localReadRepairChance, arginfo_none) PHP_ABSTRACT_ME(Table, gcGraceSeconds, arginfo_none) PHP_ABSTRACT_ME(Table, caching, arginfo_none) PHP_ABSTRACT_ME(Table, bloomFilterFPChance, arginfo_none) PHP_ABSTRACT_ME(Table, memtableFlushPeriodMs, arginfo_none) PHP_ABSTRACT_ME(Table, defaultTTL, arginfo_none) PHP_ABSTRACT_ME(Table, speculativeRetry, arginfo_none) PHP_ABSTRACT_ME(Table, indexInterval, arginfo_none) PHP_ABSTRACT_ME(Table, compactionStrategyClassName, arginfo_none) PHP_ABSTRACT_ME(Table, compactionStrategyOptions, arginfo_none) PHP_ABSTRACT_ME(Table, compressionParameters, arginfo_none) PHP_ABSTRACT_ME(Table, populateIOCacheOnFlush, arginfo_none) PHP_ABSTRACT_ME(Table, replicateOnWrite, arginfo_none) PHP_ABSTRACT_ME(Table, maxIndexInterval, arginfo_none) PHP_ABSTRACT_ME(Table, minIndexInterval, arginfo_none) PHP_ABSTRACT_ME(Table, column, arginfo_name) PHP_ABSTRACT_ME(Table, columns, arginfo_none) PHP_ABSTRACT_ME(Table, partitionKey, arginfo_none) PHP_ABSTRACT_ME(Table, primaryKey, arginfo_none) PHP_ABSTRACT_ME(Table, clusteringKey, arginfo_none) PHP_ABSTRACT_ME(Table, clusteringOrder, arginfo_none) PHP_FE_END }; void php_driver_define_Table(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Table", php_driver_table_methods); php_driver_table_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_table_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Table.h0000644000076500000240000000143113062515664016127 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TABLE_H #define PHP_DRIVER_TABLE_H #include "php_driver.h" php5to7_zval php_driver_table_build_options(CassIterator* iterator TSRMLS_DC); #endif /* PHP_DRIVER_TABLE_H */ cassandra-1.3.0/src/Time.c0000644000076500000240000002034713062515664016000 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/types.h" #include "util/math.h" #include #include #include zend_class_entry *php_driver_time_ce = NULL; #if defined(_WIN32) #ifndef _WINSOCKAPI_ #define _WINSOCKAPI_ #endif #include #elif defined(__APPLE__) && defined(__MACH__) #include #else #include #endif #define NUM_NANOSECONDS_PER_DAY 86399999999999LL #define NANOSECONDS_PER_SECOND 1000000000LL #if defined(_WIN32) cass_int64_t php_driver_time_now_ns() { FILETIME ft; cass_int64_t ns100; GetSystemTimeAsFileTime(&ft); ns100 = (((cass_int64_t) ft.dwHighDateTime) << 32 | (cass_int64_t) ft.dwLowDateTime) - 116444736000000000LL; /* 100 nanosecond increments between */ /* Jan. 1, 1601 - Jan. 1, 1970 */ return (ns100 * 100) % NUM_NANOSECONDS_PER_DAY; } #elif defined(__APPLE__) && defined(__MACH__) cass_int64_t php_driver_time_now_ns() { struct timeval tv; gettimeofday(&tv, NULL); return cass_time_from_epoch((cass_int64_t) tv.tv_sec) + (cass_int64_t) tv.tv_usec * 1000; } #else cass_int64_t php_driver_time_now_ns() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return cass_time_from_epoch((cass_int64_t) ts.tv_sec) + (cass_int64_t) ts.tv_nsec; } #endif static int to_string(zval *result, php_driver_time *time TSRMLS_DC) { char *string; #ifdef WIN32 spprintf(&string, 0, "%I64d", (long long int) time->time); #else spprintf(&string, 0, "%lld", (long long int) time->time); #endif PHP5TO7_ZVAL_STRING(result, string); efree(string); return SUCCESS; } void php_driver_time_init(INTERNAL_FUNCTION_PARAMETERS) { zval *nanoseconds = NULL; php_driver_time *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &nanoseconds) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_time_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_TIME(getThis()); } else { object_init_ex(return_value, php_driver_time_ce); self = PHP_DRIVER_GET_TIME(return_value); } if (nanoseconds == NULL) { self->time = php_driver_time_now_ns(); } else { if (Z_TYPE_P(nanoseconds) == IS_LONG) { self->time = Z_LVAL_P(nanoseconds); } else if (Z_TYPE_P(nanoseconds) == IS_STRING) { if (!php_driver_parse_bigint(Z_STRVAL_P(nanoseconds), Z_STRLEN_P(nanoseconds), &self->time TSRMLS_CC)) { return; } } else { INVALID_ARGUMENT(nanoseconds, "a string or int representing a number of nanoseconds since midnight"); } if (self->time < 0 || self->time > NUM_NANOSECONDS_PER_DAY) { INVALID_ARGUMENT(nanoseconds, "nanoseconds since midnight"); } } } /* {{{ Time::__construct(string) */ PHP_METHOD(Time, __construct) { php_driver_time_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Time::type() */ PHP_METHOD(Time, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_TIME TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Time::seconds() */ PHP_METHOD(Time, seconds) { php_driver_time *self = PHP_DRIVER_GET_TIME(getThis()); RETURN_LONG(self->time / NANOSECONDS_PER_SECOND); } /* }}} */ /* {{{ Time::fromDateTime() */ PHP_METHOD(Time, fromDateTime) { php_driver_time *self; zval *zdatetime; php5to7_zval retval; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdatetime) == FAILURE) { return; } zend_call_method_with_0_params(PHP5TO7_ZVAL_MAYBE_ADDR_OF(zdatetime), php_date_get_date_ce(), NULL, "gettimestamp", &retval); if (!PHP5TO7_ZVAL_IS_UNDEF(retval) && Z_TYPE_P(PHP5TO7_ZVAL_MAYBE_P(retval)) == IS_LONG) { object_init_ex(return_value, php_driver_time_ce); self = PHP_DRIVER_GET_TIME(return_value); self->time = cass_time_from_epoch(PHP5TO7_Z_LVAL_MAYBE_P(retval)); zval_ptr_dtor(&retval); return; } } /* }}} */ /* {{{ Time::__toString() */ PHP_METHOD(Time, __toString) { php_driver_time *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TIME(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, nanoseconds) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_datetime, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_OBJ_INFO(0, datetime, DateTime, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_time_methods[] = { PHP_ME(Time, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Time, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Time, seconds, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Time, fromDateTime, arginfo_datetime, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Time, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_time_handlers; static HashTable * php_driver_time_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_time_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval nanoseconds; php_driver_time *self = PHP_DRIVER_GET_TIME(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_TIME TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(nanoseconds); to_string(PHP5TO7_ZVAL_MAYBE_P(nanoseconds), self TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "nanoseconds", sizeof("nanoseconds"), PHP5TO7_ZVAL_MAYBE_P(nanoseconds), sizeof(zval)); return props; } static int php_driver_time_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_time *time1 = NULL; php_driver_time *time2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ time1 = PHP_DRIVER_GET_TIME(obj1); time2 = PHP_DRIVER_GET_TIME(obj2); return PHP_DRIVER_COMPARE(time1->time, time2->time); } static unsigned php_driver_time_hash_value(zval *obj TSRMLS_DC) { php_driver_time *self = PHP_DRIVER_GET_TIME(obj); return php_driver_bigint_hash(self->time); } static void php_driver_time_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_time *self = PHP5TO7_ZEND_OBJECT_GET(time, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_time_new(zend_class_entry *ce TSRMLS_DC) { php_driver_time *self = PHP5TO7_ZEND_OBJECT_ECALLOC(time, ce); self->time = 0; PHP5TO7_ZEND_OBJECT_INIT(time, self, ce); } void php_driver_define_Time(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Time", php_driver_time_methods); php_driver_time_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_time_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_time_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_time_handlers.std.get_properties = php_driver_time_properties; #if PHP_VERSION_ID >= 50400 php_driver_time_handlers.std.get_gc = php_driver_time_gc; #endif php_driver_time_handlers.std.compare_objects = php_driver_time_compare; php_driver_time_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_time_ce->create_object = php_driver_time_new; php_driver_time_handlers.hash_value = php_driver_time_hash_value; } cassandra-1.3.0/src/Time.h0000644000076500000240000000134713062515664016004 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TIME_H #define PHP_DRIVER_TIME_H void php_driver_time_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_TIME_H */ cassandra-1.3.0/src/Timestamp.c0000644000076500000240000001767513062515664017057 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/types.h" #include zend_class_entry *php_driver_timestamp_ce = NULL; void php_driver_timestamp_init(INTERNAL_FUNCTION_PARAMETERS) { cass_int64_t seconds = 0; cass_int64_t microseconds = 0; php_driver_timestamp *self; cass_int64_t value = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &seconds, µseconds) == FAILURE) { return; } if (ZEND_NUM_ARGS() == 0) { #ifdef WIN32 seconds = (cass_int64_t) time(0); #else struct timeval time; gettimeofday(&time, NULL); seconds = time.tv_sec; microseconds = (time.tv_usec / 1000) * 1000; #endif } value += microseconds / 1000; value += (seconds * 1000); if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_timestamp_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_TIMESTAMP(getThis()); } else { object_init_ex(return_value, php_driver_timestamp_ce); self = PHP_DRIVER_GET_TIMESTAMP(return_value); } self->timestamp = value; } /* {{{ Timestamp::__construct(string) */ PHP_METHOD(Timestamp, __construct) { php_driver_timestamp_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Timestamp::type() */ PHP_METHOD(Timestamp, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_TIMESTAMP TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Timestamp::time */ PHP_METHOD(Timestamp, time) { php_driver_timestamp *self = PHP_DRIVER_GET_TIMESTAMP(getThis()); RETURN_LONG(self->timestamp / 1000); } /* }}} */ /* {{{ Timestamp::microtime(bool) */ PHP_METHOD(Timestamp, microtime) { zend_bool get_as_float = 0; php_driver_timestamp *self; char *ret = NULL; long sec = -1; double usec = 0.0f; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) { return; } self = PHP_DRIVER_GET_TIMESTAMP(getThis()); if (get_as_float) { RETURN_DOUBLE((double) self->timestamp / 1000.00); } sec = (long) (self->timestamp / 1000); usec = (double) ((self->timestamp - (sec * 1000)) / 1000.00); spprintf(&ret, 0, "%.8F %ld", usec, sec); PHP5TO7_RETVAL_STRING(ret); efree(ret); } /* }}} */ /* {{{ Timestamp::toDateTime() */ PHP_METHOD(Timestamp, toDateTime) { php_driver_timestamp *self; zval datetime_object; zval *datetime = &datetime_object; php_date_obj *datetime_obj = NULL; char *str; int str_len; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TIMESTAMP(getThis()); PHP5TO7_ZVAL_MAYBE_MAKE(datetime); php_date_instantiate(php_date_get_date_ce(), datetime TSRMLS_CC); #if PHP_MAJOR_VERSION >= 7 datetime_obj = php_date_obj_from_obj(Z_OBJ_P(datetime)); #else datetime_obj = zend_object_store_get_object(datetime TSRMLS_CC); #endif str_len = spprintf(&str, 0, "@%ld", (long) (self->timestamp / 1000)); php_date_initialize(datetime_obj, str, str_len, NULL, NULL, 0 TSRMLS_CC); efree(str); RETVAL_ZVAL(datetime, 0, 0); } /* }}} */ /* {{{ Timestamp::__toString() */ PHP_METHOD(Timestamp, __toString) { php_driver_timestamp *self; char *ret = NULL; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TIMESTAMP(getThis()); spprintf(&ret, 0, "%lld", (long long int) self->timestamp); PHP5TO7_RETVAL_STRING(ret); efree(ret); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, seconds) ZEND_ARG_INFO(0, microseconds) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_microtime, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, get_as_float) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_timestamp_methods[] = { PHP_ME(Timestamp, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Timestamp, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timestamp, time, NULL, ZEND_ACC_PUBLIC) PHP_ME(Timestamp, microtime, arginfo_microtime, ZEND_ACC_PUBLIC) PHP_ME(Timestamp, toDateTime, NULL, ZEND_ACC_PUBLIC) PHP_ME(Timestamp, __toString, NULL, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_timestamp_handlers; static HashTable * php_driver_timestamp_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_timestamp_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval seconds; php5to7_zval microseconds; php_driver_timestamp *self = PHP_DRIVER_GET_TIMESTAMP(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); long sec = (long) (self->timestamp / 1000); long usec = (long) ((self->timestamp - (sec * 1000)) * 1000); type = php_driver_type_scalar(CASS_VALUE_TYPE_TIMESTAMP TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(seconds); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(seconds), sec); PHP5TO7_ZEND_HASH_UPDATE(props, "seconds", sizeof("seconds"), PHP5TO7_ZVAL_MAYBE_P(seconds), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(microseconds); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(microseconds), usec); PHP5TO7_ZEND_HASH_UPDATE(props, "microseconds", sizeof("microseconds"), PHP5TO7_ZVAL_MAYBE_P(microseconds), sizeof(zval)); return props; } static int php_driver_timestamp_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_timestamp *timestamp1 = NULL; php_driver_timestamp *timestamp2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ timestamp1 = PHP_DRIVER_GET_TIMESTAMP(obj1); timestamp2 = PHP_DRIVER_GET_TIMESTAMP(obj2); return PHP_DRIVER_COMPARE(timestamp1->timestamp, timestamp2->timestamp); } static unsigned php_driver_timestamp_hash_value(zval *obj TSRMLS_DC) { php_driver_timestamp *self = PHP_DRIVER_GET_TIMESTAMP(obj); return php_driver_bigint_hash(self->timestamp); } static void php_driver_timestamp_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_timestamp *self = PHP5TO7_ZEND_OBJECT_GET(timestamp, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_timestamp_new(zend_class_entry *ce TSRMLS_DC) { php_driver_timestamp *self = PHP5TO7_ZEND_OBJECT_ECALLOC(timestamp, ce); PHP5TO7_ZEND_OBJECT_INIT(timestamp, self, ce); } void php_driver_define_Timestamp(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Timestamp", php_driver_timestamp_methods); php_driver_timestamp_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_timestamp_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_timestamp_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_timestamp_handlers.std.get_properties = php_driver_timestamp_properties; #if PHP_VERSION_ID >= 50400 php_driver_timestamp_handlers.std.get_gc = php_driver_timestamp_gc; #endif php_driver_timestamp_handlers.std.compare_objects = php_driver_timestamp_compare; php_driver_timestamp_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_timestamp_ce->create_object = php_driver_timestamp_new; php_driver_timestamp_handlers.hash_value = php_driver_timestamp_hash_value; php_driver_timestamp_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Timestamp.h0000644000076500000240000000137313062515664017050 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TIMESTAMP_H #define PHP_DRIVER_TIMESTAMP_H void php_driver_timestamp_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_TIMESTAMP_H */ cassandra-1.3.0/src/TimestampGenerator.c0000644000076500000240000000207713062515664020714 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_timestamp_gen_ce = NULL; static zend_function_entry php_driver_timestamp_gen_methods[] = { PHP_FE_END }; void php_driver_define_TimestampGenerator(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\TimestampGenerator", php_driver_timestamp_gen_methods); php_driver_timestamp_gen_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_timestamp_gen_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/TimestampGenerator/Monotonic.c0000644000076500000240000000427213062515664022660 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_timestamp_gen_monotonic_ce = NULL; static zend_function_entry php_driver_timestamp_gen_monotonic_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_timestamp_gen_monotonic_handlers; static void php_driver_timestamp_gen_monotonic_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_timestamp_gen *self = PHP5TO7_ZEND_OBJECT_GET(timestamp_gen, object); cass_timestamp_gen_free(self->gen); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_timestamp_gen_monotonic_new(zend_class_entry *ce TSRMLS_DC) { php_driver_timestamp_gen *self = PHP5TO7_ZEND_OBJECT_ECALLOC(timestamp_gen, ce); self->gen = cass_timestamp_gen_monotonic_new(); PHP5TO7_ZEND_OBJECT_INIT_EX(timestamp_gen, timestamp_gen_monotonic, self, ce); } void php_driver_define_TimestampGeneratorMonotonic(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\TimestampGenerator\\Monotonic", php_driver_timestamp_gen_monotonic_methods); php_driver_timestamp_gen_monotonic_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_timestamp_gen_monotonic_ce TSRMLS_CC, 1, php_driver_timestamp_gen_ce); php_driver_timestamp_gen_monotonic_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_timestamp_gen_monotonic_ce->create_object = php_driver_timestamp_gen_monotonic_new; memcpy(&php_driver_timestamp_gen_monotonic_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/TimestampGenerator/ServerSide.c0000644000076500000240000000433013062515664022761 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_timestamp_gen_server_side_ce = NULL; static zend_function_entry php_driver_timestamp_gen_server_side_methods[] = { PHP_FE_END }; static zend_object_handlers php_driver_timestamp_gen_server_side_handlers; static void php_driver_timestamp_gen_server_side_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_timestamp_gen *self = PHP5TO7_ZEND_OBJECT_GET(timestamp_gen, object); cass_timestamp_gen_free(self->gen); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_timestamp_gen_server_side_new(zend_class_entry *ce TSRMLS_DC) { php_driver_timestamp_gen *self = PHP5TO7_ZEND_OBJECT_ECALLOC(timestamp_gen, ce); self->gen = cass_timestamp_gen_server_side_new(); PHP5TO7_ZEND_OBJECT_INIT_EX(timestamp_gen, timestamp_gen_server_side, self, ce); } void php_driver_define_TimestampGeneratorServerSide(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\TimestampGenerator\\ServerSide", php_driver_timestamp_gen_server_side_methods); php_driver_timestamp_gen_server_side_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_timestamp_gen_server_side_ce TSRMLS_CC, 1, php_driver_timestamp_gen_ce); php_driver_timestamp_gen_server_side_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_timestamp_gen_server_side_ce->create_object = php_driver_timestamp_gen_server_side_new; memcpy(&php_driver_timestamp_gen_server_side_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); } cassandra-1.3.0/src/Timeuuid.c0000644000076500000240000001745713062515664016677 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/types.h" #include "util/uuid_gen.h" #include zend_class_entry *php_driver_timeuuid_ce = NULL; void php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS) { long timestamp; php_driver_uuid *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", ×tamp) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_timeuuid_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_UUID(getThis()); } else { object_init_ex(return_value, php_driver_timeuuid_ce); self = PHP_DRIVER_GET_UUID(return_value); } if (ZEND_NUM_ARGS() == 0) { php_driver_uuid_generate_time(&self->uuid TSRMLS_CC); } else { if (timestamp < 0) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Timestamp must be a positive integer, %d given", timestamp); return; } php_driver_uuid_generate_from_time(timestamp, &self->uuid TSRMLS_CC); } } /* {{{ Timeuuid::__construct(string) */ PHP_METHOD(Timeuuid, __construct) { php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Timeuuid::__toString() */ PHP_METHOD(Timeuuid, __toString) { char string[CASS_UUID_STRING_LENGTH]; php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); cass_uuid_string(self->uuid, string); PHP5TO7_RETVAL_STRING(string); } /* }}} */ /* {{{ Timeuuid::type() */ PHP_METHOD(Timeuuid, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_TIMEUUID TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Timeuuid::value() */ PHP_METHOD(Timeuuid, uuid) { char string[CASS_UUID_STRING_LENGTH]; php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); cass_uuid_string(self->uuid, string); PHP5TO7_RETVAL_STRING(string); } /* }}} */ /* {{{ Timeuuid::version() */ PHP_METHOD(Timeuuid, version) { php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); RETURN_LONG((long) cass_uuid_version(self->uuid)); } /* }}} */ /* {{{ Timeuuid::time() */ PHP_METHOD(Timeuuid, time) { php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); RETURN_LONG((long) (cass_uuid_timestamp(self->uuid) / 1000)); } /* }}} */ /* {{{ Timeuuid::toDateTime() */ PHP_METHOD(Timeuuid, toDateTime) { php_driver_uuid *self; zval datetime_object; zval *datetime = &datetime_object; php_date_obj *datetime_obj = NULL; char *str; int str_len; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_UUID(getThis()); PHP5TO7_ZVAL_MAYBE_MAKE(datetime); php_date_instantiate(php_date_get_date_ce(), datetime TSRMLS_CC); #if PHP_MAJOR_VERSION >= 7 datetime_obj = php_date_obj_from_obj(Z_OBJ_P(datetime)); #else datetime_obj = zend_object_store_get_object(datetime TSRMLS_CC); #endif str_len = spprintf(&str, 0, "@%ld", (long) (cass_uuid_timestamp(self->uuid) / 1000)); php_date_initialize(datetime_obj, str, str_len, NULL, NULL, 0 TSRMLS_CC); efree(str); RETVAL_ZVAL(datetime, 0, 0); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, timestamp) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_timeuuid_methods[] = { PHP_ME(Timeuuid, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, uuid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, version, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, time, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Timeuuid, toDateTime, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_timeuuid_handlers; static HashTable * php_driver_timeuuid_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_timeuuid_properties(zval *object TSRMLS_DC) { char string[CASS_UUID_STRING_LENGTH]; php5to7_zval type; php5to7_zval uuid; php5to7_zval version; php_driver_uuid *self = PHP_DRIVER_GET_UUID(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_TIMEUUID TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); cass_uuid_string(self->uuid, string); PHP5TO7_ZVAL_MAYBE_MAKE(uuid); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(uuid), string); PHP5TO7_ZEND_HASH_UPDATE(props, "uuid", sizeof("uuid"), PHP5TO7_ZVAL_MAYBE_P(uuid), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(version); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(version), (long) cass_uuid_version(self->uuid)); PHP5TO7_ZEND_HASH_UPDATE(props, "version", sizeof("version"), PHP5TO7_ZVAL_MAYBE_P(version), sizeof(zval)); return props; } static int php_driver_timeuuid_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_uuid *uuid1 = NULL; php_driver_uuid *uuid2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ uuid1 = PHP_DRIVER_GET_UUID(obj1); uuid2 = PHP_DRIVER_GET_UUID(obj2); if (uuid1->uuid.time_and_version != uuid2->uuid.time_and_version) return uuid1->uuid.time_and_version < uuid2->uuid.time_and_version ? -1 : 1; if (uuid1->uuid.clock_seq_and_node != uuid2->uuid.clock_seq_and_node) return uuid1->uuid.clock_seq_and_node < uuid2->uuid.clock_seq_and_node ? -1 : 1; return 0; } static unsigned php_driver_timeuuid_hash_value(zval *obj TSRMLS_DC) { php_driver_uuid *self = PHP_DRIVER_GET_UUID(obj); return php_driver_combine_hash(php_driver_bigint_hash(self->uuid.time_and_version), php_driver_bigint_hash(self->uuid.clock_seq_and_node)); } static void php_driver_timeuuid_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_uuid *self = PHP5TO7_ZEND_OBJECT_GET(uuid, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_timeuuid_new(zend_class_entry *ce TSRMLS_DC) { php_driver_uuid *self = PHP5TO7_ZEND_OBJECT_ECALLOC(uuid, ce); PHP5TO7_ZEND_OBJECT_INIT_EX(uuid, timeuuid, self, ce); } void php_driver_define_Timeuuid(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Timeuuid", php_driver_timeuuid_methods); php_driver_timeuuid_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_timeuuid_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_uuid_interface_ce); memcpy(&php_driver_timeuuid_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_timeuuid_handlers.std.get_properties = php_driver_timeuuid_properties; #if PHP_VERSION_ID >= 50400 php_driver_timeuuid_handlers.std.get_gc = php_driver_timeuuid_gc; #endif php_driver_timeuuid_handlers.std.compare_objects = php_driver_timeuuid_compare; php_driver_timeuuid_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_timeuuid_ce->create_object = php_driver_timeuuid_new; php_driver_timeuuid_handlers.hash_value = php_driver_timeuuid_hash_value; php_driver_timeuuid_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Timeuuid.h0000644000076500000240000000136713062515664016675 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TIMEUUID_H #define PHP_DRIVER_TIMEUUID_H void php_driver_timeuuid_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_TIMEUUID_H */ cassandra-1.3.0/src/Tinyint.c0000644000076500000240000004071313062515664016537 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #if !defined(HAVE_STDINT_H) && !defined(_MSC_STDINT_H_) # define INT8_MAX 127 # define INT8_MIN (-INT8_MAX-1) #endif zend_class_entry *php_driver_tinyint_ce = NULL; static int to_double(zval *result, php_driver_numeric *tinyint TSRMLS_DC) { ZVAL_DOUBLE(result, (double) tinyint->data.tinyint.value); return SUCCESS; } static int to_long(zval *result, php_driver_numeric *tinyint TSRMLS_DC) { ZVAL_LONG(result, (php5to7_long) tinyint->data.tinyint.value); return SUCCESS; } static int to_string(zval *result, php_driver_numeric *tinyint TSRMLS_DC) { char *string; spprintf(&string, 0, "%d", tinyint->data.tinyint.value); PHP5TO7_ZVAL_STRING(result, string); efree(string); return SUCCESS; } void php_driver_tinyint_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_numeric *self; zval *value; cass_int32_t number; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_tinyint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_tinyint_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *other = PHP_DRIVER_GET_NUMERIC(value); self->data.tinyint.value = other->data.tinyint.value; } else { if (Z_TYPE_P(value) == IS_LONG) { number = (cass_int32_t) Z_LVAL_P(value); if (number < INT8_MIN || number > INT8_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -128 and 127, %ld given", Z_LVAL_P(value)); return; } } else if (Z_TYPE_P(value) == IS_DOUBLE) { number = (cass_int32_t) Z_DVAL_P(value); if (number < INT8_MIN || number > INT8_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -128 and 127, %g given", Z_DVAL_P(value)); return; } } else if (Z_TYPE_P(value) == IS_STRING) { if (!php_driver_parse_int(Z_STRVAL_P(value), Z_STRLEN_P(value), &number TSRMLS_CC)) { // If the parsing function fails, it would have set an exception. If it's // a range error, the error message would be wrong because the parsing // function supports all 32-bit values, so the "valid" range it reports would // be too large for Tinyint. Reset the exception in that case. if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -128 and 127, %s given", Z_STRVAL_P(value)); } return; } if (number < INT8_MIN || number > INT8_MAX) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between -128 and 127, %s given", Z_STRVAL_P(value)); return; } } else { INVALID_ARGUMENT(value, "a long, a double, a numeric string or a " \ PHP_DRIVER_NAMESPACE "\\Tinyint"); } self->data.tinyint.value = (cass_int8_t) number; } } /* {{{ Tinyint::__construct(string) */ PHP_METHOD(Tinyint, __construct) { php_driver_tinyint_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Tinyint::__toString() */ PHP_METHOD(Tinyint, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Tinyint::type() */ PHP_METHOD(Tinyint, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_TINY_INT TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Tinyint::value() */ PHP_METHOD(Tinyint, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Tinyint::add() */ PHP_METHOD(Tinyint, add) { zval *addend; php_driver_numeric *self; php_driver_numeric *tinyint; php_driver_numeric *result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &addend) == FAILURE) { return; } if (Z_TYPE_P(addend) == IS_OBJECT && instanceof_function(Z_OBJCE_P(addend), php_driver_tinyint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); tinyint = PHP_DRIVER_GET_NUMERIC(addend); object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = self->data.tinyint.value + tinyint->data.tinyint.value; if (result->data.tinyint.value - tinyint->data.tinyint.value != self->data.tinyint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Sum is out of range"); return; } } else { INVALID_ARGUMENT(addend, "a " PHP_DRIVER_NAMESPACE "\\Tinyint"); } } /* }}} */ /* {{{ Tinyint::sub() */ PHP_METHOD(Tinyint, sub) { zval *difference; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &difference) == FAILURE) { return; } if (Z_TYPE_P(difference) == IS_OBJECT && instanceof_function(Z_OBJCE_P(difference), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(difference); object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = self->data.tinyint.value - tinyint->data.tinyint.value; if (result->data.tinyint.value + tinyint->data.tinyint.value != self->data.tinyint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Difference is out of range"); return; } } else { INVALID_ARGUMENT(difference, "a " PHP_DRIVER_NAMESPACE "\\Tinyint"); } } /* }}} */ /* {{{ Tinyint::mul() */ PHP_METHOD(Tinyint, mul) { zval *multiplier; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &multiplier) == FAILURE) { return; } if (Z_TYPE_P(multiplier) == IS_OBJECT && instanceof_function(Z_OBJCE_P(multiplier), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(multiplier); object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = self->data.tinyint.value * tinyint->data.tinyint.value; if (tinyint->data.tinyint.value != 0 && result->data.tinyint.value / tinyint->data.tinyint.value != self->data.tinyint.value) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Product is out of range"); return; } } else { INVALID_ARGUMENT(multiplier, "a " PHP_DRIVER_NAMESPACE "\\Tinyint"); } } /* }}} */ /* {{{ Tinyint::div() */ PHP_METHOD(Tinyint, div) { zval *divisor; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &divisor) == FAILURE) { return; } if (Z_TYPE_P(divisor) == IS_OBJECT && instanceof_function(Z_OBJCE_P(divisor), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(divisor); object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (tinyint->data.tinyint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } result->data.tinyint.value = self->data.tinyint.value / tinyint->data.tinyint.value; } else { INVALID_ARGUMENT(divisor, "a " PHP_DRIVER_NAMESPACE "\\Tinyint"); } } /* }}} */ /* {{{ Tinyint::mod() */ PHP_METHOD(Tinyint, mod) { zval *divisor; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &divisor) == FAILURE) { return; } if (Z_TYPE_P(divisor) == IS_OBJECT && instanceof_function(Z_OBJCE_P(divisor), php_driver_tinyint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *tinyint = PHP_DRIVER_GET_NUMERIC(divisor); object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (tinyint->data.tinyint.value == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot modulo by zero"); return; } result->data.tinyint.value = self->data.tinyint.value % tinyint->data.tinyint.value; } else { INVALID_ARGUMENT(divisor, "a " PHP_DRIVER_NAMESPACE "\\Tinyint"); } } /* }}} */ /* {{{ Tinyint::abs() */ PHP_METHOD(Tinyint, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.tinyint.value == INT8_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value doesn't exist"); return; } object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = self->data.tinyint.value < 0 ? -self->data.tinyint.value : self->data.tinyint.value; } /* }}} */ /* {{{ Tinyint::neg() */ PHP_METHOD(Tinyint, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.tinyint.value == INT8_MIN) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value doesn't exist"); return; } object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = -self->data.tinyint.value; } /* }}} */ /* {{{ Tinyint::sqrt() */ PHP_METHOD(Tinyint, sqrt) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (self->data.tinyint.value < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Cannot take a square root of a negative number"); return; } object_init_ex(return_value, php_driver_tinyint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); result->data.tinyint.value = (cass_int8_t) sqrt((long double) self->data.tinyint.value); } /* }}} */ /* {{{ Tinyint::toInt() */ PHP_METHOD(Tinyint, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Tinyint::toDouble() */ PHP_METHOD(Tinyint, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_double(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Tinyint::min() */ PHP_METHOD(Tinyint, min) { php_driver_numeric *tinyint = NULL; object_init_ex(return_value, php_driver_tinyint_ce); tinyint = PHP_DRIVER_GET_NUMERIC(return_value); tinyint->data.tinyint.value = INT8_MIN; } /* }}} */ /* {{{ Tinyint::max() */ PHP_METHOD(Tinyint, max) { php_driver_numeric *tinyint = NULL; object_init_ex(return_value, php_driver_tinyint_ce); tinyint = PHP_DRIVER_GET_NUMERIC(return_value); tinyint->data.tinyint.value = INT8_MAX; } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_tinyint_methods[] = { PHP_ME(Tinyint, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Tinyint, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tinyint, min, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Tinyint, max, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; static php_driver_value_handlers php_driver_tinyint_handlers; static HashTable * php_driver_tinyint_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_tinyint_properties(zval *object TSRMLS_DC) { php5to7_zval type; php5to7_zval value; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); type = php_driver_type_scalar(CASS_VALUE_TYPE_TINY_INT TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(value); to_string(PHP5TO7_ZVAL_MAYBE_P(value), self TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); return props; } static int php_driver_tinyint_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_numeric *tinyint1 = NULL; php_driver_numeric *tinyint2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ tinyint1 = PHP_DRIVER_GET_NUMERIC(obj1); tinyint2 = PHP_DRIVER_GET_NUMERIC(obj2); if (tinyint1->data.tinyint.value == tinyint2->data.tinyint.value) return 0; else if (tinyint1->data.tinyint.value < tinyint2->data.tinyint.value) return -1; else return 1; } static unsigned php_driver_tinyint_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return 31 * 17 + self->data.tinyint.value; } static int php_driver_tinyint_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: return to_long(retval, self TSRMLS_CC); case IS_DOUBLE: return to_double(retval, self TSRMLS_CC); case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_tinyint_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_tinyint_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); self->type = PHP_DRIVER_TINYINT; PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, tinyint, self, ce); } void php_driver_define_Tinyint(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Tinyint", php_driver_tinyint_methods); php_driver_tinyint_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_tinyint_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_tinyint_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_tinyint_ce->create_object = php_driver_tinyint_new; memcpy(&php_driver_tinyint_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_tinyint_handlers.std.get_properties = php_driver_tinyint_properties; #if PHP_VERSION_ID >= 50400 php_driver_tinyint_handlers.std.get_gc = php_driver_tinyint_gc; #endif php_driver_tinyint_handlers.std.compare_objects = php_driver_tinyint_compare; php_driver_tinyint_handlers.std.cast_object = php_driver_tinyint_cast; php_driver_tinyint_handlers.hash_value = php_driver_tinyint_hash_value; } cassandra-1.3.0/src/Tinyint.h0000644000076500000240000000136313062515664016542 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TINYINT_H #define PHP_DRIVER_TINYINT_H void php_driver_tinyint_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_TINYINT_H */ cassandra-1.3.0/src/Tuple.c0000644000076500000240000003224613062515664016174 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/collections.h" #include "util/hash.h" #include "util/types.h" #include "src/Type/Tuple.h" #include "src/Tuple.h" #include zend_class_entry *php_driver_tuple_ce = NULL; void php_driver_tuple_set(php_driver_tuple *tuple, ulong index, zval *object TSRMLS_DC) { PHP5TO7_ZEND_HASH_INDEX_UPDATE(&tuple->values, index, object, sizeof(zval *)); Z_TRY_ADDREF_P(object); tuple->dirty = 1; } static void php_driver_tuple_populate(php_driver_tuple *tuple, zval *array TSRMLS_DC) { php5to7_ulong index; php_driver_type *type; php5to7_zval *current; php5to7_zval null; PHP5TO7_ZVAL_MAYBE_MAKE(null); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(null)); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(tuple->type)); PHP5TO7_ZEND_HASH_FOREACH_NUM_KEY_VAL(&type->data.tuple.types, index, current) { php5to7_zval *value = NULL; (void) current; if (PHP5TO7_ZEND_HASH_INDEX_FIND(&tuple->values, index, value)) { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_DEREF(value)) == SUCCESS) Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_DEREF(value)); else break; } else { if (add_next_index_zval(array, PHP5TO7_ZVAL_MAYBE_P(null)) == SUCCESS) Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(null)); else break; } } PHP5TO7_ZEND_HASH_FOREACH_END(&type->data.tuple.types); #if PHP_MAJOR_VERSION < 7 zval_ptr_dtor(&null); #endif } /* {{{ Tuple::__construct(types) */ PHP_METHOD(Tuple, __construct) { php_driver_tuple *self; php_driver_type *type; HashTable *types; php5to7_zval *current; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "h", &types) == FAILURE) { return; } self = PHP_DRIVER_GET_TUPLE(getThis()); self->type = php_driver_type_tuple(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZEND_HASH_FOREACH_VAL(types, current) { zval *sub_type = PHP5TO7_ZVAL_MAYBE_DEREF(current); php5to7_zval scalar_type; if (Z_TYPE_P(sub_type) == IS_STRING) { CassValueType value_type; if (!php_driver_value_type(Z_STRVAL_P(sub_type), &value_type TSRMLS_CC)) { return; } scalar_type = php_driver_type_scalar(value_type TSRMLS_CC); if (!php_driver_type_tuple_add(type, PHP5TO7_ZVAL_MAYBE_P(scalar_type) TSRMLS_CC)) { return; } } else if (Z_TYPE_P(sub_type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(sub_type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(sub_type, "type" TSRMLS_CC)) { return; } if (php_driver_type_tuple_add(type, sub_type TSRMLS_CC)) { Z_ADDREF_P(sub_type); } else { return; } } else { INVALID_ARGUMENT(sub_type, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type"); } } PHP5TO7_ZEND_HASH_FOREACH_END(types); } /* }}} */ /* {{{ Tuple::type() */ PHP_METHOD(Tuple, type) { php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } /* {{{ Tuple::values() */ PHP_METHOD(Tuple, values) { php_driver_tuple *self = NULL; array_init(return_value); self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_tuple_populate(self, return_value TSRMLS_CC); } /* }}} */ /* {{{ Tuple::set(int, mixed) */ PHP_METHOD(Tuple, set) { php_driver_tuple *self = NULL; long index; php_driver_type *type; php5to7_zval *sub_type; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &value) == FAILURE) return; self = PHP_DRIVER_GET_TUPLE(getThis()); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (index < 0 || index >= zend_hash_num_elements(&type->data.tuple.types)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Index out of bounds"); return; } if (!PHP5TO7_ZEND_HASH_INDEX_FIND(&type->data.tuple.types, index, sub_type) || !php_driver_validate_object(value, PHP5TO7_ZVAL_MAYBE_DEREF(sub_type) TSRMLS_CC)) { return; } php_driver_tuple_set(self, index, value TSRMLS_CC); } /* }}} */ /* {{{ Tuple::get(int) */ PHP_METHOD(Tuple, get) { php_driver_tuple *self = NULL; long index; php_driver_type *type; php5to7_zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) return; self = PHP_DRIVER_GET_TUPLE(getThis()); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (index < 0 || index >= zend_hash_num_elements(&type->data.tuple.types)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Index out of bounds"); return; } if (PHP5TO7_ZEND_HASH_INDEX_FIND(&self->values, index, value)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(value), 1, 0); } } /* }}} */ /* {{{ Tuple::count() */ PHP_METHOD(Tuple, count) { php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); RETURN_LONG(zend_hash_num_elements(&type->data.tuple.types)); } /* }}} */ /* {{{ Tuple::current() */ PHP_METHOD(Tuple, current) { php5to7_ulong index; php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type->data.tuple.types, NULL, &index, &self->pos) == HASH_KEY_IS_LONG) { php5to7_zval *value; if (PHP5TO7_ZEND_HASH_INDEX_FIND(&self->values, index, value)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(value), 1, 0); } } } /* }}} */ /* {{{ Tuple::key() */ PHP_METHOD(Tuple, key) { php5to7_ulong index; php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type->data.tuple.types, NULL, &index, &self->pos) == HASH_KEY_IS_LONG) { RETURN_LONG(index); } } /* }}} */ /* {{{ Tuple::next() */ PHP_METHOD(Tuple, next) { php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); zend_hash_move_forward_ex(&type->data.tuple.types, &self->pos); } /* }}} */ /* {{{ Tuple::valid() */ PHP_METHOD(Tuple, valid) { php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); RETURN_BOOL(zend_hash_has_more_elements_ex(&type->data.tuple.types, &self->pos) == SUCCESS); } /* }}} */ /* {{{ Tuple::rewind() */ PHP_METHOD(Tuple, rewind) { php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); zend_hash_internal_pointer_reset_ex(&type->data.tuple.types, &self->pos); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, types) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_index, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, index) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_tuple_methods[] = { PHP_ME(Tuple, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Tuple, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, values, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, set, arginfo_value, ZEND_ACC_PUBLIC) PHP_ME(Tuple, get, arginfo_index, ZEND_ACC_PUBLIC) /* Countable */ PHP_ME(Tuple, count, arginfo_none, ZEND_ACC_PUBLIC) /* Iterator */ PHP_ME(Tuple, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Tuple, rewind, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_tuple_handlers; static HashTable * php_driver_tuple_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_tuple_properties(zval *object TSRMLS_DC) { php5to7_zval values; php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(self->type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZVAL_MAYBE_MAKE(values); array_init(PHP5TO7_ZVAL_MAYBE_P(values)); php_driver_tuple_populate(self, PHP5TO7_ZVAL_MAYBE_P(values) TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "values", sizeof("values"), PHP5TO7_ZVAL_MAYBE_P(values), sizeof(zval)); return props; } static int php_driver_tuple_compare(zval *obj1, zval *obj2 TSRMLS_DC) { HashPosition pos1; HashPosition pos2; php5to7_zval *current1; php5to7_zval *current2; php_driver_tuple *tuple1; php_driver_tuple *tuple2; php_driver_type *type1; php_driver_type *type2; int result; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ tuple1 = PHP_DRIVER_GET_TUPLE(obj1); tuple2 = PHP_DRIVER_GET_TUPLE(obj2); type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(tuple1->type)); type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(tuple2->type)); result = php_driver_type_compare(type1, type2 TSRMLS_CC); if (result != 0) return result; if (zend_hash_num_elements(&tuple1->values) != zend_hash_num_elements(&tuple2->values)) { return zend_hash_num_elements(&tuple1->values) < zend_hash_num_elements(&tuple2->values) ? -1 : 1; } zend_hash_internal_pointer_reset_ex(&tuple1->values, &pos1); zend_hash_internal_pointer_reset_ex(&tuple2->values, &pos2); while (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&tuple1->values, current1, &pos1) && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&tuple2->values, current2, &pos2)) { result = php_driver_value_compare(PHP5TO7_ZVAL_MAYBE_DEREF(current1), PHP5TO7_ZVAL_MAYBE_DEREF(current2) TSRMLS_CC); if (result != 0) return result; zend_hash_move_forward_ex(&tuple1->values, &pos1); zend_hash_move_forward_ex(&tuple2->values, &pos2); } return 0; } static unsigned php_driver_tuple_hash_value(zval *obj TSRMLS_DC) { php5to7_zval *current; unsigned hashv = 0; php_driver_tuple *self = PHP_DRIVER_GET_TUPLE(obj); if (!self->dirty) return self->hashv; PHP5TO7_ZEND_HASH_FOREACH_VAL(&self->values, current) { hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC)); } PHP5TO7_ZEND_HASH_FOREACH_END(&self->values); self->hashv = hashv; self->dirty = 0; return hashv; } static void php_driver_tuple_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_tuple *self = PHP5TO7_ZEND_OBJECT_GET(tuple, object); zend_hash_destroy(&self->values); PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_tuple_new(zend_class_entry *ce TSRMLS_DC) { php_driver_tuple *self = PHP5TO7_ZEND_OBJECT_ECALLOC(tuple, ce); zend_hash_init(&self->values, 0, NULL, ZVAL_PTR_DTOR, 0); #if PHP_MAJOR_VERSION >= 7 self->pos = HT_INVALID_IDX; #else self->pos = NULL; #endif self->dirty = 1; PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT(tuple, self, ce); } void php_driver_define_Tuple(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Tuple", php_driver_tuple_methods); php_driver_tuple_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_tuple_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_tuple_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_tuple_handlers.std.get_properties = php_driver_tuple_properties; #if PHP_VERSION_ID >= 50400 php_driver_tuple_handlers.std.get_gc = php_driver_tuple_gc; #endif php_driver_tuple_handlers.std.compare_objects = php_driver_tuple_compare; php_driver_tuple_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_tuple_ce->create_object = php_driver_tuple_new; zend_class_implements(php_driver_tuple_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); php_driver_tuple_handlers.hash_value = php_driver_tuple_hash_value; php_driver_tuple_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Tuple.h0000644000076500000240000000141213062515664016170 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TUPLE_H #define PHP_DRIVER_TUPLE_H void php_driver_tuple_set(php_driver_tuple *tuple, ulong index, zval *object TSRMLS_DC); #endif /* PHP_DRIVER_TUPLE_H */ cassandra-1.3.0/src/Type.c0000644000076500000240000001544613062515664016027 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #include "src/Type/Tuple.h" #include "src/Type/UserType.h" zend_class_entry *php_driver_type_ce = NULL; #define XX_SCALAR_METHOD(name, value) PHP_METHOD(Type, name) \ { \ php5to7_zval ztype; \ if (zend_parse_parameters_none() == FAILURE) { \ return; \ } \ ztype = php_driver_type_scalar(value TSRMLS_CC); \ RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 1, 1); \ } PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR_METHOD) #undef XX_SCALAR_METHOD PHP_METHOD(Type, collection) { php5to7_zval ztype; zval *value_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &value_type, php_driver_type_ce) == FAILURE) { return; } if (!php_driver_type_validate(value_type, "type" TSRMLS_CC)) { return; } ztype = php_driver_type_collection(value_type TSRMLS_CC); Z_ADDREF_P(value_type); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } PHP_METHOD(Type, tuple) { php5to7_zval ztype; php_driver_type *type; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } for (i = 0; i < argc; ++i) { zval *sub_type = PHP5TO7_ZVAL_ARG(args[i]); if (!php_driver_type_validate(sub_type, "type" TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } } ztype = php_driver_type_tuple(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); for (i = 0; i < argc; ++i) { zval *sub_type = PHP5TO7_ZVAL_ARG(args[i]); if (php_driver_type_tuple_add(type, sub_type TSRMLS_CC)) { Z_ADDREF_P(sub_type); } else { break; } } PHP5TO7_MAYBE_EFREE(args); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } PHP_METHOD(Type, userType) { php5to7_zval ztype; php_driver_type *type; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) { return; } if (argc % 2 == 1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Not enough name/type pairs, user types can only be created " \ "from an even number of name/type pairs, where each odd " \ "argument is a name and each even argument is a type, " \ "e.g userType(name, type, name, type, name, type)"); PHP5TO7_MAYBE_EFREE(args); return; } for (i = 0; i < argc; i += 2) { zval *name = PHP5TO7_ZVAL_ARG(args[i]); zval *sub_type = PHP5TO7_ZVAL_ARG(args[i + 1]); if (Z_TYPE_P(name) != IS_STRING) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Argument %d is not a string", i + 1); PHP5TO7_MAYBE_EFREE(args); return; } if (!php_driver_type_validate(sub_type, "type" TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } } ztype = php_driver_type_user_type(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); for (i = 0; i < argc; i += 2) { zval *name = PHP5TO7_ZVAL_ARG(args[i]); zval *sub_type = PHP5TO7_ZVAL_ARG(args[i + 1]); if (php_driver_type_user_type_add(type, Z_STRVAL_P(name), Z_STRLEN_P(name), sub_type TSRMLS_CC)) { Z_ADDREF_P(sub_type); } else { break; } } PHP5TO7_MAYBE_EFREE(args); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } PHP_METHOD(Type, set) { php5to7_zval ztype; zval *value_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &value_type, php_driver_type_ce) == FAILURE) { return; } if (!php_driver_type_validate(value_type, "type" TSRMLS_CC)) { return; } ztype = php_driver_type_set(value_type TSRMLS_CC); Z_ADDREF_P(value_type); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } PHP_METHOD(Type, map) { php5to7_zval ztype; zval *key_type; zval *value_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO", &key_type, php_driver_type_ce, &value_type, php_driver_type_ce) == FAILURE) { return; } if (!php_driver_type_validate(key_type, "keyType" TSRMLS_CC)) { return; } if (!php_driver_type_validate(value_type, "valueType" TSRMLS_CC)) { return; } ztype = php_driver_type_map(key_type, value_type TSRMLS_CC); Z_ADDREF_P(key_type); Z_ADDREF_P(value_type); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(ztype), 0, 1); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_types, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, types) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_type, 0, ZEND_RETURN_VALUE, 1) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, type, Type, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_map, 0, ZEND_RETURN_VALUE, 2) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, keyType, Type, 0) PHP_DRIVER_NAMESPACE_ZEND_ARG_OBJ_INFO(0, valueType, Type, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_methods[] = { PHP_ABSTRACT_ME(Type, name, arginfo_none) PHP_ABSTRACT_ME(Type, __toString, arginfo_none) #define XX_SCALAR_METHOD(name, _) PHP_ME(Type, name, arginfo_none, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR_METHOD) #undef XX_SCALAR_METHOD PHP_ME(Type, collection, arginfo_type, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Type, set, arginfo_type, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Type, map, arginfo_map, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Type, tuple, arginfo_types, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_ME(Type, userType, arginfo_types, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL) PHP_FE_END }; void php_driver_define_Type(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type", php_driver_type_methods); php_driver_type_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_type_ce->ce_flags |= ZEND_ACC_ABSTRACT; } cassandra-1.3.0/src/Type/Collection.c0000644000076500000240000001362113062515664020113 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #include "src/Collection.h" #include "util/collections.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif zend_class_entry *php_driver_type_collection_ce = NULL; PHP_METHOD(TypeCollection, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Collection type is not supported." ); return; } PHP_METHOD(TypeCollection, name) { if (zend_parse_parameters_none() == FAILURE) { return; } PHP5TO7_RETVAL_STRING("list"); } PHP_METHOD(TypeCollection, valueType) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->data.collection.value_type), 1, 0); } PHP_METHOD(TypeCollection, __toString) { php_driver_type *self; smart_str string = PHP5TO7_SMART_STR_INIT; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); php_driver_type_string(self, &string TSRMLS_CC); smart_str_0(&string); PHP5TO7_RETVAL_STRING(PHP5TO7_SMART_STR_VAL(string)); smart_str_free(&string); } PHP_METHOD(TypeCollection, create) { php_driver_type *self; php_driver_collection *collection; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); object_init_ex(return_value, php_driver_collection_ce); collection = PHP_DRIVER_GET_COLLECTION(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(collection->type), getThis()); if (argc > 0) { for (i = 0; i < argc; i++) { if (!php_driver_validate_object(PHP5TO7_ZVAL_ARG(args[i]), PHP5TO7_ZVAL_MAYBE_P(self->data.collection.value_type) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } php_driver_collection_add(collection, PHP5TO7_ZVAL_ARG(args[i]) TSRMLS_CC); } PHP5TO7_MAYBE_EFREE(args); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_collection_methods[] = { PHP_ME(TypeCollection, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeCollection, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeCollection, valueType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeCollection, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeCollection, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_collection_handlers; static HashTable * php_driver_type_collection_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_collection_properties(zval *object TSRMLS_DC) { php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "valueType", sizeof("valueType"), PHP5TO7_ZVAL_MAYBE_P(self->data.collection.value_type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->data.collection.value_type)); return props; } static int php_driver_type_collection_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_collection_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->data.collection.value_type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_collection_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_LIST; self->data_type = cass_data_type_new(self->type); PHP5TO7_ZVAL_UNDEF(self->data.collection.value_type); PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_collection, self, ce); } void php_driver_define_TypeCollection(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Collection", php_driver_type_collection_methods); php_driver_type_collection_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_collection_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_collection_handlers.get_properties = php_driver_type_collection_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_collection_handlers.get_gc = php_driver_type_collection_gc; #endif php_driver_type_collection_handlers.compare_objects = php_driver_type_collection_compare; php_driver_type_collection_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_collection_ce->create_object = php_driver_type_collection_new; } cassandra-1.3.0/src/Type/Custom.c0000644000076500000240000001117413062515665017274 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_type_custom_ce = NULL; PHP_METHOD(TypeCustom, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Custom type is not supported." ); return; } PHP_METHOD(TypeCustom, name) { php_driver_type *custom; if (zend_parse_parameters_none() == FAILURE) { return; } custom = PHP_DRIVER_GET_TYPE(getThis()); PHP5TO7_RETVAL_STRING(custom->data.custom.class_name); } PHP_METHOD(TypeCustom, __toString) { php_driver_type *custom; if (zend_parse_parameters_none() == FAILURE) { return; } custom = PHP_DRIVER_GET_TYPE(getThis()); PHP5TO7_RETVAL_STRING(custom->data.custom.class_name); } PHP_METHOD(TypeCustom, create) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Custom instance is not supported." ); return; } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_custom_methods[] = { PHP_ME(TypeCustom, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeCustom, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeCustom, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeCustom, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_custom_handlers; static HashTable * php_driver_type_custom_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_custom_properties(zval *object TSRMLS_DC) { php5to7_zval name; php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZVAL_MAYBE_MAKE(name); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(name), self->data.custom.class_name); PHP5TO7_ZEND_HASH_UPDATE(props, "name", sizeof("name"), PHP5TO7_ZVAL_MAYBE_P(name), sizeof(zval)); return props; } static int php_driver_type_custom_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_custom_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); if (self->data.custom.class_name) { efree(self->data.custom.class_name); self->data.custom.class_name = NULL; } zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_custom_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_CUSTOM; self->data_type = cass_data_type_new(self->type); self->data.custom.class_name = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_custom, self, ce); } void php_driver_define_TypeCustom(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Custom", php_driver_type_custom_methods); php_driver_type_custom_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_custom_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_custom_handlers.get_properties = php_driver_type_custom_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_custom_handlers.get_gc = php_driver_type_custom_gc; #endif php_driver_type_custom_handlers.compare_objects = php_driver_type_custom_compare; php_driver_type_custom_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_custom_ce->create_object = php_driver_type_custom_new; } cassandra-1.3.0/src/Type/Map.c0000644000076500000240000001476213062515665016545 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif #include "src/Map.h" zend_class_entry *php_driver_type_map_ce = NULL; PHP_METHOD(TypeMap, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Map type is not supported." ); return; } PHP_METHOD(TypeMap, name) { if (zend_parse_parameters_none() == FAILURE) { return; } PHP5TO7_RETVAL_STRING("map"); } PHP_METHOD(TypeMap, keyType) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->data.map.key_type), 1, 0); } PHP_METHOD(TypeMap, valueType) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->data.map.value_type), 1, 0); } PHP_METHOD(TypeMap, __toString) { php_driver_type *self; smart_str string = PHP5TO7_SMART_STR_INIT; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); php_driver_type_string(self, &string TSRMLS_CC); smart_str_0(&string); PHP5TO7_RETVAL_STRING(PHP5TO7_SMART_STR_VAL(string)); smart_str_free(&string); } PHP_METHOD(TypeMap, create) { php_driver_map *map; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &args, &argc) == FAILURE) { return; } if (argc % 2 == 1) { PHP5TO7_MAYBE_EFREE(args); zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Not enough values, maps can only be created " \ "from an even number of values, where each odd " \ "value is a key and each even value is a value, " \ "e.g create(key, value, key, value, key, value)"); return; } object_init_ex(return_value, php_driver_map_ce); map = PHP_DRIVER_GET_MAP(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(map->type), getThis()); if (argc > 0) { for (i = 0; i < argc; i += 2) { if (!php_driver_map_set(map, PHP5TO7_ZVAL_ARG(args[i]), PHP5TO7_ZVAL_ARG(args[i + 1]) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } } PHP5TO7_MAYBE_EFREE(args); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_map_methods[] = { PHP_ME(TypeMap, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeMap, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeMap, keyType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeMap, valueType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeMap, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeMap, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_map_handlers; static HashTable * php_driver_type_map_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_map_properties(zval *object TSRMLS_DC) { php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "keyType", sizeof("keyType"), PHP5TO7_ZVAL_MAYBE_P(self->data.map.key_type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->data.map.key_type)); PHP5TO7_ZEND_HASH_UPDATE(props, "valueType", sizeof("valueType"), PHP5TO7_ZVAL_MAYBE_P(self->data.map.value_type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->data.map.value_type)); return props; } static int php_driver_type_map_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_map_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->data.map.key_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->data.map.value_type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_map_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_MAP; self->data_type = cass_data_type_new(self->type); PHP5TO7_ZVAL_UNDEF(self->data.map.key_type); PHP5TO7_ZVAL_UNDEF(self->data.map.value_type); PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_map, self, ce); } void php_driver_define_TypeMap(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Map", php_driver_type_map_methods); php_driver_type_map_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_map_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_map_handlers.get_properties = php_driver_type_map_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_map_handlers.get_gc = php_driver_type_map_gc; #endif php_driver_type_map_handlers.compare_objects = php_driver_type_map_compare; php_driver_type_map_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_map_ce->create_object = php_driver_type_map_new; } cassandra-1.3.0/src/Type/Scalar.c0000644000076500000240000001174013062515665017226 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" zend_class_entry *php_driver_type_scalar_ce = NULL; PHP_METHOD(TypeScalar, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Scalar objects directly is not " \ "supported, call varchar(), text(), blob(), ascii(), bigint(), " \ "smallint(), tinyint(), counter(), int(), varint(), boolean(), " \ "decimal(), double(), float(), inet(), timestamp(), uuid(), timeuuid(), " \ "map(), collection() or set() on " PHP_DRIVER_NAMESPACE "\\Type statically instead." ); return; } PHP_METHOD(TypeScalar, name) { php_driver_type *self; const char *name; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); name = php_driver_scalar_type_name(self->type TSRMLS_CC); PHP5TO7_RETVAL_STRING(name); } PHP_METHOD(TypeScalar, __toString) { php_driver_type *self; const char *name; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); name = php_driver_scalar_type_name(self->type TSRMLS_CC); PHP5TO7_RETVAL_STRING(name); } PHP_METHOD(TypeScalar, create) { php_driver_scalar_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_scalar_methods[] = { PHP_ME(TypeScalar, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeScalar, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeScalar, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeScalar, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_scalar_handlers; static HashTable * php_driver_type_scalar_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_scalar_properties(zval *object TSRMLS_DC) { php5to7_zval name; php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); /* Used for comparison and 'text' is just an alias for 'varchar' */ CassValueType type = self->type == CASS_VALUE_TYPE_TEXT ? CASS_VALUE_TYPE_VARCHAR : self->type; PHP5TO7_ZVAL_MAYBE_MAKE(name); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(name), php_driver_scalar_type_name(type TSRMLS_CC)); PHP5TO7_ZEND_HASH_UPDATE(props, "name", sizeof("name"), PHP5TO7_ZVAL_MAYBE_P(name), sizeof(zval)); return props; } static int php_driver_type_scalar_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_scalar_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_scalar_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_UNKNOWN; self->data_type = NULL; PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_scalar, self, ce); } void php_driver_define_TypeScalar(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Scalar", php_driver_type_scalar_methods); php_driver_type_scalar_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_scalar_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_scalar_handlers.get_properties = php_driver_type_scalar_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_scalar_handlers.get_gc = php_driver_type_scalar_gc; #endif php_driver_type_scalar_handlers.compare_objects = php_driver_type_scalar_compare; php_driver_type_scalar_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_scalar_ce->create_object = php_driver_type_scalar_new; } cassandra-1.3.0/src/Type/Set.c0000644000076500000240000001247513062515665016562 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif #include "src/Set.h" zend_class_entry *php_driver_type_set_ce = NULL; PHP_METHOD(TypeSet, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Set type is not supported." ); return; } PHP_METHOD(TypeSet, name) { if (zend_parse_parameters_none() == FAILURE) { return; } PHP5TO7_RETVAL_STRING("set"); } PHP_METHOD(TypeSet, valueType) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->data.set.value_type), 1, 0); } PHP_METHOD(TypeSet, __toString) { php_driver_type *self; smart_str string = PHP5TO7_SMART_STR_INIT; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); php_driver_type_string(self, &string TSRMLS_CC); smart_str_0(&string); PHP5TO7_RETVAL_STRING(PHP5TO7_SMART_STR_VAL(string)); smart_str_free(&string); } PHP_METHOD(TypeSet, create) { php_driver_set *set; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &args, &argc) == FAILURE) { return; } object_init_ex(return_value, php_driver_set_ce); set = PHP_DRIVER_GET_SET(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(set->type), getThis()); if (argc > 0) { for (i = 0; i < argc; i++) { if (!php_driver_set_add(set, PHP5TO7_ZVAL_ARG(args[i]) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } } PHP5TO7_MAYBE_EFREE(args); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_set_methods[] = { PHP_ME(TypeSet, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeSet, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeSet, valueType, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeSet, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeSet, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_set_handlers; static HashTable * php_driver_type_set_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_set_properties(zval *object TSRMLS_DC) { php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "valueType", sizeof("valueType"), PHP5TO7_ZVAL_MAYBE_P(self->data.set.value_type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->data.set.value_type)); return props; } static int php_driver_type_set_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_set_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); PHP5TO7_ZVAL_MAYBE_DESTROY(self->data.set.value_type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_set_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_SET; self->data_type = cass_data_type_new(self->type); PHP5TO7_ZVAL_UNDEF(self->data.set.value_type); PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_set, self, ce); } void php_driver_define_TypeSet(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Set", php_driver_type_set_methods); php_driver_type_set_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_set_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_set_handlers.get_properties = php_driver_type_set_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_set_handlers.get_gc = php_driver_type_set_gc; #endif php_driver_type_set_handlers.compare_objects = php_driver_type_set_compare; php_driver_type_set_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_set_ce->create_object = php_driver_type_set_new; } cassandra-1.3.0/src/Type/Tuple.c0000644000076500000240000001537113062515665017116 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #include "src/Tuple.h" #include "util/collections.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif zend_class_entry *php_driver_type_tuple_ce = NULL; int php_driver_type_tuple_add(php_driver_type *type, zval *zsub_type TSRMLS_DC) { php_driver_type *sub_type = PHP_DRIVER_GET_TYPE(zsub_type); if (cass_data_type_add_sub_type(type->data_type, sub_type->data_type) != CASS_OK) { return 0; } PHP5TO7_ZEND_HASH_NEXT_INDEX_INSERT(&type->data.tuple.types, zsub_type, sizeof(zval *)); return 1; } PHP_METHOD(TypeTuple, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\Tuple type is not supported." ); return; } PHP_METHOD(TypeTuple, name) { if (zend_parse_parameters_none() == FAILURE) { return; } PHP5TO7_RETVAL_STRING("tuple"); } PHP_METHOD(TypeTuple, types) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); array_init(return_value); PHP5TO7_ZEND_HASH_ZVAL_COPY(Z_ARRVAL_P(return_value), &self->data.tuple.types); } PHP_METHOD(TypeTuple, __toString) { php_driver_type *self; smart_str string = PHP5TO7_SMART_STR_INIT; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); php_driver_type_string(self, &string TSRMLS_CC); smart_str_0(&string); PHP5TO7_RETVAL_STRING(PHP5TO7_SMART_STR_VAL(string)); smart_str_free(&string); } PHP_METHOD(TypeTuple, create) { php_driver_type *self; php_driver_tuple *tuple; php5to7_zval_args args = NULL; int argc = 0, i, num_types; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); object_init_ex(return_value, php_driver_tuple_ce); tuple = PHP_DRIVER_GET_TUPLE(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(tuple->type), getThis()); num_types = zend_hash_num_elements(&self->data.tuple.types); if (argc > 0) { if (argc != num_types) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid number of elements given. Expected %d arguments.", zend_hash_num_elements(&self->data.tuple.types)); PHP5TO7_MAYBE_EFREE(args); return; } for (i = 0; i < argc; i++) { php5to7_zval *sub_type; if (!PHP5TO7_ZEND_HASH_INDEX_FIND(&self->data.tuple.types, i, sub_type) || !php_driver_validate_object(PHP5TO7_ZVAL_ARG(args[i]), PHP5TO7_ZVAL_MAYBE_DEREF(sub_type) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } php_driver_tuple_set(tuple, i, PHP5TO7_ZVAL_ARG(args[i]) TSRMLS_CC); } PHP5TO7_MAYBE_EFREE(args); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_values, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, values) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_tuple_methods[] = { PHP_ME(TypeTuple, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeTuple, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeTuple, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeTuple, types, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeTuple, create, arginfo_values, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_tuple_handlers; static HashTable * php_driver_type_tuple_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_tuple_properties(zval *object TSRMLS_DC) { php5to7_zval types; php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZVAL_MAYBE_MAKE(types); array_init(PHP5TO7_ZVAL_MAYBE_P(types)); PHP5TO7_ZEND_HASH_ZVAL_COPY(PHP5TO7_Z_ARRVAL_MAYBE_P(types), &self->data.tuple.types); PHP5TO7_ZEND_HASH_UPDATE(props, "types", sizeof("types"), PHP5TO7_ZVAL_MAYBE_P(types), sizeof(zval)); return props; } static int php_driver_type_tuple_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_tuple_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); zend_hash_destroy(&self->data.tuple.types); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_tuple_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_TUPLE; self->data_type = cass_data_type_new(self->type); zend_hash_init(&self->data.tuple.types, 0, NULL, ZVAL_PTR_DTOR, 0); PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_tuple, self, ce); } void php_driver_define_TypeTuple(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\Tuple", php_driver_type_tuple_methods); php_driver_type_tuple_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_tuple_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_tuple_handlers.get_properties = php_driver_type_tuple_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_tuple_handlers.get_gc = php_driver_type_tuple_gc; #endif php_driver_type_tuple_handlers.compare_objects = php_driver_type_tuple_compare; php_driver_type_tuple_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_tuple_ce->create_object = php_driver_type_tuple_new; } cassandra-1.3.0/src/Type/Tuple.h0000644000076500000240000000146313062515665017120 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TYPE_TUPLE_H #define PHP_DRIVER_TYPE_TUPLE_H int php_driver_type_tuple_add(php_driver_type *type, zval *zsub_type TSRMLS_DC); #endif /* PHP_DRIVER_TYPE_TUPLE_H */ cassandra-1.3.0/src/Type/UserType.c0000644000076500000240000002466713062515665017615 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/types.h" #include "src/UserTypeValue.h" #include "util/collections.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif zend_class_entry *php_driver_type_user_type_ce = NULL; int php_driver_type_user_type_add(php_driver_type *type, const char *name, size_t name_length, zval *zsub_type TSRMLS_DC) { php_driver_type *sub_type = PHP_DRIVER_GET_TYPE(zsub_type); if (cass_data_type_add_sub_type_by_name_n(type->data_type, name, name_length, sub_type->data_type) != CASS_OK) { return 0; } PHP5TO7_ZEND_HASH_ADD(&type->data.udt.types, name, name_length + 1, zsub_type, sizeof(zval *)); return 1; } PHP_METHOD(TypeUserType, __construct) { zend_throw_exception_ex(php_driver_logic_exception_ce, 0 TSRMLS_CC, "Instantiation of a " PHP_DRIVER_NAMESPACE "\\Type\\UserType type is not supported." ); return; } PHP_METHOD(TypeUserType, withName) { char *name; php5to7_size name_len; php_driver_type *self; php_driver_type *user_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); object_init_ex(return_value, php_driver_type_user_type_ce); user_type = PHP_DRIVER_GET_TYPE(return_value); user_type->data_type = cass_data_type_new_from_existing(self->data_type); user_type->data.udt.type_name = estrndup(name, name_len); if (self->data.udt.keyspace) { user_type->data.udt.keyspace = estrdup(self->data.udt.keyspace); } PHP5TO7_ZEND_HASH_ZVAL_COPY(&user_type->data.udt.types, &self->data.udt.types); } PHP_METHOD(TypeUserType, name) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); if (!self->data.udt.type_name) RETURN_NULL(); PHP5TO7_RETVAL_STRING(self->data.udt.type_name); } PHP_METHOD(TypeUserType, withKeyspace) { char *keyspace; php5to7_size keyspace_len; php_driver_type *self; php_driver_type *user_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &keyspace, &keyspace_len) == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); object_init_ex(return_value, php_driver_type_user_type_ce); user_type = PHP_DRIVER_GET_TYPE(return_value); user_type->data_type = cass_data_type_new_from_existing(self->data_type); if (self->data.udt.type_name) { user_type->data.udt.type_name = estrdup(self->data.udt.type_name); } user_type->data.udt.keyspace = estrndup(keyspace, keyspace_len); PHP5TO7_ZEND_HASH_ZVAL_COPY(&user_type->data.udt.types, &self->data.udt.types); } PHP_METHOD(TypeUserType, keyspace) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); if (!self->data.udt.keyspace) RETURN_NULL(); PHP5TO7_RETVAL_STRING(self->data.udt.keyspace); } PHP_METHOD(TypeUserType, types) { php_driver_type *self; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); array_init(return_value); PHP5TO7_ZEND_HASH_ZVAL_COPY(Z_ARRVAL_P(return_value), &self->data.udt.types); } PHP_METHOD(TypeUserType, __toString) { php_driver_type *self; smart_str string = PHP5TO7_SMART_STR_INIT; if (zend_parse_parameters_none() == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); php_driver_type_string(self, &string TSRMLS_CC); smart_str_0(&string); PHP5TO7_RETVAL_STRING(PHP5TO7_SMART_STR_VAL(string)); smart_str_free(&string); } PHP_METHOD(TypeUserType, create) { php_driver_type *self; php_driver_user_type_value *user_type_value; php5to7_zval_args args = NULL; int argc = 0, i; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &args, &argc) == FAILURE) { return; } self = PHP_DRIVER_GET_TYPE(getThis()); object_init_ex(return_value, php_driver_user_type_value_ce); user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(return_value); PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(user_type_value->type), getThis()); if (argc > 0) { if (argc % 2 == 1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Not enough name/value pairs, user_types can only be created " \ "from an even number of name/value pairs, where each odd " \ "argument is a name and each even argument is a value, " \ "e.g user_type(name, value, name, value, name, value)"); PHP5TO7_MAYBE_EFREE(args); return; } for (i = 0; i < argc; i += 2) { zval *name = PHP5TO7_ZVAL_ARG(args[i]); zval *value = PHP5TO7_ZVAL_ARG(args[i + 1]); php5to7_zval *sub_type; if (Z_TYPE_P(name) != IS_STRING) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Argument %d is not a string", i + 1); PHP5TO7_MAYBE_EFREE(args); return; } if (!PHP5TO7_ZEND_HASH_FIND(&self->data.udt.types, Z_STRVAL_P(name), Z_STRLEN_P(name) + 1, sub_type)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid name '%s'", Z_STRVAL_P(name)); PHP5TO7_MAYBE_EFREE(args); return; } if (!php_driver_validate_object(value, PHP5TO7_ZVAL_MAYBE_DEREF(sub_type) TSRMLS_CC)) { PHP5TO7_MAYBE_EFREE(args); return; } php_driver_user_type_value_set(user_type_value, Z_STRVAL_P(name), Z_STRLEN_P(name), value TSRMLS_CC); } PHP5TO7_MAYBE_EFREE(args); } } ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_keyspace, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, keyspace) ZEND_END_ARG_INFO() static zend_function_entry php_driver_type_user_type_methods[] = { PHP_ME(TypeUserType, __construct, arginfo_none, ZEND_ACC_PRIVATE) PHP_ME(TypeUserType, withName, arginfo_name, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, name, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, withKeyspace, arginfo_keyspace, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, keyspace, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, types, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(TypeUserType, create, arginfo_value, ZEND_ACC_PUBLIC) PHP_FE_END }; static zend_object_handlers php_driver_type_user_type_handlers; static HashTable * php_driver_type_user_type_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_type_user_type_properties(zval *object TSRMLS_DC) { php5to7_zval types; php_driver_type *self = PHP_DRIVER_GET_TYPE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZVAL_MAYBE_MAKE(types); array_init(PHP5TO7_ZVAL_MAYBE_P(types)); PHP5TO7_ZEND_HASH_ZVAL_COPY(PHP5TO7_Z_ARRVAL_MAYBE_P(types), &self->data.udt.types); PHP5TO7_ZEND_HASH_UPDATE(props, "types", sizeof("types"), PHP5TO7_ZVAL_MAYBE_P(types), sizeof(zval)); return props; } static int php_driver_type_user_type_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_type* type1 = PHP_DRIVER_GET_TYPE(obj1); php_driver_type* type2 = PHP_DRIVER_GET_TYPE(obj2); return php_driver_type_compare(type1, type2 TSRMLS_CC); } static void php_driver_type_user_type_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_GET(type, object); if (self->data_type) cass_data_type_free(self->data_type); if (self->data.udt.keyspace) efree(self->data.udt.keyspace); if (self->data.udt.type_name) efree(self->data.udt.type_name); zend_hash_destroy(&self->data.udt.types); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_type_user_type_new(zend_class_entry *ce TSRMLS_DC) { php_driver_type *self = PHP5TO7_ZEND_OBJECT_ECALLOC(type, ce); self->type = CASS_VALUE_TYPE_UDT; self->data_type = NULL; self->data.udt.keyspace = self->data.udt.type_name = NULL; zend_hash_init(&self->data.udt.types, 0, NULL, ZVAL_PTR_DTOR, 0); PHP5TO7_ZEND_OBJECT_INIT_EX(type, type_user_type, self, ce); } void php_driver_define_TypeUserType(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Type\\UserType", php_driver_type_user_type_methods); php_driver_type_user_type_ce = php5to7_zend_register_internal_class_ex(&ce, php_driver_type_ce); memcpy(&php_driver_type_user_type_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_type_user_type_handlers.get_properties = php_driver_type_user_type_properties; #if PHP_VERSION_ID >= 50400 php_driver_type_user_type_handlers.get_gc = php_driver_type_user_type_gc; #endif php_driver_type_user_type_handlers.compare_objects = php_driver_type_user_type_compare; php_driver_type_user_type_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_type_user_type_ce->create_object = php_driver_type_user_type_new; } cassandra-1.3.0/src/Type/UserType.h0000644000076500000240000000162313062515665017605 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_TYPE_USER_TYPE_H #define PHP_DRIVER_TYPE_USER_TYPE_H int php_driver_type_user_type_add(php_driver_type *type, const char *name, size_t name_length, zval *zsub_type TSRMLS_DC); #endif /* PHP_DRIVER_TYPE_USER_TYPE_H */ cassandra-1.3.0/src/UserTypeValue.c0000644000076500000240000003706713062515665017667 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/collections.h" #include "util/hash.h" #include "util/types.h" #include "src/Type/UserType.h" #include "src/UserTypeValue.h" zend_class_entry *php_driver_user_type_value_ce = NULL; void php_driver_user_type_value_set(php_driver_user_type_value *user_type_value, const char *name, size_t name_length, zval *object TSRMLS_DC) { PHP5TO7_ZEND_HASH_UPDATE(&user_type_value->values, name, name_length + 1, object, sizeof(zval *)); Z_TRY_ADDREF_P(object); user_type_value->dirty = 1; } static void php_driver_user_type_value_populate(php_driver_user_type_value *user_type_value, zval *array TSRMLS_DC) { char *name; php_driver_type *type; php5to7_zval *current; php5to7_zval null; PHP5TO7_ZVAL_MAYBE_MAKE(null); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_P(null)); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(user_type_value->type)); PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(&type->data.udt.types, name, current) { php5to7_zval *value = NULL; size_t name_len = strlen(name); (void) current; if (PHP5TO7_ZEND_HASH_FIND(&user_type_value->values, name, name_len + 1, value)) { if (PHP5TO7_ADD_ASSOC_ZVAL_EX(array, name, name_len + 1, PHP5TO7_ZVAL_MAYBE_DEREF(value)) == SUCCESS) { Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_DEREF(value)); } else { break; } } else { if (PHP5TO7_ADD_ASSOC_ZVAL_EX(array, name, name_len + 1, PHP5TO7_ZVAL_MAYBE_P(null)) == SUCCESS) { Z_TRY_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(null)); } else { break; } } } PHP5TO7_ZEND_HASH_FOREACH_END(&type->data.udt.types); #if PHP_MAJOR_VERSION < 7 zval_ptr_dtor(&null); #endif } /* {{{ UserTypeValue::__construct(types) */ PHP_METHOD(UserTypeValue, __construct) { php_driver_user_type_value *self; php_driver_type *type; HashTable *types; char *name; int index = 0; php5to7_zval *current; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "h", &types) == FAILURE) { return; } self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); self->type = php_driver_type_user_type(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(types, name, current) { zval *sub_type = PHP5TO7_ZVAL_MAYBE_DEREF(current); php5to7_zval scalar_type; if (!name) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Argument %d is not a string", index + 1); return; } index++; if (Z_TYPE_P(sub_type) == IS_STRING) { CassValueType value_type; if (!php_driver_value_type(Z_STRVAL_P(sub_type), &value_type TSRMLS_CC)) { return; } scalar_type = php_driver_type_scalar(value_type TSRMLS_CC); if (!php_driver_type_user_type_add(type, name, strlen(name), PHP5TO7_ZVAL_MAYBE_P(scalar_type) TSRMLS_CC)) { return; } } else if (Z_TYPE_P(sub_type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(sub_type), php_driver_type_ce TSRMLS_CC)) { if (!php_driver_type_validate(sub_type, "sub_type" TSRMLS_CC)) { return; } if (php_driver_type_user_type_add(type, name, strlen(name), sub_type TSRMLS_CC)) { Z_ADDREF_P(sub_type); } else { return; } } else { INVALID_ARGUMENT(sub_type, "a string or an instance of " PHP_DRIVER_NAMESPACE "\\Type"); } } PHP5TO7_ZEND_HASH_FOREACH_END(types); } /* }}} */ /* {{{ UserTypeValue::type() */ PHP_METHOD(UserTypeValue, type) { php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(self->type), 1, 0); } /* {{{ UserTypeValue::values() */ PHP_METHOD(UserTypeValue, values) { php_driver_user_type_value *self = NULL; array_init(return_value); self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); array_init(return_value); php_driver_user_type_value_populate(self, return_value TSRMLS_CC); } /* }}} */ /* {{{ UserTypeValue::set(name, mixed) */ PHP_METHOD(UserTypeValue, set) { php_driver_user_type_value *self = NULL; php_driver_type *type; php5to7_zval *sub_type; char *name; size_t name_length; zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &name_length, &value) == FAILURE) return; self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (!PHP5TO7_ZEND_HASH_FIND(&type->data.udt.types, name, name_length + 1, sub_type)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid name '%s'", name); return; } if (!php_driver_validate_object(value, PHP5TO7_ZVAL_MAYBE_DEREF(sub_type) TSRMLS_CC)) { return; } php_driver_user_type_value_set(self, name, name_length, value TSRMLS_CC); } /* }}} */ /* {{{ UserTypeValue::get(name) */ PHP_METHOD(UserTypeValue, get) { php_driver_user_type_value *self = NULL; php_driver_type *type; php5to7_zval *sub_type; char *name; size_t name_length; php5to7_zval *value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_length) == FAILURE) return; self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (!PHP5TO7_ZEND_HASH_FIND(&type->data.udt.types, name, name_length + 1, sub_type)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid name '%s'", name); return; } if (PHP5TO7_ZEND_HASH_FIND(&self->values, name, name_length + 1, value)) { RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(value), 1, 0); } } /* }}} */ /* {{{ UserTypeValue::count() */ PHP_METHOD(UserTypeValue, count) { php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); RETURN_LONG(zend_hash_num_elements(&type->data.udt.types)); } /* }}} */ /* {{{ UserTypeValue::current() */ PHP_METHOD(UserTypeValue, current) { php5to7_string key; php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type->data.udt.types, &key, NULL, &self->pos) == HASH_KEY_IS_STRING) { php5to7_zval *value; #if PHP_MAJOR_VERSION >= 7 if (PHP5TO7_ZEND_HASH_FIND(&self->values, key->val, key->len + 1, value)) { #else if (PHP5TO7_ZEND_HASH_FIND(&self->values, key, strlen(key) + 1, value)) { #endif RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_DEREF(value), 1, 0); } } } /* }}} */ /* {{{ UserTypeValue::key() */ PHP_METHOD(UserTypeValue, key) { php5to7_string key; php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); if (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type->data.udt.types, &key, NULL, &self->pos) == HASH_KEY_IS_STRING) { #if PHP_MAJOR_VERSION >= 7 RETURN_STR(key); #else RETURN_STRING(key, 1); #endif } } /* }}} */ /* {{{ UserTypeValue::next() */ PHP_METHOD(UserTypeValue, next) { php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); zend_hash_move_forward_ex(&type->data.udt.types, &self->pos); } /* }}} */ /* {{{ UserTypeValue::valid() */ PHP_METHOD(UserTypeValue, valid) { php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); RETURN_BOOL(zend_hash_has_more_elements_ex(&type->data.udt.types, &self->pos) == SUCCESS); } /* }}} */ /* {{{ UserTypeValue::rewind() */ PHP_METHOD(UserTypeValue, rewind) { php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(getThis()); php_driver_type *type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(self->type)); zend_hash_internal_pointer_reset_ex(&type->data.udt.types, &self->pos); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, types) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_value, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_name, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, name) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_user_type_value_methods[] = { PHP_ME(UserTypeValue, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, values, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, set, arginfo_value, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, get, arginfo_name, ZEND_ACC_PUBLIC) /* Countable */ PHP_ME(UserTypeValue, count, arginfo_none, ZEND_ACC_PUBLIC) /* Iterator */ PHP_ME(UserTypeValue, current, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, key, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, next, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, valid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(UserTypeValue, rewind, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_user_type_value_handlers; static HashTable * php_driver_user_type_value_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_user_type_value_properties(zval *object TSRMLS_DC) { php5to7_zval values; php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(self->type), sizeof(zval)); Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(self->type)); PHP5TO7_ZVAL_MAYBE_MAKE(values); array_init(PHP5TO7_ZVAL_MAYBE_P(values)); php_driver_user_type_value_populate(self, PHP5TO7_ZVAL_MAYBE_P(values) TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "values", sizeof("values"), PHP5TO7_ZVAL_MAYBE_P(values), sizeof(zval)); return props; } static int php_driver_user_type_value_compare(zval *obj1, zval *obj2 TSRMLS_DC) { HashPosition pos1; HashPosition pos2; php5to7_zval *current1; php5to7_zval *current2; php_driver_user_type_value *user_type_value1; php_driver_user_type_value *user_type_value2; php_driver_type *type1; php_driver_type *type2; int result; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ user_type_value1 = PHP_DRIVER_GET_USER_TYPE_VALUE(obj1); user_type_value2 = PHP_DRIVER_GET_USER_TYPE_VALUE(obj2); type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(user_type_value1->type)); type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(user_type_value2->type)); result = php_driver_type_compare(type1, type2 TSRMLS_CC); if (result != 0) return result; if (zend_hash_num_elements(&user_type_value1->values) != zend_hash_num_elements(&user_type_value2->values)) { return zend_hash_num_elements(&user_type_value1->values) < zend_hash_num_elements(&user_type_value2->values) ? -1 : 1; } zend_hash_internal_pointer_reset_ex(&user_type_value1->values, &pos1); zend_hash_internal_pointer_reset_ex(&user_type_value2->values, &pos2); while (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&user_type_value1->values, current1, &pos1) && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&user_type_value2->values, current2, &pos2)) { result = php_driver_value_compare(PHP5TO7_ZVAL_MAYBE_DEREF(current1), PHP5TO7_ZVAL_MAYBE_DEREF(current2) TSRMLS_CC); if (result != 0) return result; zend_hash_move_forward_ex(&user_type_value1->values, &pos1); zend_hash_move_forward_ex(&user_type_value2->values, &pos2); } return 0; } static unsigned php_driver_user_type_value_hash_value(zval *obj TSRMLS_DC) { php5to7_zval *current; unsigned hashv = 0; php_driver_user_type_value *self = PHP_DRIVER_GET_USER_TYPE_VALUE(obj); if (!self->dirty) return self->hashv; PHP5TO7_ZEND_HASH_FOREACH_VAL(&self->values, current) { hashv = php_driver_combine_hash(hashv, php_driver_value_hash(PHP5TO7_ZVAL_MAYBE_DEREF(current) TSRMLS_CC)); } PHP5TO7_ZEND_HASH_FOREACH_END(&self->values); self->hashv = hashv; self->dirty = 0; return hashv; } static void php_driver_user_type_value_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_user_type_value *self = PHP5TO7_ZEND_OBJECT_GET(user_type_value, object); zend_hash_destroy(&self->values); PHP5TO7_ZVAL_MAYBE_DESTROY(self->type); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_user_type_value_new(zend_class_entry *ce TSRMLS_DC) { php_driver_user_type_value *self = PHP5TO7_ZEND_OBJECT_ECALLOC(user_type_value, ce); zend_hash_init(&self->values, 0, NULL, ZVAL_PTR_DTOR, 0); #if PHP_MAJOR_VERSION >= 7 self->pos = HT_INVALID_IDX; #else self->pos = NULL; #endif self->dirty = 1; PHP5TO7_ZVAL_UNDEF(self->type); PHP5TO7_ZEND_OBJECT_INIT(user_type_value, self, ce); } void php_driver_define_UserTypeValue(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\UserTypeValue", php_driver_user_type_value_methods); php_driver_user_type_value_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_user_type_value_ce TSRMLS_CC, 1, php_driver_value_ce); memcpy(&php_driver_user_type_value_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_user_type_value_handlers.std.get_properties = php_driver_user_type_value_properties; #if PHP_VERSION_ID >= 50400 php_driver_user_type_value_handlers.std.get_gc = php_driver_user_type_value_gc; #endif php_driver_user_type_value_handlers.std.compare_objects = php_driver_user_type_value_compare; php_driver_user_type_value_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_user_type_value_ce->create_object = php_driver_user_type_value_new; zend_class_implements(php_driver_user_type_value_ce TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); php_driver_user_type_value_handlers.hash_value = php_driver_user_type_value_hash_value; php_driver_user_type_value_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/UserTypeValue.h0000644000076500000240000000164713062515665017667 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_USER_TYPE_VALUE_H #define PHP_DRIVER_USER_TYPE_VALUE_H void php_driver_user_type_value_set(php_driver_user_type_value *user_type_value, const char *name, size_t name_length, zval *object TSRMLS_DC); #endif /* PHP_DRIVER_USER_TYPE_VALUE_H */ cassandra-1.3.0/src/Uuid.c0000644000076500000240000001472513062515665016014 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/types.h" #include "util/uuid_gen.h" zend_class_entry *php_driver_uuid_ce = NULL; void php_driver_uuid_init(INTERNAL_FUNCTION_PARAMETERS) { char *value; php5to7_size value_len; php_driver_uuid *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &value, &value_len) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_uuid_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_UUID(getThis()); } else { object_init_ex(return_value, php_driver_uuid_ce); self = PHP_DRIVER_GET_UUID(return_value); } if (ZEND_NUM_ARGS() == 0) { php_driver_uuid_generate_random(&self->uuid TSRMLS_CC); } else { if (cass_uuid_from_string(value, &self->uuid) != CASS_OK) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid UUID: '%.*s'", value_len, value); return; } } } /* {{{ Uuid::__construct(string) */ PHP_METHOD(Uuid, __construct) { php_driver_uuid_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Uuid::__toString() */ PHP_METHOD(Uuid, __toString) { char string[CASS_UUID_STRING_LENGTH]; php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); cass_uuid_string(self->uuid, string); PHP5TO7_RETVAL_STRING(string); } /* }}} */ /* {{{ Uuid::type() */ PHP_METHOD(Uuid, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_UUID TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Uuid::value() */ PHP_METHOD(Uuid, uuid) { char string[CASS_UUID_STRING_LENGTH]; php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); cass_uuid_string(self->uuid, string); PHP5TO7_RETVAL_STRING(string); } /* }}} */ /* {{{ Uuid::version() */ PHP_METHOD(Uuid, version) { php_driver_uuid *self = PHP_DRIVER_GET_UUID(getThis()); RETURN_LONG((long) cass_uuid_version(self->uuid)); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 0) ZEND_ARG_INFO(0, uuid) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_uuid_methods[] = { PHP_ME(Uuid, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Uuid, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Uuid, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Uuid, uuid, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Uuid, version, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_uuid_handlers; static HashTable * php_driver_uuid_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_uuid_properties(zval *object TSRMLS_DC) { char string[CASS_UUID_STRING_LENGTH]; php5to7_zval type; php5to7_zval uuid; php5to7_zval version; php_driver_uuid *self = PHP_DRIVER_GET_UUID(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); cass_uuid_string(self->uuid, string); type = php_driver_type_scalar(CASS_VALUE_TYPE_UUID TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(uuid); PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(uuid), string); PHP5TO7_ZEND_HASH_UPDATE(props, "uuid", sizeof("uuid"), PHP5TO7_ZVAL_MAYBE_P(uuid), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(version); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_P(version), (long) cass_uuid_version(self->uuid)); PHP5TO7_ZEND_HASH_UPDATE(props, "version", sizeof("version"), PHP5TO7_ZVAL_MAYBE_P(version), sizeof(zval)); return props; } static int php_driver_uuid_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_uuid *uuid1 = NULL; php_driver_uuid *uuid2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ uuid1 = PHP_DRIVER_GET_UUID(obj1); uuid2 = PHP_DRIVER_GET_UUID(obj2); if (uuid1->uuid.time_and_version != uuid2->uuid.time_and_version) return uuid1->uuid.time_and_version < uuid2->uuid.time_and_version ? -1 : 1; if (uuid1->uuid.clock_seq_and_node != uuid2->uuid.clock_seq_and_node) return uuid1->uuid.clock_seq_and_node < uuid2->uuid.clock_seq_and_node ? -1 : 1; return 0; } static unsigned php_driver_uuid_hash_value(zval *obj TSRMLS_DC) { php_driver_uuid *self = PHP_DRIVER_GET_UUID(obj); return php_driver_combine_hash(php_driver_bigint_hash(self->uuid.time_and_version), php_driver_bigint_hash(self->uuid.clock_seq_and_node)); } static void php_driver_uuid_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_uuid *self = PHP5TO7_ZEND_OBJECT_GET(uuid, object); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_uuid_new(zend_class_entry *ce TSRMLS_DC) { php_driver_uuid *self = PHP5TO7_ZEND_OBJECT_ECALLOC(uuid, ce); PHP5TO7_ZEND_OBJECT_INIT(uuid, self, ce); } void php_driver_define_Uuid(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Uuid", php_driver_uuid_methods); php_driver_uuid_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_uuid_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_uuid_interface_ce); memcpy(&php_driver_uuid_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_uuid_handlers.std.get_properties = php_driver_uuid_properties; #if PHP_VERSION_ID >= 50400 php_driver_uuid_handlers.std.get_gc = php_driver_uuid_gc; #endif php_driver_uuid_handlers.std.compare_objects = php_driver_uuid_compare; php_driver_uuid_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_uuid_ce->create_object = php_driver_uuid_new; php_driver_uuid_handlers.hash_value = php_driver_uuid_hash_value; php_driver_uuid_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Uuid.h0000644000076500000240000000134713062515665016015 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UUID_H #define PHP_DRIVER_UUID_H void php_driver_uuid_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_UUID_H */ cassandra-1.3.0/src/UuidInterface.c0000644000076500000240000000237213062515665017630 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry *php_driver_uuid_interface_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_uuid_interface_methods[] = { PHP_ABSTRACT_ME(UuidInterface, uuid, arginfo_none) PHP_ABSTRACT_ME(UuidInterface, version, arginfo_none) PHP_FE_END }; void php_driver_define_UuidInterface(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\UuidInterface", php_driver_uuid_interface_methods); php_driver_uuid_interface_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_uuid_interface_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Value.c0000644000076500000240000000217613062515665016157 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" zend_class_entry* php_driver_value_ce = NULL; ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() static zend_function_entry php_driver_value_methods[] = { PHP_ABSTRACT_ME(Value, type, arginfo_none) PHP_FE_END }; void php_driver_define_Value(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Value", php_driver_value_methods); php_driver_value_ce = zend_register_internal_class(&ce TSRMLS_CC); php_driver_value_ce->ce_flags |= ZEND_ACC_INTERFACE; } cassandra-1.3.0/src/Varint.c0000644000076500000240000003423213062515665016344 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #include zend_class_entry *php_driver_varint_ce = NULL; static int to_double(zval *result, php_driver_numeric *varint TSRMLS_DC) { if (mpz_cmp_d(varint->data.varint.value, -DBL_MAX) < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small"); return FAILURE; } if (mpz_cmp_d(varint->data.varint.value, DBL_MAX) > 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too big"); return FAILURE; } ZVAL_DOUBLE(result, mpz_get_d(varint->data.varint.value)); return SUCCESS; } static int to_long(zval *result, php_driver_numeric *varint TSRMLS_DC) { if (mpz_cmp_si(varint->data.varint.value, LONG_MIN) < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small"); return FAILURE; } if (mpz_cmp_si(varint->data.varint.value, LONG_MAX) > 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too big"); return FAILURE; } ZVAL_LONG(result, mpz_get_si(varint->data.varint.value)); return SUCCESS; } static int to_string(zval *result, php_driver_numeric *varint TSRMLS_DC) { char *string; int string_len; php_driver_format_integer(varint->data.varint.value, &string, &string_len); PHP5TO7_ZVAL_STRINGL(result, string, string_len); efree(string); return SUCCESS; } void php_driver_varint_init(INTERNAL_FUNCTION_PARAMETERS) { zval *num; php_driver_numeric *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_varint_ce TSRMLS_CC)) { self = PHP_DRIVER_GET_NUMERIC(getThis()); } else { object_init_ex(return_value, php_driver_varint_ce); self = PHP_DRIVER_GET_NUMERIC(return_value); } if (Z_TYPE_P(num) == IS_LONG) { mpz_set_si(self->data.varint.value, Z_LVAL_P(num)); } else if (Z_TYPE_P(num) == IS_DOUBLE) { mpz_set_d(self->data.varint.value, Z_DVAL_P(num)); } else if (Z_TYPE_P(num) == IS_STRING) { php_driver_parse_varint(Z_STRVAL_P(num), Z_STRLEN_P(num), &self->data.varint.value TSRMLS_CC); } else if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); mpz_set(self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "a long, double, numeric string or a " PHP_DRIVER_NAMESPACE "\\Varint instance"); } } /* {{{ Varint::__construct(string) */ PHP_METHOD(Varint, __construct) { php_driver_varint_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } /* }}} */ /* {{{ Varint::__toString() */ PHP_METHOD(Varint, __toString) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_string(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Varint::type() */ PHP_METHOD(Varint, type) { php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_VARINT TSRMLS_CC); RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1); } /* }}} */ /* {{{ Varint::value() */ PHP_METHOD(Varint, value) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); char *string; int string_len; php_driver_format_integer(self->data.varint.value, &string, &string_len); PHP5TO7_RETVAL_STRINGL(string, string_len); efree(string); } /* }}} */ /* {{{ Varint::add() */ PHP_METHOD(Varint, add) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_add(result->data.varint.value, self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } } /* }}} */ /* {{{ Varint::sub() */ PHP_METHOD(Varint, sub) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_sub(result->data.varint.value, self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } } /* }}} */ /* {{{ Varint::mul() */ PHP_METHOD(Varint, mul) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_mul(result->data.varint.value, self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } } /* }}} */ /* {{{ Varint::div() */ PHP_METHOD(Varint, div) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (mpz_sgn(varint->data.varint.value) == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot divide by zero"); return; } mpz_div(result->data.varint.value, self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } } /* }}} */ /* {{{ Varint::mod() */ PHP_METHOD(Varint, mod) { zval *num; php_driver_numeric *result = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &num) == FAILURE) { return; } if (Z_TYPE_P(num) == IS_OBJECT && instanceof_function(Z_OBJCE_P(num), php_driver_varint_ce TSRMLS_CC)) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); php_driver_numeric *varint = PHP_DRIVER_GET_NUMERIC(num); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); if (mpz_sgn(varint->data.varint.value) == 0) { zend_throw_exception_ex(php_driver_divide_by_zero_exception_ce, 0 TSRMLS_CC, "Cannot modulo by zero"); return; } mpz_mod(result->data.varint.value, self->data.varint.value, varint->data.varint.value); } else { INVALID_ARGUMENT(num, "an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } } /* }}} */ /* {{{ Varint::abs() */ PHP_METHOD(Varint, abs) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_abs(result->data.varint.value, self->data.varint.value); } /* }}} */ /* {{{ Varint::neg() */ PHP_METHOD(Varint, neg) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_neg(result->data.varint.value, self->data.varint.value); } /* }}} */ /* {{{ Varint::sqrt() */ PHP_METHOD(Varint, sqrt) { php_driver_numeric *result = NULL; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); if (mpz_sgn(self->data.varint.value) < 0) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Cannot take a square root of a negative number"); return; } object_init_ex(return_value, php_driver_varint_ce); result = PHP_DRIVER_GET_NUMERIC(return_value); mpz_sqrt(result->data.varint.value, self->data.varint.value); } /* }}} */ /* {{{ Varint::toInt() */ PHP_METHOD(Varint, toInt) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_long(return_value, self TSRMLS_CC); } /* }}} */ /* {{{ Varint::toDouble() */ PHP_METHOD(Varint, toDouble) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(getThis()); to_double(return_value, self TSRMLS_CC); } /* }}} */ ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, value) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_num, 0, ZEND_RETURN_VALUE, 1) ZEND_ARG_INFO(0, num) ZEND_END_ARG_INFO() static zend_function_entry php_driver_varint_methods[] = { PHP_ME(Varint, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(Varint, __toString, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, type, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, value, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, add, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Varint, sub, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Varint, mul, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Varint, div, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Varint, mod, arginfo_num, ZEND_ACC_PUBLIC) PHP_ME(Varint, abs, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, neg, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, sqrt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, toInt, arginfo_none, ZEND_ACC_PUBLIC) PHP_ME(Varint, toDouble, arginfo_none, ZEND_ACC_PUBLIC) PHP_FE_END }; static php_driver_value_handlers php_driver_varint_handlers; static HashTable * php_driver_varint_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC) { *table = NULL; *n = 0; return zend_std_get_properties(object TSRMLS_CC); } static HashTable * php_driver_varint_properties(zval *object TSRMLS_DC) { char *string; int string_len; php5to7_zval type; php5to7_zval value; php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); HashTable *props = zend_std_get_properties(object TSRMLS_CC); php_driver_format_integer(self->data.varint.value, &string, &string_len); type = php_driver_type_scalar(CASS_VALUE_TYPE_VARINT TSRMLS_CC); PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval)); PHP5TO7_ZVAL_MAYBE_MAKE(value); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_P(value), string, string_len); efree(string); PHP5TO7_ZEND_HASH_UPDATE(props, "value", sizeof("value"), PHP5TO7_ZVAL_MAYBE_P(value), sizeof(zval)); return props; } static int php_driver_varint_compare(zval *obj1, zval *obj2 TSRMLS_DC) { php_driver_numeric *varint1 = NULL; php_driver_numeric *varint2 = NULL; if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2)) return 1; /* different classes */ varint1 = PHP_DRIVER_GET_NUMERIC(obj1); varint2 = PHP_DRIVER_GET_NUMERIC(obj2); return mpz_cmp(varint1->data.varint.value, varint2->data.varint.value); } static unsigned php_driver_varint_hash_value(zval *obj TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(obj); return php_driver_mpz_hash(0, self->data.varint.value); } static int php_driver_varint_cast(zval *object, zval *retval, int type TSRMLS_DC) { php_driver_numeric *self = PHP_DRIVER_GET_NUMERIC(object); switch (type) { case IS_LONG: return to_long(retval, self TSRMLS_CC); case IS_DOUBLE: return to_double(retval, self TSRMLS_CC); case IS_STRING: return to_string(retval, self TSRMLS_CC); default: return FAILURE; } return SUCCESS; } static void php_driver_varint_free(php5to7_zend_object_free *object TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_GET(numeric, object); mpz_clear(self->data.varint.value); zend_object_std_dtor(&self->zval TSRMLS_CC); PHP5TO7_MAYBE_EFREE(self); } static php5to7_zend_object php_driver_varint_new(zend_class_entry *ce TSRMLS_DC) { php_driver_numeric *self = PHP5TO7_ZEND_OBJECT_ECALLOC(numeric, ce); mpz_init(self->data.varint.value); PHP5TO7_ZEND_OBJECT_INIT_EX(numeric, varint, self, ce); } void php_driver_define_Varint(TSRMLS_D) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Varint", php_driver_varint_methods); php_driver_varint_ce = zend_register_internal_class(&ce TSRMLS_CC); zend_class_implements(php_driver_varint_ce TSRMLS_CC, 2, php_driver_value_ce, php_driver_numeric_ce); php_driver_varint_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL; php_driver_varint_ce->create_object = php_driver_varint_new; memcpy(&php_driver_varint_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); php_driver_varint_handlers.std.get_properties = php_driver_varint_properties; #if PHP_VERSION_ID >= 50400 php_driver_varint_handlers.std.get_gc = php_driver_varint_gc; #endif php_driver_varint_handlers.std.compare_objects = php_driver_varint_compare; php_driver_varint_handlers.std.cast_object = php_driver_varint_cast; php_driver_varint_handlers.hash_value = php_driver_varint_hash_value; php_driver_varint_handlers.std.clone_obj = NULL; } cassandra-1.3.0/src/Varint.h0000644000076500000240000000135713062515665016353 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_VARINT_H #define PHP_DRIVER_VARINT_H void php_driver_varint_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_VARINT_H */ cassandra-1.3.0/util/bytes.c0000644000076500000240000000211513062515665016410 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include #include "util/bytes.h" void php_driver_bytes_to_hex(const char *bin, int len, char **out, int *out_len) { char hex_str[] = "0123456789abcdef"; int i; *out_len = len * 2 + 2; *out = (char *) emalloc(sizeof(char) * (len * 2 + 3)); (*out)[0] = '0'; (*out)[1] = 'x'; (*out)[len * 2 + 2] = '\0'; for (i = 0; i < len; i++) { (*out)[i * 2 + 2] = hex_str[(bin[i] >> 4) & 0x0F]; (*out)[i * 2 + 3] = hex_str[(bin[i] ) & 0x0F]; } } cassandra-1.3.0/util/bytes.h0000644000076500000240000000140313062515665016414 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_BYTES_H #define PHP_DRIVER_BYTES_H void php_driver_bytes_to_hex(const char *bin, int len, char **out, int *out_len); #endif /* PHP_DRIVER_BYTES_H */ cassandra-1.3.0/util/collections.c0000644000076500000240000007450313062515665017612 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/bytes.h" #include "util/collections.h" #include "util/hash.h" #include "util/math.h" #include "util/types.h" #include #define EXPECTING_VALUE(expected) \ { \ throw_invalid_argument(object, "argument", expected TSRMLS_CC); \ return 0; \ } #define INSTANCE_OF(cls) \ (Z_TYPE_P(object) == IS_OBJECT && instanceof_function(Z_OBJCE_P(object), cls TSRMLS_CC)) #define CHECK_ERROR(rc) ASSERT_SUCCESS_BLOCK(rc, result = 0;) int php_driver_validate_object(zval *object, zval *ztype TSRMLS_DC) { php_driver_type *type; if (Z_TYPE_P(object) == IS_NULL) return 1; type = PHP_DRIVER_GET_TYPE(ztype); switch (type->type) { case CASS_VALUE_TYPE_VARCHAR: case CASS_VALUE_TYPE_TEXT: case CASS_VALUE_TYPE_ASCII: if (Z_TYPE_P(object) != IS_STRING) { EXPECTING_VALUE("a string"); } return 1; case CASS_VALUE_TYPE_DOUBLE: if (Z_TYPE_P(object) != IS_DOUBLE) { EXPECTING_VALUE("a float"); } return 1; case CASS_VALUE_TYPE_INT: if (Z_TYPE_P(object) != IS_LONG) { EXPECTING_VALUE("an int"); } return 1; case CASS_VALUE_TYPE_BOOLEAN: if (!PHP5TO7_ZVAL_IS_BOOL_P(object)) { EXPECTING_VALUE("a boolean"); } return 1; case CASS_VALUE_TYPE_FLOAT: if (!INSTANCE_OF(php_driver_float_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Float"); } return 1; case CASS_VALUE_TYPE_COUNTER: case CASS_VALUE_TYPE_BIGINT: if (!INSTANCE_OF(php_driver_bigint_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Bigint"); } return 1; case CASS_VALUE_TYPE_SMALL_INT: if (!INSTANCE_OF(php_driver_smallint_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Smallint"); } return 1; case CASS_VALUE_TYPE_TINY_INT: if (!INSTANCE_OF(php_driver_tinyint_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Tinyint"); } return 1; case CASS_VALUE_TYPE_BLOB: if (!INSTANCE_OF(php_driver_blob_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Blob"); } return 1; case CASS_VALUE_TYPE_DECIMAL: if (!INSTANCE_OF(php_driver_decimal_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Decimal"); } return 1; case CASS_VALUE_TYPE_DURATION: if (!INSTANCE_OF(php_driver_duration_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Duration"); } return 1; case CASS_VALUE_TYPE_TIMESTAMP: if (!INSTANCE_OF(php_driver_timestamp_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Timestamp"); } return 1; case CASS_VALUE_TYPE_DATE: if (!INSTANCE_OF(php_driver_date_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Date"); } return 1; case CASS_VALUE_TYPE_TIME: if (!INSTANCE_OF(php_driver_time_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Time"); } return 1; case CASS_VALUE_TYPE_UUID: if (!INSTANCE_OF(php_driver_uuid_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Uuid"); } return 1; case CASS_VALUE_TYPE_VARINT: if (!INSTANCE_OF(php_driver_varint_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Varint"); } return 1; case CASS_VALUE_TYPE_TIMEUUID: if (!INSTANCE_OF(php_driver_timeuuid_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Timeuuid"); } return 1; case CASS_VALUE_TYPE_INET: if (!INSTANCE_OF(php_driver_inet_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Inet"); } return 1; case CASS_VALUE_TYPE_MAP: if (!INSTANCE_OF(php_driver_map_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Map"); } else { php_driver_map *map = PHP_DRIVER_GET_MAP(object); php_driver_type *map_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); if (php_driver_type_compare(map_type, type TSRMLS_CC) != 0) { return 0; } } return 1; case CASS_VALUE_TYPE_SET: if (!INSTANCE_OF(php_driver_set_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Set"); } else { php_driver_set *set = PHP_DRIVER_GET_SET(object); php_driver_type *set_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->type)); if (php_driver_type_compare(set_type, type TSRMLS_CC) != 0) { return 0; } } return 1; case CASS_VALUE_TYPE_LIST: if (!INSTANCE_OF(php_driver_collection_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Collection"); } else { php_driver_collection *collection = PHP_DRIVER_GET_COLLECTION(object); php_driver_type *collection_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(collection->type)); if (php_driver_type_compare(collection_type, type TSRMLS_CC) != 0) { return 0; } } return 1; case CASS_VALUE_TYPE_TUPLE: if (!INSTANCE_OF(php_driver_tuple_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\Tuple"); } else { php_driver_tuple *tuple = PHP_DRIVER_GET_TUPLE(object); php_driver_type *tuple_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(tuple->type)); if (php_driver_type_compare(tuple_type, type TSRMLS_CC) != 0) { return 0; } } return 1; case CASS_VALUE_TYPE_UDT: if (!INSTANCE_OF(php_driver_user_type_value_ce)) { EXPECTING_VALUE("an instance of " PHP_DRIVER_NAMESPACE "\\UserTypeValue"); } else { php_driver_user_type_value *user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(object); php_driver_type *user_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(user_type_value->type)); if (php_driver_type_compare(user_type, type TSRMLS_CC) != 0) { return 0; } } return 1; default: EXPECTING_VALUE("a simple " PHP_DRIVER_NAMESPACE " value"); return 0; } } int php_driver_value_type(char *type, CassValueType *value_type TSRMLS_DC) { if (strcmp("ascii", type) == 0) { *value_type = CASS_VALUE_TYPE_ASCII; } else if (strcmp("bigint", type) == 0) { *value_type = CASS_VALUE_TYPE_BIGINT; } else if (strcmp("smallint", type) == 0) { *value_type = CASS_VALUE_TYPE_SMALL_INT; } else if (strcmp("tinyint", type) == 0) { *value_type = CASS_VALUE_TYPE_TINY_INT; } else if (strcmp("blob", type) == 0) { *value_type = CASS_VALUE_TYPE_BLOB; } else if (strcmp("boolean", type) == 0) { *value_type = CASS_VALUE_TYPE_BOOLEAN; } else if (strcmp("counter", type) == 0) { *value_type = CASS_VALUE_TYPE_COUNTER; } else if (strcmp("decimal", type) == 0) { *value_type = CASS_VALUE_TYPE_DECIMAL; } else if (strcmp("duration", type) == 0) { *value_type = CASS_VALUE_TYPE_DURATION; } else if (strcmp("double", type) == 0) { *value_type = CASS_VALUE_TYPE_DOUBLE; } else if (strcmp("float", type) == 0) { *value_type = CASS_VALUE_TYPE_FLOAT; } else if (strcmp("int", type) == 0) { *value_type = CASS_VALUE_TYPE_INT; } else if (strcmp("text", type) == 0) { *value_type = CASS_VALUE_TYPE_TEXT; } else if (strcmp("timestamp", type) == 0) { *value_type = CASS_VALUE_TYPE_TIMESTAMP; } else if (strcmp("date", type) == 0) { *value_type = CASS_VALUE_TYPE_DATE; } else if (strcmp("time", type) == 0) { *value_type = CASS_VALUE_TYPE_TIME; } else if (strcmp("uuid", type) == 0) { *value_type = CASS_VALUE_TYPE_UUID; } else if (strcmp("varchar", type) == 0) { *value_type = CASS_VALUE_TYPE_VARCHAR; } else if (strcmp("varint", type) == 0) { *value_type = CASS_VALUE_TYPE_VARINT; } else if (strcmp("timeuuid", type) == 0) { *value_type = CASS_VALUE_TYPE_TIMEUUID; } else if (strcmp("inet", type) == 0) { *value_type = CASS_VALUE_TYPE_INET; } else { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Unsupported type '%s'", type); return 0; } return 1; } static int php_driver_collection_append(CassCollection *collection, zval *value, CassValueType type TSRMLS_DC) { int result = 1; php_driver_blob *blob; php_driver_numeric *numeric; php_driver_timestamp *timestamp; php_driver_date *date; php_driver_time *time; php_driver_uuid *uuid; php_driver_inet *inet; php_driver_duration *duration; size_t size; cass_byte_t *data; php_driver_collection *coll; php_driver_map *map; php_driver_set *set; php_driver_tuple *tup; php_driver_user_type_value *user_type_value; CassCollection *sub_collection; CassTuple *sub_tuple; CassUserType *sub_ut; switch (type) { case CASS_VALUE_TYPE_TEXT: case CASS_VALUE_TYPE_ASCII: case CASS_VALUE_TYPE_VARCHAR: CHECK_ERROR(cass_collection_append_string_n(collection, Z_STRVAL_P(value), Z_STRLEN_P(value))); break; case CASS_VALUE_TYPE_BIGINT: case CASS_VALUE_TYPE_COUNTER: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_collection_append_int64(collection, numeric->data.bigint.value)); break; case CASS_VALUE_TYPE_SMALL_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_collection_append_int16(collection, numeric->data.smallint.value)); break; case CASS_VALUE_TYPE_TINY_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_collection_append_int8(collection, numeric->data.tinyint.value)); break; case CASS_VALUE_TYPE_BLOB: blob = PHP_DRIVER_GET_BLOB(value); CHECK_ERROR(cass_collection_append_bytes(collection, blob->data, blob->size)); break; case CASS_VALUE_TYPE_BOOLEAN: #if PHP_MAJOR_VERSION >= 7 CHECK_ERROR(cass_collection_append_bool(collection, Z_TYPE_P(value) == IS_TRUE ? cass_true : cass_false)); #else CHECK_ERROR(cass_collection_append_bool(collection, Z_BVAL_P(value))); #endif break; case CASS_VALUE_TYPE_DOUBLE: CHECK_ERROR(cass_collection_append_double(collection, Z_DVAL_P(value))); break; case CASS_VALUE_TYPE_FLOAT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_collection_append_float(collection, numeric->data.floating.value)); break; case CASS_VALUE_TYPE_INT: CHECK_ERROR(cass_collection_append_int32(collection, Z_LVAL_P(value))); break; case CASS_VALUE_TYPE_TIMESTAMP: timestamp = PHP_DRIVER_GET_TIMESTAMP(value); CHECK_ERROR(cass_collection_append_int64(collection, timestamp->timestamp)); break; case CASS_VALUE_TYPE_DATE: date = PHP_DRIVER_GET_DATE(value); CHECK_ERROR(cass_collection_append_uint32(collection, date->date)); break; case CASS_VALUE_TYPE_TIME: time = PHP_DRIVER_GET_TIME(value); CHECK_ERROR(cass_collection_append_int64(collection, time->time)); break; case CASS_VALUE_TYPE_UUID: case CASS_VALUE_TYPE_TIMEUUID: uuid = PHP_DRIVER_GET_UUID(value); CHECK_ERROR(cass_collection_append_uuid(collection, uuid->uuid)); break; case CASS_VALUE_TYPE_VARINT: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.varint.value, &size); CHECK_ERROR(cass_collection_append_bytes(collection, data, size)); free(data); break; case CASS_VALUE_TYPE_DECIMAL: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.decimal.value, &size); CHECK_ERROR(cass_collection_append_decimal(collection, data, size, numeric->data.decimal.scale)); free(data); break; case CASS_VALUE_TYPE_DURATION: duration = PHP_DRIVER_GET_DURATION(value); CHECK_ERROR(cass_collection_append_duration(collection, duration->months, duration->days, duration->nanos)); break; case CASS_VALUE_TYPE_INET: inet = PHP_DRIVER_GET_INET(value); CHECK_ERROR(cass_collection_append_inet(collection, inet->inet)); break; case CASS_VALUE_TYPE_LIST: coll = PHP_DRIVER_GET_COLLECTION(value); if (!php_driver_collection_from_collection(coll, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_collection_append_collection(collection, sub_collection)); break; case CASS_VALUE_TYPE_MAP: map = PHP_DRIVER_GET_MAP(value); if (!php_driver_collection_from_map(map, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_collection_append_collection(collection, sub_collection)); break; case CASS_VALUE_TYPE_SET: set = PHP_DRIVER_GET_SET(value); if (!php_driver_collection_from_set(set, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_collection_append_collection(collection, sub_collection)); break; case CASS_VALUE_TYPE_TUPLE: tup = PHP_DRIVER_GET_TUPLE(value); if (!php_driver_tuple_from_tuple(tup, &sub_tuple TSRMLS_CC)) return 0; CHECK_ERROR(cass_collection_append_tuple(collection, sub_tuple)); break; case CASS_VALUE_TYPE_UDT: user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(value); if (!php_driver_user_type_from_user_type_value(user_type_value, &sub_ut TSRMLS_CC)) return 0; CHECK_ERROR(cass_collection_append_user_type(collection, sub_ut)); break; default: zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unsupported collection type"); return 0; } return result; } static int php_driver_tuple_set(CassTuple *tuple, php5to7_ulong index, zval *value, CassValueType type TSRMLS_DC) { int result = 1; php_driver_blob *blob; php_driver_numeric *numeric; php_driver_timestamp *timestamp; php_driver_date *date; php_driver_time *time; php_driver_uuid *uuid; php_driver_inet *inet; php_driver_duration *duration; size_t size; cass_byte_t *data; php_driver_collection *coll; php_driver_map *map; php_driver_set *set; php_driver_tuple *tup; php_driver_user_type_value *user_type_value; CassCollection *sub_collection; CassTuple *sub_tuple; CassUserType *sub_ut; if (Z_TYPE_P(value) == IS_NULL) { CHECK_ERROR(cass_tuple_set_null(tuple, index)); return result; } switch (type) { case CASS_VALUE_TYPE_TEXT: case CASS_VALUE_TYPE_ASCII: case CASS_VALUE_TYPE_VARCHAR: CHECK_ERROR(cass_tuple_set_string_n(tuple, index, Z_STRVAL_P(value), Z_STRLEN_P(value))); break; case CASS_VALUE_TYPE_BIGINT: case CASS_VALUE_TYPE_COUNTER: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_tuple_set_int64(tuple, index, numeric->data.bigint.value)); break; case CASS_VALUE_TYPE_SMALL_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_tuple_set_int16(tuple, index, numeric->data.smallint.value)); break; case CASS_VALUE_TYPE_TINY_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_tuple_set_int8(tuple, index, numeric->data.tinyint.value)); break; case CASS_VALUE_TYPE_BLOB: blob = PHP_DRIVER_GET_BLOB(value); CHECK_ERROR(cass_tuple_set_bytes(tuple, index, blob->data, blob->size)); break; case CASS_VALUE_TYPE_BOOLEAN: #if PHP_MAJOR_VERSION >= 7 CHECK_ERROR(cass_tuple_set_bool(tuple, index, Z_TYPE_P(value) == IS_TRUE ? cass_true : cass_false)); #else CHECK_ERROR(cass_tuple_set_bool(tuple, index, Z_BVAL_P(value))); #endif break; case CASS_VALUE_TYPE_DOUBLE: CHECK_ERROR(cass_tuple_set_double(tuple, index, Z_DVAL_P(value))); break; case CASS_VALUE_TYPE_FLOAT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_tuple_set_float(tuple, index, numeric->data.floating.value)); break; case CASS_VALUE_TYPE_INT: CHECK_ERROR(cass_tuple_set_int32(tuple, index, Z_LVAL_P(value))); break; case CASS_VALUE_TYPE_TIMESTAMP: timestamp = PHP_DRIVER_GET_TIMESTAMP(value); CHECK_ERROR(cass_tuple_set_int64(tuple, index, timestamp->timestamp)); break; case CASS_VALUE_TYPE_DATE: date = PHP_DRIVER_GET_DATE(value); CHECK_ERROR(cass_tuple_set_uint32(tuple, index, date->date)); break; case CASS_VALUE_TYPE_TIME: time = PHP_DRIVER_GET_TIME(value); CHECK_ERROR(cass_tuple_set_int64(tuple, index, time->time)); break; case CASS_VALUE_TYPE_UUID: case CASS_VALUE_TYPE_TIMEUUID: uuid = PHP_DRIVER_GET_UUID(value); CHECK_ERROR(cass_tuple_set_uuid(tuple, index, uuid->uuid)); break; case CASS_VALUE_TYPE_VARINT: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.varint.value, &size); CHECK_ERROR(cass_tuple_set_bytes(tuple, index, data, size)); free(data); break; case CASS_VALUE_TYPE_DECIMAL: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.decimal.value, &size); CHECK_ERROR(cass_tuple_set_decimal(tuple, index, data, size, numeric->data.decimal.scale)); free(data); break; case CASS_VALUE_TYPE_DURATION: duration = PHP_DRIVER_GET_DURATION(value); CHECK_ERROR(cass_tuple_set_duration(tuple, index, duration->months, duration->days, duration->nanos)); break; case CASS_VALUE_TYPE_INET: inet = PHP_DRIVER_GET_INET(value); CHECK_ERROR(cass_tuple_set_inet(tuple, index, inet->inet)); break; case CASS_VALUE_TYPE_LIST: coll = PHP_DRIVER_GET_COLLECTION(value); if (!php_driver_collection_from_collection(coll, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_tuple_set_collection(tuple, index, sub_collection)); break; case CASS_VALUE_TYPE_MAP: map = PHP_DRIVER_GET_MAP(value); if (!php_driver_collection_from_map(map, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_tuple_set_collection(tuple, index, sub_collection)); break; case CASS_VALUE_TYPE_SET: set = PHP_DRIVER_GET_SET(value); if (!php_driver_collection_from_set(set, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_tuple_set_collection(tuple, index, sub_collection)); break; case CASS_VALUE_TYPE_TUPLE: tup = PHP_DRIVER_GET_TUPLE(value); if (!php_driver_tuple_from_tuple(tup, &sub_tuple TSRMLS_CC)) return 0; CHECK_ERROR(cass_tuple_set_tuple(tuple, index, sub_tuple)); break; case CASS_VALUE_TYPE_UDT: user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(value); if (!php_driver_user_type_from_user_type_value(user_type_value, &sub_ut TSRMLS_CC)) return 0; CHECK_ERROR(cass_tuple_set_user_type(tuple, index, sub_ut)); break; default: zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unsupported collection type"); return 0; } return result; } static int php_driver_user_type_set(CassUserType *ut, const char* name, zval *value, CassValueType type TSRMLS_DC) { int result = 1; php_driver_blob *blob; php_driver_numeric *numeric; php_driver_timestamp *timestamp; php_driver_date *date; php_driver_time *time; php_driver_uuid *uuid; php_driver_inet *inet; php_driver_duration *duration; size_t size; cass_byte_t *data; php_driver_collection *coll; php_driver_map *map; php_driver_set *set; php_driver_tuple *tuple; php_driver_user_type_value *user_type_value; CassCollection *sub_collection; CassTuple *sub_tup; CassUserType *sub_ut; if (Z_TYPE_P(value) == IS_NULL) { CHECK_ERROR(cass_user_type_set_null_by_name(ut, name)); return result; } switch (type) { case CASS_VALUE_TYPE_TEXT: case CASS_VALUE_TYPE_ASCII: case CASS_VALUE_TYPE_VARCHAR: CHECK_ERROR(cass_user_type_set_string_by_name(ut, name, Z_STRVAL_P(value))); break; case CASS_VALUE_TYPE_BIGINT: case CASS_VALUE_TYPE_COUNTER: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_user_type_set_int64_by_name(ut, name, numeric->data.bigint.value)); break; case CASS_VALUE_TYPE_SMALL_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_user_type_set_int16_by_name(ut, name, numeric->data.smallint.value)); break; case CASS_VALUE_TYPE_TINY_INT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_user_type_set_int8_by_name(ut, name, numeric->data.tinyint.value)); break; case CASS_VALUE_TYPE_BLOB: blob = PHP_DRIVER_GET_BLOB(value); CHECK_ERROR(cass_user_type_set_bytes_by_name(ut, name, blob->data, blob->size)); break; case CASS_VALUE_TYPE_BOOLEAN: #if PHP_MAJOR_VERSION >= 7 CHECK_ERROR(cass_user_type_set_bool_by_name(ut, name, Z_TYPE_P(value) == IS_TRUE ? cass_true : cass_false)); #else CHECK_ERROR(cass_user_type_set_bool_by_name(ut, name, Z_BVAL_P(value))); #endif break; case CASS_VALUE_TYPE_DOUBLE: CHECK_ERROR(cass_user_type_set_double_by_name(ut, name, Z_DVAL_P(value))); break; case CASS_VALUE_TYPE_FLOAT: numeric = PHP_DRIVER_GET_NUMERIC(value); CHECK_ERROR(cass_user_type_set_float_by_name(ut, name, numeric->data.floating.value)); break; case CASS_VALUE_TYPE_INT: CHECK_ERROR(cass_user_type_set_int32_by_name(ut, name, Z_LVAL_P(value))); break; case CASS_VALUE_TYPE_TIMESTAMP: timestamp = PHP_DRIVER_GET_TIMESTAMP(value); CHECK_ERROR(cass_user_type_set_int64_by_name(ut, name, timestamp->timestamp)); break; case CASS_VALUE_TYPE_DATE: date = PHP_DRIVER_GET_DATE(value); CHECK_ERROR(cass_user_type_set_uint32_by_name(ut, name, date->date)); break; case CASS_VALUE_TYPE_TIME: time = PHP_DRIVER_GET_TIME(value); CHECK_ERROR(cass_user_type_set_int64_by_name(ut, name, time->time)); break; case CASS_VALUE_TYPE_UUID: case CASS_VALUE_TYPE_TIMEUUID: uuid = PHP_DRIVER_GET_UUID(value); CHECK_ERROR(cass_user_type_set_uuid_by_name(ut, name, uuid->uuid)); break; case CASS_VALUE_TYPE_VARINT: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.varint.value, &size); CHECK_ERROR(cass_user_type_set_bytes_by_name(ut, name, data, size)); free(data); break; case CASS_VALUE_TYPE_DECIMAL: numeric = PHP_DRIVER_GET_NUMERIC(value); data = (cass_byte_t *) export_twos_complement(numeric->data.decimal.value, &size); CHECK_ERROR(cass_user_type_set_decimal_by_name(ut, name, data, size, numeric->data.decimal.scale)); free(data); break; case CASS_VALUE_TYPE_DURATION: duration = PHP_DRIVER_GET_DURATION(value); CHECK_ERROR(cass_user_type_set_duration_by_name(ut, name, duration->months, duration->days, duration->nanos)); break; case CASS_VALUE_TYPE_INET: inet = PHP_DRIVER_GET_INET(value); CHECK_ERROR(cass_user_type_set_inet_by_name(ut, name, inet->inet)); break; case CASS_VALUE_TYPE_LIST: coll = PHP_DRIVER_GET_COLLECTION(value); if (!php_driver_collection_from_collection(coll, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_user_type_set_collection_by_name(ut, name, sub_collection)); break; case CASS_VALUE_TYPE_MAP: map = PHP_DRIVER_GET_MAP(value); if (!php_driver_collection_from_map(map, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_user_type_set_collection_by_name(ut, name, sub_collection)); break; case CASS_VALUE_TYPE_SET: set = PHP_DRIVER_GET_SET(value); if (!php_driver_collection_from_set(set, &sub_collection TSRMLS_CC)) return 0; CHECK_ERROR(cass_user_type_set_collection_by_name(ut, name, sub_collection)); break; case CASS_VALUE_TYPE_TUPLE: tuple = PHP_DRIVER_GET_TUPLE(value); if (!php_driver_tuple_from_tuple(tuple, &sub_tup TSRMLS_CC)) return 0; CHECK_ERROR(cass_user_type_set_tuple_by_name(ut, name, sub_tup)); break; case CASS_VALUE_TYPE_UDT: user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(value); if (!php_driver_user_type_from_user_type_value(user_type_value, &sub_ut TSRMLS_CC)) return 0; CHECK_ERROR(cass_user_type_set_user_type_by_name(ut, name, sub_ut)); break; default: zend_throw_exception_ex(php_driver_runtime_exception_ce, 0 TSRMLS_CC, "Unsupported collection type"); return 0; } return result; } int php_driver_collection_from_set(php_driver_set *set, CassCollection **collection_ptr TSRMLS_DC) { int result = 1; CassCollection *collection = NULL; php_driver_type *type; php_driver_type *value_type; php_driver_set_entry *curr, *temp; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->type)); value_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.set.value_type)); #if CURRENT_CPP_DRIVER_VERSION > CPP_DRIVER_VERSION(2, 2, 2) collection = cass_collection_new_from_data_type(type->data_type, HASH_COUNT(set->entries)); #else collection = cass_collection_new(CASS_COLLECTION_TYPE_SET, HASH_COUNT(set->entries)); #endif HASH_ITER(hh, set->entries, curr, temp) { if (!php_driver_collection_append(collection, PHP5TO7_ZVAL_MAYBE_P(curr->value), value_type->type TSRMLS_CC)) { result = 0; break; } } if (result) *collection_ptr = collection; else cass_collection_free(collection); return result; } int php_driver_collection_from_collection(php_driver_collection *coll, CassCollection **collection_ptr TSRMLS_DC) { int result = 1; php5to7_zval *current; php_driver_type *type; php_driver_type *value_type; CassCollection *collection; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(coll->type)); value_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.collection.value_type)); #if CURRENT_CPP_DRIVER_VERSION > CPP_DRIVER_VERSION(2, 2, 2) collection = cass_collection_new_from_data_type(type->data_type, zend_hash_num_elements(&coll->values)); #else collection = cass_collection_new(CASS_COLLECTION_TYPE_LIST, zend_hash_num_elements(&coll->values)); #endif PHP5TO7_ZEND_HASH_FOREACH_VAL(&coll->values, current) { if (!php_driver_collection_append(collection, PHP5TO7_ZVAL_MAYBE_DEREF(current), value_type->type TSRMLS_CC)) { result = 0; break; } } PHP5TO7_ZEND_HASH_FOREACH_END(&coll->values); if (result) *collection_ptr = collection; else cass_collection_free(collection); return result; } int php_driver_collection_from_map(php_driver_map *map, CassCollection **collection_ptr TSRMLS_DC) { int result = 1; CassCollection *collection; php_driver_type *type; php_driver_type *key_type; php_driver_type *value_type; php_driver_map_entry *curr, *temp; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->type)); value_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.map.value_type)); key_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type)); #if CURRENT_CPP_DRIVER_VERSION > CPP_DRIVER_VERSION(2, 2, 2) collection = cass_collection_new_from_data_type(type->data_type, HASH_COUNT(map->entries)); #else collection = cass_collection_new(CASS_COLLECTION_TYPE_MAP, HASH_COUNT(map->entries)); #endif HASH_ITER(hh, map->entries, curr, temp) { if (!php_driver_collection_append(collection, PHP5TO7_ZVAL_MAYBE_P(curr->key), key_type->type TSRMLS_CC)) { result = 0; break; } if (!php_driver_collection_append(collection, PHP5TO7_ZVAL_MAYBE_P(curr->value), value_type->type TSRMLS_CC)) { result = 0; break; } } if (result) *collection_ptr = collection; else cass_collection_free(collection); return result; } int php_driver_tuple_from_tuple(php_driver_tuple *tuple, CassTuple **output TSRMLS_DC) { int result = 1; php5to7_ulong num_key; php5to7_zval *current; php_driver_type *type; CassTuple *tup; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(tuple->type)); tup = cass_tuple_new_from_data_type(type->data_type); PHP5TO7_ZEND_HASH_FOREACH_NUM_KEY_VAL(&tuple->values, num_key, current) { php5to7_zval *zsub_type; php_driver_type *sub_type; if (!PHP5TO7_ZEND_HASH_INDEX_FIND(&type->data.tuple.types, num_key, zsub_type) || !php_driver_validate_object(PHP5TO7_ZVAL_MAYBE_DEREF(current), PHP5TO7_ZVAL_MAYBE_DEREF(zsub_type) TSRMLS_CC)) { result = 0; break; } sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(zsub_type)); if (!php_driver_tuple_set(tup, num_key, PHP5TO7_ZVAL_MAYBE_DEREF(current), sub_type->type TSRMLS_CC)) { result = 0; break; } } PHP5TO7_ZEND_HASH_FOREACH_END(&tuple->values); if (result) *output = tup; else cass_tuple_free(tup); return result; } int php_driver_user_type_from_user_type_value(php_driver_user_type_value *user_type_value, CassUserType **output TSRMLS_DC) { int result = 1; char *name; php5to7_zval *current; php_driver_type *type; CassUserType *ut; type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(user_type_value->type)); ut = cass_user_type_new_from_data_type(type->data_type); PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(&user_type_value->values, name, current) { php5to7_zval *zsub_type; php_driver_type *sub_type; if (!PHP5TO7_ZEND_HASH_FIND(&type->data.udt.types, name, strlen(name) + 1, zsub_type) || !php_driver_validate_object(PHP5TO7_ZVAL_MAYBE_DEREF(current), PHP5TO7_ZVAL_MAYBE_DEREF(zsub_type) TSRMLS_CC)) { result = 0; break; } sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(zsub_type)); if (!php_driver_user_type_set(ut, name, PHP5TO7_ZVAL_MAYBE_DEREF(current), sub_type->type TSRMLS_CC)) { result = 0; break; } } PHP5TO7_ZEND_HASH_FOREACH_END(&user_type_value->values); if (result) *output = ut; else cass_user_type_free(ut); return result; } cassandra-1.3.0/util/collections.h0000644000076500000240000000256613062515665017617 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UTIL_COLLECTIONS_H #define PHP_DRIVER_UTIL_COLLECTIONS_H int php_driver_validate_object(zval* object, zval* ztype TSRMLS_DC); int php_driver_value_type(char* type, CassValueType* value_type TSRMLS_DC); int php_driver_collection_from_set(php_driver_set* set, CassCollection** collection_ptr TSRMLS_DC); int php_driver_collection_from_collection(php_driver_collection* coll, CassCollection** collection_ptr TSRMLS_DC); int php_driver_collection_from_map(php_driver_map* map, CassCollection** collection_ptr TSRMLS_DC); int php_driver_tuple_from_tuple(php_driver_tuple *tuple, CassTuple **output TSRMLS_DC); int php_driver_user_type_from_user_type_value(php_driver_user_type_value *user_type_value, CassUserType **output TSRMLS_DC); #endif /* PHP_DRIVER_UTIL_COLLECTIONS_H */ cassandra-1.3.0/util/consistency.c0000644000076500000240000000421613062515665017627 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" int php_driver_get_consistency(zval *consistency, long *result TSRMLS_DC) { if (consistency && Z_TYPE_P(consistency) == IS_LONG) { switch (Z_LVAL_P(consistency)) { case CASS_CONSISTENCY_ANY: case CASS_CONSISTENCY_ONE: case CASS_CONSISTENCY_TWO: case CASS_CONSISTENCY_THREE: case CASS_CONSISTENCY_QUORUM: case CASS_CONSISTENCY_ALL: case CASS_CONSISTENCY_LOCAL_QUORUM: case CASS_CONSISTENCY_EACH_QUORUM: case CASS_CONSISTENCY_SERIAL: case CASS_CONSISTENCY_LOCAL_SERIAL: case CASS_CONSISTENCY_LOCAL_ONE: *result = Z_LVAL_P(consistency); break; default: INVALID_ARGUMENT_VALUE(consistency, "one of " PHP_DRIVER_NAMESPACE "::CONSISTENCY_*", FAILURE); } } else { INVALID_ARGUMENT_VALUE(consistency, "one of " PHP_DRIVER_NAMESPACE "::CONSISTENCY_*", FAILURE); } return SUCCESS; } int php_driver_get_serial_consistency(zval *serial_consistency, long *result TSRMLS_DC) { if (serial_consistency && Z_TYPE_P(serial_consistency) == IS_LONG) { switch (Z_LVAL_P(serial_consistency)) { case CASS_CONSISTENCY_SERIAL: case CASS_CONSISTENCY_LOCAL_SERIAL: *result = Z_LVAL_P(serial_consistency); break; default: INVALID_ARGUMENT_VALUE(serial_consistency, "either " PHP_DRIVER_NAMESPACE "::CONSISTENCY_SERIAL or Cassanra::CASS_CONSISTENCY_LOCAL_SERIAL", FAILURE); } } else { INVALID_ARGUMENT_VALUE(serial_consistency, "either " PHP_DRIVER_NAMESPACE "::CONSISTENCY_SERIAL or Cassanra::CASS_CONSISTENCY_LOCAL_SERIAL", FAILURE); } return SUCCESS; } cassandra-1.3.0/util/consistency.h0000644000076500000240000000154713062515665017640 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_CONSISTENCY_H #define PHP_DRIVER_CONSISTENCY_H int php_driver_get_consistency(zval *consistency, long *result TSRMLS_DC); int php_driver_get_serial_consistency(zval *serial_consistency, long *result TSRMLS_DC); #endif /* PHP_DRIVER_CONSISTENCY_H */ cassandra-1.3.0/util/future.c0000644000076500000240000000412613062515665016600 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "future.h" int php_driver_future_wait_timed(CassFuture *future, zval *timeout TSRMLS_DC) { cass_duration_t timeout_us; if (cass_future_ready(future)) return SUCCESS; #if PHP_MAJOR_VERSION >= 7 if (timeout == NULL || Z_TYPE_P(timeout) == IS_NULL || Z_TYPE_P(timeout) == IS_UNDEF) { #else if (timeout == NULL || Z_TYPE_P(timeout) == IS_NULL) { #endif cass_future_wait(future); } else { if ((Z_TYPE_P(timeout) == IS_LONG && Z_LVAL_P(timeout) > 0)) { timeout_us = Z_LVAL_P(timeout) * 1000000; } else if ((Z_TYPE_P(timeout) == IS_DOUBLE && Z_DVAL_P(timeout) > 0)) { timeout_us = ceil(Z_DVAL_P(timeout) * 1000000); } else { INVALID_ARGUMENT_VALUE(timeout, "an positive number of seconds or null", FAILURE); } if (!cass_future_wait_timed(future, timeout_us)) { zend_throw_exception_ex(php_driver_timeout_exception_ce, 0 TSRMLS_CC, "Future hasn't resolved within %f seconds", timeout_us / 1000000.0); return FAILURE; } } return SUCCESS; } int php_driver_future_is_error(CassFuture *future TSRMLS_DC) { int rc = cass_future_error_code(future); if (rc != CASS_OK) { const char *message; size_t message_len; cass_future_error_message(future, &message, &message_len); zend_throw_exception_ex(exception_class(rc), rc TSRMLS_CC, "%.*s", (int) message_len, message); return FAILURE; } return SUCCESS; } cassandra-1.3.0/util/future.h0000644000076500000240000000152213062515665016602 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UTIL_FUTURE_H #define PHP_DRIVER_UTIL_FUTURE_H int php_driver_future_wait_timed(CassFuture *future, zval *timeout TSRMLS_DC); int php_driver_future_is_error(CassFuture *future TSRMLS_DC); #endif /* PHP_DRIVER_UTIL_FUTURE_H */ cassandra-1.3.0/util/hash.c0000644000076500000240000001012313062515665016203 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/hash.h" static inline cass_int64_t double_to_bits(cass_double_t value) { cass_int64_t bits; if (zend_isnan(value)) return 0x7ff8000000000000LL; /* A canonical NaN value */ memcpy(&bits, &value, sizeof(cass_int64_t)); return bits; } static inline unsigned double_hash(cass_double_t value) { return php_driver_bigint_hash(double_to_bits(value)); } unsigned php_driver_value_hash(zval* zvalue TSRMLS_DC) { switch (Z_TYPE_P(zvalue)) { case IS_LONG: #if SIZEOF_LONG == 4 return Z_LVAL_P(zvalue); #elif SIZEOF_LONG == 8 return php_driver_bigint_hash(Z_LVAL_P(zvalue)); #else #error "Unexpected sizeof(long)" #endif case IS_DOUBLE: return double_hash(Z_DVAL_P(zvalue)); #if PHP_MAJOR_VERSION >= 7 case IS_TRUE: return 1; case IS_FALSE: return 1; #else case IS_BOOL: return Z_BVAL_P(zvalue); #endif case IS_STRING: return zend_inline_hash_func(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue)); #if PHP_MAJOR_VERSION >= 7 case IS_OBJECT: return ((php_driver_value_handlers *)Z_OBJ_P(zvalue)->handlers)->hash_value(zvalue TSRMLS_CC); #else case IS_OBJECT: return ((php_driver_value_handlers *)Z_OBJVAL_P(zvalue).handlers)->hash_value(zvalue TSRMLS_CC); #endif default: break; } return 0; } static inline int double_compare(cass_double_t d1, cass_double_t d2) { cass_int64_t bits1, bits2; if (d1 < d2) return -1; if (d1 > d2) return 1; bits1 = double_to_bits(d1); bits2 = double_to_bits(d2); /* Handle NaNs and negative and positive 0.0 */ return PHP_DRIVER_COMPARE(bits1, bits2); } int php_driver_value_compare(zval* zvalue1, zval* zvalue2 TSRMLS_DC) { if (zvalue1 == zvalue2) return 0; if (Z_TYPE_P(zvalue1) != Z_TYPE_P(zvalue2)) { return Z_TYPE_P(zvalue1) < Z_TYPE_P(zvalue2) ? -1 : 1; } switch (Z_TYPE_P(zvalue1)) { case IS_NULL: return 0; case IS_LONG: return PHP_DRIVER_COMPARE(Z_LVAL_P(zvalue1), Z_LVAL_P(zvalue2)); case IS_DOUBLE: return double_compare(Z_DVAL_P(zvalue1), Z_DVAL_P(zvalue2)); #if PHP_MAJOR_VERSION >= 7 case IS_TRUE: return Z_TYPE_P(zvalue2) == IS_TRUE ? 0 : 1; case IS_FALSE: return Z_TYPE_P(zvalue2) == IS_FALSE ? 0 : -1; #else case IS_BOOL: return PHP_DRIVER_COMPARE(Z_BVAL_P(zvalue1), Z_BVAL_P(zvalue2)); #endif case IS_STRING: return zend_binary_zval_strcmp(zvalue1, zvalue2); #if PHP_MAJOR_VERSION >= 7 case IS_OBJECT: return Z_OBJ_P(zvalue1)->handlers->compare_objects(zvalue1, zvalue2 TSRMLS_CC); #else case IS_OBJECT: return Z_OBJVAL_P(zvalue1).handlers->compare_objects(zvalue1, zvalue2 TSRMLS_CC); #endif default: break; } return 1; } int php_driver_data_compare(const void* a, const void* b TSRMLS_DC) { Bucket *f, *s; zval *first, *second; #if PHP_MAJOR_VERSION >= 7 f = (Bucket *)a; s = (Bucket *)b; first = &f->val; second = &s->val; #else f = *((Bucket **) a); s = *((Bucket **) b); first = *((zval **) f->pData); second = *((zval **) s->pData); #endif return php_driver_value_compare(first, second TSRMLS_CC); } unsigned php_driver_mpz_hash(unsigned seed, mpz_t n) { size_t i; size_t size = mpz_size(n); unsigned hashv = seed; #if GMP_LIMB_BITS == 32 for (i = 0; i < size; ++i) { hashv = php_driver_combine_hash(hashv, mpz_getlimbn(n, i)); } #elif GMP_LIMB_BITS == 64 for (i = 0; i < size; ++i) { hashv = php_driver_combine_hash(hashv, php_driver_bigint_hash(mpz_getlimbn(n, i))); } #else #error "Unexpected GMP limb bits size" #endif return hashv; } cassandra-1.3.0/util/hash.h0000644000076500000240000000402713062515665016216 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_HASH_H #define PHP_DRIVER_HASH_H #define uthash_malloc(sz) emalloc(sz) #define uthash_free(ptr,sz) efree(ptr) #define HASH_FUNCTION(key,keylen,num_bkts,hashv,bkt) \ hashv = php_driver_value_hash((zval*)key TSRMLS_CC); \ bkt = (hashv) & (num_bkts - 1U) #define HASH_KEYCOMPARE(a, b, len) \ php_driver_value_compare((zval*)a, (zval*)b TSRMLS_CC) #undef HASH_ADD /* Previously defined in Zend/zend_hash.h */ #include "util/uthash.h" #define HASH_FIND_ZVAL(head, zvptr, out) \ HASH_FIND(hh, head, zvptr, 0, out) #define HASH_ADD_ZVAL(head, fieldname, add) \ HASH_ADD_KEYPTR(hh, head, PHP5TO7_ZVAL_MAYBE_P(((add)->fieldname)), 0, add) struct php_driver_map_entry_ { php5to7_zval key; php5to7_zval value; UT_hash_handle hh; }; struct php_driver_set_entry_ { php5to7_zval value; UT_hash_handle hh; }; #define PHP_DRIVER_COMPARE(a, b) ((a) < (b) ? -1 : (a) > (b)) unsigned php_driver_value_hash(zval* zvalue TSRMLS_DC); int php_driver_value_compare(zval* zvalue1, zval* zvalue2 TSRMLS_DC); int php_driver_data_compare(const void* a, const void* b TSRMLS_DC); unsigned php_driver_mpz_hash(unsigned seed, mpz_t n); static inline unsigned php_driver_bigint_hash(cass_int64_t value) { return (unsigned)(value ^ (value >> 32)); } static inline unsigned php_driver_combine_hash(unsigned seed, unsigned hashv) { return seed ^ (hashv + 0x9e3779b9 + (seed << 6) + (seed >> 2)); } #endif /* PHP_DRIVER_HASH_H */ cassandra-1.3.0/util/inet.c0000644000076500000240000003124113062515665016223 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/inet.h" #include #define IPV4 1 #define IPV6 2 #define TOKEN_MAX_LEN 4 #define IP_MAX_ADDRLEN 50 #define EXPECTING_TOKEN(expected) \ zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, \ "Unexpected %s at position %d in address \"%s\", expected " expected, \ ip_address_describe_token(type), ((int) (in_ptr - in) - 1), in \ ); \ return 0; enum token_type { TOKEN_END = 0, TOKEN_COLON, TOKEN_DOT, TOKEN_HEX, TOKEN_DEC, TOKEN_ILLEGAL }; enum parser_state { STATE_START = 0, STATE_FIELD = 1, STATE_COMPRESSED = 2, STATE_AFTERHEX = 3, STATE_AFTERDEC = 4, STATE_IPV4BYTE = 5, STATE_IPV4DOT = 6, STATE_AFTERCOLON = 7, STATE_END = 8 }; static const char * ip_address_describe_token(enum token_type type) { switch (type) { case TOKEN_END : return "end of address"; break; case TOKEN_COLON : return "colon"; break; case TOKEN_DOT : return "dot"; break; case TOKEN_HEX : return "hex address field"; break; case TOKEN_DEC : return "address field"; break; case TOKEN_ILLEGAL: return "illegal character"; break; default : return NULL; } } static enum token_type ip_address_tokenize(char *address, char *token, int *token_len, char **next_token) { enum token_type type; char ch; int len = 0; memset(token, 0, TOKEN_MAX_LEN + 1); ch = address[len]; if (isxdigit(ch)) { int is_hex = 0; while (len < TOKEN_MAX_LEN) { ch = address[len]; if (!isxdigit(ch)) break; /* To be able to differentiate between numbers and hex. */ if (!isdigit(ch)) is_hex = 1; /* Lower case, since IPv6 addresses are case insensitive. */ token[len++] = tolower(ch); } if (is_hex) type = TOKEN_HEX; else type = TOKEN_DEC; } else { switch (ch) { case '\0': type = TOKEN_END; break; case ':' : type = TOKEN_COLON; break; case '.' : type = TOKEN_DOT; break; default : type = TOKEN_ILLEGAL; } token[len++] = ch; } token[len] = '\0'; *next_token = &(address[len]); *token_len = len; return type; } int php_driver_parse_ip_address(char *in, CassInet *inet TSRMLS_DC) { char token[TOKEN_MAX_LEN + 1]; int token_len = -1; int prev_token_len = 0; char *in_ptr = in; enum token_type type; enum parser_state state = STATE_START; int pos = -1; int compress_pos = -1; int ipv4_pos = -1; int ipv4_byte; cass_uint8_t address[CASS_INET_V6_LENGTH] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int domain = 0; if (strlen(in) > (IP_MAX_ADDRLEN - 1)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "The IP address \"%s\" is too long (at most %d characters are expected)", in, IP_MAX_ADDRLEN - 1); return 0; } for (;;) { if (token_len > -1) prev_token_len = token_len; type = ip_address_tokenize(in_ptr, token, &token_len, &in_ptr); if (type == TOKEN_ILLEGAL) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Illegal character \"%c\" at position %d in address \"%s\"", *token, ((int) (in_ptr - in) - 1), in); return 0; } if (state == STATE_START) { /* A colon is found. This must be the start of a compressed * group of zeroes. */ if (type == TOKEN_COLON) { state = STATE_COMPRESSED; continue; } /* At this point, we expect an IP field. */ if (type != TOKEN_HEX && type != TOKEN_DEC) { EXPECTING_TOKEN("an address field or a colon"); } /* The IP field will be handled by the FIELD state */ state = STATE_FIELD; } /* AFTERCOLON: expect either another colon for indicating a * compressed group of zeroes or an IPv6 field. */ if (state == STATE_AFTERCOLON) { if (type == TOKEN_COLON) state = STATE_COMPRESSED; else state = STATE_FIELD; } /* COMPRESSED: expect second colon of a compressed group of zeroes */ if (state == STATE_COMPRESSED) { if (type == TOKEN_COLON) { /* Only one compressed zero block can exist. */ if (compress_pos != -1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Duplicate \"::\" block at position %d in address \"%s\"", ((int) (in_ptr - in) - 1), in); return 0; } compress_pos = pos == -1 ? 0 : pos + 1; state = STATE_FIELD; continue; } else { EXPECTING_TOKEN("a colon"); } } /* FIELD: expect an IP address field. */ if (state == STATE_FIELD) { unsigned int field; /* End of string is only valid after a zero compression * block "::". */ if (type == TOKEN_END && pos + 1 == compress_pos) break; switch (type) { case TOKEN_HEX : state = STATE_AFTERHEX; break; case TOKEN_DEC : state = STATE_AFTERDEC; break; default : EXPECTING_TOKEN("an address field"); } /* Check for too many address bytes. */ if (pos + 3 > CASS_INET_V6_LENGTH) { pos += 2; break; } /* Add the IPv6 field bytes. */ sscanf(token, "%x", &field); address[++pos] = (field & 0xff00) >> 8;; address[++pos] = (field & 0x00ff); continue; } /* AFTERHEX: the previous token was a hexadecimal number. Expect a * colon, which starts the next IPv6 field. */ if (state == STATE_AFTERHEX) { if (type == TOKEN_END) { break; } else if (type == TOKEN_COLON) { state = STATE_AFTERCOLON; continue; } else { EXPECTING_TOKEN("a colon"); } } /* AFTERDEC: the previous token was a decimal number. Expect a * colon, which starts the next IPv6 field, or a dot, which * indicates the start of an IPv4 style address. */ if (state == STATE_AFTERDEC) { if (type == TOKEN_END) { break; } else if (type == TOKEN_COLON) { state = STATE_AFTERCOLON; continue; } else if (type == TOKEN_DOT) { /* Rollback bytes that we assumed to be an IPv6 hex field */ address[pos--] = 0; address[pos--] = 0; in_ptr -= (prev_token_len + 1); /* Continue with IPv4 address parsing. */ state = STATE_IPV4BYTE; ipv4_pos = 0; continue; } else { EXPECTING_TOKEN("a colon or a dot"); } } /* IPV4BYTE: we're parsing an IPv4 style address representation * and are expecting the next byte for it (0-255). */ if (state == STATE_IPV4BYTE) { if (type == TOKEN_DEC) { if (token_len > 1 && token[0] == '0') { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Illegal IPv4 character \"%s\" at position %d " \ "in address \"%s\" (no leading zeroes are allowed)", token, ((int) (in_ptr - in) - 1), in); return 0; } ipv4_byte = atoi(token); if (ipv4_byte < 0 || ipv4_byte > 255) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Illegal IPv4 segment value '%d' at position %d " \ "in address \"%s\" (expected: 0 - 255)", ipv4_byte, ((int) (in_ptr - in) - 1), in); return 0; } /* Check for too many address bytes. */ if (pos + 2 > CASS_INET_V6_LENGTH) { pos++; break; } /* Add the IPv4 byte. */ ipv4_pos++; address[++pos] = ipv4_byte; /* After 4 bytes, our IPv4 address is complete. */ if (ipv4_pos == CASS_INET_V4_LENGTH) { /* When we have seen 4 bytes and if we only have four * bytes in the parsed address so far, then this must be * an IPv4 address. */ if (compress_pos == -1 && pos + 1 == CASS_INET_V4_LENGTH) domain = IPV4; state = STATE_END; } else { state = STATE_IPV4DOT; } continue; } else { EXPECTING_TOKEN("an IPv4 address byte (0 - 255)"); } } /* IPV4DOT: we're parsing an IPv4 style address representation * and are expecting the next dot for it. */ if (state == STATE_IPV4DOT) { if (type == TOKEN_DOT) { state = STATE_IPV4BYTE; continue; } else { EXPECTING_TOKEN("a dot"); } } /* END: we've seen the end of the IP address, no more tokens * are expected. */ if (state == STATE_END) { if (type == TOKEN_END) break; else EXPECTING_TOKEN("the end of address"); } } /* All tokenizing and analyzing is done. * The parser might already have decided that the parsed address * was an IPv4 address. Since the parser does check the byte values * and the length of the address already, we don't have to do * extensive checking on the address anymore at this point. * If we have encountered compressed IPv6 zeroes, then we inflate * the IPv6 address up to its full byte size here. */ if (!domain && compress_pos != -1) { /* Compression is in use, but there is no space for decompression * in the parsed byte array. */ if (pos + 1 >= CASS_INET_V6_LENGTH) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Address \"%s\" contains a compressed zeroes block '::', " "but the address already contains %d bytes or more", address, CASS_INET_V6_LENGTH); return 0; } /* If compression is at the end of the address. We already have * zeroes setup for those fields, so no extra work is needed. */ pos++; if (pos != compress_pos) { int i; int move_len = pos - compress_pos; /* Move bytes after the compression position to the end and * fill the old byte positions with zeroes. */ for (i = 0; i < move_len; i++) { int src_pos = compress_pos + move_len - i - 1; int dst_pos = CASS_INET_V6_LENGTH - i - 1; address[dst_pos] = address[src_pos]; address[src_pos] = 0; } } domain = IPV6; } /* When there are no compressed zeroes, then the address should be * at the required length already. */ else if (!domain) { /* Check if there are enough bytes. */ if (pos + 1 < CASS_INET_V6_LENGTH) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Address \"%s\" contains only %d bytes (%d bytes are expected)", in, pos + 1, CASS_INET_V6_LENGTH); return 0; } /* Check if the number of bytes does not exceed the maximum. */ if (pos + 1 > CASS_INET_V6_LENGTH) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Address \"%s\" exceeds the maximum IPv6 byte length " \ "(%d bytes are expected)\n", in, CASS_INET_V6_LENGTH); return 0; } domain = IPV6; } if (domain == IPV6) *inet = cass_inet_init_v6(address); else *inet = cass_inet_init_v4(address); return 1; } void php_driver_format_address(CassInet inet, char **out) { if (inet.address_length > 4) spprintf(out, 0, "%x:%x:%x:%x:%x:%x:%x:%x", (inet.address[0] * 256 + inet.address[1]), (inet.address[2] * 256 + inet.address[3]), (inet.address[4] * 256 + inet.address[5]), (inet.address[6] * 256 + inet.address[7]), (inet.address[8] * 256 + inet.address[9]), (inet.address[10] * 256 + inet.address[11]), (inet.address[12] * 256 + inet.address[13]), (inet.address[14] * 256 + inet.address[15]) ); else spprintf(out, 0, "%d.%d.%d.%d", inet.address[0], inet.address[1], inet.address[2], inet.address[3] ); } cassandra-1.3.0/util/inet.h0000644000076500000240000000147613062515665016237 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UTIL_INET_H #define PHP_DRIVER_UTIL_INET_H void php_driver_format_address(CassInet inet, char **out); int php_driver_parse_ip_address(char *in, CassInet *inet TSRMLS_DC); #endif /* PHP_DRIVER_UTIL_INET_H */ cassandra-1.3.0/util/math.c0000644000076500000240000004105713062515665016223 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include #include #include #include #include "util/math.h" #ifdef _WIN32 # if defined(DISABLE_MSVC_STDINT) || _MSC_VER <= 1700 # define strtoull _strtoui64 float strtof(const char *str, char **endptr) { return (float) strtod(str, endptr); } # endif #endif extern zend_class_entry *php_driver_invalid_argument_exception_ce; static int prepare_string_conversion(char *in, int *pos, int *negative) { int base = 0; int point = 0; /* Advance the pointer; ignore sign */ if (in[point] == '+') { point++; } else if (in[point] == '-') { point++; if (negative) { *negative = 1; } } /* Handle special case for binary e.g. "0b0100" */ if (in[point] == '0' && in[point + 1] == 'b') { base = 2; point += 2; /* Skip over "0b" */ } /* Assign the position */ if (pos) { *pos = point; } return base; } int php_driver_parse_float(char *in, int in_len, cass_float_t *number TSRMLS_DC) { char *end; errno = 0; *number = (cass_float_t) strtof(in, &end); if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small or too big for float: '%s'", in); return 0; } if (errno || end == in) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid float value: '%s'", in); return 0; } if (end != &in[in_len]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid characters were found in value: '%s'", in); return 0; } return 1; } int php_driver_parse_double(char* in, int in_len, cass_double_t* number TSRMLS_DC) { char* end; errno = 0; *number = (cass_double_t) strtod(in, &end); if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "Value is too small or too big for double: '%s'", in); return 0; } if (errno || end == in) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid double value: '%s'", in); return 0; } if (end != &in[in_len]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid characters were found in value: '%s'", in); return 0; } return 1; } int php_driver_parse_int(char* in, int in_len, cass_int32_t* number TSRMLS_DC) { char* end = NULL; int pos = 0; int negative = 0; cass_uint32_t temp = 0; int base = 0; base = prepare_string_conversion(in, &pos, &negative); errno = 0; temp = (cass_uint32_t) strtoul(in + pos, &end, base); if (negative) { if (temp > (cass_uint32_t) INT_MAX + 1) { errno = ERANGE; *number = INT_MIN; } else if (temp == (cass_uint32_t) INT_MAX + 1) { *number = INT_MIN; } else { *number = -((cass_int32_t) temp); } } else { if (temp > (cass_uint32_t) INT_MAX) { errno = ERANGE; *number = INT_MAX; } else { *number = temp; } } if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between %d and %d, %s given", INT_MIN, INT_MAX, in); return 0; } if (errno || end == &in[pos]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid integer value: '%s'", in); return 0; } if (end != &in[in_len]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid characters were found in value: '%s'", in); return 0; } return 1; } int php_driver_parse_bigint(char *in, int in_len, cass_int64_t *number TSRMLS_DC) { char* end = NULL; int pos = 0; int negative = 0; cass_uint64_t temp = 0; int base = 0; base = prepare_string_conversion(in, &pos, &negative); errno = 0; temp = (cass_uint64_t) strtoull(in + pos, &end, base); if (negative) { if (temp > (cass_uint64_t) INT64_MAX + 1) { errno = ERANGE; *number = INT64_MIN; } else if (temp == (cass_uint64_t) INT64_MAX + 1) { *number = INT64_MIN; } else { *number = -((cass_int64_t) temp); } } else { if (temp > (cass_uint64_t) INT64_MAX) { errno = ERANGE; *number = INT64_MAX; } else { *number = temp; } } if (errno == ERANGE) { zend_throw_exception_ex(php_driver_range_exception_ce, 0 TSRMLS_CC, "value must be between " LL_FORMAT " and " LL_FORMAT ", %s given", INT64_MIN, INT64_MAX, in); return 0; } if (errno || end == &in[pos]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid integer value: '%s'", in); return 0; } if (end != &in[in_len]) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid characters were found in value: '%s'", in); return 0; } return 1; } int php_driver_parse_varint(char *in, int in_len, mpz_t *number TSRMLS_DC) { int pos = 0; int negative = 0; int base = 0; base = prepare_string_conversion(in, &pos, &negative); if (mpz_set_str(*number, &in[pos], base) == -1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid integer value: '%s'", in); return 0; } if (negative) mpz_neg(*number, *number); return 1; } int php_driver_parse_decimal(char *in, int in_len, mpz_t *number, long *scale TSRMLS_DC) { /* start is the index into the char array where the significand starts */ int start = 0; /* * point is the index into the char array where the exponent starts * (or, if there is no exponent, this is equal to end) */ int point = 0; /* * dot is the index into the char array where the decimal point is * found, or -1 if there is no decimal point */ int dot = -1; /* * out will be storing the string representation of the integer part * of the decimal value */ char* out = (char*) ecalloc((in_len + 1), sizeof(char)); /* holds length of the formatted integer number */ int out_len = 0; int maybe_octal = 0; /* * The following examples show what these variables mean. Note that * point and dot don't yet have the correct values, they will be * properly assigned in a loop later on in this method. * * Example 1 * * + 1 0 2 . 4 6 9 * __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ * * offset = 2, in_len = 8, start = 3, dot = 6, point = end = 10 * * Example 2 * * + 2 3 4 . 6 1 3 E - 1 * __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ * * offset = 2, in_len = 11, start = 3, dot = 6, point = 10, end = 13 * * Example 3 * * - 1 2 3 4 5 e 7 * __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ * * offset = 2, in_len = 8, start = 3, dot = -1, point = 8, end = 10 */ /* Determine the sign of the number. */ int negative = 0; if (in[start] == '+') { start++; point++; } else if (in[start] == '-') { start++; point++; negative = 1; } maybe_octal = (in[point] == '0'); /* Hex or binary */ if (maybe_octal && (in[point + 1] == 'b' || in[point + 1] == 'x')) { *scale = 0; return php_driver_parse_varint(in, in_len, number TSRMLS_CC); } /* * Check each character looking for the decimal point and the * start of the exponent. */ while (point < in_len) { char c = in[point]; if (c == '.') { /* If dot != -1 then we've seen more than one decimal point. */ if (dot != -1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Multiple '.' (dots) in the number '%s'", in); return 0; } dot = point; } /* Break when we reach the start of the exponent. */ else if (c == 'e' || c == 'E') break; /* * Throw an exception if the character was not a decimal or an * exponent and is not a hexadecimal digit. */ else if (!isxdigit(c)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Unrecognized character '%c' at position %d", c, point); return 0; } point++; } /* Octal number */ if (maybe_octal && dot == -1) { *scale = 0; return php_driver_parse_varint(in, in_len, number TSRMLS_CC); } /* Prepend a negative sign if necessary. */ if (negative) out[0] = '-'; if (dot != -1) { /* * If there was a decimal we must combine the two parts that * contain only digits and we must set the scale properly. */ memcpy(&out[negative], &in[start], dot - start); memcpy(&out[negative + dot - start], &in[dot + 1], point - dot); out_len = point - start + negative - 1; *scale = point - 1 - dot; } else { /* * If there was no decimal then the unscaled value is just the number * formed from all the digits and the scale is zero. */ memcpy(&out[negative], &in[start], point - start); out_len = point - start + negative; *scale = 0; } if (out_len == 0) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "No digits seen in value: '%s'", in); return 0; } if (mpz_set_str(*number, out, 10) == -1) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Unable to extract integer part of decimal value: '%s', %s", in, out); efree(out); return 0; } efree(out); /* * Now parse exponent. * If point < end that means we broke out of the previous loop when we * saw an 'e' or an 'E'. */ if (point < in_len) { int diff; point++; /* Ignore a '+' sign. */ if (in[point] == '+') point++; /* * Throw an exception if there were no digits found after the 'e' * or 'E'. */ if (point >= in_len) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "No exponent following e or E in value: '%s'", in); return 0; } if (!sscanf(&in[point], "%d", &diff)) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Malformed exponent in value: '%s'", in); return 0; } *scale = *scale - diff; } return 1; } void php_driver_format_integer(mpz_t number, char **out, int *out_len) { /* Adding 2 ensures enough space for the null-terminator and negative sign */ *out = (char*) emalloc(mpz_sizeinbase(number, 10) + 2); mpz_get_str(*out, 10, number); *out_len = strlen(*out); } void php_driver_format_decimal(mpz_t number, long scale, char **out, int *out_len) { char *tmp = NULL; size_t total = 0; size_t len = mpz_sizeinbase(number, 10); int negative = 0; int point = -1; if (scale == 0) { php_driver_format_integer(number, out, out_len); return; } if (mpz_sgn(number) < 0) negative = 1; // Ultimately, we want to return a string representation of this decimal. So allocate // a buffer that could hold this decimal in the worst possible conservative case. // absolute length + negative sign + point sign + scale (in case we end up with a number with leading 0s) + // exponent modifier and sign. tmp = (char*) emalloc(len + negative + 1 + scale + 2); mpz_get_str(tmp, 10, number); // Update len to be the true length of the string representation of |number|. mpz_sizeinbase // can return a higher result than the actual length. // NOTE: the length of the string includes the negative sign (if present); account for that. len = strlen(tmp) - negative; point = len - scale; // We only support numbers with scale >= 0. assert(scale >= 0); if ((point - 1) >= -6) { if (point <= 0) { // e.g. -0.002 and 0.002 int shift_start = negative; // current position int i = 0; // Move the numeric part (skip leading minus if needed) of tmp right by enough bytes to make room for // 0.0000 (as many leading zeroes as necessary). memmove(&(tmp[shift_start + 2 - point]), &(tmp[shift_start]), len); // This is a (possibly negative) number with a 0 integer part. if (negative) tmp[i++] = '-'; tmp[i++] = '0'; tmp[i++] = '.'; // Add leading zeroes. while (point < 0) { tmp[i++] = '0'; point++; } total = i + len; tmp[total] = '\0'; } else { // e.g. 1.2, -1.2 /* absolute length + negative sign + point sign */ total = len + negative + 1; // Insert the decimal point at the right location in the string. // point is the index at which to insert the decimal point, but it assumes we have a positive // number. Move it to the right if we have a negative number. point += negative; memmove(&(tmp[point + 1]), &(tmp[point]), total - point); tmp[point] = '.'; tmp[total] = '\0'; } } else { // Very small positive or negative number that we want to express in scientific notation: // 0.000000004, -0.000000004 int exponent = -1; int exponent_size = -1; // Calculate the exponent value and its size. exponent = point - 1; exponent_size = (int) ceil(log10(abs(exponent) + 2)) + 1; // If we only have one significant digit, we want to produce a string like // 1E-9. If we have more significant digits, then 1.123E-9. if (len == 1) { // Simple case; tmp is already leading with our number as we want it. Append E(exp) to it // and we're done. sprintf(&(tmp[1 + negative]), "E%+d", exponent); total = 1 + negative + 1 + exponent_size; } else { // We have a more complex number. Insert a decimal point after the first digit. point = negative ? 2 : 1; memmove(&(tmp[point + 1]), &(tmp[point]), len-1); tmp[point] = '.'; // Now append the exponent to the end and we're done. sprintf(&(tmp[point + len]), "E%+d", exponent); total = point + len + 1 + exponent_size; } } *out = tmp; *out_len = total; } void import_twos_complement(cass_byte_t *data, size_t size, mpz_t *number) { mpz_import(*number, size, 1, sizeof(cass_byte_t), 1, 0, data); /* negative value */ if ((data[0] & 0x80) == 0x80) { /* mpz_import() imports the two's complement value as an unsigned integer * so this needs to subtract 2^(8 * num_bytes) to get the negative value. */ mpz_t temp; mpz_init(temp); mpz_set_ui(temp, 1); mpz_mul_2exp(temp, temp, 8 * size); mpz_sub(*number, *number, temp); mpz_clear(temp); } } cass_byte_t* export_twos_complement(mpz_t number, size_t *size) { cass_byte_t *bytes; if (mpz_sgn(number) == 0) { /* mpz_export() returns NULL for 0 */ bytes = (cass_byte_t*) malloc(sizeof(cass_byte_t)); *bytes = 0; *size = 1; } else if (mpz_sgn(number) == -1) { /* mpz_export() ignores sign and only exports abs(number) * so this needs to convert the number to the two's complement * unsigned value. */ size_t n; mpz_t temp; /* determine the number of bytes used in the two's complement * respresentation. */ n = mpz_sizeinbase(number, 2) / 8 + 1; /* there's a special case for -2^(8 * n) numbers e.g. -128 (1000 0000) and * -32768 (100 0000 0000 0000), etc. that can be handled by n - 1 bytes in * two's complement. */ if (mpz_scan1(number, 0) == (8 * (n - 1)) - 1) { n--; } /* Add 2^(8 * num_bytes) to get the unsigned value e.g. * -1 + 2^8 = 255 * -128 + 2^8 = 128 * -129 + 2^16 = 65407 * -32768 + 2^16 = 32768 * ... */ mpz_init(temp); mpz_set_ui(temp, 1); mpz_mul_2exp(temp, temp, 8 * n); mpz_add(temp, number, temp); bytes = (cass_byte_t*) mpz_export(NULL, size, 1, sizeof(cass_byte_t), 1, 0, temp); mpz_clear(temp); } else { /* mpz_export() always returns a unsigned number and can have * values where the most significate bit is set. A 0 byte prevents * these from being interpreted as a negative value in two's complement */ /* round to the nearest byte and add space for a leading 0 byte */ *size = (mpz_sizeinbase(number, 2) + 7) / 8 + 1; bytes = malloc(*size); bytes[0] = 0; mpz_export(bytes + 1, NULL, 1, sizeof(cass_byte_t), 1, 0, number); } return bytes; } cassandra-1.3.0/util/math.h0000644000076500000240000000271013062515665016221 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_MATH_H #define PHP_DRIVER_MATH_H void import_twos_complement(cass_byte_t *data, size_t size, mpz_t *number); cass_byte_t *export_twos_complement(mpz_t number, size_t *size); int php_driver_parse_float(char *in, int in_len, cass_float_t *number TSRMLS_DC); int php_driver_parse_double(char* in, int in_len, cass_double_t* number TSRMLS_DC); int php_driver_parse_int(char* in, int in_len, cass_int32_t* number TSRMLS_DC); int php_driver_parse_bigint(char *in, int in_len, cass_int64_t *number TSRMLS_DC); int php_driver_parse_varint(char *in, int in_len, mpz_t *number TSRMLS_DC); int php_driver_parse_decimal(char *in, int in_len, mpz_t *number, long *scale TSRMLS_DC); void php_driver_format_integer(mpz_t number, char **out, int *out_len); void php_driver_format_decimal(mpz_t number, long scale, char **out, int *out_len); #endif /* PHP_DRIVER_MATH_H */ cassandra-1.3.0/util/ref.c0000644000076500000240000000234213062515665016040 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "util/math.h" php_driver_ref* php_driver_new_peref(void *data, php_driver_free_function destructor, int persistent) { php_driver_ref *ref = (php_driver_ref*) pemalloc(sizeof(php_driver_ref), persistent); ref->data = data; ref->destruct = destructor; ref->count = 1; return ref; } void php_driver_del_peref(php_driver_ref **ref_ptr, int persistent) { php_driver_ref *ref = *ref_ptr; if (ref) { ref->count--; if (ref->count <= 0) { ref->destruct(ref->data); ref->data = NULL; pefree(ref, persistent); *ref_ptr = NULL; } } } cassandra-1.3.0/util/ref.h0000644000076500000240000000204013062515665016040 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_REF_H #define PHP_DRIVER_REF_H php_driver_ref *php_driver_new_peref(void *data, php_driver_free_function destructor, int persistent); void php_driver_del_peref(php_driver_ref **ref_ptr, int persistent); #define php_driver_new_ref(data, destructor) php_driver_new_peref(data, destructor, 0) #define php_driver_del_ref(ref) php_driver_del_peref(ref, 0) #define php_driver_add_ref(ref) (++ref->count, ref) #endif /* PHP_DRIVER_REF_H */ cassandra-1.3.0/util/result.c0000644000076500000240000003744013062515665016611 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_types.h" #include "result.h" #include "math.h" #include "collections.h" #include "types.h" #include "src/Collection.h" #include "src/Map.h" #include "src/Set.h" #include "src/Tuple.h" #include "src/UserTypeValue.h" int php_driver_value(const CassValue* value, const CassDataType* data_type, php5to7_zval *out TSRMLS_DC) { const char *v_string; size_t v_string_len; const cass_byte_t *v_bytes; size_t v_bytes_len; const cass_byte_t *v_decimal; size_t v_decimal_len; cass_int32_t v_decimal_scale; cass_int32_t v_int_32; cass_bool_t v_boolean; cass_double_t v_double; php_driver_uuid *uuid; CassIterator *iterator; php_driver_numeric *numeric = NULL; php_driver_timestamp *timestamp = NULL; php_driver_date *date = NULL; php_driver_time *time = NULL; php_driver_blob *blob = NULL; php_driver_inet *inet = NULL; php_driver_duration *duration = NULL; php_driver_collection *collection = NULL; php_driver_map *map = NULL; php_driver_set *set = NULL; php_driver_tuple *tuple = NULL; php_driver_user_type_value *user_type_value = NULL; ulong index; CassValueType type = cass_data_type_type(data_type); const CassDataType* primary_type; const CassDataType* secondary_type; PHP5TO7_ZVAL_MAYBE_MAKE(*out); if (cass_value_is_null(value)) { ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_DEREF(out)); return SUCCESS; } switch (type) { case CASS_VALUE_TYPE_ASCII: case CASS_VALUE_TYPE_TEXT: case CASS_VALUE_TYPE_VARCHAR: ASSERT_SUCCESS_BLOCK(cass_value_get_string(value, &v_string, &v_string_len), zval_ptr_dtor(out); return FAILURE; ); PHP5TO7_ZVAL_STRINGL(PHP5TO7_ZVAL_MAYBE_DEREF(out), v_string, v_string_len); break; case CASS_VALUE_TYPE_INT: ASSERT_SUCCESS_BLOCK(cass_value_get_int32(value, &v_int_32), zval_ptr_dtor(out); return FAILURE; ); ZVAL_LONG(PHP5TO7_ZVAL_MAYBE_DEREF(out), v_int_32); break; case CASS_VALUE_TYPE_COUNTER: case CASS_VALUE_TYPE_BIGINT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_bigint_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_int64(value, &numeric->data.bigint.value), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_SMALL_INT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_smallint_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_int16(value, &numeric->data.smallint.value), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_TINY_INT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_tinyint_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_int8(value, &numeric->data.tinyint.value), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_TIMESTAMP: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_timestamp_ce); timestamp = PHP_DRIVER_GET_TIMESTAMP(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_int64(value, ×tamp->timestamp), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_DATE: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_date_ce); date = PHP_DRIVER_GET_DATE(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_uint32(value, &date->date), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_TIME: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_time_ce); time = PHP_DRIVER_GET_TIME(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_int64(value, &time->time), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_BLOB: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_blob_ce); blob = PHP_DRIVER_GET_BLOB(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_bytes(value, &v_bytes, &v_bytes_len), zval_ptr_dtor(out); return FAILURE; ) blob->data = emalloc(v_bytes_len * sizeof(cass_byte_t)); blob->size = v_bytes_len; memcpy(blob->data, v_bytes, v_bytes_len); break; case CASS_VALUE_TYPE_VARINT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_varint_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_bytes(value, &v_bytes, &v_bytes_len), zval_ptr_dtor(out); return FAILURE; ); import_twos_complement((cass_byte_t*) v_bytes, v_bytes_len, &numeric->data.varint.value); break; case CASS_VALUE_TYPE_UUID: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_uuid_ce); uuid = PHP_DRIVER_GET_UUID(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_uuid(value, &uuid->uuid), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_TIMEUUID: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_timeuuid_ce); uuid = PHP_DRIVER_GET_UUID(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_uuid(value, &uuid->uuid), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_BOOLEAN: ASSERT_SUCCESS_BLOCK(cass_value_get_bool(value, &v_boolean), zval_ptr_dtor(out); return FAILURE; ); if (v_boolean) { ZVAL_TRUE(PHP5TO7_ZVAL_MAYBE_DEREF(out)); } else { ZVAL_FALSE(PHP5TO7_ZVAL_MAYBE_DEREF(out)); } break; case CASS_VALUE_TYPE_INET: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_inet_ce); inet = PHP_DRIVER_GET_INET(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_inet(value, &inet->inet), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_DECIMAL: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_decimal_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_decimal(value, &v_decimal, &v_decimal_len, &v_decimal_scale), zval_ptr_dtor(out); return FAILURE; ); import_twos_complement((cass_byte_t*) v_decimal, v_decimal_len, &numeric->data.decimal.value); numeric->data.decimal.scale = v_decimal_scale; break; case CASS_VALUE_TYPE_DURATION: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_duration_ce); duration = PHP_DRIVER_GET_DURATION(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_duration(value, &duration->months, &duration->days, &duration->nanos), zval_ptr_dtor(out); return FAILURE; ); break; case CASS_VALUE_TYPE_DOUBLE: ASSERT_SUCCESS_BLOCK(cass_value_get_double(value, &v_double), zval_ptr_dtor(out); return FAILURE; ); ZVAL_DOUBLE(PHP5TO7_ZVAL_MAYBE_DEREF(out), v_double); break; case CASS_VALUE_TYPE_FLOAT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_float_ce); numeric = PHP_DRIVER_GET_NUMERIC(PHP5TO7_ZVAL_MAYBE_DEREF(out)); ASSERT_SUCCESS_BLOCK(cass_value_get_float(value, &numeric->data.floating.value), zval_ptr_dtor(out); return FAILURE; ) break; case CASS_VALUE_TYPE_LIST: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_collection_ce); collection = PHP_DRIVER_GET_COLLECTION(PHP5TO7_ZVAL_MAYBE_DEREF(out)); primary_type = cass_data_type_sub_data_type(data_type, 0); collection->type = php_driver_type_from_data_type(data_type TSRMLS_CC); iterator = cass_iterator_from_collection(value); while (cass_iterator_next(iterator)) { php5to7_zval v; if (php_driver_value(cass_iterator_get_value(iterator), primary_type, &v TSRMLS_CC) == FAILURE) { cass_iterator_free(iterator); zval_ptr_dtor(out); return FAILURE; } php_driver_collection_add(collection, PHP5TO7_ZVAL_MAYBE_P(v) TSRMLS_CC); zval_ptr_dtor(&v); } cass_iterator_free(iterator); break; case CASS_VALUE_TYPE_MAP: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_map_ce); map = PHP_DRIVER_GET_MAP(PHP5TO7_ZVAL_MAYBE_DEREF(out)); primary_type = cass_data_type_sub_data_type(data_type, 0); secondary_type = cass_data_type_sub_data_type(data_type, 1); map->type = php_driver_type_from_data_type(data_type TSRMLS_CC); iterator = cass_iterator_from_map(value); while (cass_iterator_next(iterator)) { php5to7_zval k; php5to7_zval v; if (php_driver_value(cass_iterator_get_map_key(iterator), primary_type, &k TSRMLS_CC) == FAILURE || php_driver_value(cass_iterator_get_map_value(iterator), secondary_type, &v TSRMLS_CC) == FAILURE) { cass_iterator_free(iterator); zval_ptr_dtor(out); return FAILURE; } php_driver_map_set(map, PHP5TO7_ZVAL_MAYBE_P(k), PHP5TO7_ZVAL_MAYBE_P(v) TSRMLS_CC); zval_ptr_dtor(&k); zval_ptr_dtor(&v); } cass_iterator_free(iterator); break; case CASS_VALUE_TYPE_SET: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_set_ce); set = PHP_DRIVER_GET_SET(PHP5TO7_ZVAL_MAYBE_DEREF(out)); primary_type = cass_data_type_sub_data_type(data_type, 0); set->type = php_driver_type_from_data_type(data_type TSRMLS_CC); iterator = cass_iterator_from_collection(value); while (cass_iterator_next(iterator)) { php5to7_zval v; if (php_driver_value(cass_iterator_get_value(iterator), primary_type, &v TSRMLS_CC) == FAILURE) { cass_iterator_free(iterator); zval_ptr_dtor(out); return FAILURE; } php_driver_set_add(set, PHP5TO7_ZVAL_MAYBE_P(v) TSRMLS_CC); zval_ptr_dtor(&v); } cass_iterator_free(iterator); break; case CASS_VALUE_TYPE_TUPLE: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_tuple_ce); tuple = PHP_DRIVER_GET_TUPLE(PHP5TO7_ZVAL_MAYBE_DEREF(out)); tuple->type = php_driver_type_from_data_type(data_type TSRMLS_CC); iterator = cass_iterator_from_tuple(value); index = 0; while (cass_iterator_next(iterator)) { const CassValue* value = cass_iterator_get_value(iterator); if (!cass_value_is_null(value)) { php5to7_zval v; primary_type = cass_data_type_sub_data_type(data_type, index); if (php_driver_value(value, primary_type, &v TSRMLS_CC) == FAILURE) { cass_iterator_free(iterator); zval_ptr_dtor(out); return FAILURE; } php_driver_tuple_set(tuple, index, PHP5TO7_ZVAL_MAYBE_P(v) TSRMLS_CC); zval_ptr_dtor(&v); } index++; } cass_iterator_free(iterator); break; case CASS_VALUE_TYPE_UDT: object_init_ex(PHP5TO7_ZVAL_MAYBE_DEREF(out), php_driver_user_type_value_ce); user_type_value = PHP_DRIVER_GET_USER_TYPE_VALUE(PHP5TO7_ZVAL_MAYBE_DEREF(out)); user_type_value->type = php_driver_type_from_data_type(data_type TSRMLS_CC); iterator = cass_iterator_fields_from_user_type(value); index = 0; while (cass_iterator_next(iterator)) { const CassValue* value = cass_iterator_get_user_type_field_value(iterator); if (!cass_value_is_null(value)) { const char *name; size_t name_length; php5to7_zval v; primary_type = cass_data_type_sub_data_type(data_type, index); if (php_driver_value(value, primary_type, &v TSRMLS_CC) == FAILURE) { cass_iterator_free(iterator); zval_ptr_dtor(out); return FAILURE; } cass_iterator_get_user_type_field_name(iterator, &name, &name_length); php_driver_user_type_value_set(user_type_value, name, name_length, PHP5TO7_ZVAL_MAYBE_P(v) TSRMLS_CC); zval_ptr_dtor(&v); } index++; } cass_iterator_free(iterator); break; default: ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_DEREF(out)); break; } return SUCCESS; } int php_driver_get_keyspace_field(const CassKeyspaceMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC) { const CassValue *value; value = cass_keyspace_meta_field_by_name(metadata, field_name); if (value == NULL || cass_value_is_null(value)) { PHP5TO7_ZVAL_MAYBE_MAKE(*out); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_DEREF(out)); return SUCCESS; } return php_driver_value(value, cass_value_data_type(value), out TSRMLS_CC); } int php_driver_get_table_field(const CassTableMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC) { const CassValue *value; value = cass_table_meta_field_by_name(metadata, field_name); if (value == NULL || cass_value_is_null(value)) { PHP5TO7_ZVAL_MAYBE_MAKE(*out); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_DEREF(out)); return SUCCESS; } return php_driver_value(value, cass_value_data_type(value), out TSRMLS_CC); } int php_driver_get_column_field(const CassColumnMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC) { const CassValue *value; value = cass_column_meta_field_by_name(metadata, field_name); if (value == NULL || cass_value_is_null(value)) { PHP5TO7_ZVAL_MAYBE_MAKE(*out); ZVAL_NULL(PHP5TO7_ZVAL_MAYBE_DEREF(out)); return SUCCESS; } return php_driver_value(value, cass_value_data_type(value), out TSRMLS_CC); } int php_driver_get_result(const CassResult *result, php5to7_zval *out TSRMLS_DC) { php5to7_zval rows; php5to7_zval row; const CassRow *cass_row; const char *column_name; size_t column_name_len; const CassDataType* column_type; const CassValue *column_value; CassIterator *iterator = NULL; size_t columns = -1; char **column_names; unsigned i; PHP5TO7_ZVAL_MAYBE_MAKE(rows); array_init(PHP5TO7_ZVAL_MAYBE_P(rows)); iterator = cass_iterator_from_result(result); columns = cass_result_column_count(result); column_names = (char**) ecalloc(columns, sizeof(char*)); while (cass_iterator_next(iterator)) { PHP5TO7_ZVAL_MAYBE_MAKE(row); array_init(PHP5TO7_ZVAL_MAYBE_P(row)); cass_row = cass_iterator_get_row(iterator); for (i = 0; i < columns; i++) { php5to7_zval value; if (column_names[i] == NULL) { cass_result_column_name(result, i, &column_name, &column_name_len); column_names[i] = estrndup(column_name, column_name_len); } column_type = cass_result_column_data_type(result, i); column_value = cass_row_get_column(cass_row, i); if (php_driver_value(column_value, column_type, &value TSRMLS_CC) == FAILURE) { zval_ptr_dtor(&row); zval_ptr_dtor(&rows); for (i = 0; i < columns; i++) { if (column_names[i]) { efree(column_names[i]); } } efree(column_names); cass_iterator_free(iterator); return FAILURE; } PHP5TO7_ADD_ASSOC_ZVAL_EX(PHP5TO7_ZVAL_MAYBE_P(row), column_names[i], strlen(column_names[i]) + 1, PHP5TO7_ZVAL_MAYBE_P(value)); } add_next_index_zval(PHP5TO7_ZVAL_MAYBE_P(rows), PHP5TO7_ZVAL_MAYBE_P(row)); } for (i = 0; i < columns; i++) { if (column_names[i] != NULL) { efree(column_names[i]); } } efree(column_names); cass_iterator_free(iterator); *out = rows; return SUCCESS; } cassandra-1.3.0/util/result.h0000644000076500000240000000232713062515665016612 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_RESULT_H #define PHP_DRIVER_RESULT_H int php_driver_value(const CassValue* value, const CassDataType* data_type, php5to7_zval *out TSRMLS_DC); int php_driver_get_keyspace_field(const CassKeyspaceMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC); int php_driver_get_table_field(const CassTableMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC); int php_driver_get_column_field(const CassColumnMeta *metadata, const char *field_name, php5to7_zval *out TSRMLS_DC); int php_driver_get_result(const CassResult *result, php5to7_zval *out TSRMLS_DC); #endif /* PHP_DRIVER_RESULT_H */ cassandra-1.3.0/util/types.c0000644000076500000240000011456413062515665016442 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include "php_driver_types.h" #include "util/types.h" #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif #include "src/Bigint.h" #include "src/Smallint.h" #include "src/Tinyint.h" #include "src/Blob.h" #include "src/Decimal.h" #include "src/Duration.h" #include "src/Float.h" #include "src/Inet.h" #include "src/Timestamp.h" #include "src/Date.h" #include "src/Time.h" #include "src/Timeuuid.h" #include "src/Uuid.h" #include "src/Varint.h" #include "src/Type/Tuple.h" #include "src/Type/UserType.h" struct node_s { struct node_s *parent; const char *name; size_t name_length; struct node_s *first_child; struct node_s *last_child; struct node_s *next_sibling; struct node_s *prev_sibling; }; static int hex_value(int c) { if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } else if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } return -1; } static char* php_driver_from_hex(const char* hex, size_t hex_length) { size_t i, c = 0; size_t size = hex_length / 2; char *result; if ((hex_length & 1) == 1) { /* Invalid if not divisible by 2 */ return NULL; } result = emalloc(size + 1); for (i = 0; i < size; ++i) { int half0 = hex_value(hex[i * 2]); int half1 = hex_value(hex[i * 2 + 1]); if (half0 < 0 || half1 < 0) { efree(result); return NULL; } result[c++] = (char)(((uint8_t)half0 << 4) | (uint8_t)half1); } result[size] = '\0'; return result; } static php5to7_zval php_driver_create_type(struct node_s *node TSRMLS_DC); static php5to7_zval php_driver_tuple_from_data_type(const CassDataType *data_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *type; size_t i, count; count = cass_data_sub_type_count(data_type); ztype = php_driver_type_tuple(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); for (i = 0; i < count; ++i) { php5to7_zval sub_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, i) TSRMLS_CC); php_driver_type_tuple_add(type, PHP5TO7_ZVAL_MAYBE_P(sub_type) TSRMLS_CC); } return ztype; } static php5to7_zval php_driver_tuple_from_node(struct node_s *node TSRMLS_DC) { php5to7_zval ztype; php_driver_type *type; struct node_s *current; ztype = php_driver_type_tuple(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); for (current = node->first_child; current != NULL; current = current->next_sibling) { php5to7_zval sub_type = php_driver_create_type(current TSRMLS_CC); php_driver_type_tuple_add(type, PHP5TO7_ZVAL_MAYBE_P(sub_type) TSRMLS_CC); } return ztype; } static php5to7_zval php_driver_user_type_from_data_type(const CassDataType *data_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *type; const char *type_name, *keyspace; size_t type_name_len, keyspace_len; size_t i, count; count = cass_data_sub_type_count(data_type); ztype = php_driver_type_user_type(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); cass_data_type_type_name(data_type, &type_name, &type_name_len); type->data.udt.type_name = estrndup(type_name, type_name_len); cass_data_type_keyspace(data_type, &keyspace, &keyspace_len); type->data.udt.keyspace = estrndup(keyspace, keyspace_len); for (i = 0; i < count; ++i) { const char *name; size_t name_length; php5to7_zval sub_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, i) TSRMLS_CC); cass_data_type_sub_type_name(data_type, i, &name, &name_length); php_driver_type_user_type_add(type, name, name_length, PHP5TO7_ZVAL_MAYBE_P(sub_type) TSRMLS_CC); } return ztype; } static php5to7_zval php_driver_user_type_from_node(struct node_s *node TSRMLS_DC) { php5to7_zval ztype; php_driver_type *type; struct node_s *current = node->first_child; ztype = php_driver_type_user_type(TSRMLS_C); type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); if (current) { type->data.udt.keyspace = estrndup(current->name, current->name_length); current = current->next_sibling; } if (current) { type->data.udt.type_name = php_driver_from_hex(current->name, current->name_length); current = current->next_sibling; } for (; current; current = current->next_sibling) { php5to7_zval sub_type; char *name = php_driver_from_hex(current->name, current->name_length); current = current->next_sibling; if (!current) { efree(name); break; } sub_type = php_driver_create_type(current TSRMLS_CC); php_driver_type_user_type_add(type, name, strlen(name), PHP5TO7_ZVAL_MAYBE_P(sub_type) TSRMLS_CC); efree(name); } return ztype; } php5to7_zval php_driver_type_from_data_type(const CassDataType *data_type TSRMLS_DC) { php5to7_zval ztype; php5to7_zval key_type; php5to7_zval value_type; const char *class_name; size_t class_name_length; CassValueType type = cass_data_type_type(data_type); PHP5TO7_ZVAL_UNDEF(ztype); switch (type) { #define XX_SCALAR(name, value) \ case value: \ ztype = php_driver_type_scalar(value TSRMLS_CC); \ break; PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR case CASS_VALUE_TYPE_CUSTOM: cass_data_type_class_name(data_type, &class_name, &class_name_length); ztype = php_driver_type_custom(class_name, class_name_length TSRMLS_CC); break; case CASS_VALUE_TYPE_LIST: value_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, 0) TSRMLS_CC); ztype = php_driver_type_collection(PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); break; case CASS_VALUE_TYPE_MAP: key_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, 0) TSRMLS_CC); value_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, 1) TSRMLS_CC); ztype = php_driver_type_map(PHP5TO7_ZVAL_MAYBE_P(key_type), PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); break; case CASS_VALUE_TYPE_SET: value_type = php_driver_type_from_data_type( cass_data_type_sub_data_type(data_type, 0) TSRMLS_CC); ztype = php_driver_type_set(PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); break; case CASS_VALUE_TYPE_TUPLE: ztype = php_driver_tuple_from_data_type(data_type TSRMLS_CC); break; case CASS_VALUE_TYPE_UDT: ztype = php_driver_user_type_from_data_type(data_type TSRMLS_CC); break; default: break; } return ztype; } int php_driver_type_validate(zval *object, const char *object_name TSRMLS_DC) { if (!instanceof_function(Z_OBJCE_P(object), php_driver_type_scalar_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(object), php_driver_type_collection_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(object), php_driver_type_map_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(object), php_driver_type_set_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(object), php_driver_type_tuple_ce TSRMLS_CC) && !instanceof_function(Z_OBJCE_P(object), php_driver_type_user_type_ce TSRMLS_CC)) { throw_invalid_argument(object, object_name, "a valid " PHP_DRIVER_NAMESPACE "\\Type" TSRMLS_CC); return 0; } return 1; } static inline int collection_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { return php_driver_type_compare(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type1->data.collection.value_type)), PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type2->data.collection.value_type)) TSRMLS_CC); } static inline int map_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { int result; result = php_driver_type_compare(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type1->data.map.key_type)), PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type2->data.map.key_type)) TSRMLS_CC); if (result != 0) return result; result = php_driver_type_compare(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type1->data.map.value_type)), PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type2->data.map.value_type)) TSRMLS_CC); if (result != 0) return result; return 0; } static inline int set_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { return php_driver_type_compare(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type1->data.set.value_type)), PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type2->data.set.value_type)) TSRMLS_CC); } static inline int tuple_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { HashPosition pos1; HashPosition pos2; php5to7_zval *current1; php5to7_zval *current2; if (zend_hash_num_elements(&type1->data.tuple.types) != zend_hash_num_elements(&type2->data.tuple.types)) { return zend_hash_num_elements(&type1->data.tuple.types) < zend_hash_num_elements(&type2->data.tuple.types) ? -1 : 1; } zend_hash_internal_pointer_reset_ex(&type1->data.tuple.types, &pos1); zend_hash_internal_pointer_reset_ex(&type2->data.tuple.types, &pos2); while (PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&type1->data.tuple.types, current1, &pos1) && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&type2->data.tuple.types, current2, &pos2)) { php_driver_type *sub_type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current1)); php_driver_type *sub_type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current2)); int result = php_driver_type_compare(sub_type1, sub_type2 TSRMLS_CC); if (result != 0) return result; zend_hash_move_forward_ex(&type1->data.tuple.types, &pos1); zend_hash_move_forward_ex(&type2->data.tuple.types, &pos2); } return 0; } static inline int user_type_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { HashPosition pos1; HashPosition pos2; php5to7_string key1; php5to7_string key2; php5to7_zval *current1; php5to7_zval *current2; if (zend_hash_num_elements(&type1->data.udt.types) != zend_hash_num_elements(&type2->data.udt.types)) { return zend_hash_num_elements(&type1->data.udt.types) < zend_hash_num_elements(&type2->data.udt.types) ? -1 : 1; } zend_hash_internal_pointer_reset_ex(&type1->data.udt.types, &pos1); zend_hash_internal_pointer_reset_ex(&type2->data.udt.types, &pos2); while (PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type1->data.udt.types, &key1, NULL, &pos1) == HASH_KEY_IS_STRING && PHP5TO7_ZEND_HASH_GET_CURRENT_KEY_EX(&type2->data.udt.types, &key2, NULL, &pos2) == HASH_KEY_IS_STRING && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&type1->data.udt.types, current1, &pos1) && PHP5TO7_ZEND_HASH_GET_CURRENT_DATA_EX(&type2->data.udt.types, current2, &pos2)) { int result; php_driver_type *sub_type1 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current1)); php_driver_type *sub_type2 = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current2)); result = php5to7_string_compare(key1, key2); if (result != 0) return result; result = php_driver_type_compare(sub_type1, sub_type2 TSRMLS_CC); if (result != 0) return result; zend_hash_move_forward_ex(&type1->data.udt.types, &pos1); zend_hash_move_forward_ex(&type2->data.udt.types, &pos2); } return 0; } static inline int is_string_type(CassValueType type) { return type == CASS_VALUE_TYPE_VARCHAR || type == CASS_VALUE_TYPE_TEXT; } int php_driver_type_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC) { if (type1->type != type2->type) { if (is_string_type(type1->type) && is_string_type(type2->type)) { /* varchar and text are aliases */ return 0; } return type1->type < type2->type ? -1 : 1; } else { switch (type1->type) { case CASS_VALUE_TYPE_LIST: return collection_compare(type1, type2 TSRMLS_CC); case CASS_VALUE_TYPE_MAP: return map_compare(type1, type2 TSRMLS_CC); case CASS_VALUE_TYPE_SET: return set_compare(type1, type2 TSRMLS_CC); case CASS_VALUE_TYPE_TUPLE: return tuple_compare(type1, type2 TSRMLS_CC); case CASS_VALUE_TYPE_UDT: return user_type_compare(type1, type2 TSRMLS_CC); default: break; } return 0; } } static inline void collection_string(php_driver_type *type, smart_str *string TSRMLS_DC) { smart_str_appendl(string, "list<", 5); php_driver_type_string(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.collection.value_type)), string TSRMLS_CC); smart_str_appendl(string, ">", 1); } static inline void map_string(php_driver_type *type, smart_str *string TSRMLS_DC) { smart_str_appendl(string, "map<", 4); php_driver_type_string(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.map.key_type)), string TSRMLS_CC); smart_str_appendl(string, ", ", 2); php_driver_type_string(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.map.value_type)), string TSRMLS_CC); smart_str_appendl(string, ">", 1); } static inline void set_string(php_driver_type *type, smart_str *string TSRMLS_DC) { smart_str_appendl(string, "set<", 4); php_driver_type_string(PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(type->data.set.value_type)), string TSRMLS_CC); smart_str_appendl(string, ">", 1); } static inline void tuple_string(php_driver_type *type, smart_str *string TSRMLS_DC) { php5to7_zval *current; int first = 1; smart_str_appendl(string, "tuple<", 6); PHP5TO7_ZEND_HASH_FOREACH_VAL(&type->data.tuple.types, current) { php_driver_type *sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current)); if (!first) smart_str_appendl(string, ", ", 2); first = 0; php_driver_type_string(sub_type, string TSRMLS_CC); } PHP5TO7_ZEND_HASH_FOREACH_END(&type->data.tuple.types); smart_str_appendl(string, ">", 1); } static inline void user_type_string(php_driver_type *type, smart_str *string TSRMLS_DC) { char *name; php5to7_zval *current; int first = 1; if (type->data.udt.type_name) { if (type->data.udt.keyspace) { smart_str_appendl(string, type->data.udt.keyspace, strlen(type->data.udt.keyspace)); smart_str_appendl(string, ".", 1); } smart_str_appendl(string, type->data.udt.type_name, strlen(type->data.udt.type_name)); } else { smart_str_appendl(string, "userType<", 9); PHP5TO7_ZEND_HASH_FOREACH_STR_KEY_VAL(&type->data.udt.types, name, current) { php_driver_type *sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_DEREF(current)); if (!first) smart_str_appendl(string, ", ", 2); first = 0; smart_str_appendl(string, name, strlen(name)); smart_str_appendl(string, ":", 1); php_driver_type_string(sub_type, string TSRMLS_CC); } PHP5TO7_ZEND_HASH_FOREACH_END(&type->data.udt.types); smart_str_appendl(string, ">", 1); } } void php_driver_type_string(php_driver_type *type, smart_str *string TSRMLS_DC) { switch (type->type) { #define XX_SCALAR(name, value) \ case value: \ smart_str_appendl(string, #name, strlen(#name)); \ break; PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR case CASS_VALUE_TYPE_LIST: collection_string(type, string TSRMLS_CC); break; case CASS_VALUE_TYPE_MAP: map_string(type, string TSRMLS_CC); break; case CASS_VALUE_TYPE_SET: set_string(type, string TSRMLS_CC); break; case CASS_VALUE_TYPE_TUPLE: tuple_string(type, string TSRMLS_CC); break; case CASS_VALUE_TYPE_UDT: user_type_string(type, string TSRMLS_CC); break; default: smart_str_appendl(string, "invalid", 7); break; } } static php5to7_zval php_driver_type_scalar_new(CassValueType type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *scalar; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_scalar_ce); scalar = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); scalar->type = type; scalar->data_type = cass_data_type_new(type); return ztype; } const char * php_driver_scalar_type_name(CassValueType type TSRMLS_DC) { switch (type) { #define XX_SCALAR(name, value) \ case value: \ return #name; PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR default: return "invalid"; } } static void php_driver_varchar_init(INTERNAL_FUNCTION_PARAMETERS) { char *string; php5to7_size string_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) { return; } PHP5TO7_RETVAL_STRINGL(string, string_len); } static void php_driver_ascii_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_varchar_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } static void php_driver_boolean_init(INTERNAL_FUNCTION_PARAMETERS) { zend_bool value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &value) == FAILURE) { return; } RETURN_BOOL(value); } static void php_driver_counter_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_bigint_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } static void php_driver_double_init(INTERNAL_FUNCTION_PARAMETERS) { double value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &value) == FAILURE) { return; } RETURN_DOUBLE(value); } static void php_driver_int_init(INTERNAL_FUNCTION_PARAMETERS) { long value; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &value) == FAILURE) { return; } RETURN_LONG(value); } static void php_driver_text_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_varchar_init(INTERNAL_FUNCTION_PARAM_PASSTHRU); } #define TYPE_INIT_METHOD(t) php_driver_ ## t ## _init void php_driver_scalar_init(INTERNAL_FUNCTION_PARAMETERS) { php_driver_type *self = PHP_DRIVER_GET_TYPE(getThis()); #define XX_SCALAR(name, value) \ if (self->type == value) { \ TYPE_INIT_METHOD(name)(INTERNAL_FUNCTION_PARAM_PASSTHRU); \ } PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR } #undef TYPE_INIT_METHOD #define TYPE_CODE(m) type_ ## m php5to7_zval php_driver_type_scalar(CassValueType type TSRMLS_DC) { php5to7_zval result; PHP5TO7_ZVAL_UNDEF(result); #define XX_SCALAR(name, value) \ if (value == type) { \ if (PHP5TO7_ZVAL_IS_UNDEF(PHP_DRIVER_G(TYPE_CODE(name)))) { \ PHP_DRIVER_G(TYPE_CODE(name)) = php_driver_type_scalar_new(type TSRMLS_CC); \ } \ Z_ADDREF_P(PHP5TO7_ZVAL_MAYBE_P(PHP_DRIVER_G(TYPE_CODE(name)))); \ return PHP_DRIVER_G(TYPE_CODE(name)); \ } PHP_DRIVER_SCALAR_TYPES_MAP(XX_SCALAR) #undef XX_SCALAR zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid type"); return result; } #undef TYPE_CODE php5to7_zval php_driver_type_map(zval *key_type, zval *value_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *map; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_map_ce); map = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); if (!PHP5TO7_ZVAL_IS_UNDEF_P(key_type)) { sub_type = PHP_DRIVER_GET_TYPE(key_type); cass_data_type_add_sub_type(map->data_type, sub_type->data_type); } if (!PHP5TO7_ZVAL_IS_UNDEF_P(value_type)) { sub_type = PHP_DRIVER_GET_TYPE(value_type); cass_data_type_add_sub_type(map->data_type, sub_type->data_type); } #if PHP_MAJOR_VERSION >= 7 map->data.map.key_type = *key_type; map->data.map.value_type = *value_type; #else map->data.map.key_type = key_type; map->data.map.value_type = value_type; #endif return ztype; } php5to7_zval php_driver_type_map_from_value_types(CassValueType key_type, CassValueType value_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *map; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_map_ce); map = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); map->data.map.key_type = php_driver_type_scalar(key_type TSRMLS_CC); map->data.map.value_type = php_driver_type_scalar(value_type TSRMLS_CC); sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->data.map.key_type)); cass_data_type_add_sub_type(map->data_type, sub_type->data_type); sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(map->data.map.value_type)); cass_data_type_add_sub_type(map->data_type, sub_type->data_type); return ztype; } php5to7_zval php_driver_type_set(zval *value_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *set; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_set_ce); set = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); if (!PHP5TO7_ZVAL_IS_UNDEF_P(value_type)) { sub_type = PHP_DRIVER_GET_TYPE(value_type); cass_data_type_add_sub_type(set->data_type, sub_type->data_type); } #if PHP_MAJOR_VERSION >= 7 set->data.set.value_type = *value_type; #else set->data.set.value_type = value_type; #endif return ztype; } php5to7_zval php_driver_type_set_from_value_type(CassValueType type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *set; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_set_ce); set = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); set->data.set.value_type = php_driver_type_scalar(type TSRMLS_CC); sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(set->data.set.value_type)); cass_data_type_add_sub_type(set->data_type, sub_type->data_type); return ztype; } php5to7_zval php_driver_type_collection(zval *value_type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *collection; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_collection_ce); collection = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); if (!PHP5TO7_ZVAL_IS_UNDEF_P(value_type)) { sub_type = PHP_DRIVER_GET_TYPE(value_type); cass_data_type_add_sub_type(collection->data_type, sub_type->data_type); } #if PHP_MAJOR_VERSION >= 7 collection->data.collection.value_type = *value_type; #else collection->data.collection.value_type = value_type; #endif return ztype; } php5to7_zval php_driver_type_collection_from_value_type(CassValueType type TSRMLS_DC) { php5to7_zval ztype; php_driver_type *collection; php_driver_type *sub_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_collection_ce); collection = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); collection->data.collection.value_type = php_driver_type_scalar(type TSRMLS_CC); sub_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(collection->data.collection.value_type)); cass_data_type_add_sub_type(collection->data_type, sub_type->data_type); return ztype; } php5to7_zval php_driver_type_tuple(TSRMLS_D) { php5to7_zval ztype; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_tuple_ce); return ztype; } php5to7_zval php_driver_type_user_type(TSRMLS_D) { php5to7_zval ztype; php_driver_type *user_type; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_user_type_ce); user_type = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); user_type->data_type = cass_data_type_new(CASS_VALUE_TYPE_UDT); return ztype; } php5to7_zval php_driver_type_custom(const char *name, size_t name_length TSRMLS_DC) { php5to7_zval ztype; php_driver_type *custom; PHP5TO7_ZVAL_MAYBE_MAKE(ztype); object_init_ex(PHP5TO7_ZVAL_MAYBE_P(ztype), php_driver_type_custom_ce); custom = PHP_DRIVER_GET_TYPE(PHP5TO7_ZVAL_MAYBE_P(ztype)); custom->data.custom.class_name = estrndup(name, name_length); return ztype; } #define EXPECTING_TOKEN(expected) \ zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, \ "Unexpected %s at position %d in string \"%s\", expected " expected, \ describe_token(token), ((int) (str - validator) - 1), validator \ ); \ return FAILURE; enum token_type { TOKEN_ILLEGAL = 0, TOKEN_PAREN_OPEN, TOKEN_PAREN_CLOSE, TOKEN_COMMA, TOKEN_COLON, TOKEN_NAME, TOKEN_END }; enum parser_state { STATE_CLASS = 0, STATE_AFTER_CLASS, STATE_AFTER_PARENS, STATE_END }; static const char * describe_token(enum token_type token) { switch (token) { case TOKEN_ILLEGAL: return "illegal character"; break; case TOKEN_PAREN_OPEN: return "opening parenthesis"; break; case TOKEN_PAREN_CLOSE: return "closing parenthesis"; break; case TOKEN_COMMA: return "comma"; break; case TOKEN_COLON: return "colon"; break; case TOKEN_NAME: return "alphanumeric character"; break; case TOKEN_END: return "end of string"; break; default: return "unknown token"; } } static int isletter(char ch) { return isalnum(ch) || ch == '.'; } static enum token_type next_token(const char *str, size_t len, const char **token_str, size_t *token_len, const char **str_out, size_t *len_out) { enum token_type type; unsigned int i = 0; char c = str[i]; if (len == 0) { return TOKEN_END; } if (isalnum(c)) { type = TOKEN_NAME; while (i < len) { if (!isletter(str[i])) { break; } i++; } } else { switch (c) { case '\0': type = TOKEN_END; break; case '(': type = TOKEN_PAREN_OPEN; i++; break; case ')': type = TOKEN_PAREN_CLOSE; i++; break; case ',': type = TOKEN_COMMA; i++; break; case ':': type = TOKEN_COLON; i++; break; default: type = TOKEN_ILLEGAL; } } *token_str = &(str[0]); *token_len = i; *str_out = &(str[i]); *len_out = len - i; return type; } static struct node_s * php_driver_parse_node_new() { struct node_s *node; node = emalloc(sizeof(struct node_s)); node->parent = NULL; node->name = NULL; node->name_length = 0; node->first_child = NULL; node->last_child = NULL; node->next_sibling = NULL; node->prev_sibling = NULL; return node; } static void php_driver_parse_node_free(struct node_s *node) { if (node->first_child) { php_driver_parse_node_free(node->first_child); node->first_child = NULL; } node->last_child = NULL; if (node->next_sibling) { php_driver_parse_node_free(node->next_sibling); node->next_sibling = NULL; } efree(node); } static int php_driver_parse_class_name(const char *validator, size_t validator_len, struct node_s **result TSRMLS_DC) { const char *str; size_t len; const char *token_str; size_t token_len; enum parser_state state; enum token_type token; struct node_s *root; struct node_s *node; struct node_s *child; token_str = NULL; token_len = 0; state = STATE_CLASS; str = validator; len = validator_len; root = php_driver_parse_node_new(); node = root; while (1) { token = next_token(str, len, &token_str, &token_len, &str, &len); if (token == TOKEN_ILLEGAL) { zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Illegal character \"%c\" at position %d in \"%s\"", *token_str, ((int) (str - validator) - 1), validator); php_driver_parse_node_free(root); return FAILURE; } if (state == STATE_AFTER_PARENS) { if (token == TOKEN_COMMA) { if (node->parent == NULL) { EXPECTING_TOKEN("end of string"); } state = STATE_CLASS; child = php_driver_parse_node_new(); child->parent = node->parent; child->prev_sibling = node; node->next_sibling = child; node->parent->last_child = child; node = child; continue; } else if (token == TOKEN_PAREN_CLOSE) { if (node->parent == NULL) { EXPECTING_TOKEN("end of string"); } node = node->parent; continue; } else if (token == TOKEN_END) { break; } else { EXPECTING_TOKEN("a comma, a closing parenthesis or an end of string"); } } if (state == STATE_AFTER_CLASS) { if (token == TOKEN_PAREN_OPEN) { state = STATE_CLASS; child = php_driver_parse_node_new(); child->parent = node; if (node->first_child == NULL) { node->first_child = child; } if (node->last_child) { node->last_child->next_sibling = child; } child->prev_sibling = node->last_child; node->last_child = child; node = child; continue; } else if (token == TOKEN_COMMA || token == TOKEN_COLON) { state = STATE_CLASS; child = php_driver_parse_node_new(); child->parent = node->parent; child->prev_sibling = node; node->next_sibling = child; node->parent->last_child = child; node = child; continue; } else if (token == TOKEN_PAREN_CLOSE) { state = STATE_AFTER_PARENS; node = node->parent; continue; } else if (token == TOKEN_END) { break; } else { php_driver_parse_node_free(root); EXPECTING_TOKEN("opening/closing parenthesis or comma"); } } if (state == STATE_CLASS) { if (token != TOKEN_NAME) { php_driver_parse_node_free(root); EXPECTING_TOKEN("fully qualified class name"); } state = STATE_AFTER_CLASS; node->name = token_str; node->name_length = token_len; } } *result = root; return SUCCESS; } static CassValueType php_driver_lookup_type(struct node_s *node TSRMLS_DC) { if (strncmp("org.apache.cassandra.db.marshal.AsciiType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_ASCII; } if (strncmp("org.apache.cassandra.db.marshal.LongType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_BIGINT; } if (strncmp("org.apache.cassandra.db.marshal.ShortType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_SMALL_INT; } if (strncmp("org.apache.cassandra.db.marshal.ByteType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_TINY_INT; } if (strncmp("org.apache.cassandra.db.marshal.BytesType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_BLOB; } if (strncmp("org.apache.cassandra.db.marshal.BooleanType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_BOOLEAN; } if (strncmp("org.apache.cassandra.db.marshal.CounterColumnType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_COUNTER; } if (strncmp("org.apache.cassandra.db.marshal.DecimalType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_DECIMAL; } if (strncmp("org.apache.cassandra.db.marshal.DurationType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_DURATION; } if (strncmp("org.apache.cassandra.db.marshal.DoubleType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_DOUBLE; } if (strncmp("org.apache.cassandra.db.marshal.FloatType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_FLOAT; } if (strncmp("org.apache.cassandra.db.marshal.InetAddressType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_INET; } if (strncmp("org.apache.cassandra.db.marshal.Int32Type", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_INT; } if (strncmp("org.apache.cassandra.db.marshal.UTF8Type", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_VARCHAR; } if (strncmp("org.apache.cassandra.db.marshal.TimestampType", node->name, node->name_length) == 0 || strncmp("org.apache.cassandra.db.marshal.DateType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_TIMESTAMP; } if (strncmp("org.apache.cassandra.db.marshal.SimpleDateType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_DATE; } if (strncmp("org.apache.cassandra.db.marshal.TimeType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_TIME; } if (strncmp("org.apache.cassandra.db.marshal.UUIDType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_UUID; } if (strncmp("org.apache.cassandra.db.marshal.IntegerType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_VARINT; } if (strncmp("org.apache.cassandra.db.marshal.TimeUUIDType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_TIMEUUID; } if (strncmp("org.apache.cassandra.db.marshal.MapType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_MAP; } if (strncmp("org.apache.cassandra.db.marshal.SetType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_SET; } if (strncmp("org.apache.cassandra.db.marshal.ListType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_LIST; } if (strncmp("org.apache.cassandra.db.marshal.TupleType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_TUPLE; } if (strncmp("org.apache.cassandra.db.marshal.UserType", node->name, node->name_length) == 0) { return CASS_VALUE_TYPE_UDT; } return CASS_VALUE_TYPE_CUSTOM; } static void php_driver_node_dump_to(struct node_s *node, smart_str *text) { smart_str_appendl(text, node->name, node->name_length); if (node->first_child) { smart_str_appendl(text, "(", 1); php_driver_node_dump_to(node->first_child, text); smart_str_appendl(text, ")", 1); } if (node->next_sibling) { smart_str_appendl(text, ", ", 2); php_driver_node_dump_to(node->next_sibling, text); } } static php5to7_zval php_driver_create_type(struct node_s *node TSRMLS_DC) { CassValueType type = CASS_VALUE_TYPE_UNKNOWN; /* Skip wrapper types */ while (node && (strncmp("org.apache.cassandra.db.marshal.FrozenType", node->name, node->name_length) == 0 || strncmp("org.apache.cassandra.db.marshal.ReversedType", node->name, node->name_length) == 0 || strncmp("org.apache.cassandra.db.marshal.CompositeType", node->name, node->name_length) == 0)) { node = node->first_child; } if (node) { type = php_driver_lookup_type(node TSRMLS_CC); } if (type == CASS_VALUE_TYPE_UNKNOWN) { php5to7_zval undef; PHP5TO7_ZVAL_UNDEF(undef); return undef; } if (type == CASS_VALUE_TYPE_CUSTOM) { php5to7_zval ztype; smart_str class_name = PHP5TO7_SMART_STR_INIT; php_driver_node_dump_to(node, &class_name); ztype = php_driver_type_custom(PHP5TO7_SMART_STR_VAL(class_name), PHP5TO7_SMART_STR_LEN(class_name) TSRMLS_CC); smart_str_free(&class_name); return ztype; } else if (type == CASS_VALUE_TYPE_MAP) { php5to7_zval key_type; php5to7_zval value_type; if (node->first_child) { key_type = php_driver_create_type(node->first_child TSRMLS_CC); value_type = php_driver_create_type(node->first_child->next_sibling TSRMLS_CC); } else { PHP5TO7_ZVAL_UNDEF(key_type); PHP5TO7_ZVAL_UNDEF(value_type); } return php_driver_type_map(PHP5TO7_ZVAL_MAYBE_P(key_type), PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); } else if (type == CASS_VALUE_TYPE_LIST) { php5to7_zval value_type; if (node->first_child) { value_type = php_driver_create_type(node->first_child TSRMLS_CC); } else { PHP5TO7_ZVAL_UNDEF(value_type); } return php_driver_type_collection(PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); } else if (type == CASS_VALUE_TYPE_SET) { php5to7_zval value_type; if (node->first_child) { value_type = php_driver_create_type(node->first_child TSRMLS_CC); } else { PHP5TO7_ZVAL_UNDEF(value_type); } return php_driver_type_set(PHP5TO7_ZVAL_MAYBE_P(value_type) TSRMLS_CC); } else if (type == CASS_VALUE_TYPE_TUPLE) { return php_driver_tuple_from_node(node TSRMLS_CC); } else if (type == CASS_VALUE_TYPE_UDT) { return php_driver_user_type_from_node(node TSRMLS_CC); } return php_driver_type_scalar(type TSRMLS_CC); } int php_driver_parse_column_type(const char *validator, size_t validator_len, int *reversed_out, int *frozen_out, php5to7_zval *type_out TSRMLS_DC) { struct node_s *root; struct node_s *node = NULL; cass_bool_t reversed = 0; cass_bool_t frozen = 0; if (php_driver_parse_class_name(validator, validator_len, &root TSRMLS_CC) == FAILURE) { return FAILURE; } node = root; while (node) { if (strncmp("org.apache.cassandra.db.marshal.ReversedType", node->name, node->name_length) == 0) { reversed = 1; node = node->first_child; continue; } if (strncmp("org.apache.cassandra.db.marshal.FrozenType", node->name, node->name_length) == 0) { frozen = 1; node = node->first_child; continue; } if (strncmp("org.apache.cassandra.db.marshal.CompositeType", node->name, node->name_length) == 0) { node = node->first_child; continue; } break; } if (node == NULL) { php_driver_parse_node_free(root); zend_throw_exception_ex(php_driver_invalid_argument_exception_ce, 0 TSRMLS_CC, "Invalid type"); return FAILURE; } *reversed_out = reversed; *frozen_out = frozen; *type_out = php_driver_create_type(node TSRMLS_CC); php_driver_parse_node_free(root); return SUCCESS; } cassandra-1.3.0/util/types.h0000644000076500000240000000635213062515665016442 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UTIL_TYPES_H #define PHP_DRIVER_UTIL_TYPES_H #if PHP_MAJOR_VERSION >= 7 #include #else #include #endif #define PHP_DRIVER_SCALAR_TYPES_MAP(XX) \ XX(ascii, CASS_VALUE_TYPE_ASCII) \ XX(bigint, CASS_VALUE_TYPE_BIGINT) \ XX(smallint, CASS_VALUE_TYPE_SMALL_INT) \ XX(tinyint, CASS_VALUE_TYPE_TINY_INT) \ XX(blob, CASS_VALUE_TYPE_BLOB) \ XX(boolean, CASS_VALUE_TYPE_BOOLEAN) \ XX(counter, CASS_VALUE_TYPE_COUNTER) \ XX(decimal, CASS_VALUE_TYPE_DECIMAL) \ XX(double, CASS_VALUE_TYPE_DOUBLE) \ XX(duration, CASS_VALUE_TYPE_DURATION) \ XX(float, CASS_VALUE_TYPE_FLOAT) \ XX(int, CASS_VALUE_TYPE_INT) \ XX(text, CASS_VALUE_TYPE_TEXT) \ XX(timestamp, CASS_VALUE_TYPE_TIMESTAMP) \ XX(date, CASS_VALUE_TYPE_DATE) \ XX(time, CASS_VALUE_TYPE_TIME) \ XX(uuid, CASS_VALUE_TYPE_UUID) \ XX(varchar, CASS_VALUE_TYPE_VARCHAR) \ XX(varint, CASS_VALUE_TYPE_VARINT) \ XX(timeuuid, CASS_VALUE_TYPE_TIMEUUID) \ XX(inet, CASS_VALUE_TYPE_INET) php5to7_zval php_driver_type_from_data_type(const CassDataType *data_type TSRMLS_DC); int php_driver_type_validate(zval *object, const char *object_name TSRMLS_DC); int php_driver_type_compare(php_driver_type *type1, php_driver_type *type2 TSRMLS_DC); void php_driver_type_string(php_driver_type *type, smart_str *smart TSRMLS_DC); php5to7_zval php_driver_type_scalar(CassValueType type TSRMLS_DC); const char* php_driver_scalar_type_name(CassValueType type TSRMLS_DC); php5to7_zval php_driver_type_set(zval *value_type TSRMLS_DC); php5to7_zval php_driver_type_set_from_value_type(CassValueType type TSRMLS_DC); php5to7_zval php_driver_type_collection(zval *value_type TSRMLS_DC); php5to7_zval php_driver_type_collection_from_value_type(CassValueType type TSRMLS_DC); php5to7_zval php_driver_type_map(zval *key_type, zval *value_type TSRMLS_DC); php5to7_zval php_driver_type_map_from_value_types(CassValueType key_type, CassValueType value_type TSRMLS_DC); php5to7_zval php_driver_type_tuple(TSRMLS_D); php5to7_zval php_driver_type_user_type(TSRMLS_D); php5to7_zval php_driver_type_custom(const char *name, size_t name_length TSRMLS_DC); int php_driver_parse_column_type(const char *validator, size_t validator_len, int *reversed_out, int *frozen_out, php5to7_zval *type_out TSRMLS_DC); void php_driver_scalar_init(INTERNAL_FUNCTION_PARAMETERS); #endif /* PHP_DRIVER_UTIL_TYPES_H */ cassandra-1.3.0/util/uthash.h0000644000076500000240000017153013062515665016573 0ustar michaelpenickstaff/* Copyright (c) 2003-2014, Troy D. Hanson http://troydhanson.github.com/uthash/ All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef UTHASH_H #define UTHASH_H #include /* memcmp,strlen */ #include /* ptrdiff_t */ #include /* exit() */ /* These macros use decltype or the earlier __typeof GNU extension. As decltype is only available in newer compilers (VS2010 or gcc 4.3+ when compiling c++ source) this code uses whatever method is needed or, for VS2008 where neither is available, uses casting workarounds. */ #if defined(_MSC_VER) /* MS compiler */ #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ #define DECLTYPE(x) (decltype(x)) #else /* VS2008 or older (or VS2010 in C mode) */ #define NO_DECLTYPE #define DECLTYPE(x) #endif #elif defined(__BORLANDC__) || defined(__LCC__) || defined(__WATCOMC__) #define NO_DECLTYPE #define DECLTYPE(x) #else /* GNU, Sun and other compilers */ #define DECLTYPE(x) (__typeof(x)) #endif #ifdef NO_DECLTYPE #define DECLTYPE_ASSIGN(dst,src) \ do { \ char **_da_dst = (char**)(&(dst)); \ *_da_dst = (char*)(src); \ } while(0) #else #define DECLTYPE_ASSIGN(dst,src) \ do { \ (dst) = DECLTYPE(dst)(src); \ } while(0) #endif /* a number of the hash function use uint32_t which isn't defined on Pre VS2010 */ #if defined(_WIN32) #if defined(_MSC_VER) && _MSC_VER >= 1600 #include #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__) #include #else typedef unsigned int uint32_t; typedef unsigned char uint8_t; #endif #elif defined(__GNUC__) && !defined(__VXWORKS__) #include #else typedef unsigned int uint32_t; typedef unsigned char uint8_t; #endif #define UTHASH_VERSION 1.9.9 #ifndef uthash_fatal #define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */ #endif #ifndef uthash_malloc #define uthash_malloc(sz) malloc(sz) /* malloc fcn */ #endif #ifndef uthash_free #define uthash_free(ptr,sz) free(ptr) /* free fcn */ #endif #ifndef uthash_noexpand_fyi #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */ #endif #ifndef uthash_expand_fyi #define uthash_expand_fyi(tbl) /* can be defined to log expands */ #endif /* initial number of buckets */ #define HASH_INITIAL_NUM_BUCKETS 32U /* initial number of buckets */ #define HASH_INITIAL_NUM_BUCKETS_LOG2 5U /* lg2 of initial number of buckets */ #define HASH_BKT_CAPACITY_THRESH 10U /* expand when bucket count reaches */ /* calculate the element whose hash handle address is hhe */ #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho))) #define HASH_FIND(hh,head,keyptr,keylen,out) \ do { \ out=NULL; \ if (head != NULL) { \ unsigned _hf_bkt,_hf_hashv; \ HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \ if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv) != 0) { \ HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \ keyptr,keylen,out); \ } \ } \ } while (0) #ifdef HASH_BLOOM #define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM) #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL) #define HASH_BLOOM_MAKE(tbl) \ do { \ (tbl)->bloom_nbits = HASH_BLOOM; \ (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \ if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \ memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ } while (0) #define HASH_BLOOM_FREE(tbl) \ do { \ uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ } while (0) #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8U] |= (1U << ((idx)%8U))) #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U))) #define HASH_BLOOM_ADD(tbl,hashv) \ HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1U))) #define HASH_BLOOM_TEST(tbl,hashv) \ HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1U))) #else #define HASH_BLOOM_MAKE(tbl) #define HASH_BLOOM_FREE(tbl) #define HASH_BLOOM_ADD(tbl,hashv) #define HASH_BLOOM_TEST(tbl,hashv) (1) #define HASH_BLOOM_BYTELEN 0U #endif #define HASH_MAKE_TABLE(hh,head) \ do { \ (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \ sizeof(UT_hash_table)); \ if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \ memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ (head)->hh.tbl->tail = &((head)->hh); \ (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \ (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \ HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \ memset((head)->hh.tbl->buckets, 0, \ HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ HASH_BLOOM_MAKE((head)->hh.tbl); \ (head)->hh.tbl->signature = HASH_SIGNATURE; \ } while(0) #define HASH_ADD(hh,head,fieldname,keylen_in,add) \ HASH_ADD_KEYPTR(hh,head,&((add)->fieldname),keylen_in,add) #define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \ do { \ replaced=NULL; \ HASH_FIND(hh,head,&((add)->fieldname),keylen_in,replaced); \ if (replaced!=NULL) { \ HASH_DELETE(hh,head,replaced); \ } \ HASH_ADD(hh,head,fieldname,keylen_in,add); \ } while(0) #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ do { \ unsigned _ha_bkt; \ (add)->hh.next = NULL; \ (add)->hh.key = (char*)(keyptr); \ (add)->hh.keylen = (unsigned)(keylen_in); \ if (!(head)) { \ head = (add); \ (head)->hh.prev = NULL; \ HASH_MAKE_TABLE(hh,head); \ } else { \ (head)->hh.tbl->tail->next = (add); \ (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ (head)->hh.tbl->tail = &((add)->hh); \ } \ (head)->hh.tbl->num_items++; \ (add)->hh.tbl = (head)->hh.tbl; \ HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \ (add)->hh.hashv, _ha_bkt); \ HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \ HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \ HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \ HASH_FSCK(hh,head); \ } while(0) #define HASH_TO_BKT( hashv, num_bkts, bkt ) \ do { \ bkt = ((hashv) & ((num_bkts) - 1U)); \ } while(0) /* delete "delptr" from the hash table. * "the usual" patch-up process for the app-order doubly-linked-list. * The use of _hd_hh_del below deserves special explanation. * These used to be expressed using (delptr) but that led to a bug * if someone used the same symbol for the head and deletee, like * HASH_DELETE(hh,users,users); * We want that to work, but by changing the head (users) below * we were forfeiting our ability to further refer to the deletee (users) * in the patch-up process. Solution: use scratch space to * copy the deletee pointer, then the latter references are via that * scratch pointer rather than through the repointed (users) symbol. */ #define HASH_DELETE(hh,head,delptr) \ do { \ struct UT_hash_handle *_hd_hh_del; \ if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ uthash_free((head)->hh.tbl->buckets, \ (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ HASH_BLOOM_FREE((head)->hh.tbl); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ head = NULL; \ } else { \ unsigned _hd_bkt; \ _hd_hh_del = &((delptr)->hh); \ if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ (head)->hh.tbl->tail = \ (UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \ (head)->hh.tbl->hho); \ } \ if ((delptr)->hh.prev != NULL) { \ ((UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \ (head)->hh.tbl->hho))->next = (delptr)->hh.next; \ } else { \ DECLTYPE_ASSIGN(head,(delptr)->hh.next); \ } \ if (_hd_hh_del->next != NULL) { \ ((UT_hash_handle*)((ptrdiff_t)_hd_hh_del->next + \ (head)->hh.tbl->hho))->prev = \ _hd_hh_del->prev; \ } \ HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ (head)->hh.tbl->num_items--; \ } \ HASH_FSCK(hh,head); \ } while (0) /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ #define HASH_FIND_STR(head,findstr,out) \ HASH_FIND(hh,head,findstr,(unsigned)strlen(findstr),out) #define HASH_ADD_STR(head,strfield,add) \ HASH_ADD(hh,head,strfield[0],(unsigned int)strlen(add->strfield),add) #define HASH_REPLACE_STR(head,strfield,add,replaced) \ HASH_REPLACE(hh,head,strfield[0],(unsigned)strlen(add->strfield),add,replaced) #define HASH_FIND_INT(head,findint,out) \ HASH_FIND(hh,head,findint,sizeof(int),out) #define HASH_ADD_INT(head,intfield,add) \ HASH_ADD(hh,head,intfield,sizeof(int),add) #define HASH_REPLACE_INT(head,intfield,add,replaced) \ HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced) #define HASH_FIND_PTR(head,findptr,out) \ HASH_FIND(hh,head,findptr,sizeof(void *),out) #define HASH_ADD_PTR(head,ptrfield,add) \ HASH_ADD(hh,head,ptrfield,sizeof(void *),add) #define HASH_REPLACE_PTR(head,ptrfield,add,replaced) \ HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced) #define HASH_DEL(head,delptr) \ HASH_DELETE(hh,head,delptr) /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined. * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined. */ #ifdef HASH_DEBUG #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0) #define HASH_FSCK(hh,head) \ do { \ struct UT_hash_handle *_thh; \ if (head) { \ unsigned _bkt_i; \ unsigned _count; \ char *_prev; \ _count = 0; \ for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \ unsigned _bkt_count = 0; \ _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ _prev = NULL; \ while (_thh) { \ if (_prev != (char*)(_thh->hh_prev)) { \ HASH_OOPS("invalid hh_prev %p, actual %p\n", \ _thh->hh_prev, _prev ); \ } \ _bkt_count++; \ _prev = (char*)(_thh); \ _thh = _thh->hh_next; \ } \ _count += _bkt_count; \ if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ HASH_OOPS("invalid bucket count %u, actual %u\n", \ (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \ } \ } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("invalid hh item count %u, actual %u\n", \ (head)->hh.tbl->num_items, _count ); \ } \ /* traverse hh in app order; check next/prev integrity, count */ \ _count = 0; \ _prev = NULL; \ _thh = &(head)->hh; \ while (_thh) { \ _count++; \ if (_prev !=(char*)(_thh->prev)) { \ HASH_OOPS("invalid prev %p, actual %p\n", \ _thh->prev, _prev ); \ } \ _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \ _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \ (head)->hh.tbl->hho) : NULL ); \ } \ if (_count != (head)->hh.tbl->num_items) { \ HASH_OOPS("invalid app item count %u, actual %u\n", \ (head)->hh.tbl->num_items, _count ); \ } \ } \ } while (0) #else #define HASH_FSCK(hh,head) #endif /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to * the descriptor to which this macro is defined for tuning the hash function. * The app can #include to get the prototype for write(2). */ #ifdef HASH_EMIT_KEYS #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \ do { \ unsigned _klen = fieldlen; \ write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ write(HASH_EMIT_KEYS, keyptr, (unsigned long)fieldlen); \ } while (0) #else #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) #endif /* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ #ifdef HASH_FUNCTION #define HASH_FCN HASH_FUNCTION #else #define HASH_FCN HASH_JEN #endif /* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */ #define HASH_BER(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned _hb_keylen=(unsigned)keylen; \ const unsigned char *_hb_key=(const unsigned char*)(key); \ (hashv) = 0; \ while (_hb_keylen-- != 0U) { \ (hashv) = (((hashv) << 5) + (hashv)) + *_hb_key++; \ } \ bkt = (hashv) & (num_bkts-1U); \ } while (0) /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ #define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned _sx_i; \ const unsigned char *_hs_key=(const unsigned char*)(key); \ hashv = 0; \ for(_sx_i=0; _sx_i < keylen; _sx_i++) { \ hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \ } \ bkt = hashv & (num_bkts-1U); \ } while (0) /* FNV-1a variation */ #define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned _fn_i; \ const unsigned char *_hf_key=(const unsigned char*)(key); \ hashv = 2166136261U; \ for(_fn_i=0; _fn_i < keylen; _fn_i++) { \ hashv = hashv ^ _hf_key[_fn_i]; \ hashv = hashv * 16777619U; \ } \ bkt = hashv & (num_bkts-1U); \ } while(0) #define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned _ho_i; \ const unsigned char *_ho_key=(const unsigned char*)(key); \ hashv = 0; \ for(_ho_i=0; _ho_i < keylen; _ho_i++) { \ hashv += _ho_key[_ho_i]; \ hashv += (hashv << 10); \ hashv ^= (hashv >> 6); \ } \ hashv += (hashv << 3); \ hashv ^= (hashv >> 11); \ hashv += (hashv << 15); \ bkt = hashv & (num_bkts-1U); \ } while(0) #define HASH_JEN_MIX(a,b,c) \ do { \ a -= b; a -= c; a ^= ( c >> 13 ); \ b -= c; b -= a; b ^= ( a << 8 ); \ c -= a; c -= b; c ^= ( b >> 13 ); \ a -= b; a -= c; a ^= ( c >> 12 ); \ b -= c; b -= a; b ^= ( a << 16 ); \ c -= a; c -= b; c ^= ( b >> 5 ); \ a -= b; a -= c; a ^= ( c >> 3 ); \ b -= c; b -= a; b ^= ( a << 10 ); \ c -= a; c -= b; c ^= ( b >> 15 ); \ } while (0) #define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned _hj_i,_hj_j,_hj_k; \ unsigned const char *_hj_key=(unsigned const char*)(key); \ hashv = 0xfeedbeefu; \ _hj_i = _hj_j = 0x9e3779b9u; \ _hj_k = (unsigned)(keylen); \ while (_hj_k >= 12U) { \ _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ + ( (unsigned)_hj_key[2] << 16 ) \ + ( (unsigned)_hj_key[3] << 24 ) ); \ _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ + ( (unsigned)_hj_key[6] << 16 ) \ + ( (unsigned)_hj_key[7] << 24 ) ); \ hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ + ( (unsigned)_hj_key[10] << 16 ) \ + ( (unsigned)_hj_key[11] << 24 ) ); \ \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ \ _hj_key += 12; \ _hj_k -= 12U; \ } \ hashv += (unsigned)(keylen); \ switch ( _hj_k ) { \ case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); /* FALLTHROUGH */ \ case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); /* FALLTHROUGH */ \ case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); /* FALLTHROUGH */ \ case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); /* FALLTHROUGH */ \ case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); /* FALLTHROUGH */ \ case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); /* FALLTHROUGH */ \ case 5: _hj_j += _hj_key[4]; /* FALLTHROUGH */ \ case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \ case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \ case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \ case 1: _hj_i += _hj_key[0]; \ } \ HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ bkt = hashv & (num_bkts-1U); \ } while(0) /* The Paul Hsieh hash function */ #undef get16bits #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) #define get16bits(d) (*((const uint16_t *) (d))) #endif #if !defined (get16bits) #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ +(uint32_t)(((const uint8_t *)(d))[0]) ) #endif #define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \ do { \ unsigned const char *_sfh_key=(unsigned const char*)(key); \ uint32_t _sfh_tmp, _sfh_len = (uint32_t)keylen; \ \ unsigned _sfh_rem = _sfh_len & 3U; \ _sfh_len >>= 2; \ hashv = 0xcafebabeu; \ \ /* Main loop */ \ for (;_sfh_len > 0U; _sfh_len--) { \ hashv += get16bits (_sfh_key); \ _sfh_tmp = ((uint32_t)(get16bits (_sfh_key+2)) << 11) ^ hashv; \ hashv = (hashv << 16) ^ _sfh_tmp; \ _sfh_key += 2U*sizeof (uint16_t); \ hashv += hashv >> 11; \ } \ \ /* Handle end cases */ \ switch (_sfh_rem) { \ case 3: hashv += get16bits (_sfh_key); \ hashv ^= hashv << 16; \ hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)]) << 18; \ hashv += hashv >> 11; \ break; \ case 2: hashv += get16bits (_sfh_key); \ hashv ^= hashv << 11; \ hashv += hashv >> 17; \ break; \ case 1: hashv += *_sfh_key; \ hashv ^= hashv << 10; \ hashv += hashv >> 1; \ } \ \ /* Force "avalanching" of final 127 bits */ \ hashv ^= hashv << 3; \ hashv += hashv >> 5; \ hashv ^= hashv << 4; \ hashv += hashv >> 17; \ hashv ^= hashv << 25; \ hashv += hashv >> 6; \ bkt = hashv & (num_bkts-1U); \ } while(0) #ifdef HASH_USING_NO_STRICT_ALIASING /* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads. * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. * MurmurHash uses the faster approach only on CPU's where we know it's safe. * * Note the preprocessor built-in defines can be emitted using: * * gcc -m64 -dM -E - < /dev/null (on gcc) * cc -## a.c (where a.c is a simple test file) (Sun Studio) */ #if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86)) #define MUR_GETBLOCK(p,i) p[i] #else /* non intel */ #define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 3UL) == 0UL) #define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 3UL) == 1UL) #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 3UL) == 2UL) #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 3UL) == 3UL) #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL)) #if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__)) #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24)) #define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16)) #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8)) #else /* assume little endian non-intel */ #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24)) #define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16)) #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8)) #endif #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \ (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \ (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \ MUR_ONE_THREE(p)))) #endif #define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r)))) #define MUR_FMIX(_h) \ do { \ _h ^= _h >> 16; \ _h *= 0x85ebca6bu; \ _h ^= _h >> 13; \ _h *= 0xc2b2ae35u; \ _h ^= _h >> 16; \ } while(0) #define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \ do { \ const uint8_t *_mur_data = (const uint8_t*)(key); \ const int _mur_nblocks = (int)(keylen) / 4; \ uint32_t _mur_h1 = 0xf88D5353u; \ uint32_t _mur_c1 = 0xcc9e2d51u; \ uint32_t _mur_c2 = 0x1b873593u; \ uint32_t _mur_k1 = 0; \ const uint8_t *_mur_tail; \ const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+(_mur_nblocks*4)); \ int _mur_i; \ for(_mur_i = -_mur_nblocks; _mur_i!=0; _mur_i++) { \ _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \ _mur_k1 *= _mur_c1; \ _mur_k1 = MUR_ROTL32(_mur_k1,15); \ _mur_k1 *= _mur_c2; \ \ _mur_h1 ^= _mur_k1; \ _mur_h1 = MUR_ROTL32(_mur_h1,13); \ _mur_h1 = (_mur_h1*5U) + 0xe6546b64u; \ } \ _mur_tail = (const uint8_t*)(_mur_data + (_mur_nblocks*4)); \ _mur_k1=0; \ switch((keylen) & 3U) { \ case 3: _mur_k1 ^= (uint32_t)_mur_tail[2] << 16; /* FALLTHROUGH */ \ case 2: _mur_k1 ^= (uint32_t)_mur_tail[1] << 8; /* FALLTHROUGH */ \ case 1: _mur_k1 ^= (uint32_t)_mur_tail[0]; \ _mur_k1 *= _mur_c1; \ _mur_k1 = MUR_ROTL32(_mur_k1,15); \ _mur_k1 *= _mur_c2; \ _mur_h1 ^= _mur_k1; \ } \ _mur_h1 ^= (uint32_t)(keylen); \ MUR_FMIX(_mur_h1); \ hashv = _mur_h1; \ bkt = hashv & (num_bkts-1U); \ } while(0) #endif /* HASH_USING_NO_STRICT_ALIASING */ /* key comparison function; return 0 if keys equal */ #ifdef HASH_KEYCOMPARE #define HASH_KEYCMP HASH_KEYCOMPARE #else #define HASH_KEYCMP(a,b,len) memcmp(a,b,(unsigned long)(len)) #endif /* iterate over items in a known bucket to find desired item */ #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \ do { \ if (head.hh_head != NULL) { DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); } \ else { out=NULL; } \ while (out != NULL) { \ if ((out)->hh.keylen == (keylen_in)) { \ if ((HASH_KEYCMP((out)->hh.key,keyptr,keylen_in)) == 0) { break; } \ } \ if ((out)->hh.hh_next != NULL) { DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,(out)->hh.hh_next)); } \ else { out = NULL; } \ } \ } while(0) /* add an item to a bucket */ #define HASH_ADD_TO_BKT(head,addhh) \ do { \ head.count++; \ (addhh)->hh_next = head.hh_head; \ (addhh)->hh_prev = NULL; \ if (head.hh_head != NULL) { (head).hh_head->hh_prev = (addhh); } \ (head).hh_head=addhh; \ if ((head.count >= ((head.expand_mult+1U) * HASH_BKT_CAPACITY_THRESH)) \ && ((addhh)->tbl->noexpand != 1U)) { \ HASH_EXPAND_BUCKETS((addhh)->tbl); \ } \ } while(0) /* remove an item from a given bucket */ #define HASH_DEL_IN_BKT(hh,head,hh_del) \ (head).count--; \ if ((head).hh_head == hh_del) { \ (head).hh_head = hh_del->hh_next; \ } \ if (hh_del->hh_prev) { \ hh_del->hh_prev->hh_next = hh_del->hh_next; \ } \ if (hh_del->hh_next) { \ hh_del->hh_next->hh_prev = hh_del->hh_prev; \ } /* Bucket expansion has the effect of doubling the number of buckets * and redistributing the items into the new buckets. Ideally the * items will distribute more or less evenly into the new buckets * (the extent to which this is true is a measure of the quality of * the hash function as it applies to the key domain). * * With the items distributed into more buckets, the chain length * (item count) in each bucket is reduced. Thus by expanding buckets * the hash keeps a bound on the chain length. This bounded chain * length is the essence of how a hash provides constant time lookup. * * The calculation of tbl->ideal_chain_maxlen below deserves some * explanation. First, keep in mind that we're calculating the ideal * maximum chain length based on the *new* (doubled) bucket count. * In fractions this is just n/b (n=number of items,b=new num buckets). * Since the ideal chain length is an integer, we want to calculate * ceil(n/b). We don't depend on floating point arithmetic in this * hash, so to calculate ceil(n/b) with integers we could write * * ceil(n/b) = (n/b) + ((n%b)?1:0) * * and in fact a previous version of this hash did just that. * But now we have improved things a bit by recognizing that b is * always a power of two. We keep its base 2 log handy (call it lb), * so now we can write this with a bit shift and logical AND: * * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) * */ #define HASH_EXPAND_BUCKETS(tbl) \ do { \ unsigned _he_bkt; \ unsigned _he_bkt_i; \ struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ 2UL * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \ memset(_he_new_buckets, 0, \ 2UL * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ tbl->ideal_chain_maxlen = \ (tbl->num_items >> (tbl->log2_num_buckets+1U)) + \ (((tbl->num_items & ((tbl->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \ tbl->nonideal_items = 0; \ for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \ { \ _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \ while (_he_thh != NULL) { \ _he_hh_nxt = _he_thh->hh_next; \ HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2U, _he_bkt); \ _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \ if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \ tbl->nonideal_items++; \ _he_newbkt->expand_mult = _he_newbkt->count / \ tbl->ideal_chain_maxlen; \ } \ _he_thh->hh_prev = NULL; \ _he_thh->hh_next = _he_newbkt->hh_head; \ if (_he_newbkt->hh_head != NULL) { _he_newbkt->hh_head->hh_prev = \ _he_thh; } \ _he_newbkt->hh_head = _he_thh; \ _he_thh = _he_hh_nxt; \ } \ } \ uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ tbl->num_buckets *= 2U; \ tbl->log2_num_buckets++; \ tbl->buckets = _he_new_buckets; \ tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ (tbl->ineff_expands+1U) : 0U; \ if (tbl->ineff_expands > 1U) { \ tbl->noexpand=1; \ uthash_noexpand_fyi(tbl); \ } \ uthash_expand_fyi(tbl); \ } while(0) /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ /* Note that HASH_SORT assumes the hash handle name to be hh. * HASH_SRT was added to allow the hash handle name to be passed in. */ #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) #define HASH_SRT(hh,head,cmpfcn) \ do { \ unsigned _hs_i; \ unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \ struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ if (head != NULL) { \ _hs_insize = 1; \ _hs_looping = 1; \ _hs_list = &((head)->hh); \ while (_hs_looping != 0U) { \ _hs_p = _hs_list; \ _hs_list = NULL; \ _hs_tail = NULL; \ _hs_nmerges = 0; \ while (_hs_p != NULL) { \ _hs_nmerges++; \ _hs_q = _hs_p; \ _hs_psize = 0; \ for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \ _hs_psize++; \ _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ ((void*)((char*)(_hs_q->next) + \ (head)->hh.tbl->hho)) : NULL); \ if (! (_hs_q) ) { break; } \ } \ _hs_qsize = _hs_insize; \ while ((_hs_psize > 0U) || ((_hs_qsize > 0U) && (_hs_q != NULL))) {\ if (_hs_psize == 0U) { \ _hs_e = _hs_q; \ _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ ((void*)((char*)(_hs_q->next) + \ (head)->hh.tbl->hho)) : NULL); \ _hs_qsize--; \ } else if ( (_hs_qsize == 0U) || (_hs_q == NULL) ) { \ _hs_e = _hs_p; \ if (_hs_p != NULL){ \ _hs_p = (UT_hash_handle*)((_hs_p->next != NULL) ? \ ((void*)((char*)(_hs_p->next) + \ (head)->hh.tbl->hho)) : NULL); \ } \ _hs_psize--; \ } else if (( \ cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \ DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \ ) <= 0) { \ _hs_e = _hs_p; \ if (_hs_p != NULL){ \ _hs_p = (UT_hash_handle*)((_hs_p->next != NULL) ? \ ((void*)((char*)(_hs_p->next) + \ (head)->hh.tbl->hho)) : NULL); \ } \ _hs_psize--; \ } else { \ _hs_e = _hs_q; \ _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ ((void*)((char*)(_hs_q->next) + \ (head)->hh.tbl->hho)) : NULL); \ _hs_qsize--; \ } \ if ( _hs_tail != NULL ) { \ _hs_tail->next = ((_hs_e != NULL) ? \ ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \ } else { \ _hs_list = _hs_e; \ } \ if (_hs_e != NULL) { \ _hs_e->prev = ((_hs_tail != NULL) ? \ ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \ } \ _hs_tail = _hs_e; \ } \ _hs_p = _hs_q; \ } \ if (_hs_tail != NULL){ \ _hs_tail->next = NULL; \ } \ if ( _hs_nmerges <= 1U ) { \ _hs_looping=0; \ (head)->hh.tbl->tail = _hs_tail; \ DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ } \ _hs_insize *= 2U; \ } \ HASH_FSCK(hh,head); \ } \ } while (0) /* This function selects items from one hash into another hash. * The end result is that the selected items have dual presence * in both hashes. There is no copy of the items made; rather * they are added into the new hash through a secondary hash * hash handle that must be present in the structure. */ #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ do { \ unsigned _src_bkt, _dst_bkt; \ void *_last_elt=NULL, *_elt; \ UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \ ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \ if (src != NULL) { \ for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \ for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \ _src_hh != NULL; \ _src_hh = _src_hh->hh_next) { \ _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ if (cond(_elt)) { \ _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \ _dst_hh->key = _src_hh->key; \ _dst_hh->keylen = _src_hh->keylen; \ _dst_hh->hashv = _src_hh->hashv; \ _dst_hh->prev = _last_elt; \ _dst_hh->next = NULL; \ if (_last_elt_hh != NULL) { _last_elt_hh->next = _elt; } \ if (dst == NULL) { \ DECLTYPE_ASSIGN(dst,_elt); \ HASH_MAKE_TABLE(hh_dst,dst); \ } else { \ _dst_hh->tbl = (dst)->hh_dst.tbl; \ } \ HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \ HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \ (dst)->hh_dst.tbl->num_items++; \ _last_elt = _elt; \ _last_elt_hh = _dst_hh; \ } \ } \ } \ } \ HASH_FSCK(hh_dst,dst); \ } while (0) #define HASH_CLEAR(hh,head) \ do { \ if (head != NULL) { \ uthash_free((head)->hh.tbl->buckets, \ (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ HASH_BLOOM_FREE((head)->hh.tbl); \ uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ (head)=NULL; \ } \ } while(0) #define HASH_OVERHEAD(hh,head) \ ((head != NULL) ? ( \ (size_t)(((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \ ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \ sizeof(UT_hash_table) + \ (HASH_BLOOM_BYTELEN))) : 0U) #ifdef NO_DECLTYPE #define HASH_ITER(hh,head,el,tmp) \ for(((el)=(head)), ((*(char**)(&(tmp)))=(char*)((head!=NULL)?(head)->hh.next:NULL)); \ (el) != NULL; ((el)=(tmp)), ((*(char**)(&(tmp)))=(char*)((tmp!=NULL)?(tmp)->hh.next:NULL))) #else #define HASH_ITER(hh,head,el,tmp) \ for(((el)=(head)), ((tmp)=DECLTYPE(el)((head!=NULL)?(head)->hh.next:NULL)); \ (el) != NULL; ((el)=(tmp)), ((tmp)=DECLTYPE(el)((tmp!=NULL)?(tmp)->hh.next:NULL))) #endif /* obtain a count of items in the hash */ #define HASH_COUNT(head) HASH_CNT(hh,head) #define HASH_CNT(hh,head) ((head != NULL)?((head)->hh.tbl->num_items):0U) typedef struct UT_hash_bucket { struct UT_hash_handle *hh_head; unsigned count; /* expand_mult is normally set to 0. In this situation, the max chain length * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If * the bucket's chain exceeds this length, bucket expansion is triggered). * However, setting expand_mult to a non-zero value delays bucket expansion * (that would be triggered by additions to this particular bucket) * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. * (The multiplier is simply expand_mult+1). The whole idea of this * multiplier is to reduce bucket expansions, since they are expensive, in * situations where we know that a particular bucket tends to be overused. * It is better to let its chain length grow to a longer yet-still-bounded * value, than to do an O(n) bucket expansion too often. */ unsigned expand_mult; } UT_hash_bucket; /* random signature used only to find hash tables in external analysis */ #define HASH_SIGNATURE 0xa0111fe1u #define HASH_BLOOM_SIGNATURE 0xb12220f2u typedef struct UT_hash_table { UT_hash_bucket *buckets; unsigned num_buckets, log2_num_buckets; unsigned num_items; struct UT_hash_handle *tail; /* tail hh in app order, for fast append */ ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */ /* in an ideal situation (all buckets used equally), no bucket would have * more than ceil(#items/#buckets) items. that's the ideal chain length. */ unsigned ideal_chain_maxlen; /* nonideal_items is the number of items in the hash whose chain position * exceeds the ideal chain maxlen. these items pay the penalty for an uneven * hash distribution; reaching them in a chain traversal takes >ideal steps */ unsigned nonideal_items; /* ineffective expands occur when a bucket doubling was performed, but * afterward, more than half the items in the hash had nonideal chain * positions. If this happens on two consecutive expansions we inhibit any * further expansion, as it's not helping; this happens when the hash * function isn't a good fit for the key domain. When expansion is inhibited * the hash will still work, albeit no longer in constant time. */ unsigned ineff_expands, noexpand; uint32_t signature; /* used only to find hash tables in external analysis */ #ifdef HASH_BLOOM uint32_t bloom_sig; /* used only to test bloom exists in external analysis */ uint8_t *bloom_bv; uint8_t bloom_nbits; #endif } UT_hash_table; typedef struct UT_hash_handle { struct UT_hash_table *tbl; void *prev; /* prev element in app order */ void *next; /* next element in app order */ struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ struct UT_hash_handle *hh_next; /* next hh in bucket order */ void *key; /* ptr to enclosing struct's key */ unsigned keylen; /* enclosing struct's key len */ unsigned hashv; /* result of hash-fcn(key) */ } UT_hash_handle; #endif /* UTHASH_H */ cassandra-1.3.0/util/uuid_gen.c0000644000076500000240000000332313062515665017063 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "php_driver.h" #include "php_driver_globals.h" #include #include "util/uuid_gen.h" static CassUuidGen* get_uuid_gen(TSRMLS_D) { /* Create a new uuid generator if our PID has changed. This prevents the same * UUIDs from being generated in forked processes. */ if (PHP_DRIVER_G(uuid_gen_pid) != getpid()) { if (PHP_DRIVER_G(uuid_gen)) { cass_uuid_gen_free(PHP_DRIVER_G(uuid_gen)); } PHP_DRIVER_G(uuid_gen) = cass_uuid_gen_new(); PHP_DRIVER_G(uuid_gen_pid) = getpid(); } return PHP_DRIVER_G(uuid_gen); } void php_driver_uuid_generate_random(CassUuid *out TSRMLS_DC) { CassUuidGen* uuid_gen = get_uuid_gen(TSRMLS_C); if (!uuid_gen) return; cass_uuid_gen_random(uuid_gen, out); } void php_driver_uuid_generate_time(CassUuid *out TSRMLS_DC) { CassUuidGen* uuid_gen = get_uuid_gen(TSRMLS_C); if (!uuid_gen) return; cass_uuid_gen_time(uuid_gen, out); } void php_driver_uuid_generate_from_time(long timestamp, CassUuid *out TSRMLS_DC) { CassUuidGen* uuid_gen = get_uuid_gen(TSRMLS_C); if (!uuid_gen) return; cass_uuid_gen_from_time(uuid_gen, (cass_uint64_t) timestamp, out); } cassandra-1.3.0/util/uuid_gen.h0000644000076500000240000000162713062515665017075 0ustar michaelpenickstaff/** * Copyright 2015-2016 DataStax, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PHP_DRIVER_UTIL_UUID_GEN_H #define PHP_DRIVER_UTIL_UUID_GEN_H void php_driver_uuid_generate_random(CassUuid *out TSRMLS_DC); void php_driver_uuid_generate_time(CassUuid *out TSRMLS_DC); void php_driver_uuid_generate_from_time(long timestamp, CassUuid *out TSRMLS_DC); #endif /* PHP_DRIVER_UTIL_UUID_GEN_H */ cassandra-1.3.0/version.h0000644000076500000240000000062713062515665016005 0ustar michaelpenickstaff#ifndef PHP_DRIVER_VERSION_H #define PHP_DRIVER_VERSION_H /* Define Extension and Version Properties */ #define PHP_DRIVER_NAME "cassandra" #define PHP_DRIVER_MAJOR 1 #define PHP_DRIVER_MINOR 3 #define PHP_DRIVER_RELEASE 0 #define PHP_DRIVER_STABILITY "stable" #define PHP_DRIVER_VERSION "1.3.0" #define PHP_DRIVER_VERSION_FULL "1.3.0" #endif /* PHP_DRIVER_VERSION_H */ cassandra-1.3.0/LICENSE0000644000076500000240000000105413062515665015147 0ustar michaelpenickstaffCopyright 2013-2016 DataStax, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. cassandra-1.3.0/doc/Cassandra.php0000644000076500000240000002537513062515665017333 0ustar michaelpenickstaffschema() will always return an empty object. This * can be useful for reducing the startup overhead of short-lived sessions. * * @param bool $enabled whether the driver fetches and maintains schema metadata. * * @return Builder self */ public function withSchemaMetadata($enabled) { } /** * Enables/disables Hostname Resolution. * * If enabled the driver will resolve hostnames for IP addresses using * reverse IP lookup. This is useful for authentication (Kerberos) or * encryption SSL services that require a valid hostname for verification. * * @param bool $enabled whether the driver uses hostname resolution. * * @return Builder self */ public function withHostnameResolution($enabled) { } /** * Enables/disables Randomized Contact Points. * * If enabled this allows the driver randomly use contact points in order * to evenly spread the load across the cluster and prevent * hotspots/load spikes during notifications (e.g. massive schema change). * * Note: This setting should only be disabled for debugging and testing. * * @param bool $enabled whether the driver uses randomized contact points. * * @return Builder self */ public function withRandomizedContactPoints($enabled) { } /** * Specify interval in seconds that the driver should wait before attempting * to send heartbeat messages and control the amount of time the connection * must be idle before sending heartbeat messages. This is useful for * preventing intermediate network devices from dropping connections. * * @param float $interval interval in seconds (0 to disable heartbeat). * * @return Builder self */ public function withConnectionHeartbeatInterval($interval) { } } cassandra-1.3.0/doc/Cassandra/Collection.php0000644000076500000240000000516213062515665021416 0ustar michaelpenickstaffcreate(1, 2, 3, 4, 5, 6, 7, 8, 9); * * var_dump($collection); * @endcode * * @param Type $type The type of values * * @return Type The collection type */ public static final function collection($type) { } /** * Initialize a set type * @code{.php} * create("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); * * var_dump($set); * @endcode * * @param Type $type The types of values * * @return Type The set type */ public static final function set($type) { } /** * Initialize a map type * @code{.php} * create(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f") * * var_dump($map); * @endcode * * @param Type $keyType The type of keys * @param Type $valueType The type of values * * @return Type The map type */ public static final function map($keyType, $valueType) { } /** * Initialize a tuple type * @code{.php} * create("a", 123); * * var_dump($tuple); * @endcode * * @param Type $types A variadic list of types * * @return Type The tuple type */ public static final function tuple($types) { } /** * Initialize a user type * @code{.php} * create("a", "abc", "b", 123); * * var_dump($userType); * @endcode * * @param Type $types A variadic list of name/type pairs * * @return Type The user type */ public static final function userType($types) { } } cassandra-1.3.0/doc/Cassandra/Type/Collection.php0000644000076500000240000000336613062515666022344 0ustar michaelpenickstaff` * * @return string Type representation in CQL */ public function __toString() { } /** * Creates a new Collection from the given values. When no values * given, creates an empty list. * * @param mixed $value ,... One or more values to be added to the list. * * @throws Exception\InvalidArgumentException when values given are of a * different type than what this * list type expects. * * @return Collection A list with given values. */ public function create($value) { } } cassandra-1.3.0/doc/Cassandra/Type/Custom.php0000644000076500000240000000214013062515666021510 0ustar michaelpenickstaff` * * @return string Type representation in CQL */ public function __toString() { } /** * Creates a new Map from the given values. * * @code{.php} * create(new Uuid(), 'first uuid', * new Uuid(), 'second uuid', * new Uuid(), 'third uuid'); * * var_dump($map); * @endcode * * * is a key and each even value is a value for the * map, e.g. `create(key, value, key, value)`. * When no values given, creates an empty map. * * @param mixed $value ,... An even number of values, where each odd value * * @throws Exception\InvalidArgumentException when keys or values given are * of a different type than what * this map type expects. * * @return Map A set with given values. */ public function create($value) { } } cassandra-1.3.0/doc/Cassandra/Type/Scalar.php0000644000076500000240000000214213062515666021445 0ustar michaelpenickstaff` * * @return string Type representation in CQL */ public function __toString() { } /** * Creates a new Set from the given values. * * @param mixed $value ,... One or more values to be added to the set. When no values are given, creates an empty set. * * @throws Exception\InvalidArgumentException when values given are of a * different type than what this * set type expects. * * @return Set A set with given values. */ public function create($value) { } } cassandra-1.3.0/doc/Cassandra/Type/Tuple.php0000644000076500000240000000336113062515666021335 0ustar michaelpenickstaff` * * @return string Type representation in CQL */ public function __toString() { } /** * Returns types of values * * @return array An array of types */ public function types() { } /** * Creates a new Tuple from the given values. When no values given, * creates a tuple with null for the values. * * @param mixed $values {@inheritDoc} * * @throws Exception\InvalidArgumentException when values given are of a * different type than what the * tuple expects. * * @return Tuple A tuple with given values. */ public function create($values) { } } cassandra-1.3.0/doc/Cassandra/Type/UserType.php0000644000076500000240000000464213062515666022027 0ustar michaelpenickstaff`. * * @return string Type representation in CQL */ public function __toString() { } /** * Returns types of values * * @return array An array of types */ public function types() { } /** * Creates a new UserTypeValue from the given name/value pairs. When * no values given, creates an empty user type. * * @param mixed $value ,... One or more name/value pairs to be added to the user type. * * @throws Exception\InvalidArgumentException when values given are of a * different types than what the * user type expects. * * @return UserTypeValue A user type value with given name/value pairs. */ public function create($value) { } } cassandra-1.3.0/doc/Cassandra/UserTypeValue.php0000644000076500000240000000437213062515666022103 0ustar michaelpenickstaff --FILE-- --EXPECT-- success