client.js
5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var reader = require('./serializers').reader;
var writer = require('./serializers').writer;
var cmd = require('./commands');
var net = require('net');
var sys = require('sys');
var queue = require('./containers').queue;
function packetLength(data)
{
var len = data.charCodeAt(0);
len += (data.charCodeAt(1) << 8);
len += (data.charCodeAt(2) << 16);
return len;
}
exports.createTCPClient = function()
{
var args = exports.createTCPClient.arguments,
host = (args.length == 3) ? args['0'] : "localhost",
port = (args.length == 3) ? args['1'] : 3306,
callback = (args.length == 3) ? args['2'] : args['0'],
connection = net.createConnection(port, host)
connection.on("connect", function() {
connection.pscache = {};
connection.setEncoding("binary");
connection.setTimeout(0);
callback(null, new socketClient(connection))
})
connection.on("error", callback)
}
exports.createUNIXClient = function(path)
{
var path = path ? path : "/var/run/mysqld/mysqld.sock";
var connection = net.createConnection(path);
connection.pscache = {};
connection.setEncoding("binary");
connection.setTimeout(0);
return new socketClient(connection);
}
function dump(d)
{
return;
for (var i=0; i < d.length; ++i)
{
sys.puts(i.toString() + " " + d.charAt(i) + " " + d.charCodeAt(i).toString());
}
}
socketClient = function(connection)
{
var client = this;
this.commands = new queue();
this.connection = connection;
connection.buffer = "";
// TODO: fold following code into somthing without copy-paste
this.close = function()
{
return this.add(new cmd.close());
}
this.debug = function( text )
{
return this.add(new cmd.debug(text));
}
this.auth = function(dbname, user, password)
{
return this.add(new cmd.auth(dbname, user, password));
}
this.query = function(q)
{
return this.add(new cmd.query(q));
}
this.prepare = function(q)
{
return this.add(new cmd.prepare(q));
}
// TODO: too many copy-paste, cleanup
this.execute = function(q, parameters)
{
if (!this.pscache)
this.pscache = {};
if (this.auto_prepare == true)
{
var cached = this.connection.pscache[q];
if (!cached) {
var prepare_cmd = this.add(new cmd.prepare(q));
var execute_cmd = this.add(new cmd.execute(q, parameters));
prepare_cmd.addListener('prepared', function(ps) { execute_cmd.ps = ps; });
prepare_cmd.addListener('error', function(err)
{
execute_cmd.emit('error', err);
execute_cmd.prepare_failed = true;
});
return execute_cmd;
} else {
var execute_cmd = this.add(new cmd.execute(q, parameters));
execute_cmd.ps = cached;
return execute_cmd;
}
}
return this.add(new cmd.execute(q, parameters));
}
this.terminate = function()
{
this.connection.end();
}
this.write_packet = function(packet, pnum)
{
packet.addHeader(pnum);
this.connection.write(packet.data, 'binary');
}
this.dispatch_packet = function(packet)
{
if (this.commands.empty())
return;
if (this.commands.top().process_packet(packet))
{
this.commands.shift();
this.connection.emit('queue', this.commands.length);
this.dispatch_packet();
}
}
// proxy request to socket eventemitter
this.on = this.addListener = function()
{
this.connection.addListener.apply(this.connection, arguments);
}
this.add = function(c)
{
c.connection = this;
var need_start_queue = this.connection.connected && this.commands.empty();
this.commands.push(c);
this.connection.emit('queue', this.commands.length);
if (need_start_queue)
this.dispatch_packet();
var connection = this.connection;
//c.addListener('end', function(cmd) { connection.emit('command_end', c); });
//c.addListener('error', function(e) { sys.puts(e.message); }); // TODO: throw exception
return c;
}
this.connection.addListener("data",
function(data)
{
// TODO: move to 'onconnect' event
// replace connected with 'first packet' or 'ready state' or smth similar
if (!this.connected)
{
this.connected = true;
client.dispatch_packet();
}
this.buffer += data;
var len = packetLength(this.buffer);
while (this.buffer.length >= len + 4)
{
var packet = this.buffer.substr(4,len);
client.dispatch_packet(new reader(packet) );
this.buffer = this.buffer.substr(len+4, this.buffer.length-len-4);
len = packetLength(this.buffer);
}
}
);
return client;
}