Skip to content

Subselects and comma craziness #276

@notpeter

Description

@notpeter

I'm having some issues where subselects aren't handled properly. Specifically, while sqlparse properly formats the query alone without, once it's placed inside a subselect things fall apart quickly...for example:
image

Full Query:

SELECT * FROM
  (SELECT n.nspname,
          c.relname,
          a.attname,
          a.atttypid,
          a.attnotnull OR (t.typtype = 'd' AND t.typnotnull) AS attnotnull,
          a.atttypmod,
          a.attlen,
          row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum,
          pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,
          dsc.description,
          t.typbasetype,
          t.typtype
   FROM pg_catalog.pg_namespace n
   JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid)
   JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid)
   JOIN pg_catalog.pg_type t ON (a.atttypid = t.oid)
   LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid AND a.attnum = def.adnum)
   LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid AND a.attnum = dsc.objsubid)
   LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid AND dc.relname='pg_class')
   LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid AND dn.nspname='pg_catalog')
   WHERE c.relkind IN ('r','v','f','m')
         AND a.attnum > 0
         AND NOT a.attisdropped
         AND n.nspname LIKE 'public'
         AND c.relname LIKE 'XXXXXXXXX') c
WHERE
   TRUE
ORDER BY
   nspname,
   c.relname,
   attnum

For anyone wondering, this is the query which the PostgreSQL JDBC drivers to return the list of foreign keys associated with a given table ('XXXXXXXX' above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions