Skip to content

Fix #69592: pdo_dblib now skips datasets without columns while fetching data #1758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions ext/pdo_dblib/dblib_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,33 @@ char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, unsigned int *len T
return id;
}

static int dblib_handle_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
{
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
switch (attr) {
case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
H->skip_empty_rowsets = zval_is_true(val);
return 1;
default:
return 0;
}
}

static int pdo_dblib_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
{
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;

switch (attr) {
case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
ZVAL_BOOL(return_value, H->skip_empty_rowsets);
break;
default:
return 0;
}

return 1;
}

static struct pdo_dbh_methods dblib_methods = {
dblib_handle_closer,
dblib_handle_preparer,
Expand All @@ -252,10 +279,10 @@ static struct pdo_dbh_methods dblib_methods = {
dblib_handle_begin, /* begin */
dblib_handle_commit, /* commit */
dblib_handle_rollback, /* rollback */
NULL, /*set attr */
dblib_handle_set_attr, /*set attr */
dblib_handle_last_id, /* last insert id */
dblib_fetch_error, /* fetch error */
NULL, /* get attr */
pdo_dblib_get_attribute, /* get attr */
NULL, /* check liveness */
NULL, /* get driver methods */
NULL, /* request shutdown */
Expand Down
14 changes: 11 additions & 3 deletions ext/pdo_dblib/dblib_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ static int pdo_dblib_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC)
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
pdo_dblib_db_handle *H = S->H;
RETCODE ret;

ret = dbresults(H->link);
int num_fields;

do {
ret = dbresults(H->link);
num_fields = dbnumcols(H->link);
} while (H->skip_empty_rowsets && num_fields <= 0 && ret == SUCCEED);

if (H->skip_empty_rowsets && num_fields <= 0) {
return 0;
}

if (FAIL == ret) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "PDO_DBLIB: dbresults() returned FAIL" TSRMLS_CC);
Expand All @@ -139,7 +147,7 @@ static int pdo_dblib_stmt_next_rowset(pdo_stmt_t *stmt TSRMLS_DC)
}

stmt->row_count = DBCOUNT(H->link);
stmt->column_count = dbnumcols(H->link);
stmt->column_count = num_fields;

return 1;
}
Expand Down
1 change: 1 addition & 0 deletions ext/pdo_dblib/pdo_dblib.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ PHP_RSHUTDOWN_FUNCTION(pdo_dblib)

PHP_MINIT_FUNCTION(pdo_dblib)
{
REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_SKIP_EMPTY_ROWSETS", PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS);
if (FAIL == dbinit()) {
return FAILURE;
}
Expand Down
5 changes: 4 additions & 1 deletion ext/pdo_dblib/php_pdo_dblib_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ typedef struct {
typedef struct {
LOGINREC *login;
DBPROCESS *link;

unsigned skip_empty_rowsets:1;
pdo_dblib_err err;
} pdo_dblib_db_handle;

Expand All @@ -125,6 +125,9 @@ typedef struct {
int value;
} pdo_dblib_keyval;

enum {
PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS = PDO_ATTR_DRIVER_SPECIFIC,
};

ZEND_BEGIN_MODULE_GLOBALS(dblib)
pdo_dblib_err err;
Expand Down
60 changes: 60 additions & 0 deletions ext/pdo_dblib/tests/bug_69592.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
PDO_DBLIB: PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS for skip junk resultsets on SET NOCOUNT expression
--SKIPIF--
<?php
if (!extension_loaded('pdo_dblib')) die('skip not loaded');
require dirname(__FILE__) . '/config.inc';
?>
--FILE--
<?php
require dirname(__FILE__) . '/config.inc';

$sql = '
SET NOCOUNT ON
SELECT 0 AS [result]
';

var_dump($db->getAttribute(PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS)); // disabled by default

$stmt = $db->query($sql);
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); // expected: array(result => 0), actual: array()
var_dump($stmt->nextRowset()); // expected: bool(false), actual: bool(true)
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); // expected: array(), actual: array(result => 0)
$stmt->closeCursor();


$db->setAttribute(PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS, true);
var_dump($db->getAttribute(PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS));

$stmt = $db->query($sql);
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); // expected: array(result => 0), actual: array()
var_dump($stmt->nextRowset()); // expected: bool(false), actual: bool(true)
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); // expected: array(), actual: array(result => 0)
$stmt->closeCursor();
var_dump($db->getAttribute(PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS));

?>
--EXPECT--
bool(false)
array(0) {
}
bool(true)
array(1) {
[0]=>
array(1) {
["result"]=>
string(1) "0"
}
}
bool(true)
array(1) {
[0]=>
array(1) {
["result"]=>
string(1) "0"
}
}
bool(false)
array(0) {
}
bool(true)