import { P_DROP_TABLE } from '../../../../../typings' ; import { DatabaseModelInterface , TableModelInterface , RuleHandler } from '../typings' ; /**
 * Formatter for P_DROP_TABLE rule's parsed JSON.
 */ export class DropTable implements RuleHandler { database ! : DatabaseModelInterface ; param name getTable ( name ) { return this . database . getTable ( name ) ; } param database setDatabase ( database ) { this . database = database ; } param json handleDef ( json ) { if ( json . id === 'P_DROP_TABLE' ) { json . def . forEach ( ( tableName ) => { const table = this . getTable ( tableName ) ; if ( ! table ) { return ; } let tables = this . database . getTables ( ) ; const hasReference = tables . some ( ( t ) => t . foreignKeys ?. some ( ( k ) => k . referencesTable ( table ) ) ) ; if ( hasReference ) { return ; } if ( ! table ) { return ; } const end = tables . splice ( tables . indexOf ( table ) ) ; end . shift ( ) ; tables = tables . concat ( end ) ; this . database . setTables ( tables ) ; } ) ; return ; } throw new TypeError ( `Expected P_DROP_TABLE rule to be handled but received ${ json . id }` ) ; } }