Move in() and like() into FluentExp (they can certainly operate on an expression), and add notLike() variants.

This commit is contained in:
Par Winzell
2010-04-07 16:24:18 +00:00
parent 40d596509d
commit 31f5d593e5
5 changed files with 61 additions and 27 deletions
+19 -2
View File
@@ -123,7 +123,7 @@ public class Ops
*/ */
public static FluentExp like (SQLExpression source, Comparable<?> value) public static FluentExp like (SQLExpression source, Comparable<?> value)
{ {
return new Like(source, value); return new Like(source, value, true);
} }
/** /**
@@ -131,9 +131,26 @@ public class Ops
*/ */
public static FluentExp like (SQLExpression source, SQLExpression expr) public static FluentExp like (SQLExpression source, SQLExpression expr)
{ {
return new Like(source, expr); return new Like(source, expr, true);
} }
/**
* Returns an expression that matches when the source is NOT like the supplied value.
*/
public static FluentExp notLike (SQLExpression source, Comparable<?> value)
{
return new Like(source, value, false);
}
/**
* Returns an expression that matches when the source is NOT like the supplied expression.
*/
public static FluentExp notLike (SQLExpression source, SQLExpression expr)
{
return new Like(source, expr, false);
}
/** /**
* Creates an EXISTS expression with the supplied select clause. * Creates an EXISTS expression with the supplied select clause.
*/ */
@@ -25,8 +25,6 @@ import java.util.Collection;
import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.Join; import com.samskivert.depot.clause.Join;
import com.samskivert.depot.impl.FragmentVisitor; import com.samskivert.depot.impl.FragmentVisitor;
import com.samskivert.depot.impl.operator.In;
import com.samskivert.depot.impl.operator.Like;
/** /**
* An expression that unambiguously identifies a field of a class, for example * An expression that unambiguously identifies a field of a class, for example
@@ -54,30 +52,12 @@ public class ColumnExp extends FluentExp
return new ColumnExp(oClass, name); return new ColumnExp(oClass, name);
} }
/** Returns an {@link In} with this column and the supplied values. */
public In in (Iterable<? extends Comparable<?>> values)
{
return new In(this, values);
}
/** Returns a {@link Join} on this column and the supplied target. */ /** Returns a {@link Join} on this column and the supplied target. */
public Join join (ColumnExp join) public Join join (ColumnExp join)
{ {
return new Join(this, join); return new Join(this, join);
} }
/** Returns a {@link Like} on this column and the supplied target. */
public Like like (Comparable<?> value)
{
return new Like(this, value);
}
/** Returns a {@link Like} on this column and the supplied target. */
public Like like (SQLExpression expr)
{
return new Like(this, expr);
}
// from SQLExpression // from SQLExpression
public Object accept (FragmentVisitor<?> builder) public Object accept (FragmentVisitor<?> builder)
{ {
@@ -32,6 +32,7 @@ import com.samskivert.depot.impl.operator.In;
import com.samskivert.depot.impl.operator.IsNull; import com.samskivert.depot.impl.operator.IsNull;
import com.samskivert.depot.impl.operator.LessThan; import com.samskivert.depot.impl.operator.LessThan;
import com.samskivert.depot.impl.operator.LessThanEquals; import com.samskivert.depot.impl.operator.LessThanEquals;
import com.samskivert.depot.impl.operator.Like;
import com.samskivert.depot.impl.operator.Mul; import com.samskivert.depot.impl.operator.Mul;
import com.samskivert.depot.impl.operator.NotEquals; import com.samskivert.depot.impl.operator.NotEquals;
import com.samskivert.depot.impl.operator.Sub; import com.samskivert.depot.impl.operator.Sub;
@@ -78,6 +79,12 @@ public abstract class FluentExp
return new In(this, values); return new In(this, values);
} }
/** Returns an {@link In} with this column and the supplied values. */
public In in (Iterable<? extends Comparable<?>> values)
{
return new In(this, values);
}
/** Returns a {@link GreaterThan} with this expression and the supplied target. */ /** Returns a {@link GreaterThan} with this expression and the supplied target. */
public FluentExp greaterThan (Comparable<?> value) public FluentExp greaterThan (Comparable<?> value)
{ {
@@ -209,4 +216,29 @@ public abstract class FluentExp
{ {
return new Div(this, expr); return new Div(this, expr);
} }
/** Returns a {@link Like} on this column and the supplied target. */
public Like like (Comparable<?> value)
{
return new Like(this, value, true);
}
/** Returns a {@link Like} on this column and the supplied target. */
public Like like (SQLExpression expr)
{
return new Like(this, expr, true);
}
/** Returns a negated {@link Like} on this column and the supplied target. */
public Like notLike (Comparable<?> value)
{
return new Like(this, value, false);
}
/** Returns a negated {@link Like} on this column and the supplied target. */
public Like notLike (SQLExpression expr)
{
return new Like(this, expr, false);
}
} }
@@ -86,7 +86,8 @@ public class HSQLBuilder
for (String field : fields) { for (String field : fields) {
for (String ftsWord : ftsWords) { for (String ftsWord : ftsWords) {
// build comparisons between each word and column // build comparisons between each word and column
bits.add(new Like(new Lower(new ColumnExp(pClass, field)), "%"+ftsWord+"%")); bits.add(new Like(new Lower(
new ColumnExp(pClass, field)), "%"+ftsWord+"%", true));
} }
} }
// then just OR them all together and we have our query // then just OR them all together and we have our query
@@ -23,24 +23,26 @@ package com.samskivert.depot.impl.operator;
import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.expression.SQLExpression;
/** /**
* The SQL 'like' operator. * The SQL 'like' (and 'not like') operator.
*/ */
public class Like extends BinaryOperator public class Like extends BinaryOperator
{ {
public Like (SQLExpression column, Comparable<?> value) public Like (SQLExpression column, Comparable<?> value, boolean like)
{ {
super(column, value); super(column, value);
_like = like;
} }
public Like (SQLExpression column, SQLExpression value) public Like (SQLExpression column, SQLExpression value, boolean like)
{ {
super(column, value); super(column, value);
_like = like;
} }
@Override // from BinaryOperator @Override // from BinaryOperator
public String operator() public String operator()
{ {
return " like "; return _like ? " like " : " not like ";
} }
@Override // from BinaryOperator @Override // from BinaryOperator
@@ -48,4 +50,6 @@ public class Like extends BinaryOperator
{ {
return new NoValue("Like operator not implemented"); return new NoValue("Like operator not implemented");
} }
protected boolean _like;
} }