From 338bb62d9fa661627e4f84a4fd28e04d509e537f Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Thu, 7 Aug 2008 06:02:41 +0000 Subject: [PATCH] PreparedStatement.setObject() does a lot of runtime type checking, so we don't have to do setShort(), setString(), etc. But we do have to convert ByteEnum's to their byte representation. --- .../samskivert/jdbc/depot/BindVisitor.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/BindVisitor.java b/src/java/com/samskivert/jdbc/depot/BindVisitor.java index 3d2b290..563adf9 100644 --- a/src/java/com/samskivert/jdbc/depot/BindVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BindVisitor.java @@ -21,6 +21,7 @@ package com.samskivert.jdbc.depot; import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.Map; import java.util.Set; @@ -75,7 +76,7 @@ public class BindVisitor implements ExpressionVisitor { for (Comparable value : whereCondition.getValues()) { if (value != null) { - _stmt.setObject(_argIdx ++, value); + writeValueToStatement(value); } } } @@ -89,15 +90,14 @@ public class BindVisitor implements ExpressionVisitor public void visit (MultiKey key) throws Exception { - for (Map.Entry> entry : key.getSingleFieldsMap().entrySet()) { if (entry.getValue() != null) { - _stmt.setObject(_argIdx ++, entry.getValue()); + writeValueToStatement(entry.getValue()); } } Comparable[] values = key.getMultiValues(); for (int ii = 0; ii < values.length; ii++) { - _stmt.setObject(_argIdx ++, values[ii]); + writeValueToStatement(values[ii]); } } @@ -133,7 +133,7 @@ public class BindVisitor implements ExpressionVisitor { Comparable[] values = in.getValues(); for (int ii = 0; ii < values.length; ii++) { - _stmt.setObject(_argIdx ++, values[ii]); + writeValueToStatement(values[ii]); } } @@ -179,8 +179,8 @@ public class BindVisitor implements ExpressionVisitor public void visit (Limit limit) throws Exception { - _stmt.setObject(_argIdx++, limit.getCount()); - _stmt.setObject(_argIdx++, limit.getOffset()); + _stmt.setInt(_argIdx++, limit.getCount()); + _stmt.setInt(_argIdx++, limit.getOffset()); } public void visit (LiteralExp literalExp) throws Exception @@ -190,13 +190,7 @@ public class BindVisitor implements ExpressionVisitor public void visit (ValueExp valueExp) throws Exception { - // workaround for postgres bug, fixed in next release: - // http://archives.postgresql.org/pgsql-jdbc/2007-06/msg00080.php - if (valueExp.getValue() instanceof Byte) { - _stmt.setByte(_argIdx ++, (Byte) valueExp.getValue()); - } else { - _stmt.setObject(_argIdx ++, valueExp.getValue()); - } + writeValueToStatement(valueExp.getValue()); } public void visit (Exists exists) @@ -278,6 +272,18 @@ public class BindVisitor implements ExpressionVisitor } } + // write the value to the next argument slot in the prepared statement + protected void writeValueToStatement (Object value) + throws SQLException + { + // setObject handles almost all conversions internally, but enums require special care + if (value instanceof ByteEnum) { + _stmt.setByte(_argIdx++, ((ByteEnum)value).toByte()); + } else { + _stmt.setObject(_argIdx++, value); + } + } + protected DepotTypes _types; protected PreparedStatement _stmt; protected int _argIdx;