cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r888788 - in /cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest: ItestDBUtils.java ItestTableUtils.java ResultSetTemplate.java RowTemplate.java
Date Wed, 09 Dec 2009 12:48:40 GMT
Author: aadamchik
Date: Wed Dec  9 12:48:39 2009
New Revision: 888788

URL: http://svn.apache.org/viewvc?rev=888788&view=rev
Log:
parameterizing itest db utilities

Modified:
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestDBUtils.java
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestTableUtils.java
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ResultSetTemplate.java
    cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/RowTemplate.java

Modified: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestDBUtils.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestDBUtils.java?rev=888788&r1=888787&r2=888788&view=diff
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestDBUtils.java
(original)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestDBUtils.java
Wed Dec  9 12:48:39 2009
@@ -111,136 +111,106 @@
 
     public int getRowCount(String table) throws SQLException {
         String sql = "select count(*) from " + table;
-        
-        final int[] result = new int[1];
-        RowTemplate template = new RowTemplate(this) {
+
+        return new RowTemplate<Integer>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getInt(1);
+            Integer readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getInt(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public Object getObject(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final Object[] result = new Object[1];
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Object>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getObject(1);
+            Object readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getObject(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public byte getByte(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final byte[] result = new byte[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Byte>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getByte(1);
+            Byte readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getByte(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public byte[] getBytes(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final byte[][] result = new byte[1][];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<byte[]>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getBytes(1);
+            byte[] readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getBytes(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public int getInt(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final int[] result = new int[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Integer>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getInt(1);
+            Integer readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getInt(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public long getLong(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final long[] result = new long[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Long>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getLong(1);
+            Long readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getLong(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public double getDouble(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final double[] result = new double[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Double>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getDouble(1);
+            Double readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getDouble(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public boolean getBoolean(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final boolean[] result = new boolean[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Boolean>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getBoolean(1);
+            Boolean readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getBoolean(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public java.util.Date getUtilDate(String table, String column) throws SQLException {
@@ -251,52 +221,40 @@
     public java.sql.Date getSqlDate(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final java.sql.Date[] result = new java.sql.Date[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<java.sql.Date>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getDate(1);
+            java.sql.Date readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getDate(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public Time getTime(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final Time[] result = new Time[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Time>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getTime(1);
+            Time readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getTime(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public Timestamp getTimestamp(String table, String column) throws SQLException {
         final String sql = "select " + column + " from " + table;
 
-        final Timestamp[] result = new Timestamp[1];
-
-        RowTemplate template = new RowTemplate(this) {
+        return new RowTemplate<Timestamp>(this) {
 
             @Override
-            void readRow(ResultSet rs, String sql) throws SQLException {
-                result[0] = rs.getTimestamp(1);
+            Timestamp readRow(ResultSet rs, String sql) throws SQLException {
+                return rs.getTimestamp(1);
             }
-        };
 
-        template.execute(sql);
-        return result[0];
+        }.execute(sql);
     }
 
     public Connection getConnection() throws SQLException {

Modified: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestTableUtils.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestTableUtils.java?rev=888788&r1=888787&r2=888788&view=diff
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestTableUtils.java
(original)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ItestTableUtils.java
Wed Dec  9 12:48:39 2009
@@ -22,7 +22,6 @@
 
 /**
  * JDBC utilities for integration testing that bypass Cayenne for DB access.
- * 
  */
 public class ItestTableUtils {
 

Modified: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ResultSetTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ResultSetTemplate.java?rev=888788&r1=888787&r2=888788&view=diff
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ResultSetTemplate.java
(original)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/ResultSetTemplate.java
Wed Dec  9 12:48:39 2009
@@ -23,7 +23,7 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
-abstract class ResultSetTemplate {
+abstract class ResultSetTemplate<T> {
 
     ItestDBUtils parent;
 
@@ -31,9 +31,9 @@
         this.parent = parent;
     }
 
-    abstract void readResultSet(ResultSet rs, String sql) throws SQLException;
+    abstract T readResultSet(ResultSet rs, String sql) throws SQLException;
 
-    void execute(String sql) throws SQLException {
+    T execute(String sql) throws SQLException {
         Connection c = parent.getConnection();
         try {
 
@@ -43,7 +43,7 @@
                 ResultSet rs = st.executeQuery();
                 try {
 
-                    readResultSet(rs, sql);
+                    return readResultSet(rs, sql);
                 }
                 finally {
                     rs.close();

Modified: cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/RowTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/RowTemplate.java?rev=888788&r1=888787&r2=888788&view=diff
==============================================================================
--- cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/RowTemplate.java
(original)
+++ cayenne/main/trunk/itests/itest-common/src/main/java/org/apache/cayenne/itest/RowTemplate.java
Wed Dec  9 12:48:39 2009
@@ -25,23 +25,25 @@
  * A JDBC template for reading a single row from the database.
  * 
  */
-abstract class RowTemplate extends ResultSetTemplate {
+abstract class RowTemplate<T> extends ResultSetTemplate<T> {
 
     public RowTemplate(ItestDBUtils parent) {
         super(parent);
     }
 
-    abstract void readRow(ResultSet rs, String sql) throws SQLException;
+    abstract T readRow(ResultSet rs, String sql) throws SQLException;
 
     @Override
-    void readResultSet(ResultSet rs, String sql) throws SQLException {
+    T readResultSet(ResultSet rs, String sql) throws SQLException {
         if (rs.next()) {
 
-            readRow(rs, sql);
+            T row = readRow(rs, sql);
 
             if (rs.next()) {
                 throw new SQLException("More than one result for sql: " + sql);
             }
+
+            return row;
         }
         else {
             throw new SQLException("No results for sql: " + sql);



Mime
View raw message