Skip to content

Commit a4d0f54

Browse files
authored
Merge pull request #14 from viniciusam/feature/file_mapping
Add custom file mapping support
2 parents b611f2d + 507f675 commit a4d0f54

File tree

7 files changed

+409
-30
lines changed

7 files changed

+409
-30
lines changed

src/main/java/io/github/utplsql/api/CustomTypes.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
public final class CustomTypes {
77

8-
// Object names must be upper case.
8+
/* Object names must be upper case. */
9+
10+
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
11+
912
public static final String UT_REPORTERS = "UT_REPORTERS";
1013
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
1114
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
@@ -14,7 +17,12 @@ public final class CustomTypes {
1417
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
1518
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
1619
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
17-
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
20+
21+
public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
22+
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";
23+
24+
public static final String UT_KEY_VALUE_PAIR = "UT_KEY_VALUE_PAIR";
25+
public static final String UT_KEY_VALUE_PAIRS = "UT_KEY_VALUE_PAIRS";
1826

1927
private CustomTypes() {}
2028

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.github.utplsql.api;
2+
3+
4+
import oracle.jdbc.OracleConnection;
5+
import oracle.jdbc.OracleTypes;
6+
7+
import java.sql.Array;
8+
import java.sql.CallableStatement;
9+
import java.sql.Connection;
10+
import java.sql.SQLException;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public final class FileMapper {
16+
17+
private FileMapper() {}
18+
19+
/**
20+
* Call the database api to build the custom file mappings.
21+
*/
22+
public static Array buildFileMappingArray(
23+
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
24+
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
25+
26+
Map typeMap = conn.getTypeMap();
27+
typeMap.put(CustomTypes.UT_FILE_MAPPING, FileMapping.class);
28+
typeMap.put(CustomTypes.UT_KEY_VALUE_PAIR, KeyValuePair.class);
29+
conn.setTypeMap(typeMap);
30+
31+
CallableStatement callableStatement = conn.prepareCall(
32+
"BEGIN " +
33+
"? := ut_file_mapper.build_file_mappings(" +
34+
"a_object_owner => ?, " +
35+
"a_file_paths => ?, " +
36+
"a_file_to_object_type_mapping => ?, " +
37+
"a_regex_pattern => ?, " +
38+
"a_object_owner_subexpression => ?, " +
39+
"a_object_name_subexpression => ?, " +
40+
"a_object_type_subexpression => ?); " +
41+
"END;");
42+
43+
int paramIdx = 0;
44+
callableStatement.registerOutParameter(++paramIdx, OracleTypes.ARRAY, CustomTypes.UT_FILE_MAPPINGS);
45+
46+
callableStatement.setString(++paramIdx, mapperOptions.getObjectOwner());
47+
callableStatement.setArray(
48+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_VARCHAR2_LIST, filePaths.toArray()));
49+
callableStatement.setArray(
50+
++paramIdx, oraConn.createOracleArray(CustomTypes.UT_KEY_VALUE_PAIRS, mapperOptions.getTypeMappings().toArray()));
51+
callableStatement.setString(++paramIdx, mapperOptions.getRegexPattern());
52+
callableStatement.setInt(++paramIdx, mapperOptions.getOwnerSubExpression());
53+
callableStatement.setInt(++paramIdx, mapperOptions.getNameSubExpression());
54+
callableStatement.setInt(++paramIdx, mapperOptions.getTypeSubExpression());
55+
56+
callableStatement.execute();
57+
return callableStatement.getArray(1);
58+
}
59+
60+
public static List<FileMapping> buildFileMappingList(
61+
Connection conn, List<String> filePaths, FileMapperOptions mapperOptions) throws SQLException {
62+
java.sql.Array fileMappings = buildFileMappingArray(conn, filePaths, mapperOptions);
63+
64+
List<FileMapping> mappingList = new ArrayList<>();
65+
for (Object obj : (Object[]) fileMappings.getArray()) {
66+
mappingList.add((FileMapping) obj);
67+
}
68+
69+
return mappingList;
70+
}
71+
72+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.github.utplsql.api;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FileMapperOptions {
7+
8+
private String objectOwner;
9+
private List<KeyValuePair> typeMappings;
10+
private String regexPattern;
11+
private int ownerSubExpression;
12+
private int typeSubExpression;
13+
private int nameSubExpression;
14+
15+
public FileMapperOptions() {
16+
this.typeMappings = new ArrayList<>();
17+
}
18+
19+
public String getObjectOwner() {
20+
return objectOwner;
21+
}
22+
23+
public void setObjectOwner(String owner) {
24+
this.objectOwner = owner;
25+
}
26+
27+
public List<KeyValuePair> getTypeMappings() {
28+
return typeMappings;
29+
}
30+
31+
public void setTypeMappings(List<KeyValuePair> typeMappings) {
32+
this.typeMappings = typeMappings;
33+
}
34+
35+
public String getRegexPattern() {
36+
return regexPattern;
37+
}
38+
39+
public void setRegexPattern(String regexPattern) {
40+
this.regexPattern = regexPattern;
41+
}
42+
43+
public int getOwnerSubExpression() {
44+
return ownerSubExpression;
45+
}
46+
47+
public void setOwnerSubExpression(int ownerSubExpression) {
48+
this.ownerSubExpression = ownerSubExpression;
49+
}
50+
51+
public int getTypeSubExpression() {
52+
return typeSubExpression;
53+
}
54+
55+
public void setTypeSubExpression(int typeSubExpression) {
56+
this.typeSubExpression = typeSubExpression;
57+
}
58+
59+
public int getNameSubExpression() {
60+
return nameSubExpression;
61+
}
62+
63+
public void setNameSubExpression(int nameSubExpression) {
64+
this.nameSubExpression = nameSubExpression;
65+
}
66+
67+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.utplsql.api;
2+
3+
import java.sql.SQLData;
4+
import java.sql.SQLException;
5+
import java.sql.SQLInput;
6+
import java.sql.SQLOutput;
7+
8+
/**
9+
* Created by Vinicius on 17/07/2017.
10+
*/
11+
public class FileMapping implements SQLData {
12+
13+
private String fileName;
14+
private String objectOwner;
15+
private String objectName;
16+
private String objectType;
17+
18+
public FileMapping() {}
19+
20+
public String getFileName() {
21+
return fileName;
22+
}
23+
24+
public void setFileName(String fileName) {
25+
this.fileName = fileName;
26+
}
27+
28+
public String getObjectOwner() {
29+
return objectOwner;
30+
}
31+
32+
public void setObjectOwner(String objectOwner) {
33+
this.objectOwner = objectOwner;
34+
}
35+
36+
public String getObjectName() {
37+
return objectName;
38+
}
39+
40+
public void setObjectName(String objectName) {
41+
this.objectName = objectName;
42+
}
43+
44+
public String getObjectType() {
45+
return objectType;
46+
}
47+
48+
public void setObjectType(String objectType) {
49+
this.objectType = objectType;
50+
}
51+
52+
@Override
53+
public String getSQLTypeName() throws SQLException {
54+
return CustomTypes.UT_FILE_MAPPING;
55+
}
56+
57+
@Override
58+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
59+
setFileName(stream.readString());
60+
setObjectOwner(stream.readString());
61+
setObjectName(stream.readString());
62+
setObjectType(stream.readString());
63+
}
64+
65+
@Override
66+
public void writeSQL(SQLOutput stream) throws SQLException {
67+
stream.writeString(getFileName());
68+
stream.writeString(getObjectOwner());
69+
stream.writeString(getObjectName());
70+
stream.writeString(getObjectType());
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return String.format("%s/%s.%s", getObjectType(), getObjectOwner(), getObjectName());
76+
}
77+
78+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.github.utplsql.api;
2+
3+
import java.sql.SQLData;
4+
import java.sql.SQLException;
5+
import java.sql.SQLInput;
6+
import java.sql.SQLOutput;
7+
8+
/**
9+
* Created by Vinicius on 22/07/2017.
10+
*/
11+
public class KeyValuePair implements SQLData {
12+
13+
private String key;
14+
private String value;
15+
16+
public KeyValuePair(String key, String value) {
17+
this.key = key;
18+
this.value = value;
19+
}
20+
21+
public String getKey() {
22+
return key;
23+
}
24+
25+
public void setKey(String key) {
26+
this.key = key;
27+
}
28+
29+
public String getValue() {
30+
return value;
31+
}
32+
33+
public void setValue(String value) {
34+
this.value = value;
35+
}
36+
37+
@Override
38+
public String getSQLTypeName() throws SQLException {
39+
return CustomTypes.UT_KEY_VALUE_PAIR;
40+
}
41+
42+
@Override
43+
public void readSQL(SQLInput stream, String typeName) throws SQLException {
44+
setKey(stream.readString());
45+
setValue(stream.readString());
46+
}
47+
48+
@Override
49+
public void writeSQL(SQLOutput stream) throws SQLException {
50+
stream.writeString(getKey());
51+
stream.writeString(getValue());
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return String.format("%s => %s", getKey(), getValue());
57+
}
58+
59+
}

0 commit comments

Comments
 (0)