edink           Thu Mar 16 14:58:56 2006 UTC

  Modified files:              
    /php-src/ext/pgsql  pgsql.c php_pgsql.h 
  Log:
  Added pg_field_table() as per req: #36750
  
http://cvs.php.net/viewcvs.cgi/php-src/ext/pgsql/pgsql.c?r1=1.344&r2=1.345&diff_format=u
Index: php-src/ext/pgsql/pgsql.c
diff -u php-src/ext/pgsql/pgsql.c:1.344 php-src/ext/pgsql/pgsql.c:1.345
--- php-src/ext/pgsql/pgsql.c:1.344     Wed Mar  8 00:43:28 2006
+++ php-src/ext/pgsql/pgsql.c   Thu Mar 16 14:58:56 2006
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
  */
  
-/* $Id: pgsql.c,v 1.344 2006/03/08 00:43:28 pajoye Exp $ */
+/* $Id: pgsql.c,v 1.345 2006/03/16 14:58:56 edink Exp $ */
 
 #include <stdlib.h>
 
@@ -151,6 +151,7 @@
        PHP_FE(pg_field_type_oid, NULL)
        PHP_FE(pg_field_prtlen, NULL)
        PHP_FE(pg_field_is_null,NULL)
+       PHP_FE(pg_field_table,  NULL)
        /* async message function */
        PHP_FE(pg_get_notify,   NULL)
        PHP_FE(pg_get_pid,      NULL)
@@ -1694,6 +1695,95 @@
 }
 /* }}} */                      
 
+/* {{{ proto mixed pg_field_table(resource result, int field_number[, bool 
oid_only])
+   Returns the name of the table field belongs to, or table's oid if oid_only 
is true */
+PHP_FUNCTION(pg_field_table)
+{
+       zval *result;
+       pgsql_result_handle *pg_result;
+       long fnum = -1;
+       zend_bool return_oid = 0;
+       Oid oid;
+       smart_str hash_key = {0};
+       char *table_name;
+       zend_rsrc_list_entry *field_table;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|b!", &result, 
&fnum, &return_oid) == FAILURE) {
+               return;
+       }
+
+       ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, 
"PostgreSQL result", le_result);
+
+       if (fnum < 0 || fnum >= PQnfields(pg_result->result)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset 
specified");
+               RETURN_FALSE;
+       }
+
+       oid = PQftable(pg_result->result, fnum);
+
+       if (InvalidOid == oid) {
+               RETURN_FALSE;
+       }
+
+
+       if (return_oid) {
+               if (oid > LONG_MAX) {
+                       smart_str oidstr = {0};
+                       smart_str_append_unsigned(&oidstr, oid);
+                       smart_str_0(&oidstr);
+                       RETURN_STRINGL(oidstr.c, oidstr.len, 0);
+               } else {
+                       RETURN_LONG((long)oid);
+               }
+       }
+
+       /* try to lookup the table name in the resource list */
+       smart_str_appends(&hash_key, "pgsql_table_oid_");
+       smart_str_append_unsigned(&hash_key, oid);
+       smart_str_0(&hash_key);
+
+       if (zend_hash_find(&EG(regular_list), hash_key.c, hash_key.len+1, (void 
**) &field_table) == SUCCESS) {
+               smart_str_free(&hash_key);
+               RETURN_STRING((char *)field_table->ptr, 1);
+       } else { /* Not found, lookup by querying PostgreSQL system tables */
+               PGresult *tmp_res;
+               smart_str querystr = {0};
+               zend_rsrc_list_entry new_field_table;
+
+               smart_str_appends(&querystr, "select relname from pg_class 
where oid=");
+               smart_str_append_unsigned(&querystr, oid);
+               smart_str_0(&querystr);
+
+
+               if ((tmp_res = PQexec(pg_result->conn, querystr.c)) == NULL || 
PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
+                       if (tmp_res) {
+                               PQclear(tmp_res);
+                       }
+                       smart_str_free(&querystr);
+                       smart_str_free(&hash_key);
+                       RETURN_FALSE;
+               }
+
+               smart_str_free(&querystr);
+
+               if ((table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
+                       PQclear(tmp_res);
+                       smart_str_free(&hash_key);
+                       RETURN_FALSE;
+               }
+
+               Z_TYPE(new_field_table) = le_string;
+               new_field_table.ptr = estrdup(table_name);
+               zend_hash_update(&EG(regular_list), hash_key.c, hash_key.len+1, 
(void *) &new_field_table, sizeof(zend_rsrc_list_entry), NULL);
+
+               smart_str_free(&hash_key);
+               PQclear(tmp_res);
+               RETURN_STRING(table_name, 1);
+       }
+
+}
+/* }}} */                      
+
 #define PHP_PG_FIELD_NAME 1
 #define PHP_PG_FIELD_SIZE 2
 #define PHP_PG_FIELD_TYPE 3
http://cvs.php.net/viewcvs.cgi/php-src/ext/pgsql/php_pgsql.h?r1=1.74&r2=1.75&diff_format=u
Index: php-src/ext/pgsql/php_pgsql.h
diff -u php-src/ext/pgsql/php_pgsql.h:1.74 php-src/ext/pgsql/php_pgsql.h:1.75
--- php-src/ext/pgsql/php_pgsql.h:1.74  Sun Jan  1 13:09:53 2006
+++ php-src/ext/pgsql/php_pgsql.h       Thu Mar 16 14:58:56 2006
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
  
-/* $Id: php_pgsql.h,v 1.74 2006/01/01 13:09:53 sniper Exp $ */
+/* $Id: php_pgsql.h,v 1.75 2006/03/16 14:58:56 edink Exp $ */
 
 #ifndef PHP_PGSQL_H
 #define PHP_PGSQL_H
@@ -125,6 +125,7 @@
 PHP_FUNCTION(pg_field_type_oid);
 PHP_FUNCTION(pg_field_prtlen);
 PHP_FUNCTION(pg_field_is_null);
+PHP_FUNCTION(pg_field_table);
 /* async message functions */
 PHP_FUNCTION(pg_get_notify);
 PHP_FUNCTION(pg_get_pid);

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to