@@ -22,6 +22,24 @@ var Connection = function(config) {
22
22
23
23
util . inherits ( Connection , EventEmitter ) ;
24
24
25
+ Connection . mapMsgNames = {
26
+ 0x52 : 'authenticationOk' ,
27
+ 0x53 : 'parameterStatus' ,
28
+ 0x4b : 'backendKeyData' ,
29
+ 0x43 : 'commandComplete' ,
30
+ 0x5a : 'readyForQuery' ,
31
+ 0x54 : 'rowDescription' ,
32
+ 0x44 : 'dataRow' ,
33
+ 0x45 : 'error' ,
34
+ 0x4e : 'notice' ,
35
+ 0x31 : 'parseComplete' ,
36
+ 0x32 : 'bindComplete' ,
37
+ 0x41 : 'notification' ,
38
+ 0x6e : 'noData' ,
39
+ 0x49 : 'emptyQuery' ,
40
+ 0x73 : 'portalSuspended'
41
+ } ;
42
+
25
43
var p = Connection . prototype ;
26
44
27
45
p . connect = function ( port , host ) {
@@ -306,72 +324,15 @@ p.parseMessage = function() {
306
324
length : length
307
325
} ;
308
326
309
- switch ( id )
327
+ if ( id in Connection . mapMsgNames )
310
328
{
329
+ msg . name = Connection . mapMsgNames [ id ] ;
330
+ var fncName = "parse" + String . fromCharCode ( id ) ;
311
331
312
- case 0x52 : //R
313
- msg . name = 'authenticationOk' ;
314
- return this . parseR ( msg ) ;
315
-
316
- case 0x53 : //S
317
- msg . name = 'parameterStatus' ;
318
- return this . parseS ( msg ) ;
319
-
320
- case 0x4b : //K
321
- msg . name = 'backendKeyData' ;
322
- return this . parseK ( msg ) ;
323
-
324
- case 0x43 : //C
325
- msg . name = 'commandComplete' ;
326
- return this . parseC ( msg ) ;
327
-
328
- case 0x5a : //Z
329
- msg . name = 'readyForQuery' ;
330
- return this . parseZ ( msg ) ;
331
-
332
- case 0x54 : //T
333
- msg . name = 'rowDescription' ;
334
- return this . parseT ( msg ) ;
335
-
336
- case 0x44 : //D
337
- msg . name = 'dataRow' ;
338
- return this . parseD ( msg ) ;
339
-
340
- case 0x45 : //E
341
- msg . name = 'error' ;
342
- return this . parseE ( msg ) ;
343
-
344
- case 0x4e : //N
345
- msg . name = 'notice' ;
346
- return this . parseN ( msg ) ;
347
-
348
- case 0x31 : //1
349
- msg . name = 'parseComplete' ;
350
- return msg ;
351
-
352
- case 0x32 : //2
353
- msg . name = 'bindComplete' ;
354
- return msg ;
355
-
356
- case 0x41 : //A
357
- msg . name = 'notification' ;
358
- return this . parseA ( msg ) ;
359
-
360
- case 0x6e : //n
361
- msg . name = 'noData' ;
362
- return msg ;
363
-
364
- case 0x49 : //I
365
- msg . name = 'emptyQuery' ;
366
- return msg ;
367
-
368
- case 0x73 : //s
369
- msg . name = 'portalSuspended' ;
370
- return msg ;
371
-
372
- default :
373
- throw new Error ( "Unrecognized message code " + id ) ;
332
+ return fncName in this ? this [ fncName ] ( msg ) ;
374
333
}
334
+ else
335
+ throw new Error ( "Unrecognized message code " + id ) ;
375
336
} ;
376
337
377
338
p . parseR = function ( msg ) {
0 commit comments