@miatechnet/node-odbc 2.4.10-multiresult.1 → 2.4.13

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/lib/Statement.js CHANGED
@@ -1,207 +1,207 @@
1
- const { Cursor } = require('./Cursor');
2
-
3
- class Statement {
4
-
5
- STATEMENT_CLOSED_ERROR = 'Statement has already been closed!';
6
-
7
- constructor(odbcStatement) {
8
- this.odbcStatement = odbcStatement;
9
- }
10
-
11
- /**
12
- * Prepare an SQL statement template to which parameters can be bound and the statement then executed.
13
- * @param {string} sql - The SQL statement template to prepare, with or without unspecified parameters.
14
- * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
15
- * @returns {undefined|Promise}
16
- */
17
- prepare(sql, callback = undefined) {
18
- if (typeof sql !== 'string'
19
- || (typeof callback !== 'function' && typeof callback !== 'undefined')) {
20
- throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.prepare({string}, {function}[optional]).');
21
- }
22
-
23
- // promise...
24
- if (typeof callback === 'undefined') {
25
- if (!this.odbcStatement)
26
- {
27
- throw new Error(Statement.STATEMENT_CLOSED_ERROR);
28
- }
29
-
30
- return new Promise((resolve, reject) => {
31
- this.odbcStatement.prepare(sql, (error, result) => {
32
- if (error) {
33
- reject(error);
34
- } else {
35
- resolve(result);
36
- }
37
- });
38
- });
39
- }
40
-
41
- // ...or callback
42
- if (!this.odbcStatement)
43
- {
44
- callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
45
- } else {
46
- this.odbcStatement.prepare(sql, callback);
47
- }
48
- }
49
-
50
- /**
51
- * Bind parameters on the previously prepared SQL statement template.
52
- * @param {*[]} parameters - The parameters to bind to the previously prepared SQL statement.
53
- * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
54
- * @return {undefined|Promise}
55
- */
56
- bind(parameters, callback = undefined) {
57
- if (!Array.isArray(parameters)
58
- || (typeof callback !== 'function' && typeof callback !== 'undefined')) {
59
- throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.bind({array}, {function}[optional]).');
60
- }
61
-
62
- // promise...
63
- if (typeof callback === 'undefined') {
64
- if (!this.odbcStatement)
65
- {
66
- throw new Error(Statement.STATEMENT_CLOSED_ERROR);
67
- }
68
-
69
- return new Promise((resolve, reject) => {
70
- this.odbcStatement.bind(parameters, (error, result) => {
71
- if (error) {
72
- reject(error);
73
- } else {
74
- resolve(result);
75
- }
76
- });
77
- });
78
- }
79
-
80
- // ...or callback
81
- if (!this.odbcStatement)
82
- {
83
- callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
84
- } else {
85
- this.odbcStatement.bind(parameters, callback);
86
- }
87
- }
88
-
89
- /**
90
- * Executes the prepared SQL statement template with the bound parameters, returning the result.
91
- * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
92
- */
93
- execute(options, callback = undefined) {
94
-
95
- if (options === undefined)
96
- {
97
- options = null;
98
- }
99
-
100
- if (typeof options === 'function' && callback === undefined) {
101
- callback = options;
102
- options = null
103
- }
104
-
105
- if ((typeof callback !== 'function' && typeof callback !== 'undefined')
106
- || typeof options !== 'object' && typeof options !== 'undefined' ) {
107
- throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.execute({object}[optional], {function}[optional]).');
108
- }
109
-
110
- // Promise...
111
- if (typeof callback === 'undefined') {
112
- if (!this.odbcStatement)
113
- {
114
- throw new Error(Statement.STATEMENT_CLOSED_ERROR);
115
- }
116
-
117
- return new Promise((resolve, reject) => {
118
- this.odbcStatement.execute(options, (error, result) => {
119
- if (error) {
120
- reject(error);
121
- } else {
122
- if (options &&
123
- (
124
- options.hasOwnProperty('fetchSize') ||
125
- options.hasOwnProperty('cursor')
126
- )
127
- )
128
- {
129
- const cursor = new Cursor(result);
130
- resolve(cursor);
131
- }
132
- else
133
- {
134
- resolve(result);
135
- }
136
- }
137
- });
138
- });
139
- }
140
-
141
- // ...or callback
142
- if (!this.odbcStatement)
143
- {
144
- callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
145
- } else {
146
- process.nextTick(() => {
147
- if (options &&
148
- (
149
- options.hasOwnProperty('fetchSize') ||
150
- options.hasOwnProperty('cursor')
151
- )
152
- )
153
- {
154
- this.odbcStatement.execute(options, (error, result) => {
155
- if (error) {
156
- return callback(error);
157
- }
158
-
159
- const cursor = new Cursor(result);
160
- return callback(error, cursor);
161
- });
162
- }
163
- else
164
- {
165
- this.odbcStatement.execute(options, callback);
166
- }
167
- });
168
- }
169
- }
170
-
171
- /**
172
- * Closes the statement, deleting the prepared statement and freeing the handle, making further
173
- * calls on the object invalid.
174
- * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
175
- */
176
- close(callback = undefined) {
177
- if (typeof callback !== 'function' && typeof callback !== 'undefined') {
178
- throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.close({function}[optional]).');
179
- }
180
-
181
- if (typeof callback === 'undefined') {
182
- if (!this.odbcStatement) {
183
- throw new Error(Statement.STATEMENT_CLOSED_ERROR);
184
- }
185
- return new Promise((resolve, reject) => {
186
- this.odbcStatement.close((error) => {
187
- if (error) {
188
- reject(error);
189
- } else {
190
- this.odbcStatement = null;
191
- resolve();
192
- }
193
- });
194
- });
195
- }
196
-
197
- // ...or callback
198
- return this.odbcStatement.close((error) => {
199
- if (!error) {
200
- this.odbcStatement = null;
201
- }
202
- callback(error);
203
- });
204
- }
205
- }
206
-
207
- module.exports.Statement = Statement;
1
+ const { Cursor } = require('./Cursor');
2
+
3
+ class Statement {
4
+
5
+ STATEMENT_CLOSED_ERROR = 'Statement has already been closed!';
6
+
7
+ constructor(odbcStatement) {
8
+ this.odbcStatement = odbcStatement;
9
+ }
10
+
11
+ /**
12
+ * Prepare an SQL statement template to which parameters can be bound and the statement then executed.
13
+ * @param {string} sql - The SQL statement template to prepare, with or without unspecified parameters.
14
+ * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
15
+ * @returns {undefined|Promise}
16
+ */
17
+ prepare(sql, callback = undefined) {
18
+ if (typeof sql !== 'string'
19
+ || (typeof callback !== 'function' && typeof callback !== 'undefined')) {
20
+ throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.prepare({string}, {function}[optional]).');
21
+ }
22
+
23
+ // promise...
24
+ if (typeof callback === 'undefined') {
25
+ if (!this.odbcStatement)
26
+ {
27
+ throw new Error(Statement.STATEMENT_CLOSED_ERROR);
28
+ }
29
+
30
+ return new Promise((resolve, reject) => {
31
+ this.odbcStatement.prepare(sql, (error, result) => {
32
+ if (error) {
33
+ reject(error);
34
+ } else {
35
+ resolve(result);
36
+ }
37
+ });
38
+ });
39
+ }
40
+
41
+ // ...or callback
42
+ if (!this.odbcStatement)
43
+ {
44
+ callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
45
+ } else {
46
+ this.odbcStatement.prepare(sql, callback);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Bind parameters on the previously prepared SQL statement template.
52
+ * @param {*[]} parameters - The parameters to bind to the previously prepared SQL statement.
53
+ * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
54
+ * @return {undefined|Promise}
55
+ */
56
+ bind(parameters, callback = undefined) {
57
+ if (!Array.isArray(parameters)
58
+ || (typeof callback !== 'function' && typeof callback !== 'undefined')) {
59
+ throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.bind({array}, {function}[optional]).');
60
+ }
61
+
62
+ // promise...
63
+ if (typeof callback === 'undefined') {
64
+ if (!this.odbcStatement)
65
+ {
66
+ throw new Error(Statement.STATEMENT_CLOSED_ERROR);
67
+ }
68
+
69
+ return new Promise((resolve, reject) => {
70
+ this.odbcStatement.bind(parameters, (error, result) => {
71
+ if (error) {
72
+ reject(error);
73
+ } else {
74
+ resolve(result);
75
+ }
76
+ });
77
+ });
78
+ }
79
+
80
+ // ...or callback
81
+ if (!this.odbcStatement)
82
+ {
83
+ callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
84
+ } else {
85
+ this.odbcStatement.bind(parameters, callback);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Executes the prepared SQL statement template with the bound parameters, returning the result.
91
+ * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
92
+ */
93
+ execute(options, callback = undefined) {
94
+
95
+ if (options === undefined)
96
+ {
97
+ options = null;
98
+ }
99
+
100
+ if (typeof options === 'function' && callback === undefined) {
101
+ callback = options;
102
+ options = null
103
+ }
104
+
105
+ if ((typeof callback !== 'function' && typeof callback !== 'undefined')
106
+ || typeof options !== 'object' && typeof options !== 'undefined' ) {
107
+ throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.execute({object}[optional], {function}[optional]).');
108
+ }
109
+
110
+ // Promise...
111
+ if (typeof callback === 'undefined') {
112
+ if (!this.odbcStatement)
113
+ {
114
+ throw new Error(Statement.STATEMENT_CLOSED_ERROR);
115
+ }
116
+
117
+ return new Promise((resolve, reject) => {
118
+ this.odbcStatement.execute(options, (error, result) => {
119
+ if (error) {
120
+ reject(error);
121
+ } else {
122
+ if (options &&
123
+ (
124
+ options.hasOwnProperty('fetchSize') ||
125
+ options.hasOwnProperty('cursor')
126
+ )
127
+ )
128
+ {
129
+ const cursor = new Cursor(result);
130
+ resolve(cursor);
131
+ }
132
+ else
133
+ {
134
+ resolve(result);
135
+ }
136
+ }
137
+ });
138
+ });
139
+ }
140
+
141
+ // ...or callback
142
+ if (!this.odbcStatement)
143
+ {
144
+ callback(new Error(Statement.STATEMENT_CLOSED_ERROR));
145
+ } else {
146
+ process.nextTick(() => {
147
+ if (options &&
148
+ (
149
+ options.hasOwnProperty('fetchSize') ||
150
+ options.hasOwnProperty('cursor')
151
+ )
152
+ )
153
+ {
154
+ this.odbcStatement.execute(options, (error, result) => {
155
+ if (error) {
156
+ return callback(error);
157
+ }
158
+
159
+ const cursor = new Cursor(result);
160
+ return callback(error, cursor);
161
+ });
162
+ }
163
+ else
164
+ {
165
+ this.odbcStatement.execute(options, callback);
166
+ }
167
+ });
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Closes the statement, deleting the prepared statement and freeing the handle, making further
173
+ * calls on the object invalid.
174
+ * @param {function} [callback] - The callback function that returns the result. If omitted, uses a Promise.
175
+ */
176
+ close(callback = undefined) {
177
+ if (typeof callback !== 'function' && typeof callback !== 'undefined') {
178
+ throw new TypeError('[node-odbc]: Incorrect function signature for call to statement.close({function}[optional]).');
179
+ }
180
+
181
+ if (typeof callback === 'undefined') {
182
+ if (!this.odbcStatement) {
183
+ throw new Error(Statement.STATEMENT_CLOSED_ERROR);
184
+ }
185
+ return new Promise((resolve, reject) => {
186
+ this.odbcStatement.close((error) => {
187
+ if (error) {
188
+ reject(error);
189
+ } else {
190
+ this.odbcStatement = null;
191
+ resolve();
192
+ }
193
+ });
194
+ });
195
+ }
196
+
197
+ // ...or callback
198
+ return this.odbcStatement.close((error) => {
199
+ if (!error) {
200
+ this.odbcStatement = null;
201
+ }
202
+ callback(error);
203
+ });
204
+ }
205
+ }
206
+
207
+ module.exports.Statement = Statement;