Skip to content

Commit a021428

Browse files
committed
ext/pdo: Support changing case of table names with ATTR_CASE
Firebird returns table names in upcase, which makes the test more complicated without this.
1 parent a4e8ef0 commit a021428

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎ext/pdo/pdo_stmt.c‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ static bool dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_ty
123123
}
124124
/* }}} */
125125

126+
static zend_string *pdo_stmt_change_case(zend_string *str, enum pdo_case_conversion desired_case) /* {{{ */
127+
{
128+
/*
129+
* Note that input string may be freed; treat this like realloc() and
130+
* reassign the string.
131+
*/
132+
zend_string *orig = str;
133+
switch (desired_case) {
134+
case PDO_CASE_LOWER:
135+
str = zend_string_tolower(orig);
136+
zend_string_release(orig);
137+
break;
138+
case PDO_CASE_UPPER:
139+
str = zend_string_toupper(orig);
140+
zend_string_release(orig);
141+
break;
142+
default:
143+
break;
144+
}
145+
return str;
146+
}
147+
/* }}} */
148+
126149
bool pdo_stmt_describe_columns(pdo_stmt_t *stmt) /* {{{ */
127150
{
128151
int col;
@@ -136,26 +159,17 @@ bool pdo_stmt_describe_columns(pdo_stmt_t *stmt) /* {{{ */
136159

137160
/* if we are applying case conversions on column names, do so now */
138161
if (stmt->dbh->native_case != stmt->dbh->desired_case && stmt->dbh->desired_case != PDO_CASE_NATURAL) {
139-
zend_string *orig_name = stmt->columns[col].name;
140-
switch (stmt->dbh->desired_case) {
141-
case PDO_CASE_LOWER:
142-
stmt->columns[col].name = zend_string_tolower(orig_name);
143-
zend_string_release(orig_name);
144-
break;
145-
case PDO_CASE_UPPER:
146-
stmt->columns[col].name = zend_string_toupper(orig_name);
147-
zend_string_release(orig_name);
148-
break;
149-
default: ZEND_UNREACHABLE();
162+
stmt->columns[col].name = pdo_stmt_change_case(stmt->columns[col].name, stmt->dbh->desired_case);
163+
if (stmt->columns[col].table) {
164+
stmt->columns[col].table = pdo_stmt_change_case(stmt->columns[col].table, stmt->dbh->desired_case);
150165
}
151166
}
152167

153168
/* prepend the table name if the attribute is set */
154169
if (stmt->dbh->fetch_table_names && stmt->columns[col].table && ZSTR_LEN(stmt->columns[col].table)) {
155-
/* XXX: Apply case to table as well? */
156-
zend_string *table_name = stmt->columns[col].table;
157170
zend_string *orig_name = stmt->columns[col].name;
158-
stmt->columns[col].name = strpprintf(0, "%pS.%pS", table_name, orig_name);
171+
stmt->columns[col].name = strpprintf(0, "%pS.%pS",
172+
stmt->columns[col].table, orig_name);
159173
zend_string_release(orig_name);
160174
}
161175

‎ext/pdo/tests/attr_fetch_table_names.phpt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ PDOTest::skip();
2121
$db->exec("CREATE TABLE {$table} (id INT, label CHAR(1), PRIMARY KEY(id))");
2222
$db->exec("INSERT INTO {$table} (id, label) VALUES (1, 'a')");
2323

24+
// Firebird returns tables in upcase, force lowering
25+
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
26+
2427
$db->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true);
2528
var_dump($db->getAttribute(PDO::ATTR_FETCH_TABLE_NAMES));
2629
$stmt = $db->query("SELECT label FROM {$table}");

0 commit comments

Comments
 (0)