@@ -110,6 +110,44 @@ function parameterizeQuery(query, params, callback) {
110
110
} ) ;
111
111
}
112
112
113
+ function executePreparedStatement ( api , sql , parameters , connection , next ) {
114
+ async . waterfall ( [
115
+ function ( cb ) {
116
+ parameterizeQuery ( sql , parameters , cb ) ;
117
+ } , function ( parametrizedQuery , cb ) {
118
+ sql = parametrizedQuery ;
119
+
120
+ if ( api . helper . readTransaction ) {
121
+ api . log ( "CALLING SANTTOSH'S MAGIC" ) ;
122
+ api . log ( new Buffer ( sql ) . toString ( 'base64' ) ) ;
123
+ cb ( null , [ ] ) ; // necessary?
124
+ } else {
125
+ api . log ( "Database connected" , 'debug' ) ;
126
+ // the connection might have been closed due to other errors, so this check must be done
127
+ if ( connection . isConnected ( ) ) {
128
+ // Run the query
129
+ connection . query ( sql , cb , {
130
+ start : function ( q ) {
131
+ api . log ( 'Start to execute ' + q , 'debug' ) ;
132
+ } ,
133
+ finish : function ( f ) {
134
+ api . log ( 'Finish executing ' + f , 'debug' ) ;
135
+ }
136
+ } ) . execute ( ) ;
137
+ } else cb ( "Connection closed unexpectedly" ) ;
138
+ }
139
+ }
140
+ ] , function ( err , result ) {
141
+ if ( err ) {
142
+ api . log ( "Error occurred: " + err + " " + ( err . stack || '' ) , 'error' ) ;
143
+ } else {
144
+ api . log ( "Query executed" , "debug" ) ;
145
+ }
146
+
147
+ next ( err , result ) ;
148
+ } ) ;
149
+ }
150
+
113
151
114
152
/**
115
153
* Expose the "dataAccess" utility.
@@ -239,9 +277,10 @@ exports.dataAccess = function (api, next) {
239
277
return ;
240
278
}
241
279
242
- connection = connectionMap [ queries [ queryName ] . db ] ;
243
-
244
- error = helper . checkObject ( connection , "connection" ) ;
280
+ if ( ! api . helper . readTransaction ) {
281
+ connection = connectionMap [ queries [ queryName ] . db ] ;
282
+ error = helper . checkObject ( connection , "connection" ) ;
283
+ }
245
284
246
285
if ( error ) {
247
286
next ( error ) ;
@@ -254,36 +293,8 @@ exports.dataAccess = function (api, next) {
254
293
next ( 'The query for name ' + queryName + ' is not registered' ) ;
255
294
return ;
256
295
}
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
- } ) ;
296
+
297
+ executePreparedStatement ( api , sql , parameters , connection , next ) ;
287
298
} ,
288
299
289
300
/**
@@ -316,45 +327,17 @@ exports.dataAccess = function (api, next) {
316
327
return ;
317
328
}
318
329
319
- connection = connectionMap [ dbName ] ;
320
-
321
- error = helper . checkObject ( connection , "connection" ) ;
330
+ if ( ! api . helper . readTransaction ) {
331
+ connection = connectionMap [ dbName ] ;
332
+ error = helper . checkObject ( connection , "connection" ) ;
333
+ }
322
334
323
335
if ( error ) {
324
336
next ( error ) ;
325
337
return ;
326
338
}
327
339
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
-
340
+ executePreparedStatement ( api , sql , parameters , connection , next ) ;
358
341
}
359
342
} ;
360
343
next ( ) ;
0 commit comments