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

de.bytefish.pgbulkinsert.mapping.AbstractMapping Maven / Gradle / Ivy

There is a newer version: 7.0.1
Show newest version
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package de.bytefish.pgbulkinsert.mapping;

import de.bytefish.pgbulkinsert.function.ToBooleanFunction;
import de.bytefish.pgbulkinsert.function.ToFloatFunction;
import de.bytefish.pgbulkinsert.model.ColumnDefinition;
import de.bytefish.pgbulkinsert.model.TableDefinition;
import de.bytefish.pgbulkinsert.pgsql.PgBinaryWriter;
import de.bytefish.pgbulkinsert.pgsql.constants.DataType;
import de.bytefish.pgbulkinsert.pgsql.constants.ObjectIdentifier;
import de.bytefish.pgbulkinsert.pgsql.handlers.*;
import de.bytefish.pgbulkinsert.pgsql.model.geometric.*;
import de.bytefish.pgbulkinsert.pgsql.model.network.MacAddress;
import de.bytefish.pgbulkinsert.pgsql.model.range.Range;
import de.bytefish.pgbulkinsert.util.PostgreSqlUtils;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;

public abstract class AbstractMapping {

    protected boolean usePostgresQuoting;

    protected final IValueHandlerProvider provider;

    protected final TableDefinition table;

    protected final List> columns;

    protected AbstractMapping(String schemaName, String tableName) {
        this(new ValueHandlerProvider(), schemaName, tableName, false);
    }

    protected AbstractMapping(String schemaName, String tableName, boolean usePostgresQuoting) {
        this(new ValueHandlerProvider(), schemaName, tableName, usePostgresQuoting);
    }

    protected AbstractMapping(IValueHandlerProvider provider, String schemaName, String tableName, boolean usePostgresQuoting) {
        this.provider = provider;
        this.table = new TableDefinition(schemaName, tableName);
        this.usePostgresQuoting = usePostgresQuoting;
        this.columns = new ArrayList<>();
    }

    protected void usePostgresQuoting(boolean enabled) {
        this.usePostgresQuoting = enabled;
    }

    protected > void mapCollection(String columnName, DataType dataType, Function propertyGetter) {

        final IValueHandler valueHandler = provider.resolve(dataType);
        final int valueOID = ObjectIdentifier.mapFrom(dataType);

        map(columnName, new CollectionValueHandler<>(valueOID, valueHandler), propertyGetter);
    }

    protected  void map(String columnName, DataType dataType, Function propertyGetter) {
        final IValueHandler valueHandler = provider.resolve(dataType);

        map(columnName, valueHandler, propertyGetter);
    }

