Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-211

wrong where clause in PostgreSqlPlatform::getTableWhereClause

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1.6, 2.2
    • Component/s: Platforms
    • Security Level: All
    • 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)

        Activity

        Asmir Mustafic created issue -
        Asmir Mustafic made changes -
        Field Original Value New Value
        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}}

        {code:java}
            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;
            }
        {code}

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


        (sorry for my italian)
        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}}

        {code:java}
            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;
            }
        {code}

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


        (sorry for my english)
        Hide
        Benjamin Eberlei added a comment -

        This looks very good.

        Show
        Benjamin Eberlei added a comment - This looks very good.
        Hide
        Benjamin Eberlei added a comment -

        Fixed

        Show
        Benjamin Eberlei added a comment - Fixed
        Benjamin Eberlei made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.1.6 [ 10181 ]
        Fix Version/s 2.2 [ 10142 ]
        Resolution Fixed [ 1 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 13392 ] jira-feedback2 [ 17787 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 17787 ] jira-feedback3 [ 20142 ]

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={query=DBAL-211, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Asmir Mustafic
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: