From 21d8c481af56f3498b10bad42d1f6574ded6da91 Mon Sep 17 00:00:00 2001 From: Cheng Pan <379377944@qq.com> Date: Sat, 26 Dec 2020 11:29:39 +0800 Subject: [PATCH] Add generic constraint on StringDataType --- .../housepower/jdbc/data/IDataType.java | 2 +- .../data/type/complex/DataTypeString.java | 22 ++++---- .../housepower/jdbc/misc/BytesCharSeq.java | 50 +++++++++++++++++++ 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/misc/BytesCharSeq.java diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java index 843395d3..6f64735a 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java @@ -41,7 +41,7 @@ default String[] getAliases() { int sqlTypeId(); - Class javaTypeClass(); + Class javaTypeClass(); boolean nullable(); diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java index 99d2daac..d755417a 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java @@ -16,6 +16,7 @@ import com.github.housepower.jdbc.connect.NativeContext; import com.github.housepower.jdbc.data.IDataType; +import com.github.housepower.jdbc.misc.BytesCharSeq; import com.github.housepower.jdbc.misc.SQLLexer; import com.github.housepower.jdbc.serde.BinaryDeserializer; import com.github.housepower.jdbc.serde.BinarySerializer; @@ -25,7 +26,7 @@ import java.sql.SQLException; import java.sql.Types; -public class DataTypeString implements IDataType { +public class DataTypeString implements IDataType { public static DataTypeCreator CREATOR = (lexer, serverContext) -> new DataTypeString(serverContext); @@ -46,12 +47,13 @@ public int sqlTypeId() { } @Override - public Object defaultValue() { + public String defaultValue() { return ""; } + // TODO FIX Later @Override - public Class javaTypeClass() { + public Class javaTypeClass() { return String.class; } @@ -71,12 +73,12 @@ public int getScale() { } @Override - public void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException { - if (data instanceof CharSequence) { - serializer.writeStringBinary(data.toString(), charset); - } else { - serializer.writeBytesBinary((byte[]) data); + public void serializeBinary(CharSequence data, BinarySerializer serializer) throws SQLException, IOException { + if (data instanceof BytesCharSeq) { + serializer.writeBytesBinary(((BytesCharSeq) data).bytes()); + return; } + serializer.writeStringBinary(data.toString(), charset); } /** @@ -90,7 +92,7 @@ public String deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep } @Override - public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { + public String[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException { String[] data = new String[rowCnt]; for (int row = 0; row < rowCnt; row++) { byte[] bs = deserializer.readBytesBinary(); @@ -115,7 +117,7 @@ public String[] getAliases() { } @Override - public Object deserializeTextQuoted(SQLLexer lexer) throws SQLException { + public CharSequence deserializeTextQuoted(SQLLexer lexer) throws SQLException { return lexer.stringView(); } } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/misc/BytesCharSeq.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/misc/BytesCharSeq.java new file mode 100644 index 00000000..df0a0bd2 --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/misc/BytesCharSeq.java @@ -0,0 +1,50 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.housepower.jdbc.misc; + +public class BytesCharSeq implements CharSequence { + + private final byte[] bytes; + + public BytesCharSeq(byte[] bytes) { + this.bytes = bytes; + } + + @Override + public int length() { + return bytes.length; + } + + @Override + public char charAt(int index) { + return (char) bytes[index]; + } + + @Override + public CharSequence subSequence(int start, int end) { + byte[] newBytes = new byte[end - start]; + System.arraycopy(bytes, start, newBytes, 0, end - start); + return new BytesCharSeq(newBytes); + } + + @Override + public String toString() { + return "BytesCharSeq, length: " + length(); + } + + public byte[] bytes() { + return bytes; + } +}