Skip to content

Commit aa382af

Browse files
committed
added prepare and execute listeners
1 parent 4125dca commit aa382af

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

lib/commands/server_handshake.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Errors = require('../constants/errors.js');
55

66
const Command = require('./command.js');
77
const Packets = require('../packets/index.js');
8+
const Types = require('../constants/types');
89

910
class ServerHandshake extends Command {
1011
constructor(args) {
@@ -75,6 +76,90 @@ class ServerHandshake extends Command {
7576
const encoding = connection.clientHelloReply.encoding;
7677
const commandCode = packet.readInt8();
7778
switch (commandCode) {
79+
case CommandCode.STMT_PREPARE:
80+
if (connection.listeners('stmt_prepare').length) {
81+
const query = packet.readString(undefined, encoding);
82+
connection.emit('stmt_prepare', query);
83+
} else {
84+
connection.writeError({
85+
code: Errors.HA_ERR_INTERNAL_ERROR,
86+
message:
87+
'No query handler for prepared statements.'
88+
});
89+
}
90+
break;
91+
case CommandCode.STMT_EXECUTE:
92+
if (connection.listeners('stmt_execute').length) {
93+
console.log(packet)
94+
const stmtId = packet.readInt32();
95+
const flags = packet.readInt8();
96+
const iterationCount = packet.readInt32();
97+
98+
let nullBitmaps = 0;
99+
let i = packet.offset;
100+
while (i < packet.end - 1) {
101+
if((packet.buffer[i+1] == Types.VAR_STRING
102+
|| packet.buffer[i+1] == Types.NULL
103+
|| packet.buffer[i+1] == Types.DOUBLE
104+
|| packet.buffer[i+1] == Types.TINY
105+
|| packet.buffer[i+1] == Types.DATETIME
106+
|| packet.buffer[i+1] == Types.JSON) && packet.buffer[i] == 1 && packet.buffer[i+2] == 0) {
107+
break
108+
}
109+
else {
110+
nullBitmaps += packet.readInt8();
111+
}
112+
i++;
113+
}
114+
115+
const types = [];
116+
117+
for(let i = packet.offset + 1; i < packet.end - 1; i++) {
118+
if((packet.buffer[i] == Types.VAR_STRING
119+
|| packet.buffer[i] == Types.NULL
120+
|| packet.buffer[i] == Types.DOUBLE
121+
|| packet.buffer[i] == Types.TINY
122+
|| packet.buffer[i] == Types.DATETIME
123+
|| packet.buffer[i] == Types.JSON) && packet.buffer[i + 1] == 0) {
124+
types.push(packet.buffer[i]);
125+
packet.skip(2);
126+
}
127+
}
128+
129+
packet.skip(1);
130+
131+
const values = [];
132+
for(let i = 0; i < types.length; i++) {
133+
if(types[i] == Types.VAR_STRING) {
134+
values.push(packet.readLengthCodedString(encoding))
135+
}
136+
else if(types[i] == Types.DOUBLE) {
137+
values.push(packet.readDouble())
138+
}
139+
else if(types[i] == Types.TINY) {
140+
values.push(packet.readInt8())
141+
}
142+
else if(types[i] == Types.DATETIME) {
143+
values.push(packet.readDateTime())
144+
}
145+
else if(types[i] == Types.JSON) {
146+
values.push(JSON.parse(packet.readLengthCodedString(encoding)))
147+
}
148+
if(types[i] == Types.NULL) {
149+
values.push(null)
150+
}
151+
}
152+
console.log(values)
153+
154+
connection.emit('stmt_execute', stmtId, flags, iterationCount, values);
155+
} else {
156+
connection.writeError({
157+
code: Errors.HA_ERR_INTERNAL_ERROR,
158+
message:
159+
'No query handler for execute statements.'
160+
});
161+
}
162+
break;
78163
case CommandCode.QUIT:
79164
if (connection.listeners('quit').length) {
80165
connection.emit('quit');

0 commit comments

Comments
 (0)