vsql-node 1.2.7 → 1.2.8

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/README.md CHANGED
@@ -266,9 +266,37 @@ try {
266
266
  }
267
267
  ```
268
268
 
269
+ `diagnostics` are the other conditions that vsql raised with the error, each an
270
+ object of `sqlstate`, `message` and the same fields (it's usually empty):
271
+
272
+ ```js
273
+ try {
274
+ db.all('SELECT SUM(x) / 0 FROM t');
275
+ } catch (e) {
276
+ console.log(e.sqlstate, e.diagnostics); // 22012 [{ sqlstate: '01003', message: 'null value eliminated in set function' }]
277
+ }
278
+ ```
279
+
269
280
  An error from the driver itself (such as using a closed connection, or a value
270
281
  that can't be a parameter) is a `TypeError` or `RangeError`.
271
282
 
283
+ ### Warnings
284
+
285
+ A statement that succeeds with a warning from vsql (such as `01003` when a
286
+ `NULL` is eliminated from `SUM`, or `01004` when a string is truncated) gives a
287
+ `vsql.VsqlWarning` to `process.emitWarning`, which is how Node reports problems
288
+ that shouldn't stop the program. It has the same fields as a `VsqlError`. Node
289
+ prints warnings to stderr, unless they're handled with `process.on('warning')`
290
+ or Node is run with `--no-warnings` (or `--disable-warning=VsqlWarning`):
291
+
292
+ ```js
293
+ process.on('warning', (w) => {
294
+ if (w instanceof vsql.VsqlWarning) {
295
+ console.log(w.sqlstate, w.message); // 01003 null value eliminated in set function (SQLSTATE 01003)
296
+ }
297
+ });
298
+ ```
299
+
272
300
  ## Threads
273
301
 
274
302
  Each connection has its own libvsql environment, so connections in different
package/index.d.ts CHANGED
@@ -225,6 +225,43 @@ export class VsqlError extends Error {
225
225
  readonly cursorName?: string;
226
226
  /** Whether vsql raised more conditions than it kept. */
227
227
  readonly more: boolean;
228
+ /** The other conditions that vsql raised with the error (usually none). */
229
+ readonly diagnostics: Diagnostic[];
230
+ }
231
+
232
+ /** A condition that vsql raised with an error. */
233
+ export interface Diagnostic {
234
+ /** The SQLSTATE, such as "01003". */
235
+ readonly sqlstate: string;
236
+ readonly message: string;
237
+ readonly constraintCatalog?: string;
238
+ readonly constraintSchema?: string;
239
+ readonly constraintName?: string;
240
+ readonly catalogName?: string;
241
+ readonly schemaName?: string;
242
+ readonly tableName?: string;
243
+ readonly columnName?: string;
244
+ readonly cursorName?: string;
245
+ }
246
+
247
+ /**
248
+ * A warning from vsql (such as "01003" when a NULL is eliminated from SUM),
249
+ * which is given to process.emitWarning.
250
+ */
251
+ export class VsqlWarning extends Error {
252
+ readonly name: 'VsqlWarning';
253
+ /** The SQLSTATE, such as "01003". */
254
+ readonly sqlstate: string;
255
+ /** The same as sqlstate. */
256
+ readonly code: string;
257
+ readonly constraintCatalog?: string;
258
+ readonly constraintSchema?: string;
259
+ readonly constraintName?: string;
260
+ readonly catalogName?: string;
261
+ readonly schemaName?: string;
262
+ readonly tableName?: string;
263
+ readonly columnName?: string;
264
+ readonly cursorName?: string;
228
265
  }
229
266
 
230
267
  /**
package/lib/binding.js CHANGED
@@ -4,10 +4,17 @@
4
4
 
5
5
  const path = require('node:path');
6
6
 
7
- const { VsqlError } = require('./errors');
7
+ const { VsqlError, VsqlWarning } = require('./errors');
8
8
 
9
9
  const lib = require('node-gyp-build')(path.join(__dirname, '..'));
10
10
 
11
- lib.setErrorFactory((sqlstate, message, fields) => new VsqlError(sqlstate, message, fields));
11
+ lib.setErrorFactory(
12
+ (sqlstate, message, fields) => new VsqlError(sqlstate, message, fields),
13
+ (records) => {
14
+ for (const record of records) {
15
+ process.emitWarning(new VsqlWarning(record.sqlstate, record.message, record));
16
+ }
17
+ },
18
+ );
12
19
 
13
20
  module.exports = lib;
package/lib/errors.js CHANGED
@@ -11,17 +11,47 @@ class VsqlError extends Error {
11
11
  // constraintName), the table or column (catalogName, schemaName, tableName
12
12
  // and columnName) and the cursor (cursorName). more is true if vsql raised
13
13
  // more conditions than it kept.
14
+ //
15
+ // diagnostics are the other conditions that vsql raised with the error (each
16
+ // an object of sqlstate, message and the same fields), which is usually
17
+ // empty.
14
18
  constructor(sqlstate, message, fields = {}) {
15
19
  super(`${message} (SQLSTATE ${sqlstate})`);
16
20
  this.name = 'VsqlError';
17
21
  this.sqlstate = sqlstate;
18
22
  this.code = sqlstate;
19
- for (const name of FIELDS) {
20
- if (fields[name] !== undefined) {
21
- this[name] = fields[name];
22
- }
23
- }
23
+ setFields(this, fields);
24
24
  this.more = Boolean(fields.more);
25
+ this.diagnostics = (fields.diagnostics ?? []).map((record) => {
26
+ const d = { sqlstate: record.sqlstate, message: record.message };
27
+ setFields(d, record);
28
+ return d;
29
+ });
30
+ }
31
+ }
32
+
33
+ // VsqlWarning is a warning from vsql (a completion condition, such as "01003"
34
+ // when a NULL is eliminated from SUM), when a statement still succeeds. Each
35
+ // warning is given to process.emitWarning, which is how Node reports problems
36
+ // that shouldn't stop the program: they're printed to stderr, unless there's
37
+ // a listener for process.on('warning') or Node is run with --no-warnings (or
38
+ // --disable-warning=VsqlWarning). It has the same fields as VsqlError.
39
+ class VsqlWarning extends Error {
40
+ constructor(sqlstate, message, fields = {}) {
41
+ super(`${message} (SQLSTATE ${sqlstate})`);
42
+ this.name = 'VsqlWarning';
43
+ this.sqlstate = sqlstate;
44
+ this.code = sqlstate;
45
+ setFields(this, fields);
46
+ }
47
+ }
48
+
49
+ // setFields copies the diagnostic fields that vsql gave.
50
+ function setFields(target, fields) {
51
+ for (const name of FIELDS) {
52
+ if (fields[name] !== undefined) {
53
+ target[name] = fields[name];
54
+ }
25
55
  }
26
56
  }
27
57
 
@@ -36,4 +66,4 @@ const FIELDS = [
36
66
  'cursorName',
37
67
  ];
38
68
 
39
- module.exports = { VsqlError };
69
+ module.exports = { VsqlError, VsqlWarning };
package/lib/index.js CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  const { Connection, Statement } = require('./connection');
20
20
  const { SqlValue, numeric, date, time, timestamp } = require('./convert');
21
- const { VsqlError } = require('./errors');
21
+ const { VsqlError, VsqlWarning } = require('./errors');
22
22
 
23
23
  // connect opens a connection to a database file (which is created if it
24
24
  // doesn't exist), or ":memory:" for a database that only exists for the life
@@ -33,6 +33,7 @@ module.exports = {
33
33
  Connection,
34
34
  Statement,
35
35
  VsqlError,
36
+ VsqlWarning,
36
37
  SqlValue,
37
38
  numeric,
38
39
  date,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vsql-node",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "A Node.js driver for vsql, a single file or in-memory SQL database.",
5
5
  "keywords": [
6
6
  "vsql",
package/src/binding.c CHANGED
@@ -8,7 +8,9 @@
8
8
  //
9
9
  // When a routine fails, the error is made by the function given to
10
10
  // setErrorFactory, from the SQLSTATE, message and other fields (see
11
- // diag_fields) of the first diagnostic record.
11
+ // diag_fields) of the first diagnostic record, with the other records as
12
+ // "diagnostics". When it succeeds with warnings (SQL_SUCCESS_WITH_INFO), the
13
+ // records are given to the warning function given to setErrorFactory.
12
14
 
13
15
  #include <stdlib.h>
14
16
  #include <string.h>
@@ -44,6 +46,7 @@ static void vsql_gc_after_node_init(void) {
44
46
  // The data of each instance of the addon (one for each thread that loads it).
45
47
  typedef struct {
46
48
  napi_ref error_factory;
49
+ napi_ref warn;
47
50
  } instance;
48
51
 
49
52
  // throw_napi_error throws an error for the Node-API call that just failed, unless
@@ -144,10 +147,11 @@ static const struct {
144
147
  {NULL, 0},
145
148
  };
146
149
 
147
- // get_diag_fields is an object of the diagnostic fields of the first record
148
- // (see diag_fields), and "more" (SQL_DIAG_MORE) if a condition was dropped.
149
- // It's NULL if the fields can't be read.
150
- static napi_value get_diag_fields(napi_env env, SQLSMALLINT handle_type, SQLINTEGER handle) {
150
+ // get_diag_fields is an object of the diagnostic fields of a record (see
151
+ // diag_fields), and for the first record "more" (SQL_DIAG_MORE, a header
152
+ // field) if a condition was dropped. It's NULL if the fields can't be read.
153
+ static napi_value get_diag_fields(napi_env env, SQLSMALLINT handle_type, SQLINTEGER handle,
154
+ SQLSMALLINT record) {
151
155
  napi_value result, value;
152
156
  if (napi_create_object(env, &result) != napi_ok) {
153
157
  return NULL;
@@ -157,7 +161,7 @@ static napi_value get_diag_fields(napi_env env, SQLSMALLINT handle_type, SQLINTE
157
161
  SQLCHAR buf[4 * 128 + 1];
158
162
  for (int i = 0; diag_fields[i].name != NULL; i++) {
159
163
  SQLSMALLINT length = 0;
160
- SQLRETURN rc = SQLGetDiagField(handle_type, handle, 1, diag_fields[i].field, buf,
164
+ SQLRETURN rc = SQLGetDiagField(handle_type, handle, record, diag_fields[i].field, buf,
161
165
  sizeof(buf), &length);
162
166
  if ((rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) || length <= 0) {
163
167
  continue;
@@ -173,7 +177,7 @@ static napi_value get_diag_fields(napi_env env, SQLSMALLINT handle_type, SQLINTE
173
177
 
174
178
  SQLINTEGER more = 0;
175
179
  SQLRETURN rc = SQLGetDiagField(handle_type, handle, 0, SQL_DIAG_MORE, &more, 0, NULL);
176
- if ((rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) && more) {
180
+ if (record == 1 && (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) && more) {
177
181
  if (napi_get_boolean(env, true, &value) != napi_ok ||
178
182
  napi_set_named_property(env, result, "more", value) != napi_ok) {
179
183
  return NULL;
@@ -183,49 +187,141 @@ static napi_value get_diag_fields(napi_env env, SQLSMALLINT handle_type, SQLINTE
183
187
  return result;
184
188
  }
185
189
 
186
- // check returns 0 for a return code that isn't an error (SQL_NO_DATA isn't),
187
- // otherwise it throws the error from the diagnostics of the handle and returns
188
- // -1.
189
- static int check(napi_env env, SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
190
- switch (rc) {
191
- case SQL_SUCCESS:
192
- case SQL_SUCCESS_WITH_INFO:
193
- case SQL_NO_DATA:
194
- return 0;
195
- case SQL_INVALID_HANDLE:
196
- throw_error(env, "HY000", "invalid handle", NAPI_AUTO_LENGTH);
197
- return -1;
198
- }
199
-
200
- SQLCHAR sqlstate[6];
201
- SQLSMALLINT size = 512, length = 0;
202
- SQLCHAR *message = malloc(size);
190
+ // get_message reads the SQLSTATE and message of a diagnostic record. The
191
+ // message must be freed. It returns -1 if there's no such record, or -2 if it's
192
+ // out of memory.
193
+ static int get_message(SQLSMALLINT handle_type, SQLINTEGER handle, SQLSMALLINT record,
194
+ SQLCHAR sqlstate[6], SQLCHAR **message, SQLSMALLINT *length) {
195
+ SQLSMALLINT size = 512;
203
196
  for (;;) {
204
- if (message == NULL) {
205
- napi_throw_error(env, NULL, "out of memory");
206
- return -1;
197
+ *message = malloc(size);
198
+ if (*message == NULL) {
199
+ return -2;
207
200
  }
208
201
 
209
- rc = SQLGetDiagRec(handle_type, handle, 1, sqlstate, NULL, message, size, &length);
202
+ SQLRETURN rc = SQLGetDiagRec(handle_type, handle, record, sqlstate, NULL, *message, size,
203
+ length);
210
204
  if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
211
- free(message);
212
- throw_error(env, "HY000", "unknown error", NAPI_AUTO_LENGTH);
205
+ free(*message);
206
+ *message = NULL;
213
207
  return -1;
214
208
  }
215
209
 
216
210
  // The message was truncated.
217
- if (length >= size) {
218
- size = length + 1;
219
- free(message);
220
- message = malloc(size);
211
+ if (*length >= size) {
212
+ size = *length + 1;
213
+ free(*message);
221
214
  continue;
222
215
  }
223
216
 
224
- napi_value fields = get_diag_fields(env, handle_type, handle);
225
- throw_error_fields(env, (const char *)sqlstate, (const char *)message, length, fields);
217
+ return 0;
218
+ }
219
+ }
220
+
221
+ // get_records is an array of the diagnostic records from first, each an object
222
+ // of its sqlstate, message and fields (see get_diag_fields). It's NULL if they
223
+ // can't be read.
224
+ static napi_value get_records(napi_env env, SQLSMALLINT handle_type, SQLINTEGER handle,
225
+ SQLSMALLINT first) {
226
+ SQLINTEGER number = 0;
227
+ SQLRETURN rc = SQLGetDiagField(handle_type, handle, 0, SQL_DIAG_NUMBER, &number, 0, NULL);
228
+ if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
229
+ number = 0;
230
+ }
231
+
232
+ napi_value records;
233
+ if (napi_create_array(env, &records) != napi_ok) {
234
+ return NULL;
235
+ }
236
+ for (SQLINTEGER i = first; i <= number; i++) {
237
+ SQLCHAR sqlstate[6], *message = NULL;
238
+ SQLSMALLINT length = 0;
239
+ if (get_message(handle_type, handle, (SQLSMALLINT)i, sqlstate, &message, &length) < 0) {
240
+ break;
241
+ }
242
+
243
+ napi_value record = get_diag_fields(env, handle_type, handle, (SQLSMALLINT)i), value;
244
+ napi_status status = record == NULL ? napi_generic_failure
245
+ : napi_create_string_utf8(env, (const char *)message,
246
+ length, &value);
226
247
  free(message);
248
+ if (status != napi_ok || napi_set_named_property(env, record, "message", value) != napi_ok ||
249
+ napi_create_string_utf8(env, (const char *)sqlstate, NAPI_AUTO_LENGTH, &value) !=
250
+ napi_ok ||
251
+ napi_set_named_property(env, record, "sqlstate", value) != napi_ok ||
252
+ napi_set_element(env, records, (uint32_t)(i - first), record) != napi_ok) {
253
+ return NULL;
254
+ }
255
+ }
256
+
257
+ return records;
258
+ }
259
+
260
+ // warn gives the diagnostic records of a routine that succeeded with warnings
261
+ // to the warning function. It returns -1 if that throws.
262
+ static int warn(napi_env env, SQLSMALLINT handle_type, SQLINTEGER handle) {
263
+ instance *inst = NULL;
264
+ napi_value fn = NULL, records, global, result;
265
+ napi_get_instance_data(env, (void **)&inst);
266
+ if (inst == NULL || inst->warn == NULL ||
267
+ napi_get_reference_value(env, inst->warn, &fn) != napi_ok || fn == NULL) {
268
+ return 0;
269
+ }
270
+
271
+ records = get_records(env, handle_type, handle, 1);
272
+ if (records == NULL) {
273
+ return 0;
274
+ }
275
+ if (napi_get_global(env, &global) != napi_ok ||
276
+ napi_call_function(env, global, fn, 1, &records, &result) != napi_ok) {
277
+ return -1;
278
+ }
279
+
280
+ return 0;
281
+ }
282
+
283
+ // check_warn returns 0 for a return code that isn't an error (SQL_NO_DATA
284
+ // isn't), otherwise it throws the error from the diagnostics of the handle and
285
+ // returns -1. If warnings is 1, the warnings of SQL_SUCCESS_WITH_INFO are
286
+ // given to the warning function (see warn). It's 0 when the routine is
287
+ // expected to truncate a value (01004) that's then read in pieces.
288
+ static int check_warn(napi_env env, SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle,
289
+ int warnings) {
290
+ switch (rc) {
291
+ case SQL_SUCCESS_WITH_INFO:
292
+ return warnings ? warn(env, handle_type, handle) : 0;
293
+ case SQL_SUCCESS:
294
+ case SQL_NO_DATA:
295
+ return 0;
296
+ case SQL_INVALID_HANDLE:
297
+ throw_error(env, "HY000", "invalid handle", NAPI_AUTO_LENGTH);
227
298
  return -1;
228
299
  }
300
+
301
+ SQLCHAR sqlstate[6], *message = NULL;
302
+ SQLSMALLINT length = 0;
303
+ switch (get_message(handle_type, handle, 1, sqlstate, &message, &length)) {
304
+ case -2:
305
+ napi_throw_error(env, NULL, "out of memory");
306
+ return -1;
307
+ case -1:
308
+ throw_error(env, "HY000", "unknown error", NAPI_AUTO_LENGTH);
309
+ return -1;
310
+ }
311
+
312
+ // The other records (if any) are "diagnostics".
313
+ napi_value fields = get_diag_fields(env, handle_type, handle, 1);
314
+ napi_value records = fields == NULL ? NULL : get_records(env, handle_type, handle, 2);
315
+ if (records != NULL) {
316
+ napi_set_named_property(env, fields, "diagnostics", records);
317
+ }
318
+ throw_error_fields(env, (const char *)sqlstate, (const char *)message, length, fields);
319
+ free(message);
320
+ return -1;
321
+ }
322
+
323
+ static int check(napi_env env, SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
324
+ return check_warn(env, rc, handle_type, handle, 1);
229
325
  }
230
326
 
231
327
  // CHECK returns NULL from the function if a libvsql routine fails.
@@ -292,16 +388,22 @@ static napi_value number(napi_env env, double n) {
292
388
  return result;
293
389
  }
294
390
 
295
- // setErrorFactory(factory)
391
+ // setErrorFactory(factory, warn) sets the function that makes errors, and the
392
+ // function that's called with the records of warnings.
296
393
  static napi_value set_error_factory(napi_env env, napi_callback_info info) {
297
- ARGS(1);
394
+ ARGS(2);
298
395
  instance *inst = NULL;
299
396
  NAPI_CALL(napi_get_instance_data(env, (void **)&inst));
300
397
  if (inst->error_factory != NULL) {
301
398
  napi_delete_reference(env, inst->error_factory);
302
399
  inst->error_factory = NULL;
303
400
  }
401
+ if (inst->warn != NULL) {
402
+ napi_delete_reference(env, inst->warn);
403
+ inst->warn = NULL;
404
+ }
304
405
  NAPI_CALL(napi_create_reference(env, argv[0], 1, &inst->error_factory));
406
+ NAPI_CALL(napi_create_reference(env, argv[1], 1, &inst->warn));
305
407
 
306
408
  return undefined(env);
307
409
  }
@@ -743,7 +845,7 @@ static napi_value describe_col(napi_env env, napi_callback_info info) {
743
845
 
744
846
  SQLRETURN rc = SQLDescribeCol(stmt, (SQLSMALLINT)n, name, size, &length, &data_type,
745
847
  &column_size, &decimal_digits, &nullable);
746
- if (check(env, rc, SQL_HANDLE_STMT, stmt) < 0) {
848
+ if (check_warn(env, rc, SQL_HANDLE_STMT, stmt, 0) < 0) {
747
849
  free(name);
748
850
  return NULL;
749
851
  }
@@ -792,7 +894,7 @@ static napi_value get_chars(napi_env env, SQLINTEGER stmt, SQLSMALLINT n) {
792
894
  size_t length = 0;
793
895
  for (;;) {
794
896
  SQLRETURN rc = SQLGetData(stmt, n, SQL_CHAR, buf + length, size - (SQLINTEGER)length, &ind);
795
- if (check(env, rc, SQL_HANDLE_STMT, stmt) < 0) {
897
+ if (check_warn(env, rc, SQL_HANDLE_STMT, stmt, 0) < 0) {
796
898
  free(large);
797
899
  return NULL;
798
900
  }
@@ -841,7 +943,7 @@ static napi_value get_octets(napi_env env, SQLINTEGER stmt, SQLSMALLINT n) {
841
943
  for (;;) {
842
944
  SQLRETURN rc =
843
945
  SQLGetData(stmt, n, SQL_BINARY, buf + length, size - (SQLINTEGER)length, &ind);
844
- if (check(env, rc, SQL_HANDLE_STMT, stmt) < 0) {
946
+ if (check_warn(env, rc, SQL_HANDLE_STMT, stmt, 0) < 0) {
845
947
  free(large);
846
948
  return NULL;
847
949
  }
@@ -1010,6 +1112,9 @@ static void free_instance(napi_env env, void *data, void *hint) {
1010
1112
  if (inst->error_factory != NULL) {
1011
1113
  napi_delete_reference(env, inst->error_factory);
1012
1114
  }
1115
+ if (inst->warn != NULL) {
1116
+ napi_delete_reference(env, inst->warn);
1117
+ }
1013
1118
  free(inst);
1014
1119
  }
1015
1120