You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2013/04/02 23:40:44 UTC

svn commit: r1463764 - in /jena/Experimental/jena-jdbc/jena-jdbc-core/src: main/java/org/apache/jena/jdbc/results/ main/java/org/apache/jena/jdbc/utils/ test/java/org/apache/jena/jdbc/results/

Author: rvesse
Date: Tue Apr  2 21:40:44 2013
New Revision: 1463764

URL: http://svn.apache.org/r1463764
Log:
Cover date times in result set tests

Modified:
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaJdbcResultSet.java
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/utils/JenaJdbcNodeUtils.java
    jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/results/AbstractResultSetTests.java

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaJdbcResultSet.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaJdbcResultSet.java?rev=1463764&r1=1463763&r2=1463764&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaJdbcResultSet.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/results/JenaJdbcResultSet.java Tue Apr  2 21:40:44 2013
@@ -335,43 +335,35 @@ public abstract class JenaJdbcResultSet 
     }
 
     public Time getTime(int columnIndex) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
+        return this.getTime(this.findColumnLabel(columnIndex));
     }
 
     public Time getTime(String columnLabel) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public Time getTime(int columnIndex, Calendar cal) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public Time getTime(String columnLabel, Calendar cal) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
+        Node n = this.getNode(columnLabel);
+        if (n == null) {
+            this.setNull(true);
+            return null;
+        } else {
+            // Try to marshal into a time
+            this.setNull(false);
+            return JenaJdbcNodeUtils.toTime(n);
+        }
     }
 
     public Timestamp getTimestamp(int columnIndex) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
+        return this.getTimestamp(this.findColumnLabel(columnIndex));
     }
 
     public Timestamp getTimestamp(String columnLabel) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
