Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attach/detach db to IModelDb connection #7530

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a17f8fa
add interface method and test
khanaffan Jan 7, 2025
d7b4351
Update test
khanaffan Jan 8, 2025
a920743
rush extract-api
khanaffan Jan 8, 2025
52b5cba
rush changes
khanaffan Jan 8, 2025
e603c27
lint
khanaffan Jan 8, 2025
8e9dc21
Merge branch 'master' into affanK/attach-detach-file
khanaffan Jan 8, 2025
f3a1d9f
Merge branch 'master' into affanK/attach-detach-file
khanaffan Jan 9, 2025
68f7417
Merge branch 'master' of https://github.com/iTwin/itwinjs-core into a…
khanaffan Jan 10, 2025
b064f7a
add test
khanaffan Jan 14, 2025
3b14578
Merge branch 'master' of https://github.com/iTwin/itwinjs-core into a…
khanaffan Feb 19, 2025
3c4984a
fix test
khanaffan Feb 19, 2025
a9a804f
undo cloud sqlite test
khanaffan Feb 20, 2025
941735e
Merge branch 'master' of https://github.com/iTwin/itwinjs-core into a…
khanaffan Feb 20, 2025
c2187f2
add unit test
khanaffan Feb 20, 2025
a2dde85
close file at the end of the function
khanaffan Feb 20, 2025
740fd4e
update next version md
khanaffan Feb 20, 2025
59f0ce8
Update docs/changehistory/NextVersion.md
khanaffan Feb 20, 2025
7e363b2
Update docs/changehistory/NextVersion.md
khanaffan Feb 20, 2025
edcfafd
Update core/backend/src/ECDb.ts
khanaffan Feb 20, 2025
2547b69
Update core/backend/src/ECDb.ts
khanaffan Feb 20, 2025
23450c0
Update core/backend/src/IModelDb.ts
khanaffan Feb 20, 2025
6aea062
Update core/backend/src/IModelDb.ts
khanaffan Feb 20, 2025
41472bd
Merge branch 'master' into affanK/attach-detach-file
khanaffan Feb 24, 2025
29bfd7b
Merge branch 'master' into affanK/attach-detach-file
khanaffan Feb 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/api/core-backend.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1792,11 +1792,13 @@ export class ECDb implements Disposable {
[Symbol.dispose](): void;
constructor();
abandonChanges(): void;
attachDb(fileName: string, alias: string): void;
// @internal
clearStatementCache(): void;
closeDb(): void;
createDb(pathName: string): void;
createQueryReader(ecsql: string, params?: QueryBinder, config?: QueryOptions): ECSqlReader;
detachDb(alias: string): void;
// @deprecated (undocumented)
dispose(): void;
// @internal
Expand Down Expand Up @@ -3169,6 +3171,7 @@ export abstract class IModelDb extends IModel {
});
abandonChanges(): void;
acquireSchemaLock(): Promise<void>;
attachDb(fileName: string, alias: string): void;
// @internal
protected beforeClose(): void;
// @internal
Expand Down Expand Up @@ -3201,6 +3204,7 @@ export abstract class IModelDb extends IModel {
deleteFileProperty(prop: FilePropertyProps): void;
// @beta
deleteSettingDictionary(name: string): void;
detachDb(alias: string): void;
// @beta
elementGeometryCacheOperation(requestProps: ElementGeometryCacheOperationRequestProps): BentleyStatus;
// @beta
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-backend",
"comment": "Allow attach/detach db",
"type": "none"
}
],
"packageName": "@itwin/core-backend"
}
16 changes: 16 additions & 0 deletions core/backend/src/ECDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ export class ECDb implements Disposable {
this._nativeDb.dispose();
this._nativeDb = undefined;
}
/**
* Attach an ECDb file to this connection and load and register its schemas.
* @param fileName ECDb file name
* @param alias identifier for the attached file. This identifer is used a tablespace executing ECSQL queries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what "a tablespace" is supposed to mean. Maybe you meant "as tablespace", but I don't recognize the term "tablespace". I know what this parameter does, but I don't know the proper way to describe it.

*/
public attachDb(fileName: string, alias: string): void {
this[_nativeDb].attachDb(fileName, alias);
}
/**
* Detach the attached file from this connection. The attached file is closed and its schemas are unregistered.
* @param alias identifer that was used in the call to [[attachDb]]
*/
public detachDb(alias: string): void {
this.clearStatementCache();
this[_nativeDb].detachDb(alias);
}

/** @deprecated in 5.0 Use [Symbol.dispose] instead. */
public dispose(): void {
Expand Down
17 changes: 16 additions & 1 deletion core/backend/src/IModelDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,22 @@ export abstract class IModelDb extends IModel {
});
}
}

