org.mariadb.jdbc.plugin.codec.DoubleCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mariadb-java-client Show documentation
Show all versions of mariadb-java-client Show documentation
JDBC driver for MariaDB and MySQL
The newest version!
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2024 MariaDB Corporation Ab
package org.mariadb.jdbc.plugin.codec;
import java.io.IOException;
import java.sql.SQLDataException;
import java.util.Calendar;
import java.util.EnumSet;
import org.mariadb.jdbc.client.*;
import org.mariadb.jdbc.client.socket.Writer;
import org.mariadb.jdbc.client.util.MutableInt;
import org.mariadb.jdbc.plugin.Codec;
/** Double codec */
public class DoubleCodec implements Codec {
/** default instance */
public static final DoubleCodec INSTANCE = new DoubleCodec();
private static final EnumSet COMPATIBLE_TYPES =
EnumSet.of(
DataType.TINYINT,
DataType.SMALLINT,
DataType.MEDIUMINT,
DataType.INTEGER,
DataType.FLOAT,
DataType.DOUBLE,
DataType.BIGINT,
DataType.YEAR,
DataType.OLDDECIMAL,
DataType.DECIMAL,
DataType.VARCHAR,
DataType.VARSTRING,
DataType.STRING,
DataType.BLOB,
DataType.TINYBLOB,
DataType.MEDIUMBLOB,
DataType.LONGBLOB);
public String className() {
return Double.class.getName();
}
public boolean canDecode(ColumnDecoder column, Class> type) {
return COMPATIBLE_TYPES.contains(column.getType())
&& ((type.isPrimitive() && type == Double.TYPE) || type.isAssignableFrom(Double.class));
}
public boolean canEncode(Object value) {
return value instanceof Double;
}
@Override
@SuppressWarnings("fallthrough")
public Double decodeText(
final ReadableByteBuf buf,
final MutableInt length,
final ColumnDecoder column,
final Calendar cal,
final Context context)
throws SQLDataException {
return column.decodeDoubleText(buf, length);
}
@Override
@SuppressWarnings("fallthrough")
public Double decodeBinary(
final ReadableByteBuf buf,
final MutableInt length,
final ColumnDecoder column,
final Calendar cal,
final Context context)
throws SQLDataException {
return column.decodeDoubleBinary(buf, length);
}
@Override
public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long maxLen)
throws IOException {
encoder.writeAscii(value.toString());
}
@Override
public void encodeBinary(
final Writer encoder,
final Context context,
final Object value,
final Calendar cal,
final Long maxLength)
throws IOException {
encoder.writeDouble((Double) value);
}
public int getBinaryEncodeType() {
return DataType.DOUBLE.get();
}
}