Skip to content

Bugfix/coverage key value parameter #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ repositories {
}
}
mavenCentral()
jcenter()
}

dependencies {
Expand All @@ -55,6 +56,9 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
testImplementation("org.hamcrest:hamcrest:2.1")

// Mockito
testCompile("org.mockito:mockito-core:2.+")

// deployer for packagecloud
deployerJars("io.packagecloud.maven.wagon:maven-packagecloud-wagon:0.0.6")
}
Expand Down
110 changes: 0 additions & 110 deletions src/main/java/org/utplsql/api/FileMapper.java

This file was deleted.

91 changes: 91 additions & 0 deletions src/main/java/org/utplsql/api/db/DynamicParameterList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.utplsql.api.db;

import oracle.jdbc.OracleConnection;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

public class DynamicParameterList {

private LinkedHashMap<String, DynamicParameter> params;

interface DynamicParameter {
void setParam( CallableStatement statement, int index ) throws SQLException;
}

static class DynamicStringParameter implements DynamicParameter {
private final String value;

DynamicStringParameter( String value ) {
this.value = value;
}

@Override
public void setParam(CallableStatement statement, int index) throws SQLException {
if ( value == null ) {
statement.setNull(index, Types.VARCHAR);
} else {
statement.setString(index, value);
}
}
}
static class DynamicIntegerParameter implements DynamicParameter {
private final Integer value;

DynamicIntegerParameter( Integer value ) {
this.value = value;
}

@Override
public void setParam(CallableStatement statement, int index) throws SQLException {
if ( value == null ) {
statement.setNull(index, Types.INTEGER);
} else {
statement.setInt(index, value);
}
}
}
static class DynamicArrayParameter implements DynamicParameter {
private final Object[] value;
private final String customTypeName;
private final OracleConnection oraConnection;

DynamicArrayParameter( Object[] value, String customTypeName, OracleConnection oraConnection ) {
this.value = value;
this.customTypeName = customTypeName;
this.oraConnection = oraConnection;
}

@Override
public void setParam(CallableStatement statement, int index) throws SQLException {
if ( value == null ) {
statement.setNull(index, Types.ARRAY, customTypeName);
} else {
statement.setArray(
index, oraConnection.createOracleArray(customTypeName, value)
);
}
}
}

DynamicParameterList(LinkedHashMap<String, DynamicParameter> params) {
this.params = params;
}

public String getSql() {
return params.keySet().stream()
.map(e -> e + " => ?")
.collect(Collectors.joining(", "));
}

public void setParamsStartWithIndex(CallableStatement statement, int startIndex ) throws SQLException {
int index = startIndex;
for ( DynamicParameter param : params.values() ) {
param.setParam(statement, index++);
}
}

}
48 changes: 48 additions & 0 deletions src/main/java/org/utplsql/api/db/DynamicParameterListBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.utplsql.api.db;

import oracle.jdbc.OracleConnection;

import java.util.LinkedHashMap;

public class DynamicParameterListBuilder {

private LinkedHashMap<String, DynamicParameterList.DynamicParameter> params = new LinkedHashMap<>();
private boolean addIfNullOrEmpty = true;

private DynamicParameterListBuilder() {

}

public DynamicParameterListBuilder onlyAddIfNotEmpty() {
addIfNullOrEmpty = false;
return this;
}

public DynamicParameterListBuilder add( String identifier, String value ) {
if ( addIfNullOrEmpty || (value != null && !value.isEmpty()) ) {
params.put(identifier, new DynamicParameterList.DynamicStringParameter(value));
}
return this;
}
public DynamicParameterListBuilder add( String identifier, Integer value ) {
if ( addIfNullOrEmpty || (value != null)) {
params.put(identifier, new DynamicParameterList.DynamicIntegerParameter(value));
}
return this;
}
public DynamicParameterListBuilder add(String identifier, Object[] value, String customTypeName, OracleConnection oraConnection ) {
if ( addIfNullOrEmpty || (value != null && value.length > 0 )) {
params.put(identifier, new DynamicParameterList.DynamicArrayParameter(value, customTypeName, oraConnection));
}
return this;
}

public DynamicParameterList build() {
return new DynamicParameterList(params);
}


public static DynamicParameterListBuilder create() {
return new DynamicParameterListBuilder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import oracle.jdbc.OracleConnection;
import org.utplsql.api.CustomTypes;
import org.utplsql.api.FileMapper;
import org.utplsql.api.FileMapping;
import org.utplsql.api.TestRunnerOptions;

Expand Down
86 changes: 86 additions & 0 deletions src/main/java/org/utplsql/api/testRunner/FileMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.utplsql.api.testRunner;


import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.utplsql.api.CustomTypes;
import org.utplsql.api.FileMapperOptions;
import org.utplsql.api.FileMapping;
import org.utplsql.api.KeyValuePair;
import org.utplsql.api.db.DynamicParameterList;
import org.utplsql.api.db.DynamicParameterListBuilder;

import java.sql.*;
import java.util.*;

final class FileMapper {

private static final Logger logger = LoggerFactory.getLogger(FileMapper.class);

private FileMapper() {
}

/**
* Call the database api to build the custom file mappings.
*/
private static Array buildFileMappingArray(
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
OracleConnection oraConn = conn.unwrap(OracleConnection.class);

Map<String, Class<?>> typeMap = conn.getTypeMap();
typeMap.put(CustomTypes.UT_FILE_MAPPING, FileMapping.class);
typeMap.put(CustomTypes.UT_KEY_VALUE_PAIR, KeyValuePair.class);
conn.setTypeMap(typeMap);

logger.debug("Building fileMappingArray");
final Object[] filePathsArray = mapperOptions.getFilePaths().toArray();
for ( Object elem : filePathsArray ) {
logger.debug("Path: " + elem);
}
Object[] typeMapArray = null;
if ( mapperOptions.getTypeMappings() != null ) {
typeMapArray = mapperOptions.getTypeMappings().toArray();
}

DynamicParameterList parameterList = DynamicParameterListBuilder.create()
.add("a_file_paths", filePathsArray, CustomTypes.UT_VARCHAR2_LIST, oraConn)
.onlyAddIfNotEmpty()
.add("a_object_owner", mapperOptions.getObjectOwner())
.add("a_file_to_object_type_mapping", typeMapArray, CustomTypes.UT_KEY_VALUE_PAIRS, oraConn)
.add("a_regex_pattern", mapperOptions.getRegexPattern())
.add("a_object_owner_subexpression", mapperOptions.getOwnerSubExpression())
.add("a_object_name_subexpression", mapperOptions.getNameSubExpression())
.add("a_object_type_subexpression", mapperOptions.getTypeSubExpression())
.build();

CallableStatement callableStatement = conn.prepareCall(
"BEGIN " +
"? := ut_file_mapper.build_file_mappings(" +
parameterList.getSql() +
"); " +
"END;");

int paramIdx = 0;
callableStatement.registerOutParameter(++paramIdx, OracleTypes.ARRAY, CustomTypes.UT_FILE_MAPPINGS);

parameterList.setParamsStartWithIndex(callableStatement, ++paramIdx);

callableStatement.execute();
return callableStatement.getArray(1);
}

static List<FileMapping> buildFileMappingList(
Connection conn, FileMapperOptions mapperOptions) throws SQLException {
java.sql.Array fileMappings = buildFileMappingArray(conn, mapperOptions);

List<FileMapping> mappingList = new ArrayList<>();
for (Object obj : (Object[]) fileMappings.getArray()) {
mappingList.add((FileMapping) obj);
}

return mappingList;
}

}
Loading