Skip to content

Commit

Permalink
Fix listener handling
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Feb 13, 2016
1 parent e47ad11 commit db83fae
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions querydsl-sql/src/main/java/com/querydsl/sql/SQLListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,9 @@ public void exception(final SQLListenerContext context) {
listener.exception(context);
}
}

public Set<SQLDetailedListener> getListeners() {
return listeners;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* 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
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.sql;

/**
* NoClostListener can be used to block {@link SQLCloseListener} from closing the connection, useful for
* helper query executions
*/
public final class SQLNoCloseListener extends SQLBaseListener {

public static final SQLNoCloseListener DEFAULT = new SQLNoCloseListener();

private SQLNoCloseListener() { }

@Override
public void start(SQLListenerContext context) {
context.setData(AbstractSQLQuery.PARENT_CONTEXT, context);
}

@Override
public void end(SQLListenerContext context) {
context.setData(AbstractSQLQuery.PARENT_CONTEXT, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ public List<SQLBindings> getSQL() {

private boolean hasRow() {
SQLQuery<?> query = new SQLQuery<Void>(connection(), configuration).from(entity);
for (SQLListener listener : listeners.getListeners()) {
query.addListener(listener);
}
query.addListener(SQLNoCloseListener.DEFAULT);
addKeyConditions(query);
return query.select(Expressions.ONE).fetchFirst() != null;
}
Expand All @@ -335,17 +339,25 @@ private long executeCompositeMerge() {
// update
SQLUpdateClause update = new SQLUpdateClause(connection(), configuration, entity);
populate(update);
addListeners(update);
addKeyConditions(update);
return update.execute();
} else {
// insert
SQLInsertClause insert = new SQLInsertClause(connection(), configuration, entity);
addListeners(insert);
populate(insert);
return insert.execute();

}
}

private void addListeners(AbstractSQLClause<?> clause) {
for (SQLListener listener : listeners.getListeners()) {
clause.addListener(listener);
}
}

@SuppressWarnings("unchecked")
private void populate(StoreClause<?> clause) {
for (int i = 0; i < columns.size(); i++) {
Expand Down
21 changes: 21 additions & 0 deletions querydsl-sql/src/test/java/com/querydsl/sql/MergeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Before;
Expand All @@ -31,6 +32,7 @@
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.sql.dml.AbstractSQLClause;
import com.querydsl.sql.dml.SQLMergeClause;
import com.querydsl.sql.domain.QSurvey;

Expand Down Expand Up @@ -223,5 +225,24 @@ public void merge_with_templateExpression_in_batch() {
assertEquals(1, merge.execute());
}

@Test
public void merge_listener() {
final AtomicInteger calls = new AtomicInteger(0);
SQLListener listener = new SQLBaseListener() {
@Override
public void end(SQLListenerContext context) {
if (context.getData(AbstractSQLQuery.PARENT_CONTEXT) == null) {
calls.incrementAndGet();
}
}
};

SQLMergeClause clause = merge(survey).keys(survey.id)
.set(survey.id, 5)
.set(survey.name, "Hello World");
clause.addListener(listener);
assertEquals(1, clause.execute());
assertEquals(1, calls.intValue());
}

}

0 comments on commit db83fae

Please sign in to comment.