org.babyfish.jimmer.sql.runtime.AbstractScalarProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.runtime;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.UUID;
public abstract class AbstractScalarProvider implements ScalarProvider {
static final ScalarProvider UUID_BY_BYTE_ARRAY =
new AbstractScalarProvider() {
@Override
public UUID toScalar(byte[] sqlValue) {
ByteBuffer byteBuffer = ByteBuffer.wrap(sqlValue);
long high = byteBuffer.getLong();
long low = byteBuffer.getLong();
return new UUID(high, low);
}
@Override
public byte[] toSql(UUID scalarValue) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(scalarValue.getMostSignificantBits());
byteBuffer.putLong(scalarValue.getLeastSignificantBits());
return byteBuffer.array();
}
@Override
public String toString() {
return "ScalarProvider.UUID_BY_BYTE_ARRAY";
}
};
static final ScalarProvider UUID_BY_STRING =
new AbstractScalarProvider() {
@Override
public UUID toScalar(String sqlValue) {
return UUID.fromString(sqlValue);
}
@Override
public String toSql(UUID scalarValue) {
return scalarValue.toString();
}
@Override
public String toString() {
return "ScalarProvider.UUID_BY_STRING";
}
};
private final Type scalarType;
private final Class sqlType;
public AbstractScalarProvider(Class scalarType, Class sqlType) {
Meta.validateScalarType(scalarType);
this.scalarType = scalarType;
this.sqlType = sqlType;
}
@SuppressWarnings("unchecked")
protected AbstractScalarProvider() {
Meta meta = Meta.create(this.getClass());
this.scalarType = meta.scalarType;
this.sqlType = (Class) meta.sqlType;
}
@NotNull
public final Type getScalarType() {
return scalarType;
}
@NotNull
public final Class getSqlType() {
return sqlType;
}
}