Search in sources :

Example 1 with RepositoryException

use of com.baremaps.osm.repository.RepositoryException in project baremaps by baremaps.

the class PostgresRelationRepository method get.

/**
 * {@inheritDoc}
 */
@Override
public List<Relation> get(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return List.of();
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(selectIn)) {
        statement.setArray(1, connection.createArrayOf("int8", keys.toArray()));
        try (ResultSet result = statement.executeQuery()) {
            Map<Long, Relation> values = new HashMap<>();
            while (result.next()) {
                Relation value = getValue(result);
                values.put(value.getId(), value);
            }
            return keys.stream().map(values::get).collect(Collectors.toList());
        }
    } catch (SQLException | JsonProcessingException e) {
        throw new RepositoryException(e);
    }
}
Also used : Relation(com.baremaps.osm.domain.Relation) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with RepositoryException

use of com.baremaps.osm.repository.RepositoryException in project baremaps by baremaps.

the class PostgresRelationRepository method put.

/**
 * {@inheritDoc}
 */
@Override
public void put(List<Relation> values) throws RepositoryException {
    if (values.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(insert)) {
        for (Relation value : values) {
            statement.clearParameters();
            setValue(statement, value);
            statement.addBatch();
        }
        statement.executeBatch();
    } catch (SQLException | JsonProcessingException e) {
        throw new RepositoryException(e);
    }
}
Also used : Relation(com.baremaps.osm.domain.Relation) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with RepositoryException

use of com.baremaps.osm.repository.RepositoryException in project baremaps by baremaps.

the class PostgresRelationRepository method delete.

/**
 * {@inheritDoc}
 */
@Override
public void delete(Long key) throws RepositoryException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(delete)) {
        statement.setObject(1, key);
        statement.execute();
    } catch (SQLException e) {
        throw new RepositoryException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException)

Example 4 with RepositoryException

use of com.baremaps.osm.repository.RepositoryException in project baremaps by baremaps.

the class PostgresWayRepository method copy.

/**
 * {@inheritDoc}
 */
public void copy(List<Way> values) throws RepositoryException {
    if (values.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection()) {
        PGConnection pgConnection = connection.unwrap(PGConnection.class);
        try (CopyWriter writer = new CopyWriter(new PGCopyOutputStream(pgConnection, copy))) {
            writer.writeHeader();
            for (Way value : values) {
                writer.startRow(8);
                writer.writeLong(value.getId());
                writer.writeInteger(value.getInfo().getVersion());
                writer.writeInteger(value.getInfo().getUid());
                writer.writeLocalDateTime(value.getInfo().getTimestamp());
                writer.writeLong(value.getInfo().getChangeset());
                writer.writeJsonb(toJson(value.getTags()));
                writer.writeLongList(value.getNodes());
                writer.writeGeometry(value.getGeometry());
            }
        }
    } catch (IOException | SQLException e) {
        throw new RepositoryException(e);
    }
}
Also used : PGConnection(org.postgresql.PGConnection) PGCopyOutputStream(org.postgresql.copy.PGCopyOutputStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) RepositoryException(com.baremaps.osm.repository.RepositoryException) IOException(java.io.IOException) Way(com.baremaps.osm.domain.Way) CopyWriter(com.baremaps.postgres.jdbc.CopyWriter)

Example 5 with RepositoryException

use of com.baremaps.osm.repository.RepositoryException in project baremaps by baremaps.

the class PostgresWayRepository method put.

/**
 * {@inheritDoc}
 */
@Override
public void put(Way value) throws RepositoryException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(insert)) {
        setValue(statement, value);
        statement.execute();
    } catch (SQLException | JsonProcessingException e) {
        throw new RepositoryException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(com.baremaps.osm.repository.RepositoryException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

RepositoryException (com.baremaps.osm.repository.RepositoryException)25 Connection (java.sql.Connection)25 SQLException (java.sql.SQLException)25 PGConnection (org.postgresql.PGConnection)25 PreparedStatement (java.sql.PreparedStatement)21 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 ResultSet (java.sql.ResultSet)5 Header (com.baremaps.osm.domain.Header)4 CopyWriter (com.baremaps.postgres.jdbc.CopyWriter)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 PGCopyOutputStream (org.postgresql.copy.PGCopyOutputStream)4 Node (com.baremaps.osm.domain.Node)3 Relation (com.baremaps.osm.domain.Relation)3 Way (com.baremaps.osm.domain.Way)3 Member (com.baremaps.osm.domain.Member)1 MemberType (com.baremaps.osm.domain.Member.MemberType)1 ArrayList (java.util.ArrayList)1