-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecuteSharedDBStatement.js
46 lines (38 loc) · 1.56 KB
/
executeSharedDBStatement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Note - I did not author this, I don't know who did. Happy to give credit where due, just message me. */
/**
Executes a SQL statement using a shared database connection stored in the Global Map. If the
connection does not exist, creates it.
@param {String} statement - The SQL statement to execute
@param {Boolean} isQuery - true if the statement is a SELECT, otherwise false
@param {List} paramList - A Java List object containing the parameters for the statement
(optional)
@return {Any}
*/
function executeSharedDBStatement(statement, isQuery, paramList) {
var result;
try {
var dbConn = globalMap.get("dbConn");
if (dbConn == null || dbConn.getConnection().isClosed()) {
dbConn = DatabaseConnectionFactory.createDatabaseConnection(configurationMap.get('DBDriver'), configurationMap.get('DBServerDatabase'), configurationMap.get('DBUserName'), configurationMap.get('DBPassword'));
globalMap.put("dbConn", dbConn);
}
if (paramList == undefined) {
paramList = new Packages.java.util.ArrayList();
}
if (isQuery) {
result = dbConn.executeCachedQuery(statement, paramList);
} else {
result = dbConn.executeUpdate(statement, paramList);
}
} catch (e) {
logger.error(e);
} finally {
return result;
}
}
/* in the mirth Configuration Map:
DBDriver org.postgresql.Driver
DBServerDtabase jdbc:postgresql://<hostname>:5432/<dbname>
DBUserName <the username>
DBPassword <the password>
*/