This is a patch for 5.2.x to return a select()-able variable for a given PostgreSQL connection.

PG allows ASync queries, pg_send_query, and notifications, pg_get_notify. Currently (afaik), there is no way to add a PostgreSQL
socket to the event notification array in used with stream_select();

usage:

$db=pg_connect(...);
$db_socket = pg_get_socket($db);
pg_send_query("select 1,pg_sleep(3)");
stream_select($db_socket, ...);
pg_get_result($db);

feature was requested Jan 2005 on
the pg_get_result page of the main English manual.


-Charles

diff -c pgsql_old/php_pgsql.h pgsql/php_pgsql.h
*** pgsql_old/php_pgsql.h       2007-12-30 23:20:10.000000000 -0800
--- pgsql/php_pgsql.h   2009-01-23 12:26:02.000000000 -0800
***************
*** 129,134 ****
--- 129,135 ----
  /* async message functions */
  PHP_FUNCTION(pg_get_notify);
  PHP_FUNCTION(pg_get_pid);
+ PHP_FUNCTION(pg_get_socket);
  /* error message functions */
  PHP_FUNCTION(pg_result_error);
  #if HAVE_PQRESULTERRORFIELD
diff -c pgsql_old/pgsql.c pgsql/pgsql.c
*** pgsql_old/pgsql.c   2008-10-15 17:45:21.000000000 -0700
--- pgsql/pgsql.c       2009-01-23 12:28:03.000000000 -0800
***************
*** 164,169 ****
--- 164,170 ----
        /* async message function */
        PHP_FE(pg_get_notify,   NULL)
        PHP_FE(pg_get_pid,      NULL)
+       PHP_FE(pg_get_socket,   NULL)
        /* error message functions */
        PHP_FE(pg_result_error, NULL)
  #if HAVE_PQRESULTERRORFIELD
***************
*** 4405,4410 ****
--- 4406,4441 ----
  }
  /* }}} */
  
+ /* {{{ proto resource pg_socket(resource connection)
+    return connection socket */
+ PHP_FUNCTION(pg_get_socket)
+ {
+         zval **pgsql_link;
+         int id = -1;
+         PGconn *pgsql;
+         int fd;
+         php_stream *stream;
+         php_socket_t *sock;
+         if (zend_get_parameters_ex(1, &pgsql_link) == FAILURE) {
+                 WRONG_PARAM_COUNT;
+         }
+         
+         if (pgsql_link == NULL && id == -1) {
+                 RETURN_FALSE;
+         }
+         
+         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL 
link", le_link, le_plink);
+ 
+         fd = PQsocket(pgsql);
+ 
+         stream = php_stream_sock_open_from_socket(fd,NULL);
+  
+         if(stream == NULL) RETURN_FALSE;
+ 
+         ZVAL_RESOURCE(return_value, php_stream_get_resource_id(stream));
+ }
+ /* }}} */
+ 
  /* {{{ php_pgsql_meta_data
   * TODO: Add meta_data cache for better performance
   */

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to