@@ -34,8 +34,11 @@ var fs = require("fs");
3434var async = require ( "async" ) ;
3535var java = require ( 'java' ) ;
3636var Jdbc = require ( 'informix-wrapper' ) ;
37+ var req = require ( 'request' ) ;
3738var helper ;
3839
40+ var javaReadBridge = process . env . JAVA_READ_BRIDGE || "http://localhost:8082/bridge" ;
41+
3942/**
4043 * Regex for sql paramters e.g @param_name@
4144 */
@@ -110,6 +113,67 @@ function parameterizeQuery(query, params, callback) {
110113 } ) ;
111114}
112115
116+ function executePreparedStatement ( api , sql , parameters , connection , next , db ) {
117+ async . waterfall ( [
118+ function ( cb ) {
119+ parameterizeQuery ( sql , parameters , cb ) ;
120+ } ,
121+ function ( parametrizedQuery , cb ) {
122+ sql = parametrizedQuery ;
123+
124+ if ( api . helper . readTransaction ) {
125+ api . log ( "Calling Java Bridge" , "debug" ) ;
126+
127+ api . log ( sql , "debug" ) ;
128+
129+ var body = {
130+ "sql" : new Buffer ( sql ) . toString ( 'base64' ) ,
131+ "db" : db
132+ } ;
133+
134+ api . log ( JSON . stringify ( body ) , "debug" ) ;
135+
136+ req ( { url : javaReadBridge , method : "POST" , body : body , json : true } , function ( error , response , body ) {
137+ if ( error ) {
138+ api . log ( error , "error" ) ;
139+ cb ( error ) ;
140+ }
141+
142+ if ( response . statusCode != 200 ) {
143+ api . log ( response , "error" ) ;
144+ cb ( response . statusMessage ) ;
145+ }
146+
147+ api . log ( "Response:" + JSON . stringify ( body ) , "debug" ) ;
148+ cb ( null , body . results ) ;
149+ } ) ;
150+ } else {
151+ api . log ( "Database connected" , 'debug' ) ;
152+ // the connection might have been closed due to other errors, so this check must be done
153+ if ( connection . isConnected ( ) ) {
154+ // Run the query
155+ connection . query ( sql , cb , {
156+ start : function ( q ) {
157+ api . log ( 'Start to execute ' + q , 'debug' ) ;
158+ } ,
159+ finish : function ( f ) {
160+ api . log ( 'Finish executing ' + f , 'debug' ) ;
161+ }
162+ } ) . execute ( ) ;
163+ } else cb ( "Connection closed unexpectedly" ) ;
164+ }
165+ }
166+ ] , function ( err , result ) {
167+ if ( err ) {
168+ api . log ( "Error occurred: " + err + " " + ( err . stack || '' ) , 'error' ) ;
169+ } else {
170+ api . log ( "Query executed" , "debug" ) ;
171+ }
172+
173+ next ( err , result ) ;
174+ } ) ;
175+ }
176+
113177
114178/**
115179 * Expose the "dataAccess" utility.
@@ -239,9 +303,10 @@ exports.dataAccess = function (api, next) {
239303 return ;
240304 }
241305
242- connection = connectionMap [ queries [ queryName ] . db ] ;
243-
244- error = helper . checkObject ( connection , "connection" ) ;
306+ if ( ! api . helper . readTransaction ) {
307+ connection = connectionMap [ queries [ queryName ] . db ] ;
308+ error = helper . checkObject ( connection , "connection" ) ;
309+ }
245310
246311 if ( error ) {
247312 next ( error ) ;
@@ -254,36 +319,8 @@ exports.dataAccess = function (api, next) {
254319 next ( 'The query for name ' + queryName + ' is not registered' ) ;
255320 return ;
256321 }
257-
258- async . waterfall ( [
259- function ( cb ) {
260- parameterizeQuery ( sql , parameters , cb ) ;
261- } , function ( parametrizedQuery , cb ) {
262- sql = parametrizedQuery ;
263- api . log ( "Database connected" , 'debug' ) ;
264-
265- // the connection might have been closed due to other errors, so this check must be done
266- if ( connection . isConnected ( ) ) {
267- // Run the query
268- connection . query ( sql , cb , {
269- start : function ( q ) {
270- api . log ( 'Start to execute ' + q , 'debug' ) ;
271- } ,
272- finish : function ( f ) {
273- api . log ( 'Finish executing ' + f , 'debug' ) ;
274- }
275- } ) . execute ( ) ;
276- } else cb ( "Connection closed unexpectedly" ) ;
277- }
278- ] , function ( err , result ) {
279- if ( err ) {
280- api . log ( "Error occurred: " + err + " " + ( err . stack || '' ) , 'error' ) ;
281- } else {
282- api . log ( "Query executed" , "debug" ) ;
283- }
284-
285- next ( err , result ) ;
286- } ) ;
322+
323+ executePreparedStatement ( api , sql , parameters , connection , next , queries [ queryName ] . db ) ;
287324 } ,
288325
289326 /**
@@ -316,45 +353,17 @@ exports.dataAccess = function (api, next) {
316353 return ;
317354 }
318355
319- connection = connectionMap [ dbName ] ;
320-
321- error = helper . checkObject ( connection , "connection" ) ;
356+ if ( ! api . helper . readTransaction ) {
357+ connection = connectionMap [ dbName ] ;
358+ error = helper . checkObject ( connection , "connection" ) ;
359+ }
322360
323361 if ( error ) {
324362 next ( error ) ;
325363 return ;
326364 }
327365
328- async . waterfall ( [
329- function ( cb ) {
330- parameterizeQuery ( sql , parameters , cb ) ;
331- } , function ( parametrizedQuery , cb ) {
332- sql = parametrizedQuery ;
333- api . log ( "Database connected" , 'info' ) ;
334-
335- // the connection might have been closed due to other errors, so this check must be done
336- if ( connection . isConnected ( ) ) {
337- // Run the query
338- connection . query ( sql , cb , {
339- start : function ( q ) {
340- api . log ( 'Start to execute ' + q , 'debug' ) ;
341- } ,
342- finish : function ( f ) {
343- api . log ( 'Finish executing ' + f , 'debug' ) ;
344- }
345- } ) . execute ( ) ;
346- } else cb ( "Connection closed unexpectedly" ) ;
347- }
348- ] , function ( err , result ) {
349- if ( err ) {
350- api . log ( "Error occurred: " + err + " " + ( err . stack || '' ) , 'error' ) ;
351- } else {
352- api . log ( "Query executed" , "debug" ) ;
353- }
354-
355- next ( err , result ) ;
356- } ) ;
357-
366+ executePreparedStatement ( api , sql , parameters , connection , next , dbName ) ;
358367 }
359368 } ;
360369 next ( ) ;
0 commit comments