Skip to content

Commit 675f556

Browse files
committed
Add generic constraint on StringDataType
1 parent b65b5cb commit 675f556

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/IDataType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ default String[] getAliases() {
4141

4242
int sqlTypeId();
4343

44-
Class<T> javaTypeClass();
44+
Class<? extends T> javaTypeClass();
4545

4646
boolean nullable();
4747

clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/data/type/complex/DataTypeString.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.github.housepower.jdbc.connect.NativeContext;
1818
import com.github.housepower.jdbc.data.IDataType;
19+
import com.github.housepower.jdbc.misc.BytesCharSeq;
1920
import com.github.housepower.jdbc.misc.SQLLexer;
2021
import com.github.housepower.jdbc.serde.BinaryDeserializer;
2122
import com.github.housepower.jdbc.serde.BinarySerializer;
@@ -25,7 +26,7 @@
2526
import java.sql.SQLException;
2627
import java.sql.Types;
2728

28-
public class DataTypeString implements IDataType {
29+
public class DataTypeString implements IDataType<CharSequence> {
2930

3031
public static DataTypeCreator CREATOR = (lexer, serverContext) -> new DataTypeString(serverContext);
3132

@@ -46,12 +47,13 @@ public int sqlTypeId() {
4647
}
4748

4849
@Override
49-
public Object defaultValue() {
50+
public String defaultValue() {
5051
return "";
5152
}
5253

54+
// TODO FIX Later
5355
@Override
54-
public Class javaTypeClass() {
56+
public Class<String> javaTypeClass() {
5557
return String.class;
5658
}
5759

@@ -71,12 +73,12 @@ public int getScale() {
7173
}
7274

7375
@Override
74-
public void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException {
75-
if (data instanceof CharSequence) {
76-
serializer.writeStringBinary(data.toString(), charset);
77-
} else {
78-
serializer.writeBytesBinary((byte[]) data);
76+
public void serializeBinary(CharSequence data, BinarySerializer serializer) throws SQLException, IOException {
77+
if (data instanceof BytesCharSeq) {
78+
serializer.writeBytesBinary(((BytesCharSeq) data).bytes());
79+
return;
7980
}
81+
serializer.writeStringBinary(data.toString(), charset);
8082
}
8183

8284
/**
@@ -90,7 +92,7 @@ public String deserializeBinary(BinaryDeserializer deserializer) throws SQLExcep
9092
}
9193

9294
@Override
93-
public Object[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException {
95+
public String[] deserializeBinaryBulk(int rowCnt, BinaryDeserializer deserializer) throws SQLException, IOException {
9496
String[] data = new String[rowCnt];
9597
for (int row = 0; row < rowCnt; row++) {
9698
byte[] bs = deserializer.readBytesBinary();
@@ -115,7 +117,7 @@ public String[] getAliases() {
115117
}
116118

117119
@Override
118-
public Object deserializeTextQuoted(SQLLexer lexer) throws SQLException {
120+
public CharSequence deserializeTextQuoted(SQLLexer lexer) throws SQLException {
119121
return lexer.stringView();
120122
}
121123
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.github.housepower.jdbc.misc;
16+
17+
public class BytesCharSeq implements CharSequence {
18+
19+
private final byte[] bytes;
20+
21+
public BytesCharSeq(byte[] bytes) {
22+
this.bytes = bytes;
23+
}
24+
25+
@Override
26+
public int length() {
27+
return bytes.length;
28+
}
29+
30+
@Override
31+
public char charAt(int index) {
32+
return (char) bytes[index];
33+
}
34+
35+
@Override
36+
public CharSequence subSequence(int start, int end) {
37+
byte[] newBytes = new byte[end - start];
38+
System.arraycopy(bytes, start, newBytes, 0, end - start);
39+
return new BytesCharSeq(newBytes);
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "BytesCharSeq, length: " + length();
45+
}
46+
47+
public byte[] bytes() {
48+
return bytes;
49+
}
50+
}

0 commit comments

Comments
 (0)