forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowResultDecoder.java
More file actions
90 lines (76 loc) · 2.47 KB
/
RowResultDecoder.java
File metadata and controls
90 lines (76 loc) · 2.47 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
/*
* 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.impl.MSSQLRowImpl;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.impl.RowDecoder;
import io.vertx.sqlclient.internal.RowInternal;
import java.util.stream.Collector;
public class RowResultDecoder<C, R> extends RowDecoder<C, R> {
private static final int FETCH_MISSING = 0x0002;
private final MSSQLRowDesc desc;
public boolean nbc;
public RowResultDecoder(Collector<Row, C, R> collector, MSSQLRowDesc desc) {
super(collector);
this.desc = desc;
}
public MSSQLRowDesc desc() {
return desc;
}
@Override
protected RowInternal row() {
return new MSSQLRowImpl(desc);
}
@Override
protected boolean decodeRow(int len, ByteBuf in, Row row) {
if (nbc) {
return decodeMssqlNbcRow(in, row);
} else {
return decodeMssqlRow(in, row);
}
}
private boolean decodeMssqlRow(ByteBuf in, Row row) {
int len = desc.size();
for (int c = 0; c < len; c++) {
ColumnData columnData = desc.get(c);
row.addValue(columnData.dataType().decodeValue(in, columnData.typeInfo()));
}
return ifNotMissing(in, row);
}
private boolean ifNotMissing(ByteBuf in, Row row) {
if (desc.hasRowStat() && in.readIntLE() == FETCH_MISSING) {
return false;
} else {
return true;
}
}
private boolean decodeMssqlNbcRow(ByteBuf in, Row row) {
int len = desc.size();
int nullBitmapByteCount = (len - (desc.hasRowStat() ? 0 : 1) >> 3) + 1;
int nullBitMapStartIdx = in.readerIndex();
in.skipBytes(nullBitmapByteCount);
for (int c = 0; c < len; c++) {
int bytePos = c >> 3;
int bitPos = c & 7;
byte mask = (byte) (1 << bitPos);
byte nullByte = in.getByte(nullBitMapStartIdx + bytePos);
Object decoded = null;
if ((nullByte & mask) == 0) {
// not null
ColumnData columnData = desc.get(c);
decoded = columnData.dataType().decodeValue(in, columnData.typeInfo());
}
row.addValue(decoded);
}
return ifNotMissing(in, row);
}
}