提交 ace5c2ba authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use own constants in Transfer

上级 9edf8c8e
...@@ -42,6 +42,34 @@ public class Transfer { ...@@ -42,6 +42,34 @@ public class Transfer {
private static final int LOB_MAGIC = 0x1234; private static final int LOB_MAGIC = 0x1234;
private static final int LOB_MAC_SALT_LENGTH = 16; private static final int LOB_MAC_SALT_LENGTH = 16;
private static final int NULL = 0;
private static final int BOOLEAN = 1;
private static final int BYTE = 2;
private static final int SHORT = 3;
private static final int INT = 4;
private static final int LONG = 5;
private static final int DECIMAL = 6;
private static final int DOUBLE = 7;
private static final int FLOAT = 8;
private static final int TIME = 9;
private static final int DATE = 10;
private static final int TIMESTAMP = 11;
private static final int BYTES = 12;
private static final int STRING = 13;
private static final int STRING_IGNORECASE = 14;
private static final int BLOB = 15;
private static final int CLOB = 16;
private static final int ARRAY = 17;
private static final int RESULT_SET = 18;
private static final int JAVA_OBJECT = 19;
private static final int UUID = 20;
private static final int STRING_FIXED = 21;
private static final int GEOMETRY = 22;
private static final int TIMESTAMP_TZ = 24;
private static final int ENUM = 25;
private static final int INTERVAL = 26;
private static final int ROW = 27;
private Socket socket; private Socket socket;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
...@@ -322,45 +350,48 @@ public class Transfer { ...@@ -322,45 +350,48 @@ public class Transfer {
int type = v.getValueType(); int type = v.getValueType();
switch (type) { switch (type) {
case Value.NULL: case Value.NULL:
writeInt(Value.NULL); writeInt(NULL);
break; break;
case Value.BYTES: case Value.BYTES:
writeInt(BYTES);
writeBytes(v.getBytesNoCopy());
break;
case Value.JAVA_OBJECT: case Value.JAVA_OBJECT:
writeInt(type); writeInt(JAVA_OBJECT);
writeBytes(v.getBytesNoCopy()); writeBytes(v.getBytesNoCopy());
break; break;
case Value.UUID: { case Value.UUID: {
writeInt(Value.UUID); writeInt(UUID);
ValueUuid uuid = (ValueUuid) v; ValueUuid uuid = (ValueUuid) v;
writeLong(uuid.getHigh()); writeLong(uuid.getHigh());
writeLong(uuid.getLow()); writeLong(uuid.getLow());
break; break;
} }
case Value.BOOLEAN: case Value.BOOLEAN:
writeInt(Value.BOOLEAN); writeInt(BOOLEAN);
writeBoolean(v.getBoolean()); writeBoolean(v.getBoolean());
break; break;
case Value.BYTE: case Value.BYTE:
writeInt(Value.BYTE); writeInt(BYTE);
writeByte(v.getByte()); writeByte(v.getByte());
break; break;
case Value.TIME: case Value.TIME:
writeInt(Value.TIME); writeInt(TIME);
writeLong(((ValueTime) v).getNanos()); writeLong(((ValueTime) v).getNanos());
break; break;
case Value.DATE: case Value.DATE:
writeInt(Value.DATE); writeInt(DATE);
writeLong(((ValueDate) v).getDateValue()); writeLong(((ValueDate) v).getDateValue());
break; break;
case Value.TIMESTAMP: { case Value.TIMESTAMP: {
writeInt(Value.TIMESTAMP); writeInt(TIMESTAMP);
ValueTimestamp ts = (ValueTimestamp) v; ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue()); writeLong(ts.getDateValue());
writeLong(ts.getTimeNanos()); writeLong(ts.getTimeNanos());
break; break;
} }
case Value.TIMESTAMP_TZ: { case Value.TIMESTAMP_TZ: {
writeInt(Value.TIMESTAMP_TZ); writeInt(TIMESTAMP_TZ);
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v; ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
writeLong(ts.getDateValue()); writeLong(ts.getDateValue());
writeLong(ts.getTimeNanos()); writeLong(ts.getTimeNanos());
...@@ -368,37 +399,43 @@ public class Transfer { ...@@ -368,37 +399,43 @@ public class Transfer {
break; break;
} }
case Value.DECIMAL: case Value.DECIMAL:
writeInt(Value.DECIMAL); writeInt(DECIMAL);
writeString(v.getString()); writeString(v.getString());
break; break;
case Value.DOUBLE: case Value.DOUBLE:
writeInt(Value.DOUBLE); writeInt(DOUBLE);
writeDouble(v.getDouble()); writeDouble(v.getDouble());
break; break;
case Value.FLOAT: case Value.FLOAT:
writeInt(Value.FLOAT); writeInt(FLOAT);
writeFloat(v.getFloat()); writeFloat(v.getFloat());
break; break;
case Value.INT: case Value.INT:
writeInt(Value.INT); writeInt(INT);
writeInt(v.getInt()); writeInt(v.getInt());
break; break;
case Value.LONG: case Value.LONG:
writeInt(Value.LONG); writeInt(LONG);
writeLong(v.getLong()); writeLong(v.getLong());
break; break;
case Value.SHORT: case Value.SHORT:
writeInt(Value.SHORT); writeInt(SHORT);
writeInt(v.getShort()); writeInt(v.getShort());
break; break;
case Value.STRING: case Value.STRING:
writeInt(STRING);
writeString(v.getString());
break;
case Value.STRING_IGNORECASE: case Value.STRING_IGNORECASE:
writeInt(STRING_IGNORECASE);
writeString(v.getString());
break;
case Value.STRING_FIXED: case Value.STRING_FIXED:
writeInt(type); writeInt(STRING_FIXED);
writeString(v.getString()); writeString(v.getString());
break; break;
case Value.BLOB: { case Value.BLOB: {
writeInt(Value.BLOB); writeInt(BLOB);
if (version >= Constants.TCP_PROTOCOL_VERSION_11) { if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) { if (v instanceof ValueLobDb) {
ValueLobDb lob = (ValueLobDb) v; ValueLobDb lob = (ValueLobDb) v;
...@@ -429,7 +466,7 @@ public class Transfer { ...@@ -429,7 +466,7 @@ public class Transfer {
break; break;
} }
case Value.CLOB: { case Value.CLOB: {
writeInt(Value.CLOB); writeInt(CLOB);
if (version >= Constants.TCP_PROTOCOL_VERSION_11) { if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) { if (v instanceof ValueLobDb) {
ValueLobDb lob = (ValueLobDb) v; ValueLobDb lob = (ValueLobDb) v;
...@@ -457,7 +494,7 @@ public class Transfer { ...@@ -457,7 +494,7 @@ public class Transfer {
break; break;
} }
case Value.ARRAY: { case Value.ARRAY: {
writeInt(Value.ARRAY); writeInt(ARRAY);
ValueArray va = (ValueArray) v; ValueArray va = (ValueArray) v;
Value[] list = va.getList(); Value[] list = va.getList();
int len = list.length; int len = list.length;
...@@ -474,7 +511,7 @@ public class Transfer { ...@@ -474,7 +511,7 @@ public class Transfer {
break; break;
} }
case Value.ROW: { case Value.ROW: {
writeInt(version >= Constants.TCP_PROTOCOL_VERSION_18 ? Value.ROW : Value.ARRAY); writeInt(version >= Constants.TCP_PROTOCOL_VERSION_18 ? ROW : ARRAY);
ValueRow va = (ValueRow) v; ValueRow va = (ValueRow) v;
Value[] list = va.getList(); Value[] list = va.getList();
int len = list.length; int len = list.length;
...@@ -485,13 +522,13 @@ public class Transfer { ...@@ -485,13 +522,13 @@ public class Transfer {
break; break;
} }
case Value.ENUM: { case Value.ENUM: {
writeInt(Value.ENUM); writeInt(ENUM);
writeInt(v.getInt()); writeInt(v.getInt());
writeString(v.getString()); writeString(v.getString());
break; break;
} }
case Value.RESULT_SET: { case Value.RESULT_SET: {
writeInt(Value.RESULT_SET); writeInt(RESULT_SET);
ResultInterface result = ((ValueResultSet) v).getResult(); ResultInterface result = ((ValueResultSet) v).getResult();
int columnCount = result.getVisibleColumnCount(); int columnCount = result.getVisibleColumnCount();
writeInt(columnCount); writeInt(columnCount);
...@@ -520,7 +557,7 @@ public class Transfer { ...@@ -520,7 +557,7 @@ public class Transfer {
break; break;
} }
case Value.GEOMETRY: case Value.GEOMETRY:
writeInt(Value.GEOMETRY); writeInt(GEOMETRY);
if (version >= Constants.TCP_PROTOCOL_VERSION_14) { if (version >= Constants.TCP_PROTOCOL_VERSION_14) {
writeBytes(v.getBytesNoCopy()); writeBytes(v.getBytesNoCopy());
} else { } else {
...@@ -533,12 +570,16 @@ public class Transfer { ...@@ -533,12 +570,16 @@ public class Transfer {
case Value.INTERVAL_HOUR: case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE: case Value.INTERVAL_MINUTE:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) { if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
writeInt(type);
ValueInterval interval = (ValueInterval) v; ValueInterval interval = (ValueInterval) v;
writeBoolean(interval.isNegative()); int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL);
writeByte((byte) ordinal);
writeLong(interval.getLeading()); writeLong(interval.getLeading());
} else { } else {
writeInt(Value.STRING); writeInt(STRING);
writeString(v.getString()); writeString(v.getString());
} }
break; break;
...@@ -551,13 +592,17 @@ public class Transfer { ...@@ -551,13 +592,17 @@ public class Transfer {
case Value.INTERVAL_HOUR_TO_SECOND: case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND: case Value.INTERVAL_MINUTE_TO_SECOND:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) { if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
writeInt(type);
ValueInterval interval = (ValueInterval) v; ValueInterval interval = (ValueInterval) v;
writeBoolean(interval.isNegative()); int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL);
writeByte((byte) ordinal);
writeLong(interval.getLeading()); writeLong(interval.getLeading());
writeLong(interval.getRemaining()); writeLong(interval.getRemaining());
} else { } else {
writeInt(Value.STRING); writeInt(STRING);
writeString(v.getString()); writeString(v.getString());
} }
break; break;
...@@ -579,51 +624,51 @@ public class Transfer { ...@@ -579,51 +624,51 @@ public class Transfer {
public Value readValue() throws IOException { public Value readValue() throws IOException {
int type = readInt(); int type = readInt();
switch (type) { switch (type) {
case Value.NULL: case NULL:
return ValueNull.INSTANCE; return ValueNull.INSTANCE;
case Value.BYTES: case BYTES:
return ValueBytes.getNoCopy(readBytes()); return ValueBytes.getNoCopy(readBytes());
case Value.UUID: case UUID:
return ValueUuid.get(readLong(), readLong()); return ValueUuid.get(readLong(), readLong());
case Value.JAVA_OBJECT: case JAVA_OBJECT:
return ValueJavaObject.getNoCopy(null, readBytes(), session.getDataHandler()); return ValueJavaObject.getNoCopy(null, readBytes(), session.getDataHandler());
case Value.BOOLEAN: case BOOLEAN:
return ValueBoolean.get(readBoolean()); return ValueBoolean.get(readBoolean());
case Value.BYTE: case BYTE:
return ValueByte.get(readByte()); return ValueByte.get(readByte());
case Value.DATE: case DATE:
return ValueDate.fromDateValue(readLong()); return ValueDate.fromDateValue(readLong());
case Value.TIME: case TIME:
return ValueTime.fromNanos(readLong()); return ValueTime.fromNanos(readLong());
case Value.TIMESTAMP: case TIMESTAMP:
return ValueTimestamp.fromDateValueAndNanos(readLong(), readLong()); return ValueTimestamp.fromDateValueAndNanos(readLong(), readLong());
case Value.TIMESTAMP_TZ: { case TIMESTAMP_TZ: {
return ValueTimestampTimeZone.fromDateValueAndNanos(readLong(), readLong(), (short) readInt()); return ValueTimestampTimeZone.fromDateValueAndNanos(readLong(), readLong(), (short) readInt());
} }
case Value.DECIMAL: case DECIMAL:
return ValueDecimal.get(new BigDecimal(readString())); return ValueDecimal.get(new BigDecimal(readString()));
case Value.DOUBLE: case DOUBLE:
return ValueDouble.get(readDouble()); return ValueDouble.get(readDouble());
case Value.FLOAT: case FLOAT:
return ValueFloat.get(readFloat()); return ValueFloat.get(readFloat());
case Value.ENUM: { case ENUM: {
final int ordinal = readInt(); final int ordinal = readInt();
final String label = readString(); final String label = readString();
return ValueEnumBase.get(label, ordinal); return ValueEnumBase.get(label, ordinal);
} }
case Value.INT: case INT:
return ValueInt.get(readInt()); return ValueInt.get(readInt());
case Value.LONG: case LONG:
return ValueLong.get(readLong()); return ValueLong.get(readLong());
case Value.SHORT: case SHORT:
return ValueShort.get((short) readInt()); return ValueShort.get((short) readInt());
case Value.STRING: case STRING:
return ValueString.get(readString()); return ValueString.get(readString());
case Value.STRING_IGNORECASE: case STRING_IGNORECASE:
return ValueStringIgnoreCase.get(readString()); return ValueStringIgnoreCase.get(readString());
case Value.STRING_FIXED: case STRING_FIXED:
return ValueStringFixed.get(readString()); return ValueStringFixed.get(readString());
case Value.BLOB: { case BLOB: {
long length = readLong(); long length = readLong();
if (version >= Constants.TCP_PROTOCOL_VERSION_11) { if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (length == -1) { if (length == -1) {
...@@ -648,7 +693,7 @@ public class Transfer { ...@@ -648,7 +693,7 @@ public class Transfer {
} }
return v; return v;
} }
case Value.CLOB: { case CLOB: {
long length = readLong(); long length = readLong();
if (version >= Constants.TCP_PROTOCOL_VERSION_11) { if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (length == -1) { if (length == -1) {
...@@ -678,7 +723,7 @@ public class Transfer { ...@@ -678,7 +723,7 @@ public class Transfer {
} }
return v; return v;
} }
case Value.ARRAY: { case ARRAY: {
int len = readInt(); int len = readInt();
Class<?> componentType = Object.class; Class<?> componentType = Object.class;
if (len < 0) { if (len < 0) {
...@@ -691,7 +736,7 @@ public class Transfer { ...@@ -691,7 +736,7 @@ public class Transfer {
} }
return ValueArray.get(componentType, list); return ValueArray.get(componentType, list);
} }
case Value.ROW: { case ROW: {
int len = readInt(); int len = readInt();
Value[] list = new Value[len]; Value[] list = new Value[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
...@@ -699,7 +744,7 @@ public class Transfer { ...@@ -699,7 +744,7 @@ public class Transfer {
} }
return ValueRow.get(list); return ValueRow.get(list);
} }
case Value.RESULT_SET: { case RESULT_SET: {
SimpleResult rs = new SimpleResult(); SimpleResult rs = new SimpleResult();
int columns = readInt(); int columns = readInt();
for (int i = 0; i < columns; i++) { for (int i = 0; i < columns; i++) {
...@@ -719,28 +764,20 @@ public class Transfer { ...@@ -719,28 +764,20 @@ public class Transfer {
} }
return ValueResultSet.get(rs); return ValueResultSet.get(rs);
} }
case Value.GEOMETRY: case GEOMETRY:
if (version >= Constants.TCP_PROTOCOL_VERSION_14) { if (version >= Constants.TCP_PROTOCOL_VERSION_14) {
return ValueGeometry.get(readBytes()); return ValueGeometry.get(readBytes());
} }
return ValueGeometry.get(readString()); return ValueGeometry.get(readString());
case Value.INTERVAL_YEAR: case INTERVAL: {
case Value.INTERVAL_MONTH: int ordinal = readByte();
case Value.INTERVAL_DAY: boolean negative = ordinal < 0;
case Value.INTERVAL_HOUR: if (negative) {
case Value.INTERVAL_MINUTE: ordinal = ~ordinal;
return ValueInterval.from(IntervalQualifier.valueOf(type - Value.INTERVAL_YEAR), readBoolean(), readLong(), }
0L); return ValueInterval.from(IntervalQualifier.valueOf(ordinal), negative, readLong(),
case Value.INTERVAL_SECOND: ordinal < 5 ? 0 : readLong());
case Value.INTERVAL_YEAR_TO_MONTH: }
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
return ValueInterval.from(IntervalQualifier.valueOf(type - Value.INTERVAL_YEAR), readBoolean(), readLong(),
readLong());
default: default:
if (JdbcUtils.customDataTypesHandler != null) { if (JdbcUtils.customDataTypesHandler != null) {
return JdbcUtils.customDataTypesHandler.convert( return JdbcUtils.customDataTypesHandler.convert(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论