Skip to content

Add custom file mapping support #14

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 3 commits into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/main/java/io/github/utplsql/api/CustomTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
public final class CustomTypes {

// Object names must be upper case.
/* Object names must be upper case. */

public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";

public static final String UT_REPORTERS = "UT_REPORTERS";
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
Expand All @@ -14,7 +17,12 @@ public final class CustomTypes {
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";

public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";

public static final String UT_KEY_VALUE_PAIR = "UT_KEY_VALUE_PAIR";
public static final String UT_KEY_VALUE_PAIRS = "UT_KEY_VALUE_PAIRS";

private CustomTypes() {}

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


import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleTypes;

import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class FileMapper {

private FileMapper() {}

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

Map typeMap = conn.getTypeMap();
typeMap.put(CustomTypes.UT_FILE_MAPPING, FileMapping.class);
typeMap.put(CustomTypes.UT_KEY_VALUE_PAIR, KeyValuePair.class);
conn.setTypeMap(typeMap);

CallableStatement callableStatement = conn.prepareCall(
"BEGIN " +
"? := ut_file_mapper.build_file_mappings(" +
"a_object_owner => ?, " +
"a_file_paths => ?, " +
"a_file_to_object_type_mapping => ?, " +
"a_regex_pattern => ?, " +
"a_object_owner_subexpression => ?, " +
"a_object_name_subexpression => ?, " +
"a_object_type_subexpression => ?); " +
"END;");

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

callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, filePaths.toArray()));
callableStatement.setArray(
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());

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

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

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

return mappingList;
}

}
67 changes: 67 additions & 0 deletions src/main/java/io/github/utplsql/api/FileMapperOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.utplsql.api;

import java.util.ArrayList;
import java.util.List;

public class FileMapperOptions {

private String objectOwner;
private List<KeyValuePair> typeMappings;
private String regexPattern;
private int ownerSubExpression;
private int typeSubExpression;
private int nameSubExpression;

public FileMapperOptions() {
this.typeMappings = new ArrayList<>();
}

public String getObjectOwner() {
return objectOwner;
}

public void setObjectOwner(String owner) {
this.objectOwner = owner;
}

public List<KeyValuePair> getTypeMappings() {
return typeMappings;
}

public void setTypeMappings(List<KeyValuePair> typeMappings) {
this.typeMappings = typeMappings;
}

public String getRegexPattern() {
return regexPattern;
}

public void setRegexPattern(String regexPattern) {
this.regexPattern = regexPattern;
}

public int getOwnerSubExpression() {
return ownerSubExpression;
}

public void setOwnerSubExpression(int ownerSubExpression) {
this.ownerSubExpression = ownerSubExpression;
}

public int getTypeSubExpression() {
return typeSubExpression;
}

public void setTypeSubExpression(int typeSubExpression) {
this.typeSubExpression = typeSubExpression;
}

public int getNameSubExpression() {
return nameSubExpression;
}

public void setNameSubExpression(int nameSubExpression) {
this.nameSubExpression = nameSubExpression;
}

}
78 changes: 78 additions & 0 deletions src/main/java/io/github/utplsql/api/FileMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.github.utplsql.api;

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

/**
* Created by Vinicius on 17/07/2017.
*/
public class FileMapping implements SQLData {

private String fileName;
private String objectOwner;
private String objectName;
private String objectType;

public FileMapping() {}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getObjectOwner() {
return objectOwner;
}

public void setObjectOwner(String objectOwner) {
this.objectOwner = objectOwner;
}

public String getObjectName() {
return objectName;
}

public void setObjectName(String objectName) {
this.objectName = objectName;
}

public String getObjectType() {
return objectType;
}

public void setObjectType(String objectType) {
this.objectType = objectType;
}

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_FILE_MAPPING;
}

@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
setFileName(stream.readString());
setObjectOwner(stream.readString());
setObjectName(stream.readString());
setObjectType(stream.readString());
}

@Override
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getFileName());
stream.writeString(getObjectOwner());
stream.writeString(getObjectName());
stream.writeString(getObjectType());
}

@Override
public String toString() {
return String.format("%s/%s.%s", getObjectType(), getObjectOwner(), getObjectName());
}

}
59 changes: 59 additions & 0 deletions src/main/java/io/github/utplsql/api/KeyValuePair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.utplsql.api;

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

/**
* Created by Vinicius on 22/07/2017.
*/
public class KeyValuePair implements SQLData {

private String key;
private String value;

public KeyValuePair(String key, String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public String getSQLTypeName() throws SQLException {
return CustomTypes.UT_KEY_VALUE_PAIR;
}

@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
setKey(stream.readString());
setValue(stream.readString());
}

@Override
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getKey());
stream.writeString(getValue());
}

@Override
public String toString() {
return String.format("%s => %s", getKey(), getValue());
}

}
Loading