-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathPgEncoder.java
More file actions
467 lines (424 loc) · 16.1 KB
/
PgEncoder.java
File metadata and controls
467 lines (424 loc) · 16.1 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright (C) 2018 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.pgclient.impl.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.socket.SocketChannel;
import io.vertx.sqlclient.Tuple;
import io.vertx.pgclient.impl.util.Util;
import io.vertx.sqlclient.impl.HexSequence;
import io.vertx.sqlclient.impl.ParamDesc;
import io.vertx.sqlclient.impl.RowDesc;
import io.vertx.sqlclient.impl.command.CloseConnectionCommand;
import io.vertx.sqlclient.impl.command.CloseCursorCommand;
import io.vertx.sqlclient.impl.command.CloseStatementCommand;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.ExtendedQueryCommand;
import io.vertx.sqlclient.impl.command.InitCommand;
import io.vertx.sqlclient.impl.command.PrepareStatementCommand;
import io.vertx.sqlclient.impl.command.SimpleQueryCommand;
import java.util.ArrayDeque;
import java.util.Map;
import static io.vertx.pgclient.impl.util.Util.writeCString;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a>
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
final class PgEncoder extends ChannelOutboundHandlerAdapter {
// Frontend message types for {@link io.reactiverse.pgclient.impl.codec.encoder.MessageEncoder}
private static final byte PASSWORD_MESSAGE = 'p';
private static final byte QUERY = 'Q';
private static final byte TERMINATE = 'X';
private static final byte PARSE = 'P';
private static final byte BIND = 'B';
private static final byte DESCRIBE = 'D';
private static final byte EXECUTE = 'E';
private static final byte CLOSE = 'C';
private static final byte SYNC = 'S';
private final ArrayDeque<PgCommandCodec<?, ?>> inflight;
final boolean useLayer7Proxy;
private ChannelHandlerContext ctx;
private ByteBuf out;
private final HexSequence psSeq = new HexSequence(); // used for generating named prepared statement name
boolean closeSent;
PgEncoder(boolean useLayer7Proxy, ArrayDeque<PgCommandCodec<?, ?>> inflight) {
this.useLayer7Proxy = useLayer7Proxy;
this.inflight = inflight;
}
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
if (!closeSent) {
CloseConnectionCommand cmd = CloseConnectionCommand.INSTANCE;
PgCommandCodec<?, ?> codec = wrap(cmd);
codec.encode(this);
}
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
if (out != null) {
ByteBuf buff = this.out;
this.out = null;
buff.release();
}
}
void write(CommandBase<?> cmd) {
PgCommandCodec<?, ?> codec = wrap(cmd);
codec.completionHandler = resp -> {
PgCommandCodec<?, ?> c = inflight.poll();
resp.cmd = (CommandBase) c.cmd;
ctx.fireChannelRead(resp);
};
codec.noticeHandler = ctx::fireChannelRead;
inflight.add(codec);
codec.encode(this);
}
private PgCommandCodec<?, ?> wrap(CommandBase<?> cmd) {
if (cmd instanceof InitCommand) {
return new InitCommandCodec((InitCommand) cmd);
} else if (cmd instanceof SimpleQueryCommand<?>) {
return new SimpleQueryCodec<>((SimpleQueryCommand<?>) cmd);
} else if (cmd instanceof ExtendedQueryCommand<?>) {
return new ExtendedQueryCommandCodec<>((ExtendedQueryCommand<?>) cmd);
} else if (cmd instanceof PrepareStatementCommand) {
return new PrepareStatementCommandCodec((PrepareStatementCommand) cmd);
} else if (cmd instanceof CloseConnectionCommand) {
return CloseConnectionCommandCodec.INSTANCE;
} else if (cmd instanceof CloseCursorCommand) {
return new ClosePortalCommandCodec((CloseCursorCommand) cmd);
} else if (cmd instanceof CloseStatementCommand) {
return new CloseStatementCommandCodec((CloseStatementCommand) cmd);
} else if (cmd instanceof CopyOutCommand) {
return new CopyOutCommandCodec((CopyOutCommand) cmd);
}
throw new AssertionError();
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof CommandBase<?>) {
CommandBase<?> cmd = (CommandBase<?>) msg;
write(cmd);
} else {
super.write(ctx, msg, promise);
}
}
@Override
public void flush(ChannelHandlerContext ctx) {
flush();
}
void close() {
ByteBuf buff;
if (out != null) {
buff = out;
out = null;
} else {
buff = Unpooled.EMPTY_BUFFER;
}
ctx.writeAndFlush(buff).addListener(v -> {
Channel ch = channelHandlerContext().channel();
if (ch instanceof SocketChannel) {
SocketChannel channel = (SocketChannel) ch;
channel.shutdownOutput();
}
});
}
void flush() {
if (out != null) {
ByteBuf buff = out;
out = null;
ctx.writeAndFlush(buff, ctx.voidPromise());
} else {
ctx.flush();
}
}
/**
* This message immediately closes the connection. On receipt of this message,
* the backend closes the connection and terminates.
*/
void writeTerminate() {
ensureBuffer();
out.writeByte(TERMINATE);
out.writeInt(4);
}
/**
* <p>
* The purpose of this message is to provide a resynchronization point for error recovery.
* When an error is detected while processing any extended-query message, the backend issues {@link ErrorResponse},
* then reads and discards messages until this message is reached, then issues {@link ReadyForQuery} and returns to normal
* message processing.
* <p>
* Note that no skipping occurs if an error is detected while processing this message which ensures that there is one
* and only one {@link ReadyForQuery} sent for each of this message.
* <p>
* Note this message does not cause a transaction block opened with BEGIN to be closed. It is possible to detect this
* situation in {@link ReadyForQuery#txStatus()} that includes {@link TxStatus} information.
*/
void writeSync() {
ensureBuffer();
out.writeByte(SYNC);
out.writeInt(4);
}
/**
* <p>
* The message closes an existing prepared statement or portal and releases resources.
* Note that closing a prepared statement implicitly closes any open portals that were constructed from that statement.
* <p>
* The response is either {@link CloseComplete} or {@link ErrorResponse}
*
* @param portal
*/
void writeClosePortal(String portal) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(CLOSE);
out.writeInt(0);
out.writeByte('P'); // 'S' to close a prepared statement or 'P' to close a portal
Util.writeCStringUTF8(out, portal);
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
void writeClosePreparedStatement(byte[] statementName) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(CLOSE);
out.writeInt(0);
out.writeByte('S'); // 'S' to close a prepared statement or 'P' to close a portal
out.writeBytes(statementName);
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
void writeStartupMessage(StartupMessage msg) {
ensureBuffer();
int pos = out.writerIndex();
out.writeInt(0);
// protocol version
out.writeShort(3);
out.writeShort(0);
writeCString(out, StartupMessage.BUFF_USER);
Util.writeCStringUTF8(out, msg.username);
writeCString(out, StartupMessage.BUFF_DATABASE);
Util.writeCStringUTF8(out, msg.database);
for (Map.Entry<String, String> property : msg.properties.entrySet()) {
writeCString(out, property.getKey(), UTF_8);
writeCString(out, property.getValue(), UTF_8);
}
out.writeByte(0);
out.setInt(pos, out.writerIndex() - pos);
}
void writePasswordMessage(PasswordMessage msg) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(PASSWORD_MESSAGE);
out.writeInt(0);
Util.writeCStringUTF8(out, msg.hash);
out.setInt(pos + 1, out.writerIndex() - pos- 1);
}
void writeScramClientInitialMessage(ScramClientInitialMessage msg) {
ensureBuffer();
out.writeByte(PASSWORD_MESSAGE);
int totalLengthPosition = out.writerIndex();
out.writeInt(0); // message length -> will be set later
Util.writeCStringUTF8(out, msg.mechanism);
int msgPosition = out.writerIndex();
out.writeInt(0);
out.writeCharSequence(msg.message, UTF_8);
// rewind to set the message and total length
out.setInt(msgPosition, out.writerIndex() - msgPosition - Integer.BYTES);
out.setInt(totalLengthPosition, out.writerIndex() - totalLengthPosition);
}
void writeScramClientFinalMessage(ScramClientFinalMessage msg) {
ensureBuffer();
out.writeByte(PASSWORD_MESSAGE);
int totalLengthPosition = out.writerIndex();
out.writeInt(0); // message length -> will be set later
out.writeCharSequence(msg.message, UTF_8);
// rewind to set the message length
out.setInt(totalLengthPosition, out.writerIndex() - totalLengthPosition);
}
/**
* <p>
* This message includes an SQL command (or commands) expressed as a text string.
* <p>
* The possible response messages from the backend are
* {@link CommandComplete}, {@link RowDesc}, {@link DataRow}, {@link EmptyQueryResponse}, {@link ErrorResponse},
* {@link ReadyForQuery} and {@link NoticeResponse}
*/
void writeQuery(Query query) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(QUERY);
out.writeInt(0);
Util.writeCStringUTF8(out, query.sql);
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
/**
* <p>
* The message that using "statement" variant specifies the name of an existing prepared statement.
* <p>
* The response is a {@link ParamDesc} message describing the parameters needed by the statement,
* followed by a {@link RowDesc} message describing the rows that will be returned when the statement is eventually
* executed or a {@link NoData} message if the statement will not return rows.
* {@link ErrorResponse} is issued if there is no such prepared statement.
* <p>
* Note that since {@link Bind} has not yet been issued, the formats to be used for returned columns are not yet known to
* the backend; the format code fields in the {@link RowDesc} message will be zeroes in this case.
* <p>
* The message that using "portal" variant specifies the name of an existing portal.
* <p>
* The response is a {@link RowDesc} message describing the rows that will be returned by executing the portal;
* or a {@link NoData} message if the portal does not contain a query that will return rows; or {@link ErrorResponse}
* if there is no such portal.
*/
void writeDescribe(Describe describe) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(DESCRIBE);
out.writeInt(0);
if (describe.statement.length > 1) {
out.writeByte('S');
out.writeBytes(describe.statement);
} else if (describe.portal != null) {
out.writeByte('P');
Util.writeCStringUTF8(out, describe.portal);
} else {
out.writeByte('S');
Util.writeCStringUTF8(out, "");
}
out.setInt(pos + 1, out.writerIndex() - pos- 1);
}
void writeParse(String sql, byte[] statement, DataType[] parameterTypes) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(PARSE);
out.writeInt(0);
out.writeBytes(statement);
Util.writeCStringUTF8(out, sql);
if (parameterTypes == null) {
// Let pg figure out
out.writeShort(0);
} else {
out.writeShort(parameterTypes.length);
for (DataType parameterType : parameterTypes) {
out.writeInt(parameterType.id);
}
}
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
/**
* The message specifies the portal and a maximum row count (zero meaning "fetch all rows") of the result.
* <p>
* The row count of the result is only meaningful for portals containing commands that return row sets;
* in other cases the command is always executed to completion, and the row count of the result is ignored.
* <p>
* The possible responses to this message are the same as {@link Query} message, except that
* it doesn't cause {@link ReadyForQuery} or {@link RowDesc} to be issued.
* <p>
* If Execute terminates before completing the execution of a portal, it will send a {@link PortalSuspended} message;
* the appearance of this message tells the frontend that another Execute should be issued against the same portal to
* complete the operation. The {@link CommandComplete} message indicating completion of the source SQL command
* is not sent until the portal's execution is completed. Therefore, This message is always terminated by
* the appearance of exactly one of these messages: {@link CommandComplete},
* {@link EmptyQueryResponse}, {@link ErrorResponse} or {@link PortalSuspended}.
*
* @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a>
*/
void writeExecute(String portal, int rowCount) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(EXECUTE);
out.writeInt(0);
if (portal != null) {
out.writeCharSequence(portal, UTF_8);
}
out.writeByte(0);
out.writeInt(rowCount); // Zero denotes "no limit" maybe for ReadStream<Row>
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
/**
* <p>
* The message gives the name of the prepared statement, the name of portal,
* and the values to use for any parameter values present in the prepared statement.
* The supplied parameter set must match those needed by the prepared statement.
* <p>
* The response is either {@link BindComplete} or {@link ErrorResponse}.
*/
void writeBind(Bind bind, String portal, Tuple paramValues) {
ensureBuffer();
int pos = out.writerIndex();
out.writeByte(BIND);
out.writeInt(0);
if (portal != null) {
out.writeCharSequence(portal, UTF_8);
}
out.writeByte(0);
out.writeBytes(bind.statement);
int paramLen = paramValues.size();
out.writeShort(paramLen);
// Parameter formats
for (int c = 0;c < paramLen;c++) {
// for now each format is Binary
out.writeShort(bind.paramTypes[c].supportsBinary ? 1 : 0);
}
out.writeShort(paramLen);
for (int c = 0;c < paramLen;c++) {
Object param = paramValues.getValue(c);
if (param == null) {
// NULL value
out.writeInt(-1);
} else {
DataType dataType = bind.paramTypes[c];
if (dataType.supportsBinary) {
int idx = out.writerIndex();
out.writeInt(0);
DataTypeCodec.encodeBinary(dataType, param, out);
out.setInt(idx, out.writerIndex() - idx - 4);
} else {
DataTypeCodec.encodeText(dataType, param, out);
}
}
}
// MAKE resultColumsn non null to avoid null check
// Result columns are all in Binary format
if (bind.resultColumns.length > 0) {
out.writeShort(bind.resultColumns.length);
for (PgColumnDesc resultColumn : bind.resultColumns) {
out.writeShort(resultColumn.dataType.supportsBinary ? 1 : 0);
}
} else {
out.writeShort(1);
out.writeShort(1);
}
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
private void ensureBuffer() {
if (out == null) {
out = ctx.alloc().ioBuffer();
}
}
byte[] nextStatementName() {
return psSeq.next();
}
public ChannelHandlerContext channelHandlerContext() {
return ctx;
}
}