@@ -34,8 +34,11 @@ var fs = require("fs");
34
34
var async = require ( "async" ) ;
35
35
var java = require ( 'java' ) ;
36
36
var Jdbc = require ( 'informix-wrapper' ) ;
37
+ var req = require ( 'request' ) ;
37
38
var helper ;
38
39
40
+ var javaReadBridge = process . env . JAVA_READ_BRIDGE || "http://localhost:8082/bridge" ;
41
+
39
42
/**
40
43
* Regex for sql paramters e.g @param_name@
41
44
*/
@@ -110,6 +113,67 @@ function parameterizeQuery(query, params, callback) {
110
113
} ) ;
111
114
}
112
115
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
+
113
177
114
178
/**
115
179
* Expose the "dataAccess" utility.
@@ -239,9 +303,10 @@ exports.dataAccess = function (api, next) {
239
303
return ;
240
304
}
241
305
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
+ }
245
310
246
311
if ( error ) {
247
312
next ( error ) ;
@@ -254,36 +319,8 @@ exports.dataAccess = function (api, next) {
254
319
next ( 'The query for name ' + queryName + ' is not registered' ) ;
255
320
return ;
256
321
}
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 ) ;
287
324
} ,
288
325
289
326
/**
@@ -316,45 +353,17 @@ exports.dataAccess = function (api, next) {
316
353
return ;
317
354
}
318
355
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
+ }
322
360
323
361
if ( error ) {
324
362
next ( error ) ;
325
363
return ;
326
364
}
327
365
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 ) ;
358
367
}
359
368
} ;
360
369
next ( ) ;
0 commit comments