From: Remi Collet Date: Mon, 12 Jun 2023 15:09:39 +0000 (+0200) Subject: add libraryVersion, protocolVersion and serverVersion properties X-Git-Tag: v2.2.3~3^2~5 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=7b61c6f8eb76dc00a5e456ebcaaa2166644ef8ae;p=m6w6%2Fext-pq add libraryVersion, protocolVersion and serverVersion properties --- diff --git a/src/php_pqconn.c b/src/php_pqconn.c index 6278323..70e2c82 100644 --- a/src/php_pqconn.c +++ b/src/php_pqconn.c @@ -491,6 +491,40 @@ static void php_pqconn_object_write_def_auto_conv(void *o, zval *value) obj->intern->default_auto_convert = zval_get_long(value) & PHP_PQRES_CONV_ALL; } +#ifdef HAVE_PQLIBVERSION +static void php_pqconn_object_read_lib_version(void *o, zval *return_value) +{ + char ver[16]; + int v = PQlibVersion(); + + if (v < 100000) { + slprintf(ver, sizeof(ver), "%d.%d.%d", v/10000, v/100%100, v%100); + } else { + slprintf(ver, sizeof(ver), "%d.%d", v/10000, v%100); + } + RETVAL_STRING(ver); +} +#endif +static void php_pqconn_object_read_protocol_version(void *o, zval *return_value) +{ + php_pqconn_object_t *obj = o; + + RETVAL_LONG(PQprotocolVersion(obj->intern->conn)); +} +static void php_pqconn_object_read_server_version(void *o, zval *return_value) +{ + php_pqconn_object_t *obj = o; + char ver[16]; + int v = PQserverVersion(obj->intern->conn); + + if (v < 100000) { + slprintf(ver, sizeof(ver), "%d.%d.%d", v/10000, v/100%100, v%100); + } else { + slprintf(ver, sizeof(ver), "%d.%d", v/10000, v%100); + } + RETVAL_STRING(ver); +} + static ZEND_RESULT_CODE php_pqconn_update_socket(zval *zobj, php_pqconn_object_t *obj) { zval zsocket, zmember; @@ -2131,6 +2165,20 @@ PHP_MINIT_FUNCTION(pqconn) zend_hash_str_add_mem(&php_pqconn_object_prophandlers, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph, sizeof(ph)); ph.write = NULL; +#ifdef HAVE_PQLIBVERSION + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("libraryVersion"), ZEND_ACC_PUBLIC); + ph.read = php_pqconn_object_read_lib_version; + zend_hash_str_add_mem(&php_pqconn_object_prophandlers, ZEND_STRL("libraryVersion"), (void *) &ph, sizeof(ph)); +#endif + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("protocolVersion"), ZEND_ACC_PUBLIC); + ph.read = php_pqconn_object_read_protocol_version; + zend_hash_str_add_mem(&php_pqconn_object_prophandlers, ZEND_STRL("protocolVersion"), (void *) &ph, sizeof(ph)); + + zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("serverVersion"), ZEND_ACC_PUBLIC); + ph.read = php_pqconn_object_read_server_version; + zend_hash_str_add_mem(&php_pqconn_object_prophandlers, ZEND_STRL("serverVersion"), (void *) &ph, sizeof(ph)); + zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("OK"), CONNECTION_OK); zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("BAD"), CONNECTION_BAD); zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("STARTED"), CONNECTION_STARTED);