Search in sources :

Example 11 with RepositoryException

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

the class PostgresHeaderRepository method delete.

/**
 * {@inheritDoc}
 */
@Override
public void delete(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(delete)) {
        for (Long key : keys) {
            statement.clearParameters();
            statement.setObject(1, key);
            statement.addBatch();
        }
        statement.executeBatch();
    } 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 12 with RepositoryException

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

the class PostgresNodeRepository method delete.

/**
 * {@inheritDoc}
 */
@Override
public void delete(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(delete)) {
        for (Long key : keys) {
            statement.clearParameters();
            statement.setObject(1, key);
            statement.addBatch();
        }
        statement.executeBatch();
    } 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 13 with RepositoryException

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

the class PostgresNodeRepository method get.

/**
 * {@inheritDoc}
 */
@Override
public List<Node> 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, Node> values = new HashMap<>();
            while (result.next()) {
                Node 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 : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Node(com.baremaps.osm.domain.Node) 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 14 with RepositoryException

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

the class PostgresNodeRepository method copy.

/**
 * {@inheritDoc}
 */
@Override
public void copy(List<Node> 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 (Node value : values) {
                writer.startRow(9);
                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.writeDouble(value.getLon());
                writer.writeDouble(value.getLat());
                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) Node(com.baremaps.osm.domain.Node) Connection(java.sql.Connection) PGConnection(org.postgresql.PGConnection) RepositoryException(com.baremaps.osm.repository.RepositoryException) IOException(java.io.IOException) CopyWriter(com.baremaps.postgres.jdbc.CopyWriter)

Example 15 with RepositoryException

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

the class PostgresRelationRepository method delete.

/**
 * {@inheritDoc}
 */
@Override
public void delete(List<Long> keys) throws RepositoryException {
    if (keys.isEmpty()) {
        return;
    }
    try (Connection connection = dataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(delete)) {
        for (Long key : keys) {
            statement.clearParameters();
            statement.setObject(1, key);
            statement.addBatch();
        }
        statement.executeBatch();
    } 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)

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