Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
SQL functions not supported in database queries in source connector? #2276
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
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:
In my setup this also triggers the error, whereas the same channel with the following query works as expected:
Imported Issue. Original Details:
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:
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: