Skip to content

Commit ced646c

Browse files
committed
ext/pdo: Refactor do_fetch() when fetch mode is PDO::FETCH_COLUMN
This is one hell of a convoluted and bonker behaviour
1 parent ca1c44d commit ced646c

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -691,36 +691,35 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
691691

692692
/* When fetching a column we only do one value fetch, so handle it separately */
693693
if (how == PDO_FETCH_COLUMN) {
694-
int colno = stmt->fetch.column;
695-
696-
if ((flags & (PDO_FETCH_GROUP|PDO_FETCH_UNIQUE)) && stmt->fetch.column == -1) {
697-
colno = 1;
694+
int fetch_column_number = stmt->fetch.column;
695+
int group_column_number = 0;
696+
697+
/* For an explicit column fetch with PDO_FETCH_GROUP, the group and fetch column is 1 */
698+
if (stmt->fetch.column != -1) {
699+
if (flags == PDO_FETCH_GROUP) {
700+
group_column_number = 1;
701+
fetch_column_number = 1;
702+
}
703+
} else {
704+
if (flags == PDO_FETCH_GROUP || flags == PDO_FETCH_UNIQUE) {
705+
fetch_column_number = 1;
706+
}
698707
}
699708

700-
if (colno < 0 ) {
709+
if (fetch_column_number < 0) {
701710
zend_value_error("Column index must be greater than or equal to 0");
702711
return false;
703712
}
704713

705-
if (colno >= stmt->column_count) {
714+
if (fetch_column_number >= stmt->column_count) {
706715
zend_value_error("Invalid column index");
707716
return false;
708717
}
709718

710-
if (flags == PDO_FETCH_GROUP && stmt->fetch.column == -1) {
711-
fetch_value(stmt, return_value, 1, NULL);
712-
} else if (flags == PDO_FETCH_GROUP && colno) {
713-
fetch_value(stmt, return_value, 0, NULL);
714-
} else {
715-
fetch_value(stmt, return_value, colno, NULL);
716-
}
719+
fetch_value(stmt, return_value, fetch_column_number, NULL);
717720

718721
if (group_key) {
719-
if (flags == PDO_FETCH_GROUP && stmt->fetch.column > 0) {
720-
fetch_value(stmt, group_key, colno, NULL);
721-
} else {
722-
fetch_value(stmt, group_key, 0, NULL);
723-
}
722+
fetch_value(stmt, group_key, group_column_number, NULL);
724723
convert_to_string(group_key);
725724
}
726725
return true;

0 commit comments

Comments
 (0)