rbeckman-nextgen / test-mc

test-migration
1 stars 0 forks source link

SQL functions not supported in database queries in source connector? #2315

Closed rbeckman-nextgen closed 4 years ago

rbeckman-nextgen commented 4 years ago

I am having issues using functions in a database source connector connected to a database. The channel starts but seems to crashes for every job when using string functions like CONCAT with the following exception:

[2013-01-25 12:44:06,323] ERROR (com.mirth.connect.connectors.jdbc.JdbcMessageReceiver:195): Error in channel: CONCAT issue org.mule.umo.transformer.TransformerException: Failed to parse result map at com.mirth.connect.server.mule.transformers.ResultMapToXML.doTransform(ResultMapToXML.java:93) at org.mule.transformers.AbstractTransformer.transform(AbstractTransformer.java:197) at org.mule.impl.MuleEvent.getTransformedMessage(MuleEvent.java:251) at org.mule.routing.inbound.SelectiveConsumer.isMatch(SelectiveConsumer.java:61) at org.mule.routing.inbound.InboundMessageRouter.route(InboundMessageRouter.java:79) at org.mule.providers.AbstractMessageReceiver$DefaultInternalMessageListener.onMessage(AbstractMessageReceiver.java:487) at org.mule.providers.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:266) at org.mule.providers.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:229) at com.mirth.connect.connectors.jdbc.JdbcMessageReceiver.processMessage(JdbcMessageReceiver.java:180) at org.mule.providers.TransactedPollingMessageReceiver$1.doInTransaction(TransactedPollingMessageReceiver.java:98) at org.mule.transaction.TransactionTemplate.execute(TransactionTemplate.java:72) at org.mule.providers.TransactedPollingMessageReceiver.poll(TransactedPollingMessageReceiver.java:104) at org.mule.providers.PollingMessageReceiver.run(PollingMessageReceiver.java:97) at org.mule.impl.work.WorkerContext.run(WorkerContext.java:290) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061) at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:575) at java.lang.Thread.run(Unknown Source)

Since I cannot publish the query here (as it is rather complex,multitable, multidatabase join) I have reduced it to a small test case like this:

SELECT field1 AS key, CONCAT( field2 field3 ) AS value FROM table LIMIT 2

In my setup this also triggers the error, whereas the same channel with the following query works as expected:

SELECT field1 AS key, field2, field3 AS value FROM table LIMIT 2

Imported Issue. Original Details: Reporter: jonathan.martens Created: 2013-01-25T03:50:42.000-0800

rbeckman-nextgen commented 4 years ago

This has to do with how different JDBC drivers handle their ResultSetMetaData objects. The example you gave doesn't work for MySQL, but works flawlessly for PostgreSQL.

PostgreSQL returns an org.postgresql.jdbc4.Jdbc4ResultSetMetaData object whose field apparently doesn't contain a table name if it's an alias. When converting a ResultSet to a Map, BasicRowProcessor (Apache DBUtils) first checks the Field objects to see if they have the same table name. If they do, then the keys used for the map are the column names themselves, without any "tablename." prefix. If, on the other hand, the table names are different, then the keys used are always "tablename.columnname". In the case of PostgreSQL, when all the selected columns have AS clauses, then the table name returned by rsmd.getTableName() will always be an empty string. Ergo the table names will all be "the same", and the map keys only consist of the column names. In the case of your example, the keys used would be "key" and "value" (as you would expect).

On the other hand, the MySQL Connector/J driver returns a com.mysql.jdbc.ResultSetMetaData object, which handles its fields differently. Whether or not an alias is used for the column name, the catalog and table name is still preserved. That means that when BasicRowProcessor iterates through fields to find whether table names are different, your first field will have a table name of "table" (as expected), but the second field will return an empty string for the table name (because it's coming from a CONCAT call, not directly from a table). Therefore the keys used in this case would be "table.key" and ".value". Notice the second key....

Later on in ResultMapToXML, a Document object is constructed which will eventually be the XML message returned to the channel. Each key in the result map is appended to the "result" node, but when it gets to ".value", it can't create an element with that QName, so an exception is thrown:

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.

That is then wrapped in a "Failed to parse result map" TransformerException, which is what you end up seeing in the server log (the DOMException isn't being included though, so the cause is hidden).

SO, LONG STORY SHORT:

It's not so easy to change how Apache DBUtils works, much less specific database drivers (which are more or less plugins anyway). So one solution is to add some checks in ResultMapToXML.doTransform, basically to check whether each key is a valid QName, and fix them if necessary (e.g. remove leading "." characters). I'll attach a patch to illustrate.

Imported Comment. Original Details: Author: narupley Created: 2013-01-25T09:35:31.000-0800

rbeckman-nextgen commented 4 years ago

Someone appeared to have the same or a similar problem with SQL Server: http://www.mirthcorp.com/community/forums/showthread.php?t=8369

Imported Comment. Original Details: Author: narupley Created: 2013-02-25T09:19:45.000-0800

rbeckman-nextgen commented 4 years ago

Marking as duplicate of MIRTH-1071 since it is the same bug.

Imported Comment. Original Details: Author: brentm Created: 2014-02-05T08:23:01.000-0800