Skip to content

Commit

Permalink
[CONJ-899] Support UUID Object
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Nov 4, 2022
1 parent a55fa42 commit d31e672
Show file tree
Hide file tree
Showing 9 changed files with 723 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/mariadb/jdbc/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class Configuration {
DriverManager.getLoginTimeout() > 0 ? DriverManager.getLoginTimeout() * 1000 : 30_000;
private String pipe = null;
private String localSocket = null;
private boolean uuidAsString = false;
private boolean tcpKeepAlive = true;
private int tcpKeepIdle = 0;
private int tcpKeepCount = 0;
Expand Down Expand Up @@ -174,6 +175,7 @@ private Configuration(
String pipe,
String localSocket,
boolean tcpKeepAlive,
boolean uuidAsString,
int tcpKeepIdle,
int tcpKeepCount,
int tcpKeepInterval,
Expand Down Expand Up @@ -245,6 +247,7 @@ private Configuration(
this.pipe = pipe;
this.localSocket = localSocket;
this.tcpKeepAlive = tcpKeepAlive;
this.uuidAsString = uuidAsString;
this.tcpKeepIdle = tcpKeepIdle;
this.tcpKeepCount = tcpKeepCount;
this.tcpKeepInterval = tcpKeepInterval;
Expand Down Expand Up @@ -309,6 +312,7 @@ private Configuration(
String pipe,
String localSocket,
Boolean tcpKeepAlive,
Boolean uuidAsString,
Integer tcpKeepIdle,
Integer tcpKeepCount,
Integer tcpKeepInterval,
Expand Down Expand Up @@ -383,6 +387,7 @@ private Configuration(
this.pipe = pipe;
this.localSocket = localSocket;
if (tcpKeepAlive != null) this.tcpKeepAlive = tcpKeepAlive;
if (uuidAsString != null) this.uuidAsString = uuidAsString;
if (tcpKeepIdle != null) this.tcpKeepIdle = tcpKeepIdle;
if (tcpKeepCount != null) this.tcpKeepCount = tcpKeepCount;
if (tcpKeepInterval != null) this.tcpKeepInterval = tcpKeepInterval;
Expand Down Expand Up @@ -748,6 +753,7 @@ public Configuration clone(String username, String password) {
this.pipe,
this.localSocket,
this.tcpKeepAlive,
this.uuidAsString,
this.tcpKeepIdle,
this.tcpKeepCount,
this.tcpKeepInterval,
Expand Down Expand Up @@ -964,6 +970,15 @@ public boolean tcpKeepAlive() {
return tcpKeepAlive;
}

/**
* must uuid fields return as String and not java.util.UUID
*
* @return must UUID return as String and not uuid
*/
public boolean uuidAsString() {
return uuidAsString;
}

/**
* socket tcp keep idle (java 11+ only)
*
Expand Down Expand Up @@ -1672,6 +1687,7 @@ public static final class Builder implements Cloneable {
private String pipe;
private String localSocket;
private Boolean tcpKeepAlive;
private Boolean uuidAsString;
private Integer tcpKeepIdle;
private Integer tcpKeepCount;
private Integer tcpKeepInterval;
Expand Down Expand Up @@ -1933,6 +1949,17 @@ public Builder tcpKeepAlive(Boolean tcpKeepAlive) {
return this;
}

/**
* Indicate if UUID fields must returns as String
*
* @param uuidAsString value
* @return this {@link Builder}
*/
public Builder uuidAsString(Boolean uuidAsString) {
this.uuidAsString = uuidAsString;
return this;
}

/**
* Indicate TCP keep-idle value (for java 11+ only).
*
Expand Down Expand Up @@ -2589,6 +2616,7 @@ public Configuration build() throws SQLException {
this.pipe,
this.localSocket,
this.tcpKeepAlive,
this.uuidAsString,
this.tcpKeepIdle,
this.tcpKeepCount,
this.tcpKeepInterval,
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.sql.Timestamp;
import java.util.Calendar;
import org.mariadb.jdbc.Configuration;
import org.mariadb.jdbc.client.column.UuidColumn;
import org.mariadb.jdbc.client.impl.StandardReadableByteBuf;
import org.mariadb.jdbc.util.constants.ColumnFlags;

Expand Down Expand Up @@ -349,9 +350,11 @@ static ColumnDecoder decode(ReadableByteBuf buf, boolean extendedInfo) {
int flags = buf.readUnsignedShort();
byte decimals = buf.readByte();
DataType.ColumnConstructor constructor =
(flags & ColumnFlags.UNSIGNED) == 0
? dataType.getColumnConstructor()
: dataType.getUnsignedColumnConstructor();
(extTypeName != null && extTypeName.equals("uuid"))
? UuidColumn::new
: (flags & ColumnFlags.UNSIGNED) == 0
? dataType.getColumnConstructor()
: dataType.getUnsignedColumnConstructor();
return constructor.create(
buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat);
}
Expand Down
215 changes: 215 additions & 0 deletions src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab

package org.mariadb.jdbc.client.column;

import java.sql.*;
import java.util.Calendar;
import java.util.UUID;
import org.mariadb.jdbc.Configuration;
import org.mariadb.jdbc.client.ColumnDecoder;
import org.mariadb.jdbc.client.DataType;
import org.mariadb.jdbc.client.ReadableByteBuf;
import org.mariadb.jdbc.message.server.ColumnDefinitionPacket;
import org.mariadb.jdbc.util.CharsetEncodingLength;

/** Column metadata definition */
public class UuidColumn extends ColumnDefinitionPacket implements ColumnDecoder {

/**
* UUID metadata type decoder
*
* @param buf buffer
* @param charset charset
* @param length maximum data length
* @param dataType data type. see https://mariadb.com/kb/en/result-set-packets/#field-types
* @param decimals decimal length
* @param flags flags. see https://mariadb.com/kb/en/result-set-packets/#field-details-flag
* @param stringPos string offset position in buffer
* @param extTypeName extended type name
* @param extTypeFormat extended type format
*/
public UuidColumn(
ReadableByteBuf buf,
int charset,
long length,
DataType dataType,
byte decimals,
int flags,
int[] stringPos,
String extTypeName,
String extTypeFormat) {
super(buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat);
}

public String defaultClassname(Configuration conf) {
return conf.uuidAsString() ? String.class.getName() : UUID.class.getName();
}

public int getColumnType(Configuration conf) {
return conf.uuidAsString() ? Types.CHAR : Types.OTHER;
}

public String getColumnTypeName(Configuration conf) {
return "uuid";
}

public int getPrecision() {
Integer maxWidth = CharsetEncodingLength.maxCharlen.get(charset);
if (maxWidth == null) {
return (int) columnLength;
}
return (int) (columnLength / maxWidth);
}

@Override
public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length)
throws SQLDataException {
return conf.uuidAsString() ? buf.readString(length) : UUID.fromString(buf.readAscii(length));
}

@Override
public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length)
throws SQLDataException {
return conf.uuidAsString() ? buf.readString(length) : UUID.fromString(buf.readAscii(length));
}

@Override
public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Boolean");
}

@Override
public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Boolean");
}

@Override
public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as byte");
}

@Override
public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as byte");
}

@Override
public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal)
throws SQLDataException {
return buf.readString(length);
}

@Override
public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal)
throws SQLDataException {
return buf.readString(length);
}

@Override
public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Short");
}

@Override
public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Short");
}

@Override
public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Integer");
}

@Override
public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Integer");
}

@Override
public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Long");
}

@Override
public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Long");
}

@Override
public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Float");
}

@Override
public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Float");
}

@Override
public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Double");
}

@Override
public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Double");
}

@Override
public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Date");
}

@Override
public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Date");
}

@Override
public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Time");
}

@Override
public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Time");
}

@Override
public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Timestamp");
}

@Override
public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam)
throws SQLDataException {
buf.skip(length);
throw new SQLDataException("Data type UUID cannot be decoded as Timestamp");
}
}

0 comments on commit d31e672

Please sign in to comment.