-        // TODO Auto-generated method stub
-        return null;
+        Node n = this.getNode(columnLabel);
+        if (n == null) {
+            this.setNull(true);
+            return null;
+        } else {
+            // Try to marshal into a timestamp
+            this.setNull(false);
+            return JenaJdbcNodeUtils.toTimestamp(n);
+        }
     }
 
     public URL getURL(int columnIndex) throws SQLException {
@@ -520,6 +512,22 @@ public abstract class JenaJdbcResultSet 
     public Statement getStatement() throws SQLException {
         return this.statement;
     }
+    
+    public Time getTime(int columnIndex, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException();
+    }
+
+    public Time getTime(String columnLabel, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException();
+    }
+    
+    public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException();
+    }
+
+    public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
+        throw new SQLFeatureNotSupportedException();
+    }
 
     @Deprecated
     public InputStream getUnicodeStream(int columnIndex) throws SQLException {

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/utils/JenaJdbcNodeUtils.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/utils/JenaJdbcNodeUtils.java?rev=1463764&r1=1463763&r2=1463764&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/utils/JenaJdbcNodeUtils.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/utils/JenaJdbcNodeUtils.java Tue Apr  2 21:40:44 2013
@@ -22,6 +22,8 @@ import java.math.BigDecimal;
 import java.net.URL;
 import java.sql.Date;
 import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -32,12 +34,12 @@ import com.hp.hpl.jena.vocabulary.XSD;
 
 /**
  * Class with helpful utility methods for Jena JDBC
- *
+ * 
  */
 public class JenaJdbcNodeUtils {
-    
+
     private static Set<String> numericTypes = new HashSet<String>();
-    
+
     static {
         numericTypes.add(XSD.decimal.getURI());
         numericTypes.add(XSD.integer.getURI());
@@ -59,17 +61,22 @@ public class JenaJdbcNodeUtils {
     /**
      * Private constructor prevents instantiation
      */
-    private JenaJdbcNodeUtils() { }
+    private JenaJdbcNodeUtils() {
+    }
 
     /**
      * Tries to convert a node to a boolean
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Boolean
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static boolean toBoolean(Node n) throws SQLException {
         try {
-            if (n == null) return false;
+            if (n == null)
+                return false;
             if (n.isLiteral()) {
                 if (n.getLiteralDatatypeURI().equals(XSD.xboolean.getURI())) {
                     return Boolean.parseBoolean(n.getLiteralLexicalForm());
@@ -89,16 +96,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a boolean", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a byte
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Byte
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static byte toByte(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return Byte.decode(n.getLiteralLexicalForm());
             } else {
@@ -112,16 +123,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a byte", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a short integer
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Short Integer
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static short toShort(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return Short.parseShort(n.getLiteralLexicalForm());
             } else {
@@ -135,16 +150,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to an integer", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to an integer
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Integer
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static int toInt(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return NodeFactoryExtra.nodeToInt(n);
             } else {
@@ -158,16 +177,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to an integer", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a long integer
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Long Integer
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static long toLong(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return NodeFactoryExtra.nodeToLong(n);
             } else {
@@ -181,16 +204,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a long integer", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a float
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Float
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static float toFloat(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return NodeFactoryExtra.nodeToFloat(n);
             } else {
@@ -204,16 +231,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a float", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a double
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Double
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static double toDouble(Node n) throws SQLException {
         try {
-            if (n == null) return 0;
+            if (n == null)
+                return 0;
             if (n.isLiteral()) {
                 return NodeFactoryExtra.nodeToDouble(n);
             } else {
@@ -227,16 +258,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a double", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a decimal
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Decimal
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static BigDecimal toDecimal(Node n) throws SQLException {
         try {
-            if (n == null) return null;
+            if (n == null)
+                return null;
             if (n.isLiteral()) {
                 return new BigDecimal(n.getLiteralLexicalForm());
             } else {
@@ -250,18 +285,23 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a decimal", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a date
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return Date
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static Date toDate(Node n) throws SQLException {
         try {
-            if (n == null) return null;
+            if (n == null)
+                return null;
             if (n.isLiteral()) {
-                return new Date(NodeValue.xmlDatatypeFactory.newXMLGregorianCalendar(n.getLiteralLexicalForm()).toGregorianCalendar().getTimeInMillis());
+                return new Date(NodeValue.xmlDatatypeFactory.newXMLGregorianCalendar(n.getLiteralLexicalForm())
+                        .toGregorianCalendar().getTimeInMillis());
             } else {
                 throw new SQLException("Unable to marshal a non-literal to a date");
             }
@@ -273,16 +313,76 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a date", e);
         }
     }
-    
+
+    /**
+     * Tries to convert a node to a time
+     * 
+     * @param n
+     *            Node
+     * @return Time
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
+     */
+    public static Time toTime(Node n) throws SQLException {
+        try {
+            if (n == null)
+                return null;
+            if (n.isLiteral()) {
+                return new Time(NodeValue.xmlDatatypeFactory.newXMLGregorianCalendar(n.getLiteralLexicalForm())
+                        .toGregorianCalendar().getTimeInMillis());
+            } else {
+                throw new SQLException("Unable to marshal a non-literal to a time");
+            }
+        } catch (SQLException e) {
+            // Throw as is
+            throw e;
+        } catch (Exception e) {
+            // Wrap other exceptions
+            throw new SQLException("Unable to marshal the value to a time", e);
+        }
+    }
+
+    /**
+     * Tries to convert a node to a timestamp
+     * 
+     * @param n
+     *            Node
+     * @return Timestamp
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
+     */
+    public static Timestamp toTimestamp(Node n) throws SQLException {
+        try {
+            if (n == null)
+                return null;
+            if (n.isLiteral()) {
+                return new Timestamp(NodeValue.xmlDatatypeFactory.newXMLGregorianCalendar(n.getLiteralLexicalForm())
+                        .toGregorianCalendar().getTimeInMillis());
+            } else {
+                throw new SQLException("Unable to marshal a non-literal to a timestamp");
+            }
+        } catch (SQLException e) {
+            // Throw as is
+            throw e;
+        } catch (Exception e) {
+            // Wrap other exceptions
+            throw new SQLException("Unable to marshal the value to a timestamp", e);
+        }
+    }
+
     /**
      * Tries to convert a noew to a string
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return String
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static String toString(Node n) throws SQLException {
         try {
-            if (n == null) return null;
+            if (n == null)
+                return null;
             if (n.isURI()) {
                 return n.getURI();
             } else if (n.isLiteral()) {
@@ -302,16 +402,20 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a string", e);
         }
     }
-    
+
     /**
      * Tries to convert a node to a URL
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return URL or null
-     * @throws SQLException Thrown if the node cannot be converted
+     * @throws SQLException
+     *             Thrown if the node cannot be converted
      */
     public static URL toURL(Node n) throws SQLException {
         try {
-            if (n == null) return null;
+            if (n == null)
+                return null;
             if (n.isURI()) {
                 return new URL(n.getURI());
             } else {
@@ -325,9 +429,10 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal the value to a URL", e);
         }
     }
-    
+
     private static long parseAsInteger(Node n) throws SQLException {
-        if (n == null) throw new SQLException("Unable to marshal a null to an integer");
+        if (n == null)
+            throw new SQLException("Unable to marshal a null to an integer");
         if (n.isLiteral()) {
             try {
                 String lex = n.getLiteralLexicalForm();
@@ -343,15 +448,19 @@ public class JenaJdbcNodeUtils {
             throw new SQLException("Unable to marshal a non-literal to an integer");
         }
     }
-    
+
     /**
      * Gets whether a node has a numeric datatype
-     * @param n Node
+     * 
+     * @param n
+     *            Node
      * @return True if a numeric datatype, false otherwise
      */
     private static boolean hasNumericDatatype(Node n) {
-        if (n == null) return false;
-        if (!n.isLiteral()) return false;
+        if (n == null)
+            return false;
+        if (!n.isLiteral())
+            return false;
         return numericTypes.contains(n.getLiteralDatatypeURI());
     }
 }

Modified: jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/results/AbstractResultSetTests.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/results/AbstractResultSetTests.java?rev=1463764&r1=1463763&r2=1463764&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/results/AbstractResultSetTests.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-core/src/test/java/org/apache/jena/jdbc/results/AbstractResultSetTests.java Tue Apr  2 21:40:44 2013
@@ -413,4 +413,76 @@ public abstract class AbstractResultSetT
         rset.close();
         Assert.assertTrue(rset.isClosed());
     }
+    
+    /**
+     * Tests that SELECT result values can be marshalled to dates OK
+     * 
+     * @throws SQLException
+     */
+    @Test
+    public void test_results_select_dates_01() throws SQLException {
+        ResultSet rset = this.createResults(ds, "SELECT ?o { ?s ?p ?o . FILTER(DATATYPE(?o) = <" + XSD.dateTime.toString() + ">) }");
+        Assert.assertFalse(rset.isClosed());
+        Assert.assertTrue(rset.isBeforeFirst());
+        Assert.assertFalse(rset.isLast());
+
+        // Check all rows allow us to marshal strings OK
+        while (rset.next()) {
+            Assert.assertNotNull(rset.getString("o"));
+            Assert.assertNotNull(rset.getDate("o"));
+            Assert.assertFalse(rset.wasNull());
+        }
+
+        Assert.assertTrue(rset.isAfterLast());
+        rset.close();
+        Assert.assertTrue(rset.isClosed());
+    }
+    
+    /**
+     * Tests that SELECT result values can be marshalled to dates OK
+     * 
+     * @throws SQLException
+     */
+    @Test
+    public void test_results_select_dates_02() throws SQLException {
+        ResultSet rset = this.createResults(ds, "SELECT ?o { ?s ?p ?o . FILTER(DATATYPE(?o) = <" + XSD.dateTime.toString() + ">) }");
+        Assert.assertFalse(rset.isClosed());
+        Assert.assertTrue(rset.isBeforeFirst());
+        Assert.assertFalse(rset.isLast());
+
+        // Check all rows allow us to marshal strings OK
+        while (rset.next()) {
+            Assert.assertNotNull(rset.getString("o"));
+            Assert.assertNotNull(rset.getTime("o"));
+            Assert.assertFalse(rset.wasNull());
+        }
+
+        Assert.assertTrue(rset.isAfterLast());
+        rset.close();
+        Assert.assertTrue(rset.isClosed());
+    }
+    
+    /**
+     * Tests that SELECT result values can be marshalled to dates OK
+     * 
+     * @throws SQLException
+     */
+    @Test
+    public void test_results_select_dates_03() throws SQLException {
+        ResultSet rset = this.createResults(ds, "SELECT ?o { ?s ?p ?o . FILTER(DATATYPE(?o) = <" + XSD.dateTime.toString() + ">) }");
+        Assert.assertFalse(rset.isClosed());
+        Assert.assertTrue(rset.isBeforeFirst());
+        Assert.assertFalse(rset.isLast());
+
+        // Check all rows allow us to marshal strings OK
+        while (rset.next()) {
+            Assert.assertNotNull(rset.getString("o"));
+            Assert.assertNotNull(rset.getTimestamp("o"));
+            Assert.assertFalse(rset.wasNull());
+        }
+
+        Assert.assertTrue(rset.isAfterLast());
+        rset.close();
+        Assert.assertTrue(rset.isClosed());
+    }
 }