Search in sources :

Example 1 with PGpolygon

use of org.postgresql.geometric.PGpolygon in project debezium by debezium.

the class Wal2JsonReplicationMessage method getValue.

/**
 * Converts the value (string representation) coming from wal2json plugin to
 * a Java value based on the type of the column from the message. This value will be converted later on if necessary by the
 * {@link PostgresValueConverter#converter(Column, Field)} instance to match whatever the Connect schema type expects.
 *
 * Note that the logic here is tightly coupled (i.e. dependent) on the wal2json plugin logic which writes the actual
 * JSON messages.
 * @param a supplier to get a connection to Postgres instance for array handling
 *
 * @return the value; may be null
 */
public Object getValue(String columnName, PostgresType type, String fullType, Value rawValue, final PgConnectionSupplier connection, boolean includeUnknownDatatypes) {
    if (rawValue.isNull()) {
        // nulls are null
        return null;
    }
    if (type.isArrayType()) {
        try {
            final String dataString = rawValue.asString();
            PgArray arrayData = new PgArray(connection.get(), type.getOid(), dataString);
            Object deserializedArray = arrayData.getArray();
            return Arrays.asList((Object[]) deserializedArray);
        } catch (SQLException e) {
            LOGGER.warn("Unexpected exception trying to process PgArray ({}) column '{}', {}", fullType, columnName, e);
        }
        return null;
    }
    switch(type.getName()) {
        // plus aliases from the shorter names produced by older wal2json
        case "boolean":
        case "bool":
            return rawValue.asBoolean();
        case "integer":
        case "int":
        case "int4":
        case "smallint":
        case "int2":
        case "smallserial":
        case "serial":
        case "serial2":
        case "serial4":
        case "oid":
            return rawValue.asInteger();
        case "bigint":
        case "bigserial":
        case "int8":
            return rawValue.asLong();
        case "real":
        case "float4":
            return rawValue.isNumber() ? rawValue.asFloat() : Float.valueOf(rawValue.asString());
        case "double precision":
        case "float8":
            return rawValue.isNumber() ? rawValue.asDouble() : Double.valueOf(rawValue.asString());
        case "numeric":
        case "decimal":
            if (rawValue.isInteger()) {
                return new SpecialValueDecimal(new BigDecimal(rawValue.asInteger()));
            } else if (rawValue.isLong()) {
                return new SpecialValueDecimal(new BigDecimal(rawValue.asLong()));
            } else if (rawValue.isBigInteger()) {
                return new SpecialValueDecimal(new BigDecimal(rawValue.asBigInteger()));
            }
            return SpecialValueDecimal.valueOf(rawValue.asString());
        case "character":
        case "char":
        case "character varying":
        case "varchar":
        case "bpchar":
        case "text":
            return rawValue.asString();
        case "date":
            return DateTimeFormat.get().date(rawValue.asString());
        case "timestamp with time zone":
        case "timestamptz":
            return DateTimeFormat.get().timestampWithTimeZone(rawValue.asString());
        case "timestamp":
        case "timestamp without time zone":
            return DateTimeFormat.get().timestamp(rawValue.asString());
        case "time":
        case "time without time zone":
            return DateTimeFormat.get().time(rawValue.asString());
        case "time with time zone":
        case "timetz":
            return DateTimeFormat.get().timeWithTimeZone(rawValue.asString());
        case "bytea":
            return Strings.hexStringToByteArray(rawValue.asString());
        // i.e. those values won't actually be propagated to the outbound message until that's the case
        case "box":
            try {
                return new PGbox(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "circle":
            try {
                return new PGcircle(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse circle {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "interval":
            try {
                return new PGInterval(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "line":
            try {
                return new PGline(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "lseg":
            try {
                return new PGlseg(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "money":
            try {
                return new PGmoney(rawValue.asString()).val;
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse money {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "path":
            try {
                return new PGpath(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "point":
            try {
                return new PGpoint(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        case "polygon":
            try {
                return new PGpolygon(rawValue.asString());
            } catch (final SQLException e) {
                LOGGER.error("Failed to parse point {}, {}", rawValue.asString(), e);
                throw new ConnectException(e);
            }
        // ValueConverter turns them into the correct types
        case "geometry":
        case "geography":
            return rawValue.asString();
        case "bit":
        case "bit varying":
        case "varbit":
        case "json":
        case "jsonb":
        case "xml":
        case "uuid":
        case "tstzrange":
            return rawValue.asString();
        // TODO: improve with more specific/useful classes here?
        case "cidr":
        case "inet":
        case "macaddr":
        case "macaddr8":
        case "pg_lsn":
        case "tsquery":
        case "tsvector":
        case "txid_snapshot":
        // catch-all for unknown (extension module/custom) types
        default:
            break;
    }
    if (includeUnknownDatatypes) {
        // this includes things like PostGIS geometries or other custom types.
        // leave up to the downstream message recipient to deal with.
        LOGGER.debug("processing column '{}' with unknown data type '{}' as byte array", columnName, fullType);
        return rawValue.asString();
    }
    LOGGER.debug("Unknown column type {} for column {} – ignoring", fullType, columnName);
    return null;
}
Also used : PGpoint(org.postgresql.geometric.PGpoint) PGcircle(org.postgresql.geometric.PGcircle) SQLException(java.sql.SQLException) PGmoney(org.postgresql.util.PGmoney) PGpolygon(org.postgresql.geometric.PGpolygon) BigDecimal(java.math.BigDecimal) PGInterval(org.postgresql.util.PGInterval) PGline(org.postgresql.geometric.PGline) SpecialValueDecimal(io.debezium.data.SpecialValueDecimal) PGpath(org.postgresql.geometric.PGpath) PgArray(org.postgresql.jdbc.PgArray) PGbox(org.postgresql.geometric.PGbox) ConnectException(org.apache.kafka.connect.errors.ConnectException) PGlseg(org.postgresql.geometric.PGlseg)

Example 2 with PGpolygon

use of org.postgresql.geometric.PGpolygon in project TrakEM2 by trakem2.

the class DBLoader method fetchBezierArrays.

/**
 * Get the bezier points from the database for the given profile but as a triple array of points, that is, three arrays with 2 arrays (x and y) each.
 */
public double[][][] fetchBezierArrays(long id) {
    synchronized (db_lock) {
        // connect if disconnected
        if (!connectToDatabase()) {
            return null;
        }
        releaseMemory2();
        PGpolygon p = null;
        try {
            ResultSet r = connection.prepareStatement("SELECT id, polygon FROM ab_profiles WHERE id=" + id).executeQuery();
            if (r.next()) {
                p = (PGpolygon) r.getObject("polygon");
            }
            r.close();
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
        return toBezierArrays(p);
    }
}
Also used : PGpolygon(org.postgresql.geometric.PGpolygon) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Example 3 with PGpolygon

use of org.postgresql.geometric.PGpolygon in project TrakEM2 by trakem2.

the class DBLoader method makePGpolygon.

private PGpolygon makePGpolygon(final ArrayList al_points) {
    final PGpoint[] pol = new PGpoint[al_points.size()];
    for (int i = 0; i < pol.length; i++) {
        Point p = (Point) al_points.get(i);
        pol[i] = new PGpoint(p.x, p.y);
    }
    return new PGpolygon(pol);
}
Also used : PGpoint(org.postgresql.geometric.PGpoint) PGpolygon(org.postgresql.geometric.PGpolygon) Point(java.awt.Point) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint)

Example 4 with PGpolygon

use of org.postgresql.geometric.PGpolygon in project TrakEM2 by trakem2.

the class DBLoader method makePGpolygon.

private PGpolygon makePGpolygon(final double[][][] bezarr) {
    final PGpoint[] points = new PGpoint[bezarr[0][0].length * 3];
    final double[][] p_l = bezarr[0];
    final double[][] p = bezarr[1];
    final double[][] p_r = bezarr[2];
    for (int i = 0, j = 0; i < points.length; i += 3, j++) {
        points[i] = new PGpoint(p_l[0][j], p_l[1][j]);
        points[i + 1] = new PGpoint(p[0][j], p[1][j]);
        points[i + 2] = new PGpoint(p_r[0][j], p_r[1][j]);
    }
    return new PGpolygon(points);
}
Also used : PGpoint(org.postgresql.geometric.PGpoint) PGpolygon(org.postgresql.geometric.PGpolygon) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint)

Example 5 with PGpolygon

use of org.postgresql.geometric.PGpolygon in project TrakEM2 by trakem2.

the class DBLoader method fetchArea.

public Area fetchArea(long area_list_id, long layer_id) {
    synchronized (db_lock) {
        // connect if disconnected
        if (!connectToDatabase()) {
            return null;
        }
        releaseMemory2();
        Area area = new Area();
        try {
            ResultSet r = connection.createStatement().executeQuery(new StringBuffer("SELECT * from ab_area_paths WHERE area_list_id=").append(area_list_id).append(" AND layer_id=").append(layer_id).toString());
            while (r.next()) {
                PGpolygon pol = (PGpolygon) r.getObject("polygon");
                area.add(new Area(makePolygon(pol)));
            }
            r.close();
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
        return area;
    }
}
Also used : Area(java.awt.geom.Area) PGpolygon(org.postgresql.geometric.PGpolygon) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Aggregations

PGpolygon (org.postgresql.geometric.PGpolygon)5 SQLException (java.sql.SQLException)3 PGpoint (org.postgresql.geometric.PGpoint)3 Point (java.awt.Point)2 ResultSet (java.sql.ResultSet)2 SpecialValueDecimal (io.debezium.data.SpecialValueDecimal)1 Area (java.awt.geom.Area)1 BigDecimal (java.math.BigDecimal)1 ConnectException (org.apache.kafka.connect.errors.ConnectException)1 PGbox (org.postgresql.geometric.PGbox)1 PGcircle (org.postgresql.geometric.PGcircle)1 PGline (org.postgresql.geometric.PGline)1 PGlseg (org.postgresql.geometric.PGlseg)1 PGpath (org.postgresql.geometric.PGpath)1 PgArray (org.postgresql.jdbc.PgArray)1 PGInterval (org.postgresql.util.PGInterval)1 PGmoney (org.postgresql.util.PGmoney)1