From: Michael Wallner Date: Tue, 17 May 2016 09:48:53 +0000 (+0200) Subject: Added pq\Result::$diag, listing PQresultErrorField details X-Git-Tag: v1.1.0~6 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=0cfa9a40013ceab9edd0a8bcf4189e4be9c1878c;p=m6w6%2Fext-pq Added pq\Result::$diag, listing PQresultErrorField details Closes issue #14 --- diff --git a/package.xml b/package.xml index 1bc9f9a..70a4eca 100644 --- a/package.xml +++ b/package.xml @@ -35,10 +35,10 @@ daverandom@php.net yes - 2016-05-03 + 2016-05-17 - 1.0.2dev - 1.0.0 + 1.1.0dev + 1.1.0 stable @@ -46,7 +46,7 @@ BSD-2-Clause diff --git a/php_pq.h b/php_pq.h index 2ec7b70..add67c2 100644 --- a/php_pq.h +++ b/php_pq.h @@ -14,7 +14,7 @@ #ifndef PHP_PQ_H #define PHP_PQ_H -#define PHP_PQ_VERSION "1.0.2dev" +#define PHP_PQ_VERSION "1.1.0dev" #ifdef PHP_WIN32 # define PHP_PQ_API __declspec(dllexport) diff --git a/src/php_pqres.c b/src/php_pqres.c index 6e1fa68..6cc9954 100644 --- a/src/php_pqres.c +++ b/src/php_pqres.c @@ -511,6 +511,45 @@ static void php_pqres_object_read_error_message(zval *object, void *o, zval *ret } } +static void php_pqres_object_read_diag(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqres_object_t *obj = o; + int i; + struct { + char code; + const char *const name; + } diag[] = { + {PG_DIAG_SEVERITY, "severity"}, + {PG_DIAG_SQLSTATE, "sqlstate"}, + {PG_DIAG_MESSAGE_PRIMARY, "message_primary"}, + {PG_DIAG_MESSAGE_DETAIL, "message_detail"}, + {PG_DIAG_MESSAGE_HINT, "message_hint"}, + {PG_DIAG_STATEMENT_POSITION,"statement_position"}, + {PG_DIAG_INTERNAL_POSITION, "internal_position"}, + {PG_DIAG_INTERNAL_QUERY, "internal_query"}, + {PG_DIAG_CONTEXT, "context"}, + {PG_DIAG_SCHEMA_NAME, "schema_name"}, + {PG_DIAG_TABLE_NAME, "table_name"}, + {PG_DIAG_COLUMN_NAME, "column_name"}, + {PG_DIAG_DATATYPE_NAME, "datatype_name"}, + {PG_DIAG_CONSTRAINT_NAME, "constraint_name"}, + {PG_DIAG_SOURCE_FILE, "source_file"}, + {PG_DIAG_SOURCE_LINE, "source_line"}, + {PG_DIAG_SOURCE_FUNCTION, "source_function"} + }; + + array_init_size(return_value, 32); + for (i = 0; i < sizeof(diag)/sizeof(diag[0]); ++i) { + char *value = PQresultErrorField(obj->intern->res, diag[i].code); + + if (value) { + add_assoc_string(return_value, diag[i].name, value, 1); + } else { + add_assoc_null(return_value, diag[i].name); + } + } +} + static void php_pqres_object_read_num_rows(zval *object, void *o, zval *return_value TSRMLS_DC) { php_pqres_object_t *obj = o; @@ -1195,6 +1234,10 @@ PHP_MINIT_FUNCTION(pqres) ph.read = php_pqres_object_read_error_message; zend_hash_add(&php_pqres_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_null(php_pqres_class_entry, ZEND_STRL("diag"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqres_object_read_diag; + zend_hash_add(&php_pqres_object_prophandlers, "diag", sizeof("diag"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_long(php_pqres_class_entry, ZEND_STRL("numRows"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); ph.read = php_pqres_object_read_num_rows; zend_hash_add(&php_pqres_object_prophandlers, "numRows", sizeof("numRows"), (void *) &ph, sizeof(ph), NULL);