Support async pgsql connections and non-blocking queries

- New functions (each accepts a pgsql $connection resource):

  . pg_connect_poll
  . pg_socket
  . pg_consume_input
  . pg_flush

- Modified functions

  The following functions now additionally return zero if the
  underlying socket is set to non-blocking mode and the send
  operation does not complete immediately. Previously these
  functions returned only boolean TRUE/FALSE and blocked
  execution while polling until all data was sent:

  . pg_send_execute
  . pg_send_prepare
  . pg_send_query
  . pg_send_query_params

- New constants

  Used with pg_connect() to initiate an asynchronous connection
  attempt:

  . PGSQL_CONNECT_ASYNC

  Used with pg_connection_status() to determine the current state
  of an async connection attempt:

  . PGSQL_CONNECTION_STARTED
  . PGSQL_CONNECTION_MADE
  . PGSQL_CONNECTION_AWAITING_RESPONSE
  . PGSQL_CONNECTION_AUTH_OK
  . PGSQL_CONNECTION_SSL_STARTUP
  . PGSQL_CONNECTION_SETENV

  Used with pg_connect_poll() to determine the result of an
  async connection attempt:

  . PGSQL_POLLING_FAILED
  . PGSQL_POLLING_READING
  . PGSQL_POLLING_WRITING
  . PGSQL_POLLING_OK
  . PGSQL_POLLING_ACTIVE

- Polling via returned pg_socket() stream

  pg_socket() returns a read-only socket stream that may be
  cast to a file descriptor for select (and similar) polling
  operations. Blocking behavior of the pgsql connection socket
  can be controlled by calling stream_set_blocking() on the
  stream returned by pg_socket().
This commit is contained in:
Daniel Lowrey 2014-03-13 12:55:33 -06:00
parent 8cde747336
commit 2ee4c987e6
7 changed files with 762 additions and 99 deletions

View file

@ -67,6 +67,7 @@ PHP_MINFO_FUNCTION(pgsql);
/* connection functions */
PHP_FUNCTION(pg_connect);
PHP_FUNCTION(pg_pconnect);
PHP_FUNCTION(pg_connect_poll);
PHP_FUNCTION(pg_close);
PHP_FUNCTION(pg_connection_reset);
PHP_FUNCTION(pg_connection_status);
@ -134,6 +135,9 @@ PHP_FUNCTION(pg_field_is_null);
PHP_FUNCTION(pg_field_table);
/* async message functions */
PHP_FUNCTION(pg_get_notify);
PHP_FUNCTION(pg_socket);
PHP_FUNCTION(pg_consume_input);
PHP_FUNCTION(pg_flush);
PHP_FUNCTION(pg_get_pid);
/* error message functions */
PHP_FUNCTION(pg_result_error);
@ -191,6 +195,7 @@ PHP_FUNCTION(pg_select);
/* connection options - ToDo: Add async connection option */
#define PGSQL_CONNECT_FORCE_NEW (1<<1)
#define PGSQL_CONNECT_ASYNC (1<<2)
/* php_pgsql_convert options */
#define PGSQL_CONV_IGNORE_DEFAULT (1<<1) /* Do not use DEAFULT value by removing field from returned array */
#define PGSQL_CONV_FORCE_NULL (1<<2) /* Convert to NULL if string is null string */
@ -222,6 +227,13 @@ static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_typ
static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type);
static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC);
static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count TSRMLS_DC);
static int php_pgsql_fd_close(php_stream *stream, int close_handle TSRMLS_DC);
static int php_pgsql_fd_flush(php_stream *stream TSRMLS_DC);
static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret TSRMLS_DC);
typedef enum _php_pgsql_data_type {
/* boolean */
PG_BOOL,
@ -284,6 +296,18 @@ typedef struct _php_pgsql_notice {
size_t len;
} php_pgsql_notice;
static php_stream_ops php_stream_pgsql_fd_ops = {
php_pgsql_fd_write,
php_pgsql_fd_read,
php_pgsql_fd_close,
php_pgsql_fd_flush,
"PostgreSQL link",
NULL, /* seek */
php_pgsql_fd_cast, /* cast */
NULL, /* stat */
php_pgsql_fd_set_option
};
ZEND_BEGIN_MODULE_GLOBALS(pgsql)
long default_link; /* default link when connection is omitted */
long num_links,num_persistent;