Skip to content

[FEATURE] Utility to find table names corresponding with SelectItems #1873

Closed

Description

@rsmckinney

For instance, I want something like this:

PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(query);
List<SelectItem<?>> selectItems = select.getSelectItems();
. . .
String tableName = TableNamesFinder.findTable(selectItem, colName -> findTables(colName));

I want a findTable(SelectItem<?> selectItem, Function<String, Set<String>> tableMatcher) method where tableMatcher provides the parser with a lookup for tables having a specified column name. For my case I can supply this via JDBC. Note, selectItem must be a column defined in a table or the all-columns expression, otherwise the method returns null.

I know this type of utility is more on the semantics side of the equation, but I feel like this parser could begin to provide these kinds of services. Even better, build a resolver/backend based on JDBC where not only table names, but also data types etc. could be attached to the AST. Just a thought, considering most of the time a specific DBMS is in context when using this parser.

Btw, I need this utility as a backup for JDBC drivers that don't support ResultSetMetaData#getTableName(int columnIndex) properly; Server and Oracle don't provide table names corresponding with columns.

I'll probably begin writing this utility myself, but would appreciate feedback if you have any. Would be awesome to hear something similar is available!

Activity

  1. manticore-projects commented on Oct 6, 2023

    @manticore-projects
    Contributor

    Greetings.

    Its not possible with JSQLParser alone since the SQL itself does not provide enough information. You will always need a combination of the AST and MetaData.

    MetaData of Oracle and MS SQL Server ARE sufficient (I do use that myself a lot).
    However, there is also INFORMATIOn_SCHEMA and or ALL_TABLES or ALL_TAB_COLUMNS.

    So from the JSQLParser side, there is not much else we can do. We don't want to rely on or interfere with JDBC.

  2. manticore-projects commented on Oct 6, 2023

    @manticore-projects
    Contributor

    Server and Oracle don't provide table names corresponding with columns.

    That is not correct. You will get precise Column definitions for each table, as well as indices or primary keys or references.

  3. rsmckinney commented on Oct 6, 2023

    @rsmckinney
    Author

    That is not correct. You will get precise Column definitions for each table, as well as indices or primary keys or references.

    Let me clarify. Sql Server and Oracle drivers fail to handle:

    ResultSetMetaData#getTableName(int colIndex)

    This is a known limitation with those drivers.

  4. rsmckinney commented on Oct 6, 2023

    @rsmckinney
    Author

    Its not possible with JSQLParser alone since the SQL itself does not provide enough information. You will always need a combination of the AST and MetaData.

    Yes, I mentioned that this request is more on the semantic side of the equation. I think it can be done with just table metadata and only column names corresponding with tables. This is primarily because column names must either be qualified or be non-ambiguous wrt target tables.

  5. manticore-projects commented on Oct 6, 2023

    @manticore-projects
    Contributor

    That is not correct. You will get precise Column definitions for each table, as well as indices or primary keys or references.

    Let me clarify. Sql Server and Oracle drivers fail to handle:

    ResultSetMetaData#getTableName(int colIndex)

    This is a known limitation with those drivers.

    But you can get all Tables with Columns for a given schema.
    You can get the tables names from TablesNameFinder and then browse your schema to get all the information you need.

  6. manticore-projects commented on Oct 6, 2023

    @manticore-projects
    Contributor

    That is not correct. You will get precise Column definitions for each table, as well as indices or primary keys or references.

    Let me clarify. Sql Server and Oracle drivers fail to handle:

    ResultSetMetaData#getTableName(int colIndex)

    This is a known limitation with those drivers.

    But you can get all Tables with Columns for a given schema. You can get the tables names from TablesNameFinder and then browse your schema to get all the information you need.

    Not in our scope because it relies on JDBC drivers and connection information and RDBMS specific stuff (like types).

  7. locked and limited conversation to collaborators on Oct 6, 2023
  8. converted this issue into a discussion #1874 on Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions