Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Refactor IDataType #248

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<byte[], BytesCharSeq> {

@Override
public BytesCharSeq convert(byte[] source) {
return new BytesCharSeq(source);
}
}
Original file line number Diff line number Diff line change
@@ -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<BytesCharSeq, byte[]> {

@Override
public byte[] convert(BytesCharSeq source) {
return source.bytes();
}
}
Original file line number Diff line number Diff line change
@@ -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 <S> the source type
* @param <T> the target type
*/
@ThreadSafe
@FunctionalInterface
public interface Converter<S, T> {

/**
* 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<S> sourceType() {
return (Class<S>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
}

@SuppressWarnings("unchecked")
default Class<T> targetType() {
return (Class<T>) ((ParameterizedType) this.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1];
}
}
Original file line number Diff line number Diff line change
@@ -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<JDBC> {

int sqlTypeId();

Class<JDBC> javaType();

boolean nullable();

int getPrecision();

int getScale();

boolean isSigned();

default String format(JDBC value) {
return value.toString();
}
}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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<BigDecimal> {

public JdbcDecimal(IDataType<BigDecimal, BigDecimal> ckDataType) {
super(ckDataType);
}

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

@Override
public Class<BigDecimal> javaType() {
return BigDecimal.class;
}
}
Original file line number Diff line number Diff line change
@@ -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<JDBC> implements JdbcDataType<JDBC> {

protected final IDataType<JDBC, JDBC> ckDataType;

protected WrappedJdbcDataType(IDataType<JDBC, JDBC> 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);
}
}