Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColumnNotFoundException when using queries with column aliasing in JDBCWrapper #96

Closed
hemil-ruparel-blox opened this issue Mar 12, 2024 · 2 comments

Comments

@hemil-ruparel-blox
Copy link
Collaborator

Consider the following yaml file:

port: 5003

routes:
  get:
    /test: _/test.zm

data-sources:
  test:
    type: jdbc
    secrets: secret_source
    properties:
      user: <username>
      password: <password>
    connection: "jdbc:mysql://localhost/test"

test.zm

resp = _ds.test.select('select a as b from a', [])
panic(resp.inError, resp.error)

resp.value

If you hit localhost:5003/test, you get a SQL Exception:

SEVERE: Error in query : java.sql.SQLException: Column 'a' not found.

Root Cause

Relevant lines from JDBCWrapper:line 176 to 183

Map<String,Object> m = new LinkedHashMap<>(); // because of... order preserving
for (int index = 1; index <= count; index++) {
    String column = rsmd.getColumnName(index);
    Object value = rs.getObject(column);
    Object transformedValue = getObject(value);
    m.put(column, transformedValue);
}
result.add(m);

Root cause is rsmd.getColumnName(index); this gets the name of the column selected. But this name may differ from the actual result returned by the query.

Fix

Replace rsmd.getColumnName(index); with rsmd.getColumnLabel(index);. As per documentation,

Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.

nmondal added a commit that referenced this issue Mar 12, 2024
@hemil-ruparel-blox
Copy link
Collaborator Author

@nmondal Fix looks good to me. Can you publish it as well please?

@nmondal
Copy link
Owner

nmondal commented Mar 12, 2024

Already done.

@nmondal nmondal closed this as completed Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants