Skip to content

Commit cbe14aa

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 7788cd3 commit cbe14aa

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
@@ -763,36 +763,35 @@ static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type h
763763

764764
/* When fetching a column we only do one value fetch, so handle it separately */
765765
if (how == PDO_FETCH_COLUMN) {
766-
int colno = stmt->fetch.column;
767-
768-
if ((flags & PDO_FETCH_GROUP) && stmt->fetch.column == -1) {
769-
colno = 1;
766+
int fetch_column_number = stmt->fetch.column;
767+
int group_column_number = 0;
768+
769+
/* For an explicit column fetch with PDO_FETCH_GROUP, the group and fetch column is 1 */
770+
if (stmt->fetch.column != -1) {
771+
if (flags == PDO_FETCH_GROUP) {
772+
group_column_number = 1;
773+
fetch_column_number = 1;
774+
}
775+
} else {
776+
if (flags == PDO_FETCH_GROUP || flags == PDO_FETCH_UNIQUE) {
777+
fetch_column_number = 1;
778+
}
770779
}
771780

772-
if (colno < 0 ) {
781+
if (fetch_column_number < 0 ) {
773782
zend_value_error("Column index must be greater than or equal to 0");
774783
return false;
775784
}
776785

777-
if (colno >= stmt->column_count) {
786+
if (fetch_column_number >= stmt->column_count) {
778787
zend_value_error("Invalid column index");
779788
return false;
780789
}
781790

782-
if (flags == PDO_FETCH_GROUP && stmt->fetch.column == -1) {
783-
fetch_value(stmt, return_value, 1, NULL);
784-
} else if (flags == PDO_FETCH_GROUP && colno) {
785-
fetch_value(stmt, return_value, 0, NULL);
786-
} else {
787-
fetch_value(stmt, return_value, colno, NULL);
788-
}
791+
fetch_value(stmt, return_value, fetch_column_number, NULL);
789792

790793
if (group_key) {
791-
if (flags == PDO_FETCH_GROUP && stmt->fetch.column > 0) {
792-
fetch_value(stmt, group_key, colno, NULL);
793-
} else {
794-
fetch_value(stmt, group_key, 0, NULL);
795-
}
794+
fetch_value(stmt, group_key, group_column_number, NULL);
796795
convert_to_string(group_key);
797796
}
798797
return true;

0 commit comments

Comments
 (0)