HSQLDBSchema.java
/*-
* ========================LICENSE_START=================================
* flyway-database-hsqldb
* ========================================================================
* Copyright (C) 2010 - 2025 Red Gate Software Ltd
* ========================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package org.flywaydb.database.hsqldb;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.database.base.Table;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* HSQLDB implementation of Schema.
*/
public class HSQLDBSchema extends Schema<HSQLDBDatabase, HSQLDBTable> {
/**
* Creates a new Hsql schema.
*
* @param jdbcTemplate The Jdbc Template for communicating with the DB.
* @param database The database-specific support.
* @param name The name of the schema.
*/
HSQLDBSchema(JdbcTemplate jdbcTemplate, HSQLDBDatabase database, String name) {
super(jdbcTemplate, database, name);
}
@Override
protected boolean doExists() throws SQLException {
return jdbcTemplate.queryForInt("SELECT COUNT (*) FROM information_schema.system_schemas WHERE table_schem=?", name) > 0;
}
@Override
protected boolean doEmpty() {
return allTables().length == 0;
}
@Override
protected void doCreate() throws SQLException {
String user = jdbcTemplate.queryForString("SELECT USER() FROM (VALUES(0))");
jdbcTemplate.execute("CREATE SCHEMA " + database.quote(name) + " AUTHORIZATION " + user);
}
@Override
protected void doDrop() throws SQLException {
jdbcTemplate.execute("DROP SCHEMA " + database.quote(name) + " CASCADE");
}
@Override
protected void doClean() throws SQLException {
for (Table table : allTables()) {
table.drop();
}
for (String statement : generateDropStatementsForSequences()) {
jdbcTemplate.execute(statement);
}
}
/**
* Generates the statements to drop the sequences in this schema.
*
* @return The drop statements.
* @throws SQLException when the drop statements could not be generated.
*/
private List<String> generateDropStatementsForSequences() throws SQLException {
List<String> sequenceNames = jdbcTemplate.queryForStringList(
"SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SYSTEM_SEQUENCES where SEQUENCE_SCHEMA = ?", name);
List<String> statements = new ArrayList<>();
for (String seqName : sequenceNames) {
statements.add("DROP SEQUENCE " + database.quote(name, seqName));
}
return statements;
}
@Override
protected HSQLDBTable[] doAllTables() throws SQLException {
List<String> tableNames = jdbcTemplate.queryForStringList(
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_SCHEM = ? AND TABLE_TYPE = 'TABLE'", name);
HSQLDBTable[] tables = new HSQLDBTable[tableNames.size()];
for (int i = 0; i < tableNames.size(); i++) {
tables[i] = new HSQLDBTable(jdbcTemplate, database, this, tableNames.get(i));
}
return tables;
}
@Override
public Table getTable(String tableName) {
return new HSQLDBTable(jdbcTemplate, database, this, tableName);
}
}