Skip to content

Commit 66b569c

Browse files
author
Brian C
committed
Merge pull request brianc#172 from linearray/fixbytea
brianc#161: Fixed bytea decode and added 'hex' for pg >= 9.0.
2 parents 400d410 + 430f107 commit 66b569c

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

lib/textParsers.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,32 @@ var parseInterval = function(val) {
119119
};
120120

121121
var parseByteA = function(val) {
122-
return new Buffer(val.replace(/\\([0-7]{3})/g, function (full_match, code) {
123-
return String.fromCharCode(parseInt(code, 8));
124-
}).replace(/\\\\/g, "\\"), "binary");
122+
if(/^\\x/.test(val)){
123+
// new 'hex' style response (pg >9.0)
124+
return new Buffer(val.substr(2), 'hex');
125+
}else{
126+
out = ""
127+
i = 0
128+
while(i < val.length){
129+
if(val[i] != "\\"){
130+
out += val[i]
131+
++i
132+
}else{
133+
if(val.substr(i+1,3).match(/[0-7]{3}/)){
134+
out += String.fromCharCode(parseInt(val.substr(i+1,3),8))
135+
i += 4
136+
}else{
137+
backslashes = 1
138+
while(i+backslashes < val.length && val[i+backslashes] == "\\")
139+
backslashes++
140+
for(k=0; k<Math.floor(backslashes/2); ++k)
141+
out += "\\"
142+
i += Math.floor(backslashes / 2) * 2
143+
}
144+
}
145+
}
146+
return new Buffer(out,"binary");
147+
}
125148
}
126149

127150
var maxLen = Number.MAX_VALUE.toString().length

0 commit comments

Comments
 (0)