[DBAL-211] wrong where clause in PostgreSqlPlatform::getTableWhereClause Created: 26/Jan/12  Updated: 28/Jan/12  Resolved: 28/Jan/12

Status: Resolved
Project: Doctrine DBAL
Component/s: Platforms
Affects Version/s: None
Fix Version/s: 2.1.6, 2.2
Security Level: All

Type: Bug Priority: Major
Reporter: Asmir Mustafic Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None
Environment:

ubuntu + postgres



 Description   

I have the folowing table structure:

Schema "public": with one table called "users"
Schema "forums": with one table called "users"

methods like PostgreSqlPlatform::getListTableForeignKeysSQL($table, $database = '') should list FK inside $table

the default search path is "public,pg_catalog"

calling PostgreSqlPlatform::getListTableForeignKeysSQL('users') it shuld extract the FK from public.users table, but this is the current result:

[PDOException] SQLSTATE[21000]: Cardinality violation: 7 ERROR: more than one row returned by a subquery used as an expression

this exception is thrown because PostgreSqlPlatform::getTableWhereClause do not cosider the current search path.

i propose the following implementation for PostgreSqlPlatform::getTableWhereClause

 
    private function getTableWhereClause($table, $classAlias = 'c', $namespaceAlias = 'n')
    {
        $whereClause = $namespaceAlias.".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND ";
        if (strpos($table, ".") !== false) {
            list($schema, $table) = explode(".", $table);
            $whereClause .= "$classAlias.relname = '" . $table . "' AND $namespaceAlias.nspname = '" . $schema . "'";
        } else {
            // $whereClause .= "$classAlias.relname = '" . $table . "'"; // this was the current implementation
            $whereClause .= "$classAlias.relname = '" . $table . "' AND $namespaceAlias.nspname = ANY(string_to_array((select setting from pg_catalog.pg_settings where name = 'search_path'),','))";
        }

        return $whereClause;
    }

this implementation will restrict the search range only to current "search_path".

(sorry for my english)



 Comments   
Comment by Benjamin Eberlei [ 28/Jan/12 ]

This looks very good.

Comment by Benjamin Eberlei [ 28/Jan/12 ]

Fixed

Generated at Thu Sep 18 19:50:29 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.