|
9 | 9 |
|
10 | 10 | var chr, proto;
|
11 | 11 |
|
12 |
| - exports.Encoder = function () { |
| 12 | + exports.Encoder = function (header) { |
| 13 | + this.header = header || ""; |
13 | 14 | this.data = "";
|
14 | 15 | };
|
15 | 16 |
|
|
30 | 31 | return chr(a) + chr(b) + chr(c) + chr(d);
|
31 | 32 | }
|
32 | 33 |
|
33 |
| - // Add a postgres header to the binary string |
34 |
| - proto.add_header = function (code) { |
35 |
| - if (code === undefined) { |
36 |
| - code = ""; |
37 |
| - } |
38 |
| - this.data = code + encode_int32(this.data.length + 4) + this.data; |
39 |
| - return this; |
| 34 | + // Add a postgres header to the binary string and return it. |
| 35 | + proto.toString = function () { |
| 36 | + return this.header + encode_int32(this.data.length + 4) + this.data; |
40 | 37 | };
|
41 | 38 |
|
42 | 39 | // Encode number as 32 bit 2s compliment
|
|
73 | 70 | this.data += fields.join("\0") + "\0\0";
|
74 | 71 | return this;
|
75 | 72 | };
|
| 73 | + |
| 74 | + proto.push_hash = function (hash) { |
| 75 | + for (key in hash) { |
| 76 | + if (hash.hasOwnProperty(key)) { |
| 77 | + this.data += key + "\0" + hash[key] + "\0"; |
| 78 | + } |
| 79 | + } |
| 80 | + this.data += "\0"; |
| 81 | + return this; |
| 82 | + }; |
76 | 83 |
|
77 | 84 | }());
|
78 | 85 |
|
|
89 | 96 |
|
90 | 97 | proto = exports.Decoder.prototype;
|
91 | 98 |
|
| 99 | + proto.shift_code = function () { |
| 100 | + var code = this.data[0]; |
| 101 | + this.data = this.data.substr(1); |
| 102 | + return code; |
| 103 | + } |
| 104 | + |
92 | 105 | // Convert 4 characters to signed 32 bit integer
|
93 | 106 | proto.shift_int32 = function () {
|
94 | 107 | var unsigned = this.data.charCodeAt(0) * 0x1000000 + this.data.charCodeAt(1) * 0x10000 + this.data.charCodeAt(2) * 0x100 + this.data.charCodeAt(3);
|
|
113 | 126 | // Grab a null terminated string
|
114 | 127 | proto.shift_cstring = function () {
|
115 | 128 | var pos, string;
|
116 |
| - pos = this.indexOf("\0"); |
| 129 | + pos = this.data.indexOf("\0"); |
117 | 130 | string = this.data.substr(0, pos);
|
118 | 131 | this.data = this.data.substr(pos + 1);
|
119 | 132 | return string;
|
|
0 commit comments