UNPKG

6.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Statement = void 0;
4/**
5 * Promisified wrapper for the sqlite3#Statement interface.
6 */
7class Statement {
8 constructor(stmt) {
9 this.stmt = stmt;
10 }
11 /**
12 * Returns the underlying sqlite3 Statement instance
13 */
14 getStatementInstance() {
15 return this.stmt;
16 }
17 /**
18 * Binds parameters to the prepared statement.
19 *
20 * Binding parameters with this function completely resets the statement object and row cursor
21 * and removes all previously bound parameters, if any.
22 */
23 bind(...params) {
24 return new Promise((resolve, reject) => {
25 this.stmt.bind(...params, err => {
26 if (err) {
27 return reject(err);
28 }
29 resolve();
30 });
31 });
32 }
33 /**
34 * Resets the row cursor of the statement and preserves the parameter bindings.
35 * Use this function to re-execute the same query with the same bindings.
36 */
37 reset() {
38 return new Promise(resolve => {
39 this.stmt.reset(() => {
40 resolve();
41 });
42 });
43 }
44 /**
45 * Finalizes the statement. This is typically optional, but if you experience long delays before
46 * the next query is executed, explicitly finalizing your statement might be necessary.
47 * This might be the case when you run an exclusive query (see section Control Flow).
48 * After the statement is finalized, all further function calls on that statement object
49 * will throw errors.
50 */
51 finalize() {
52 return new Promise((resolve, reject) => {
53 this.stmt.finalize(err => {
54 if (err) {
55 return reject(err);
56 }
57 resolve();
58 });
59 });
60 }
61 /**
62 * Binds parameters and executes the statement.
63 *
64 * If you specify bind parameters, they will be bound to the statement before it is executed.
65 * Note that the bindings and the row cursor are reset when you specify even a single bind parameter.
66 *
67 * The execution behavior is identical to the Database#run method with the difference that the
68 * statement will not be finalized after it is run. This means you can run it multiple times.
69 *
70 * @param {any} [params, ...] When the SQL statement contains placeholders, you
71 * can pass them in here. They will be bound to the statement before it is
72 * executed. There are three ways of passing bind parameters: directly in
73 * the function's arguments, as an array, and as an object for named
74 * parameters. This automatically sanitizes inputs.
75 */
76 run(...params) {
77 return new Promise((resolve, reject) => {
78 const stmt = this;
79 this.stmt.run(...params, function (err) {
80 if (err) {
81 return reject(err);
82 }
83 resolve({
84 stmt,
85 lastID: this.lastID,
86 changes: this.changes
87 });
88 });
89 });
90 }
91 /**
92 * Binds parameters, executes the statement and retrieves the first result row.
93 * The parameters are the same as the Statement#run function, with the following differences:
94 *
95 * Using this method can leave the database locked, as the database awaits further
96 * calls to Statement#get to retrieve subsequent rows. To inform the database that you
97 * are finished retrieving rows, you should either finalize (with Statement#finalize)
98 * or reset (with Statement#reset) the statement.
99 *
100 * @param {any} [params, ...] When the SQL statement contains placeholders, you
101 * can pass them in here. They will be bound to the statement before it is
102 * executed. There are three ways of passing bind parameters: directly in
103 * the function's arguments, as an array, and as an object for named
104 * parameters. This automatically sanitizes inputs.
105 */
106 get(...params) {
107 return new Promise((resolve, reject) => {
108 this.stmt.get(...params, (err, row) => {
109 if (err) {
110 return reject(err);
111 }
112 resolve(row);
113 });
114 });
115 }
116 /**
117 * Binds parameters, executes the statement and calls the callback with all result rows.
118 * The parameters are the same as the Statement#run function, with the following differences:
119 *
120 * If the result set is empty, it will resolve to an empty array, otherwise it contains an
121 * object for each result row which in turn contains the values of that row.
122 * Like with Statement#run, the statement will not be finalized after executing this function.
123 *
124 * @param {any} [params, ...] When the SQL statement contains placeholders, you
125 * can pass them in here. They will be bound to the statement before it is
126 * executed. There are three ways of passing bind parameters: directly in
127 * the function's arguments, as an array, and as an object for named
128 * parameters. This automatically sanitizes inputs.
129 *
130 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaseallsql-param--callback
131 */
132 all(...params) {
133 return new Promise((resolve, reject) => {
134 this.stmt.all(...params, (err, rows) => {
135 if (err) {
136 return reject(err);
137 }
138 resolve(rows);
139 });
140 });
141 }
142 each(...params) {
143 return new Promise((resolve, reject) => {
144 const callback = params.pop();
145 if (!callback || typeof callback !== 'function') {
146 throw new Error('sqlite: Last param of Statement#each() must be a callback function');
147 }
148 if (params.length > 0) {
149 const positional = params.pop();
150 if (typeof positional === 'function') {
151 throw new Error('sqlite: Statement#each() should only have a single callback defined. See readme for usage.');
152 }
153 params.push(positional);
154 }
155 this.stmt.each(...params, (err, row) => {
156 if (err) {
157 return callback(err, null);
158 }
159 callback(null, row);
160 }, (err, count) => {
161 if (err) {
162 return reject(err);
163 }
164 resolve(count);
165 });
166 });
167 }
168}
169exports.Statement = Statement;
170//# sourceMappingURL=Statement.js.map
\No newline at end of file