Add $query and $types properties to Statement
authorChris Wright <github@daverandom.com>
Fri, 12 Dec 2014 00:18:50 +0000 (00:18 +0000)
committerChris Wright <github@daverandom.com>
Fri, 12 Dec 2014 01:47:01 +0000 (01:47 +0000)
src/php_pqstm.c
tests/stm_props001.phpt [new file with mode: 0644]

index 178342a640d288c54ac097b3c68d6905037bec67..10d57686ae9d5c91fc6db135c00b9c84ef776243 100644 (file)
@@ -132,6 +132,31 @@ static void php_pqstm_object_read_connection(zval *object, void *o, zval *return
        php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
 }
 
+static void php_pqstm_object_read_query(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+       php_pqstm_object_t *obj = o;
+
+       RETVAL_STRING(obj->intern->query, 1);
+}
+
+static void php_pqstm_object_read_types(zval *object, void *o, zval *return_value)
+{
+       int i;
+       HashTable *ht;
+       php_pqstm_object_t *obj;
+
+       obj = (php_pqstm_object_t *)o;
+       ht = (HashTable *)emalloc(sizeof(HashTable));
+
+       zend_hash_init(ht, obj->intern->params->type.count, NULL, ZVAL_PTR_DTOR, 0);
+       Z_TYPE_P(return_value) = IS_ARRAY;
+       Z_ARRVAL_P(return_value) = ht;
+
+       for (i = 0; i < obj->intern->params->type.count; i++) {
+               add_next_index_long(return_value, (long)obj->intern->params->type.oids[i]);
+       }
+}
+
 php_pqstm_t *php_pqstm_init(php_pqconn_object_t *conn, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
 {
        php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
@@ -491,6 +516,14 @@ PHP_MINIT_FUNCTION(pqstm)
        ph.read = php_pqstm_object_read_connection;
        zend_hash_add(&php_pqstm_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
 
+       zend_declare_property_null(php_pqstm_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqstm_object_read_query;
+       zend_hash_add(&php_pqstm_object_prophandlers, "query", sizeof("query"), (void *) &ph, sizeof(ph), NULL);
+
+       zend_declare_property_null(php_pqstm_class_entry, ZEND_STRL("types"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       ph.read = php_pqstm_object_read_types;
+       zend_hash_add(&php_pqstm_object_prophandlers, "types", sizeof("types"), (void *) &ph, sizeof(ph), NULL);
+
        return SUCCESS;
 }
 
diff --git a/tests/stm_props001.phpt b/tests/stm_props001.phpt
new file mode 100644 (file)
index 0000000..33a733f
--- /dev/null
@@ -0,0 +1,32 @@
+--TEST--
+Statement properties
+--SKIPIF--
+<?php
+include "_skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+include "_setup.inc";
+
+$n = 'props';
+$q = 'SELECT $1, $2, $3';
+$t = array(pq\Types::BOOL, pq\Types::INT4, pq\Types::TEXT);
+
+$c = new pq\Connection(PQ_DSN);
+$s = new pq\Statement($c, $n, $q, $t);
+
+var_dump($c === $s->connection);
+var_dump($n === $s->name);
+var_dump($q === $s->query);
+var_dump($t === $s->types);
+
+?>
+Done
+--EXPECT--
+Test
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+Done