@ladybugdb/core-win32-x64 0.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/connection.js +471 -0
- package/database.js +211 -0
- package/index.js +19 -0
- package/index.mjs +10 -0
- package/lbug.d.ts +399 -0
- package/lbug_native.js +25 -0
- package/lbugjs.node +0 -0
- package/package.json +29 -0
- package/prepared_statement.js +42 -0
- package/query_result.js +242 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const assert = require("assert");
|
|
4
|
+
|
|
5
|
+
class PreparedStatement {
|
|
6
|
+
/**
|
|
7
|
+
* Internal constructor. Use `Connection.prepare` to get a
|
|
8
|
+
* `PreparedStatement` object.
|
|
9
|
+
* @param {Connection} connection the connection object.
|
|
10
|
+
* @param {LbugNative.NodePreparedStatement} preparedStatement the native prepared statement object.
|
|
11
|
+
*/
|
|
12
|
+
constructor(connection, preparedStatement) {
|
|
13
|
+
assert(
|
|
14
|
+
typeof connection === "object" &&
|
|
15
|
+
connection.constructor.name === "Connection"
|
|
16
|
+
);
|
|
17
|
+
assert(
|
|
18
|
+
typeof preparedStatement === "object" &&
|
|
19
|
+
preparedStatement.constructor.name === "NodePreparedStatement"
|
|
20
|
+
);
|
|
21
|
+
this._connection = connection;
|
|
22
|
+
this._preparedStatement = preparedStatement;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if the prepared statement is successfully prepared.
|
|
27
|
+
* @returns {Boolean} true if the prepared statement is successfully prepared.
|
|
28
|
+
*/
|
|
29
|
+
isSuccess() {
|
|
30
|
+
return this._preparedStatement.isSuccess();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get the error message if the prepared statement is not successfully prepared.
|
|
35
|
+
* @returns {String} the error message.
|
|
36
|
+
*/
|
|
37
|
+
getErrorMessage() {
|
|
38
|
+
return this._preparedStatement.getErrorMessage();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = PreparedStatement;
|
package/query_result.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const assert = require("assert");
|
|
4
|
+
|
|
5
|
+
class QueryResult {
|
|
6
|
+
/**
|
|
7
|
+
* Internal constructor. Use `Connection.query` or `Connection.execute`
|
|
8
|
+
* to get a `QueryResult` object.
|
|
9
|
+
* @param {Connection} connection the connection object.
|
|
10
|
+
* @param {LbugNative.NodeQueryResult} queryResult the native query result object.
|
|
11
|
+
*/
|
|
12
|
+
constructor(connection, queryResult) {
|
|
13
|
+
assert(
|
|
14
|
+
typeof connection === "object" &&
|
|
15
|
+
connection.constructor.name === "Connection"
|
|
16
|
+
);
|
|
17
|
+
assert(
|
|
18
|
+
typeof queryResult === "object" &&
|
|
19
|
+
queryResult.constructor.name === "NodeQueryResult"
|
|
20
|
+
);
|
|
21
|
+
this._connection = connection;
|
|
22
|
+
this._queryResult = queryResult;
|
|
23
|
+
this._isClosed = false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Reset the iterator of the query result to the beginning.
|
|
28
|
+
* This function is useful if the query result is iterated multiple times.
|
|
29
|
+
*/
|
|
30
|
+
resetIterator() {
|
|
31
|
+
this._checkClosed();
|
|
32
|
+
this._queryResult.resetIterator();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check if the query result has more rows.
|
|
37
|
+
* @returns {Boolean} true if the query result has more rows.
|
|
38
|
+
*/
|
|
39
|
+
hasNext() {
|
|
40
|
+
this._checkClosed();
|
|
41
|
+
return this._queryResult.hasNext();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the number of rows of the query result.
|
|
46
|
+
* @returns {Number} the number of rows of the query result.
|
|
47
|
+
*/
|
|
48
|
+
getNumTuples() {
|
|
49
|
+
this._checkClosed();
|
|
50
|
+
return this._queryResult.getNumTuples();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the next row of the query result.
|
|
55
|
+
* @returns {Promise<Object>} a promise that resolves to the next row of the query result. The promise is rejected if there is an error.
|
|
56
|
+
*/
|
|
57
|
+
getNext() {
|
|
58
|
+
this._checkClosed();
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
this._queryResult.getNextAsync((err, result) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
return reject(err);
|
|
63
|
+
}
|
|
64
|
+
return resolve(result);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the next row of the query result synchronously.
|
|
71
|
+
* @returns {Object} the next row of the query result.
|
|
72
|
+
*/
|
|
73
|
+
getNextSync() {
|
|
74
|
+
this._checkClosed();
|
|
75
|
+
return this._queryResult.getNextSync();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Iterate through the query result with callback functions.
|
|
80
|
+
* @param {Function} resultCallback the callback function that is called for each row of the query result.
|
|
81
|
+
* @param {Function} doneCallback the callback function that is called when the iteration is done.
|
|
82
|
+
* @param {Function} errorCallback the callback function that is called when there is an error.
|
|
83
|
+
*/
|
|
84
|
+
each(resultCallback, doneCallback, errorCallback) {
|
|
85
|
+
this._checkClosed();
|
|
86
|
+
if (!this.hasNext()) {
|
|
87
|
+
return doneCallback();
|
|
88
|
+
}
|
|
89
|
+
this.getNext()
|
|
90
|
+
.then((row) => {
|
|
91
|
+
resultCallback(row);
|
|
92
|
+
this.each(resultCallback, doneCallback, errorCallback);
|
|
93
|
+
})
|
|
94
|
+
.catch((err) => {
|
|
95
|
+
errorCallback(err);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get all rows of the query result.
|
|
101
|
+
* @returns {Promise<Array<Object>>} a promise that resolves to all rows of the query result. The promise is rejected if there is an error.
|
|
102
|
+
*/
|
|
103
|
+
async getAll() {
|
|
104
|
+
this._checkClosed();
|
|
105
|
+
this._queryResult.resetIterator();
|
|
106
|
+
const result = [];
|
|
107
|
+
while (this.hasNext()) {
|
|
108
|
+
result.push(await this.getNext());
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get all rows of the query result synchronously. Note that this function can block the main thread if the number of rows is large, so use it with caution.
|
|
115
|
+
* @returns {Array<Object>} all rows of the query result.
|
|
116
|
+
*/
|
|
117
|
+
getAllSync() {
|
|
118
|
+
this._checkClosed();
|
|
119
|
+
this._queryResult.resetIterator();
|
|
120
|
+
const result = [];
|
|
121
|
+
while (this.hasNext()) {
|
|
122
|
+
result.push(this.getNextSync());
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get all rows of the query result with callback functions.
|
|
129
|
+
* @param {Function} resultCallback the callback function that is called with all rows of the query result.
|
|
130
|
+
* @param {Function} errorCallback the callback function that is called when there is an error.
|
|
131
|
+
*/
|
|
132
|
+
all(resultCallback, errorCallback) {
|
|
133
|
+
this._checkClosed();
|
|
134
|
+
this.getAll()
|
|
135
|
+
.then((result) => {
|
|
136
|
+
resultCallback(result);
|
|
137
|
+
})
|
|
138
|
+
.catch((err) => {
|
|
139
|
+
errorCallback(err);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get the data types of the columns of the query result.
|
|
145
|
+
* @returns {Promise<Array<String>>} a promise that resolves to the data types of the columns of the query result. The promise is rejected if there is an error.
|
|
146
|
+
*/
|
|
147
|
+
getColumnDataTypes() {
|
|
148
|
+
this._checkClosed();
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
this._queryResult.getColumnDataTypesAsync((err, result) => {
|
|
151
|
+
if (err) {
|
|
152
|
+
return reject(err);
|
|
153
|
+
}
|
|
154
|
+
return resolve(result);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the data types of the columns of the query result synchronously.
|
|
161
|
+
* @returns {Array<String>} the data types of the columns of the query result.
|
|
162
|
+
*/
|
|
163
|
+
getColumnDataTypesSync() {
|
|
164
|
+
this._checkClosed();
|
|
165
|
+
return this._queryResult.getColumnDataTypesSync();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get the names of the columns of the query result.
|
|
170
|
+
* @returns {Promise<Array<String>>} a promise that resolves to the names of the columns of the query result. The promise is rejected if there is an error.
|
|
171
|
+
*/
|
|
172
|
+
getColumnNames() {
|
|
173
|
+
this._checkClosed();
|
|
174
|
+
return new Promise((resolve, reject) => {
|
|
175
|
+
this._queryResult.getColumnNamesAsync((err, result) => {
|
|
176
|
+
if (err) {
|
|
177
|
+
return reject(err);
|
|
178
|
+
}
|
|
179
|
+
return resolve(result);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get the names of the columns of the query result synchronously.
|
|
186
|
+
* @returns {Array<String>} the names of the columns of the query result.
|
|
187
|
+
*/
|
|
188
|
+
getColumnNamesSync() {
|
|
189
|
+
this._checkClosed();
|
|
190
|
+
return this._queryResult.getColumnNamesSync();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Get the query summary (compiling and execution time) of the query result.
|
|
195
|
+
* @returns {Promise<Object>} a promise that resolves to the query summary of the query result. The promise is rejected if there is an error.
|
|
196
|
+
*/
|
|
197
|
+
getQuerySummary() {
|
|
198
|
+
this._checkClosed();
|
|
199
|
+
return new Promise((resolve, reject) => {
|
|
200
|
+
this._queryResult.getQuerySummaryAsync((err, result) => {
|
|
201
|
+
if (err) {
|
|
202
|
+
return reject(err);
|
|
203
|
+
}
|
|
204
|
+
return resolve(result);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Get the query summary (compiling and execution time) of the query result synchronously.
|
|
211
|
+
* @returns {Object} the query summary of the query result.
|
|
212
|
+
*/
|
|
213
|
+
getQuerySummarySync() {
|
|
214
|
+
this._checkClosed();
|
|
215
|
+
return this._queryResult.getQuerySummarySync();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Close the query result.
|
|
220
|
+
*/
|
|
221
|
+
close() {
|
|
222
|
+
if (this._isClosed) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
this._queryResult.close();
|
|
226
|
+
delete this._queryResult;
|
|
227
|
+
this._connection = null;
|
|
228
|
+
this._isClosed = true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Internal function to check if the query result is closed.
|
|
233
|
+
* @throws {Error} if the query result is closed.
|
|
234
|
+
*/
|
|
235
|
+
_checkClosed() {
|
|
236
|
+
if (this._isClosed) {
|
|
237
|
+
throw new Error("Query result is closed.");
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
module.exports = QueryResult;
|