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

"No such column" when getting results by index without alias

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 1.1.0
    • 1.1.1
    • None
    • None
    • Tested on Windows and Linux

    Description

      Query:
      select count from table_a;

      Retrieve results using:
      int count = resultSet.getInt(1);

      Problem:
      Driver needs to translate index to a column name. In MySQLResultSetMetaData.getColumnName(index), the method getColumnInformation(column).getOriginalName() returns "". If I changed the code to this workaround:

      public String getColumnName(final int column) throws SQLException {
      String results = getColumnInformation(column).getOriginalName();
      if (results == null || "".equals(results))

      { results = getColumnInformation(column).getName(); }

      return results;
      }

      then it works. This is because getColumnInformation(column).getName() returns "count" which is what is needed in ColumnNameMap.getIndex(name).

      I don't know the code deep enough to tell if the workaround above is the best way to address the issue, please advise.

      Attachments

        Activity

          wlad Vladislav Vaintroub added a comment - - edited

          Hi,
          After reading http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSetMetaData.html , I think you need to use getColumnLabel(), not getColumnName(). count does not refer to an actual column in the database.

          And .. what was actually the code leading to exception?

          Following test actually passes successfully to me (I use "count" in getInt())

          @Test
          public void conj17() throws Exception

          { ResultSet rs = connection.createStatement().executeQuery("select count(*) from information_schema.tables"); rs.next(); assertTrue(rs.getInt(1) > 0); assertTrue(rs.getInt("count(*)") > 0); assertEquals(rs.getMetaData().getColumnLabel(1),"count(*)"); }
          wlad Vladislav Vaintroub added a comment - - edited Hi, After reading http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSetMetaData.html , I think you need to use getColumnLabel(), not getColumnName(). count does not refer to an actual column in the database. And .. what was actually the code leading to exception? Following test actually passes successfully to me (I use "count " in getInt()) @Test public void conj17() throws Exception { ResultSet rs = connection.createStatement().executeQuery("select count(*) from information_schema.tables"); rs.next(); assertTrue(rs.getInt(1) > 0); assertTrue(rs.getInt("count(*)") > 0); assertEquals(rs.getMetaData().getColumnLabel(1),"count(*)"); }

          After some thinkling.. yes, fixing it to be ConnectorJ compatible won't hurt anyone and would prevent surprises for the users. Thanks for your suggestion!

          wlad Vladislav Vaintroub added a comment - After some thinkling.. yes, fixing it to be ConnectorJ compatible won't hurt anyone and would prevent surprises for the users. Thanks for your suggestion!
          keith.chew Keith Chew added a comment -

          Hi Vladislav

          Hmm, getInt() is throwing the exception for me. I am using v1.1.0, are you using the unreleased v1.2.0?

          Also, I am using a prepared statement, can you try:

          PreparedStatement preparedStatement = connection.prepareStatement("select count from information_schema.tables");
          ResultSet rs = preparedStatement.executeQuery();

          if you think that makes a difference.

          Regards
          Keith

          keith.chew Keith Chew added a comment - Hi Vladislav Hmm, getInt() is throwing the exception for me. I am using v1.1.0, are you using the unreleased v1.2.0? Also, I am using a prepared statement, can you try: PreparedStatement preparedStatement = connection.prepareStatement("select count from information_schema.tables"); ResultSet rs = preparedStatement.executeQuery(); if you think that makes a difference. Regards Keith
          keith.chew Keith Chew added a comment -

          Hi Vladislav

          OK, I think I know the problem. I am using p6spy (http://sourceforge.net/projects/p6spy/), which I believe is calling getColumnName() instead of getColumnLabel(). The workaround keeps p6spy happy, so will stick with it for now.

          Thanks again for your help.

          Regards
          Keith

          keith.chew Keith Chew added a comment - Hi Vladislav OK, I think I know the problem. I am using p6spy ( http://sourceforge.net/projects/p6spy/ ), which I believe is calling getColumnName() instead of getColumnLabel(). The workaround keeps p6spy happy, so will stick with it for now. Thanks again for your help. Regards Keith

          People

            wlad Vladislav Vaintroub
            keith.chew Keith Chew
            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.