Skip to content

Commit

Permalink
Updating commons-dbutils to 1.7.
Browse files Browse the repository at this point in the history
Removing BasicRowProcessor override as it's now correctly using
getColumnLabel like we were.

Updating DatabaseReceiver to check for duplicate columns
(case-insensitive) manually.
  • Loading branch information
narupley committed Jul 9, 2021
1 parent adef964 commit 1bb4e7f
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 227 deletions.
2 changes: 1 addition & 1 deletion donkey/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/commons/commons-dbutils-1.4.jar">
<classpathentry kind="lib" path="lib/commons/commons-dbutils-1.7.jar">
<attributes>
<attribute name="javadoc_location" value="http://commons.apache.org/dbutils/apidocs/"/>
</attributes>
Expand Down
Binary file removed donkey/lib/commons/commons-dbutils-1.4.jar
Binary file not shown.
Binary file added donkey/lib/commons/commons-dbutils-1.7.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion server/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<classpathentry kind="lib" path="lib/commons/commons-cli-1.2.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-configuration2-2.7.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-dbcp2-2.0.1.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-dbutils-1.4.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-dbutils-1.7.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-digester-2.0.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-fileupload-1.2.1.jar"/>
<classpathentry kind="lib" path="lib/commons/commons-jxpath-1.3.jar"/>
Expand Down
Binary file removed server/lib/commons/commons-dbutils-1.4.jar
Binary file not shown.
Binary file added server/lib/commons/commons-dbutils-1.7.jar
Binary file not shown.
73 changes: 68 additions & 5 deletions server/src/com/mirth/connect/connectors/jdbc/DatabaseReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -179,6 +184,8 @@ private void processResultSet(ResultSet resultSet) throws SQLException, Interrup
BasicRowProcessor basicRowProcessor = new BasicRowProcessor();

try {
checkForDuplicateColumns(resultSet);

List<Map<String, Object>> resultsList = null;
if (connectorProperties.isAggregateResults()) {
resultsList = new ArrayList<Map<String, Object>>();
Expand Down Expand Up @@ -213,20 +220,76 @@ private void processResultSet(ResultSet resultSet) throws SQLException, Interrup
}
}

void checkForDuplicateColumns(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
int colCount = metaData.getColumnCount();
Set<String> lowerCaseColumnNames = new HashSet<String>();

for (int i = 1; i <= colCount; i++) {
// This is the same logic used in BasicRowProcessor from commons-dbutils 1.7
String columnName = metaData.getColumnLabel(i);
if (null == columnName || 0 == columnName.length()) {
columnName = metaData.getColumnName(i);
}

if (columnName != null) {
columnName = columnName.toLowerCase(Locale.ENGLISH);
}

if (!lowerCaseColumnNames.add(columnName)) {
/*
* Currently, duplicate keys/aliases would get overwritten in the resultMap, so we
* throw an exception to keep the message from processing since it would contain
* incomplete data and so that the user can have a chance to modify the query and
* select those records again. In the future, we plan to allow duplicate field names
* (MIRTH-3138).
*/
throw new SQLException("Multiple columns have the alias/name '" + columnName + "' (case-insensitive). To prevent this error from occurring, specify unique aliases for each column.");
}
}
}

/**
* For each record in the given list, convert it to XML and dispatch it as a raw message to the
* channel. Then run the post-process if applicable.
*/
private void processResultList(List<Map<String, Object>> resultList) throws InterruptedException, DatabaseReceiverException {
@SuppressWarnings("unchecked")
void processResultList(List<Map<String, Object>> resultList) throws InterruptedException, DatabaseReceiverException {
for (Object object : resultList) {
if (isTerminated()) {
return;
}

if (object instanceof Map) {
Map<String, Object> caseInsensitiveMap = new BasicRowProcessor.CaseInsensitiveHashMap();
caseInsensitiveMap.putAll((Map<String, Object>) object);
processRecord(caseInsensitiveMap);
/*
* Previously, this code was adding to a CaseInsensitiveMap that we overrode in
* commons-dbutils to be public. Instead, we're no longer overriding that class, and
* just checking for case sensitivity here when adding to the map.
*/
Map<String, Object> map = new LinkedHashMap<String, Object>();
Set<String> caseInsensitiveKeys = new HashSet<String>();

for (Entry<String, Object> entry : ((Map<String, Object>) object).entrySet()) {
String lowerCaseKey = entry.getKey();
if (lowerCaseKey != null) {
lowerCaseKey = lowerCaseKey.toLowerCase(Locale.ENGLISH);
}
// Only put into the map if the key (case-insensitive) doesn't already exist
if (caseInsensitiveKeys.add(lowerCaseKey)) {
map.put(entry.getKey(), entry.getValue());
} else {
/*
* Currently, duplicate keys/aliases would get overwritten in the resultMap,
* so we throw an exception to keep the message from processing since it
* would contain incomplete data and so that the user can have a chance to
* modify the query and select those records again. In the future, we plan
* to allow duplicate field names (MIRTH-3138).
*/
throw new DatabaseReceiverException("Multiple columns have the alias/name '" + lowerCaseKey + "' (case-insensitive). To prevent this error from occurring, specify unique aliases for each column.");
}
}

processRecord(map);
} else {
String errorMessage = "Received invalid list entry in channel \"" + ChannelController.getInstance().getDeployedChannelById(getChannelId()).getName() + "\", expected Map<String, Object>: " + object.toString();
logger.error(errorMessage);
Expand All @@ -239,7 +302,7 @@ private void processResultList(List<Map<String, Object>> resultList) throws Inte
* Convert the given resultMap into XML and dispatch it as a raw message to the channel. Then
* run the post-process if applicable.
*/
private void processRecord(Map<String, Object> resultMap) throws InterruptedException, DatabaseReceiverException {
void processRecord(Map<String, Object> resultMap) throws InterruptedException, DatabaseReceiverException {
try {
if (isProcessBatch()) {
BatchRawMessage batchRawMessage = new BatchRawMessage(new BatchMessageReader(resultMapToXml(resultMap)));
Expand Down
219 changes: 0 additions & 219 deletions server/src/org/apache/commons/dbutils/BasicRowProcessor.java

This file was deleted.

Loading

0 comments on commit 1bb4e7f

Please sign in to comment.