mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
#619667 : added support for query flags in SQL DML clauses
This commit is contained in:
parent
3b209430f8
commit
85fccaaec5
@ -28,6 +28,8 @@ public class QueryFlag implements Serializable{
|
||||
|
||||
START,
|
||||
|
||||
START_OVERRIDE,
|
||||
|
||||
AFTER_SELECT,
|
||||
|
||||
AFTER_PROJECTION,
|
||||
|
||||
@ -102,12 +102,15 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
}
|
||||
}
|
||||
|
||||
protected void serialize(Position position, Set<QueryFlag> flags) {
|
||||
protected boolean serialize(Position position, Set<QueryFlag> flags) {
|
||||
boolean handled = false;
|
||||
for (QueryFlag flag : flags){
|
||||
if (flag.getPosition() == position){
|
||||
handle(flag.getFlag());
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
return handled;
|
||||
}
|
||||
|
||||
public void setConstantPrefix(String prefix){
|
||||
|
||||
@ -33,7 +33,6 @@ import com.mysema.query.SearchResults;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.support.ProjectableQuery;
|
||||
import com.mysema.query.support.QueryMixin;
|
||||
import com.mysema.query.types.EConstructor;
|
||||
import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.FactoryExpression;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
|
||||
@ -239,21 +239,33 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
|
||||
}
|
||||
|
||||
public void serializeForDelete(PEntity<?> entity, EBoolean where) {
|
||||
public void serializeForDelete(QueryMetadata metadata, PEntity<?> entity) {
|
||||
this.entity = entity;
|
||||
append(templates.getDeleteFrom());
|
||||
|
||||
serialize(Position.START, metadata.getFlags());
|
||||
|
||||
if (!serialize(Position.START_OVERRIDE, metadata.getFlags())){
|
||||
append(templates.getDeleteFrom());
|
||||
}
|
||||
handle(entity);
|
||||
if (where != null) {
|
||||
if (metadata.getWhere() != null) {
|
||||
skipParent = true;
|
||||
append(templates.getWhere()).handle(where);
|
||||
append(templates.getWhere()).handle(metadata.getWhere());
|
||||
skipParent = false;
|
||||
}
|
||||
|
||||
serialize(Position.END, metadata.getFlags());
|
||||
}
|
||||
|
||||
public void serializeForMerge(PEntity<?> entity, List<Path<?>> keys,
|
||||
public void serializeForMerge(QueryMetadata metadata, PEntity<?> entity, List<Path<?>> keys,
|
||||
List<Path<?>> columns, List<Expr<?>> values, @Nullable SubQuery<?> subQuery) {
|
||||
this.entity = entity;
|
||||
append(templates.getMergeInto());
|
||||
|
||||
serialize(Position.START, metadata.getFlags());
|
||||
|
||||
if (!serialize(Position.START_OVERRIDE, metadata.getFlags())){
|
||||
append(templates.getMergeInto());
|
||||
}
|
||||
handle(entity);
|
||||
append(" ");
|
||||
// columns
|
||||
@ -279,12 +291,19 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
append(templates.getValues());
|
||||
append("(").handle(COMMA, values).append(") ");
|
||||
}
|
||||
|
||||
serialize(Position.END, metadata.getFlags());
|
||||
}
|
||||
|
||||
public void serializeForInsert(PEntity<?> entity, List<Path<?>> columns,
|
||||
public void serializeForInsert(QueryMetadata metadata, PEntity<?> entity, List<Path<?>> columns,
|
||||
List<Expr<?>> values, @Nullable SubQuery<?> subQuery) {
|
||||
this.entity = entity;
|
||||
append(templates.getInsertInto());
|
||||
|
||||
serialize(Position.START, metadata.getFlags());
|
||||
|
||||
if (!serialize(Position.START_OVERRIDE, metadata.getFlags())){
|
||||
append(templates.getInsertInto());
|
||||
}
|
||||
handle(entity);
|
||||
// columns
|
||||
if (!columns.isEmpty()){
|
||||
@ -305,12 +324,18 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
handle(COMMA, values);
|
||||
append(")");
|
||||
}
|
||||
|
||||
serialize(Position.END, metadata.getFlags());
|
||||
}
|
||||
|
||||
public void serializeForUpdate(PEntity<?> entity,
|
||||
List<Pair<Path<?>, ?>> updates, EBoolean where) {
|
||||
public void serializeForUpdate(QueryMetadata metadata, PEntity<?> entity, List<Pair<Path<?>, ?>> updates) {
|
||||
this.entity = entity;
|
||||
append(templates.getUpdate());
|
||||
|
||||
serialize(Position.START, metadata.getFlags());
|
||||
|
||||
if (!serialize(Position.START_OVERRIDE, metadata.getFlags())){
|
||||
append(templates.getUpdate());
|
||||
}
|
||||
handle(entity);
|
||||
append("\n");
|
||||
append(templates.getSet());
|
||||
@ -330,9 +355,11 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
first = false;
|
||||
}
|
||||
skipParent = false;
|
||||
if (where != null) {
|
||||
append(templates.getWhere()).handle(where);
|
||||
if (metadata.getWhere() != null) {
|
||||
append(templates.getWhere()).handle(metadata.getWhere());
|
||||
}
|
||||
|
||||
serialize(Position.END, metadata.getFlags());
|
||||
}
|
||||
|
||||
private void serializeSources(List<JoinExpression> joins) {
|
||||
|
||||
@ -13,8 +13,11 @@ import java.util.Collections;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mysema.query.BooleanBuilder;
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryException;
|
||||
import com.mysema.query.QueryFlag;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.dml.DeleteClause;
|
||||
import com.mysema.query.sql.Configuration;
|
||||
import com.mysema.query.sql.SQLSerializer;
|
||||
@ -40,8 +43,8 @@ public class SQLDeleteClause extends AbstractSQLClause implements DeleteClause<S
|
||||
|
||||
private final PEntity<?> entity;
|
||||
|
||||
private final BooleanBuilder where = new BooleanBuilder();
|
||||
|
||||
private final QueryMetadata metadata = new DefaultQueryMetadata();
|
||||
|
||||
public SQLDeleteClause(Connection connection, SQLTemplates templates, PEntity<?> entity) {
|
||||
this(connection, new Configuration(templates), entity);
|
||||
}
|
||||
@ -51,6 +54,11 @@ public class SQLDeleteClause extends AbstractSQLClause implements DeleteClause<S
|
||||
this.connection = connection;
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public SQLDeleteClause addFlag(Position position, String flag){
|
||||
metadata.addFlag(new QueryFlag(position, flag));
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void close(PreparedStatement stmt) {
|
||||
try {
|
||||
@ -63,7 +71,7 @@ public class SQLDeleteClause extends AbstractSQLClause implements DeleteClause<S
|
||||
@Override
|
||||
public long execute() {
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForDelete(entity, where.getValue());
|
||||
serializer.serializeForDelete(metadata, entity);
|
||||
String queryString = serializer.toString();
|
||||
logger.debug(queryString);
|
||||
|
||||
@ -83,16 +91,14 @@ public class SQLDeleteClause extends AbstractSQLClause implements DeleteClause<S
|
||||
|
||||
@Override
|
||||
public SQLDeleteClause where(EBoolean... o) {
|
||||
for (EBoolean e : o){
|
||||
where.and(e);
|
||||
}
|
||||
metadata.addWhere(o);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForDelete(entity, where.getValue());
|
||||
serializer.serializeForDelete(metadata, entity);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,11 @@ import javax.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryException;
|
||||
import com.mysema.query.QueryFlag;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.dml.InsertClause;
|
||||
import com.mysema.query.sql.Configuration;
|
||||
import com.mysema.query.sql.SQLSerializer;
|
||||
@ -49,6 +53,8 @@ public class SQLInsertClause extends AbstractSQLClause implements InsertClause<S
|
||||
private final Connection connection;
|
||||
|
||||
private final PEntity<?> entity;
|
||||
|
||||
private final QueryMetadata metadata = new DefaultQueryMetadata();
|
||||
|
||||
@Nullable
|
||||
private SubQuery<?> subQuery;
|
||||
@ -71,6 +77,11 @@ public class SQLInsertClause extends AbstractSQLClause implements InsertClause<S
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public SQLInsertClause addFlag(Position position, String flag){
|
||||
metadata.addFlag(new QueryFlag(position, flag));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SQLInsertClause addBatch() {
|
||||
batches.add(new SQLInsertBatch(columns, values, subQuery));
|
||||
columns.clear();
|
||||
@ -138,13 +149,13 @@ public class SQLInsertClause extends AbstractSQLClause implements InsertClause<S
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
PreparedStatement stmt = null;
|
||||
if (batches.isEmpty()){
|
||||
serializer.serializeForInsert(entity, columns, values, subQuery);
|
||||
serializer.serializeForInsert(metadata, entity, columns, values, subQuery);
|
||||
queryString = serializer.toString();
|
||||
logger.debug(queryString);
|
||||
stmt = connection.prepareStatement(queryString);
|
||||
setParameters(stmt, serializer.getConstants(),Collections.<Param<?>,Object>emptyMap());
|
||||
}else{
|
||||
serializer.serializeForInsert(entity, batches.get(0).getColumns(), batches.get(0).getValues(), batches.get(0).getSubQuery());
|
||||
serializer.serializeForInsert(metadata, entity, batches.get(0).getColumns(), batches.get(0).getValues(), batches.get(0).getSubQuery());
|
||||
queryString = serializer.toString();
|
||||
logger.debug(queryString);
|
||||
stmt = connection.prepareStatement(queryString);
|
||||
@ -158,7 +169,7 @@ public class SQLInsertClause extends AbstractSQLClause implements InsertClause<S
|
||||
SQLInsertBatch batch = batches.get(i);
|
||||
serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
// TODO : add support for dry serialization (without SQL construction)
|
||||
serializer.serializeForInsert(entity, batch.getColumns(), batch.getValues(), batch.getSubQuery());
|
||||
serializer.serializeForInsert(metadata, entity, batch.getColumns(), batch.getValues(), batch.getSubQuery());
|
||||
setParameters(stmt, serializer.getConstants(),Collections.<Param<?>,Object>emptyMap());
|
||||
stmt.addBatch();
|
||||
}
|
||||
@ -249,7 +260,7 @@ public class SQLInsertClause extends AbstractSQLClause implements InsertClause<S
|
||||
@Override
|
||||
public String toString(){
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForInsert(entity, columns, values, subQuery);
|
||||
serializer.serializeForInsert(metadata, entity, columns, values, subQuery);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -17,7 +17,11 @@ import javax.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryException;
|
||||
import com.mysema.query.QueryFlag;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.dml.StoreClause;
|
||||
import com.mysema.query.sql.Configuration;
|
||||
import com.mysema.query.sql.SQLQuery;
|
||||
@ -49,6 +53,8 @@ public class SQLMergeClause extends AbstractSQLClause implements StoreClause<SQL
|
||||
|
||||
private final PEntity<?> entity;
|
||||
|
||||
private final QueryMetadata metadata = new DefaultQueryMetadata();
|
||||
|
||||
private final List<Path<?>> keys = new ArrayList<Path<?>>();
|
||||
|
||||
@Nullable
|
||||
@ -65,6 +71,11 @@ public class SQLMergeClause extends AbstractSQLClause implements StoreClause<SQL
|
||||
this.connection = connection;
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public SQLMergeClause addFlag(Position position, String flag){
|
||||
metadata.addFlag(new QueryFlag(position, flag));
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void close(PreparedStatement stmt) {
|
||||
try {
|
||||
@ -119,7 +130,7 @@ public class SQLMergeClause extends AbstractSQLClause implements StoreClause<SQL
|
||||
|
||||
private long executeNativeMerge() {
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForMerge(entity, keys, columns, values, subQuery);
|
||||
serializer.serializeForMerge(metadata, entity, keys, columns, values, subQuery);
|
||||
String queryString = serializer.toString();
|
||||
logger.debug(queryString);
|
||||
|
||||
@ -163,7 +174,7 @@ public class SQLMergeClause extends AbstractSQLClause implements StoreClause<SQL
|
||||
@Override
|
||||
public String toString(){
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForMerge(entity, keys, columns, values, subQuery);
|
||||
serializer.serializeForMerge(metadata, entity, keys, columns, values, subQuery);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -16,8 +16,11 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mysema.commons.lang.Pair;
|
||||
import com.mysema.query.BooleanBuilder;
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryException;
|
||||
import com.mysema.query.QueryFlag;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.dml.UpdateClause;
|
||||
import com.mysema.query.sql.Configuration;
|
||||
import com.mysema.query.sql.SQLSerializer;
|
||||
@ -47,8 +50,8 @@ public class SQLUpdateClause extends AbstractSQLClause implements UpdateClause<
|
||||
|
||||
private final List<Pair<Path<?>,?>> updates = new ArrayList<Pair<Path<?>,?>>();
|
||||
|
||||
private final BooleanBuilder where = new BooleanBuilder();
|
||||
|
||||
private final QueryMetadata metadata = new DefaultQueryMetadata();
|
||||
|
||||
public SQLUpdateClause(Connection connection, SQLTemplates templates, PEntity<?> entity) {
|
||||
this(connection, new Configuration(templates), entity);
|
||||
}
|
||||
@ -58,6 +61,11 @@ public class SQLUpdateClause extends AbstractSQLClause implements UpdateClause<
|
||||
this.connection = connection;
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public SQLUpdateClause addFlag(Position position, String flag){
|
||||
metadata.addFlag(new QueryFlag(position, flag));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
protected void close(PreparedStatement stmt) {
|
||||
@ -71,7 +79,7 @@ public class SQLUpdateClause extends AbstractSQLClause implements UpdateClause<
|
||||
@Override
|
||||
public long execute() {
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForUpdate(entity, updates, where.getValue());
|
||||
serializer.serializeForUpdate(metadata, entity, updates);
|
||||
String queryString = serializer.toString();
|
||||
logger.debug(queryString);
|
||||
|
||||
@ -114,16 +122,14 @@ public class SQLUpdateClause extends AbstractSQLClause implements UpdateClause<
|
||||
|
||||
@Override
|
||||
public SQLUpdateClause where(EBoolean... o) {
|
||||
for (EBoolean e : o){
|
||||
where.and(e);
|
||||
}
|
||||
metadata.addWhere(o);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
SQLSerializer serializer = new SQLSerializer(configuration.getTemplates(), true);
|
||||
serializer.serializeForUpdate(entity, updates, where.getValue());
|
||||
serializer.serializeForUpdate(metadata, entity, updates);
|
||||
return serializer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,15 +5,21 @@
|
||||
*/
|
||||
package com.mysema.query._mysql;
|
||||
|
||||
import static com.mysema.query.Constants.survey;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.Connections;
|
||||
import com.mysema.query.InsertBaseTest;
|
||||
import com.mysema.query.Target;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.sql.MySQLTemplates;
|
||||
import com.mysema.query.sql.dml.SQLInsertClause;
|
||||
import com.mysema.testutil.Label;
|
||||
import com.mysema.testutil.ResourceCheck;
|
||||
|
||||
@ -31,5 +37,17 @@ public class InsertMySQLTest extends InsertBaseTest{
|
||||
templates = new MySQLTemplates().newLineToSingleSpace();
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insert_with_special_options(){
|
||||
SQLInsertClause clause = insert(survey)
|
||||
.columns(survey.id, survey.name)
|
||||
.values(3, "Hello");
|
||||
|
||||
clause.addFlag(Position.START_OVERRIDE, "INSERT IGNORE INTO ");
|
||||
|
||||
assertEquals("INSERT IGNORE INTO SURVEY(ID, NAME) values (?, ?)", clause.toString());
|
||||
clause.execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user