@itwin/core-common
Version:
iTwin.js components common to frontend and backend
577 lines • 23.4 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* 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 { BentleyError, CompressedId64Set, DbResult, Id64, OrderedId64Iterable } from "@itwin/core-bentley";
import { Point2d, Point3d } from "@itwin/core-geometry";
import { Base64 } from "js-base64";
/**
* Specifies the format of the rows returned by the `query` and `restartQuery` methods of
* [IModelConnection]($frontend), [IModelDb]($backend), and [ECDb]($backend).
*
* @public
* @extensions
*/
export var QueryRowFormat;
(function (QueryRowFormat) {
/** Each row is an object in which each non-null column value can be accessed by its name as defined in the ECSql.
* Null values are omitted.
*/
QueryRowFormat[QueryRowFormat["UseECSqlPropertyNames"] = 0] = "UseECSqlPropertyNames";
/** Each row is an array of values accessed by an index corresponding to the property's position in the ECSql SELECT statement.
* Null values are included if they are followed by a non-null column, but trailing null values at the end of the array are omitted.
*/
QueryRowFormat[QueryRowFormat["UseECSqlPropertyIndexes"] = 1] = "UseECSqlPropertyIndexes";
/** Each row is an object in which each non-null column value can be accessed by a [remapped property name]($docs/learning/ECSqlRowFormat.md).
* This format is backwards-compatible with the format produced by iTwin.js 2.x. Null values are omitted.
* @depreacted in 4.11. Switch to UseECSqlPropertyIndexes for best performance, and UseECSqlPropertyNames if you want a JSON object as the result.
*/
QueryRowFormat[QueryRowFormat["UseJsPropertyNames"] = 2] = "UseJsPropertyNames";
})(QueryRowFormat || (QueryRowFormat = {}));
/** @public */
export class QueryOptionsBuilder {
_options;
constructor(_options = {}) {
this._options = _options;
}
getOptions() { return this._options; }
/**
* @internal
* Allow to set priority of query. Query will be inserted int queue base on priority value. This value will be ignored if concurrent query is configured with ignored priority is true.
* @param val integer value which can be negative as well. By default its zero.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setPriority(val) {
this._options.priority = val;
return this;
}
/**
* Allow to set restart token. If restart token is set then any other query(s) in queue with same token is cancelled if its not already executed.
* @param val A string token identifying a use case in which previous query with same token is cancelled.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setRestartToken(val) {
this._options.restartToken = val;
return this;
}
/**
* Allow to set quota restriction for query. Its a hint and may be overriden or ignored by concurrent query manager.
* @param val @type QueryQuota Specify time and memory that can be used by a query.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setQuota(val) {
this._options.quota = val;
return this;
}
/**
* Force a query to be executed synchronously against primary connection. This option is ignored if provided by frontend.
* @param val A boolean value to force use primary connection on main thread to execute query.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setUsePrimaryConnection(val) {
this._options.usePrimaryConn = val;
return this;
}
/**
* By default all blobs are abbreviated to save memory and network bandwidth. If set to false, all blob data will be returned by query as is.
* Use @type BlobReader to access blob data more efficiently.
* @param val A boolean value, if set to false will return complete blob type property data. This could cost time and network bandwidth.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setAbbreviateBlobs(val) {
this._options.abbreviateBlobs = val;
return this;
}
/**
* When query fail to prepare it will log error. This setting will suppress log errors in case where query come from user typing it and its expected to fail often.
* @param val A boolean value, if set to true, any error logging will be suppressed.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setSuppressLogErrors(val) {
this._options.suppressLogErrors = val;
return this;
}
/**
* If set ECClassId, SourceECClassId and TargetECClassId system properties will return qualified name of class instead of a @typedef Id64String.
* @param val A boolean value.
* @returns @type QueryOptionsBuilder for fluent interface.
* @deprecated in 4.11 - will not be removed until after 2026-06-13. Use ecsql function ec_classname to get class name instead.
*/
setConvertClassIdsToNames(val) {
// eslint-disable-next-line @typescript-eslint/no-deprecated
this._options.convertClassIdsToClassNames = val;
return this;
}
/**
* Specify limit for query. Limit determine number of rows and offset in result-set.
* @param val Specify count and offset from within the result-set of a ECSQL query.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setLimit(val) {
this._options.limit = val;
return this;
}
/**
* Specify row format returned by concurrent query manager.
* @param val @enum QueryRowFormat specifying format for result.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setRowFormat(val) {
this._options.rowFormat = val;
return this;
}
/**
* @internal
* Defers execution of query in queue by specified milliseconds. This parameter is ignored by default unless concurrent query is configure to not ignore it.
* @param val Number of milliseconds.
* @returns @type QueryOptionsBuilder for fluent interface.
*/
setDelay(val) {
this._options.delay = val;
return this;
}
}
/** @beta */
export class BlobOptionsBuilder {
_options;
constructor(_options = {}) {
this._options = _options;
}
getOptions() { return this._options; }
/**
* @internal
* Allow to set priority of blob request. Blob request will be inserted int queue base on priority value. This value will be ignored if concurrent query is configured with ignored priority is true.
* @param val integer value which can be negative as well. By default its zero.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setPriority(val) {
this._options.priority = val;
return this;
}
/**
* Allow to set restart token. If restart token is set then any other blob request in queue with same token is cancelled if its not already executed.
* @param val A string token identifying a use case in which previous blob request with same token is cancelled.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setRestartToken(val) {
this._options.restartToken = val;
return this;
}
/**
* Allow to set quota restriction for blob request. Its a hint and may be overriden or ignored by concurrent query manager.
* @param val @type QueryQuota Specify time and memory that can be used by a query.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setQuota(val) {
this._options.quota = val;
return this;
}
/**
* Force a blob request to be executed synchronously against primary connection. This option is ignored if provided by frontend.
* @param val A boolean value to force use primary connection on main thread to execute blob request.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setUsePrimaryConnection(val) {
this._options.usePrimaryConn = val;
return this;
}
/**
* Specify range with in the blob that need to be returned.
* @param val Specify offset and count of bytes that need to be returned.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setRange(val) {
this._options.range = val;
return this;
}
/**
* @internal
* Defers execution of blob request in queue by specified milliseconds. This parameter is ignored by default unless concurrent query is configure to not ignore it.
* @param val Number of milliseconds.
* @returns @type BlobOptionsBuilder for fluent interface.
*/
setDelay(val) {
this._options.delay = val;
return this;
}
}
/** @internal */
export var QueryParamType;
(function (QueryParamType) {
QueryParamType[QueryParamType["Boolean"] = 0] = "Boolean";
QueryParamType[QueryParamType["Double"] = 1] = "Double";
QueryParamType[QueryParamType["Id"] = 2] = "Id";
QueryParamType[QueryParamType["IdSet"] = 3] = "IdSet";
QueryParamType[QueryParamType["Integer"] = 4] = "Integer";
QueryParamType[QueryParamType["Long"] = 5] = "Long";
QueryParamType[QueryParamType["Null"] = 6] = "Null";
// eslint-disable-next-line @typescript-eslint/no-shadow
QueryParamType[QueryParamType["Point2d"] = 7] = "Point2d";
// eslint-disable-next-line @typescript-eslint/no-shadow
QueryParamType[QueryParamType["Point3d"] = 8] = "Point3d";
QueryParamType[QueryParamType["String"] = 9] = "String";
QueryParamType[QueryParamType["Blob"] = 10] = "Blob";
QueryParamType[QueryParamType["Struct"] = 11] = "Struct";
})(QueryParamType || (QueryParamType = {}));
/**
* Bind values to an ECSQL query.
*
* All binding class methods accept an `indexOrName` parameter as a `string | number` type and a value to bind to it.
* A binding must be mapped either by a positional index or a string/name. See the examples below.
*
* @example
* Parameter By Index:
* ```sql
* SELECT a, v FROM test.Foo WHERE a=? AND b=?
* ```
* The first `?` is index 1 and the second `?` is index 2. The parameter index starts with 1 and not 0.
*
* @example
* Parameter By Name:
* ```sql
* SELECT a, v FROM test.Foo WHERE a=:name_a AND b=:name_b
* ```
* Using "name_a" as the `indexOrName` will bind the provided value to `name_a` in the query. And the same goes for
* using "name_b" and the `name_b` binding respectively.
*
* @see
* - [ECSQL Parameters]($docs/learning/ECSQL.md#ecsql-parameters)
* - [ECSQL Parameter Types]($docs/learning/ECSQLParameterTypes)
* - [ECSQL Code Examples]($docs/learning/backend/ECSQLCodeExamples#parameter-bindings)
*
* @public
*/
export class QueryBinder {
_args = {};
verify(indexOrName) {
if (typeof indexOrName === "number") {
if (indexOrName < 1)
throw new Error("expect index to be >= 1");
return;
}
if (!/^[a-zA-Z_]+\w*$/i.test(indexOrName)) {
throw new Error("expect named parameter to meet identifier specification");
}
}
/**
* Bind boolean value to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val Boolean value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindBoolean(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true,
value: {
type: QueryParamType.Boolean,
value: val,
},
});
return this;
}
/**
* Bind blob value to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val Blob value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindBlob(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
const base64 = Base64.fromUint8Array(val);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Blob,
value: base64,
},
});
return this;
}
/**
* Bind double value to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val Double value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindDouble(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Double,
value: val,
},
});
return this;
}
/**
* Bind @typedef Id64String value to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val @typedef Id64String value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindId(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Id,
value: val,
},
});
return this;
}
/**
* Bind @type OrderedId64Iterable to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val @type OrderedId64Iterable value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindIdSet(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
OrderedId64Iterable.uniqueIterator(val);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.IdSet,
value: CompressedId64Set.sortAndCompress(OrderedId64Iterable.uniqueIterator(val)),
},
});
return this;
}
/**
* Bind integer to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val Integer value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindInt(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Integer,
value: val,
},
});
return this;
}
/**
* Bind struct to ECSQL statement. Struct specified as object.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val struct value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindStruct(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Struct,
value: val,
},
});
return this;
}
/**
* Bind long to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val Long value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindLong(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Long,
value: val,
},
});
return this;
}
/**
* Bind string to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val String value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindString(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.String,
value: val,
},
});
return this;
}
/**
* Bind null to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindNull(indexOrName) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Null,
value: null,
},
});
return this;
}
/**
* Bind @type Point2d to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val @type Point2d value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindPoint2d(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Point2d,
value: val,
},
});
return this;
}
/**
* Bind @type Point3d to ECSQL statement.
* @param indexOrName Specify parameter index or its name used in ECSQL statement.
* @param val @type Point3d value to bind to ECSQL statement.
* @returns @type QueryBinder to allow fluent interface.
*/
bindPoint3d(indexOrName, val) {
this.verify(indexOrName);
const name = String(indexOrName);
Object.defineProperty(this._args, name, {
enumerable: true, value: {
type: QueryParamType.Point3d,
value: val,
},
});
return this;
}
static bind(params, nameOrId, val) {
if (typeof val === "boolean") {
params.bindBoolean(nameOrId, val);
}
else if (typeof val === "number") {
params.bindDouble(nameOrId, val);
}
else if (typeof val === "string") {
params.bindString(nameOrId, val);
}
else if (val instanceof Uint8Array) {
params.bindBlob(nameOrId, val);
}
else if (val instanceof Point2d) {
params.bindPoint2d(nameOrId, val);
}
else if (val instanceof Point3d) {
params.bindPoint3d(nameOrId, val);
}
else if (val instanceof Array && val.length > 0 && typeof val[0] === "string" && Id64.isValidId64(val[0])) {
params.bindIdSet(nameOrId, val);
}
else if (typeof val === "undefined" || val === null) {
params.bindNull(nameOrId);
}
else if (typeof val === "object" && !Array.isArray(val)) {
params.bindStruct(nameOrId, val);
}
else {
throw new Error("unsupported type");
}
}
/**
* Allow bulk bind either parameters by index as value array or by parameter names as object.
* @param args if array of values is provided then array index is used as index. If object is provided then object property name is used as parameter name of reach value.
* @returns @type QueryBinder to allow fluent interface.
*/
static from(args) {
const params = new QueryBinder();
if (typeof args === "undefined")
return params;
if (Array.isArray(args)) {
let i = 1;
for (const val of args) {
this.bind(params, i++, val);
}
}
else {
for (const prop of Object.getOwnPropertyNames(args)) {
this.bind(params, prop, args[prop]);
}
}
return params;
}
serialize() {
return this._args;
}
}
/** @internal */
export var DbRequestKind;
(function (DbRequestKind) {
DbRequestKind[DbRequestKind["BlobIO"] = 0] = "BlobIO";
DbRequestKind[DbRequestKind["ECSql"] = 1] = "ECSql";
})(DbRequestKind || (DbRequestKind = {}));
/** @internal */
export var DbResponseKind;
(function (DbResponseKind) {
DbResponseKind[DbResponseKind["BlobIO"] = 0] = "BlobIO";
DbResponseKind[DbResponseKind["ECSql"] = 1] = "ECSql";
DbResponseKind[DbResponseKind["NoResult"] = 2] = "NoResult";
})(DbResponseKind || (DbResponseKind = {}));
/** @internal */
export var DbResponseStatus;
(function (DbResponseStatus) {
DbResponseStatus[DbResponseStatus["Done"] = 1] = "Done";
DbResponseStatus[DbResponseStatus["Cancel"] = 2] = "Cancel";
DbResponseStatus[DbResponseStatus["Partial"] = 3] = "Partial";
DbResponseStatus[DbResponseStatus["Timeout"] = 4] = "Timeout";
DbResponseStatus[DbResponseStatus["QueueFull"] = 5] = "QueueFull";
DbResponseStatus[DbResponseStatus["ShuttingDown"] = 6] = "ShuttingDown";
DbResponseStatus[DbResponseStatus["Error"] = 100] = "Error";
DbResponseStatus[DbResponseStatus["Error_ECSql_PreparedFailed"] = 101] = "Error_ECSql_PreparedFailed";
DbResponseStatus[DbResponseStatus["Error_ECSql_StepFailed"] = 102] = "Error_ECSql_StepFailed";
DbResponseStatus[DbResponseStatus["Error_ECSql_RowToJsonFailed"] = 103] = "Error_ECSql_RowToJsonFailed";
DbResponseStatus[DbResponseStatus["Error_ECSql_BindingFailed"] = 104] = "Error_ECSql_BindingFailed";
DbResponseStatus[DbResponseStatus["Error_BlobIO_OpenFailed"] = 105] = "Error_BlobIO_OpenFailed";
DbResponseStatus[DbResponseStatus["Error_BlobIO_OutOfRange"] = 106] = "Error_BlobIO_OutOfRange";
})(DbResponseStatus || (DbResponseStatus = {}));
/** @internal */
export var DbValueFormat;
(function (DbValueFormat) {
DbValueFormat[DbValueFormat["ECSqlNames"] = 0] = "ECSqlNames";
DbValueFormat[DbValueFormat["JsNames"] = 1] = "JsNames";
})(DbValueFormat || (DbValueFormat = {}));
/** @public */
export class DbQueryError extends BentleyError {
response;
request;
constructor(response, request, rc) {
super(rc ?? DbResult.BE_SQLITE_ERROR, response.error, { response, request });
this.response = response;
this.request = request;
}
static throwIfError(response, request) {
if (response.status >= DbResponseStatus.Error) {
throw new DbQueryError(response, request);
}
if (response.status === DbResponseStatus.Cancel) {
throw new DbQueryError(response, request, DbResult.BE_SQLITE_INTERRUPT);
}
}
}
//# sourceMappingURL=ConcurrentQuery.js.map