diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/ByteArrayToBytesCharSeqConverter.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/ByteArrayToBytesCharSeqConverter.java new file mode 100644 index 00000000..a0fc924f --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/ByteArrayToBytesCharSeqConverter.java @@ -0,0 +1,28 @@ +/* + * 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.convert; + +import com.github.housepower.misc.BytesCharSeq; + +import javax.annotation.concurrent.ThreadSafe; + +@ThreadSafe +public class ByteArrayToBytesCharSeqConverter implements Converter { + + @Override + public BytesCharSeq convert(byte[] source) { + return new BytesCharSeq(source); + } +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/BytesCharSeqToByteArrayConverter.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/BytesCharSeqToByteArrayConverter.java new file mode 100644 index 00000000..e0bb5d9e --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/BytesCharSeqToByteArrayConverter.java @@ -0,0 +1,28 @@ +/* + * 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.convert; + +import com.github.housepower.misc.BytesCharSeq; + +import javax.annotation.concurrent.ThreadSafe; + +@ThreadSafe +public class BytesCharSeqToByteArrayConverter implements Converter { + + @Override + public byte[] convert(BytesCharSeq source) { + return source.bytes(); + } +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/Converter.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/Converter.java new file mode 100644 index 00000000..7df6a2db --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/convert/Converter.java @@ -0,0 +1,48 @@ +/* + * 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.convert; + +import javax.annotation.concurrent.ThreadSafe; +import java.lang.reflect.ParameterizedType; + +/** + * A converter converts a source object of type {@code S} to a target of type {@code T}. + * + * @param the source type + * @param the target type + */ +@ThreadSafe +@FunctionalInterface +public interface Converter { + + /** + * Convert the source object of type {@code S} to target type {@code T}. + * + * @param source the source object to convert, which must be an instance of {@code S} + * @return the converted object, which must be an instance of {@code T} + * @throws IllegalArgumentException if the source cannot be converted to the desired target type + */ + T convert(S source); + + @SuppressWarnings("unchecked") + default Class sourceType() { + return (Class) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; + } + + @SuppressWarnings("unchecked") + default Class targetType() { + return (Class) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1]; + } +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataType.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataType.java new file mode 100644 index 00000000..994b615f --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataType.java @@ -0,0 +1,34 @@ +/* + * 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.type; + +public interface JdbcDataType { + + int sqlTypeId(); + + Class javaType(); + + boolean nullable(); + + int getPrecision(); + + int getScale(); + + boolean isSigned(); + + default String format(JDBC value) { + return value.toString(); + } +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataTypeFactory.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataTypeFactory.java new file mode 100644 index 00000000..f184c9fd --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDataTypeFactory.java @@ -0,0 +1,19 @@ +/* + * 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.type; + +public class JdbcDataTypeFactory { + +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDecimal.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDecimal.java new file mode 100644 index 00000000..9513cbe4 --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/JdbcDecimal.java @@ -0,0 +1,37 @@ +/* + * 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.type; + +import com.github.housepower.data.IDataType; + +import java.math.BigDecimal; +import java.sql.Types; + +public class JdbcDecimal extends WrappedJdbcDataType { + + public JdbcDecimal(IDataType ckDataType) { + super(ckDataType); + } + + @Override + public int sqlTypeId() { + return Types.DECIMAL; + } + + @Override + public Class javaType() { + return BigDecimal.class; + } +} diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/WrappedJdbcDataType.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/WrappedJdbcDataType.java new file mode 100644 index 00000000..3dea0292 --- /dev/null +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/type/WrappedJdbcDataType.java @@ -0,0 +1,51 @@ +/* + * 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.type; + +import com.github.housepower.data.IDataType; + +public abstract class WrappedJdbcDataType implements JdbcDataType { + + protected final IDataType ckDataType; + + protected WrappedJdbcDataType(IDataType ckDataType) { + this.ckDataType = ckDataType; + } + + @Override + public boolean nullable() { + return ckDataType.nullable(); + } + + @Override + public int getPrecision() { + return ckDataType.getPrecision(); + } + + @Override + public int getScale() { + return ckDataType.getScale(); + } + + @Override + public boolean isSigned() { + return ckDataType.isSigned(); + } + + @Override + public String format(JDBC value) { + return ckDataType.serializeText(value); + } +}