added QDateTime and QString expression libraries

updated tests for HSQLDB and MySQL
This commit is contained in:
Timo Westkämper 2009-01-20 12:27:59 +00:00
parent bfb0fd8e83
commit 6db5aeee99
7 changed files with 178 additions and 15 deletions

View File

@ -96,11 +96,11 @@ public class Grammar {
}
public static Expr<Character> charAt(Expr<String> left, Expr<Integer> right) {
return createComparable(Ops.CHAR_AT, left, right);
return createComparable(Character.class, Ops.CHAR_AT, left, right);
}
public static Expr<Character> charAt(Expr<String> left, int right) {
return createComparable(Ops.CHAR_AT, left, createConstant(right));
return createComparable(Character.class, Ops.CHAR_AT, left, createConstant(right));
}
public static EString concat(Expr<String> left, Expr<String> right) {

View File

@ -78,16 +78,29 @@ public interface Ops {
Op<Boolean> ENDSWITH = new Op<Boolean>();
Op<Boolean> CONTAINS = new Op<Boolean>();
/**
* The Interface OpNumberAgg.
*/
public interface OpNumberAgg{
Op<Number> AVG = new Op<Number>();
Op<Number> MAX = new Op<Number>();
Op<Number> MIN = new Op<Number>();
}
public interface OpDateTime{
Op<java.util.Date> CURRENT_DATE = new Op<java.util.Date>();
Op<java.util.Date> CURRENT_TIME = new Op<java.util.Date>();
Op<java.util.Date> CURRENT_TIMESTAMP = new Op<java.util.Date>();
Op<Integer> DAY = new Op<Integer>();
Op<Integer> HOUR = new Op<Integer>();
Op<Integer> MINUTE = new Op<Integer>();
Op<Integer> MONTH = new Op<Integer>();
Op<Integer> SECOND = new Op<Integer>();
Op<java.util.Date> SYSDATE = new Op<java.util.Date>();
Op<Integer> YEAR = new Op<Integer>();
Op<Integer> WEEK = new Op<Integer>();
Op<Integer> DAY_OF_WEEK = new Op<Integer>();
Op<Integer> DAY_OF_MONTH = new Op<Integer>();
Op<Integer> DAY_OF_YEAR =new Op<Integer>();
}
public interface OpMath{
Op<Number> ABS = new Op<Number>();
Op<Number> ACOS = new Op<Number>();
@ -107,7 +120,13 @@ public interface Ops {
Op<Number> LOG10 = new Op<Number>();
Op<Number> LOG = new Op<Number>();
Op<Number> FLOOR = new Op<Number>();
Op<Number> EXP = new Op<Number>();
Op<Number> EXP = new Op<Number>();
}
public interface OpString{
Op<Number> LENGTH = new Op<Number>();
Op<String> LTRIM = new Op<String>();
Op<String> RTRIM = new Op<String>();
Op<String> SPACE = new Op<String>();
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import java.sql.Time;
import java.util.Date;
import com.mysema.query.grammar.types.Expr;
import com.mysema.query.grammar.types.Factory;
import com.mysema.query.grammar.types.Expr.EComparable;
import com.mysema.query.grammar.types.Expr.ENumber;
/**
* QDateTime provides
*
* @author tiwe
* @version $Id$
*/
public class QDateTime extends Factory{
public static EComparable<Date> currentDate() {
return createComparable(Date.class, Ops.OpDateTime.CURRENT_DATE);
}
public static EComparable<Date> currentTime() {
return createComparable(Date.class, Ops.OpDateTime.CURRENT_DATE);
}
public static ENumber<Integer> dayOfMonth(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.DAY_OF_MONTH, d);
}
public static ENumber<Integer> dayOfWeek(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.DAY_OF_WEEK, d);
}
public static ENumber<Integer> dayOfYear(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.DAY_OF_YEAR, d);
}
public static ENumber<Integer> hour(Expr<Time> t) {
return createNumber(Integer.class, Ops.OpDateTime.HOUR, t);
}
public static ENumber<Integer> minute(Expr<Time> t) {
return createNumber(Integer.class, Ops.OpDateTime.MINUTE, t);
}
public static ENumber<Integer> year(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.YEAR, d);
}
public static ENumber<Integer> week(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.WEEK, d);
}
public static ENumber<Integer> second(Expr<Time> t) {
return createNumber(Integer.class, Ops.OpDateTime.SECOND, t);
}
public static EComparable<Date> now() {
return createComparable(Date.class, Ops.OpDateTime.CURRENT_TIME);
}
public static ENumber<Integer> month(Expr<Date> d) {
return createNumber(Integer.class, Ops.OpDateTime.MONTH, d);
}
}

