import { O_CREATE_TABLE_CREATE_DEFINITION , P_CREATE_INDEX , STATEMENT , O_CREATE_TABLE_CREATE_DEFINITION_SPATIAL_INDEX , O_ALTER_TABLE_SPEC_ADD_SPATIAL_INDEX , SpatialIndexInterface , } from '../../../../typings' ; import { isDefined } from '../../../../shared/utils' ; import { IndexColumn } from './index-column' ; import { IndexOptions } from './index-options' ; import { SpatialIndexModelInterface , IndexColumnModelInterface , IndexOptionsModelInterface , TableModelInterface , ColumnModelInterface , } from './typings' ; /**
 * Spatial index of a table.
 */ export class SpatialIndex implements SpatialIndexModelInterface { name ? : string ; columns : IndexColumnModelInterface [ ] = [ ] ; options ? : IndexOptionsModelInterface ; param json static fromDef ( json ) { if ( json . id === 'O_CREATE_TABLE_CREATE_DEFINITION' ) { return SpatialIndex . fromObject ( json . def . spatialIndex as O_CREATE_TABLE_CREATE_DEFINITION_SPATIAL_INDEX ) ; } if ( json . id === 'P_CREATE_INDEX' ) { return SpatialIndex . fromObject ( json . def ) ; } throw new TypeError ( `Unknown json id to build spatial index from: ${ ( json as STATEMENT ) . id }` ) ; } /**
     * Creates a spatial index from an object containing needed properties.
     */ static fromObject ( json ) { const spatialIndex = new SpatialIndex ( ) ; spatialIndex . columns = json . columns . map ( IndexColumn . fromDef ) ; if ( json . name ) { spatialIndex . name = json . name ; } if ( isDefined ( json . options ) && json . options . length ) { spatialIndex . options = IndexOptions . fromArray ( json . options ) ; } return spatialIndex ; } /**
     * JSON casting of this object calls this method.
     */ toJSON ( ) { const json = { columns : this . columns . map ( ( c ) => c . toJSON ( ) ) , } ; if ( isDefined ( this . name ) ) { json . name = this . name ; } if ( isDefined ( this . options ) ) { json . options = this . options . toJSON ( ) ; } return json ; } /**
     * Create a deep clone of this model.
     */ clone ( ) { const index = new SpatialIndex ( ) ; index . columns = this . columns . map ( ( c ) => c . clone ( ) ) ; if ( isDefined ( this . name ) ) { index . name = this . name ; } if ( isDefined ( this . options ) ) { index . options = this . options . clone ( ) ; } return index ; } param name dropColumn ( name ) { let pos = - 1 ; const found = this . columns . some ( ( c , i ) => { pos = i ; return c . column === name ; } ) ; if ( ! found || pos < 0 ) { return false ; } const end = this . columns . splice ( pos ) ; end . shift ( ) ; this . columns = this . columns . concat ( end ) ; return true ; } param table getColumnsFromTable ( table ) { return ( table . columns ?? [ ] ) . filter ( ( tableColumn ) => this . columns . some ( ( indexColumn ) => indexColumn . column === tableColumn . name ) ) ; } param table hasAllColumnsFromTable ( table ) { return ( ( table . columns ?? [ ] ) . filter ( ( tableColumn ) => this . columns . some ( ( indexColumn ) => indexColumn . column === tableColumn . name ) ) . length === this . columns . length ) ; } param column param newName renameColumn ( column , newName ) { this . columns . filter ( ( c ) => c . column === column . name ) . forEach ( ( c ) => { c . column = newName ; } ) ; } }