Derby
  1. Derby
  2. DERBY-2631

Expose existing auto-generated key functionality through more JDBC APIs in embedded mode.

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 10.3.1.4
    • Fix Version/s: 10.3.1.4
    • Component/s: JDBC
    • Labels:
      None
    • Environment:
      Embedded mode only.

      Description

      Derby currently supports the following JDBC methods for auto-generated keys:

      // Indicate that we want to retrieve auto-generated key values.

      Connection.prepareStatement(String sql, int autoGeneratedKeys);
      Statement.execute(String sql, int autoGeneratedKeys);
      Statement.executeUpdate(String sql, int autoGeneratedKeys);

      // Retrieve the auto-generated values (only applies to INSERT statements).

      ResultSet rs = Statement.getGeneratedKeys();

      The current implementation of getGeneratedKeys() internally maps to the "IDENTITY_VAL_LOCAL()" method, which means that Derby's implementation only returns generated keys for autoincrement columns (no other default columns are supported). Further:

      1. The generated key result set only ever has a single column. This is
      because Derby only allows one autoincrement column per table.

      2. The type of the single column in the result set will be DECIMAL(31,0).
      This is defined by IDENTITY_VAL_LOCAL().

      3. The generated key result set will only ever have a single row. This is
      because IDENTITY_VAL_LOCAL() only returns values that were assigned as
      the result of a single row INSERT statement using a VALUES clause.
      For a single row INSERT statement, at most one autoincrement value
      will be generated.

      All of that said, JDBC 3.0 also defines the following methods, which allow the user to explicitly indicate, via column position or column name, the columns for which the auto-generated keys should be made available:

      Connection.prepareStatement(String sql, String[] columnNames);
      Connection.prepareStatement(String sql, int[] columnIndexes);

      Statement.execute(String sql, String[] columNames);
      Statement.execute(String sql, int[] columIndexes);

      Statement.executeUpdate(String sql, String[] columnNames);
      Statement.executeUpdate(String sql, int[] columnIndexes);

      Derby currently throws a "Feature not supported" error for all of these methods. However, it seems like the above methods could be "mapped" onto the existing Derby behavior with relatively little effort (in embedded mode). Most of the required code is already in place.

      Doing so would make it easier for applications that rely on the columnNames and/or columnIndexes APIs to work with Derby (assuming the app just wants generated keys for identity (autoincrement) columns).

      Note that this Jira does not entail removing any of the restrictions nor changing any of the behavior outlined above. All of that will remain exactly as it is. This Jira simply exposes the existing functionality (restrictions and all) through additional (standard) API methods. In particular this means that any column specified by index (position) or name must be an auto-increment column for the INSERT table; otherwise Derby should throw an error. Or put differently, a user who specifies a column name/position will get-in the absence of errors-the exact same results as s/he would get from invoking the "(String sql, int autoGeneratedKeys)" method.

      Note also: This Jira is specifically for embedded mode. I think it would be harder to support these methods for Derby Client and so do not plan to address that.

      1. d2631_v1.stat
        0.7 kB
        A B
      2. d2631_v1.patch
        25 kB
        A B

        Issue Links

        There are no Sub-Tasks for this issue.

          Activity

          Hide
          A B added a comment -

          Attaching a patch, d2631_v1.patch, which does the following:

          1. Updates the following JDBC methods so that they no longer throw a "Feature
          not implemented" error. Instead they make calls to an already existing
          internal method and pass in the appropriate arguments:

          Connection.prepareStatement(String sql, String[] columnNames);
          Connection.prepareStatement(String sql, int[] columnIndexes);

          Statement.execute(String sql, String[] columNames);
          Statement.execute(String sql, int[] columIndexes);

          Statement.executeUpdate(String sql, String[] columnNames);
          Statement.executeUpdate(String sql, int[] columnIndexes);

          2. Changes the (already existing but not currently used) code in
          sql/execute/InsertResultSet that handles autogen column indexes/names
          to throw an error for any target column that is not an auto-increment
          column. This is because Derby's implementation of getGeneratedKeys()
          internally maps to the IDENTITY_VAL_LOCAL() function, which only returns
          keys for identity (autoincrement) columns. So if the user specifies
          something else, we'll throw an error.

          3. Changes the names of two existing (but unused) errors to more accurately
          reflect their intended use (they are now called by the changes for #2).
          Also changes the text for those errors as the old text seemed a tad
          awkward. No regression impact here because the two errors in question
          were never exposed to users before now.

          4. Makes a slight change to jdbcapi/statementJdbc30.java (a test) to
          reflect the new behavior (we no longer throw "Feature not implemented"
          errors).

          5. Adds test cases for the newly supported APIs to the JUnit test
          lang/AutoGenJDBC30Test.java. This involved adding two more utility
          methods to junit/BaseJDBCTestCase.java, as well.

          I ran derbyall and suites.All on SUSE Linux with ibm142 and the only failures were those which also show up in the Tinderbox results. I.e. they are not caused by these changes. So I believe this patch is ready for commit.

          Review comments or other feedback are appreciated...

          Show
          A B added a comment - Attaching a patch, d2631_v1.patch, which does the following: 1. Updates the following JDBC methods so that they no longer throw a "Feature not implemented" error. Instead they make calls to an already existing internal method and pass in the appropriate arguments: Connection.prepareStatement(String sql, String[] columnNames); Connection.prepareStatement(String sql, int[] columnIndexes); Statement.execute(String sql, String[] columNames); Statement.execute(String sql, int[] columIndexes); Statement.executeUpdate(String sql, String[] columnNames); Statement.executeUpdate(String sql, int[] columnIndexes); 2. Changes the (already existing but not currently used) code in sql/execute/InsertResultSet that handles autogen column indexes/names to throw an error for any target column that is not an auto-increment column. This is because Derby's implementation of getGeneratedKeys() internally maps to the IDENTITY_VAL_LOCAL() function, which only returns keys for identity (autoincrement) columns. So if the user specifies something else, we'll throw an error. 3. Changes the names of two existing (but unused) errors to more accurately reflect their intended use (they are now called by the changes for #2). Also changes the text for those errors as the old text seemed a tad awkward. No regression impact here because the two errors in question were never exposed to users before now. 4. Makes a slight change to jdbcapi/statementJdbc30.java (a test) to reflect the new behavior (we no longer throw "Feature not implemented" errors). 5. Adds test cases for the newly supported APIs to the JUnit test lang/AutoGenJDBC30Test.java. This involved adding two more utility methods to junit/BaseJDBCTestCase.java, as well. I ran derbyall and suites.All on SUSE Linux with ibm142 and the only failures were those which also show up in the Tinderbox results. I.e. they are not caused by these changes. So I believe this patch is ready for commit. Review comments or other feedback are appreciated...
          Hide
          Kathey Marsden added a comment -

          I am a little hesitant about introducing this new difference between embedded and client. Will it be possible with some future work to implement this functionality for client?

          Show
          Kathey Marsden added a comment - I am a little hesitant about introducing this new difference between embedded and client. Will it be possible with some future work to implement this functionality for client?
          Hide
          A B added a comment -

          > Will it be possible with some future work to implement this functionality for client?

          I have not looked in detail at how to implement this for client, but I would be surprised if such a thing was not possible with some future work. My guess, though, is that it will require more work than was needed for embedded (most of the embedded code already existed). Since the fish I'm hoping to fry right now is limited to embedded, and since there does not appear to be a straightforward mapping to the client code, I was hoping to focus this Jira on embedded only.

          But as far as I know there's no reason this functionality couldn't be implemented in client, once someone has the desire to fry that fish. It may be more difficult, but it seems like it should be possible...

          Show
          A B added a comment - > Will it be possible with some future work to implement this functionality for client? I have not looked in detail at how to implement this for client, but I would be surprised if such a thing was not possible with some future work. My guess, though, is that it will require more work than was needed for embedded (most of the embedded code already existed). Since the fish I'm hoping to fry right now is limited to embedded, and since there does not appear to be a straightforward mapping to the client code, I was hoping to focus this Jira on embedded only. But as far as I know there's no reason this functionality couldn't be implemented in client, once someone has the desire to fry that fish. It may be more difficult, but it seems like it should be possible...
          Hide
          Kathey Marsden added a comment -

          Thanks Army. It would be great if you could open an issue for client and link to DERBY-310 once this goes in.

          Show
          Kathey Marsden added a comment - Thanks Army. It would be great if you could open an issue for client and link to DERBY-310 once this goes in.
          Hide
          Knut Anders Hatlen added a comment -

          I'm +1 to the patch. I don't consider this as introducing differences between embedded and the client, but rather as making the embedded driver more complete, which is a good thing. I agree with Kathey that we should open an issue for the client driver and link it to DERBY-310.

          Show
          Knut Anders Hatlen added a comment - I'm +1 to the patch. I don't consider this as introducing differences between embedded and the client, but rather as making the embedded driver more complete, which is a good thing. I agree with Kathey that we should open an issue for the client driver and link it to DERBY-310 .
          Hide
          A B added a comment -

          Thank you very much for your feedback, Kathey and Knut Anders. I will indeed create a new issue for client driver once the changes for this Jira are committed.

          I'll try to commit within the next day or so if there are no other comments.

          Show
          A B added a comment - Thank you very much for your feedback, Kathey and Knut Anders. I will indeed create a new issue for client driver once the changes for this Jira are committed. I'll try to commit within the next day or so if there are no other comments.
          Hide
          A B added a comment -

          Committed _v1 patch with patch with svn # 538260:

          URL: http://svn.apache.org/viewvc?view=rev&rev=538260

          Filed DERBY-2653 for the Derby Client and linked that issue to DERBY-310, as requested by Kathey and Knut Anders.

          Thanks again for the feedback.

          Show
          A B added a comment - Committed _v1 patch with patch with svn # 538260: URL: http://svn.apache.org/viewvc?view=rev&rev=538260 Filed DERBY-2653 for the Derby Client and linked that issue to DERBY-310 , as requested by Kathey and Knut Anders. Thanks again for the feedback.
          Hide
          A B added a comment -

          Tinderbox run ran cleanly after commit, so resolving this issue.

          Show
          A B added a comment - Tinderbox run ran cleanly after commit, so resolving this issue.

            People

            • Assignee:
              A B
              Reporter:
              A B
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development