From: Remi Collet Date: Tue, 13 Jun 2023 13:06:39 +0000 (+0200) Subject: add php_pq_version_to_string() helper (factorization) X-Git-Tag: v2.2.3~3^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ee45162841e41d7eea48c9fb6f2082e3dc449ddc;p=m6w6%2Fext-pq add php_pq_version_to_string() helper (factorization) --- diff --git a/src/php_pq_misc.c b/src/php_pq_misc.c index 9acef34..4d251d2 100644 --- a/src/php_pq_misc.c +++ b/src/php_pq_misc.c @@ -29,6 +29,14 @@ #undef PHP_PQ_TYPE #include "php_pq_type.h" +/* convert version to string */ +extern void php_pq_version_to_string(int version, char *buffer, int len) { + if (version < 100000) { + slprintf(buffer, len, "%d.%d.%d", version/10000, version/100%100, version%100); + } else { /* since version 10 */ + slprintf(buffer, len, "%d.%d", version/10000, version%100); + } +} /* clear result object associated with a result handle */ void php_pqres_clear(PGresult *r) { diff --git a/src/php_pq_misc.h b/src/php_pq_misc.h index 34ab6c1..d86beeb 100644 --- a/src/php_pq_misc.h +++ b/src/php_pq_misc.h @@ -33,6 +33,8 @@ extern PGresult *php_pq_exec_params(PGconn *conn, const char *command, int nPara extern PGresult *php_pq_prepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes); extern PGresult *php_pq_exec_prepared(PGconn *conn, const char *stmtName, int nParams, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat); +/* convert version to string */ +extern void php_pq_version_to_string(int version, char *buffer, int len); /* trim LF from EOL */ extern char *php_pq_rtrim(char *e); diff --git a/src/php_pq_module.c b/src/php_pq_module.c index 3d6dacb..7385b0b 100644 --- a/src/php_pq_module.c +++ b/src/php_pq_module.c @@ -105,11 +105,7 @@ static PHP_MINFO_FUNCTION(pq) php_info_print_table_header(3, "Used Library", "Compiled", "Linked"); #ifdef HAVE_PQLIBVERSION libpq_v = PQlibVersion(); - if (libpq_v < 100000) { - slprintf(libpq_version, sizeof(libpq_version), "%d.%d.%d", libpq_v/10000, libpq_v/100%100, libpq_v%100); - } else { - slprintf(libpq_version, sizeof(libpq_version), "%d.%d", libpq_v/10000, libpq_v%100); - } + php_pq_version_to_string(libpq_v, libpq_version, sizeof(libpq_version)); #endif php_info_print_table_row(3, "libpq", PHP_PQ_LIBVERSION, libpq_version); php_info_print_table_end(); diff --git a/src/php_pqconn.c b/src/php_pqconn.c index 32cc526..0e9e32f 100644 --- a/src/php_pqconn.c +++ b/src/php_pqconn.c @@ -495,13 +495,8 @@ static void php_pqconn_object_write_def_auto_conv(void *o, zval *value) 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); - } + php_pq_version_to_string(PQlibVersion(), ver, sizeof(ver)); RETVAL_STRING(ver); } #endif @@ -518,13 +513,8 @@ 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); - } + php_pq_version_to_string(PQserverVersion(obj->intern->conn), ver, sizeof(ver)); RETVAL_STRING(ver); } #endif