Skip to content

Commit e87b0a1

Browse files
authored
Merge pull request #665 from aguibert/db-metadata
Initial database metadata API
2 parents 952f753 + c56cb01 commit e87b0a1

35 files changed

+451
-79
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.vertx.db2client.impl;
2+
3+
import java.util.Objects;
4+
5+
import io.vertx.sqlclient.spi.DatabaseMetadata;
6+
7+
public class DB2DatabaseMetadata implements DatabaseMetadata {
8+
9+
private final boolean isZOS;
10+
private final String productName;
11+
private final String fullVersion;
12+
private final int majorVersion;
13+
private final int minorVersion;
14+
15+
public DB2DatabaseMetadata(String serverReleaseLevel) {
16+
Objects.requireNonNull(serverReleaseLevel, "No server release level (SRVRLSLV) returned by server");
17+
fullVersion = serverReleaseLevel;
18+
if (serverReleaseLevel.startsWith("SQL")) {
19+
isZOS = false;
20+
productName = "DB2 for Linux/Unix/Windows";
21+
} else if (serverReleaseLevel.startsWith("DSN")) {
22+
isZOS = true;
23+
productName = "DB2 for z/OS";
24+
} else {
25+
throw new IllegalArgumentException("Received unknown server product release level: " + serverReleaseLevel);
26+
}
27+
if (serverReleaseLevel.length() < 7) {
28+
throw new IllegalArgumentException("Unable to determine server major/minor version from release level: " + serverReleaseLevel);
29+
}
30+
majorVersion = Integer.parseInt(serverReleaseLevel.substring(3, 5));
31+
minorVersion = Integer.parseInt(serverReleaseLevel.substring(5, 7));
32+
}
33+
34+
public boolean isZOS() {
35+
return isZOS;
36+
}
37+
38+
@Override
39+
public String productName() {
40+
return productName;
41+
}
42+
43+
@Override
44+
public String fullVersion() {
45+
return fullVersion;
46+
}
47+
48+
@Override
49+
public int majorVersion() {
50+
return majorVersion;
51+
}
52+
53+
@Override
54+
public int minorVersion() {
55+
return minorVersion;
56+
}
57+
58+
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/DB2SocketConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.vertx.core.net.impl.NetSocketInternal;
2626
import io.vertx.db2client.impl.codec.DB2Codec;
2727
import io.vertx.db2client.impl.command.InitialHandshakeCommand;
28+
import io.vertx.db2client.impl.drda.ConnectionMetaData;
2829
import io.vertx.sqlclient.impl.Connection;
2930
import io.vertx.sqlclient.impl.QueryResultHandler;
3031
import io.vertx.sqlclient.impl.SocketConnectionBase;
@@ -33,11 +34,13 @@
3334
import io.vertx.sqlclient.impl.command.QueryCommandBase;
3435
import io.vertx.sqlclient.impl.command.SimpleQueryCommand;
3536
import io.vertx.sqlclient.impl.command.TxCommand;
37+
import io.vertx.sqlclient.spi.DatabaseMetadata;
3638

3739
public class DB2SocketConnection extends SocketConnectionBase {
3840

3941
private DB2Codec codec;
4042
private Handler<Void> closeHandler;
43+
public final ConnectionMetaData connMetadata = new ConnectionMetaData();
4144

4245
public DB2SocketConnection(NetSocketInternal socket,
4346
boolean cachePreparedStatements,
@@ -90,6 +93,11 @@ public void handleClose(Throwable t) {
9093
super.handleClose(t);
9194
context().runOnContext(closeHandler);
9295
}
96+
97+
@Override
98+
public DatabaseMetadata getDatabaseMetaData() {
99+
return connMetadata.dbMetadata;
100+
}
93101

94102
public DB2SocketConnection closeHandler(Handler<Void> handler) {
95103
closeHandler = handler;

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/CloseConnectionCommandCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ class CloseConnectionCommandCodec extends CommandCodec<Void, CloseConnectionComm
3030
void encode(DB2Encoder encoder) {
3131
super.encode(encoder);
3232
ByteBuf packet = allocateBuffer();
33-
DRDAQueryRequest closeCursor = new DRDAQueryRequest(packet, encoder.connMetadata);
33+
DRDAQueryRequest closeCursor = new DRDAQueryRequest(packet, encoder.socketConnection.connMetadata);
3434
closeCursor.buildRDBCMM();
3535
closeCursor.completeCommand();
3636
sendNonSplitPacket(packet);
3737
}
3838

3939
@Override
4040
void decodePayload(ByteBuf payload, int payloadLength) {
41-
DRDAQueryResponse closeCursor = new DRDAQueryResponse(payload, encoder.connMetadata);
41+
DRDAQueryResponse closeCursor = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
4242
closeCursor.readLocalCommit();
4343
encoder.chctx.channel().close();
4444
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/CloseCursorCommandCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ void encode(DB2Encoder encoder) {
4242
statement.closeQuery(query);
4343

4444
ByteBuf packet = allocateBuffer();
45-
DRDAQueryRequest closeCursor = new DRDAQueryRequest(packet, encoder.connMetadata);
46-
closeCursor.buildCLSQRY(statement.section, encoder.connMetadata.databaseName, query.queryInstanceId);
45+
DRDAQueryRequest closeCursor = new DRDAQueryRequest(packet, encoder.socketConnection.connMetadata);
46+
closeCursor.buildCLSQRY(statement.section, encoder.socketConnection.connMetadata.databaseName, query.queryInstanceId);
4747
closeCursor.completeCommand();
4848
sendNonSplitPacket(packet);
4949
}
5050

5151
@Override
5252
void decodePayload(ByteBuf payload, int payloadLength) {
53-
DRDAQueryResponse closeCursor = new DRDAQueryResponse(payload, encoder.connMetadata);
53+
DRDAQueryResponse closeCursor = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
5454
closeCursor.readCursorClose();
5555
completionHandler.handle(CommandResponse.success(null));
5656
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/DB2Encoder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.vertx.db2client.impl.DB2SocketConnection;
2626
import io.vertx.db2client.impl.command.InitialHandshakeCommand;
2727
import io.vertx.db2client.impl.command.PingCommand;
28-
import io.vertx.db2client.impl.drda.ConnectionMetaData;
2928
import io.vertx.sqlclient.impl.command.CloseConnectionCommand;
3029
import io.vertx.sqlclient.impl.command.CloseCursorCommand;
3130
import io.vertx.sqlclient.impl.command.CloseStatementCommand;
@@ -40,11 +39,10 @@ class DB2Encoder extends ChannelOutboundHandlerAdapter {
4039

4140
private static final Logger LOG = LoggerFactory.getLogger(DB2Encoder.class);
4241

43-
public final ConnectionMetaData connMetadata = new ConnectionMetaData();
4442
private final ArrayDeque<CommandCodec<?, ?>> inflight;
4543
ChannelHandlerContext chctx;
4644

47-
DB2SocketConnection socketConnection;
45+
final DB2SocketConnection socketConnection;
4846

4947
DB2Encoder(ArrayDeque<CommandCodec<?, ?>> inflight, DB2SocketConnection db2SocketConnection) {
5048
this.inflight = inflight;

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/ExtendedBatchQueryCommandCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void encodeUpdate(DRDAQueryRequest req) {
7777

7878
void decodeQuery(ByteBuf payload) {
7979
boolean hasMoreResults = true;
80-
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.connMetadata);
80+
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
8181
for (int i = 0; i < params.size(); i++) {
8282
if (LOG.isDebugEnabled())
8383
LOG.debug("Decode query " + i);
@@ -91,7 +91,7 @@ void decodeQuery(ByteBuf payload) {
9191
}
9292

9393
void decodeUpdate(ByteBuf payload) {
94-
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.connMetadata);
94+
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
9595
for (int i = 0; i < params.size(); i++) {
9696
handleUpdateResult(updateResponse);
9797
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/ExtendedQueryCommandBaseCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ void encodePreparedQuery(DRDAQueryRequest queryRequest, QueryInstance queryInsta
5050

5151
Object[] inputs = sanitize(params);
5252
if (queryInstance.cursor == null) {
53-
queryRequest.writeOpenQuery(statement.section, encoder.connMetadata.databaseName, cmd.fetch(),
53+
queryRequest.writeOpenQuery(statement.section, encoder.socketConnection.connMetadata.databaseName, cmd.fetch(),
5454
ResultSet.TYPE_FORWARD_ONLY, statement.paramDesc.paramDefinitions(), inputs);
5555
} else {
56-
queryRequest.writeFetch(statement.section, encoder.connMetadata.databaseName, cmd.fetch(),
56+
queryRequest.writeFetch(statement.section, encoder.socketConnection.connMetadata.databaseName, cmd.fetch(),
5757
queryInstance.queryInstanceId);
5858
}
5959
}
@@ -62,7 +62,7 @@ void encodePreparedUpdate(DRDAQueryRequest queryRequest, Tuple params) {
6262
Object[] inputs = sanitize(params);
6363
boolean outputExpected = false; // TODO @AGG implement later, is true if result set metadata num columns > 0
6464
boolean chainAutoCommit = true;
65-
queryRequest.writeExecute(statement.section, encoder.connMetadata.databaseName,
65+
queryRequest.writeExecute(statement.section, encoder.socketConnection.connMetadata.databaseName,
6666
statement.paramDesc.paramDefinitions(), inputs, outputExpected, chainAutoCommit);
6767
// TODO: for auto generated keys we also need to flow a writeOpenQuery
6868
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/ExtendedQueryCommandCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ void encodeUpdate(DRDAQueryRequest req) {
4545
}
4646

4747
void decodeQuery(ByteBuf payload) {
48-
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.connMetadata);
48+
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
4949
RowResultDecoder<?, R> decoder = decodePreparedQuery(payload, resp, queryInstance);
5050
boolean hasMoreResults = !decoder.isQueryComplete();
5151
handleQueryResult(decoder);
5252
completionHandler.handle(CommandResponse.success(hasMoreResults));
5353
}
5454

5555
void decodeUpdate(ByteBuf payload) {
56-
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.connMetadata);
56+
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
5757
handleUpdateResult(updateResponse);
5858
if (cmd.autoCommit()) {
5959
updateResponse.readLocalCommit();

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/InitialHandshakeCommandCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static enum ConnectionState {
5050
@Override
5151
void encode(DB2Encoder encoder) {
5252
super.encode(encoder);
53-
encoder.connMetadata.databaseName = cmd.database();
53+
encoder.socketConnection.connMetadata.databaseName = cmd.database();
5454
encoder.socketConnection.closeHandler(h -> {
5555
if (status == ConnectionState.CONNECTING) {
5656
// Sometimes DB2 closes the connection when sending an invalid Database name.
@@ -64,7 +64,7 @@ void encode(DB2Encoder encoder) {
6464

6565
ByteBuf packet = allocateBuffer();
6666
int packetStartIdx = packet.writerIndex();
67-
DRDAConnectRequest connectRequest = new DRDAConnectRequest(packet, encoder.connMetadata);
67+
DRDAConnectRequest connectRequest = new DRDAConnectRequest(packet, encoder.socketConnection.connMetadata);
6868
connectRequest.buildEXCSAT(DRDAConstants.EXTNAM, // externalName,
6969
0x0A, // targetAgent,
7070
DRDAConstants.TARGET_SQL_AM, // targetSqlam,
@@ -91,7 +91,7 @@ void encode(DB2Encoder encoder) {
9191

9292
@Override
9393
void decodePayload(ByteBuf payload, int payloadLength) {
94-
DRDAConnectResponse response = new DRDAConnectResponse(payload, encoder.connMetadata);
94+
DRDAConnectResponse response = new DRDAConnectResponse(payload, encoder.socketConnection.connMetadata);
9595
response.readExchangeServerAttributes();
9696
// readAccessSecurity can throw a DB2Exception if there are problems connecting.
9797
// In that case, we want to catch that exception and

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/PrepareStatementCodec.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ private void sendStatementPrepareCommand() {
5656
ByteBuf packet = allocateBuffer();
5757
// encode packet header
5858
int packetStartIdx = packet.writerIndex();
59-
DRDAQueryRequest prepareCommand = new DRDAQueryRequest(packet, encoder.connMetadata);
60-
section = encoder.connMetadata.sectionManager.getSection(cmd.sql());
61-
String dbName = encoder.connMetadata.databaseName;
59+
DRDAQueryRequest prepareCommand = new DRDAQueryRequest(packet, encoder.socketConnection.connMetadata);
60+
section = encoder.socketConnection.connMetadata.sectionManager.getSection(cmd.sql());
61+
String dbName = encoder.socketConnection.connMetadata.databaseName;
6262
prepareCommand.writePrepareDescribeOutput(cmd.sql(), dbName, section);
6363
prepareCommand.writeDescribeInput(section, dbName);
6464
prepareCommand.completeCommand();
@@ -72,7 +72,7 @@ private void sendStatementPrepareCommand() {
7272
void decodePayload(ByteBuf payload, int payloadLength) {
7373
switch (commandHandlerState) {
7474
case INIT:
75-
DRDAQueryResponse response = new DRDAQueryResponse(payload, encoder.connMetadata);
75+
DRDAQueryResponse response = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
7676
response.readPrepareDescribeInputOutput();
7777
rowDesc = response.getOutputColumnMetaData();
7878
paramDesc = response.getInputColumnMetaData();

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/QueryCommandBaseCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void encode(DB2Encoder encoder) {
4848

4949
ByteBuf packet = allocateBuffer();
5050
int packetStartIdx = packet.writerIndex();
51-
DRDAQueryRequest req = new DRDAQueryRequest(packet, encoder.connMetadata);
51+
DRDAQueryRequest req = new DRDAQueryRequest(packet, encoder.socketConnection.connMetadata);
5252
if (isQuery) {
5353
encodeQuery(req);
5454
} else {

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/SimpleQueryCommandCodec.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ class SimpleQueryCommandCodec<T> extends QueryCommandBaseCodec<T, SimpleQueryCom
3636

3737
@Override
3838
void encodeQuery(DRDAQueryRequest queryCommand) {
39-
querySection = encoder.connMetadata.sectionManager.getSection(cmd.sql());
40-
queryCommand.writePrepareDescribeOutput(cmd.sql(), encoder.connMetadata.databaseName, querySection);
39+
querySection = encoder.socketConnection.connMetadata.sectionManager.getSection(cmd.sql());
40+
queryCommand.writePrepareDescribeOutput(cmd.sql(), encoder.socketConnection.connMetadata.databaseName, querySection);
4141
// fetchSize=0 triggers default fetch size (64) to be used (TODO @AGG this
4242
// should be configurable)
4343
// @AGG hard coded to TYPE_FORWARD_ONLY
44-
queryCommand.writeOpenQuery(querySection, encoder.connMetadata.databaseName, 0, ResultSet.TYPE_FORWARD_ONLY);
44+
queryCommand.writeOpenQuery(querySection, encoder.socketConnection.connMetadata.databaseName, 0, ResultSet.TYPE_FORWARD_ONLY);
4545
}
4646

4747
@Override
4848
void encodeUpdate(DRDAQueryRequest updateCommand) {
49-
querySection = encoder.connMetadata.sectionManager.getSection(cmd.sql());
50-
updateCommand.writeExecuteImmediate(cmd.sql(), querySection, encoder.connMetadata.databaseName);
49+
querySection = encoder.socketConnection.connMetadata.sectionManager.getSection(cmd.sql());
50+
updateCommand.writeExecuteImmediate(cmd.sql(), querySection, encoder.socketConnection.connMetadata.databaseName);
5151
if (cmd.autoCommit()) {
5252
updateCommand.buildRDBCMM();
5353
}
@@ -61,7 +61,7 @@ void encodeUpdate(DRDAQueryRequest updateCommand) {
6161
}
6262

6363
void decodeUpdate(ByteBuf payload) {
64-
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.connMetadata);
64+
DRDAQueryResponse updateResponse = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
6565
querySection.release();
6666

6767
int updatedCount = (int) updateResponse.readExecuteImmediate();
@@ -83,7 +83,7 @@ static <A, T> T emptyResult(Collector<Row, A, T> collector) {
8383
void decodeQuery(ByteBuf payload) {
8484
querySection.release();
8585

86-
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.connMetadata);
86+
DRDAQueryResponse resp = new DRDAQueryResponse(payload, encoder.socketConnection.connMetadata);
8787
resp.readPrepareDescribeOutput();
8888
resp.readBeginOpenQuery();
8989
columnDefinitions = resp.getOutputColumnMetaData();

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/ConnectionMetaData.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@
1717

1818
import java.nio.charset.Charset;
1919

20+
import io.vertx.db2client.impl.DB2DatabaseMetadata;
21+
2022
public class ConnectionMetaData {
2123

2224
public String databaseName;
25+
public DB2DatabaseMetadata dbMetadata;
2326
public final SectionManager sectionManager = new SectionManager();
2427

25-
private boolean isZos;
2628
private Charset currentCCSID = CCSIDConstants.EBCDIC;
2729

28-
public void setZos(boolean isZos) {
29-
this.isZos = isZos;
30-
if (isZos && currentCCSID != CCSIDConstants.UTF8) {
31-
currentCCSID = CCSIDConstants.UTF8;
32-
sectionManager.configureForZOS();
33-
}
30+
public Charset getCCSID() {
31+
return currentCCSID;
3432
}
3533

3634
public boolean isZos() {
37-
return isZos;
38-
}
39-
40-
public Charset getCCSID() {
41-
return currentCCSID;
35+
return dbMetadata.isZOS();
4236
}
4337

4438
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/DRDAConnectRequest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public void buildACCRDB(String rdbnam,
7676
// constructCrrtkn();
7777
// }
7878

79-
if (!metadata.isZos()) {
80-
Objects.requireNonNull(crrtkn);
81-
buildCRRTKN(crrtkn);
82-
}
79+
// if (!metadata.isZos()) {
80+
// Objects.requireNonNull(crrtkn);
81+
// buildCRRTKN(crrtkn);
82+
// }
8383

8484
// This specifies the single-byte, double-byte
8585
// and mixed-byte CCSIDs of the Scalar Data Arrays (SDAs) in the identified
@@ -340,9 +340,9 @@ private void buildTYPDEFOVR(boolean sendCcsidSbc, int ccsidSbc, boolean sendCcsi
340340
writeScalar2Bytes(CodePoint.CCSIDMBC, ccsidMbc);
341341
}
342342

343-
if (metadata.isZos()) {
344-
writeScalar2Bytes(CodePoint.CCSIDXML, ccsidMbc);
345-
}
343+
// if (metadata.isZos()) {
344+
// writeScalar2Bytes(CodePoint.CCSIDXML, ccsidMbc);
345+
// }
346346

347347
updateLengthBytes();
348348

0 commit comments

Comments
 (0)