Uploaded image for project: 'MariaDB Connector/J'
  1. MariaDB Connector/J
  2. CONJ-1160

getGeneratedKeys() return ResultSet with column name

Details

    Description

      It would be preferable if the ResultSet returned by getGeneratedKeys() provided the name of the table columns with ResultSet.getMetaData().getColumnName(int index) rather than insert_id

      Otherwise it assumes that we already know the name of the column which is the primary key!
      In any case this does not follow the JDBC specifications.

      see: Statement.java

      Attachments

        Activity

          diego dupin Diego Dupin added a comment -

          mysql/mariadb protocol doesn't know auto_increment column name when inserting data.
          mysql driver return by default "GENERATED_KEY" while mariadb driver return "insert_id".

          The only solution would be to parse command, extract table name and search for auto_increment field. That would be costly in terms of performance.

          Since MariaDB/MySQL can have only one auto_increment field, retrieving value using getLong(0) or getLong("insert_id") in place of getLong("<my real column name>") with connector command parsing + an additional round trip seems a bit too much just to be rigorous.

          I understand that this is not perfect, but better than the alternative

          diego dupin Diego Dupin added a comment - mysql/mariadb protocol doesn't know auto_increment column name when inserting data. mysql driver return by default "GENERATED_KEY" while mariadb driver return "insert_id". The only solution would be to parse command, extract table name and search for auto_increment field. That would be costly in terms of performance. Since MariaDB/MySQL can have only one auto_increment field, retrieving value using getLong(0) or getLong("insert_id") in place of getLong("<my real column name>") with connector command parsing + an additional round trip seems a bit too much just to be rigorous. I understand that this is not perfect, but better than the alternative
          prrvchr prrvchr added a comment -

          Thanks for these informations.
          This is not really a problem since it is possible to use the names of the columns returned to obtain the correct record...

          prrvchr prrvchr added a comment - Thanks for these informations. This is not really a problem since it is possible to use the names of the columns returned to obtain the correct record...
          prrvchr prrvchr added a comment -

          Hi Diego,

          Failing to know the name of the column, would it not be possible to know its index (ie: from 1 to n)?

          prrvchr prrvchr added a comment - Hi Diego, Failing to know the name of the column, would it not be possible to know its index (ie: from 1 to n)?
          diego dupin Diego Dupin added a comment -

          index is always 1.
          example:

          stmt.executeUpdate("INSERT INTO MyTable (test) VALUES ('somevalue')",
                      java.sql.Statement.RETURN_GENERATED_KEYS);
          ResultSet rsGen = stmt.getGeneratedKeys();
          rsGen.next();
          long generatedId = rsGen.getLong(1);
          

          diego dupin Diego Dupin added a comment - index is always 1. example: stmt.executeUpdate( "INSERT INTO MyTable (test) VALUES ('somevalue')" , java.sql.Statement.RETURN_GENERATED_KEYS); ResultSet rsGen = stmt.getGeneratedKeys(); rsGen.next(); long generatedId = rsGen.getLong( 1 );

          People

            diego dupin Diego Dupin
            prrvchr prrvchr
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.