mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-21 21:14:12 +08:00
This commit is contained in:
parent
b603a617e2
commit
3ef9fa9974
@ -38,6 +38,7 @@ public class ColQueryTemplates extends JavaTemplates {
|
||||
// Date and Time
|
||||
add(Ops.DateTimeOps.YEAR, functions + ".getYear({0})");
|
||||
add(Ops.DateTimeOps.MONTH, functions + ".getMonth({0})");
|
||||
add(Ops.DateTimeOps.WEEK, functions + ".getWeek({0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_WEEK, functions + ".getDayOfWeek({0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_MONTH, functions + ".getDayOfMonth({0})");
|
||||
add(Ops.DateTimeOps.DAY_OF_YEAR, functions + ".getDayOfYear({0})");
|
||||
@ -82,6 +83,10 @@ public class ColQueryTemplates extends JavaTemplates {
|
||||
return getField(date, Calendar.MONTH) + 1;
|
||||
}
|
||||
|
||||
public static int getWeek(Date date){
|
||||
return getField(date, Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public static int getDayOfMonth(Date date){
|
||||
return getField(date, Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
@ -139,6 +139,7 @@ public abstract class EDateTime<D extends Comparable> extends EDateOrTime<D> {
|
||||
|
||||
/**
|
||||
* Create a week expression
|
||||
* <p>NOT supported in JDOQL and not in Derby</p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
|
||||
@ -55,10 +55,13 @@ public class MatchingFilters {
|
||||
HashSet<EBoolean> rv = new HashSet<EBoolean>();
|
||||
rv.addAll(comparable(expr, other));
|
||||
rv.add(expr.getDayOfMonth().eq(other.getDayOfMonth()));
|
||||
|
||||
if (!target.equals(Target.DERBY) && !module.equals(Module.JDOQL)){
|
||||
rv.add(expr.getDayOfWeek().eq(other.getDayOfWeek ()));
|
||||
rv.add(expr.getDayOfYear().eq(other.getDayOfYear()));
|
||||
rv.add(expr.getDayOfYear().eq(other.getDayOfYear()));
|
||||
rv.add(expr.getWeek().eq(other.getWeek()));
|
||||
}
|
||||
|
||||
rv.add(expr.getMonth().eq(other.getMonth()));
|
||||
rv.add(expr.getYear().eq(other.getYear()));
|
||||
return rv;
|
||||
@ -82,8 +85,9 @@ public class MatchingFilters {
|
||||
|
||||
if (!target.equals(Target.DERBY) && !module.equals(Module.JDOQL)){
|
||||
rv.add(expr.getDayOfWeek().eq(other.getDayOfWeek ()));
|
||||
rv.add(expr.getDayOfYear().eq(other.getDayOfYear()));
|
||||
}
|
||||
rv.add(expr.getDayOfYear().eq(other.getDayOfYear()));
|
||||
rv.add(expr.getWeek().eq(other.getWeek()));
|
||||
}
|
||||
|
||||
rv.add(expr.getMonth().eq(other.getMonth()));
|
||||
rv.add(expr.getYear().eq(other.getYear()));
|
||||
@ -159,10 +163,6 @@ public class MatchingFilters {
|
||||
|
||||
rv.add(expr.indexOf(other).eq(0));
|
||||
|
||||
if (!module.equals(Module.HQL) && !module.equals(Module.JDOQL)){
|
||||
rv.add(expr.lastIndexOf(other).eq(0));
|
||||
}
|
||||
|
||||
if (!target.equals(Target.DERBY)){
|
||||
rv.add(expr.indexOf(other.substring(1)).eq(1));
|
||||
rv.add(expr.indexOf(other.substring(2)).eq(2));
|
||||
@ -170,7 +170,11 @@ public class MatchingFilters {
|
||||
|
||||
rv.add(expr.isEmpty().not());
|
||||
rv.add(expr.isNotEmpty());
|
||||
|
||||
|
||||
if (!module.equals(Module.HQL) && !module.equals(Module.JDOQL)){
|
||||
rv.add(expr.lastIndexOf(other).eq(0));
|
||||
}
|
||||
|
||||
rv.add(expr.length().eq(other.length()));
|
||||
|
||||
rv.add(expr.like(other));
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
package com.mysema.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.mysema.query.StandardTest.Module;
|
||||
@ -91,14 +92,18 @@ public class Projections {
|
||||
}
|
||||
|
||||
<A extends Number & Comparable<A>> Collection<ENumber<?>> numericCasts(ENumber<A> expr, ENumber<A> other, A knownValue){
|
||||
HashSet<ENumber<?>> rv = new HashSet<ENumber<?>>();
|
||||
rv.add(expr.byteValue());
|
||||
rv.add(expr.doubleValue());
|
||||
rv.add(expr.floatValue());
|
||||
rv.add(expr.intValue());
|
||||
rv.add(expr.longValue());
|
||||
rv.add(expr.shortValue());
|
||||
return rv;
|
||||
if (!target.equals(Target.MYSQL)){
|
||||
HashSet<ENumber<?>> rv = new HashSet<ENumber<?>>();
|
||||
rv.add(expr.byteValue());
|
||||
rv.add(expr.doubleValue());
|
||||
rv.add(expr.floatValue());
|
||||
rv.add(expr.intValue());
|
||||
rv.add(expr.longValue());
|
||||
rv.add(expr.shortValue());
|
||||
return rv;
|
||||
}else{
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
Collection<EString> string(EString expr, EString other, String knownValue){
|
||||
|
||||
@ -88,11 +88,11 @@ public abstract class AbstractStandardTest {
|
||||
public void setUp(){
|
||||
Cat prev = null;
|
||||
for (Cat cat : Arrays.asList(
|
||||
new Cat("Bob", 1),
|
||||
new Cat("Ruth", 2),
|
||||
new Cat("Felix", 3),
|
||||
new Cat("Allen", 4),
|
||||
new Cat("Mary", 5))){
|
||||
new Cat("Bob123", 1),
|
||||
new Cat("Ruth123", 2),
|
||||
new Cat("Felix123", 3),
|
||||
new Cat("Allen123", 4),
|
||||
new Cat("Mary123", 5))){
|
||||
if (prev != null){
|
||||
cat.getKittens().add(prev);
|
||||
}
|
||||
@ -122,7 +122,7 @@ public abstract class AbstractStandardTest {
|
||||
// standardTest.mapTests(cat.kittensByName, otherCat.kittensByName, "Kitty", kitten);
|
||||
standardTest.numericCasts(cat.id, otherCat.id, 1);
|
||||
standardTest.numericTests(cat.id, otherCat.id, 1);
|
||||
standardTest.stringTests(cat.name, otherCat.name, "Bob");
|
||||
standardTest.stringTests(cat.name, otherCat.name, kitten.getName());
|
||||
standardTest.timeTests(cat.timeField, otherCat.timeField, time);
|
||||
|
||||
standardTest.report();
|
||||
@ -132,21 +132,29 @@ public abstract class AbstractStandardTest {
|
||||
public void testStringOperations(){
|
||||
// startsWith
|
||||
assertEquals(1, catQuery().where(cat.name.startsWith("R")).count());
|
||||
assertEquals(0, catQuery().where(cat.name.startsWith("r")).count());
|
||||
assertEquals(0, catQuery().where(cat.name.startsWith("X")).count());
|
||||
assertEquals(1, catQuery().where(cat.name.startsWith("r",false)).count());
|
||||
|
||||
// endsWith
|
||||
assertEquals(1, catQuery().where(cat.name.endsWith("h")).count());
|
||||
assertEquals(0, catQuery().where(cat.name.endsWith("H")).count());
|
||||
assertEquals(1, catQuery().where(cat.name.endsWith("H",false)).count());
|
||||
assertEquals(1, catQuery().where(cat.name.endsWith("h123")).count());
|
||||
assertEquals(0, catQuery().where(cat.name.endsWith("X")).count());
|
||||
assertEquals(1, catQuery().where(cat.name.endsWith("H123",false)).count());
|
||||
|
||||
// contains
|
||||
assertEquals(1, catQuery().where(cat.name.contains("eli")).count());
|
||||
|
||||
// indexOf
|
||||
assertEquals(Integer.valueOf(0), catQuery().where(cat.name.eq("Bob")).uniqueResult(cat.name.indexOf("B")));
|
||||
assertEquals(Integer.valueOf(1), catQuery().where(cat.name.eq("Bob")).uniqueResult(cat.name.indexOf("o")));
|
||||
assertEquals(Integer.valueOf(2), catQuery().where(cat.name.eq("Bob")).uniqueResult(cat.name.indexOf("b")));
|
||||
assertEquals(Integer.valueOf(0), catQuery().where(cat.name.eq("Bob123")).uniqueResult(cat.name.indexOf("B")));
|
||||
assertEquals(Integer.valueOf(1), catQuery().where(cat.name.eq("Bob123")).uniqueResult(cat.name.indexOf("o")));
|
||||
|
||||
// case-sensitivity
|
||||
|
||||
if (!getTarget().equals(Target.MYSQL)){ // NOTE : locate in MYSQL in case-insensitive
|
||||
assertEquals(0, catQuery().where(cat.name.startsWith("r")).count());
|
||||
assertEquals(0, catQuery().where(cat.name.endsWith("H123")).count());
|
||||
assertEquals(Integer.valueOf(2), catQuery().where(cat.name.eq("Bob123")).uniqueResult(cat.name.indexOf("b")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.StandardTest.Target;
|
||||
|
||||
@RunWith(HibernateTestRunner.class)
|
||||
@HibernateConfig("mysql.properties")
|
||||
public class MySQLJPAStandardTest extends AbstractHibernateTest{
|
||||
|
||||
@Override
|
||||
protected Target getTarget() {
|
||||
return Target.MYSQL;
|
||||
}
|
||||
|
||||
}
|
||||
@ -7,15 +7,15 @@ package com.mysema.query;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.StandardTest.Target;
|
||||
|
||||
@RunWith(HibernateTestRunner.class)
|
||||
@HibernateConfig("mysql.properties")
|
||||
public abstract class MySQLStandardTest extends AbstractStandardTest{
|
||||
// 0 failures
|
||||
|
||||
// 5 errors
|
||||
// cast(cat.id,class java.lang.Byte) failed : could not execute query
|
||||
// cast(cat.id,class java.lang.Double) failed : could not execute query
|
||||
// cast(cat.id,class java.lang.Float) failed : could not execute query
|
||||
// cast(cat.id,class java.lang.Long) failed : could not execute query
|
||||
// cast(cat.id,class java.lang.Short) failed : could not execute query
|
||||
public class MySQLStandardTest extends AbstractHibernateTest{
|
||||
|
||||
@Override
|
||||
protected Target getTarget() {
|
||||
return Target.MYSQL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user