This commit is contained in:
Timo Westkämper 2009-05-25 18:03:08 +00:00
parent 32e0346e26
commit e1a25373ec
6 changed files with 85 additions and 87 deletions

View File

@ -6,7 +6,7 @@
<parent>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-root</artifactId>
<version>0.3.6-SNAPSHOT</version>
<version>0.3.7-SNAPSHOT</version>
</parent>
<groupId>com.mysema.querydsl</groupId>

View File

@ -5,10 +5,7 @@
*/
package com.mysema.query.jdoql;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.mysema.query.serialization.OperationPatterns;
import com.mysema.query.types.operation.Ops;
@ -17,6 +14,7 @@ import com.mysema.query.types.path.PathMetadata;
import com.mysema.query.types.path.PathMetadata.PathType;
/**
* Operation patterns for JDOQL serialization
*
* @author tiwe
*
@ -25,35 +23,25 @@ public class JDOQLOps extends OperationPatterns {
public static final JDOQLOps DEFAULT = new JDOQLOps();
public static final List<Op<?>> wrapCollectionsForOp;
static{
wrapCollectionsForOp = Collections.<Op<?>>unmodifiableList(Arrays.<Op<?>>asList(
Ops.IN,
Ops.NOTIN,
OpQuant.ALL,
OpQuant.ANY,
OpQuant.AVG_IN_COL,
OpQuant.EXISTS,
OpQuant.NOTEXISTS));
}
public JDOQLOps(){
add(Ops.AFTER, "%s.compareTo(%s) > 0");
add(Ops.BEFORE, "%s.compareTo(%s) < 0");
add(Ops.AOE, "%s.compareTo(%s) >= 0");
add(Ops.BOE, "%s.compareTo(%s) <= 0");
add(Ops.EQ_PRIMITIVE, "%s == %s");
add(Ops.EQ_PRIMITIVE, "%s == %s");
add(Ops.EQ_OBJECT, "%s == %s");
add(Ops.NE_OBJECT, "%s != %s");
add(Ops.ISNULL, "%s == null");
add(Ops.ISNOTNULL, "%s != null");
add(Ops.ISTYPEOF, "%s instanceof %s");
// collection
add(Ops.IN, "%2$s.contains(%1$s)");
add(Ops.NOTIN, "!%2$s.contains(%1$s)");
add(Ops.NOTIN, "!%2$s.contains(%1$s)");
add(Ops.COL_ISEMPTY, "%s.isEmpty()");
add(Ops.COL_ISNOTEMPTY, "!%s.isEmpty()");
// comparable
add(Ops.AFTER, "%s > %s");
add(Ops.BEFORE, "%s < %s");
add(Ops.AOE, "%s >= %s");
add(Ops.BOE, "%s <= %s");
// java.lang.String
add(Ops.CHAR_AT, "%s.charAt(%s)");
@ -67,7 +55,7 @@ public class JDOQLOps extends OperationPatterns {
add(Ops.STRING_LENGTH, "%s.length(%s)");
add(Ops.LAST_INDEX_2ARGS, "%s.lastIndex(%s)");
add(Ops.LAST_INDEX, "%s.lastIndex(%s,%s)");
add(Ops.ISEMPTY, "%s.isEmpty()");
add(Ops.STRING_ISEMPTY, "%s.isEmpty()");
add(Ops.STARTSWITH, "%s.startsWith(%s)");
add(Ops.STARTSWITH_IC, "%s.toLowerCase().startsWith(%s.toLowerCase())");
add(Ops.INDEXOF_2ARGS, "%s.indexOf(%s,%s)");
@ -77,13 +65,16 @@ public class JDOQLOps extends OperationPatterns {
add(Ops.ENDSWITH_IC, "%s.toLowerCase().endsWith(%s.toLowerCase())");
add(Ops.CONTAINS, "%s.contains(%s)");
// path types
for (PathType type : new PathType[]{PathMetadata.LISTVALUE, PathMetadata.LISTVALUE_CONSTANT, PathMetadata.MAPVALUE, PathMetadata.MAPVALUE_CONSTANT}){
add(type,"%s[%s]");
}
add(PathMetadata.PROPERTY,"%s.%s");
add(PathMetadata.SIZE,"%s.size");
add(PathMetadata.VARIABLE,"%s");
// path types
add(PathMetadata.VARIABLE,"%s");
for (PathType type : new PathType[]{PathMetadata.LISTVALUE, PathMetadata.LISTVALUE_CONSTANT, PathMetadata.MAPVALUE, PathMetadata.MAPVALUE_CONSTANT}){
add(type,"%s.get(%s)");
}
add(PathMetadata.ARRAYVALUE, "%s[%s]");
add(PathMetadata.ARRAYVALUE_CONSTANT, "%s[%s]");
add(PathMetadata.ARRAY_SIZE,"%s.length");
add(PathMetadata.SIZE,"%s.size()");
}

View File

@ -12,22 +12,56 @@ import com.mysema.query.types.expr.EBoolean;
import com.mysema.query.types.path.PEntity;
/**
* Query interface for JDOQLQueries
*
* @author tiwe
*
*/
public interface JDOQLQuery extends Projectable { // --> projections go into result
public interface JDOQLQuery extends Projectable {
JDOQLQuery from(PEntity<?>... o); // first is candidate, rest are variables
/**
* Define the sources of the query, the first becomes the candidate, the rest variables
*/
JDOQLQuery from(PEntity<?>... o);
JDOQLQuery orderBy(OrderSpecifier<?>... o); // -> ordering
/**
* Define the order the projection
*
* @param o
* @return
*/
JDOQLQuery orderBy(OrderSpecifier<?>... o);
JDOQLQuery where(EBoolean... o); // limit
/**
* Define the filter of the query
*
* @param o
* @return
*/
JDOQLQuery where(EBoolean... o);
JDOQLQuery limit(long limit); // --> range
/**
* Define the limit of the results
*
* @param limit
* @return
*/
JDOQLQuery limit(long limit);
JDOQLQuery offset(long offset); // --> range
/**
* Define the offset of the results
*
* @param offset
* @return
*/
JDOQLQuery offset(long offset);
JDOQLQuery restrict(QueryModifiers mod); // --> range
/**
* Define the limit and offset of the results
*
* @param mod
* @return
*/
JDOQLQuery restrict(QueryModifiers mod);
}

View File

@ -24,6 +24,7 @@ import com.mysema.query.types.expr.Expr;
import com.mysema.query.types.path.PEntity;
/**
* Default implementation of the JDOQLQuery interface
*
* @author tiwe
*
@ -94,13 +95,12 @@ class JDOQLQueryImpl extends QueryBaseWithProjection<Object, JDOQLQueryImpl> imp
}
public long count(){
// return uniqueResult(HQLGrammar.count());
throw new UnsupportedOperationException();
}
public long count(Expr<?> expr){
// return uniqueResult(HQLGrammar.count(expr));
throw new UnsupportedOperationException();
String filterString = getFilterString();
logger.debug("query : {}", filterString);
Query query = createQuery(filterString, QueryModifiers.limit(1));
query.setUnique(true);
query.setResult("count(this)");
return (Long) execute(query);
}
private Query createQuery(String filterString, QueryModifiers modifiers) {
@ -148,7 +148,14 @@ class JDOQLQueryImpl extends QueryBaseWithProjection<Object, JDOQLQueryImpl> imp
}
// projection
// TODO
if (!getMetadata().getProjection().isEmpty()){
List<? extends Expr<?>> projection = getMetadata().getProjection();
if (!projection.get(0).equals(sources.get(0))){
JDOQLSerializer serializer = new JDOQLSerializer(ops, sources.get(0));
serializer.handle(", ", projection);
query.setResult(serializer.toString());
}
}
// order
// TODO

View File

@ -24,8 +24,6 @@ import com.mysema.query.types.path.Path;
*/
public class JDOQLSerializer extends BaseSerializer<JDOQLSerializer> {
private boolean wrapElements = false;
private PEntity<?> candidatePath;
public JDOQLSerializer(JDOQLOps ops, PEntity<?> candidate) {
@ -71,18 +69,16 @@ public class JDOQLSerializer extends BaseSerializer<JDOQLSerializer> {
@Override
protected void visitOperation(Class<?> type, Op<?> operator, List<Expr<?>> args) {
boolean old = wrapElements;
wrapElements = JDOQLOps.wrapCollectionsForOp.contains(operator);
//
if (operator.equals(Ops.STRING_CAST)) {
if (operator.equals(Ops.ISTYPEOF)){
handle(args.get(0)).append(" instanceof ");
append(((EConstant<Class<?>>)args.get(1)).getConstant().getName());
}else if (operator.equals(Ops.STRING_CAST)) {
visitCast(operator, args.get(0), String.class);
} else if (operator.equals(Ops.NUMCAST)) {
visitCast(operator, args.get(0), (Class<?>) ((EConstant<?>) args.get(1)).getConstant());
} else {
super.visitOperation(type, operator, args);
}
//
wrapElements = old;
}
@Override

View File

@ -1,30 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE jdo PUBLIC
"-//Sun Microsystems, Inc.//DTD Java Data Objects ORM Metadata 2.0//EN"
"http://java.sun.com/dtd/orm_2_0.dtd">
<orm>
<package name="com.mysema.query.jdoql.testdomain">
<class name="Product" identity-type="datastore" table="JDO_PRODUCTS">
<inheritance strategy="new-table"/>
<field name="name">
<column name="PRODUCT_NAME" length="100" jdbc-type="VARCHAR"/>
</field>
<field name="description">
<column length="255" jdbc-type="VARCHAR"/>
</field>
</class>
<class name="Book" identity-type="datastore" table="JDO_BOOKS">
<inheritance strategy="new-table"/>
<field name="author">
<column length="40" jdbc-type="VARCHAR"/>
</field>
<field name="isbn">
<column length="20" jdbc-type="CHAR"/>
</field>
<field name="publisher">
<column length="40" jdbc-type="VARCHAR"/>
</field>
</class>
</package>
</orm>