import { computed , makeObservable } from 'mobx' ; import { DataTypeLogicalOperation , ResultDataFormat , SqlResultColumn } from '@cloudbeaver/core-sdk' ; import { DatabaseDataAction } from '../../DatabaseDataAction' ; import type { IDatabaseDataSource } from '../../IDatabaseDataSource' ; import type { IDatabaseResultSet } from '../../IDatabaseResultSet' ; import { databaseDataAction } from '../DatabaseDataActionDecorator' ; import type { IDatabaseDataResultAction } from '../IDatabaseDataResultAction' ; import type { IResultSetContentValue } from './IResultSetContentValue' ; import type { IResultSetColumnKey , IResultSetElementKey , IResultSetRowKey } from './IResultSetDataKey' ; import { isResultSetContentValue } from './isResultSetContentValue' ; import type { IResultSetValue } from './ResultSetFormatAction' ; @ databaseDataAction ( ) export class ResultSetDataAction extends DatabaseDataAction < any , IDatabaseResultSet > implements IDatabaseDataResultAction < IDatabaseResultSet > { static dataFormat = [ ResultDataFormat . Resultset ] ; get rows ( ) : IResultSetValue [ ] [ ] { return this . result . data ?. rows || [ ] ; } get columns ( ) : SqlResultColumn [ ] { return this . result . data ?. columns || [ ] ; } constructor ( source , result ) { super ( source , result ) ; makeObservable ( this , { rows : computed , columns : computed , } ) ; } getDefaultKey ( ) { return { row : { index : 0 , } , column : { index : 0 , } , } ; } insertRow ( row , value , shift = 0 ) { if ( this . result . data ?. rows ) { const index = row . index + shift ; this . result . data . rows . splice ( index , 0 , value ) ; return { index } ; } return undefined ; } removeRow ( row , shift = 0 ) { if ( this . result . data ?. rows ) { const index = row . index + shift ; this . result . data . rows . splice ( index , 1 ) ; return { index : index - 1 } ; } return undefined ; } setRowValue ( row , value , shift = 0 ) { if ( this . result . data ?. rows ) { this . result . data . rows [ row . index + shift ] = value ; } } getRowValue ( row ) { if ( row . index >= this . rows . length ) { return undefined ; } return this . rows [ row . index ] ; } getCellValue ( cell ) { if ( cell . row === undefined || cell . column === undefined || cell . row . index >= this . rows . length || cell . column . index >= this . columns . length ) { return undefined ; } return this . rows [ cell . row . index ] [ cell . column . index ] ; } getContent ( cell ) { const value = this . getCellValue ( cell ) ; if ( isResultSetContentValue ( value ) ) { return value ; } return null ; } getColumn ( key ) { if ( key . index >= this . columns . length ) { return undefined ; } return this . columns [ key . index ] ; } getColumnOperations ( key ) { const column = this . getColumn ( key ) ; if ( ! column ) { return [ ] ; } return column . supportedOperations . filter ( operation => operation . argumentCount === 1 || operation . argumentCount === 0 ) ; } }