All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ydb.jooq.binding.TimestampBinding Maven / Gradle / Ivy

The newest version!
package tech.ydb.jooq.binding;

import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import org.jooq.BindingGetResultSetContext;
import org.jooq.BindingSetStatementContext;
import org.jooq.Converter;
import org.jooq.impl.AbstractBinding;
import static tech.ydb.jooq.binding.BindingTools.indexType;
import tech.ydb.table.values.PrimitiveType;
import tech.ydb.table.values.PrimitiveValue;

@SuppressWarnings("resource")
public final class TimestampBinding extends AbstractBinding {

    private static final int INDEX_TYPE = indexType(PrimitiveType.Timestamp);

    @Override
    public Converter converter() {
        return new TimestampConverter();
    }

    @Override
    public void set(BindingSetStatementContext ctx) throws SQLException {
        if (ctx.value() == null) {
            ctx.statement().setNull(ctx.index(), INDEX_TYPE);
        } else {
            ctx.statement().setObject(ctx.index(), PrimitiveValue.newTimestamp(ctx.value()), INDEX_TYPE);
        }
    }

    @Override
    public void get(BindingGetResultSetContext ctx) throws SQLException {
        Instant value = (Instant) ctx.resultSet().getObject(ctx.index());
        ctx.value(value);
    }

    private static class TimestampConverter implements Converter {
        @Override
        public Instant from(LocalDateTime databaseObject) {
            return databaseObject.toInstant(ZoneOffset.UTC);
        }

        @Override
        public LocalDateTime to(Instant userObject) {
            return LocalDateTime.ofInstant(userObject, ZoneOffset.UTC);
        }

        @Override
        public Class fromType() {
            return LocalDateTime.class;
        }

        @Override
        public Class toType() {
            return Instant.class;
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy