From 458cad28fb8611221755f44805c1e0f4a61c61b3 Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 15 Dec 2006 23:08:26 +0000 Subject: [PATCH] Support for more operators and functions from Zell. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1997 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../jdbc/depot/expression/FunctionExp.java | 72 ++++++++++ .../jdbc/depot/operator/Arithmetic.java | 133 ++++++++++++++++++ .../jdbc/depot/operator/Conditionals.java | 44 +----- .../samskivert/jdbc/depot/operator/Logic.java | 44 +----- .../jdbc/depot/operator/SQLOperator.java | 89 ++++++++++++ 5 files changed, 296 insertions(+), 86 deletions(-) create mode 100644 src/java/com/samskivert/jdbc/depot/expression/FunctionExp.java create mode 100644 src/java/com/samskivert/jdbc/depot/operator/Arithmetic.java diff --git a/src/java/com/samskivert/jdbc/depot/expression/FunctionExp.java b/src/java/com/samskivert/jdbc/depot/expression/FunctionExp.java new file mode 100644 index 00000000..acdcd214 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/expression/FunctionExp.java @@ -0,0 +1,72 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.expression; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import com.samskivert.jdbc.depot.Query; + +/** + * An expression for a function, e.g. FLOOR(blah). + */ +public class FunctionExp + implements SQLExpression +{ + /** + * Create a new FunctionExp with the given function and arguments. + */ + public FunctionExp (String function, SQLExpression... arguments) + { + _function = function; + _arguments = arguments; + } + + // from SQLExpression + public void appendExpression (Query query, StringBuilder builder) + { + builder.append(_function); + builder.append("("); + for (int ii = 0; ii < _arguments.length; ii ++) { + if (ii > 0) { + builder.append(", "); + } + _arguments[ii].appendExpression(query, builder); + } + builder.append(")"); + } + + // from SQLExpression + public int bindArguments (PreparedStatement pstmt, int argIdx) + throws SQLException + { + for (int ii = 0; ii < _arguments.length; ii ++) { + argIdx = _arguments[ii].bindArguments(pstmt, argIdx); + } + return argIdx; + } + + /** The literal name of this function, e.g. FLOOR */ + protected String _function; + + /** The arguments to this function */ + protected SQLExpression[] _arguments; +} diff --git a/src/java/com/samskivert/jdbc/depot/operator/Arithmetic.java b/src/java/com/samskivert/jdbc/depot/operator/Arithmetic.java new file mode 100644 index 00000000..41308ca3 --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/operator/Arithmetic.java @@ -0,0 +1,133 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.operator; + +import com.samskivert.jdbc.depot.expression.ColumnExp; +import com.samskivert.jdbc.depot.expression.SQLExpression; +import com.samskivert.jdbc.depot.operator.SQLOperator.BinaryOperator; + +/** + * A convenient container for implementations of arithmetic operators. Classes that value brevity + * over disambiguation will import Arithmetic.* and construct queries with Add() and Sub(); classes + * that feel otherwise will use Arithmetic.Add() and Arithmetic.Sub(). + */ +public abstract class Arithmetic +{ + /** The SQL '+' operator. */ + public static class Add extends BinaryOperator + { + public Add (String pColumn, Comparable value) + { + super(new ColumnExp(pColumn), value); + } + + public Add (SQLExpression column, Comparable value) + { + super(column, value); + } + + public Add (SQLExpression column, SQLExpression value) + { + super(column, value); + } + + @Override + protected String operator() + { + return "+"; + } + } + + /** The SQL '-' operator. */ + public static class Sub extends BinaryOperator + { + public Sub (String pColumn, Comparable value) + { + super(new ColumnExp(pColumn), value); + } + + public Sub (SQLExpression column, Comparable value) + { + super(column, value); + } + + public Sub (SQLExpression column, SQLExpression value) + { + super(column, value); + } + + @Override + protected String operator() + { + return "-"; + } + } + + /** The SQL '*' operator. */ + public static class Mul extends BinaryOperator + { + public Mul (String pColumn, Comparable value) + { + super(new ColumnExp(pColumn), value); + } + + public Mul (SQLExpression column, Comparable value) + { + super(column, value); + } + + public Mul (SQLExpression column, SQLExpression value) + { + super(column, value); + } + + @Override + protected String operator() + { + return "*"; + } + } + + /** The SQL '/' operator. */ + public static class Div extends BinaryOperator + { + public Div (String pColumn, Comparable value) + { + super(new ColumnExp(pColumn), value); + } + + public Div (SQLExpression column, Comparable value) + { + super(column, value); + } + + public Div (SQLExpression column, SQLExpression value) + { + super(column, value); + } + + @Override + protected String operator() + { + return "/"; + } + } +} diff --git a/src/java/com/samskivert/jdbc/depot/operator/Conditionals.java b/src/java/com/samskivert/jdbc/depot/operator/Conditionals.java index c5867f54..577e129a 100644 --- a/src/java/com/samskivert/jdbc/depot/operator/Conditionals.java +++ b/src/java/com/samskivert/jdbc/depot/operator/Conditionals.java @@ -26,7 +26,7 @@ import java.sql.SQLException; import com.samskivert.jdbc.depot.Query; import com.samskivert.jdbc.depot.expression.ColumnExp; import com.samskivert.jdbc.depot.expression.SQLExpression; -import com.samskivert.jdbc.depot.expression.ValueExp; +import com.samskivert.jdbc.depot.operator.SQLOperator.BinaryOperator; /** * A convenient container for implementations of conditional operators. Classes that value brevity @@ -196,46 +196,4 @@ public abstract class Conditionals protected ColumnExp _column; protected Comparable[] _values; } - - /** - * Does the real work for simple binary operators such as Equals. - */ - protected abstract static class BinaryOperator implements SQLOperator - { - public BinaryOperator (SQLExpression lhs, SQLExpression rhs) - { - _lhs = lhs; - _rhs = rhs; - } - - public BinaryOperator (SQLExpression lhs, Comparable rhs) - { - this(lhs, new ValueExp(rhs)); - } - - // from SQLExpression - public void appendExpression (Query query, StringBuilder builder) - { - _lhs.appendExpression(query, builder); - builder.append(operator()); - _rhs.appendExpression(query, builder); - } - - // from SQLExpression - public int bindArguments (PreparedStatement pstmt, int argIdx) - throws SQLException - { - argIdx = _lhs.bindArguments(pstmt, argIdx); - argIdx = _rhs.bindArguments(pstmt, argIdx); - return argIdx; - } - - /** - * Returns the string representation of the operator. - */ - protected abstract String operator(); - - protected SQLExpression _lhs; - protected SQLExpression _rhs; - } } diff --git a/src/java/com/samskivert/jdbc/depot/operator/Logic.java b/src/java/com/samskivert/jdbc/depot/operator/Logic.java index 8c7b7956..f84c7c1c 100644 --- a/src/java/com/samskivert/jdbc/depot/operator/Logic.java +++ b/src/java/com/samskivert/jdbc/depot/operator/Logic.java @@ -24,6 +24,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import com.samskivert.jdbc.depot.Query; +import com.samskivert.jdbc.depot.operator.SQLOperator.MultiOperator; /** * A convenient container for implementations of logical operators. Classes that value brevity @@ -95,47 +96,4 @@ public abstract class Logic protected SQLOperator _condition; } - - /** - * Represents an operator with any number of operands. - */ - protected abstract static class MultiOperator - implements SQLOperator - { - public MultiOperator (SQLOperator... conditions) - { - super(); - _conditions = conditions; - } - - // from SQLExpression - public void appendExpression (Query query, StringBuilder builder) - { - for (int ii = 0; ii < _conditions.length; ii++) { - if (ii > 0) { - builder.append(" ").append(operator()).append(" "); - } - builder.append("("); - _conditions[ii].appendExpression(query, builder); - builder.append(")"); - } - } - - // from SQLExpression - public int bindArguments (PreparedStatement pstmt, int argIdx) - throws SQLException - { - for (int ii = 0; ii < _conditions.length; ii++) { - argIdx = _conditions[ii].bindArguments(pstmt, argIdx); - } - return argIdx; - } - - /** - * Returns the text infix to be used to join expressions together. - */ - protected abstract String operator (); - - protected SQLOperator[] _conditions; - } } diff --git a/src/java/com/samskivert/jdbc/depot/operator/SQLOperator.java b/src/java/com/samskivert/jdbc/depot/operator/SQLOperator.java index 7cbee573..49672dc9 100644 --- a/src/java/com/samskivert/jdbc/depot/operator/SQLOperator.java +++ b/src/java/com/samskivert/jdbc/depot/operator/SQLOperator.java @@ -20,7 +20,12 @@ package com.samskivert.jdbc.depot.operator; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import com.samskivert.jdbc.depot.Query; import com.samskivert.jdbc.depot.expression.SQLExpression; +import com.samskivert.jdbc.depot.expression.ValueExp; /** * A common interface for operator hierarchies in SQL. The main purpose of breaking this out from @@ -29,4 +34,88 @@ import com.samskivert.jdbc.depot.expression.SQLExpression; */ public interface SQLOperator extends SQLExpression { + /** + * Represents an operator with any number of operands. + */ + public abstract static class MultiOperator + implements SQLOperator + { + public MultiOperator (SQLOperator... conditions) + { + super(); + _conditions = conditions; + } + + // from SQLExpression + public void appendExpression (Query query, StringBuilder builder) + { + for (int ii = 0; ii < _conditions.length; ii++) { + if (ii > 0) { + builder.append(" ").append(operator()).append(" "); + } + builder.append("("); + _conditions[ii].appendExpression(query, builder); + builder.append(")"); + } + } + + // from SQLExpression + public int bindArguments (PreparedStatement pstmt, int argIdx) + throws SQLException + { + for (int ii = 0; ii < _conditions.length; ii++) { + argIdx = _conditions[ii].bindArguments(pstmt, argIdx); + } + return argIdx; + } + + /** + * Returns the text infix to be used to join expressions together. + */ + protected abstract String operator (); + + protected SQLOperator[] _conditions; + } + + /** + * Does the real work for simple binary operators such as Equals. + */ + public abstract static class BinaryOperator implements SQLOperator + { + public BinaryOperator (SQLExpression lhs, SQLExpression rhs) + { + _lhs = lhs; + _rhs = rhs; + } + + public BinaryOperator (SQLExpression lhs, Comparable rhs) + { + this(lhs, new ValueExp(rhs)); + } + + // from SQLExpression + public void appendExpression (Query query, StringBuilder builder) + { + _lhs.appendExpression(query, builder); + builder.append(operator()); + _rhs.appendExpression(query, builder); + } + + // from SQLExpression + public int bindArguments (PreparedStatement pstmt, int argIdx) + throws SQLException + { + argIdx = _lhs.bindArguments(pstmt, argIdx); + argIdx = _rhs.bindArguments(pstmt, argIdx); + return argIdx; + } + + /** + * Returns the string representation of the operator. + */ + protected abstract String operator(); + + protected SQLExpression _lhs; + protected SQLExpression _rhs; + } }