typedef int STATUS; /* SUCCESS/FAILURE */
-static char *PHP_PQerrorMessage(PGconn *c) {
- char *e = PQerrorMessage(c);
+static char *rtrim(char *e) {
size_t l = strlen(e);
while (l-- > 0 && e[l] == '\n') {
e[l] = '\0';
}
-
return e;
}
+#define PHP_PQerrorMessage(c) rtrim(PQerrorMessage((c)))
+#define PHP_PQresultErrorMessage(r) rtrim(PQresultErrorMessage((r)))
+
+static int php_pqconn_event(PGEventId id, void *e, void *data);
+
+#define PHP_PQclear(_r) \
+ do { \
+ zval *_resinszv = PQresultInstanceData((_r), php_pqconn_event); \
+ if (!_resinszv) PQclear((_r)); \
+ } while (0)
+
/*
ZEND_DECLARE_MODULE_GLOBALS(pq)
*/
static zend_class_entry *php_pqcancel_class_entry;
static zend_class_entry *php_pqevent_class_entry;
static zend_class_entry *php_pqlob_class_entry;
+static zend_class_entry *php_pqcopy_class_entry;
static zend_object_handlers php_pqconn_object_handlers;
static zend_object_handlers php_pqtypes_object_handlers;
static zend_object_handlers php_pqcancel_object_handlers;
static zend_object_handlers php_pqevent_object_handlers;
static zend_object_handlers php_pqlob_object_handlers;
+static zend_object_handlers php_pqcopy_object_handlers;
typedef struct php_pq_callback {
zend_fcall_info fci;
typedef struct php_pqtxn {
php_pqconn_object_t *conn;
php_pqtxn_isolation_t isolation;
+ unsigned savepoint;
unsigned readonly:1;
unsigned deferrable:1;
} php_pqtxn_t;
php_pqlob_t *intern;
} php_pqlob_object_t;
+typedef enum php_pqcopy_direction {
+ PHP_PQCOPY_FROM_STDIN,
+ PHP_PQCOPY_TO_STDOUT
+} php_pqcopy_direction_t;
+
+typedef enum php_pqcopy_status {
+ PHP_PQCOPY_FAIL,
+ PHP_PQCOPY_CONT,
+ PHP_PQCOPY_DONE
+} php_pqcopy_status_t;
+
+typedef struct php_pqcopy {
+ php_pqcopy_direction_t direction;
+ char *expression;
+ char *options;
+ php_pqconn_object_t *conn;
+} php_pqcopy_t;
+
+typedef struct php_pqcopy_object {
+ zend_object zo;
+ zend_object_value zv;
+ HashTable *prophandler;
+ php_pqcopy_t *intern;
+} php_pqcopy_object_t;
+
static HashTable php_pqconn_object_prophandlers;
static HashTable php_pqtypes_object_prophandlers;
static HashTable php_pqres_object_prophandlers;
static HashTable php_pqcancel_object_prophandlers;
static HashTable php_pqevent_object_prophandlers;
static HashTable php_pqlob_object_prophandlers;
+static HashTable php_pqcopy_object_prophandlers;
typedef void (*php_pq_object_prophandler_func_t)(zval *object, void *o, zval *return_value TSRMLS_DC);
return SUCCESS;
}
-static zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type TSRMLS_DC)
+static zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type, zval **data_ptr TSRMLS_DC)
{
- zval *data;
+ zval *data = NULL;
int c, cols;
- MAKE_STD_ZVAL(data);
- if (PHP_PQRES_FETCH_OBJECT == fetch_type) {
- object_init(data);
- } else {
- array_init(data);
+ if (data_ptr) {
+ data = *data_ptr;
+ }
+ if (!data) {
+ MAKE_STD_ZVAL(data);
+ if (PHP_PQRES_FETCH_OBJECT == fetch_type) {
+ object_init(data);
+ } else {
+ array_init(data);
+ }
+ if (data_ptr) {
+ *data_ptr = data;
+ }
}
for (c = 0, cols = PQnfields(res); c < cols; ++c) {
if (iter->current_val) {
zval_ptr_dtor(&iter->current_val);
}
- iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type TSRMLS_CC);
+ iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type, NULL TSRMLS_CC);
*data_ptr = &iter->current_val;
}
NULL
};
+static int php_pqres_count_elements(zval *object, long *count TSRMLS_DC)
+{
+ php_pqres_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
+
+ if (obj->intern) {
+ *count = (long) PQntuples(obj->intern->res);
+ return SUCCESS;
+ } else {
+ return FAILURE;
+ }
+}
+
static STATUS php_pqres_success(PGresult *res TSRMLS_DC)
{
switch (PQresultStatus(res)) {
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PQresultErrorMessage(res));
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PHP_PQresultErrorMessage(res));
return FAILURE;
default:
return SUCCESS;
efree(obj);
}
-static int php_pqconn_event(PGEventId id, void *e, void *data);
-
static void php_pqres_object_free(void *o TSRMLS_DC)
{
php_pqres_object_t *obj = o;
if (obj->intern->res) {
zval *res = PQresultInstanceData(obj->intern->res, php_pqconn_event);
if (res) {
- PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL);
+ if (1 == Z_REFCOUNT_P(res)) {
+ PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL);
+ }
zval_ptr_dtor(&res);
} else {
PQclear(obj->intern->res);
efree(obj);
}
+static void php_pqcopy_object_free(void *o TSRMLS_DC)
+{
+ php_pqcopy_object_t *obj = o;
+
+ if (obj->intern) {
+ efree(obj->intern->expression);
+ efree(obj->intern->options);
+ php_pq_object_delref(obj->intern->conn TSRMLS_CC);
+ efree(obj->intern);
+ obj->intern = NULL;
+ }
+ zend_object_std_dtor((zend_object *) o TSRMLS_CC);
+ efree(obj);
+}
+
static zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_pqconn_t *intern, php_pqconn_object_t **ptr TSRMLS_DC)
{
php_pqconn_object_t *o;
return o->zv;
}
+static zend_object_value php_pqcopy_create_object_ex(zend_class_entry *ce, php_pqcopy_t *intern, php_pqcopy_object_t **ptr TSRMLS_DC)
+{
+ php_pqcopy_object_t *o;
+
+ o = ecalloc(1, sizeof(*o));
+ zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
+ object_properties_init((zend_object *) o, ce);
+ o->prophandler = &php_pqcopy_object_prophandlers;
+
+ if (ptr) {
+ *ptr = o;
+ }
+
+ if (intern) {
+ o->intern = intern;
+ }
+
+ o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcopy_object_free, NULL TSRMLS_CC);
+ o->zv.handlers = &php_pqcopy_object_handlers;
+
+ return o->zv;
+}
+
static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC)
{
return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
return php_pqlob_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
}
+static zend_object_value php_pqcopy_create_object(zend_class_entry *class_type TSRMLS_DC)
+{
+ return php_pqcopy_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
+}
+
static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
{
php_pq_object_prophandler_t *ph = p;
php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC);
- if (!Z_REFCOUNT_P(property)) {
+ if (1||!Z_REFCOUNT_P(property)) {
Z_ADDREF_P(property);
}
zend_hash_add(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
static void php_pqres_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
{
php_pqres_object_t *obj = o;
- char *error = PQresultErrorMessage(obj->intern->res);
+ char *error = PHP_PQresultErrorMessage(obj->intern->res);
if (error) {
RETVAL_STRING(error, 1);
if (res) {
php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
}
}
if (res) {
php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
}
}
if (res) {
php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
}
}
RETVAL_LONG(obj->intern->loid);
}
+static void php_pqcopy_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+ php_pqcopy_object_t *obj = o;
+
+ php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
+}
+
+static void php_pqcopy_object_read_direction(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+ php_pqcopy_object_t *obj = o;
+
+ RETVAL_LONG(obj->intern->direction);
+}
+
+static void php_pqcopy_object_read_expression(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+ php_pqcopy_object_t *obj = o;
+
+ RETURN_STRING(obj->intern->expression, 1);
+}
+
+static void php_pqcopy_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
+{
+ php_pqcopy_object_t *obj = o;
+
+ RETURN_STRING(obj->intern->options, 1);
+}
+
static zend_class_entry *ancestor(zend_class_entry *ce) {
while (ce->parent) {
ce = ce->parent;
array_init(args);
php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
add_next_index_zval(args, connection);
- add_next_index_string(args, PQresultErrorMessage(res), 1);
+ add_next_index_string(args, PHP_PQresultErrorMessage(res), 1);
zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
zval_ptr_dtor(&args);
}
} else {
RETVAL_FALSE;
}
- PQclear(res);
+ PHP_PQclear(res);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
RETVAL_FALSE;
} else {
RETVAL_FALSE;
}
- PQclear(res);
+ PHP_PQclear(res);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
RETVAL_FALSE;
if (res) {
rv = php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
} else {
rv = FAILURE;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
if (res) {
STATUS rv = php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
return rv;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
int r, rows;
for (r = 0, rows = PQntuples(res); r < rows; ++r) {
- zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT TSRMLS_CC);
+ zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT, NULL TSRMLS_CC);
long oid = atol(PQgetvalue(res, r, 0 ));
char *name = PQgetvalue(res, r, 1);
zend_hash_add(&obj->intern->types, name, strlen(name) + 1, (void *) &row, sizeof(zval *), NULL);
}
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types: %s", PQresultErrorMessage(res));
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types (%s)", PHP_PQresultErrorMessage(res));
}
- PQclear(res);
+ PHP_PQclear(res);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
}
{0}
};
-static zval **php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type TSRMLS_DC)
+static zval *php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type, zval ***row TSRMLS_DC)
{
- zval **row = NULL;
php_pqres_fetch_t orig_fetch;
if (!obj) {
orig_fetch = obj->intern->iter->fetch_type;
obj->intern->iter->fetch_type = fetch_type;
if (SUCCESS == obj->intern->iter->zi.funcs->valid((zend_object_iterator *) obj->intern->iter TSRMLS_CC)) {
- obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, &row TSRMLS_CC);
+ obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, row TSRMLS_CC);
obj->intern->iter->zi.funcs->move_forward((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
}
obj->intern->iter->fetch_type = orig_fetch;
- return row ? row : NULL;
+ return *row ? **row : NULL;
}
ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_row, 0, 0, 0)
static PHP_METHOD(pqres, fetchRow) {
zend_error_handling zeh;
php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
- long fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+ long fetch_type = -1;
zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_type)) {
- zval **row = php_pqres_iteration(getThis(), obj, fetch_type TSRMLS_CC);
+ if (obj->intern) {
+ zval **row = NULL;
+
+ if (fetch_type == -1) {
+ fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+ }
+ php_pqres_iteration(getThis(), obj, fetch_type, &row TSRMLS_CC);
- if (row) {
- RETVAL_ZVAL(*row, 1, 0);
+ if (row) {
+ RETVAL_ZVAL(*row, 1, 0);
+ }
} else {
- RETVAL_FALSE;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
}
}
zend_restore_error_handling(&zeh TSRMLS_CC);
zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col)) {
php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
- zval **row = php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0 TSRMLS_CC);
- if (row) {
- zval **col = column_at(*row, fetch_col TSRMLS_CC);
+ if (obj->intern) {
+ zval **row = NULL;
- if (col) {
- RETVAL_ZVAL(*col, 1, 0);
+ php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC);
+
+ if (row) {
+ zval **col = column_at(*row, fetch_col TSRMLS_CC);
+
+ if (col) {
+ RETVAL_ZVAL(*col, 1, 0);
+ }
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+typedef struct php_pqres_col {
+ char *name;
+ int num;
+} php_pqres_col_t;
+
+static int apply_to_col(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+{
+ zval **c = p;
+ php_pqres_object_t *obj = va_arg(argv, php_pqres_object_t *);
+ php_pqres_col_t *col, **cols = va_arg(argv, php_pqres_col_t **);
+ STATUS *rv = va_arg(argv, STATUS *);
+ long index = -1;
+ char *name = NULL;
+
+ switch (Z_TYPE_PP(c)) {
+ default:
+ convert_to_string(*c);
+ /* no break */
+ case IS_STRING:
+ if (!is_numeric_string(Z_STRVAL_PP(c), Z_STRLEN_PP(c), &index, NULL, 0)) {
+ name = Z_STRVAL_PP(c);
+ }
+ break;
+ case IS_LONG:
+ index = Z_LVAL_PP(c);
+ break;
+ }
+
+ col = *cols;
+ if (name) {
+ col->name = name;
+ col->num = PQfnumber(obj->intern->res, name);
+ } else {
+ col->name = PQfname(obj->intern->res, index);
+ col->num = index;
+ }
+
+ if (!col->name) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column at index %ld", index);
+ *rv = FAILURE;
+ return ZEND_HASH_APPLY_STOP;
+ }
+ if (col->num == -1) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column with name '%s'", name);
+ *rv = FAILURE;
+ return ZEND_HASH_APPLY_STOP;
+ }
+
+ ++*cols;
+ return ZEND_HASH_APPLY_KEEP;
+}
+
+static php_pqres_col_t *php_pqres_convert_to_cols(php_pqres_object_t *obj, HashTable *ht TSRMLS_DC)
+{
+ php_pqres_col_t *tmp, *cols = ecalloc(zend_hash_num_elements(ht), sizeof(*cols));
+ STATUS rv = SUCCESS;
+
+ tmp = cols;
+ zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_col, 2, obj, &tmp, &rv);
+
+ if (SUCCESS == rv) {
+ return cols;
+ } else {
+ efree(cols);
+ return NULL;
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqres_map, 0, 0, 0)
+ ZEND_ARG_INFO(0, keys)
+ ZEND_ARG_INFO(0, vals)
+ ZEND_ARG_INFO(0, fetch_type)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqres, map) {
+ zend_error_handling zeh;
+ zval *zkeys = 0, *zvals = 0;
+ long fetch_type = -1;
+
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/!z/!l", &zkeys, &zvals, &fetch_type)) {
+ php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ int ks = 0, vs = 0;
+ php_pqres_col_t def = {PQfname(obj->intern->res, 0), 0}, *keys = NULL, *vals = NULL;
+
+ if (zkeys) {
+ convert_to_array(zkeys);
+
+ if ((ks = zend_hash_num_elements(Z_ARRVAL_P(zkeys)))) {
+ keys = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zkeys) TSRMLS_CC);
+ } else {
+ ks = 1;
+ keys = &def;
+ }
} else {
- RETVAL_FALSE;
+ ks = 1;
+ keys = &def;
+ }
+ if (zvals) {
+ convert_to_array(zvals);
+
+ if ((vs = zend_hash_num_elements(Z_ARRVAL_P(zvals)))) {
+ vals = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zvals) TSRMLS_CC);
+ }
+ }
+
+ if (fetch_type == -1) {
+ fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
+ }
+
+ if (keys) {
+ int rows, r;
+ zval **cur;
+
+ switch (fetch_type) {
+ case PHP_PQRES_FETCH_ARRAY:
+ case PHP_PQRES_FETCH_ASSOC:
+ array_init(return_value);
+ break;
+ case PHP_PQRES_FETCH_OBJECT:
+ object_init(return_value);
+ break;
+ }
+ for (r = 0, rows = PQntuples(obj->intern->res); r < rows; ++r) {
+ int k, v;
+
+ cur = &return_value;
+ for (k = 0; k < ks; ++k) {
+ char *key = PQgetvalue(obj->intern->res, r, keys[k].num);
+ int len = PQgetlength(obj->intern->res, r, keys[k].num);
+
+ if (SUCCESS != zend_symtable_find(HASH_OF(*cur), key, len + 1, (void *) &cur)) {
+ zval *tmp;
+
+ MAKE_STD_ZVAL(tmp);
+ switch (fetch_type) {
+ case PHP_PQRES_FETCH_ARRAY:
+ case PHP_PQRES_FETCH_ASSOC:
+ array_init(tmp);
+ break;
+ case PHP_PQRES_FETCH_OBJECT:
+ object_init(tmp);
+ break;
+ }
+ if (SUCCESS != zend_symtable_update(HASH_OF(*cur), key, len + 1, (void *) &tmp, sizeof(zval *), (void *) &cur)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create map");
+ goto err;
+ }
+ }
+ }
+ if (vals && vs) {
+ for (v = 0; v < vs; ++v) {
+ char *val = PQgetvalue(obj->intern->res, r, vals[v].num);
+ int len = PQgetlength(obj->intern->res, r, vals[v].num);
+
+ switch (fetch_type) {
+ case PHP_PQRES_FETCH_ARRAY:
+ add_index_stringl(*cur, vals[v].num, val, len, 1);
+ break;
+ case PHP_PQRES_FETCH_ASSOC:
+ add_assoc_stringl(*cur, vals[v].name, val, len, 1);
+ break;
+ case PHP_PQRES_FETCH_OBJECT:
+ add_property_stringl(*cur, vals[v].name, val, len, 1);
+ break;
+ }
+ }
+ } else {
+ php_pqres_row_to_zval(obj->intern->res, r, fetch_type, cur TSRMLS_CC);
+ }
+ }
+ }
+
+ err:
+ if (keys && keys != &def) {
+ efree(keys);
+ }
+ if (vals) {
+ efree(vals);
}
} else {
- RETVAL_FALSE;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
}
}
zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+ZEND_BEGIN_ARG_INFO_EX(ai_pqres_count, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqres, count) {
+ if (SUCCESS == zend_parse_parameters_none()) {
+ long count;
+
+ if (SUCCESS == php_pqres_count_elements(getThis(), &count TSRMLS_CC)) {
+ RETVAL_LONG(count);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
+ }
+ }
}
static zend_function_entry php_pqres_methods[] = {
PHP_ME(pqres, fetchRow, ai_pqres_fetch_row, ZEND_ACC_PUBLIC)
PHP_ME(pqres, fetchCol, ai_pqres_fetch_col, ZEND_ACC_PUBLIC)
+ PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC)
+ PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC)
{0}
};
zend_restore_error_handling(&zeh TSRMLS_CC);
}
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, savepoint) {
+ zend_error_handling zeh;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ char *cmd;
+ PGresult *res;
+
+ spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint);
+ res = PQexec(obj->intern->conn->intern->conn, cmd);
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+
+ efree(cmd);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, savepointAsync) {
+ zend_error_handling zeh;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ char *cmd;
+
+ spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint);
+ if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+
+ efree(cmd);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit, 0, 0, 0)
ZEND_END_ARG_INFO();
static PHP_METHOD(pqtxn, commit) {
php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->intern) {
+ PGresult *res;
- if (obj->intern->conn->intern) {
- PGresult *res = PQexec(obj->intern->conn->intern->conn, "COMMIT");
+ if (obj->intern->savepoint) {
+ char *cmd;
+
+ spprintf(&cmd, 0, "RELEASE SAVEPOINT \"%u\"", obj->intern->savepoint--);
+ res = PQexec(obj->intern->conn->intern->conn, cmd);
if (res) {
php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
}
+
+ efree(cmd);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+ res = PQexec(obj->intern->conn->intern->conn, "COMMIT");
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->intern) {
- if (obj->intern->conn->intern) {
- obj->intern->conn->intern->poller = PQconsumeInput;
+ obj->intern->conn->intern->poller = PQconsumeInput;
- if (!PQsendQuery(obj->intern->conn->intern->conn, "COMMIT")) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ if (obj->intern->savepoint) {
+ char *cmd;
+
+ spprintf(&cmd, 0, "RELEASE SAVEPOINT \"%u\"", obj->intern->savepoint--);
+ if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
}
+
+ efree(cmd);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+ if (!PQsendQuery(obj->intern->conn->intern->conn, "COMMIT")) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to commit transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->intern) {
- if (obj->intern->conn->intern) {
- PGresult *res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK");
+ PGresult *res;
+
+ if (obj->intern->savepoint) {
+ char *cmd;
+
+ spprintf(&cmd, 0, "ROLLBACK TO SAVEPOINT \"%u\"", obj->intern->savepoint--);
+ res = PQexec(obj->intern->conn->intern->conn, cmd);
if (res) {
php_pqres_success(res TSRMLS_CC);
- PQclear(res);
+ PHP_PQclear(res);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
}
+
+ efree(cmd);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+ res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK");
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->intern) {
- if (obj->intern->conn->intern) {
- obj->intern->conn->intern->poller = PQconsumeInput;
- if (!PQsendQuery(obj->intern->conn->intern->conn, "REOLLBACK")) {
+ obj->intern->conn->intern->poller = PQconsumeInput;
+
+ if (obj->intern->savepoint) {
+ char *cmd;
+
+ spprintf(&cmd, 0, "ROLLBACK TO SAVEPOINT \"%u\"", obj->intern->savepoint--);
+ if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+
+ efree(cmd);
+ } else {
+ if (!PQsendQuery(obj->intern->conn->intern->conn, "ROLLBACK")) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not rollback transaction (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
}
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_export_snapshot, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, exportSnapshot) {
+ zend_error_handling zeh;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ PGresult *res = PQexec(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()");
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ RETVAL_STRING(PQgetvalue(res, 0, 0), 1);
+ }
+
+ PHP_PQclear(res);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not intialized");
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_export_snapshot_async, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, exportSnapshotAsync) {
+ zend_error_handling zeh;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters_none()) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ obj->intern->conn->intern->poller = PQconsumeInput;
+
+ if (!PQsendQuery(obj->intern->conn->intern->conn, "SELECT pg_export_snapshot()")) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to export transaction snapshot (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_import_snapshot, 0, 0, 1)
+ ZEND_ARG_INFO(0, snapshot_id)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, importSnapshot) {
+ zend_error_handling zeh;
+ char *snapshot_str;
+ int snapshot_len;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len)) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ if (obj->intern->isolation >= PHP_PQTXN_REPEATABLE_READ) {
+ char *cmd, *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len);
+ PGresult *res;
+
+ spprintf(&cmd, 0, "SET TRANSACTION SNAPSHOT %s", sid);
+ res = PQexec(obj->intern->conn->intern->conn, cmd);
+
+ if (res) {
+ php_pqres_success(res TSRMLS_CC);
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "A transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot");
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_import_snapshot_async, 0, 0, 1)
+ ZEND_ARG_INFO(0, snapshot_id)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqtxn, importSnapshotAsync) {
+ zend_error_handling zeh;
+ char *snapshot_str;
+ int snapshot_len;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &snapshot_str, &snapshot_len)) {
+ php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ if (obj->intern->isolation >= PHP_PQTXN_REPEATABLE_READ) {
+ char *sid = PQescapeLiteral(obj->intern->conn->intern->conn, snapshot_str, snapshot_len);
+
+ if (sid) {
+ char *cmd;
+ obj->intern->conn->intern->poller = PQconsumeInput;
+
+ spprintf(&cmd, 0, "SET TRANSACTION SNAPSHOT %s", sid);
+ if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ efree(cmd);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote snapshot identifier (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "A transaction must have at least isolation level REPEATABLE READ to be able to import a snapshot");
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
PHP_ME(pqtxn, rollback, ai_pqtxn_rollback, ZEND_ACC_PUBLIC)
PHP_ME(pqtxn, commitAsync, ai_pqtxn_commit_async, ZEND_ACC_PUBLIC)
PHP_ME(pqtxn, rollbackAsync, ai_pqtxn_rollback_async, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, savepoint, ai_pqtxn_savepoint, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, savepointAsync, ai_pqtxn_savepoint_async, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, exportSnapshot, ai_pqtxn_export_snapshot, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, exportSnapshotAsync, ai_pqtxn_export_snapshot_async, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, importSnapshot, ai_pqtxn_import_snapshot, ZEND_ACC_PUBLIC)
+ PHP_ME(pqtxn, importSnapshotAsync, ai_pqtxn_import_snapshot_async, ZEND_ACC_PUBLIC)
PHP_ME(pqtxn, openLOB, ai_pqtxn_open_lob, ZEND_ACC_PUBLIC)
PHP_ME(pqtxn, createLOB, ai_pqtxn_create_lob, ZEND_ACC_PUBLIC)
PHP_ME(pqtxn, unlinkLOB, ai_pqtxn_unlink_lob, ZEND_ACC_PUBLIC)
{0}
};
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_construct, 0, 0, 3)
+ ZEND_ARG_OBJ_INFO(0, "connection", pq\\Connection, 0)
+ ZEND_ARG_INFO(0, expression)
+ ZEND_ARG_INFO(0, direction)
+ ZEND_ARG_INFO(0, options)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcopy, __construct) {
+ zend_error_handling zeh;
+ zval *zconn;
+ char *expr_str, *opt_str = "";
+ int expr_len, opt_len = 0;
+ long direction;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Osl|s", &zconn, php_pqconn_class_entry, &expr_str, &expr_len, &direction, &opt_str, &opt_len)) {
+ php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
+
+ if (conn_obj->intern) {
+ char *copy = NULL;
+
+ switch (direction) {
+ case PHP_PQCOPY_FROM_STDIN:
+ spprintf(©, 0, "COPY %s %s %s", expr_str, "FROM STDIN", opt_str);
+ break;
+
+ case PHP_PQCOPY_TO_STDOUT:
+ spprintf(©, 0, "COPY %s %s %s", expr_str, "TO STDOUT", opt_str);
+ break;
+
+ default:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid COPY direction, expected one of FROM_STDIN (%d) TO_STDOUT (%d), got %ld",
+ PHP_PQCOPY_FROM_STDIN, PHP_PQCOPY_TO_STDOUT, direction);
+ break;
+ }
+
+ if (copy) {
+ php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ PGresult *res = PQexec(conn_obj->intern->conn, copy);
+
+ efree(copy);
+
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ obj->intern = ecalloc(1, sizeof(*obj->intern));
+ obj->intern->direction = direction;
+ obj->intern->expression = estrdup(expr_str);
+ obj->intern->options = estrdup(opt_str);
+ obj->intern->conn = conn_obj;
+ php_pq_object_addref(conn_obj TSRMLS_CC);
+ }
+
+ PHP_PQclear(res);
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_put, 0, 0, 1)
+ ZEND_ARG_INFO(0, data)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcopy, put) {
+ zend_error_handling zeh;
+ char *data_str;
+ int data_len;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_str, &data_len)) {
+ php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ if (PHP_PQCOPY_FROM_STDIN == obj->intern->direction) {
+ if (1 == PQputCopyData(obj->intern->conn->intern->conn, data_str, data_len)) {
+ RETVAL_TRUE;
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY was not initialized with FROM_STDIN");
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_end, 0, 0, 0)
+ ZEND_ARG_INFO(0, error)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcopy, end) {
+ zend_error_handling zeh;
+ char *error_str = NULL;
+ int error_len = 0;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &error_str, &error_len)) {
+ php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (obj->intern) {
+ if (PHP_PQCOPY_FROM_STDIN == obj->intern->direction) {
+ if (1 == PQputCopyEnd(obj->intern->conn->intern->conn, error_str)) {
+ PGresult *res = PQgetResult(obj->intern->conn->intern->conn);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ RETVAL_TRUE;
+ }
+
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to end COPY (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY was not initialized with FROM_STDIN");
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\COPY not initialized");
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_get, 0, 0, 1)
+ ZEND_ARG_INFO(1, data)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcopy, get) {
+ zend_error_handling zeh;
+ zval *zdata;
+
+ zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata)) {
+ php_pqcopy_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ char *buffer = NULL;
+
+ if (obj->intern) {
+ if (PHP_PQCOPY_TO_STDOUT == obj->intern->direction) {
+ PGresult *res;
+ int bytes = PQgetCopyData(obj->intern->conn->intern->conn, &buffer, 0);
+
+ switch (bytes) {
+ case -1:
+ res = PQgetResult(obj->intern->conn->intern->conn);
+
+ if (res) {
+ if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
+ RETVAL_FALSE;
+ }
+
+ PHP_PQclear(res);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ }
+ break;
+
+ case -2:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
+ break;
+
+ default:
+ zval_dtor(zdata);
+ if (buffer) {
+ ZVAL_STRINGL(zdata, buffer, bytes, 1);
+ } else {
+ ZVAL_EMPTY_STRING(zdata);
+ }
+ RETVAL_TRUE;
+ break;
+ }
+
+ if (buffer) {
+ PQfreemem(buffer);
+ }
+ }
+ }
+ }
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+}
+
+static zend_function_entry php_pqcopy_methods[] = {
+ PHP_ME(pqcopy, __construct, ai_pqcopy_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+ PHP_ME(pqcopy, put, ai_pqcopy_put, ZEND_ACC_PUBLIC)
+ PHP_ME(pqcopy, end, ai_pqcopy_end, ZEND_ACC_PUBLIC)
+ PHP_ME(pqcopy, get, ai_pqcopy_get, ZEND_ACC_PUBLIC)
+ {0}
+};
+
/* {{{ PHP_MINIT_FUNCTION
*/
static PHP_MINIT_FUNCTION(pq)
php_pqres_class_entry->create_object = php_pqres_create_object;
php_pqres_class_entry->iterator_funcs.funcs = &php_pqres_iterator_funcs;
php_pqres_class_entry->get_iterator = php_pqres_iterator_init;
- zend_class_implements(php_pqres_class_entry TSRMLS_CC, 1, zend_ce_traversable);
+ zend_class_implements(php_pqres_class_entry TSRMLS_CC, 2, zend_ce_traversable, spl_ce_Countable);
memcpy(&php_pqres_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
php_pqres_object_handlers.read_property = php_pq_object_read_prop;
php_pqres_object_handlers.clone_obj = NULL;
php_pqres_object_handlers.get_property_ptr_ptr = NULL;
php_pqres_object_handlers.get_debug_info = php_pq_object_debug_info;
+ php_pqres_object_handlers.count_elements = php_pqres_count_elements;
zend_hash_init(&php_pqres_object_prophandlers, 6, NULL, NULL, 1);
zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("W"), INV_WRITE TSRMLS_CC);
zend_declare_class_constant_long(php_pqlob_class_entry, ZEND_STRL("RW"), INV_READ|INV_WRITE TSRMLS_CC);
+ memset(&ce, 0, sizeof(ce));
+ INIT_NS_CLASS_ENTRY(ce, "pq", "COPY", php_pqcopy_methods);
+ php_pqcopy_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
+ php_pqcopy_class_entry->create_object = php_pqcopy_create_object;
+
+ memcpy(&php_pqcopy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+ php_pqcopy_object_handlers.read_property = php_pq_object_read_prop;
+ php_pqcopy_object_handlers.write_property = php_pq_object_write_prop;
+ php_pqcopy_object_handlers.clone_obj = NULL;
+ php_pqcopy_object_handlers.get_property_ptr_ptr = NULL;
+ php_pqcopy_object_handlers.get_debug_info = php_pq_object_debug_info;
+
+ zend_hash_init(&php_pqcopy_object_prophandlers, 4, NULL, NULL, 1);
+
+ zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
+ ph.read = php_pqcopy_object_read_connection;
+ zend_hash_add(&php_pqcopy_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
+
+ zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("expression"), ZEND_ACC_PUBLIC TSRMLS_CC);
+ ph.read = php_pqcopy_object_read_expression;
+ zend_hash_add(&php_pqcopy_object_prophandlers, "expression", sizeof("expression"), (void *) &ph, sizeof(ph), NULL);
+
+ zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("direction"), ZEND_ACC_PUBLIC TSRMLS_CC);
+ ph.read = php_pqcopy_object_read_direction;
+ zend_hash_add(&php_pqcopy_object_prophandlers, "direction", sizeof("direction"), (void *) &ph, sizeof(ph), NULL);
+
+ zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
+ ph.read = php_pqcopy_object_read_options;
+ zend_hash_add(&php_pqcopy_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);
+
+ zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("FROM_STDIN"), PHP_PQCOPY_FROM_STDIN TSRMLS_CC);
+ zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("TO_STDOUT"), PHP_PQCOPY_TO_STDOUT TSRMLS_CC);
+
/*
REGISTER_INI_ENTRIES();
*/