Page MenuHomePhabricator

Removing table prefixes in column names (EDConnectorDb.php)
Closed, ResolvedPublic

Description

Hello,
In EDConnectorDb.php there is a statement (line 54), which removes table prefixes from column names:

// Deal with table prefixes in column names (internal_var=tbl1.col1).
if ( preg_match( '/[^.]+$/', $alias, $matches ) ) {
        $alias = $matches[0];
}

It works fine in most cases, but when, for example, we have the following #get_db_data, with column value in an SQL expression

{{#get_db_data:
|db=enc
|from=enccategorylinks=ecl,encpage=p
|join on=ecl.cl_from=p.page_id
|where=ecl.cl_to='Gimę_{{CURRENTMONTHNAMEGEN}}_{{LOCALDAY}}_d.'
|limit=200
|order by=ecl.cl_sortkey asc
|data=asm=p.page_title,asmt=replace(p.page_title, '_', ' ')
}}

it turns replace(p.page_title, '_', ' ') into page_title, '_', ' ') and things break. Expected SQL would be

SELECT p.page_title,replace(p.page_title, '_', ' ') …

but the result is

SELECT p.page_title,page_title, '_', ' ') …

As a workaround I use strpbrk() to check whether column name is not just table.column, but instead is evaluated from an SQL expression

if ( !strpbrk($alias, '\(\)\'\\",') ) {
   if ( preg_match( '/[^.]+$/', $alias, $matches ) ) {
       $alias = $matches[0];
    }
}

So this “fixes” articles involved, but then I don't know how important it is to remove table name prefixes and perhaps there can be a better workaround. Thanks for any comment on this.

Event Timeline

Change 979439 had a related patch set uploaded (by Alex Mashin; author: mashin):

[mediawiki/extensions/ExternalData@master] Improve parsing of column identifiers

https://gerrit.wikimedia.org/r/979439

alex-mashin changed the task status from Open to In Progress.Dec 3 2023, 2:49 PM
alex-mashin claimed this task.

Change 979439 merged by jenkins-bot:

[mediawiki/extensions/ExternalData@master] Improve parsing of column identifiers

https://gerrit.wikimedia.org/r/979439

Yes, the problem seems to be fixed. Thank you.