#304 add single arg alternatives to varargs methods

This commit is contained in:
Timo Westkämper 2012-12-08 00:09:26 +02:00
parent 7c1a60191c
commit 37f50c0cd3
10 changed files with 113 additions and 25 deletions

View File

@ -23,7 +23,7 @@ import com.mysema.query.types.Predicate;
* @param <C> concrete subtype
*/
public interface FilteredClause<C extends FilteredClause<C>> {
/**
* Adds the given filter conditions
*

View File

@ -36,17 +36,33 @@ public abstract class QueryBase<Q extends QueryBase<Q>> {
return queryMixin.distinct();
}
public Q groupBy(Expression<?> e) {
return queryMixin.groupBy(e);
}
public Q groupBy(Expression<?>... o) {
return queryMixin.groupBy(o);
}
public Q having(Predicate e) {
return queryMixin.having(e);
}
public Q having(Predicate... o) {
return queryMixin.having(o);
}
public Q orderBy(OrderSpecifier<?> o) {
return queryMixin.orderBy(o);
}
public Q orderBy(OrderSpecifier<?>... o) {
return queryMixin.orderBy(o);
}
public Q where(Predicate o) {
return queryMixin.where(o);
}
public Q where(Predicate... o) {
return queryMixin.where(o);

View File

@ -156,6 +156,11 @@ public class QueryMixin<T> {
return self;
}
public final T from(Expression<?> arg) {
metadata.addJoin(JoinType.DEFAULT, arg);
return self;
}
public final T from(Expression<?>... args) {
for (Expression<?> arg : args) {
metadata.addJoin(JoinType.DEFAULT, arg);
@ -207,12 +212,22 @@ public class QueryMixin<T> {
return self;
}
public final T groupBy(Expression<?> e) {
metadata.addGroupBy(e);
return self;
}
public final T groupBy(Expression<?>... o) {
for (Expression<?> e : o) {
metadata.addGroupBy(e);
}
return self;
}
public final T having(Predicate e) {
metadata.addHaving(normalize(e, false));
return self;
}
public final T having(Predicate... o) {
for (Predicate e : o) {
@ -346,6 +361,11 @@ public class QueryMixin<T> {
metadata.setOffset(offset);
return self;
}
public final T on(Predicate condition) {
metadata.addJoinCondition(normalize(condition, false));
return self;
}
public final T on(Predicate... conditions){
for (Predicate condition : conditions) {
@ -354,14 +374,19 @@ public class QueryMixin<T> {
return self;
}
public final T orderBy(OrderSpecifier<?> spec) {
Expression<?> e = convert(spec.getTarget());
if (!spec.getTarget().equals(e)) {
metadata.addOrderBy(new OrderSpecifier(spec.getOrder(), e));
} else {
metadata.addOrderBy(spec);
}
return self;
}
public final T orderBy(OrderSpecifier<?>... o) {
for (OrderSpecifier<?> spec : o) {
Expression<?> e = convert(spec.getTarget());
if (!spec.getTarget().equals(e)) {
metadata.addOrderBy(new OrderSpecifier(spec.getOrder(), e));
} else {
metadata.addOrderBy(spec);
}
orderBy(spec);
}
return self;
}
@ -424,6 +449,11 @@ public class QueryMixin<T> {
metadata.setUnique(unique);
}
public final T where(Predicate e) {
metadata.addWhere(normalize(e, true));
return self;
}
public final T where(Predicate... o) {
for (Predicate e : o) {
metadata.addWhere(normalize(e, true));

View File

@ -88,6 +88,10 @@ public abstract class JPQLQueryBase<Q extends JPQLQueryBase<Q>> extends Projecta
return queryMixin.fetchAll();
}
public Q from(EntityPath<?> arg) {
return queryMixin.from(arg);
}
public Q from(EntityPath<?>... args) {
return queryMixin.from(args);
}
@ -216,6 +220,10 @@ public abstract class JPQLQueryBase<Q extends JPQLQueryBase<Q>> extends Projecta
return queryMixin.rightJoin(target, alias);
}
public Q on(Predicate condition) {
return queryMixin.on(condition);
}
public Q on(Predicate... conditions) {
return queryMixin.on(conditions);
}

View File

@ -271,6 +271,10 @@ SimpleProjectable<T> {
return queryMixin.offset(offset);
}
public Q orderBy(OrderSpecifier<?> o) {
return queryMixin.orderBy(o);
}
@Override
public Q orderBy(OrderSpecifier<?>... o) {
return queryMixin.orderBy(o);
@ -335,6 +339,10 @@ SimpleProjectable<T> {
public T uniqueResult() {
return oneResult(true);
}
public Q where(Predicate e) {
return queryMixin.where(e);
}
@Override
public Q where(Predicate... e) {

View File

@ -189,6 +189,10 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
return queryMixin.distinct();
}
public MongodbQuery<K> where(Predicate e) {
return queryMixin.where(e);
}
@Override
public MongodbQuery<K> where(Predicate... e) {
return queryMixin.where(e);
@ -209,6 +213,10 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
return queryMixin.restrict(modifiers);
}
public MongodbQuery<K> orderBy(OrderSpecifier<?> o) {
return queryMixin.orderBy(o);
}
@Override
public MongodbQuery<K> orderBy(OrderSpecifier<?>... o) {
return queryMixin.orderBy(o);

View File

@ -192,6 +192,10 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query> ex
return new SQLSerializer(configuration.getTemplates());
}
public Q from(Expression<?> arg) {
return queryMixin.from(arg);
}
public Q from(Expression<?>... args) {
return queryMixin.from(args);
}
@ -482,6 +486,10 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query> ex
}
return c.newInstance(args);
}
public Q on(Predicate condition) {
return queryMixin.on(condition);
}
public Q on(Predicate... conditions) {
return queryMixin.on(conditions);

View File

@ -155,6 +155,11 @@ public class SQLDeleteClause extends AbstractSQLClause<SQLDeleteClause> implemen
}
}
public SQLDeleteClause where(Predicate p) {
metadata.addWhere(p);
return this;
}
@Override
public SQLDeleteClause where(Predicate... o) {
for (Predicate p : o) {

View File

@ -213,6 +213,11 @@ public class SQLUpdateClause extends AbstractSQLClause<SQLUpdateClause> implemen
return this;
}
public SQLUpdateClause where(Predicate p) {
metadata.addWhere(p);
return this;
}
@Override
public SQLUpdateClause where(Predicate... o) {
for (Predicate p : o) {

View File

@ -15,11 +15,9 @@ import org.junit.Test;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.sql.Configuration;
import com.mysema.query.sql.H2Templates;
import com.mysema.query.sql.SQLQuery;
import com.mysema.query.sql.SQLQueryImpl;
import com.mysema.query.sql.SQLSerializer;
import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.types.EntityPath;
public class QueryPerformanceTest {
@ -56,7 +54,8 @@ public class QueryPerformanceTest {
Connection conn = Connections.getConnection();
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
PreparedStatement stmt = conn.prepareStatement("select COMPANIES.NAME from COMPANIES COMPANIES where COMPANIES.ID = ?");
PreparedStatement stmt = conn.prepareStatement(
"select COMPANIES.NAME from COMPANIES COMPANIES where COMPANIES.ID = ?");
try {
stmt.setInt(1, i);
ResultSet rs = stmt.executeQuery();
@ -80,7 +79,8 @@ public class QueryPerformanceTest {
Connection conn = Connections.getConnection();
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
PreparedStatement stmt = conn.prepareStatement("select COMPANIES.ID from COMPANIES COMPANIES where COMPANIES.NAME = ?");
PreparedStatement stmt = conn.prepareStatement(
"select COMPANIES.ID from COMPANIES COMPANIES where COMPANIES.NAME = ?");
try {
stmt.setString(1, String.valueOf(i));
ResultSet rs = stmt.executeQuery();
@ -107,7 +107,7 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf);
SQLQueryImpl query = new SQLQueryImpl(conn, conf);
query.from(companies).where(companies.id.eq((long)i)).list(companies.name);
}
System.err.println("qdsl by id " + (System.currentTimeMillis() - start));
@ -121,8 +121,9 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf);
CloseableIterator<String> it = query.from(companies).where(companies.id.eq((long)i)).iterate(companies.name);
SQLQueryImpl query = new SQLQueryImpl(conn, conf);
CloseableIterator<String> it = query.from(companies)
.where(companies.id.eq((long)i)).iterate(companies.name);
try {
while (it.hasNext()) {
it.next();
@ -142,26 +143,24 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf, new DefaultQueryMetadata().noValidate());
SQLQueryImpl query = new SQLQueryImpl(conn, conf, new DefaultQueryMetadata().noValidate());
query.from(companies).where(companies.id.eq((long)i)).list(companies.name);
}
System.err.println("qdsl by id " + (System.currentTimeMillis() - start) + " (no validation)");
}
@Test
public void Querydsl14() {
Connection conn = Connections.getConnection();
Configuration conf = new Configuration(new H2Templates());
QCompanies companies = QCompanies.companies;
EntityPath[] companies_ = new EntityPath[]{companies};
// Path[] name_ = new Path[]{companies.name};
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
SQLQuery query = new SQLQueryImpl(conn, conf);
query.from(companies_).where(companies.id.eq((long)i)).list(companies.name);
SQLQueryImpl query = new SQLQueryImpl(conn, conf);
query.from(companies).where(companies.id.eq((long)i)).list(companies.id, companies.name);
}
System.err.println("qdsl by id " + (System.currentTimeMillis() - start) + " (less varargs)");
System.err.println("qdsl by id " + (System.currentTimeMillis() - start) + " (two cols)");
}
@Test
@ -172,7 +171,7 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf);
SQLQueryImpl query = new SQLQueryImpl(conn, conf);
query.from(companies).where(companies.name.eq(String.valueOf(i))).list(companies.name);
}
System.err.println("qdsl by name " + (System.currentTimeMillis() - start));
@ -186,8 +185,9 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf);
CloseableIterator<String> it = query.from(companies).where(companies.name.eq(String.valueOf(i))).iterate(companies.name);
SQLQueryImpl query = new SQLQueryImpl(conn, conf);
CloseableIterator<String> it = query.from(companies)
.where(companies.name.eq(String.valueOf(i))).iterate(companies.name);
try {
while (it.hasNext()) {
it.next();
@ -207,7 +207,7 @@ public class QueryPerformanceTest {
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
QCompanies companies = QCompanies.companies;
SQLQuery query = new SQLQueryImpl(conn, conf, new DefaultQueryMetadata().noValidate());
SQLQueryImpl query = new SQLQueryImpl(conn, conf, new DefaultQueryMetadata().noValidate());
query.from(companies).where(companies.name.eq(String.valueOf(i))).list(companies.name);
}
System.err.println("qdsl by name " + (System.currentTimeMillis() - start) + " (no validation)");