/**
* Attach an iModel file to this connection and load and register its schemas.
* @param fileName IModel file name
* @param alias identifier for the attached file. This identifer is used a tablespace executing ECSQL queries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for ECDb regarding "a tablespace".

*/
public attachDb(fileName: string, alias: string): void {
this[_nativeDb].attachDb(fileName, alias);
}
/**
* Detach the attached file from this connection. The attached file is closed and its schemas are unregistered.
* @param alias identifer that was used in the call to [[attachDb]]
*/
public detachDb(alias: string): void {
this.clearCaches();
this[_nativeDb].detachDb(alias);
}
/** Close this IModel, if it is currently open, and save changes if it was opened in ReadWrite mode. */
public close(): void {
if (!this.isOpen)
Expand Down
162 changes: 162 additions & 0 deletions core/backend/src/test/AttachDb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { expect } from "chai";
import { SnapshotDb } from "../IModelDb";
import { IModelTestUtils } from "./IModelTestUtils";

describe("Attach/Detach Db", () => {
it("attach simulation db", async () => {
const masterFile = IModelTestUtils.resolveAssetFile("sim-master.bim");
const simulationFile = IModelTestUtils.resolveAssetFile("sim-attach.ecdb");

const master = SnapshotDb.openFile(masterFile);
master.attachDb(simulationFile, "SimDb");

const ecsql = `
SELECT ts.TimeFromStart [Time From Start (s)],
p.UserLabel [Pipe with Max Flow],
MAX(ltvrr.Flow) [Max Flow (L/s)]
FROM
SimDb.simrescore.TimeStep ts
INNER JOIN SimDb.stmswrres.BasicFlowResultRecord ltvrr ON ts.ECInstanceId = ltvrr.TimeStep.Id
INNER JOIN swrhyd.Pipe p ON p.ECInstanceId = ltvrr.ElementId
GROUP BY
ts.ECInstanceId
HAVING
MAX(ltvrr.Flow) > 1`;

const reader = master.createQueryReader(ecsql);
const rows = [];
while (await reader.step()) {
rows.push(reader.current.toRow());
}
const expected = [
{
'Time From Start (s)': 0,
'Pipe with Max Flow': 'CO-4',
'Max Flow (L/s)': 66.14359584163114
},
{
'Time From Start (s)': 3600,
'Pipe with Max Flow': 'CO-4',
'Max Flow (L/s)': 78.33925707748288
},
{
'Time From Start (s)': 7200,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 85.32875334207684
},
{
'Time From Start (s)': 10800,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 73.66891955141061
},
{
'Time From Start (s)': 14400,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 98.03457129303487
},
{
'Time From Start (s)': 18000,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 79.73721449443349
},
{
'Time From Start (s)': 21600,
'Pipe with Max Flow': 'CO-4',
'Max Flow (L/s)': 91.99697459812566
},
{
'Time From Start (s)': 25200,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 97.3640516154515
},
{
'Time From Start (s)': 28800,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 82.92153917380564
},
{
'Time From Start (s)': 32400,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 93.43596024800935
},
{
'Time From Start (s)': 36000,
'Pipe with Max Flow': 'CO-1',
'Max Flow (L/s)': 93.38944851040705
},
{
'Time From Start (s)': 39600,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 96.89678313985426
},
{
'Time From Start (s)': 43200,
'Pipe with Max Flow': 'CO-4',
'Max Flow (L/s)': 68.37554676909588
},
{
'Time From Start (s)': 46800,
'Pipe with Max Flow': 'CO-4',
'Max Flow (L/s)': 40.71067873689955
},
{
'Time From Start (s)': 50400,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 94.95603088826243
},
{
'Time From Start (s)': 54000,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 90.30742518949977
},
{
'Time From Start (s)': 57600,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 96.32532799368296
},
{
'Time From Start (s)': 61200,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 98.7241157161529
},
{
'Time From Start (s)': 64800,
'Pipe with Max Flow': 'CO-1',
'Max Flow (L/s)': 76.65530275985837
},
{
'Time From Start (s)': 68400,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 61.81109058119374
},
{
'Time From Start (s)': 72000,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 98.37332278792479
},
{
'Time From Start (s)': 75600,
'Pipe with Max Flow': 'CO-3',
'Max Flow (L/s)': 70.55434454594996
},
{
'Time From Start (s)': 79200,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 84.03731418990937
},
{
'Time From Start (s)': 82800,
'Pipe with Max Flow': 'CO-2',
'Max Flow (L/s)': 96.3267817742375
}
];
expect(rows).to.deep.equal(expected);
master.close();
});

});
Binary file added core/backend/src/test/assets/sim-attach.ecdb
Binary file not shown.
Binary file added core/backend/src/test/assets/sim-master.bim
Binary file not shown.
Loading
Loading