Skip to content

Commit

Permalink
(#29) extracted statement composition and parameter management from j…
Browse files Browse the repository at this point in the history
…dbc statements
  • Loading branch information
svettwer committed Sep 5, 2019
1 parent 6b1aca4 commit b615aef
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import com.consol.citrus.db.driver.data.CitrusBlob;
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.statement.JdbcCallableStatement;
import com.consol.citrus.db.driver.statement.JdbcPreparedStatement;
import com.consol.citrus.db.driver.statement.JdbcStatement;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.data.Row;
import com.consol.citrus.db.driver.dataset.DataSet;
import com.consol.citrus.db.driver.statement.JdbcStatement;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class JdbcResultSet implements java.sql.ResultSet {
/**
* Constructor using remote result set.
*/
JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) {
public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) {
this.dataSet = dataSet;
this.statement = statement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.consol.citrus.db.driver;
package com.consol.citrus.db.driver.statement;

import com.consol.citrus.db.driver.JdbcConnection;
import com.consol.citrus.db.driver.data.CitrusBlob;
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.utils.LobUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.consol.citrus.db.driver;
package com.consol.citrus.db.driver.statement;

import com.consol.citrus.db.driver.JdbcConnection;
import com.consol.citrus.db.driver.data.CitrusBlob;
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.utils.LobUtils;
Expand Down Expand Up @@ -43,13 +44,9 @@
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;


public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement {
Expand All @@ -58,14 +55,16 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
private final String preparedStatement;

/** The parameters to add to the statement */
private final Map<String, Object> parameters = new TreeMap<>();
private final StatementParameters parameters = new StatementParameters();

/** A list of parameter sets for batch execution purposes */
private final List<Map<String, Object>> batchParameters = new LinkedList<>();
private final List<StatementParameters> batchParameters = new LinkedList<>();

private LobUtils lobUtils = new LobUtils();

JdbcPreparedStatement(final HttpClient httpClient,
private StatementComposer statementComposer = new StatementComposer();

public JdbcPreparedStatement(final HttpClient httpClient,
final String preparedStatement,
final String serverUrl,
final JdbcConnection connection) {
Expand Down Expand Up @@ -199,15 +198,15 @@ public boolean execute() throws SQLException {

@Override
public void addBatch() {
batchParameters.add(new HashMap<>(parameters));
batchParameters.add(new StatementParameters(parameters));
parameters.clear();
}

@Override
public int[] executeBatch() throws SQLException {
final ArrayList<Integer> arrayList = new ArrayList<>();
for (final Map<String, Object> statementParameters : batchParameters){
execute(composeStatement(statementParameters));
for (final StatementParameters statementParameters : batchParameters){
execute(statementComposer.composeStatement(preparedStatement, statementParameters));
arrayList.add(getUpdateCount());
}
return ArrayUtils.toPrimitive(arrayList.toArray(new Integer[0]));
Expand Down Expand Up @@ -382,22 +381,18 @@ public void setNClob(final int parameterIndex, final Reader reader) throws SQLEx
}

void setParameter(final int parameterIndex, final Object value){
setParameter(String.valueOf(parameterIndex - 1), value);
parameters.setParameter(parameterIndex, value);
}

void setParameter(final String parameterName, final Object value){
parameters.put(parameterName, value);
parameters.setParameter(parameterName, value);
}

String composeStatement() {
return composeStatement(parameters);
}

private String composeStatement(final Map<String, Object> parameters) {
return preparedStatement + " - (" + parameters.values().stream().map(param -> param != null ? param.toString() : "null").collect(Collectors.joining(",")) + ")";
return statementComposer.composeStatement(preparedStatement, parameters);
}

Map<String, Object> getParameters() {
StatementParameters getParameters() {
return parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

package com.consol.citrus.db.driver;
package com.consol.citrus.db.driver.statement;

import com.consol.citrus.db.driver.JdbcConnection;
import com.consol.citrus.db.driver.JdbcResultSet;
import com.consol.citrus.db.driver.exchange.DatabaseResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -62,7 +64,7 @@ public class JdbcStatement implements Statement {
* @param httpClient The http client to use for the db communication
* @param serverUrl Thr url of the server
*/
JdbcStatement(final HttpClient httpClient, final String serverUrl, final JdbcConnection connection) {
public JdbcStatement(final HttpClient httpClient, final String serverUrl, final JdbcConnection connection) {
this.httpClient = httpClient;
this.serverUrl = serverUrl;
this.connection = connection;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.consol.citrus.db.driver.statement;

import java.util.stream.Collectors;

class StatementComposer {

String composeStatement(final String statement, final StatementParameters parameters) {
return statement + " - (" +
parameters.getParametersAsMap()
.values()
.stream()
.map(param -> param != null ? param.toString() : "null")
.collect(Collectors.joining(",")) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.consol.citrus.db.driver.statement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toMap;

public class StatementParameters {

private Map<String, Object> namedParameters = new HashMap<>();
private Map<Integer, Object> indexedParameters = new HashMap<>();

StatementParameters(final StatementParameters statementParameters) {
this.namedParameters = new HashMap<>(statementParameters.getParametersAsMap());

final List<Object> originalIndexParameters = statementParameters.getParametersAsList();
this.indexedParameters = IntStream
.range(0, originalIndexParameters.size())
.boxed()
.collect(toMap(Function.identity(), originalIndexParameters::get));
}

StatementParameters() {
}

public Object get(final int index) {
return indexedParameters.get(index);
}

public Object get(final String namedParameter) {
return namedParameters.get(namedParameter);
}

void setParameter(final String parameterName, final Object value) {
namedParameters.put(parameterName, value);
}

void setParameter(final int parameterIndex, final Object value) {
indexedParameters.put(parameterIndex-1, value);
}

Map<String, Object> getParametersAsMap(){
return namedParameters;
}

List<Object> getParametersAsList() {
return new ArrayList<>(indexedParameters.values());
}

void clear() {
namedParameters.clear();
indexedParameters.clear();
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof StatementParameters)) return false;
final StatementParameters that = (StatementParameters) o;
return Objects.equals(namedParameters, that.namedParameters);
}

@Override
public int hashCode() {
return Objects.hash(namedParameters);
}

@Override
public String toString() {
return "StatementParameters{" +
"parameters=" + namedParameters +
'}';
}

public int size() {
return namedParameters.size() + indexedParameters.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.consol.citrus.db.driver.data.CitrusBlob;
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.statement.JdbcPreparedStatement;
import com.consol.citrus.db.driver.statement.JdbcStatement;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.consol.citrus.db.driver.data.Row;
import com.consol.citrus.db.driver.dataset.DataSet;
import com.consol.citrus.db.driver.dataset.DataSetBuilder;
import com.consol.citrus.db.driver.statement.JdbcStatement;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.consol.citrus.db.driver;
package com.consol.citrus.db.driver.statement;

import com.consol.citrus.db.driver.JdbcConnection;
import com.consol.citrus.db.driver.JdbcResultSet;
import com.consol.citrus.db.driver.data.CitrusBlob;
import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.dataset.DataSet;
import com.consol.citrus.db.driver.statement.JdbcCallableStatement;
import com.consol.citrus.db.driver.statement.JdbcStatement;
import com.consol.citrus.db.driver.utils.LobUtils;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
Expand Down Expand Up @@ -74,7 +78,7 @@ public void testRegisterOutParameter() throws SQLException {
callableStatement.registerOutParameter(index, Types.INTEGER);

//THEN
assertEquals(callableStatement.getParameters().get("1"), "?");
assertEquals(callableStatement.getParameters().get(1), "?");
}

@Test
Expand All @@ -87,7 +91,7 @@ public void testRegisterOutParameterWithScale() throws SQLException {
callableStatement.registerOutParameter(index, Types.INTEGER, 2);

//THEN
assertEquals(callableStatement.getParameters().get("1"), "?");
assertEquals(callableStatement.getParameters().get(1), "?");
}

@Test
Expand All @@ -100,7 +104,7 @@ public void testRegisterOutParameterWithTypeName() throws SQLException {
callableStatement.registerOutParameter(index, Types.INTEGER, "STRUCT");

//THEN
assertEquals(callableStatement.getParameters().get("1"), "?");
assertEquals(callableStatement.getParameters().get(1), "?");
}

@Test
Expand Down Expand Up @@ -154,7 +158,7 @@ public void testRegisterOutParameterWithSqlType() {
callableStatement.registerOutParameter(1, JDBCType.INTEGER);

//THEN
assertEquals(callableStatement.getParameters().get("0"), "?");
assertEquals(callableStatement.getParameters().get(0), "?");
}

@Test
Expand All @@ -166,7 +170,7 @@ public void testRegisterOutParameterWithSqlTypeAndScale() {
callableStatement.registerOutParameter(1, JDBCType.INTEGER, 2);

//THEN
assertEquals(callableStatement.getParameters().get("0"), "?");
assertEquals(callableStatement.getParameters().get(0), "?");
}

@Test
Expand All @@ -177,7 +181,7 @@ public void testRegisterOutParameterWithSqlTypeAndTypeName() {
callableStatement.registerOutParameter(1, JDBCType.INTEGER, "STRUCT");

//THEN
assertEquals(callableStatement.getParameters().get("0"), "?");
assertEquals(callableStatement.getParameters().get(0), "?");
}

@Test
Expand Down Expand Up @@ -962,7 +966,7 @@ public void testIndexedParametersAreOrderedCorrectly() throws SQLException {
callableStatement.setString(11, "bar");

//THEN
assertEquals(callableStatement.getParameters().values(), expectedParameter);
assertEquals(callableStatement.getParameters().getParametersAsList(), expectedParameter);
}

@Test
Expand All @@ -975,7 +979,7 @@ public void testNamedParametersAreOrderedCorrectly() {
generateCallableStatementWithParameter(parameter1, parameter2);
callableStatement.registerOutParameter(parameter1, Types.VARCHAR);
callableStatement.registerOutParameter(parameter2, Types.VARCHAR);
final String expectedStatement = "CALL myFunction(z-param=>?,a-param=>?) - (foo, bar)";
final String expectedStatement = "CALL myFunction(z-param=>?,a-param=>?) - (foo,bar)";

//WHEN
callableStatement.setString(parameter1,"foo");
Expand Down Expand Up @@ -1014,7 +1018,7 @@ public void testAnonymousNamedParametersAreOrderedCorrectly() {
final JdbcCallableStatement callableStatement = generateCallableStatement();
callableStatement.registerOutParameter(parameter1, Types.VARCHAR);
callableStatement.registerOutParameter(parameter2, Types.VARCHAR);
final String expectedStatement = "CALL myFunction(?,?) - (foo, bar)";
final String expectedStatement = "CALL myFunction(?,?) - (foo,bar)";

//WHEN
callableStatement.setString(parameter1,"foo");
Expand All @@ -1028,7 +1032,7 @@ public void testAnonymousNamedParametersAreOrderedCorrectly() {
public void testToString(){
ToStringVerifier
.forClass(JdbcCallableStatement.class)
.withIgnoredFields("lobUtils")//stateless
.withIgnoredFields("lobUtils", "statementComposer")//stateless
.verify();
}

Expand All @@ -1042,6 +1046,7 @@ public void equalsContract(){
.suppress(Warning.NONFINAL_FIELDS)
.withIgnoredFields("resultSet")
.withIgnoredFields("lobUtils")//stateless
.withIgnoredFields("statementComposer")//stateless
.verify();
}

Expand Down
Loading

0 comments on commit b615aef

Please sign in to comment.