Skip to content

Commit

Permalink
JdbcDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
pan3793 committed Jan 9, 2021
1 parent 7e6391b commit fd78024
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.housepower.jdbc.data.type.complex.DataTypeNullable;
import com.github.housepower.jdbc.log.Logger;
import com.github.housepower.jdbc.log.LoggerFactory;
import com.github.housepower.jdbc.settings.ClickHouseDefines;
import com.github.housepower.jdbc.wrapper.SQLResultSetMetaData;

import java.sql.ResultSetMetaData;
Expand All @@ -45,8 +46,7 @@ public int getColumnCount() throws SQLException {

@Override
public int getColumnType(int index) throws SQLException {
IDataType type = header.getColumnByPosition(index - 1).type();
return type.sqlTypeId();
return header.getColumnByPosition(index - 1).type().sqlTypeId();
}

@Override
Expand Down Expand Up @@ -117,7 +117,7 @@ public String getTableName(int column) throws SQLException {

@Override
public String getCatalogName(int column) throws SQLException {
return "default";
return ClickHouseDefines.FAKE_CATALOG;
}

@Override
Expand All @@ -130,22 +130,6 @@ public boolean isDefinitelyWritable(int column) throws SQLException {
return false;
}

@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return (T) this;
}
throw new SQLException("Unable to unwrap to " + iface.toString());
}

@Override
public boolean isWrapperFor(Class<?> iface) {
return iface != null && iface.isAssignableFrom(getClass());
}

/*=========================================================*/

@Override
public boolean isReadOnly(int index) throws SQLException {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import com.github.housepower.jdbc.misc.BytesCharSeq;

import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class ByteArrayToBytesCharSeqConverter implements Converter<byte[], BytesCharSeq> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import com.github.housepower.jdbc.misc.BytesCharSeq;

import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class BytesCharSeqToByteArrayConverter implements Converter<BytesCharSeq, byte[]> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

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}.
*
* <p>Implementations of this interface are thread-safe and can be shared.
*
* @param <S> the source type
* @param <T> the target type
*/
@ThreadSafe
@FunctionalInterface
public interface Converter<S, T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ default <O> O castTo(T inner, Class<O> castToClazz) {
}

// 5. format to string
default <O> String format(O value) {
return castTo(castFrom(value), String.class);
default String format(T value) {
return value.toString();
}

default boolean isSigned() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.data.jdbc;

import com.github.housepower.jdbc.data.IDataType;

import java.sql.Types;

public class JdbcByteArrayType implements JdbcDataType<byte[]> {

private final IDataType<CharSequence> ckDataType;

public JdbcByteArrayType(IDataType<CharSequence> ckDataType) {
this.ckDataType = ckDataType;
}

@Override
public int sqlTypeId() {
return Types.BINARY;
}

@Override
public Class<byte[]> javaType() {
return byte[].class;
}

@Override
public boolean nullable() {
return ckDataType.nullable();
}

@Override
public int getPrecision() {
return ckDataType.getPrecision();
}

@Override
public int getScale() {
return ckDataType.getScale();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.data.jdbc;


public interface JdbcDataType<JDBC> {

int sqlTypeId();

Class<JDBC> javaType();

boolean nullable();

int getPrecision();

int getScale();

// 4. cast between inner type and other types
//default <CK> JDBC castFrom(IDataType<CK> other) {
// return javaType().cast(other);
//}
//
//default <CK> IDataType<CK> castTo(JDBC inner, Class<O> castToClazz) {
// return castToClazz.cast(inner);
//}

// 5. format to string
default String format(JDBC value) {
return value.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public String defaultValue() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

public class ClickHouseDefines {
public static final String NAME = "ClickHouse";
public static final String FAKE_CATALOG = "default";
public static final String DEFAULT_DATABASE = "default";

public static final int MAJOR_VERSION = 1;
Expand Down

0 comments on commit fd78024

Please sign in to comment.