    protected  void map(String columnName, IValueHandler valueHandler, Function propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.write(valueHandler, propertyGetter.apply(entity));
        });
    }

    // region Numeric

    protected void mapBoolean(String columnName, Function propertyGetter) {
        map(columnName, DataType.Boolean, propertyGetter);
    }

    protected void mapBoolean(String columnName, ToBooleanFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeBoolean(propertyGetter.applyAsBoolean(entity));
        });
    }

    protected void mapByte(String columnName, Function propertyGetter) {
        map(columnName, DataType.Char, propertyGetter);
    }

    protected void mapByte(String columnName, ToIntFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeByte(propertyGetter.applyAsInt(entity));
        });
    }

    protected void mapShort(String columnName, Function propertyGetter) {
        map(columnName, DataType.Int2, propertyGetter);
    }

    protected void mapShort(String columnName, ToIntFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeShort(propertyGetter.applyAsInt(entity));
        });
    }

    protected void mapInteger(String columnName, Function propertyGetter) {
        map(columnName, DataType.Int4, propertyGetter);
    }

    protected void mapInteger(String columnName, ToIntFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeInt(propertyGetter.applyAsInt(entity));
        });
    }

    protected void mapNumeric(String columnName, Function propertyGetter) {
        map(columnName, DataType.Numeric, propertyGetter);
    }

    protected void mapLong(String columnName, Function propertyGetter) {
        map(columnName, DataType.Int8, propertyGetter);
    }

    protected void mapLong(String columnName, ToLongFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeLong(propertyGetter.applyAsLong(entity));
        });
    }

    protected void mapFloat(String columnName, Function propertyGetter) {
        map(columnName, DataType.SinglePrecision, propertyGetter);
    }

    protected void mapFloat(String columnName, ToFloatFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeFloat(propertyGetter.applyAsFloat(entity));
        });
    }

    protected void mapDouble(String columnName, Function propertyGetter) {
        map(columnName, DataType.DoublePrecision, propertyGetter);
    }

    protected void mapDouble(String columnName, ToDoubleFunction propertyGetter) {
        addColumn(columnName, (binaryWriter, entity) -> {
            binaryWriter.writeDouble(propertyGetter.applyAsDouble(entity));
        });
    }

    // endregion

    // region Network
    protected void mapInet4Addr(String columnName, Function propertyGetter) {
        map(columnName, DataType.Inet4, propertyGetter);
    }

    protected void mapInet6Addr(String columnName, Function propertyGetter) {
        map(columnName, DataType.Inet6, propertyGetter);
    }

    protected void mapMacAddress(String columnName, Function propertyGetter) {
        map(columnName, DataType.MacAddress, propertyGetter);
    }

    // endregion

    // region Temporal

    protected void mapDate(String columnName, Function propertyGetter) {
        map(columnName, DataType.Date, propertyGetter);
    }

    protected void mapTime(String columnName, Function propertyGetter) {
        map(columnName, DataType.Time, propertyGetter);
    }

    protected void mapTimeStamp(String columnName, Function propertyGetter) {
        map(columnName, DataType.Timestamp, propertyGetter);
    }

    protected void mapTimeStampTz(String columnName, Function propertyGetter) {
        map(columnName, DataType.TimestampTz, propertyGetter);
    }

    // endregion

    // region Text

    protected void mapText(String columnName, Function propertyGetter) {
        map(columnName, DataType.Text, propertyGetter);
    }

    protected void mapVarChar(String columnName, Function propertyGetter) {
        map(columnName, DataType.Text, propertyGetter);
    }

    // engregion

    // region UUID

    protected void mapUUID(String columnName, Function propertyGetter) {
        map(columnName, DataType.Uuid, propertyGetter);
    }

    // endregion

    // region JSON

    protected void mapJsonb(String columnName, Function propertyGetter) {
        map(columnName, DataType.Jsonb, propertyGetter);
    }

    // endregion

    // region hstore

    protected void mapHstore(String columnName, Function> propertyGetter) {
        map(columnName, DataType.Hstore, propertyGetter);
    }

    // endregion

    // region Geo

    protected void mapPoint(String columnName, Function propertyGetter) {
        map(columnName, DataType.Point, propertyGetter);
    }

    protected void mapBox(String columnName, Function propertyGetter) {
        map(columnName, DataType.Box, propertyGetter);
    }

    protected void mapPath(String columnName, Function propertyGetter) {
        map(columnName, DataType.Path, propertyGetter);
    }

    protected void mapPolygon(String columnName, Function propertyGetter) {
        map(columnName, DataType.Polygon, propertyGetter);
    }

    protected void mapLine(String columnName, Function propertyGetter) {
        map(columnName, DataType.Line, propertyGetter);
    }

    protected void mapLineSegment(String columnName, Function propertyGetter) {
        map(columnName, DataType.LineSegment, propertyGetter);
    }

    protected void mapCircle(String columnName, Function propertyGetter) {
        map(columnName, DataType.Circle, propertyGetter);
    }

    // endregion

    // region Arrays

    protected void mapBooleanArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Boolean, propertyGetter);
    }

    protected void mapByteArray(String columnName, Function propertyGetter) {
        map(columnName, DataType.Bytea, propertyGetter);
    }

    protected  void mapShortArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Int2, propertyGetter);
    }

    protected  void mapIntegerArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Int4, propertyGetter);
    }

    protected  void mapLongArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Int8, propertyGetter);
    }

    protected void mapTextArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Text, propertyGetter);
    }

    protected void mapVarCharArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.VarChar, propertyGetter);
    }

    protected  void mapFloatArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.SinglePrecision, propertyGetter);
    }

    protected  void mapDoubleArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.DoublePrecision, propertyGetter);
    }

    protected  void mapNumericArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Numeric, propertyGetter);
    }

    protected void mapUUIDArray(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Uuid, propertyGetter);
    }

    protected void mapInet4Array(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Inet4, propertyGetter);
    }

    protected void mapInet6Array(String columnName, Function> propertyGetter) {
        mapCollection(columnName, DataType.Inet6, propertyGetter);
    }

    // endregion

    // region Ranges

    protected  void mapRange(String columnName, DataType dataType, Function> propertyGetter) {
        final IValueHandler valueHandler = provider.resolve(dataType);

        map(columnName, new RangeValueHandler<>(valueHandler), propertyGetter);
    }

    protected void mapTsRange(String columnName, Function> propertyGetter) {
        map(columnName, DataType.TsRange, propertyGetter);
    }

    protected void mapTsTzRange(String columnName, Function> propertyGetter) {
        map(columnName, DataType.TsTzRange, propertyGetter);
    }

    protected void mapInt4Range(String columnName, Function> propertyGetter) {
        map(columnName, DataType.Int4Range, propertyGetter);
    }

    protected void mapInt8Range(String columnName, Function> propertyGetter) {
        map(columnName, DataType.Int8Range, propertyGetter);
    }

    protected void mapNumRange(String columnName, Function> propertyGetter) {
        map(columnName, DataType.NumRange, propertyGetter);
    }

    protected void mapDateRange(String columnName, Function> propertyGetter) {
        map(columnName, DataType.DateRange, propertyGetter);
    }

    // endregion

    private void addColumn(String columnName, BiConsumer action) {
        columns.add(new ColumnDefinition<>(columnName, action));
    }

    public List> getColumns() {
        return columns;
    }

    public String getCopyCommand() {
        String commaSeparatedColumns = columns.stream()
                .map(x -> x.getColumnName())
                .map(x -> usePostgresQuoting ? PostgreSqlUtils.quoteIdentifier(x) : x)
                .collect(Collectors.joining(", "));

        return String.format("COPY %1$s(%2$s) FROM STDIN BINARY",
                table.GetFullyQualifiedTableName(usePostgresQuoting),
                commaSeparatedColumns);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy