-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathMSSQLCommandCodec.java
More file actions
224 lines (191 loc) · 6.36 KB
/
Copy pathMSSQLCommandCodec.java
File metadata and controls
224 lines (191 loc) · 6.36 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.mssqlclient.impl.codec;
import io.netty.buffer.ByteBuf;
import io.vertx.mssqlclient.MSSQLException;
import io.vertx.mssqlclient.MSSQLInfo;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.CommandResponse;
import static io.vertx.mssqlclient.impl.codec.EnvChange.*;
import static io.vertx.mssqlclient.impl.codec.TokenType.*;
import static io.vertx.mssqlclient.impl.utils.ByteBufUtils.readUnsignedByteLengthString;
import static io.vertx.mssqlclient.impl.utils.ByteBufUtils.readUnsignedShortLengthString;
abstract class MSSQLCommandCodec<R, C extends CommandBase<R>> {
protected final TdsMessageCodec tdsMessageCodec;
final C cmd;
public MSSQLException failure;
public R result;
MSSQLCommandCodec(TdsMessageCodec tdsMessageCodec, C cmd) {
this.tdsMessageCodec = tdsMessageCodec;
this.cmd = cmd;
}
abstract void encode();
void decode(ByteBuf payload) {
while (payload.isReadable()) {
short tokenType = payload.readUnsignedByte();
switch (tokenType) {
case LOGINACK:
payload.skipBytes(payload.readUnsignedShortLE());
handleLoginAck();
break;
case COLMETADATA:
handleColumnMetadata(payload);
break;
case ROW:
handleRow(payload);
break;
case NBCROW:
handleNbcRow(payload);
break;
case DONEINPROC:
case DONEPROC:
case DONE:
handleDone(tokenType, payload);
break;
case INFO:
handleInfo(payload);
break;
case ORDER:
case TABNAME:
case COLINFO:
payload.skipBytes(payload.readUnsignedShortLE());
break;
case RETURNSTATUS:
payload.skipBytes(4);
break;
case RETURNVALUE:
handleReturnValue(payload);
break;
case ERROR:
handleError(payload);
break;
case ENVCHANGE:
handleEnvChange(payload);
break;
default:
throw new UnsupportedOperationException("Unsupported token: 0x" + Integer.toHexString(tokenType));
}
}
handleDecodingComplete();
}
protected void handleInfo(ByteBuf payload) {
payload.skipBytes(2); // length
MSSQLInfo info = new MSSQLInfo()
.setNumber(payload.readIntLE())
.setState(payload.readByte())
.setSeverity(payload.readByte())
.setMessage(readUnsignedShortLengthString(payload))
.setServerName(readUnsignedByteLengthString(payload))
.setProcedureName(readUnsignedByteLengthString(payload))
.setLineNumber(payload.readIntLE());
tdsMessageCodec.chctx().fireChannelRead(info);
}
protected void handleLoginAck() {
}
private void handleColumnMetadata(ByteBuf payload) {
int columnCount = payload.readUnsignedShortLE();
if (columnCount == 0xFFFF) { // no metadata
return;
}
ColumnData[] columnDatas = new ColumnData[columnCount];
for (int i = 0; i < columnCount; i++) {
payload.skipBytes(6);
DataType dataType = DataType.forId(payload.readUnsignedByte());
TypeInfo metadata = dataType.decodeTypeInfo(payload);
String columnName = readUnsignedByteLengthString(payload);
columnDatas[i] = new ColumnData(columnName, dataType, metadata);
}
handleRowDesc(createRowDesc(columnDatas));
}
protected MSSQLRowDesc createRowDesc(ColumnData[] columnData) {
return MSSQLRowDesc.create(columnData, false);
}
protected void handleRowDesc(MSSQLRowDesc mssqlRowDesc) {
}
protected void handleRow(ByteBuf payload) {
}
protected void handleNbcRow(ByteBuf payload) {
}
private void handleDone(short tokenType, ByteBuf content) {
short status = content.readShortLE();
if ((status & Done.STATUS_DONE_COUNT) != 0) {
content.skipBytes(2);
handleAffectedRows(content.readLongLE());
} else {
content.skipBytes(10);
}
handleDone(tokenType);
}
protected void handleAffectedRows(long count) {
}
protected void handleDone(short tokenType) {
}
protected void handleReturnValue(ByteBuf payload) {
}
private void handleError(ByteBuf buffer) {
buffer.skipBytes(2); // length
int number = buffer.readIntLE();
byte state = buffer.readByte();
byte severity = buffer.readByte();
String message = readUnsignedShortLengthString(buffer);
String serverName = readUnsignedByteLengthString(buffer);
String procedureName = readUnsignedByteLengthString(buffer);
int lineNumber = buffer.readIntLE();
MSSQLException failure = new MSSQLException(number, state, severity, message, serverName, procedureName, lineNumber);
if (this.failure == null) {
this.failure = failure;
} else {
this.failure.add(failure);
}
}
private void handleEnvChange(ByteBuf payload) {
int totalLength = payload.readUnsignedShortLE();
int startPos = payload.readerIndex();
short type = payload.readUnsignedByte();
switch (type) {
case PACKETSIZE:
tdsMessageCodec.encoder().setPacketSize(Integer.parseInt(readUnsignedByteLengthString(payload)));
break;
case XACT_BEGIN:
case DTC_ENLIST:
if (payload.readUnsignedByte() != 8) {
throw new IllegalStateException();
}
tdsMessageCodec.setTransactionDescriptor(payload.readLongLE());
break;
case XACT_COMMIT:
case XACT_ROLLBACK:
case DTC_DEFECT:
tdsMessageCodec.setTransactionDescriptor(0);
break;
case ROUTING:
handleRouting(payload);
break;
default:
break;
}
payload.readerIndex(startPos + totalLength);
}
protected void handleRouting(ByteBuf payload) {
}
protected void handleDecodingComplete() {
complete();
}
void complete() {
CommandResponse<R> resp;
if (failure != null) {
resp = CommandResponse.failure(failure);
} else {
resp = CommandResponse.success(result);
}
tdsMessageCodec.decoder().fireCommandResponse(resp);
}
}