From 0ece63c797c344a51705fce885b48a84c4a94441 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 23 Nov 2006 00:35:18 +0000 Subject: Query filter constants appear as SQL constants. --- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 50 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index a1d4cd9..6f797b7 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -1121,25 +1121,61 @@ class JDBCStorage extends StandardQueryFactory return e; } - mPropertyFilters.add(filter); + if (!filter.isConstant()) { + addBindParameter(filter); + } else { + RelOp op = filter.getOperator(); + + Object constant = filter.constant(); + if (constant == null) { + if (op == RelOp.EQ) { + mStatementBuilder.append("IS NULL"); + } else if (op == RelOp.NE) { + mStatementBuilder.append("IS NOT NULL"); + } else { + mStatementBuilder.append(sqlOperatorFor(op)); + mStatementBuilder.append("NULL"); + } + } else if (filter.getType() == String.class) { + mStatementBuilder.append(sqlOperatorFor(op)); + mStatementBuilder.append('\''); + mStatementBuilder.append(String.valueOf(constant).replace("'", "''")); + mStatementBuilder.append('\''); + } else if (Number.class.isAssignableFrom(filter.getBoxedType())) { + mStatementBuilder.append(sqlOperatorFor(op)); + mStatementBuilder.append(String.valueOf(constant)); + } else { + // Don't try to create literal for special type. Instead, + // fallback to bind parameter and let JDBC driver do the work. + addBindParameter(filter); + } + } + + return null; + } + private void addBindParameter(PropertyFilter filter) { RelOp op = filter.getOperator(); StorableProperty property = filter.getChainedProperty().getLastProperty(); + mPropertyFilters.add(filter); + if (property.isNullable() && (op == RelOp.EQ || op == RelOp.NE)) { mPropertyFilterNullable.add(true); mStatementBuilder.append(new NullablePropertyStatement(filter, op == RelOp.EQ)); } else { mPropertyFilterNullable.add(false); - if (op == RelOp.NE) { - mStatementBuilder.append("<>"); - } else { - mStatementBuilder.append(op.toString()); - } + mStatementBuilder.append(sqlOperatorFor(op)); mStatementBuilder.append('?'); } + } - return null; + private String sqlOperatorFor(RelOp op) { + if (op == RelOp.NE) { + return "<>"; + } else { + return op.toString(); + } } } } -- cgit v1.2.3