Author: rvesse
Date: Fri May 31 19:11:15 2013
New Revision: 1488357

URL: http://svn.apache.org/r1488357
Log:
Flesh out most of the implementation for setObject() on Prepared Statements

Modified:
    
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaPreparedStatement.java

Modified: 
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaPreparedStatement.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaPreparedStatement.java?rev=1488357&r1=1488356&r2=1488357&view=diff
==============================================================================
--- 
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaPreparedStatement.java
 (original)
+++ 
jena/Experimental/jena-jdbc/jena-jdbc-core/src/main/java/org/apache/jena/jdbc/statements/JenaPreparedStatement.java
 Fri May 31 19:11:15 2013
@@ -21,6 +21,7 @@ package org.apache.jena.jdbc.statements;
 import java.io.InputStream;
 import java.io.Reader;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.net.URI;
 import java.net.URL;
 import java.sql.Array;
@@ -40,6 +41,7 @@ import java.sql.SQLXML;
 import java.sql.Time;
 import java.sql.Timestamp;
 import java.sql.Types;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.TimeZone;
 
@@ -312,38 +314,42 @@ public abstract class JenaPreparedStatem
 
     @Override
     public void setObject(int parameterIndex, Object value) throws 
SQLException {
+        if (value == null) throw new SQLException("Setting a null value is not 
permitted");
+        
         if (value instanceof Node) {
             this.setParameter(parameterIndex, (Node) value);
         } else if (value instanceof RDFNode) {
-            this.setParameter(parameterIndex, ((RDFNode)value).asNode());
+            this.setParameter(parameterIndex, ((RDFNode) value).asNode());
         } else if (value instanceof String) {
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral((String)value));
+            this.setParameter(parameterIndex, 
NodeFactory.createLiteral((String) value));
         } else if (value instanceof Boolean) {
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Boolean.toString((Boolean)value), 
XSDDatatype.XSDboolean));
+            this.setParameter(parameterIndex,
+                    NodeFactory.createLiteral(Boolean.toString((Boolean) 
value), XSDDatatype.XSDboolean));
         } else if (value instanceof Long) {
-            this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Long)value));
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Long) value));
         } else if (value instanceof Integer) {
-            this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Integer)value));
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Integer) value));
         } else if (value instanceof Short) {
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Short.toString((Short)value), XSDDatatype.XSDshort));
+            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Short.toString((Short) value), XSDDatatype.XSDshort));
         } else if (value instanceof Byte) {
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Byte.toString((Byte)value), XSDDatatype.XSDbyte));
+            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Byte.toString((Byte) value), XSDDatatype.XSDbyte));
         } else if (value instanceof BigDecimal) {
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(((BigDecimal)value).toPlainString(), 
XSDDatatype.XSDdecimal));
+            this.setParameter(parameterIndex,
+                    NodeFactory.createLiteral(((BigDecimal) 
value).toPlainString(), XSDDatatype.XSDdecimal));
         } else if (value instanceof Float) {
-            this.setParameter(parameterIndex, 
NodeFactoryExtra.floatToNode((Float)value));
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.floatToNode((Float) value));
         } else if (value instanceof Double) {
-            this.setParameter(parameterIndex, 
NodeFactoryExtra.doubleToNode((Double)value));
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.doubleToNode((Double) value));
         } else if (value instanceof Date) {
             Calendar c = Calendar.getInstance();
-            c.setTimeInMillis(((Date)value).getTime());
-            this.setParameter(parameterIndex, NodeFactoryExtra.dateToNode(c));
+            c.setTimeInMillis(((Date) value).getTime());
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode(c));
         } else if (value instanceof Time) {
             Calendar c = Calendar.getInstance();
-            c.setTimeInMillis(((Time)value).getTime());
+            c.setTimeInMillis(((Time) value).getTime());
             this.setParameter(parameterIndex, NodeFactoryExtra.timeToNode(c));
         } else if (value instanceof Calendar) {
-            this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode((Calendar)value));
+            this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode((Calendar) value));
         } else if (value instanceof URL) {
             this.setParameter(parameterIndex, 
NodeFactory.createURI(value.toString()));
         } else if (value instanceof URI) {
@@ -351,89 +357,222 @@ public abstract class JenaPreparedStatem
         } else if (value instanceof IRI) {
             this.setParameter(parameterIndex, 
NodeFactory.createURI(value.toString()));
         } else {
-            throw new SQLException("setObject() received a value that could 
not be converted to a RDF node for use in a SPARQL query");
+            throw new SQLException(
+                    "setObject() received a value that could not be converted 
to a RDF node for use in a SPARQL query");
         }
     }
 
     @Override
     public void setObject(int parameterIndex, Object value, int targetSqlType) 
throws SQLException {
-        switch (targetSqlType) {
-        case Types.ARRAY:
-        case Types.BINARY:
-        case Types.BIT:
-        case Types.BLOB:
-        case Types.CHAR:
-        case Types.CLOB:
-        case Types.DATALINK:
-        case Types.DISTINCT:
-        case Types.LONGNVARCHAR:
-        case Types.LONGVARBINARY:
-        case Types.LONGVARCHAR:
-        case Types.NCHAR:
-        case Types.NCLOB:
-        case Types.NULL:
-        case Types.NUMERIC:
-        case Types.OTHER:
-        case Types.REAL:
-        case Types.REF:
-        case Types.ROWID:
-        case Types.SQLXML:
-        case Types.STRUCT:
-        case Types.VARBINARY:
-        case Types.VARCHAR:
-            throw new SQLException("The provided SQL Target Type cannot be 
translated into an appropriate RDF term type");
-        case Types.BIGINT:
-            if (value instanceof Long || value instanceof Integer || value 
instanceof Short || value instanceof Byte) {
-                this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Long)value));
-            } else if (value instanceof Node) {
-                long l = JenaJdbcNodeUtils.toLong((Node)value);
-                this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode(l));
-            } else {
-                throw new SQLException("The given value is not marshallable to 
the desired target type");
+        if (value == null) throw new SQLException("Setting a null value is not 
permitted");
+        
+        try {
+            switch (targetSqlType) {
+            case Types.ARRAY:
+            case Types.BINARY:
+            case Types.BIT:
+            case Types.BLOB:
+            case Types.CHAR:
+            case Types.CLOB:
+            case Types.DATALINK:
+            case Types.DISTINCT:
+            case Types.LONGNVARCHAR:
+            case Types.LONGVARBINARY:
+            case Types.LONGVARCHAR:
+            case Types.NCHAR:
+            case Types.NCLOB:
+            case Types.NULL:
+            case Types.NUMERIC:
+            case Types.OTHER:
+            case Types.REAL:
+            case Types.REF:
+            case Types.ROWID:
+            case Types.SQLXML:
+            case Types.STRUCT:
+            case Types.VARBINARY:
+            case Types.VARCHAR:
+                throw new SQLException("The provided SQL Target Type cannot be 
translated into an appropriate RDF term type");
+            case Types.BIGINT:
+                if (value instanceof Long || value instanceof Integer || value 
instanceof Short || value instanceof Byte) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Long) value));
+                } else if (value instanceof Node) {
+                    long l = JenaJdbcNodeUtils.toLong((Node) value);
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode(l));
+                } else if (value instanceof String) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode(Long.parseLong((String)value)));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.BOOLEAN:
+                if (value instanceof Boolean) {
+                    this.setParameter(parameterIndex,
+                            
NodeFactory.createLiteral(Boolean.toString((Boolean) value), 
XSDDatatype.XSDboolean));
+                } else if (value instanceof Node) {
+                    boolean b = JenaJdbcNodeUtils.toBoolean((Node) value);
+                    this.setParameter(parameterIndex,
+                            NodeFactory.createLiteral(Boolean.toString(b), 
XSDDatatype.XSDboolean));
+                } else if (value instanceof String) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Boolean.toString(Boolean.parseBoolean((String)value)),
 XSDDatatype.XSDboolean));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.DATE:
