modeldba / sql-surveyor

High-level SQL parser. Identify tables, columns, aliases and more from your SQL script in one easy to consume object. Supports PostgreSQL, MySQL, SQL Server and Oracle (PL/SQL) dialects.
https://modeldba.com/sql-surveyor
MIT License
27 stars 2 forks source link

Inconsistency between MYSQL and PLpgSQL #2

Closed mmkal closed 3 years ago

mmkal commented 3 years ago

Related to #1 but thought I'd make a separate issue:

PLpgSQL treats aliases differently from MYSQL (and the MYSQL way seems more correct - if this is expected behaviour, I couldn't find it in the docs):

let sqlSurveyor = require("sql-surveyor")

function getAliasF({ dialect, alias }) {
  let surveyor = new sqlSurveyor.SQLSurveyor(dialect)
  let result = surveyor.survey('select f.id from foo as f')
  return result.getTableFromAlias(alias)
}

console.log(getAliasF({ dialect: sqlSurveyor.SQLDialect.MYSQL, alias: 'f' }) // prints 'foo', as expected
console.log(getAliasF({ dialect: sqlSurveyor.SQLDialect.PLpgSQL, alias: 'f' }) // prints null

console.log(getAliasF({ dialect: sqlSurveyor.SQLDialect.MYSQL, alias: 'as f' }) // prints null
console.log(getAliasF({ dialect: sqlSurveyor.SQLDialect.PLpgSQL, alias: 'as f' }) // prints 'foo', as expected

Another difference (noted in #1):

// result.outputColumns[0] with MYSQL:
    OutputColumn {
      columnName: 'f.id',
      columnAlias: null,
      tableName: 'foo',
      tableAlias: 'f'
    }

// result.outputColumns[0] with PLpgSQL:
    OutputColumn {
      columnName: 'f.id',
      columnAlias: null,
      tableName: 'f',
      tableAlias: null
    }
mtriff commented 3 years ago

Thanks for the bug report! The output should be identical between the two dialects. The MySQL output is the expected behaviour.

I'll have a fix for this available shortly.

mtriff commented 3 years ago

This has been fixed in 1.2.2.

Both MySQL and PL/pgSQL now output:

outputColumns: [
      OutputColumn {
        columnName: 'f.id',
        columnAlias: null,
        tableName: 'foo',
        tableAlias: 'f'
      }
    ],

Thanks again for reporting!

mmkal commented 3 years ago

Wow that was fast! I'll try it out!