[#2066] Add Executor.bindValues(QueryPart) to extract bind values in the
context of an Executor (i.e. Configuration)
This commit is contained in:
parent
56d7064043
commit
372b1df203
@ -139,6 +139,8 @@ public interface Query extends QueryPart, Attachable {
|
||||
* Retrieve the bind values that will be bound by this Query. This
|
||||
* <code>List</code> cannot be modified. To modify bind values, use
|
||||
* {@link #getParams()} instead.
|
||||
*
|
||||
* @see Executor#extractBindValues(QueryPart)
|
||||
*/
|
||||
List<Object> getBindValues();
|
||||
|
||||
@ -152,6 +154,7 @@ public interface Query extends QueryPart, Attachable {
|
||||
*
|
||||
* @see Param
|
||||
* @see Factory#param(String, Object)
|
||||
* @see Executor#extractParams(QueryPart)
|
||||
*/
|
||||
Map<String, Param<?>> getParams();
|
||||
|
||||
@ -163,6 +166,7 @@ public interface Query extends QueryPart, Attachable {
|
||||
*
|
||||
* @see Param
|
||||
* @see Factory#param(String, Object)
|
||||
* @see Executor#extractParam(QueryPart, String)
|
||||
*/
|
||||
Param<?> getParam(String name);
|
||||
|
||||
|
||||
@ -41,8 +41,6 @@ import static org.jooq.conf.SettingsTools.executePreparedStatements;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -93,25 +91,17 @@ abstract class AbstractQuery extends AbstractQueryPart implements Query, Attacha
|
||||
|
||||
@Override
|
||||
public final List<Object> getBindValues() {
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
|
||||
for (Param<?> param : getParams().values()) {
|
||||
result.add(param.getValue());
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(result);
|
||||
return create().extractBindValues(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<String, Param<?>> getParams() {
|
||||
ParamCollector collector = new ParamCollector(getConfiguration());
|
||||
collector.bind(this);
|
||||
return Collections.unmodifiableMap(collector.result);
|
||||
return create().extractParams(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Param<?> getParam(String name) {
|
||||
return getParams().get(name);
|
||||
return create().extractParam(this, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -63,6 +63,7 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -93,6 +94,7 @@ import org.jooq.LoaderOptionsStep;
|
||||
import org.jooq.MergeKeyStep;
|
||||
import org.jooq.MergeUsingStep;
|
||||
import org.jooq.Meta;
|
||||
import org.jooq.Param;
|
||||
import org.jooq.Query;
|
||||
import org.jooq.QueryPart;
|
||||
import org.jooq.Record;
|
||||
@ -405,6 +407,55 @@ public class Executor implements Configuration {
|
||||
return renderContext().inline(true).render(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the bind values that will be bound by a given
|
||||
* <code>QueryPart</code>
|
||||
* <p>
|
||||
* The returned <code>List</code> is immutable. To modify bind values, use
|
||||
* {@link #extractParams(QueryPart)} instead.
|
||||
*/
|
||||
public final List<Object> extractBindValues(QueryPart part) {
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
|
||||
for (Param<?> param : extractParams(part).values()) {
|
||||
result.add(param.getValue());
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a <code>Map</code> of named parameters
|
||||
* <p>
|
||||
* The <code>Map</code> itself is immutable, but the {@link Param} elements
|
||||
* allow for modifying bind values on an existing {@link Query} (or any
|
||||
* other {@link QueryPart}).
|
||||
* <p>
|
||||
* Bind values created with {@link Factory#val(Object)} will have their bind
|
||||
* index as name.
|
||||
*
|
||||
* @see Param
|
||||
* @see Factory#param(String, Object)
|
||||
*/
|
||||
public final Map<String, Param<?>> extractParams(QueryPart part) {
|
||||
ParamCollector collector = new ParamCollector(this);
|
||||
collector.bind(part);
|
||||
return Collections.unmodifiableMap(collector.result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a named parameter from a {@link QueryPart}, provided its name.
|
||||
* <p>
|
||||
* Bind values created with {@link Factory#val(Object)} will have their bind
|
||||
* index as name.
|
||||
*
|
||||
* @see Param
|
||||
* @see Factory#param(String, Object)
|
||||
*/
|
||||
public final Param<?> extractParam(QueryPart part, String name) {
|
||||
return extractParams(part).get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new {@link BindContext} for the context of this executor
|
||||
* <p>
|
||||
|
||||
@ -2332,10 +2332,25 @@ public class BasicTest extends AbstractTest {
|
||||
Param<?> p32 = q3.getParam("2");
|
||||
Param<?> p42 = q4.getParam("p2");
|
||||
|
||||
assertEquals(Arrays.asList("1", "2"), new ArrayList<String>(q1.getParams().keySet()));
|
||||
assertEquals(Arrays.asList("p1", "p2"), new ArrayList<String>(q2.getParams().keySet()));
|
||||
assertEquals(Arrays.asList("p1", "2"), new ArrayList<String>(q3.getParams().keySet()));
|
||||
assertEquals(Arrays.asList("1", "p2"), new ArrayList<String>(q4.getParams().keySet()));
|
||||
assertEquals(p11, create.extractParam(q1, "1"));
|
||||
assertEquals(p21, create.extractParam(q2, "p1"));
|
||||
assertEquals(p31, create.extractParam(q3, "p1"));
|
||||
assertEquals(p41, create.extractParam(q4, "1"));
|
||||
|
||||
assertEquals(p12, create.extractParam(q1, "2"));
|
||||
assertEquals(p22, create.extractParam(q2, "p2"));
|
||||
assertEquals(p32, create.extractParam(q3, "2"));
|
||||
assertEquals(p42, create.extractParam(q4, "p2"));
|
||||
|
||||
assertEquals(Arrays.asList("1", "2"), new ArrayList<String>(create.extractParams(q1).keySet()));
|
||||
assertEquals(Arrays.asList("p1", "p2"), new ArrayList<String>(create.extractParams(q2).keySet()));
|
||||
assertEquals(Arrays.asList("p1", "2"), new ArrayList<String>(create.extractParams(q3).keySet()));
|
||||
assertEquals(Arrays.asList("1", "p2"), new ArrayList<String>(create.extractParams(q4).keySet()));
|
||||
|
||||
assertEquals(Arrays.asList("1", "2"), new ArrayList<String>(create.extractParams(q1).keySet()));
|
||||
assertEquals(Arrays.asList("p1", "p2"), new ArrayList<String>(create.extractParams(q2).keySet()));
|
||||
assertEquals(Arrays.asList("p1", "2"), new ArrayList<String>(create.extractParams(q3).keySet()));
|
||||
assertEquals(Arrays.asList("1", "p2"), new ArrayList<String>(create.extractParams(q4).keySet()));
|
||||
|
||||
// Types
|
||||
assertEquals(Integer.class, p11.getType());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user