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.
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
package com.samskivert.jdbc.depot;
|
package com.samskivert.jdbc.depot;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ public class BindVisitor implements ExpressionVisitor
|
|||||||
{
|
{
|
||||||
for (Comparable<?> value : whereCondition.getValues()) {
|
for (Comparable<?> value : whereCondition.getValues()) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
_stmt.setObject(_argIdx ++, value);
|
writeValueToStatement(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,15 +90,14 @@ public class BindVisitor implements ExpressionVisitor
|
|||||||
public void visit (MultiKey<? extends PersistentRecord> key)
|
public void visit (MultiKey<? extends PersistentRecord> key)
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
for (Map.Entry<String, Comparable<?>> entry : key.getSingleFieldsMap().entrySet()) {
|
for (Map.Entry<String, Comparable<?>> entry : key.getSingleFieldsMap().entrySet()) {
|
||||||
if (entry.getValue() != null) {
|
if (entry.getValue() != null) {
|
||||||
_stmt.setObject(_argIdx ++, entry.getValue());
|
writeValueToStatement(entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Comparable<?>[] values = key.getMultiValues();
|
Comparable<?>[] values = key.getMultiValues();
|
||||||
for (int ii = 0; ii < values.length; ii++) {
|
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();
|
Comparable<?>[] values = in.getValues();
|
||||||
for (int ii = 0; ii < values.length; ii++) {
|
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
|
public void visit (Limit limit) throws Exception
|
||||||
{
|
{
|
||||||
_stmt.setObject(_argIdx++, limit.getCount());
|
_stmt.setInt(_argIdx++, limit.getCount());
|
||||||
_stmt.setObject(_argIdx++, limit.getOffset());
|
_stmt.setInt(_argIdx++, limit.getOffset());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit (LiteralExp literalExp) throws Exception
|
public void visit (LiteralExp literalExp) throws Exception
|
||||||
@@ -190,13 +190,7 @@ public class BindVisitor implements ExpressionVisitor
|
|||||||
|
|
||||||
public void visit (ValueExp valueExp) throws Exception
|
public void visit (ValueExp valueExp) throws Exception
|
||||||
{
|
{
|
||||||
// workaround for postgres bug, fixed in next release:
|
writeValueToStatement(valueExp.getValue());
|
||||||
// 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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visit (Exists<? extends PersistentRecord> exists)
|
public void visit (Exists<? extends PersistentRecord> 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 DepotTypes _types;
|
||||||
protected PreparedStatement _stmt;
|
protected PreparedStatement _stmt;
|
||||||
protected int _argIdx;
|
protected int _argIdx;
|
||||||
|
|||||||
Reference in New Issue
Block a user