@sqlitecloud/drivers
Version:
SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients
122 lines (121 loc) • 5.14 kB
JavaScript
"use strict";
/**
* statement.ts
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Statement = void 0;
const utilities_1 = require("./utilities");
/**
* A statement generated by Database.prepare used to prepare SQL with ? bindings.
*
* SCSP protocol does not support named placeholders yet.
*/
class Statement {
constructor(database, sql, ...params) {
/** The SQL statement with binding values applied */
this._preparedSql = { query: '' };
this._database = database;
this._sql = sql;
this.bind(...params);
}
/**
* Binds parameters to the prepared statement and calls the callback when done
* or when an error occurs. The function returns the Statement object to allow
* for function chaining. The first and only argument to the callback is null
* when binding was successful. Binding parameters with this function completely
* resets the statement object and row cursor and removes all previously bound
* parameters, if any.
*
* In SQLiteCloud the statement is prepared on the database server and binding errors
* are raised on execution time.
*/
bind(...params) {
const { args, callback } = (0, utilities_1.popCallback)(params);
this._preparedSql = { query: this._sql, parameters: args };
if (callback) {
callback.call(this, null);
}
return this;
}
run(...params) {
var _a;
const { args, callback } = (0, utilities_1.popCallback)(params || []);
if ((args === null || args === void 0 ? void 0 : args.length) > 0) {
// apply new bindings then execute
this.bind(...args, () => {
var _a;
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.run(query, ...parametes, callback);
});
}
else {
// execute prepared sql with same bindings
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.run(query, ...parametes, callback);
}
return this;
}
get(...params) {
var _a;
const { args, callback } = (0, utilities_1.popCallback)(params || []);
if ((args === null || args === void 0 ? void 0 : args.length) > 0) {
// apply new bindings then execute
this.bind(...args, () => {
var _a;
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.get(query, ...parametes, callback);
});
}
else {
// execute prepared sql with same bindings
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.get(query, ...parametes, callback);
}
return this;
}
all(...params) {
var _a;
const { args, callback } = (0, utilities_1.popCallback)(params || []);
if ((args === null || args === void 0 ? void 0 : args.length) > 0) {
// apply new bindings then execute
this.bind(...args, () => {
var _a;
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.all(query, ...parametes, callback);
});
}
else {
// execute prepared sql with same bindings
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : [])];
this._database.all(query, ...parametes, callback);
}
return this;
}
each(...params) {
var _a;
const { args, callback, complete } = (0, utilities_1.popCallback)(params);
if ((args === null || args === void 0 ? void 0 : args.length) > 0) {
// apply new bindings then execute
this.bind(...args, () => {
var _a;
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : []), ...[callback, complete]];
this._database.each(query, ...parametes);
});
}
else {
// execute prepared sql with same bindings
const query = this._preparedSql.query || '';
const parametes = [...((_a = this._preparedSql.parameters) !== null && _a !== void 0 ? _a : []), ...[callback, complete]];
this._database.each(query, ...parametes);
}
return this;
}
}
exports.Statement = Statement;