View File

@ -1,11 +1,14 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import static com.mysema.query.grammar.types.Factory.createConstant;
import static com.mysema.query.grammar.types.Factory.createNumber;
import com.mysema.query.grammar.Ops.OpMath;
import com.mysema.query.grammar.Ops.OpNumberAgg;
import com.mysema.query.grammar.types.Expr;
import com.mysema.query.grammar.types.Factory;
import com.mysema.query.grammar.types.Expr.ENumber;
/**
@ -14,7 +17,7 @@ import com.mysema.query.grammar.types.Expr.ENumber;
* @author tiwe
* @version $Id$
*/
public class QMath {
public class QMath extends Factory{
public static <A extends Number & Comparable<A>> ENumber<A> mult(Expr<A> left, A right) {
return createNumber(left.getType(), Ops.MULT, left, createConstant(right));

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2008 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.grammar;
import com.mysema.query.grammar.types.Expr;
import com.mysema.query.grammar.types.Factory;
import com.mysema.query.grammar.types.Expr.ENumber;
import com.mysema.query.grammar.types.Expr.EString;
/**
* QString provides
*
* @author tiwe
* @version $Id$
*/
public class QString extends Factory{
public static ENumber<Integer> length(Expr<String> s) {
return createNumber(Integer.class, Ops.OpString.LENGTH, s);
}
public static EString ltrim(Expr<String> s) {
return createString(Ops.OpString.LTRIM, s);
}
public static EString rtrim(Expr<String> s) {
return createString(Ops.OpString.RTRIM, s);
}
public static EString space(int i) {
return createString(Ops.OpString.SPACE, createConstant(i));
}
}

View File

@ -25,10 +25,10 @@ public class Factory {
return new Operation.OBoolean(operator, args);
}
public static final <OpType, RT extends Comparable<RT>> Expr.EComparable<RT> createComparable(Op<OpType> operator, Expr<?>... args) {
public static final <OpType, RT extends Comparable<RT>> Expr.EComparable<RT> createComparable(Class<RT> type, Op<OpType> operator, Expr<?>... args) {
checkArg("operator",operator);
checkArg("args",args);
return new Operation.OComparable<OpType,RT>(operator, args);
return new Operation.OComparable<OpType,RT>(type, operator, args);
}
@SuppressWarnings("unchecked")

View File

@ -73,6 +73,22 @@ public abstract class OperationPatterns{
add(Ops.TRIM, "trim(%s)");
add(Ops.UPPER, "upper(%s)");
// date time
add(Ops.OpDateTime.SYSDATE, "sysdate");
add(Ops.OpDateTime.CURRENT_DATE, "current_date()");
add(Ops.OpDateTime.CURRENT_TIME, "current_time()");
add(Ops.OpDateTime.CURRENT_TIMESTAMP, "current_timestamp()");
add(Ops.OpDateTime.SECOND, "second(%s)");
add(Ops.OpDateTime.MINUTE, "minute(%s)");
add(Ops.OpDateTime.HOUR, "hour(%s)");
add(Ops.OpDateTime.DAY, "day(%s)");
add(Ops.OpDateTime.WEEK, "week(%s)");
add(Ops.OpDateTime.MONTH, "month(%s)");
add(Ops.OpDateTime.YEAR, "year(%s)");
add(Ops.OpDateTime.DAY_OF_WEEK, "dayofweek(%s)");
add(Ops.OpDateTime.DAY_OF_MONTH, "dayofmonth(%s)");
add(Ops.OpDateTime.DAY_OF_YEAR, "dayofyear(%s)");
// math
add(Ops.OpMath.ABS,"abs(%s)");
add(Ops.OpMath.ACOS,"acos(%s)");
@ -91,8 +107,14 @@ public abstract class OperationPatterns{
add(Ops.OpMath.MOD,"mod(%s,%s)");
add(Ops.OpMath.LOG10,"log10(%s)");
add(Ops.OpMath.LOG,"log(%s)");
add(Ops.OpMath.FLOOR,"floor(%s)");
add(Ops.OpMath.FLOOR,"floor(%s)");
add(Ops.OpMath.EXP,"exp(%s)");
// string
add(Ops.OpString.LENGTH, "length(%s)");
add(Ops.OpString.LTRIM, "ltrim(%s)");
add(Ops.OpString.RTRIM, "rtrim(%s)");
add(Ops.OpString.SPACE, "space(%s)");
// path types
for (PathType type : new PathType[]{PathMetadata.LISTVALUE, PathMetadata.LISTVALUE_CONSTANT, PathMetadata.MAPVALUE, PathMetadata.MAPVALUE_CONSTANT}){