Skip to content

Commit 2630267

Browse files
committed
Update postgres.js to use the new bits library.
1 parent 9a56cdf commit 2630267

File tree

1 file changed

+51
-72
lines changed

1 file changed

+51
-72
lines changed

postgres.js

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 2 */
22
/*globals include md5 node exports */
33

4-
process.mixin(require('./postgres-js/bits'));
54
process.mixin(require('./postgres-js/md5'));
5+
var bits = require('./postgres-js/bits');
66
var tcp = require("tcp");
77
var sys = require("sys");
88

99
exports.DEBUG = 0;
1010

11-
String.prototype.add_header = function (code) {
12-
if (code === undefined) {
13-
code = "";
14-
}
15-
return code.add_int32(this.length + 4) + this;
16-
};
17-
18-
Object.prototype.map_pairs = function (func) {
19-
var result = [];
20-
for (var k in this) {
21-
if (this.hasOwnProperty(k)) {
22-
result.push(func.call(this, k, this[k]));
23-
}
24-
}
25-
return result;
26-
}
27-
2811
// http://www.postgresql.org/docs/8.3/static/protocol-message-formats.html
2912
var formatter = {
3013
CopyData: function () {
@@ -34,62 +17,65 @@ var formatter = {
3417
// TODO: implement
3518
},
3619
Describe: function (name, type) {
37-
var stream = [type.charCodeAt(0)].add_cstring(name);
38-
return stream.add_header('D');
20+
return (new bits.Encoder('D'))
21+
.push_raw_string(type)
22+
.push_cstring(name);
3923
},
4024
Execute: function (name, max_rows) {
41-
var stream = []
42-
.add_cstring(name)
43-
.add_int32(max_rows);
44-
return stream.add_header('E');
25+
return (new bits.Encoder('E'))
26+
.push_cstring(name)
27+
.push_int32(max_rows);
4528
},
4629
Flush: function () {
47-
return [].add_header('H');
30+
return new bits.Encoder('H');
4831
},
4932
FunctionCall: function () {
5033
// TODO: implement
5134
},
5235
Parse: function (name, query, var_types) {
53-
var stream = []
54-
.add_cstring(name)
55-
.add_cstring(query)
56-
.add_int16(var_types.length);
36+
var builder = (new bits.Encoder('P'))
37+
.push_cstring(name)
38+
.push_cstring(query)
39+
.push_int16(var_types.length);
5740
var_types.each(function (var_type) {
58-
stream.add_int32(var_type);
41+
stream.push_int32(var_type);
5942
});
60-
return stream.add_header('P');
43+
return builder;
6144
},
6245
PasswordMessage: function (password) {
63-
return "".add_cstring(password).add_header('p');
46+
return (new bits.Encoder('p'))
47+
.push_cstring(password);
6448
},
6549
Query: function (query) {
66-
return "".add_cstring(query).add_header('Q');
50+
return (new bits.Encoder('Q'))
51+
.push_cstring(query);
6752
},
6853
SSLRequest: function () {
69-
return "".add_int32(0x4D2162F).add_header();
54+
return (new bits.Encoder())
55+
.push_int32(0x4D2162F);
7056
},
7157
StartupMessage: function (options) {
7258
// Protocol version number 3
73-
return ("".add_int32(0x30000) +
74-
options.map_pairs(function (key, value) {
75-
return "".add_cstring(key).add_cstring(value);
76-
}).join("") + "0").add_header();
59+
return (new bits.Encoder())
60+
.push_int32(0x30000)
61+
.push_hash(options);
7762
},
7863
Sync: function () {
79-
return [].add_header('S');
64+
return new bits.Encoder('S');
8065
},
8166
Terminate: function () {
82-
return [].add_header('X');
67+
return new bits.Encoder('X');
8368
}
8469
};
8570

8671
// Parse response streams from the server
8772
function parse_response(code, stream) {
73+
var input = new bits.Decoder(stream);
8874
var type, args;
8975
args = [];
9076
switch (code) {
9177
case 'R':
92-
switch (stream.parse_int32()[1]) {
78+
switch (stream.shift_int32()) {
9379
case 0:
9480
type = "AuthenticationOk";
9581
break;
@@ -101,11 +87,11 @@ function parse_response(code, stream) {
10187
break;
10288
case 4:
10389
type = "AuthenticationCryptPassword";
104-
args = stream.substr(4).parse(["raw_string", 2])[1];
90+
args = [stream.shift_raw_string(2)];
10591
break;
10692
case 5:
10793
type = "AuthenticationMD5Password";
108-
args = stream.substr(4).parse(["raw_string", 4])[1];
94+
args = [stream.shift_raw_string(4)];
10995
break;
11096
case 6:
11197
type = "AuthenticationSCMCredential";
@@ -122,62 +108,56 @@ function parse_response(code, stream) {
122108
case 'E':
123109
type = "ErrorResponse";
124110
args = [{}];
125-
stream.parse("multi_cstring")[1][0].forEach(function (field) {
111+
stream.shift_multi_cstring().forEach(function (field) {
126112
args[0][field[0]] = field.substr(1);
127113
});
128114
break;
129115
case 'S':
130116
type = "ParameterStatus";
131-
args = stream.parse("cstring", "cstring")[1];
117+
args = [stream.shift_cstring(), stream.shift_cstring()];
132118
break;
133119
case 'K':
134120
type = "BackendKeyData";
135-
args = stream.parse("int32", "int32")[1];
121+
args = [stream.shift_int32(), stream.shift_int32()];
136122
break;
137123
case 'Z':
138124
type = "ReadyForQuery";
139-
args = stream.parse(["raw_string", 1])[1];
125+
args = [stream.shift_raw_string(1)];
140126
break;
141127
case 'T':
142128
type = "RowDescription";
143-
var num_fields = stream.parse_int16()[1];
144-
stream = stream.substr(2);
129+
var num_fields = stream.shift_int16();
145130
var row = [];
146131
for (var i = 0; i < num_fields; i += 1) {
147-
var parts = stream.parse("cstring", "int32", "int16", "int32", "int16", "int32", "int16");
148132
row.push({
149-
field: parts[1][0],
150-
table_id: parts[1][1],
151-
column_id: parts[1][2],
152-
type_id: parts[1][3],
153-
type_size: parts[1][4],
154-
type_modifier: parts[1][5],
155-
format_code: parts[1][6]
133+
field: stream.shift_cstring(),
134+
table_id: stream.shift_int32(),
135+
column_id: stream.shift_int16(),
136+
type_id: stream.shift_int32(),
137+
type_size: stream.shift_int16(),
138+
type_modifier: stream.shift_int32(),
139+
format_code: stream.shift_int16()
156140
});
157-
stream = stream.substr(parts[0]);
158141
}
159142
args = [row];
160143
break;
161144
case 'D':
162145
type = "DataRow";
163146
var data = [];
164-
var num_cols = stream.parse_int16()[1];
165-
stream = stream.substr(2);
147+
var num_cols = stream.shift_int16();
166148
for (i = 0; i < num_cols; i += 1) {
167-
var size = stream.parse_int32()[1];
168-
stream = stream.substr(4);
149+
var size = stream.shift_int32();
169150
if (size === -1) {
170151
data.push(null);
171152
} else {
172-
data.push(stream.parse_raw_string(size)[1]);
173-
stream = stream.substr(size);
153+
data.push(stream.shift_raw_string(size));
174154
}
175155
}
176156
args = [data];
177157
break;
178158
case 'C':
179159
type = "CommandComplete";
180-
args = stream.parse("cstring")[1];
160+
args = [stream.shift_cstring()];
181161
break;
182162
}
183163
if (!type) {
@@ -205,7 +185,7 @@ exports.Connection = function (database, username, password, port) {
205185

206186
// Sends a message to the postgres server
207187
function sendMessage(type, args) {
208-
var stream = formatter[type].apply(this, args);
188+
var stream = (formatter[type].apply(this, args)).toString();
209189
if (exports.DEBUG > 0) {
210190
sys.debug("Sending " + type + ": " + JSON.stringify(args));
211191
if (exports.DEBUG > 2) {
@@ -221,16 +201,15 @@ exports.Connection = function (database, username, password, port) {
221201
sendMessage('StartupMessage', [{user: username, database: database}]);
222202
});
223203
connection.addListener("receive", function (data) {
224-
204+
var input = new bits.Decoder(data);
225205
if (exports.DEBUG > 2) {
226206
sys.debug("<-" + JSON.stringify(data));
227207
}
228208

229-
while (data.length > 0) {
230-
var code = data[0];
231-
var len = data.substr(1, 4).parse_int32()[1];
232-
var stream = data.substr(5, len - 4);
233-
data = data.substr(len + 1);
209+
while (input.data.length > 0) {
210+
var code = input.shift_code();
211+
var len = input.shift_int32();
212+
var stream = new bits.Decoder(input.shift_raw_string(len - 4));
234213
if (exports.DEBUG > 1) {
235214
sys.debug("stream: " + code + " " + JSON.stringify(stream));
236215
}

0 commit comments

Comments
 (0)