Skip to content

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

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

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