Support for more operators and functions from Zell.

This commit is contained in:
Michael Bayne
2006-12-15 23:08:26 +00:00
parent d06b9685dc
commit cf15c7c998
5 changed files with 296 additions and 86 deletions
@@ -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;
}
@@ -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 "/";
}
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}