+                if (value instanceof Date) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Date) value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode(c));
+                } else if (value instanceof Node) {
+                    Calendar c = Calendar.getInstance();
+                    
c.setTimeInMillis(JenaJdbcNodeUtils.toDate((Node)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode(c));
+                } else if (value instanceof Time) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Time) value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode(c));
+                } else if (value instanceof Calendar) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode((Calendar) value));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.DECIMAL:
+                if (value instanceof BigDecimal) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(((BigDecimal)value).toPlainString(), 
XSDDatatype.XSDdecimal));
+                } else if (value instanceof Node) {
+                    BigDecimal d = JenaJdbcNodeUtils.toDecimal((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(d.toPlainString(), XSDDatatype.XSDdecimal));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.DOUBLE:
+                if (value instanceof Double || value instanceof Float) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.doubleToNode((Double)value));
+                } else if (value instanceof Node) {
+                    Double d = JenaJdbcNodeUtils.toDouble((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.doubleToNode(d));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.FLOAT:
+                if (value instanceof Float) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.floatToNode((Float)value));
+                } else if (value instanceof Node) {
+                    Float f = JenaJdbcNodeUtils.toFloat((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.floatToNode(f));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.INTEGER:
+                if (value instanceof Integer || value instanceof Short || 
value instanceof Byte) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Integer)value));
+                } else if (value instanceof Node) {
+                    Integer i = JenaJdbcNodeUtils.toInt((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode(i));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired target type");
+                }
+                break;
+            case Types.JAVA_OBJECT:
+                if (value instanceof Node) {
+                    this.setParameter(parameterIndex, (Node) value);
+                } else if (value instanceof RDFNode) {
+                    this.setParameter(parameterIndex, ((RDFNode) 
value).asNode());
+                } else if (value instanceof String) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral((String) value));
+                } else if (value instanceof URL) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createURI(((URL) value).toString()));
+                } else if (value instanceof URI) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createURI(((URI)value).toString()));
+                } else if (value instanceof IRI) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createURI(((IRI)value).toString()));
+                } else if (value instanceof BigDecimal) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(((BigDecimal)value).toPlainString(), 
XSDDatatype.XSDdecimal));
+                } else if (value instanceof Boolean) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Boolean.toString(((Boolean)value)), 
XSDDatatype.XSDboolean));
+                } else if (value instanceof Byte) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Byte.toString((Byte)value), XSDDatatype.XSDbyte));
+                } else if (value instanceof Double) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.doubleToNode((Double)value));
+                } else if (value instanceof Float) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.floatToNode((Float)value));
+                } else if (value instanceof Integer) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Integer)value));
+                } else if (value instanceof Long) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.intToNode((Long)value));
+                } else if (value instanceof Date) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Date)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode(c));
+                } else if (value instanceof Time) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Time)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode(c));
+                } else if (value instanceof Calendar) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.dateTimeToNode((Calendar)value));
+                } else { 
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(value.toString()));
+                }
+                break;
+            case Types.NVARCHAR:
+                this.setParameter(parameterIndex, 
NodeFactory.createLiteral(value.toString()));
+                break;
+            case Types.SMALLINT:
+                if (value instanceof Short || value instanceof Byte) {
+                    Short s = (Short)value;
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Short.toString(s), XSDDatatype.XSDshort));
+                } else if (value instanceof Node) {
+                    Short s = JenaJdbcNodeUtils.toShort((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Short.toString(s), XSDDatatype.XSDshort));
+                } else if (value instanceof String) {
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Short.toString(Short.parseShort((String)value)), 
XSDDatatype.XSDshort));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired type");
+                }
+                break;
+            case Types.TIME:
+                if (value instanceof Time) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Time)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode(c));
+                } else if (value instanceof Node) {
+                    Calendar c = Calendar.getInstance();
+                    
c.setTimeInMillis(JenaJdbcNodeUtils.toDate((Node)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode(c));
+                } else if (value instanceof Date) {
+                    Calendar c = Calendar.getInstance();
+                    c.setTimeInMillis(((Date)value).getTime());
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode(c));
+                } else if (value instanceof Calendar) {
+                    this.setParameter(parameterIndex, 
NodeFactoryExtra.timeToNode((Calendar)value));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired type");
+                }
+                break;
+            case Types.TINYINT:
+                if (value instanceof Byte) {
+                    Byte b = (Byte)value;
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Byte.toString(b), XSDDatatype.XSDbyte));
+                } else if (value instanceof Node) {
+                    Byte b = JenaJdbcNodeUtils.toByte((Node)value);
+                    this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Byte.toString(b), XSDDatatype.XSDbyte));
+                } else {
+                    throw new SQLException("The given value is not 
marshallable to the desired type");
+                }
+                break;
+            default:
+                throw new SQLException("Cannot translate an unknown SQL Target 
Type into an appropriate RDF term type");
             }
-            break;
-        case Types.BOOLEAN:
-            if (value instanceof Boolean) {
-                this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Boolean.toString((Boolean)value), 
XSDDatatype.XSDboolean));
-            } else if (value instanceof Node) {
-                boolean b = JenaJdbcNodeUtils.toBoolean((Node)value);
-                this.setParameter(parameterIndex, 
NodeFactory.createLiteral(Boolean.toString((Boolean)value), 
XSDDatatype.XSDboolean));
-            } else {
-                throw new SQLException("The given value is not marshallable to 
the desired target type");
-            }
-            break;
-        case Types.DATE:
-            break;
-        case Types.DECIMAL:
-            break;
-        case Types.DOUBLE:
-            break;
-        case Types.FLOAT:
-            break;
-        case Types.INTEGER:
-            break;
-        case Types.JAVA_OBJECT:
-            if (value instanceof Node) {
-                this.setParameter(parameterIndex, (Node)value);
-            } else if (value instanceof RDFNode) {
-                this.setParameter(parameterIndex, ((RDFNode)value).asNode());
-            } else if (value instanceof String) {
-                this.setParameter(parameterIndex, 
NodeFactory.createLiteral((String)value));
-            } else if (value instanceof URL) {
-                this.setParameter(parameterIndex, 
NodeFactory.createURI(((URL)value).toString()));
-            } else 
-            break;
-        case Types.NVARCHAR:
-            this.setParameter(parameterIndex, 
NodeFactory.createLiteral(value.toString()));
-            break;
-        case Types.SMALLINT:
-            break;
-        case Types.TIME:
-            break;
-        case Types.TINYINT:
-            break;
-        default:
-            throw new SQLException("Cannot translate an unknown SQL Target 
Type into an appropriate RDF term type");
+        } catch (SQLException e) {
+            // Throw as-is
+            throw e;
+        } catch (Throwable e) {
+            // Wrap as SQL Exception
+            throw new SQLException("Unexpected error trying to marshal a value 
to the desired target type", e);
         }
     }
 
@@ -499,9 +638,11 @@ public abstract class JenaPreparedStatem
     public void setUnicodeStream(int parameterIndex, InputStream value, int 
arg2) throws SQLException {
         throw new SQLFeatureNotSupportedException();
     }
-    
+
     /**
-     * Gets a copy of the underlying {@link ParameterizedSparqlString} used to 
implement this prepared statement
+     * Gets a copy of the underlying {@link ParameterizedSparqlString} used to
+     * implement this prepared statement
+     * 
      * @return Copy of the underlying string
      */
     public ParameterizedSparqlString getParameterizedString() {


Reply via email to