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

Let ParameterMetadata for PreparedStatement report correct ParameterType before execution #35

Closed
jeffersonatsafe opened this issue Jan 17, 2020 · 1 comment · Fixed by #1218
Assignees
Labels
api: spanner Issues related to the googleapis/java-spanner-jdbc API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jeffersonatsafe
Copy link

jeffersonatsafe commented Jan 17, 2020

Thanks for stopping by to let us know something could be better!

Is your feature request related to a problem? Please describe.
When using PreparedStatements with parameters, I need to obtain the parameters' JDBC Types before executing the PreparedStatement (so I can set the values of the parameters using the correct object types). The current version of the driver (v 1.12.0) does not allow this. Before executing the PreparedStatement, all parameters will be reported as JDBCType = 1111.
After manually setting the parameters and executing the PreparedStatement, the ParameterMetaData will then report the correct parameter types.

Describe the solution you'd like
ParameterMetaData to return the correct parameter types before PreparedStatement is executed.

Describe alternatives you've considered
The alternative is to do a column name matching using the destination table schema, so I'm able to figure out the data types of the destination columns, and set the objects accordingly. However, this feels like a hacky solution.

Additional context
Code snippet:

      Connection connection = driver.connect(connString, connProps);
      Statement statement = connection.createStatement();
      statement.execute("Create table manualTable (mykey int64, myint int64) primary key (mykey)");
      ParameterMetaData parameterMetaData = preparedStatement.getParameterMetaData();
      PreparedStatement preparedStatement = connection.prepareStatement(
            "INSERT INTO `manualTable` (`mykey`, `myint`) VALUES (?, ?)");
      for (int i = 1; i <= parameterMetaData.getParameterCount(); i++ )
      {
         int jdbcTypeInt = parameterMetaData.getParameterType(i);
         JDBCType jdbcType = JDBCType.valueOf(jdbcTypeInt);
         System.out.println(String.format(
               "parameter %d is JDBC Type: %d - %s",
               i,
               jdbcTypeInt,
               jdbcType.getName()));
      }

Thanks!

@jeffersonatsafe jeffersonatsafe changed the title Let ParameterMetadata for PreparedStatement report on correct ParameterType before execution Let ParameterMetadata for PreparedStatement report correct ParameterType before execution Jan 17, 2020
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Jan 18, 2020
@olavloite olavloite added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Jan 20, 2020
@olavloite
Copy link
Collaborator

This feature will not be trivial to implement, as the Cloud Spanner backend does not have any API that can be used to get this information. It would only be possible to implement this by parsing the SQL statement in the driver and extracting the type information from the schema metadata tables (basically what you also describe as your workaround). I'm not sure that this is something that we would want to do in this driver.

@google-cloud-label-sync google-cloud-label-sync bot added the api: spanner Issues related to the googleapis/java-spanner-jdbc API. label Jan 30, 2020
@olavloite olavloite self-assigned this Apr 28, 2023
olavloite added a commit that referenced this issue Apr 29, 2023
Add actual support for `PreparedStatement#getParameterMetaData()`. The first time
this method is called for a PreparedStatement, the connection will now send the
query to Cloud Spanner in analyze mode and without any parameter values. This
will instruct Cloud Spanner to return the names and types of any query parameters
in the statement.

Fixes #35
@rajatbhatta rajatbhatta removed their assignment Aug 17, 2023
olavloite added a commit that referenced this issue Dec 22, 2023
* feat: support PreparedStatement#getParameterMetaData()

Add actual support for `PreparedStatement#getParameterMetaData()`. The first time
this method is called for a PreparedStatement, the connection will now send the
query to Cloud Spanner in analyze mode and without any parameter values. This
will instruct Cloud Spanner to return the names and types of any query parameters
in the statement.

Fixes #35

* fix: restore previous behavior

* fix: PostgreSQL string type name should be 'character varying'

* fix: update type name to 'character varying' in integration test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner-jdbc API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants