[Commits] Rev 408: Refactor code for getGeneratedKeys() - remove unnecessary classes. in file:///H:/bzr/mariadb-java-client/

Vladislav Vaintroub wlad at montyprogram.com
Wed Feb 13 18:28:14 EET 2013


At file:///H:/bzr/mariadb-java-client/

------------------------------------------------------------
revno: 408
revision-id: wlad at montyprogram.com-20130213162801-tfmiw1lova983zr3
parent: wlad at montyprogram.com-20130213100514-yli9jey98up7wzaf
committer: Vladislav Vaintroub <wlad at montyprogram.com>
branch nick: mariadb-java-client
timestamp: Wed 2013-02-13 17:28:01 +0100
message:
  Refactor code for getGeneratedKeys() - remove unnecessary classes.
-------------- next part --------------
=== removed file 'src/main/java/org/mariadb/jdbc/MySQLGeneratedKeysResultSet.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLGeneratedKeysResultSet.java	2012-11-27 17:47:27 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLGeneratedKeysResultSet.java	1970-01-01 00:00:00 +0000
@@ -1,22 +0,0 @@
-package org.mariadb.jdbc;
-
-import org.mariadb.jdbc.internal.common.queryresults.*;
-import org.mariadb.jdbc.internal.mysql.MySQLProtocol;
-
-import java.sql.*;
-
-public class MySQLGeneratedKeysResultSet extends MySQLResultSet {
-    public MySQLGeneratedKeysResultSet(QueryResult dqr, Statement statement, MySQLProtocol protocol) {
-       super(dqr,statement, protocol);
-    }
-
-    public int findColumn(String columnLabel) throws SQLException {
-        /*
-         There is only a single column returned in generated keys (autoincrement value),
-         and we do not know the real name of the primary  key, because it is not returned by the protocol
-         (and because we're too lazy  to check in information schema, this is expensive operation).
-         At the moment the solution is to map just any name to index 1.
-        */
-        return 1;
-    }
-}

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLResultSet.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLResultSet.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLResultSet.java	2013-02-13 16:28:01 +0000
@@ -3685,12 +3685,12 @@ public class MySQLResultSet implements R
     * @param columnName  - string array of column names
     * @param columnTypes - column types
     * @data - each element of this array represents a complete row in the ResultSet.
-    *  
     * Each value is given in its string representation, as in MySQL text protocol, except boolean (BIT(1)) values
     * that are represented as "1" or "0" strings
+    * @findColumnReturnsOne - special parameter, used only in generated key result sets
     */
     static ResultSet createResultSet(String[] columnNames, MySQLType.Type[] columnTypes, String[][] data, 
-            MySQLProtocol protocol)  {
+            MySQLProtocol protocol, boolean findColumnReturnsOne)  {
         int N = columnNames.length;
         ColumnInformation[] columns = new ColumnInformation[N];
         
@@ -3715,7 +3715,25 @@ public class MySQLResultSet implements R
             }
             rows.add(row);
         }
+        if (findColumnReturnsOne) {
+            return new MySQLResultSet(new CachedSelectResult(columns , rows, (short)0),
+                    null, protocol) {
+                public int findColumn(String name) {
+                    return 1;
+                } 
+            };
+        }
         return new MySQLResultSet(new CachedSelectResult(columns , rows, (short)0),
                 null, protocol);
     }
+    
+    static ResultSet createResultSet(String[] columnNames, MySQLType.Type[] columnTypes, String[][] data, 
+            MySQLProtocol protocol)  {
+        return createResultSet(columnNames, columnTypes, data, protocol,false);
+    }
+    
+    static ResultSet createGeneratedKeysResultSet(long id, MySQLProtocol protocol) {
+        return createResultSet(new String[]{"insert_id"}, new MySQLType.Type[] {MySQLType.Type.BIGINT},
+                new String[][]{{""+id}}, protocol,true);
+    }
 }

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLResultSetMetaData.java	2013-02-13 16:28:01 +0000
@@ -122,7 +122,7 @@ public class MySQLResultSetMetaData impl
      * @throws java.sql.SQLException if a database access error occurs
      */
     public boolean isCurrency(final int column) throws SQLException {
-        return false;  // no currency columns in drizzle
+        return false;
     }
 
     /**

=== modified file 'src/main/java/org/mariadb/jdbc/MySQLStatement.java'
--- a/src/main/java/org/mariadb/jdbc/MySQLStatement.java	2013-02-13 10:05:14 +0000
+++ b/src/main/java/org/mariadb/jdbc/MySQLStatement.java	2013-02-13 16:28:01 +0000
@@ -651,8 +651,7 @@ public class MySQLStatement implements S
      */
     public ResultSet getGeneratedKeys() throws SQLException {
         if (queryResult != null && queryResult.getResultSetType() == ResultSetType.MODIFY) {
-            final QueryResult genRes = ((ModifyQueryResult) queryResult).getGeneratedKeysResult();
-            return new MySQLGeneratedKeysResultSet(genRes, this, protocol);
+            return MySQLResultSet.createGeneratedKeysResultSet(((ModifyQueryResult)queryResult).getInsertId(), protocol);
         }
         return MySQLResultSet.EMPTY;
     }

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/CachedSelectResult.java	2013-02-13 16:28:01 +0000
@@ -106,7 +106,7 @@ public  class CachedSelectResult extends
      * @param i index, starts at 0
      * @return
      */
-    public ValueObject getValueObject(final int i) throws NoSuchColumnException {
+    public ValueObject getValueObject(int i) throws NoSuchColumnException {
         if (rowPointer < 0) {
             throw new NoSuchColumnException("Current position is before the first row");
         }
@@ -114,7 +114,7 @@ public  class CachedSelectResult extends
             throw new NoSuchColumnException("Current position is after the last row");
         }
         ValueObject[] row = resultSet.get(rowPointer);
-        if (i < 0 || i > row.length) {
+        if (i < 0 || i >= row.length) {
             throw new NoSuchColumnException("No such column: " + i);
         }
         return row[i];

=== removed file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/InsertIdQueryResult.java	1970-01-01 00:00:00 +0000
@@ -1,112 +0,0 @@
-/*
-MariaDB Client for Java
-
-Copyright (c) 2012 Monty Program Ab.
-
-This library is free software; you can redistribute it and/or modify it under
-the terms of the GNU Lesser General Public License as published by the Free
-Software Foundation; either version 2.1 of the License, or (at your option)
-any later version.
-
-This library is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
-for more details.
-
-You should have received a copy of the GNU Lesser General Public License along
-with this library; if not, write to Monty Program Ab info at montyprogram.com.
-
-This particular MariaDB Client for Java file is work
-derived from a Drizzle-JDBC. Drizzle-JDBC file which is covered by subject to
-the following copyright and notice provisions:
-
-Copyright (c) 2009-2011, Marcus Eriksson
-
-Redistribution and use in source and binary forms, with or without modification,
-are permitted provided that the following conditions are met:
-Redistributions of source code must retain the above copyright notice, this list
-of conditions and the following disclaimer.
-
-Redistributions in binary form must reproduce the above copyright notice, this
-list of conditions and the following disclaimer in the documentation and/or
-other materials provided with the distribution.
-
-Neither the name of the driver nor the names of its contributors may not be
-used to endorse or promote products derived from this software without specific
-prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS  AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
-OF SUCH DAMAGE.
-*/
-package org.mariadb.jdbc.internal.common.queryresults;
-
-import org.mariadb.jdbc.internal.common.ColumnInformation;
-import org.mariadb.jdbc.internal.common.GeneratedIdValueObject;
-import org.mariadb.jdbc.internal.common.ValueObject;
-import org.mariadb.jdbc.internal.mysql.MySQLColumnInformation;
-import org.mariadb.jdbc.internal.mysql.MySQLType;
-
-
-public class InsertIdQueryResult extends SelectQueryResult {
-
-    private final long insertId;
-    private int rowPointer = 0;
-    private final long rows;
-    private static ColumnInformation[] ci;
-
-
-    public InsertIdQueryResult(final long insertId, final long rows) {
-        this.insertId = insertId;
-        this.rows = rows;
-    }
-
-    public ValueObject getValueObject(final int index) throws NoSuchColumnException {
-        if (index != 0) {
-            throw new NoSuchColumnException("No such column: " + index);
-        }
-        return new GeneratedIdValueObject(insertId);
-    }
-
-    public int getRows() {
-        return (int) rows;
-    }
-
-    public void moveRowPointerTo(final int i) {
-
-    }
-
-    public int getRowPointer() {
-        return rowPointer;
-    }
-
-    public boolean next() {
-        return rowPointer++ < rows;
-    }
-
-    public synchronized ColumnInformation[] getColumnInformation() {
-        if (ci != null)
-            return ci;
-        MySQLColumnInformation info = MySQLColumnInformation.create("insert_id", MySQLType.Type.BIGINT);
-        ci = new ColumnInformation[] {info};
-        return ci;
-    }
-
-
-    public boolean isBeforeFirst() {
-        return false;
-    }
-    public boolean  isAfterLast() {
-      return rowPointer >= rows;
-    }
-    public void close() {
-
-    }
-}

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/ModifyQueryResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/ModifyQueryResult.java	2012-12-21 18:15:36 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/ModifyQueryResult.java	2013-02-13 16:28:01 +0000
@@ -56,6 +56,4 @@ public abstract class ModifyQueryResult 
     public abstract String getMessage();
 
     public abstract long getInsertId();
-
-    public abstract QueryResult getGeneratedKeysResult();
 }

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/StreamingSelectResult.java	2013-02-13 16:28:01 +0000
@@ -11,7 +11,6 @@ import org.mariadb.jdbc.internal.mysql.M
 import org.mariadb.jdbc.internal.mysql.packet.MySQLRowPacket;
 
 import java.io.IOException;
-import java.util.List;
 
 public class StreamingSelectResult extends SelectQueryResult {
     PacketFetcher packetFetcher;

=== modified file 'src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java'
--- a/src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java	2013-02-12 21:29:01 +0000
+++ b/src/main/java/org/mariadb/jdbc/internal/common/queryresults/UpdateResult.java	2013-02-13 16:28:01 +0000
@@ -57,14 +57,13 @@ public class UpdateResult extends Modify
     private final short warnings;
     private final String message;
     private final long insertId;
-    private final QueryResult generatedKeysResult;
+
 
     public UpdateResult(final long updateCount, final short warnings, final String message, final long insertId) {
         this.updateCount = updateCount;
         this.warnings = warnings;
         this.message = message;
         this.insertId = insertId;
-        generatedKeysResult = new InsertIdQueryResult(insertId, updateCount);
     }
 
     public long getUpdateCount() {
@@ -76,7 +75,6 @@ public class UpdateResult extends Modify
     }
 
     public void close() {
-        generatedKeysResult.close();
     }
 
     public short getWarnings() {
@@ -98,8 +96,4 @@ public class UpdateResult extends Modify
     public long getInsertId() {
         return insertId;
     }
-
-    public QueryResult getGeneratedKeysResult() {
-        return generatedKeysResult;
-    }
 }

=== modified file 'src/test/java/org/mariadb/jdbc/DriverTest.java'
--- a/src/test/java/org/mariadb/jdbc/DriverTest.java	2013-01-17 22:34:26 +0000
+++ b/src/test/java/org/mariadb/jdbc/DriverTest.java	2013-02-13 16:28:01 +0000
@@ -744,12 +744,14 @@ public class DriverTest extends BaseTest
       Statement st = connection.createStatement();
       st.executeUpdate("insert into genkeys(dataField) values('a')", Statement.RETURN_GENERATED_KEYS);
       ResultSet rs = st.getGeneratedKeys();
+      assertTrue(rs.next());
       assertEquals(rs.getInt(1), 1);
       assertEquals(rs.getInt("priKey"), 1);
       assertEquals(rs.getInt("foo"),1);
       int indexes[] = {1,2,3};
       st.executeUpdate("insert into genkeys(dataField) values('b')", indexes);
       rs = st.getGeneratedKeys();
+      assertTrue(rs.next());
       assertEquals(rs.getInt(1), 2);
       try {
         assertEquals(rs.getInt(2), 2);
@@ -761,6 +763,7 @@ public class DriverTest extends BaseTest
       String columnNames[] = {"priKey", "Alice", "Bob"};
       st.executeUpdate("insert into genkeys(dataField) values('c')", columnNames);
       rs = st.getGeneratedKeys();
+      assertTrue(rs.next());
       for(int i=0; i < 3; i++)  {
           assertEquals(rs.getInt(columnNames[i]), 3);
       }



More information about the commits mailing list