mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Extend and test SetQueryBandClause #553
This commit is contained in:
parent
c0896e0327
commit
3bd9c48ce6
@ -14,8 +14,8 @@
|
||||
package com.mysema.query.sql.teradata;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -41,6 +41,8 @@ public class SetQueryBandClause extends AbstractSQLClause<SetQueryBandClause> {
|
||||
|
||||
private transient String queryString;
|
||||
|
||||
private transient String parameter;
|
||||
|
||||
public SetQueryBandClause(Connection connection, SQLTemplates templates) {
|
||||
this(connection, new Configuration(templates));
|
||||
}
|
||||
@ -76,22 +78,31 @@ public class SetQueryBandClause extends AbstractSQLClause<SetQueryBandClause> {
|
||||
|
||||
@Override
|
||||
public long execute() {
|
||||
Statement stmt = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
stmt = connection.createStatement();
|
||||
stmt.execute(toString());
|
||||
stmt = connection.prepareStatement(toString());
|
||||
if (parameter != null) {
|
||||
stmt.setString(1, parameter);
|
||||
}
|
||||
return 1;
|
||||
} catch (SQLException e) {
|
||||
throw new QueryException("Caught " + e.getClass().getSimpleName() + " for "
|
||||
+ queryString, e);
|
||||
} finally {
|
||||
close(stmt);
|
||||
if (stmt != null) {
|
||||
close(stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SQLBindings> getSQL() {
|
||||
SQLBindings bindings = new SQLBindings(toString(), ImmutableList.of());
|
||||
SQLBindings bindings;
|
||||
if (configuration.getUseLiterals() || forSession) {
|
||||
bindings = new SQLBindings(toString(), ImmutableList.of());
|
||||
} else {
|
||||
bindings = new SQLBindings(toString(), ImmutableList.<Object>of(parameter));
|
||||
}
|
||||
return ImmutableList.of(bindings);
|
||||
}
|
||||
|
||||
@ -99,13 +110,20 @@ public class SetQueryBandClause extends AbstractSQLClause<SetQueryBandClause> {
|
||||
public String toString() {
|
||||
if (queryString == null) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("set query band = '");
|
||||
for (Map.Entry<String, String> entry : values.entrySet()) {
|
||||
builder.append(entry.getKey()).append("=").append(entry.getValue());
|
||||
builder.append(";");
|
||||
}
|
||||
builder.append(forSession ? "' for session" : "' for transaction");
|
||||
queryString = builder.toString();
|
||||
if (configuration.getUseLiterals() || forSession) {
|
||||
queryString = "set query_band="
|
||||
+ configuration.getTemplates().asLiteral(builder.toString())
|
||||
+ (forSession ? " for session" : " for transaction");
|
||||
parameter = null;
|
||||
} else {
|
||||
queryString = "set query_band=?" + (forSession ? " for session" : " for transaction");
|
||||
parameter = builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
return queryString;
|
||||
}
|
||||
|
||||
@ -38,6 +38,7 @@ import com.mysema.query.sql.dml.SQLUpdateClause;
|
||||
import com.mysema.query.sql.mysql.MySQLQuery;
|
||||
import com.mysema.query.sql.mysql.MySQLReplaceClause;
|
||||
import com.mysema.query.sql.oracle.OracleQuery;
|
||||
import com.mysema.query.sql.teradata.SetQueryBandClause;
|
||||
import com.mysema.query.sql.teradata.TeradataQuery;
|
||||
|
||||
public abstract class AbstractBaseTest {
|
||||
@ -106,6 +107,10 @@ public abstract class AbstractBaseTest {
|
||||
return new SQLMergeClause(connection, configuration, e);
|
||||
}
|
||||
|
||||
protected SetQueryBandClause setQueryBand() {
|
||||
return new SetQueryBandClause(connection, configuration);
|
||||
}
|
||||
|
||||
protected ExtendedSQLQuery extQuery() {
|
||||
return new ExtendedSQLQuery(connection, configuration);
|
||||
}
|
||||
|
||||
@ -1179,6 +1179,20 @@ public class SelectBase extends AbstractBaseTest {
|
||||
assertEquals("from SURVEY s, SURVEY s2", query.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@IncludeIn(TERADATA)
|
||||
public void SetQueryBand_ForSession() {
|
||||
setQueryBand().set("a", "bb").forSession().execute();
|
||||
query().from(survey).list(survey.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IncludeIn(TERADATA)
|
||||
public void SetQueryBand_ForTransaction() {
|
||||
setQueryBand().set("a", "bb").forTransaction().execute();
|
||||
query().from(survey).list(survey.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Single() {
|
||||
assertNotNull(query().from(survey).singleResult(survey.name));
|
||||
|
||||
@ -10,18 +10,29 @@ import com.mysema.query.sql.SQLTemplates;
|
||||
|
||||
public class SetQueryBandClauseTest {
|
||||
|
||||
private Configuration conf;
|
||||
|
||||
private SetQueryBandClause clause;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Configuration conf = new Configuration(SQLTemplates.DEFAULT);
|
||||
conf = new Configuration(SQLTemplates.DEFAULT);
|
||||
conf.setUseLiterals(true);
|
||||
clause = new SetQueryBandClause(null, conf);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ToString() {
|
||||
clause.set("a", "b");
|
||||
assertEquals("set query band = 'a=b;' for session", clause.toString());
|
||||
assertEquals("set query_band='a=b;' for session", clause.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ToString2() {
|
||||
conf.setUseLiterals(false);
|
||||
clause.set("a", "b");
|
||||
clause.forTransaction();
|
||||
assertEquals("set query_band=? for transaction", clause.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -29,7 +40,7 @@ public class SetQueryBandClauseTest {
|
||||
clause.forTransaction();
|
||||
clause.set("a", "b");
|
||||
clause.set("b", "c");
|
||||
assertEquals("set query band = 'b=c;a=b;' for transaction", clause.toString());
|
||||
assertEquals("set query_band='b=c;a=b;' for transaction", clause.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -37,7 +48,7 @@ public class SetQueryBandClauseTest {
|
||||
clause.forTransaction();
|
||||
clause.set("a", "b");
|
||||
clause.set("b", "c");
|
||||
assertEquals("set query band = 'b=c;a=b;' for transaction", clause.getSQL().get(0).getSQL());
|
||||
assertEquals("set query_band='b=c;a=b;' for transaction", clause.getSQL().get(0).getSQL());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user