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 1bc9f9ac548e1065f9e902dbb349f493d713bbaf..70a4eca8d6e4cb3db7573677d554df1375fa58f9 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 2ec7b709b47046016c138dd63af09b9cf62adf96..add67c26c5787b1a2dec8456a5719298e09a1ea0 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 6e1fa681090aa1f476f1362e6b26e80ae277d03c..6cc99545c5e0e259f8ca684833f7acac98801304 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);