+ RETVAL_LONG(obj->intern->flags);
+}
+
+char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len, int *query_offset)
+{
+ size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR ");
+ char *decl_str;
+
+ decl_str = emalloc(decl_len);
+ decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s",
+ name_str,
+ (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "",
+ (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "",
+ (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" :
+ (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "",
+ (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "",
+ query_str
+ );
+
+ if (query_offset) {
+ /* sizeof() includes the terminating null byte, so no need for spaces in the string literals */
+ *query_offset = sizeof("DECLARE")
+ + (name_len + 1)
+ + ((flags & PHP_PQ_DECLARE_BINARY) ? sizeof("BINARY") : 1)
+ + ((flags & PHP_PQ_DECLARE_INSENSITIVE) ? sizeof("INSENSITIVE") : 1)
+ + ((flags & PHP_PQ_DECLARE_NO_SCROLL) ? sizeof("NO SCROLL") :
+ (flags & PHP_PQ_DECLARE_SCROLL) ? sizeof("SCROLL") : 1)
+ + sizeof("CURSOR")
+ + ((flags & PHP_PQ_DECLARE_WITH_HOLD) ? sizeof("WITH HOLD") : 1)
+ + sizeof("FOR");
+ }
+
+ return decl_str;
+}
+
+php_pqcur_t *php_pqcur_init(php_pqconn_object_t *conn, const char *name, char *decl, int query_offset, long flags)
+{
+ php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
+
+ php_pq_object_addref(conn);
+ cur->conn = conn;
+ cur->name = estrdup(name);
+ cur->decl = decl;
+ cur->query_offset = query_offset;
+ cur->flags = flags;
+ cur->open = 1;
+
+ return cur;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct, 0, 0, 4)
+ ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
+ ZEND_ARG_INFO(0, name)
+ ZEND_ARG_INFO(0, flags)
+ ZEND_ARG_INFO(0, query)
+ ZEND_ARG_INFO(0, async)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqcur, __construct) {
+ zend_error_handling zeh;
+ char *name_str, *query_str;
+ size_t name_len, query_len;
+ zend_long flags;
+ zval *zconn;
+ ZEND_RESULT_CODE rv;
+ zend_bool async = 0;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
+ rv = zend_parse_parameters(ZEND_NUM_ARGS(), "Osls|b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &flags, &query_str, &query_len, &async);
+ zend_restore_error_handling(&zeh);
+
+ if (SUCCESS == rv) {
+ php_pqcur_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
+ php_pqconn_object_t *conn_obj = PHP_PQ_OBJ(zconn, NULL);
+
+ if (obj->intern) {
+ throw_exce(EX_BAD_METHODCALL, "pq\\Cursor already initialized");
+ } if (!conn_obj->intern) {
+ throw_exce(EX_UNINITIALIZED, "pq\\Connection not initialized");
+ } else {
+ int query_offset;
+ char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len, &query_offset);
+
+ if (async) {
+ rv = php_pqconn_declare_async(zconn, conn_obj, decl);
+ } else {
+ rv = php_pqconn_declare(zconn, conn_obj, decl);
+ }
+
+ if (SUCCESS != rv) {
+ efree(decl);
+ } else {
+ obj->intern = php_pqcur_init(conn_obj, name_str, decl, query_offset, flags);
+ }
+ }
+ }