-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ElementCascadingDeleter to fix FK errors while deleting element w…
…hich is referenced in code scopes (#75) Added ElementCascadingDeleter, which works similarly to ElementTreeDeleter, but also finds elements that reference this element in their code scopes and deletes those elements. #61
- Loading branch information
1 parent
c434b4d
commit df80ed2
Showing
5 changed files
with
113 additions
and
15 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@itwin-imodel-transformer-f78fb57a-fc9a-44ae-839f-8b02b42df1a6.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Added ElementCascadingDeleter to fix FK errors while deleting element which is referenced in code scopes of other elements", | ||
"packageName": "@itwin/imodel-transformer", | ||
"email": "deividas.davidavicius@bentley.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
/** @packageDocumentation | ||
* @module iModels | ||
*/ | ||
import { ElementTreeDeleter, ElementTreeWalkerScope, IModelDb } from "@itwin/core-backend"; | ||
import { DbResult, Id64String } from "@itwin/core-bentley"; | ||
|
||
/** Deletes an element tree and code scope references starting with the specified top element. The top element is also deleted. Uses ElementCascadeDeleter. | ||
* @param iModel The iModel | ||
* @param topElement The parent of the sub-tree | ||
*/ | ||
export function deleteElementTreeCascade(iModel: IModelDb, topElement: Id64String): void { | ||
const del = new ElementCascadingDeleter(iModel); | ||
del.deleteNormalElements(topElement); | ||
del.deleteSpecialElements(); | ||
} | ||
|
||
/** Deletes an entire element tree, including sub-models, child elements and code scope references. | ||
* Items are deleted in bottom-up order. Definitions and Subjects are deleted after normal elements. | ||
* Call deleteNormalElements on each tree. Then call deleteSpecialElements. | ||
*/ | ||
export class ElementCascadingDeleter extends ElementTreeDeleter { | ||
protected shouldVisitCodeScopes(_elementId: Id64String, _scope: ElementTreeWalkerScope) { return true; } | ||
|
||
/** The main tree-walking function */ | ||
protected override processElementTree(element: Id64String, scope: ElementTreeWalkerScope): void { | ||
if (this.shouldVisitCodeScopes(element, scope)) { | ||
this._processCodeScopes(element, scope); | ||
} | ||
super.processElementTree(element, scope); | ||
} | ||
/** Process code scope references */ | ||
private _processCodeScopes(element: Id64String, scope: ElementTreeWalkerScope) { | ||
const newScope = new ElementTreeWalkerScope(scope, element); | ||
this._iModel.withPreparedStatement(` | ||
SELECT ECInstanceId | ||
FROM bis.Element | ||
WHERE CodeScope.id=? | ||
AND Parent.id IS NULL | ||
`, (stmt) => { | ||
stmt.bindId(1, element); | ||
while (stmt.step() === DbResult.BE_SQLITE_ROW) { | ||
const elementId = stmt.getValue(0).getId(); | ||
this.processElementTree(elementId, newScope); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters