UNPKG

ts-hdb

Version:

hdb driver with typescript

168 lines 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Statement = void 0; /* eslint-disable max-len */ const errors_1 = require("./errors"); const types_1 = require("./types"); const utils_1 = require("./utils"); class Statement { /** * node-hdb statement object */ #statement; /** * @private * @internal * @param statement */ constructor(statement) { this.#statement = statement; } /** * get statement id */ get id() { return this.#statement?.id; } /** * get functionCode */ get functionCode() { return this.#statement.functionCode; } /** * execute statement directly, return result if applicable * * @param params * @returns */ async exec(...params) { return new Promise((resolve, reject) => { this.#statement.exec(params, (err, results) => { if (err) { reject(err); } else { resolve(results); } }); }); } async write(...params) { return this.exec(...params); } /** * direct execute query * * @param params * @returns query result */ async query(...params) { return this.exec(...params); } /** * call proc directly * * ref the [document](https://github.com/SAP/node-hdb#calling-stored-procedures) * * @param param * param map, * for example, * a proc have 3 `in` params: `A,B,C`, * the input param should have this format `{A:1,B:2,C:3}` * * @returns * out parameters array, * the plain type will be converted as map and stored in the first item * and other `table type` out parameters will be appended to the results array */ async call(param) { return new Promise((resolve, reject) => { this.#statement.exec(param, (err, ...results) => { if (err) { reject(err); } else { resolve(results); } }); }); } /** * drop the prepared statement * * @returns */ async drop() { return new Promise((resolve, reject) => { this.#statement.drop((err) => { if (err) { reject(err); } else { resolve(undefined); } }); }); } /** * query result set, in stream mode * * @param params * @returns result set * @throws {NotSupportedOperationError} */ async streamQuery(...params) { // currently, node-hdb do not convert table type out parameter to result set if (this.functionCode === types_1.FunctionCode.DB_PROCEDURE_CALL) { throw new errors_1.NotSupportedOperationError(`not support to use 'execute' method to call procedure`); } return new Promise((resolve, reject) => { this.#statement.execute(params, (err, rs) => { if (err) { reject(err); } else { resolve(rs); } }); }); } /** * query object stream * * @returns async object iterator * * @example * * ```ts * for await (const row of stat.streamQueryObject(1)) { * expect(row.ID).not.toBeUndefined(); * expect(row.NAME).not.toBeUndefined(); * } * ``` * */ streamQueryObject(...params) { return (0, utils_1.createAsyncStream)(this, "createObjectStream", params); } /** * query object list stream * * @returns async object iterator * @example * * ```ts * for await (const rows of stat.streamQueryObject(1, 'string')) { * expect(rows[0].ID).not.toBeUndefined(); * expect(rows[0].NAME).not.toBeUndefined(); * } * ``` * */ streamQueryList(...params) { return (0, utils_1.createAsyncStream)(this, "createArrayStream", params); } } exports.Statement = Statement; //# sourceMappingURL=Statement.js.map