org.jdbi.v3.postgres.DurationArgumentFactory Maven / Gradle / Ivy
/*
 * 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 org.jdbi.v3.postgres;
import java.sql.Types;
import java.time.Duration;
import org.jdbi.v3.core.argument.AbstractArgumentFactory;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.config.ConfigRegistry;
import org.postgresql.util.PGInterval;
/**
 * Postgres version of argument factory for {@link Duration}.
 *
 * 
 * For simplicity, this implementation makes the duration positive before proceeding. However, this can cause an
 * {@link ArithmeticException} to be thrown. E.g., this can occur if your duration is -2^63 seconds.
 *
 * 
 * Not all {@link Duration}s can be represented as intervals in Postgres.
 * One incompatibility results from {@link Duration}s that are too large. This is due to (1) {@link Duration}s using
 * a {@code long} internally, and {@link PGInterval}s using {@code int}s; and (2) the conversion of days to months or
 * years being ambiguous.
 * Another results from {@link Duration}s being too precise; they have nanosecond precision, whereas Postgres has only
 * microsecond.
 * An {@link IllegalArgumentException} will be thrown in these cases.
 * The handling of the second is subject to revision in the future; for example, it would be reasonable to have a
 * configurable truncation option.
 */
public class DurationArgumentFactory extends AbstractArgumentFactory {
    public DurationArgumentFactory() {
        super(Types.OTHER);
    }
    @Override
    public Argument build(Duration duration, ConfigRegistry config) {
        final boolean isNegative = duration.isNegative();
        if (isNegative) {
            duration = duration.negated();
        }
        final long days = duration.toDays();
        if (days > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(
                    String.format("duration %s too large to be represented unambiguously as postgres interval",
                            duration));
        }
        duration = duration.minusDays(days);
        final int hours = (int)duration.toHours();
        duration = duration.minusHours(hours);
        final int minutes = (int)duration.toMinutes();
        duration = duration.minusMinutes(minutes);
        if (duration.getNano() % 1000 != 0) {
            throw new IllegalArgumentException(
                    String.format("duration %s too precise to represented as postgres interval", duration));
        }
        double seconds = duration.getSeconds() + duration.getNano() / 1e9;
        final PGInterval interval = new PGInterval(0, 0, (int)days, hours, minutes, seconds);
        if (isNegative) {
            interval.scale(-1);
        }
        return (i, p, cx) -> p.setObject(i, interval, Types.OTHER);
    }
}