package com.splicemachine.test_dao;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

/**
 * Manipulate tables.
 */
public class TableDAO {

    private JDBCTemplate jdbcTemplate;

    public TableDAO(Connection connection) {
        this.jdbcTemplate = new JDBCTemplate(connection);
    }

    /**
     * Attempts to drop each table in the order passed, ignores any exceptions.
     */
    public void deleteTableForce(String... tableNames) {
        for (String tableName : tableNames) {
            try {
                jdbcTemplate.executeUpdate("drop table " + tableName);
            } catch (Exception e) {
                /* ignore per method contract */
            }
        }
    }

    /**
     * Drop the given table or view.
     */
    public void drop(String schemaName, String tableName, boolean isView) {
        try {
            DatabaseMetaData metaData = jdbcTemplate.getConnection().getMetaData();
            ResultSet rs = metaData.getTables(null, schemaName.toUpperCase(), tableName.toUpperCase(), null);
            if (rs.next()) {
                if (isView) {
                    jdbcTemplate.executeUpdate(String.format("drop view %s.%s", schemaName.toUpperCase(), tableName.toUpperCase()));
                } else {
                    jdbcTemplate.executeUpdate(String.format("drop table if exists %s.%s", schemaName.toUpperCase(), tableName.toUpperCase()));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void drop(String schemaName, String tableName) {
        drop(schemaName, tableName, false);
    }

}

@@ -118,16 +118,79 @@
 sultSet;
+%0Aimport java.util.List;%0A%0Aimport static java.lang.String.format;
 %0A%0A/**%0A *
@@ -407,78 +407,32 @@
   * 
-Attempts to drop each table in the order passed, ignores any exception
+Drop the specified table
 s.%0A 
@@ -459,24 +459,31 @@
 id d
-eleteTableForce(
+rop(String schemaName, 
 Stri
@@ -564,183 +564,43 @@
     
-try %7B%0A                jdbcTemplate.executeUpdate(%22drop table %22 + tableName);%0A            %7D catch (Exception e) %7B%0A                /* ignore per method contract */%0A            %7D
+drop(schemaName, tableName, false);
 %0A   
@@ -1048,39 +1048,32 @@
 e.executeUpdate(
-String.
 format(%22drop vie
@@ -1096,47 +1096,19 @@
 Name
-.toUpperCase(), tableName.toUpperCase()
+, tableName
 ));%0A
@@ -1179,23 +1179,16 @@
 eUpdate(
-String.
 format(%22
@@ -1230,275 +1230,2011 @@
 Name
-.toUpperCase(), tableName.toUpperCase()));%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    public void drop(String schemaName, String tableName) %7B%0A        drop(schemaName, tableName, fals
+, tableName));%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A            // Drop failed because of FK constraint.%0A            if (e.getMessage().contains(%22DROP CONSTRAINT%22) && e.getMessage().contains(%22is dependent on that object%22)) %7B%0A                dropTableRecursive(schemaName, tableName);%0A            %7D else %7B%0A                throw new RuntimeException(e);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Delete the specified table, deleting any tables with FKs referencing it first, recursively.%0A     */%0A    public void dropTableRecursive(String schemaName, String tableName) %7B%0A        try %7B%0A            List%3CString%3E dependentTableNames = getDependentTableNames(schemaName, tableName);%0A            for (String dependentTableName : dependentTableNames) %7B%0A                dropTableRecursive(schemaName, dependentTableName);%0A            %7D%0A            jdbcTemplate.executeUpdate(format(%22drop table if exists %25s.%25s%22, schemaName, tableName));%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Returns a list of table names in the specified schema that have FKs referencing the specified table.%0A     */%0A    private List%3CString%3E getDependentTableNames(String schemaName, String tableName) %7B%0A        String sql = %22%22 +%0A                %22select t.tablename %22 +%0A                %22 from sys.systables t %22 +%0A                %22  join sys.SYSCONSTRAINTS c on c.tableid = t.tableid %22 +%0A                %22  where c.constraintid in ( %22 +%0A                %22    select fk.CONSTRAINTID %22 +%0A                %22    from sys.sysforeignkeys fk %22 +%0A                %22      join sys.sysconstraints c on fk.keyconstraintid = c.constraintid %22 +%0A                %22      join sys.systables      t on t.tableid  = c.tableid %22 +%0A                %22      join sys.sysschemas     s on s.schemaid = t.schemaid %22 +%0A                %22    where s.SCHEMANAME=? %22 +%0A                %22          and t.tablename=?      )%22;%0A        return jdbcTemplate.query(sql, schemaName, tableNam
 e);%0A

Add a method to TableDAO to delete tables recursively, accounting for FKs.