-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ffc8425
We don't want to have this publicly accessible
pesse 952d344
Default TypeMapping is used on NULL and also empty typeMap parameter
pesse 8a326f9
FileMapper doesn't need to be public
pesse d329e8e
Simple DynamicParameterList object to make some PL/SQL calls easier
pesse e6f3a57
Let's do it cleaner and use several DynamicParameter implementations
pesse c799e6a
Add functionality to ignore null or empty adds
pesse c01e8c2
Fix PL/SQL bug - it's not = but =>
pesse 9cd9937
Now let's use that sweet new DynamicParameterListBuilder
pesse db3e1fb
Inlining DynamicParameterListBuilder
pesse e0291ab
Reducing visibilty of some classes and methods, adding comments
pesse d2292cf
Add addIfNotEmpty method, remove allowEmpty-flag
pesse 1bd0f48
Use newer mockito framework and have it as testImplementation
pesse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
src/main/java/org/utplsql/api/db/DynamicParameterList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
pesse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
48
src/main/java/org/utplsql/api/db/DynamicParameterListBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
pesse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new DynamicParameterListBuilder(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.