@@ -6,6 +6,8 @@ var util = require('util');
6
6
var utils = require ( __dirname + '/utils' ) ;
7
7
var Writer = require ( 'buffer-writer' ) ;
8
8
9
+ var TEXT_MODE = 0 ;
10
+ var BINARY_MODE = 1 ;
9
11
var Connection = function ( config ) {
10
12
EventEmitter . call ( this ) ;
11
13
config = config || { } ;
@@ -19,6 +21,7 @@ var Connection = function(config) {
19
21
this . writer = new Writer ( ) ;
20
22
this . ssl = config . ssl || false ;
21
23
this . _ending = false ;
24
+ this . _mode = TEXT_MODE ;
22
25
} ;
23
26
24
27
util . inherits ( Connection , EventEmitter ) ;
@@ -488,8 +491,15 @@ Connection.prototype.parseField = function() {
488
491
dataTypeID : this . parseInt32 ( ) ,
489
492
dataTypeSize : this . parseInt16 ( ) ,
490
493
dataTypeModifier : this . parseInt32 ( ) ,
491
- format : this . parseInt16 ( ) === 0 ? 'text' : 'binary'
494
+ format : undefined
492
495
} ;
496
+ if ( this . parseInt16 ( ) === TEXT_MODE ) {
497
+ this . _mode = TEXT_MODE ;
498
+ field . format = 'text' ;
499
+ } else {
500
+ this . _mode = BINARY_MODE ;
501
+ field . format = 'binary' ;
502
+ }
493
503
return field ;
494
504
} ;
495
505
@@ -500,7 +510,11 @@ Connection.prototype.parseD = function(msg) {
500
510
var length = this . parseInt32 ( ) ;
501
511
var value = null ;
502
512
if ( length !== - 1 ) {
503
- value = this . readBytes ( length ) ;
513
+ if ( this . _mode === TEXT_MODE ) {
514
+ value = this . readString ( length ) ;
515
+ } else {
516
+ value = this . readBytes ( length ) ;
517
+ }
504
518
}
505
519
fields . push ( value ) ;
506
520
}
@@ -569,7 +583,7 @@ Connection.prototype.parseGH = function (msg) {
569
583
} ;
570
584
571
585
Connection . prototype . readChar = function ( ) {
572
- return Buffer ( [ this . buffer [ this . offset ++ ] ] ) . toString ( this . encoding ) ;
586
+ return this . readString ( 1 ) ;
573
587
} ;
574
588
575
589
Connection . prototype . parseInt32 = function ( ) {
@@ -594,7 +608,7 @@ Connection.prototype.readBytes = function(length) {
594
608
595
609
Connection . prototype . parseCString = function ( ) {
596
610
var start = this . offset ;
597
- while ( this . buffer [ this . offset ++ ] ) { }
611
+ while ( this . buffer [ this . offset ++ ] !== 0 ) { }
598
612
return this . buffer . toString ( this . encoding , start , this . offset - 1 ) ;
599
613
} ;
600
614
0 commit comments