Skip to content

Commit 2b11f29

Browse files
committed
refactored fromPacket for execute
1 parent aa382af commit 2b11f29

File tree

2 files changed

+69
-62
lines changed

2 files changed

+69
-62
lines changed

lib/commands/server_handshake.js

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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');
98

109
class ServerHandshake extends Command {
1110
constructor(args) {
@@ -70,6 +69,11 @@ class ServerHandshake extends Command {
7069
return ServerHandshake.prototype.dispatchCommands;
7170
}
7271

72+
_isStatement(query, name) {
73+
const firstWord = query.split(' ')[0].toUpperCase();
74+
return firstWord == name;
75+
}
76+
7377
dispatchCommands(packet, connection) {
7478
// command from client to server
7579
let knownCommand = true;
@@ -90,67 +94,7 @@ class ServerHandshake extends Command {
9094
break;
9195
case CommandCode.STMT_EXECUTE:
9296
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-
97+
const { stmtId, flags, iterationCount, values } = Packets.Execute.fromPacket(packet, encoding);
15498
connection.emit('stmt_execute', stmtId, flags, iterationCount, values);
15599
} else {
156100
connection.writeError({

lib/packets/execute.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,69 @@ class Execute {
8181
this.timezone = timezone;
8282
}
8383

84+
static fromPacket(packet, encoding) {
85+
const stmtId = packet.readInt32();
86+
const flags = packet.readInt8();
87+
const iterationCount = packet.readInt32();
88+
89+
let nullBitmaps = 0;
90+
let i = packet.offset;
91+
while (i < packet.end - 1) {
92+
if((packet.buffer[i+1] == Types.VAR_STRING
93+
|| packet.buffer[i+1] == Types.NULL
94+
|| packet.buffer[i+1] == Types.DOUBLE
95+
|| packet.buffer[i+1] == Types.TINY
96+
|| packet.buffer[i+1] == Types.DATETIME
97+
|| packet.buffer[i+1] == Types.JSON) && packet.buffer[i] == 1 && packet.buffer[i+2] == 0) {
98+
break
99+
}
100+
else {
101+
nullBitmaps += packet.readInt8();
102+
}
103+
i++;
104+
}
105+
106+
const types = [];
107+
108+
for(let i = packet.offset + 1; i < packet.end - 1; i++) {
109+
if((packet.buffer[i] == Types.VAR_STRING
110+
|| packet.buffer[i] == Types.NULL
111+
|| packet.buffer[i] == Types.DOUBLE
112+
|| packet.buffer[i] == Types.TINY
113+
|| packet.buffer[i] == Types.DATETIME
114+
|| packet.buffer[i] == Types.JSON) && packet.buffer[i + 1] == 0) {
115+
types.push(packet.buffer[i]);
116+
packet.skip(2);
117+
}
118+
}
119+
120+
packet.skip(1);
121+
122+
const values = [];
123+
for(let i = 0; i < types.length; i++) {
124+
if(types[i] == Types.VAR_STRING) {
125+
values.push(packet.readLengthCodedString(encoding))
126+
}
127+
else if(types[i] == Types.DOUBLE) {
128+
values.push(packet.readDouble())
129+
}
130+
else if(types[i] == Types.TINY) {
131+
values.push(packet.readInt8())
132+
}
133+
else if(types[i] == Types.DATETIME) {
134+
values.push(packet.readDateTime())
135+
}
136+
else if(types[i] == Types.JSON) {
137+
values.push(JSON.parse(packet.readLengthCodedString(encoding)))
138+
}
139+
if(types[i] == Types.NULL) {
140+
values.push(null)
141+
}
142+
}
143+
144+
return { stmtId, flags, iterationCount, values };
145+
}
146+
84147
toPacket() {
85148
// TODO: don't try to calculate packet length in advance, allocate some big buffer in advance (header + 256 bytes?)
86149
// and copy + reallocate if not enough

0 commit comments

Comments
 (0)