Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.vertx.db2client.impl.codec;

import io.vertx.core.VertxException;
import io.vertx.core.buffer.Buffer;
import io.vertx.db2client.impl.drda.ClientTypes;
import io.vertx.db2client.impl.drda.ColumnMetaData;
Expand All @@ -35,19 +36,19 @@ ColumnMetaData paramDefinitions() {
return paramDefinitions;
}

public String prepare(TupleInternal values) {
public TupleInternal prepare(TupleInternal values) {
if (values.size() != paramDefinitions.columns_) {
return ErrorMessageFactory.buildWhenArgumentsLengthNotMatched(paramDefinitions.columns_, values.size());
throw new VertxException(ErrorMessageFactory.buildWhenArgumentsLengthNotMatched(paramDefinitions.columns_, values.size()), true);
}
for (int i = 0; i < paramDefinitions.columns_; i++) {
Object val = values.getValue(i);
int type = paramDefinitions.types_[i];
if (!canConvert(val, type)) {
Class<?> preferredType = ClientTypes.preferredJavaType(type);
return ErrorMessageFactory.buildWhenArgumentsTypeNotMatched(preferredType, i, val);
throw new VertxException(ErrorMessageFactory.buildWhenArgumentsTypeNotMatched(preferredType, i, val), true);
}
}
return null;
return values;
}

private static boolean canConvert(Object val, int type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String sql() {
}

@Override
public String prepare(TupleInternal values) {
public TupleInternal prepare(TupleInternal values) {
return paramDesc.prepare(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,4 @@ public String sql() {
return sql;
}

@Override
public String prepare(TupleInternal values) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.vertx.mysqlclient.impl.codec;

import io.vertx.core.VertxException;
import io.vertx.mysqlclient.impl.MySQLParamDesc;
import io.vertx.mysqlclient.impl.MySQLRowDesc;
import io.vertx.mysqlclient.impl.datatype.DataType;
Expand Down Expand Up @@ -71,13 +72,13 @@ public String sql() {
}

@Override
public String prepare(TupleInternal values) {
public TupleInternal prepare(TupleInternal values) {
int numberOfParameters = values.size();
int paramDescLength = paramDesc.paramDefinitions().length;
if (numberOfParameters != paramDescLength) {
return ErrorMessageFactory.buildWhenArgumentsLengthNotMatched(paramDescLength, numberOfParameters);
throw new VertxException(ErrorMessageFactory.buildWhenArgumentsLengthNotMatched(paramDescLength, numberOfParameters), true);
} else {
return null;
return values;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,4 @@ public RowDesc rowDesc() {
public String sql() {
return sql;
}

@Override
public String prepare(TupleInternal values) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @author <a href="mailto:[email protected]">Emad Alblueshi</a>
*/
final class Bind {
final class Bind extends OutboundMessage {

final byte[] statement;
final DataType[] paramTypes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.vertx.pgclient.impl.codec;

class ClosePortalMessage extends OutboundMessage {

static final ClosePortalMessage INSTANCE = new ClosePortalMessage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.vertx.pgclient.impl.codec;

class ClosePreparedStatementMessage extends OutboundMessage {

static final ClosePreparedStatementMessage INSTANCE = new ClosePreparedStatementMessage();

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ static void encodeText(DataType id, Object value, ByteBuf buff) {
private static void textEncode(DataType id, Object value, ByteBuf buff) {
switch (id) {
case NUMERIC:
textEncodeNUMERIC((Number) value, buff);
textEncodeNUMERIC((String) value, buff);
break;
case NUMERIC_ARRAY:
textEncodeNUMERIC_ARRAY((Number[]) value, buff);
textEncodeNUMERIC_ARRAY((Object[]) value, buff);
break;
case UNKNOWN:
//default to treating unknown as a string
buff.writeCharSequence(String.valueOf(value), StandardCharsets.UTF_8);
buff.writeCharSequence((CharSequence) value, StandardCharsets.UTF_8);
break;
default:
logger.debug("Data type " + id + " does not support text encoding");
Expand Down Expand Up @@ -281,13 +281,13 @@ public static void encodeBinary(DataType id, Object value, ByteBuf buff) {
binaryEncodeArray((UUID[]) value, DataType.UUID, buff);
break;
case JSON:
binaryEncodeJSON((Object) value, buff);
binaryEncodeJSON((CharSequence) value, buff);
break;
case JSON_ARRAY:
binaryEncodeArray((Object[]) value, DataType.JSON, buff);
break;
case JSONB:
binaryEncodeJSONB((Object) value, buff);
binaryEncodeJSONB((CharSequence) value, buff);
break;
case JSONB_ARRAY:
binaryEncodeArray((Object[]) value, DataType.JSONB, buff);
Expand Down Expand Up @@ -938,12 +938,11 @@ private static Interval textDecodeINTERVAL(int index, int len, ByteBuf buff) {
return new Interval(years, months, days, hours, minutes, seconds, microseconds);
}

private static void textEncodeNUMERIC(Number value, ByteBuf buff) {
String s = value.toString();
buff.writeCharSequence(s, StandardCharsets.UTF_8);
private static void textEncodeNUMERIC(String value, ByteBuf buff) {
buff.writeCharSequence(value, StandardCharsets.UTF_8);
}

private static void textEncodeNUMERIC_ARRAY(Number[] value, ByteBuf buff) {
private static void textEncodeNUMERIC_ARRAY(Object[] value, ByteBuf buff) {
textEncodeArray(value, DataType.NUMERIC, buff);
}

Expand Down Expand Up @@ -1366,14 +1365,8 @@ private static Object binaryDecodeJSON(int index, int len, ByteBuf buff) {
return textDecodeJSONB(index, len, buff);
}

private static void binaryEncodeJSON(Object value, ByteBuf buff) {
String s;
if (value == Tuple.JSON_NULL) {
s = "null";
} else {
s = Json.encode(value);
}
buff.writeCharSequence(s, StandardCharsets.UTF_8);
private static void binaryEncodeJSON(CharSequence value, ByteBuf buff) {
buff.writeCharSequence(value, StandardCharsets.UTF_8);
}

private static Object textDecodeJSONB(int index, int len, ByteBuf buff) {
Expand Down Expand Up @@ -1410,7 +1403,7 @@ private static Object binaryDecodeJSONB(int index, int len, ByteBuf buff) {
return textDecodeJSONB(index + 1, len - 1, buff);
}

private static void binaryEncodeJSONB(Object value, ByteBuf buff) {
private static void binaryEncodeJSONB(CharSequence value, ByteBuf buff) {
buff.writeByte(1); // version
binaryEncodeJSON(value, buff);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package io.vertx.pgclient.impl.codec;

import io.netty.handler.codec.DecoderException;
import io.netty.util.CharsetUtil;
import io.vertx.core.buffer.Buffer;
import io.vertx.pgclient.data.Cidr;
import io.vertx.pgclient.data.Inet;
import io.vertx.pgclient.data.Path;
import io.vertx.pgclient.data.Polygon;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;

/**
*/
class DataTypeEstimator {

static final int UNSUPPORTED = 0;
private static final int UTF8 = -1;

static final int NUMERIC = -2;
static final int NUMERIC_ARRAY = -7;
static final int BUFFER = -3;

static final int UNKNOWN = -6;

static final int BOOL = 1;
static final int INT2 = 2;
static final int INT4 = 4;
static final int INT8 = 8;
static final int FLOAT4 = 4;
static final int FLOAT8 = 8;

static final int CHAR = UTF8;
static final int VARCHAR = UTF8;
static final int BPCHAR = UTF8;
static final int TEXT = UTF8;
static final int NAME = UTF8;

static final int DATE = 4;
static final int TIME = 8;
static final int TIMETZ = 12;
static final int TIMESTAMP = 8;
static final int TIMESTAMPTZ = 8;
static final int INTERVAL = 16;

static final int BYTEA = BUFFER;

static final int INET = -10;
static final int CIDR = -9;
static final int UUID = 16;

static final int JSON = UTF8;
static final int JSONB = -8;

static final int MONEY = 8;

static final int POINT = 16;
static final int LINE = 24;
static final int LSEG = 32;
static final int BOX = 32;
static final int CIRCLE = 24;
static final int POLYGON = -4;
static final int PATH = -5;

// Eventually make this configurable per options
private static final float AVG_BYTES_PER_CHAR_UTF8 = CharsetUtil.encoder(CharsetUtil.UTF_8).averageBytesPerChar();

static int estimateUTF8(String s) {
return (int)(s.length() * AVG_BYTES_PER_CHAR_UTF8);
}

static int estimateByteArray(byte[] b) {
return b.length;
}

static int estimateCStringUTF8(String s) {
return estimateUTF8(s) + 1;
}

private static int estimateUnknown(String value) {
return estimateUTF8(value);
}

private static int estimateJSONB(String value) {
return 1 + estimateUTF8(value);
}

private static int estimateNumeric(String value) {
return estimateUTF8(value);
}

private static int estimateInetOrCidr(Cidr value) {
return estimateInetOrCidr(value.getAddress());
}

private static int estimateInetOrCidr(Inet value) {
return estimateInetOrCidr(value.getAddress());
}

private static int estimateInetOrCidr(InetAddress address) {
int len;
if (address instanceof Inet6Address) {
Inet6Address inet6Address = (Inet6Address) address;
len = inet6Address.getAddress().length;
} else if (address instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address;
len = inet4Address.getAddress().length;
} else {
// Invalid
len = 0;
}
return 1 + 1 + 1 + 1 + len;
}

private static int estimateNumericArray(Object[] value) {
int length = 1;
for (Object elt : value) {
length += elt == null ? 4 : estimateNumeric((String) elt);
}
length += value.length;
return length;
}

private static int estimateBuffer(Buffer b) {
return b.length();
}

private static int estimatePolygon(Polygon p) {
return 4 + p.getPoints().size() * 16;
}

private static int estimatePath(Path p) {
return 1 + 4 + p.getPoints().size() * 16;
}

static int estimate(int estimator, Object o) {
if (estimator > 0) {
return estimator;
} else {
switch (estimator) {
case DataTypeEstimator.CIDR:
return estimateInetOrCidr((Cidr) o);
case DataTypeEstimator.INET:
return estimateInetOrCidr((Inet) o);
case DataTypeEstimator.JSONB:
return estimateJSONB((String) o);
case DataTypeEstimator.UNKNOWN:
return estimateUnknown((String) o);
case DataTypeEstimator.NUMERIC:
return estimateNumeric((String) o);
case DataTypeEstimator.NUMERIC_ARRAY:
return estimateNumericArray((Object[]) o);
case DataTypeEstimator.UTF8:
return estimateUTF8((String) o);
case DataTypeEstimator.BUFFER:
return estimateBuffer((Buffer) o);
case DataTypeEstimator.POLYGON:
return estimatePolygon((Polygon) o);
case DataTypeEstimator.PATH:
return estimatePath((Path) o);
default:
throw new UnsupportedOperationException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @author <a href="mailto:[email protected]">Emad Alblueshi</a>
*/
class Describe {
class Describe extends OutboundMessage {

final byte[] statement;
final String portal;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.vertx.pgclient.impl.codec;

class ExecuteMessage extends OutboundMessage {

static final ExecuteMessage INSTANCE = new ExecuteMessage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.vertx.pgclient.impl.codec;

public class OutboundMessage {
}
Loading