Skip to content

Commit

Permalink
Use static query flags for common flags #528
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Oct 28, 2013
1 parent 0abdd1c commit 1c61446
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
Expand Up @@ -152,6 +152,16 @@ public Q addFlag(Position position, String prefix, Expression<?> expr) {
return queryMixin.addFlag(new QueryFlag(position, flag));
}

/**
* Add the given query flag
*
* @param flag
* @return
*/
public Q addFlag(QueryFlag flag) {
return queryMixin.addFlag(flag);
}

/**
* Add the given String literal as query flag
*
Expand Down Expand Up @@ -214,7 +224,7 @@ public boolean exists() {
* @return
*/
public Q forUpdate() {
return addFlag(Position.END, configuration.getTemplates().getForUpdate());
return addFlag(SQLTemplates.FOR_UPDATE_FLAG);
}

protected SQLSerializer createSerializer() {
Expand Down
Expand Up @@ -28,6 +28,9 @@
import com.mysema.query.types.SubQueryExpression;

/**
* SQLListeners is an SQLListener implementation which dispatches the
* notifications to a list of SQLListener instances
*
* @author tiwe
*
*/
Expand Down
51 changes: 21 additions & 30 deletions querydsl-sql/src/main/java/com/mysema/query/sql/SQLTemplates.java
Expand Up @@ -19,13 +19,16 @@
import java.util.Locale;
import java.util.Map;

import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Primitives;
import com.mysema.query.JoinType;
import com.mysema.query.QueryException;
import com.mysema.query.QueryFlag;
import com.mysema.query.QueryFlag.Position;
import com.mysema.query.QueryMetadata;
import com.mysema.query.QueryModifiers;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OperationImpl;
import com.mysema.query.types.Operator;
import com.mysema.query.types.OperatorImpl;
import com.mysema.query.types.Ops;
Expand Down Expand Up @@ -68,6 +71,21 @@ public class SQLTemplates extends Templates {

public static final Operator<Object> WITH_COLUMNS = new OperatorImpl<Object>("WITH_COLUMNS");

public static final Operator<Object> NO_WAIT = new OperatorImpl<Object>("NO_WAIT");

public static final Operator<Object> FOR_UPDATE = new OperatorImpl<Object>("FOR_UPDATE");

public static final Operator<Object> FOR_SHARE = new OperatorImpl<Object>("FOR_SHARE");

public static final QueryFlag NO_WAIT_FLAG = new QueryFlag(Position.END, new OperationImpl<Object>(
Object.class, NO_WAIT, ImmutableList.<Expression<?>>of()));

public static final QueryFlag FOR_UPDATE_FLAG = new QueryFlag(Position.END, new OperationImpl<Object>(
Object.class, FOR_UPDATE, ImmutableList.<Expression<?>>of()));

public static final QueryFlag FOR_SHARE_FLAG = new QueryFlag(Position.END, new OperationImpl<Object>(
Object.class, FOR_SHARE, ImmutableList.<Expression<?>>of()));

public static final SQLTemplates DEFAULT = new SQLTemplates("\"",'\\',false);

public static abstract class Builder {
Expand Down Expand Up @@ -139,10 +157,6 @@ public SQLTemplates build() {

private String dummyTable = "dual";

private String forUpdate = "\nfor update";

private String forShare = "\nfor share";

private String from = "\nfrom ";

private String fullJoin = "\nfull join ";
Expand Down Expand Up @@ -171,8 +185,6 @@ public SQLTemplates build() {

private String notNull = " not null";

private String noWait = " nowait";

private String offsetTemplate = "\noffset {0}";

private String on = "\non ";
Expand Down Expand Up @@ -222,6 +234,9 @@ protected SQLTemplates(String quoteStr, char escape, boolean useQuotes) {

add(WITH_ALIAS, "{0} as {1}", 0);
add(WITH_COLUMNS, "{0} {1}", 0);
add(FOR_UPDATE, "\nfor update");
add(FOR_SHARE, "\nfor share");
add(NO_WAIT, " nowait");

// boolean
add(Ops.AND, "{0} and {1}", 36);
Expand Down Expand Up @@ -531,22 +546,10 @@ public final boolean isBigDecimalSupported() {
return bigDecimalSupported;
}

public final String getForUpdate() {
return forUpdate;
}

public final String getForShare() {
return forShare;
}

public final boolean isUseQuotes() {
return useQuotes;
}

public final String getNoWait() {
return noWait;
}

public final boolean isUnionsWrapped() {
return unionsWrapped;
}
Expand Down Expand Up @@ -787,18 +790,6 @@ protected void setBigDecimalSupported(boolean bigDecimalSupported) {
this.bigDecimalSupported = bigDecimalSupported;
}

protected void setForUpdate(String forUpdate) {
this.forUpdate = forUpdate;
}

protected void setForShare(String forShare) {
this.forShare = forShare;
}

protected void setNoWait(String noWait) {
this.noWait = noWait;
}

protected void setUnionsWrapped(boolean unionsWrapped) {
this.unionsWrapped = unionsWrapped;
}
Expand Down
@@ -1,6 +1,6 @@
/*
* Copyright 2011, Mysema Ltd
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -28,43 +28,43 @@

/**
* PostgresQuery provides Postgres related extensions to SQLQuery
*
*
* @author tiwe
* @see SQLQuery
*
*/
public class PostgresQuery extends AbstractSQLQuery<PostgresQuery> implements SQLCommonQuery<PostgresQuery> {

public PostgresQuery(Connection conn) {
this(conn, new Configuration(new PostgresTemplates()), new DefaultQueryMetadata());
}

public PostgresQuery(Connection conn, SQLTemplates templates) {
this(conn, new Configuration(templates), new DefaultQueryMetadata());
}

public PostgresQuery(Connection conn, Configuration configuration) {
this(conn, configuration, new DefaultQueryMetadata());
}

public PostgresQuery(Connection conn, Configuration configuration, QueryMetadata metadata) {
super(conn, configuration, metadata);
}

/**
* @return
*/
public PostgresQuery forShare() {
return addFlag(Position.END, getConfiguration().getTemplates().getForShare());
return addFlag(SQLTemplates.FOR_SHARE_FLAG);
}

/**
* @return
*/
public PostgresQuery noWait() {
return addFlag(Position.END, getConfiguration().getTemplates().getNoWait());
return addFlag(SQLTemplates.NO_WAIT_FLAG);
}

/**
* @param paths
* @return
Expand All @@ -79,5 +79,5 @@ public PostgresQuery of(RelationalPath<?>... paths) {
}
return addFlag(Position.END, builder.toString());
}

}

0 comments on commit 1c61446

Please sign in to comment.