web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #2  
Old 04-17-2012, 10:39 AM
upstart33 upstart33 is offline
Mirth Guru
 
Join Date: Dec 2010
Location: Chicago, IL.
Posts: 459
upstart33 is on a distinguished road
Default

2. Quick SELECT/UPDATE/INSERT functions
Purpose: If most of your channels are connecting to the same database, you can use these functions to cut down some lines of code and for re-usability

Let's say that all of your channels are connecting to the same MySQL database. You could create a SELECT function as such:

Code:
function CDRSelect(sql) {

    var dbConn = DatabaseConnectionFactory.createDatabaseConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/cdrexample","USER","PASSWORD");
    
    var results = dbConn.executeCachedQuery(sql);

    dbConn.close();

    return results;
}
Now, in your Source or in a javascript transformer, you can execute a SELECT query by calling the following:

Code:
var results= CDRSelect("SELECT * FROM results");
....and for an UPDATE query, it would look like the following:

Code:
function CDRUpdate(sql) {

    var dbConn = DatabaseConnectionFactory.createDatabaseConnection("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/cdrexample","USER","PASSWORD");
    var results = dbConn.executeUpdate(sql);
    dbConn.close();

    return results;
}
...which can be referenced by :

Code:
var results= CDRUpdate("UPDATE results set name = 'test'");
Reply With Quote