Created
February 3, 2011 18:53
-
-
Save alf239/809944 to your computer and use it in GitHub Desktop.
Aggregare function for HSQL to emulate Oracle's WM_CONCAT
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
| @SuppressWarnings("unused") | |
| public static String wmConcat(String in, Boolean flag, | |
| String[] register, Integer[] counter) { | |
| if (flag) { | |
| if (register[0] == null) { | |
| return null; | |
| } | |
| return register[0]; | |
| } | |
| if (in == null) { | |
| return null; | |
| } | |
| if (register[0] == null) { | |
| register[0] = in; | |
| counter[0] = 1; | |
| } else { | |
| register[0] += "," + in; | |
| counter[0]++; | |
| } | |
| return null; | |
| } |
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
| CREATE AGGREGATE FUNCTION wm_concat( | |
| IN val VARCHAR(255), IN flag BOOLEAN, | |
| INOUT register VARCHAR(255), INOUT counter INT) | |
| RETURNS VARCHAR(512) | |
| NO SQL | |
| LANGUAGE JAVA | |
| EXTERNAL NAME 'CLASSPATH:uk.ac.ebi.gxa.dao.AtlasDAOTestCase.wmConcat' |
Author
Author
Please note the stacktraces are not too useful if an exception is thrown by a custom function. Log everything or, better, use a debugger
Author
For further details, study http://hsqldb.org/doc/2.0/guide/sqlroutines-chapt.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HSQLDB 2.0+ supports user-defined aggregate functions. In case you're using Oracle for production and stick to WM_CONCAT for whatever reason, use this function, it'll help you maintaining your test suite one week longer. Anyway, you're almost always better off not using Oracle-specific SQL at all...