/** * Creates a new instance. * * @param loginCredentials * Contains all information required to connect to the database. * @param preferences * Contains preferences configuring database behaviour. */ public PostgreSqlTruncator(DatabaseLoginCredentials loginCredentials, DatabasePreferences preferences) { dbCtx = new DatabaseContext(loginCredentials); schemaVersionValidator = new SchemaVersionValidator(dbCtx, preferences); }
/** * Truncates all data from the database. */ public void run() { try { schemaVersionValidator.validateVersion(PostgreSqlVersionConstants.SCHEMA_VERSION); LOG.fine("Truncating tables."); for (int i = 0; i < SQL_TABLE_NAMES.length; i++) { if (dbCtx.doesTableExist(SQL_TABLE_NAMES[i])) { LOG.finer("Truncating table " + SQL_TABLE_NAMES[i] + "."); dbCtx.executeStatement("TRUNCATE " + SQL_TABLE_NAMES[i]); } else { LOG.finer("Skipping table " + SQL_TABLE_NAMES[i] + " which doesn't exist in the current schema."); } } LOG.fine("Committing changes."); dbCtx.commit(); LOG.fine("Vacuuming database."); dbCtx.setAutoCommit(true); dbCtx.executeStatement("VACUUM ANALYZE"); LOG.fine("Complete."); } finally { dbCtx.close(); } } }
private void initialize() { if (!initialized) { isActionSupported = dbCtx.doesTableExist("actions"); isWayBboxSupported = dbCtx.doesColumnExist("ways", "bbox"); isWayLinestringSupported = dbCtx.doesColumnExist("ways", "linestring"); initialized = true; } }
try (DatabaseContext dbCtx = new DatabaseContext(loginCredentials)) { IndexManager indexManager; dbCtx.commit(); dbCtx.setAutoCommit(true); dbCtx.executeStatement("VACUUM ANALYZE");
dbCtx.commit(); dbCtx.setAutoCommit(true); dbCtx.executeStatement("VACUUM ANALYZE");
/** * Creates a new database statement. * * @return The newly created statement. */ public Statement createStatement() { try { Statement resultStatement; LOG.finest("Creating a new statement."); resultStatement = getConnection().createStatement(); return resultStatement; } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to create database statement.", e); } }
LOG.finer("Creating a new database connection from JNDI."); connection = getConnectionFromDatasource(); connection = getConnectionFromDriverManager();
dbCtx.executeStatement("SET enable_seqscan = false"); dbCtx.executeStatement("SET enable_mergejoin = false"); dbCtx.executeStatement("SET enable_hashjoin = false"); dbCtx.executeStatement("CREATE TEMPORARY TABLE box_node_list (id bigint PRIMARY KEY) ON COMMIT DROP"); dbCtx.executeStatement("CREATE TEMPORARY TABLE box_way_list (id bigint PRIMARY KEY) ON COMMIT DROP"); dbCtx.executeStatement("CREATE TEMPORARY TABLE box_relation_list (id bigint PRIMARY KEY) ON COMMIT DROP"); preparedStatement = dbCtx.prepareStatement( "INSERT INTO box_node_list SELECT id FROM nodes WHERE (geom && ?)"); prmIndex = 1; preparedStatement = dbCtx.prepareStatement( "INSERT INTO box_way_list " + "SELECT id FROM ways w where w.linestring && ?" preparedStatement = dbCtx.prepareStatement( "INSERT INTO box_way_list " + "SELECT way_id FROM (" preparedStatement = dbCtx.prepareStatement( "INSERT INTO box_way_list " + "SELECT wn.way_id FROM way_nodes wn INNER JOIN box_node_list n ON wn.node_id = n.id" preparedStatement = dbCtx.prepareStatement( "INSERT INTO box_relation_list (" + "SELECT rm.relation_id AS relation_id FROM relation_members rm"
/** * Enforces cleanup of any remaining resources during garbage collection. * This is a safeguard and should not be required if release is called * appropriately. * * @throws Throwable * if an unexpected problem occurs during finalization. */ @Override protected void finalize() throws Throwable { close(); super.finalize(); } }
/** * {@inheritDoc} */ @Override protected ResultSet createResultSet(DatabaseContext queryDbCtx) { return queryDbCtx.executeQuery(sql); }
/** * {@inheritDoc} */ @Override public void complete() { if (dbCtx != null) { dbCtx.commit(); } }
for (int i = 0; i < POST_LOAD_SQL.length; i++) { LOG.finer("SQL: " + POST_LOAD_SQL[i]); dbCtx.executeStatement(POST_LOAD_SQL[i]); if (populateBbox) { LOG.finer("SQL: " + POST_LOAD_SQL_POPULATE_WAY_BBOX); dbCtx.executeStatement(POST_LOAD_SQL_POPULATE_WAY_BBOX); dbCtx.executeStatement(POST_LOAD_SQL_WAY_BBOX[i]); if (populateLinestring) { LOG.finer("SQL: " + POST_LOAD_SQL_POPULATE_WAY_LINESTRING); dbCtx.executeStatement(POST_LOAD_SQL_POPULATE_WAY_LINESTRING); dbCtx.executeStatement(POST_LOAD_SQL_WAY_LINESTRING[i]);
int dbVersion; statement = dbCtx.createStatement(); resultSet = statement.executeQuery(SELECT_SQL);
/** * Creates a new database callable statement. * * @param sql * The statement to be created. * @return The newly created statement. */ public CallableStatement prepareCall(String sql) { try { CallableStatement callableStatement; LOG.finest("Creating callable statement {" + sql + "}"); callableStatement = getConnection().prepareCall(sql); return callableStatement; } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to create database callable statement.", e); } }
/** * {@inheritDoc} */ public void close() { changeWriter.release(); dbCtx.close(); } }
/** * {@inheritDoc} */ @Override protected ResultSet createResultSet(DatabaseContext queryDbCtx) { return queryDbCtx.executeQuery(sql); }
/** * {@inheritDoc} */ public void complete() { changeWriter.complete(); dbCtx.commit(); }
/** * Drops indexes and constraints in the database. */ public void prepareForLoad() { LOG.fine("Running pre-load SQL statements."); for (int i = 0; i < PRE_LOAD_SQL.length; i++) { LOG.finer("SQL: " + PRE_LOAD_SQL[i]); dbCtx.executeStatement(PRE_LOAD_SQL[i]); } if (capabilityChecker.isWayBboxSupported()) { LOG.fine("Running pre-load bbox SQL statements."); for (int i = 0; i < PRE_LOAD_SQL_WAY_BBOX.length; i++) { LOG.finer("SQL: " + PRE_LOAD_SQL_WAY_BBOX[i]); dbCtx.executeStatement(PRE_LOAD_SQL_WAY_BBOX[i]); } } if (capabilityChecker.isWayLinestringSupported()) { LOG.fine("Running pre-load linestring SQL statements."); for (int i = 0; i < PRE_LOAD_SQL_WAY_LINESTRING.length; i++) { LOG.finer("SQL: " + PRE_LOAD_SQL_WAY_LINESTRING[i]); dbCtx.executeStatement(PRE_LOAD_SQL_WAY_LINESTRING[i]); } } LOG.fine("Pre-load SQL statements complete."); }
/** * Indicates if the specified column exists in the database. * * @param tableName The table to check for. * @param columnName The column to check for. * @return True if the column exists, false otherwise. */ public boolean doesColumnExist(String tableName, String columnName) { LOG.finest("Checking if column {" + columnName + "} in table {" + tableName + "} exists."); try (ResultSet resultSet = getConnection().getMetaData().getColumns(null, null, tableName, columnName)) { return resultSet.next(); } catch (SQLException e) { throw new OsmosisRuntimeException( "Unable to check for the existence of column " + tableName + "." + columnName + ".", e ); } }
/** * Creates a new instance. * * @param loginCredentials * Contains all information required to connect to the database. * @param preferences * Contains preferences configuring database behaviour. */ public PostgreSqlChangeWriter(DatabaseLoginCredentials loginCredentials, DatabasePreferences preferences) { dbCtx = new DatabaseContext(loginCredentials); changeWriter = new ChangeWriter(dbCtx); actionWriterMap = new HashMap<ChangeAction, ActionChangeWriter>(); actionWriterMap.put(ChangeAction.Create, new ActionChangeWriter(changeWriter, ChangeAction.Create)); actionWriterMap.put(ChangeAction.Modify, new ActionChangeWriter(changeWriter, ChangeAction.Modify)); actionWriterMap.put(ChangeAction.Delete, new ActionChangeWriter(changeWriter, ChangeAction.Delete)); schemaVersionValidator = new SchemaVersionValidator(dbCtx, preferences); }