forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrepareStatementCodec.java
More file actions
169 lines (152 loc) · 6.07 KB
/
PrepareStatementCodec.java
File metadata and controls
169 lines (152 loc) · 6.07 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
/*
* Copyright (C) 2017 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.vertx.mysqlclient.impl.codec;
import io.netty.buffer.ByteBuf;
import io.vertx.mysqlclient.impl.MySQLParamDesc;
import io.vertx.mysqlclient.impl.protocol.ColumnDefinition;
import io.vertx.mysqlclient.impl.protocol.CommandType;
import io.vertx.sqlclient.internal.PreparedStatement;
import io.vertx.sqlclient.internal.command.CommandResponse;
import io.vertx.sqlclient.internal.command.PrepareStatementCommand;
import static io.vertx.mysqlclient.impl.protocol.Packets.ERROR_PACKET_HEADER;
class PrepareStatementCodec extends CommandCodec<PreparedStatement, PrepareStatementCommand> {
private CommandHandlerState commandHandlerState = CommandHandlerState.INIT;
private long statementId;
private int processingIndex;
private ColumnDefinition[] paramDescs;
private ColumnDefinition[] columnDescs;
PrepareStatementCodec(PrepareStatementCommand cmd) {
super(cmd);
}
@Override
void encode(MySQLEncoder encoder) {
super.encode(encoder);
sendStatementPrepareCommand();
}
@Override
void decodePayload(ByteBuf payload, int payloadLength) {
switch (commandHandlerState) {
case INIT:
int firstByte = payload.getUnsignedByte(payload.readerIndex());
if (firstByte == ERROR_PACKET_HEADER) {
handleErrorPacketPayload(payload);
} else {
// handle COM_STMT_PREPARE response
payload.readUnsignedByte(); // 0x00: OK
long statementId = payload.readUnsignedIntLE();
int numberOfColumns = payload.readUnsignedShortLE();
int numberOfParameters = payload.readUnsignedShortLE();
payload.readByte(); // [00] filler
int numberOfWarnings = payload.readShortLE();
// handle metadata here
this.statementId = statementId;
this.paramDescs = new ColumnDefinition[numberOfParameters];
this.columnDescs = new ColumnDefinition[numberOfColumns];
if (numberOfParameters != 0) {
processingIndex = 0;
this.commandHandlerState = CommandHandlerState.HANDLING_PARAM_COLUMN_DEFINITION;
} else if (numberOfColumns != 0) {
processingIndex = 0;
this.commandHandlerState = CommandHandlerState.HANDLING_COLUMN_COLUMN_DEFINITION;
} else {
handleReadyForQuery();
resetIntermediaryResult();
}
}
break;
case HANDLING_PARAM_COLUMN_DEFINITION:
paramDescs[processingIndex++] = decodeColumnDefinitionPacketPayload(payload);
if (processingIndex == paramDescs.length) {
if (isDeprecatingEofFlagEnabled()) {
// we enabled the DEPRECATED_EOF flag and don't need to accept an EOF_Packet
handleParamDefinitionsDecodingCompleted();
} else {
// we need to decode an EOF_Packet before handling rows, to be compatible with MySQL version below 5.7.5
commandHandlerState = CommandHandlerState.PARAM_DEFINITIONS_DECODING_COMPLETED;
}
}
break;
case PARAM_DEFINITIONS_DECODING_COMPLETED:
skipEofPacketIfNeeded(payload);
handleParamDefinitionsDecodingCompleted();
break;
case HANDLING_COLUMN_COLUMN_DEFINITION:
columnDescs[processingIndex++] = decodeColumnDefinitionPacketPayload(payload);
if (processingIndex == columnDescs.length) {
if (isDeprecatingEofFlagEnabled()) {
// we enabled the DEPRECATED_EOF flag and don't need to accept an EOF_Packet
handleColumnDefinitionsDecodingCompleted();
} else {
// we need to decode an EOF_Packet before handling rows, to be compatible with MySQL version below 5.7.5
commandHandlerState = CommandHandlerState.COLUMN_DEFINITIONS_DECODING_COMPLETED;
}
}
break;
case COLUMN_DEFINITIONS_DECODING_COMPLETED:
handleColumnDefinitionsDecodingCompleted();
break;
}
}
private void sendStatementPrepareCommand() {
ByteBuf packet = allocateBuffer();
// encode packet header
int packetStartIdx = packet.writerIndex();
packet.writeMediumLE(0); // will set payload length later by calculation
packet.writeByte(sequenceId);
// encode packet payload
packet.writeByte(CommandType.COM_STMT_PREPARE);
packet.writeCharSequence(cmd.sql(), encoder.encodingCharset);
// set payload length
int payloadLength = packet.writerIndex() - packetStartIdx - 4;
packet.setMediumLE(packetStartIdx, payloadLength);
sendPacket(packet, payloadLength);
}
private void handleReadyForQuery() {
encoder.fireCommandResponse(CommandResponse.success(new MySQLPreparedStatement(
cmd.sql(),
this.statementId,
new MySQLParamDesc(paramDescs),
!cmd.isManaged())));
}
private void resetIntermediaryResult() {
commandHandlerState = CommandHandlerState.INIT;
statementId = 0;
processingIndex = 0;
paramDescs = null;
columnDescs = null;
}
private void handleParamDefinitionsDecodingCompleted() {
if (columnDescs.length == 0) {
handleReadyForQuery();
resetIntermediaryResult();
} else {
processingIndex = 0;
this.commandHandlerState = CommandHandlerState.HANDLING_COLUMN_COLUMN_DEFINITION;
}
}
private void handleColumnDefinitionsDecodingCompleted() {
handleReadyForQuery();
resetIntermediaryResult();
}
private enum CommandHandlerState {
INIT,
HANDLING_PARAM_COLUMN_DEFINITION,
PARAM_DEFINITIONS_DECODING_COMPLETED,
HANDLING_COLUMN_COLUMN_DEFINITION,
COLUMN_DEFINITIONS_DECODING_COMPLETED
}
}