package ca.uhn.fhir.jpa.migrate.taskdef;

/*-
 * #%L
 * HAPI FHIR JPA Server - Migration
 * %%
 * Copyright (C) 2014 - 2020 University Health Network
 * %%
 * 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.
 * #L%
 */

import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

public class DropTableTask extends BaseTableTask {

	private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class);

	public DropTableTask(String theProductVersion, String theSchemaVersion) {
		super(theProductVersion, theSchemaVersion);
	}

	@Override
	public void validate() {
		super.validate();
		setDescription("Drop table " + getTableName());
	}

	@Override
	public void doExecute() throws SQLException {
		Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
		if (!tableNames.contains(getTableName())) {
			return;
		}

		Set<String> foreignKeys = JdbcUtils.getForeignKeys(getConnectionProperties(), null, getTableName());
		logInfo(ourLog, "Table {} has the following foreign keys: {}", getTableName(), foreignKeys);

		Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName());
		logInfo(ourLog, "Table {} has the following indexes: {}", getTableName(), indexNames);

		for (String next : foreignKeys) {
			List<String> sql = DropForeignKeyTask.generateSql(getTableName(), next, getDriverType());
			for (@Language("SQL") String nextSql : sql) {
				executeSql(getTableName(), nextSql);
			}
		}

		for (String nextIndex : indexNames) {
			List<String> sqls = DropIndexTask.createDropIndexSql(getConnectionProperties(), getTableName(), nextIndex, getDriverType());
			if (!sqls.isEmpty()) {
				logInfo(ourLog, "Dropping index {} on table {} in preparation for table delete", nextIndex, getTableName());
			}
			for (@Language("SQL") String sql : sqls) {
				executeSql(getTableName(), sql);
			}
		}

		logInfo(ourLog, "Dropping table: {}", getTableName());

		@Language("SQL")
		String sql = "DROP TABLE " + getTableName();
		executeSql(getTableName(), sql);

	}


}

@@ -2131,405 +2131,340 @@
 %0A%0A%09%09
-for (String nextIndex : indexNames) %7B%0A%09%09%09List%3CString%3E sqls = DropIndexTask.createDropIndexSql(getConnectionProperties(), getTableName(), nextIndex, getDriverType());%0A%09%09%09if (!sqls.isEmpty()) %7B%0A%09%09%09%09logInfo(ourLog, %22Dropping index %7B%7D on table %7B%7D in preparation for table delete%22, nextIndex, getTableName());%0A%09%09%09%7D%0A%09%09%09for (@Language(%22SQL%22) String sql : sqls) %7B
+DropIndexTask theIndexTask = new DropIndexTask(getMyProductVersion(), getMySchemaVersion());%0A%09%09theIndexTask%0A%09%09%09.setTableName(getTableName())%0A%09%09%09.setConnectionProperties(getConnectionProperties())%0A%09%09%09.setDriverType(getDriverType());%0A%09%09for (String nextIndex : indexNames) %7B%0A%09%09%09theIndexTask%0A%09%09%09%09.setIndexName(nextIndex)
 %0A%09%09%09%09
+.
 execute
-Sql(getTableName(), sql);%0A%09%09%09%7D
+();
 %0A%09%09%7D

replace fetching of query strings with running of tasks