import { assert , DbResult , Id64String , Logger } from "@bentley/bentleyjs-core" ; import { BriefcaseDb , ChangeSummary , ChangeSummaryManager , ECSqlStatement } from "@bentley/imodeljs-backend" ; import { ChangedValueState , ChangeOpCode } from "@bentley/imodeljs-common" ; import { AuthorizedClientRequestContext } from "@bentley/itwin-client" ; import { QueryAgentConfig } from "./QueryAgentConfig" ; export class ChangeSummaryExtractor { public async extractChangeSummary ( requestContext , iModelDb , changeSetId ) { requestContext . enter ( ) ; try { const changeSummaryIds = await ChangeSummaryManager . extractChangeSummaries ( requestContext , iModelDb , { currentVersionOnly : true } ) ; Logger . logTrace ( QueryAgentConfig . loggingCategory , `Extracted summary information from change set "${ changeSetId }"` ) ; ChangeSummaryManager . attachChangeCache ( iModelDb ) ; assert ( changeSummaryIds . length === 1 ) ; const changeSummary = ChangeSummaryManager . queryChangeSummary ( iModelDb , changeSummaryIds [ 0 ] ) ; const changeContent = { id : changeSummary . id , changeSet : changeSummary . changeSet , instanceChanges : { } } ; Logger . logTrace ( QueryAgentConfig . loggingCategory , `   Description: ${ changeSummary . changeSet . description }` ) ; Logger . logTrace ( QueryAgentConfig . loggingCategory , `   Push Date: ${ new Date ( changeSummary . changeSet . pushDate ) . toLocaleString ( ) }` ) ; Logger . logTrace ( QueryAgentConfig . loggingCategory , `   Author: ${ changeSummary . changeSet . userCreated }` ) ; const sql = "SELECT ECInstanceId FROM ecchange.change.InstanceChange WHERE Summary.Id=? ORDER BY ECInstanceId" ; changeContent . instanceChanges = await iModelDb . withPreparedStatement < Promise < any [ ] > > ( sql , async ( stmt ) : Promise < any [ ] > => { stmt . bindId ( 1 , changeSummary . id ) ; const instanceChanges = new Array < any > ( ) ; while ( stmt . step ( ) === DbResult . BE_SQLITE_ROW ) { const row = stmt . getRow ( ) ; const instanceChange = ChangeSummaryManager . queryInstanceChange ( iModelDb , row . id ) ; switch ( instanceChange . opCode ) { case ChangeOpCode . Insert : { const afterQuery = ChangeSummaryManager . buildPropertyValueChangesECSql ( iModelDb , instanceChange , ChangedValueState . AfterInsert ) ; const after = await iModelDb . withPreparedStatement ( afterQuery , async ( afterStmt ) => { return afterStmt . next ( ) ; } ) ; instanceChange . after = after ; break ; } case ChangeOpCode . Update : { const beforeQuery = ChangeSummaryManager . buildPropertyValueChangesECSql ( iModelDb , instanceChange , ChangedValueState . BeforeUpdate ) ; const before = await iModelDb . withPreparedStatement ( beforeQuery , async ( beforeStmt ) => { return beforeStmt . next ( ) ; } ) ; instanceChange . before = before ; const afterQuery = ChangeSummaryManager . buildPropertyValueChangesECSql ( iModelDb , instanceChange , ChangedValueState . AfterUpdate ) ; const after = await iModelDb . withPreparedStatement ( afterQuery , async ( afterStmt ) => { return afterStmt . next ( ) ; } ) ; instanceChange . after = after ; break ; } case ChangeOpCode . Delete : { const beforeQuery = ChangeSummaryManager . buildPropertyValueChangesECSql ( iModelDb , instanceChange , ChangedValueState . BeforeDelete ) ; const before = await iModelDb . withPreparedStatement ( beforeQuery , async ( beforeStmt ) => { return beforeStmt . next ( ) ; } ) ; instanceChange . before = before ; break ; } } instanceChanges . push ( instanceChange ) ; } return instanceChanges ; } ) ; ChangeSummaryManager . detachChangeCache ( iModelDb ) ; return changeContent ; } catch ( error ) { Logger . logError ( QueryAgentConfig . loggingCategory , `Error while extracting changeset summary ${ changeSetId }: ${ error }` ) ; } return undefined ; } }