/*
 * Copyright (C) Red Gate Software Ltd 2010-2022
 *
 * 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.
 */
package org.flywaydb.core.internal.database.snowflake;

import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.jdbc.RowMapper;

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

public class SnowflakeSchema extends Schema<SnowflakeDatabase, SnowflakeTable> {
    /**
     * Creates a new Snowflake schema.
     *
     * @param jdbcTemplate The Jdbc Template for communicating with the DB.
     * @param database The database-specific support.
     * @param name The name of the schema.
     */
    SnowflakeSchema(JdbcTemplate jdbcTemplate, SnowflakeDatabase database, String name) {
        super(jdbcTemplate, database, name);
    }

    @Override
    protected boolean doExists() throws SQLException {
        List<Boolean> results = jdbcTemplate.query("SHOW SCHEMAS LIKE '" + name + "'", rs -> true);
        return !results.isEmpty();
    }

    @Override
    protected boolean doEmpty() throws SQLException {
        int objectCount = getObjectCount("TABLE") + getObjectCount("VIEW")
                + getObjectCount("SEQUENCE");

        return objectCount == 0;
    }

    private int getObjectCount(String objectType) throws SQLException {
        return jdbcTemplate.query("SHOW " + objectType + "S IN SCHEMA " + database.quote(name), rs -> 1).size();
    }

    @Override
    protected void doCreate() throws SQLException {
        jdbcTemplate.execute("CREATE SCHEMA " + database.quote(name));
    }

    @Override
    protected void doDrop() throws SQLException {
        jdbcTemplate.execute("DROP SCHEMA " + database.quote(name));
    }

    @Override
    protected void doClean() throws SQLException {
        for (String dropStatement : generateDropStatements("VIEW")) {
            jdbcTemplate.execute(dropStatement);
        }

        for (String dropStatement : generateDropStatements("TABLE")) {
            jdbcTemplate.execute(dropStatement);
        }

        for (String dropStatement : generateDropStatements("SEQUENCE")) {
            jdbcTemplate.execute(dropStatement);
        }

        for (String dropStatement : generateDropStatementsWithArgs("USER FUNCTIONS", "FUNCTION")) {
            jdbcTemplate.execute(dropStatement);
        }

        for (String dropStatement : generateDropStatementsWithArgs("PROCEDURES", "PROCEDURE")) {
            jdbcTemplate.execute(dropStatement);
        }
    }

    @Override
    protected SnowflakeTable[] doAllTables() throws SQLException {
        List<SnowflakeTable> tables = jdbcTemplate.query("SHOW TABLES IN SCHEMA " + database.quote(name), new RowMapper<SnowflakeTable>() {
            @Override
            public SnowflakeTable mapRow(ResultSet rs) throws SQLException {
                String tableName = rs.getString("name");
                return (SnowflakeTable) getTable(tableName);
            }
        });
        return tables.toArray(new SnowflakeTable[0]);
    }

    @Override
    public Table getTable(String tableName) {
        return new SnowflakeTable(jdbcTemplate, database, this, tableName);
    }

    private List<String> generateDropStatements(final String objectType) throws SQLException {
        return jdbcTemplate.query("SHOW " + objectType + "S IN SCHEMA " + database.quote(name), rs -> {
            String tableName = rs.getString("name");
            return "DROP " + objectType + " " + database.quote(name) + "." + database.quote(tableName);
        });
    }

    private List<String> generateDropStatementsWithArgs(final String showObjectType, final String dropObjectType) throws SQLException {
        return jdbcTemplate.query("SHOW " + showObjectType + " IN SCHEMA " + database.quote(name), rs -> {
            String nameAndArgsList = rs.getString("arguments");
            int indexOfEndOfArgs = nameAndArgsList.indexOf(") RETURN ");
            String functionName = nameAndArgsList.substring(0, indexOfEndOfArgs + 1);
            return "DROP " + dropObjectType + " " + name + "." + functionName;
        });
    }
}
@@ -667,16 +667,41 @@
 flake;%0A%0A
+import lombok.CustomLog;%0A
 import o
@@ -905,16 +905,61 @@
 wMapper;
+%0Aimport org.flywaydb.core.internal.util.Pair;
 %0A%0Aimport
@@ -1033,16 +1033,27 @@
 .List;%0A%0A
+@CustomLog%0A
 public c
@@ -2892,36 +2892,54 @@
    for (
+Pair%3C
 String
- dropStatement
+, String%3E snowflakeDropPair
  : gener
@@ -3013,44 +3013,269 @@
     
-jdbcTemplate.execute(dropStatement);
+try %7B%0A                jdbcTemplate.execute(snowflakeDropPair.getRight());%0A            %7D catch (SQLException sqlException) %7B%0A                LOG.warn(%22Unable to drop User Function %22 + snowflakeDropPair.getLeft() + %22: %22 + sqlException.getMessage());%0A            %7D
 %0A   
@@ -3295,36 +3295,54 @@
    for (
+Pair%3C
 String
- dropStatement
+, String%3E snowflakeDropPair
  : gener
@@ -3413,44 +3413,265 @@
     
-jdbcTemplate.execute(dropStatement);
+try %7B%0A                jdbcTemplate.execute(snowflakeDropPair.getRight());%0A            %7D catch (SQLException sqlException) %7B%0A                LOG.warn(%22Unable to drop Procedure %22 + snowflakeDropPair.getLeft() + %22: %22 + sqlException.getMessage());%0A            %7D
 %0A   
@@ -4736,38 +4736,51 @@
    private List%3C
+Pair%3C
 String
+,String%3E
 %3E generateDropSt
@@ -5216,32 +5216,54 @@
           return
+ Pair.of(functionName,
  %22DROP %22 + dropO
@@ -5305,16 +5305,17 @@
 tionName
+)
 ;%0A      

Handle errors (with warning) when cleaning procs on snowflake