Detect our type based on the first element of the array rather than the type of

the array which may be Comparable[] or Object[] and therefore not informative.
This commit is contained in:
Michael Bayne
2009-04-27 22:42:23 +00:00
parent 5a1dfdc306
commit cbde87c6a2
@@ -24,6 +24,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import com.samskivert.depot.DatabaseException; import com.samskivert.depot.DatabaseException;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.Conditionals.In; import com.samskivert.depot.operator.Conditionals.In;
/** /**
@@ -34,23 +35,28 @@ public class PostgreSQL4Builder extends PostgreSQLBuilder
public class PG4BuildVisitor extends PGBuildVisitor public class PG4BuildVisitor extends PGBuildVisitor
{ {
@Override public Void visit (In in) { @Override public Void visit (In in) {
// if the In() expression is empty, replace it with a 'false'
final Comparable<?>[] values = in.getValues();
if (values.length == 0) {
new ValueExp(false).accept(this);
return null;
}
in.getColumn().accept(this); in.getColumn().accept(this);
_builder.append(" = any (?)"); _builder.append(" = any (?)");
final Comparable<?>[] values = in.getValues();
_bindables.add(new Bindable() { _bindables.add(new Bindable() {
public void doBind (Connection conn, PreparedStatement stmt, int argIdx) public void doBind (Connection conn, PreparedStatement stmt, int argIdx)
throws Exception { throws Exception {
stmt.setObject(argIdx, conn.createArrayOf(getElementType(values), stmt.setObject(argIdx, conn.createArrayOf(getElementType(values[0]),
(Object[])values)); (Object[])values));
} }
protected String getElementType (Comparable<?>[] values) { protected String getElementType (Object value) {
if (values instanceof Integer[]) { if (value instanceof Integer) {
return "integer"; return "integer";
} else if (values instanceof String[]) { } else if (value instanceof String) {
return "character varying"; return "character varying";
} else { } else {
throw new DatabaseException( throw new DatabaseException(
"Don't know how to make Postgres array for " + values.getClass()); "Don't know how to make Postgres array for " + value.getClass());
} }
} }
}); });