Skip to content

Commit

Permalink
Add generic constraint on StringDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
pan3793 committed Dec 26, 2020
1 parent b65b5cb commit 675f556
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ default String[] getAliases() {

int sqlTypeId();

Class<T> javaTypeClass();
Class<? extends T> javaTypeClass();

boolean nullable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@
import java.sql.SQLException;
import java.sql.Types;

public class DataTypeString implements IDataType {
public class DataTypeString implements IDataType<CharSequence> {

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

Expand All @@ -46,12 +47,13 @@ public int sqlTypeId() {
}

@Override
public Object defaultValue() {
public String defaultValue() {
return "";
}

// TODO FIX Later
@Override
public Class javaTypeClass() {
public Class<String> javaTypeClass() {
return String.class;
}

Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 675f556

Please sign in to comment.