Added pq\Result::$diag, listing PQresultErrorField details
authorMichael Wallner <mike@php.net>
Tue, 17 May 2016 09:48:53 +0000 (11:48 +0200)
committerMichael Wallner <mike@php.net>
Tue, 17 May 2016 09:48:53 +0000 (11:48 +0200)
Closes issue #14

package.xml
php_pq.h
src/php_pqres.c

index 1bc9f9a..70a4eca 100644 (file)
   <email>daverandom@php.net</email>
   <active>yes</active>
  </developer>
- <date>2016-05-03</date>
+ <date>2016-05-17</date>
  <version>
-  <release>1.0.2dev</release>
-  <api>1.0.0</api>
+  <release>1.1.0dev</release>
+  <api>1.1.0</api>
  </version>
  <stability>
   <release>stable</release>
@@ -46,7 +46,7 @@
  </stability>
  <license uri="http://copyfree.org/content/standard/licenses/2bsd/license.txt">BSD-2-Clause</license>
  <notes><![CDATA[
-*
+* Added public readonly array pq\Result::$diag property, listing PQresultErrorField details (gh-issue #14)
 ]]></notes>
  <contents>
   <dir name="/">
index 2ec7b70..add67c2 100644 (file)
--- 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)
index 6e1fa68..6cc9954 100644 (file)
@@ -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);