drizzle-sybase 4.0.0 → 5.0.0

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.
Files changed (88) hide show
  1. package/README.md +264 -7
  2. package/THIRD_PARTY_NOTICES.md +25 -0
  3. package/dist/bulk.d.ts +164 -0
  4. package/dist/bulk.d.ts.map +1 -0
  5. package/dist/bulk.js +1135 -0
  6. package/dist/bulk.js.map +1 -0
  7. package/dist/checks.d.ts +15 -0
  8. package/dist/checks.d.ts.map +1 -0
  9. package/dist/checks.js +19 -0
  10. package/dist/checks.js.map +1 -0
  11. package/dist/codecs.d.ts +1 -1
  12. package/dist/codecs.d.ts.map +1 -1
  13. package/dist/codecs.js +10 -2
  14. package/dist/codecs.js.map +1 -1
  15. package/dist/columns/index.d.ts +9 -1
  16. package/dist/columns/index.d.ts.map +1 -1
  17. package/dist/columns/index.js +5 -5
  18. package/dist/columns/index.js.map +1 -1
  19. package/dist/connection.d.ts +75 -12
  20. package/dist/connection.d.ts.map +1 -1
  21. package/dist/connection.js +216 -43
  22. package/dist/connection.js.map +1 -1
  23. package/dist/db.d.ts +51 -26
  24. package/dist/db.d.ts.map +1 -1
  25. package/dist/db.js +56 -2
  26. package/dist/db.js.map +1 -1
  27. package/dist/dialect.d.ts +4 -2
  28. package/dist/dialect.d.ts.map +1 -1
  29. package/dist/dialect.js +41 -15
  30. package/dist/dialect.js.map +1 -1
  31. package/dist/errors.d.ts +82 -1
  32. package/dist/errors.d.ts.map +1 -1
  33. package/dist/errors.js +90 -4
  34. package/dist/errors.js.map +1 -1
  35. package/dist/index.d.ts +5 -3
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +1 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/introspect/codegen.d.ts.map +1 -1
  40. package/dist/introspect/codegen.js +22 -12
  41. package/dist/introspect/codegen.js.map +1 -1
  42. package/dist/introspect/fetch.d.ts.map +1 -1
  43. package/dist/introspect/fetch.js +24 -8
  44. package/dist/introspect/fetch.js.map +1 -1
  45. package/dist/introspect/types.d.ts +5 -0
  46. package/dist/introspect/types.d.ts.map +1 -1
  47. package/dist/native/index.d.ts +97 -9
  48. package/dist/native/index.d.ts.map +1 -1
  49. package/dist/native/index.js +17 -0
  50. package/dist/native/index.js.map +1 -1
  51. package/dist/native/sybase_native.darwin-arm64.node +0 -0
  52. package/dist/native/sybase_native.darwin-x64.node +0 -0
  53. package/dist/native/sybase_native.linux-arm64.node +0 -0
  54. package/dist/native/sybase_native.linux-x64.node +0 -0
  55. package/dist/native/sybase_native.win32-x64.node +0 -0
  56. package/dist/pool.d.ts +24 -2
  57. package/dist/pool.d.ts.map +1 -1
  58. package/dist/pool.js +117 -37
  59. package/dist/pool.js.map +1 -1
  60. package/dist/query-builders/insert.d.ts.map +1 -1
  61. package/dist/query-builders/insert.js +5 -0
  62. package/dist/query-builders/insert.js.map +1 -1
  63. package/dist/query-builders/select.d.ts +5 -0
  64. package/dist/query-builders/select.d.ts.map +1 -1
  65. package/dist/query-builders/select.js +16 -0
  66. package/dist/query-builders/select.js.map +1 -1
  67. package/dist/result.d.ts +20 -7
  68. package/dist/result.d.ts.map +1 -1
  69. package/dist/result.js +35 -17
  70. package/dist/result.js.map +1 -1
  71. package/dist/session.d.ts +25 -2
  72. package/dist/session.d.ts.map +1 -1
  73. package/dist/session.js +134 -11
  74. package/dist/session.js.map +1 -1
  75. package/dist/table.d.ts.map +1 -1
  76. package/dist/table.js +5 -2
  77. package/dist/table.js.map +1 -1
  78. package/licenses/FreeTDS-COPYING_LIB.txt +481 -0
  79. package/package.json +8 -4
  80. package/patches/freetds-1.5.17-sybase-bulk.patch +592 -0
  81. package/scripts/build-native.ts +34 -0
  82. package/scripts/build-win.ps1 +49 -8
  83. package/scripts/build.sh +101 -30
  84. package/scripts/smoke-native.ts +67 -0
  85. package/scripts/smoke-package.sh +79 -0
  86. package/scripts/verify-published.ts +29 -5
  87. package/src/native/binding.c +3325 -322
  88. package/vendor/freetds-1.5.17.tar.gz +0 -0
@@ -11,24 +11,24 @@
11
11
  * - DBSETLPACKET is set on every login to avoid the 512-byte default,
12
12
  * which causes excessive round-trips on large result sets.
13
13
  * - Hard timeout watchdog: every async query spawns a uv_thread_t that, after
14
- * `hardTimeoutMs` (default 2 * timeout_seconds, min 30s) signals the JS
15
- * layer that the query has missed its deadline. The watchdog REJECTS the
16
- * pending promise immediately (via a threadsafe function) and marks the
17
- * connection dead so the pool discards it.
14
+ * `hardTimeoutMs` (default 2 * timeout_seconds, min 30s, counted from when
15
+ * the call was queued) marks the connection dead. The worker then stops: it
16
+ * checks between rows, and db-lib's interrupt handler ends a wait within
17
+ * about a second. The promise settles when the worker returns — it is the
18
+ * JS layer (SybaseConnection) that rejects on schedule whatever the worker
19
+ * does.
18
20
  *
19
21
  * The watchdog deliberately does NOT call dbcancel() from its thread:
20
22
  * dbcancel() invokes tds_process_cancel() which reads from the same
21
23
  * socket the worker is reading from, racing with the worker's own
22
- * dbnextrow(). We rely on db-lib's per-connection DBSETTIME to actually
23
- * unblock the worker, which then exits naturally; the worker's late
24
- * return is discarded since the promise was already rejected.
24
+ * dbnextrow().
25
25
  * - Deferred close: fn_close detects in-flight queries (via in_flight
26
26
  * counter) and instead of freeing immediately, sets pending_close.
27
27
  * conn_release in the worker performs the actual destruction, eliminating
28
28
  * the use-after-free that would otherwise happen when the JS layer
29
29
  * abandons a stuck connection.
30
- * - Concurrency self-protection: conn_acquire rejects re-entry while
31
- * another query is in flight on the same connection — db-lib's DBPROCESS
30
+ * - Concurrency self-protection: a call is refused while another is queued
31
+ * or running on the same handle (ConnHandle.busy) — db-lib's DBPROCESS
32
32
  * is not reentrant, and JS-side serialization can be bypassed.
33
33
  * - All db-lib calls run on libuv worker threads via napi_create_async_work
34
34
  * so the JS event loop is never blocked.
@@ -46,7 +46,9 @@
46
46
  #include <sybdb.h>
47
47
  #include <uv.h>
48
48
  #include <errno.h>
49
+ #include <float.h>
49
50
  #include <limits.h>
51
+ #include <math.h>
50
52
  #include <stdarg.h>
51
53
  #include <stdint.h>
52
54
  #include <string.h>
@@ -73,6 +75,14 @@
73
75
  // stock limit; a server configured lower (down to 512) needs packetSize set.
74
76
  #define DEFAULT_PACKET_SIZE 2048
75
77
 
78
+ /** Longest user name the TDS 5.0 login record holds, in bytes (FreeTDS TDS_MAXNAME). */
79
+ #define LOGIN_USER_MAX 30
80
+ /** Longest password db-lib takes, in bytes (FreeTDS TDS_MAX_LOGIN_STR_SZ). */
81
+ #define LOGIN_PASSWORD_MAX 128
82
+
83
+ /** Largest timeout, in seconds, whose derived hard timeout (2x, in ms) fits an int. */
84
+ #define MAX_TIMEOUT_SECONDS (INT32_MAX / 2000)
85
+
76
86
  // ---------------------------------------------------------------------------
77
87
  // Global mutex for serializing inherently process-wide db-lib calls.
78
88
  //
@@ -118,8 +128,30 @@ static void append_login_error(const char *fmt, ...) {
118
128
  va_end(args);
119
129
  }
120
130
 
131
+ static int err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr,
132
+ char *oserrstr);
133
+ static int msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext,
134
+ char *srvname, char *procname, int line);
135
+
136
+ /** Whether db-lib initialised; set once, by init_globals. */
137
+ static int dblib_ready = 0;
138
+
139
+ /**
140
+ * Once per process, whatever the number of Node environments (worker threads)
141
+ * loading the addon. dbinit() first resets db-lib's error handler to its
142
+ * default — which exits the process on a timeout or a dead connection — so it
143
+ * must not run again while another thread's calls may hit an error.
144
+ */
121
145
  static void init_globals(void) {
122
- uv_mutex_init(&login_mutex);
146
+ if (uv_mutex_init(&login_mutex) != 0) {
147
+ return; // not ready: every connect needs the mutex
148
+ }
149
+ if (dbinit() == FAIL) {
150
+ return;
151
+ }
152
+ dberrhandle(err_handler);
153
+ dbmsghandle(msg_handler);
154
+ dblib_ready = 1;
123
155
  }
124
156
 
125
157
  // ---------------------------------------------------------------------------
@@ -152,6 +184,7 @@ typedef struct {
152
184
  int has_error;
153
185
  int dead;
154
186
  int timed_out; // set when err_handler sees SYBETIME
187
+ int last_dberr; // number of the last db-lib (not server) error, 0 if none
155
188
  int timeout_seconds; // remembered so watchdog can compute its deadline
156
189
  uv_mutex_t lock;
157
190
  int in_flight; // # of queries pinning this connection, from enqueue to completion
@@ -162,10 +195,14 @@ typedef struct {
162
195
  int latin1;
163
196
  // The server's default character set name, as syscharsets spells it.
164
197
  char charset[32];
198
+ // Set between bulkBegin and the end of the copy: the DBPROCESS is then in the
199
+ // middle of a bulk copy and takes nothing else (see the bulk copy section).
200
+ struct BulkState *bulk;
165
201
  } SybaseConnection;
166
202
 
167
203
  // Forward declarations.
168
204
  static void destroy_connection(SybaseConnection *conn);
205
+ static void bulk_state_free(struct BulkState *state);
169
206
 
170
207
  /**
171
208
  * Mark a query as starting on the connection. Returns 0 on success, -1 if
@@ -214,6 +251,36 @@ static int conn_is_dead(SybaseConnection *conn) {
214
251
  return dead;
215
252
  }
216
253
 
254
+ /**
255
+ * Whether the caller has given up on the connection: its hard timeout fired
256
+ * (the watchdog marks it dead) or JS closed it.
257
+ */
258
+ static int conn_abandoned(SybaseConnection *conn) {
259
+ uv_mutex_lock(&conn->lock);
260
+ int abandoned = conn->dead || conn->pending_close;
261
+ uv_mutex_unlock(&conn->lock);
262
+ return abandoned;
263
+ }
264
+
265
+ /*
266
+ * db-lib's interrupt check. While a call waits on the socket, FreeTDS wakes
267
+ * once a second and calls it — on the worker thread itself, so unlike calling
268
+ * db-lib from the watchdog it races with nothing. Once the connection has been
269
+ * abandoned the wait is cut short: INT_CANCEL ends it as a timeout, which
270
+ * err_handler turns into closing the socket. Without this, a worker blocked in
271
+ * one wait (a lock, a long waitfor) held its thread and its server session
272
+ * until DBSETTIME, long after its hard timeout.
273
+ */
274
+ static int interrupt_check(void *dbproc) {
275
+ SybaseConnection *conn = (SybaseConnection *)dbgetuserdata((DBPROCESS *)dbproc);
276
+ return conn != NULL && conn_abandoned(conn);
277
+ }
278
+
279
+ static int interrupt_handle(void *dbproc) {
280
+ (void)dbproc;
281
+ return INT_CANCEL;
282
+ }
283
+
217
284
  /**
218
285
  * Actually free a connection. Must be called outside conn->lock; this
219
286
  * function destroys the lock itself.
@@ -227,15 +294,86 @@ static void destroy_connection(SybaseConnection *conn) {
227
294
  dbloginfree(conn->login);
228
295
  conn->login = NULL;
229
296
  }
297
+ // After dbclose: db-lib holds pointers into the bound buffers until then.
298
+ // Closing the connection in the middle of a copy is how a copy is aborted —
299
+ // the server rolls back the batch it never saw the end of.
300
+ bulk_state_free(conn->bulk);
301
+ conn->bulk = NULL;
230
302
  uv_mutex_destroy(&conn->lock);
231
303
  free(conn);
232
304
  }
233
305
 
306
+ /**
307
+ * Per Node environment (the main thread, each worker thread). Held by the
308
+ * environment and by each connection handle's finalizer, which may run as the
309
+ * environment is torn down — in an order N-API does not promise — so it is
310
+ * freed by whichever lets go of it last. Only the environment's own thread
311
+ * touches it.
312
+ */
313
+ typedef struct {
314
+ int closing; // the environment is being torn down
315
+ int refs;
316
+ } EnvState;
317
+
318
+ static void env_state_unref(EnvState *state) {
319
+ if (--state->refs == 0) {
320
+ free(state);
321
+ }
322
+ }
323
+
324
+ static void env_cleanup(void *arg) {
325
+ ((EnvState *)arg)->closing = 1;
326
+ }
327
+
328
+ static void env_state_free(napi_env env, void *data, void *hint) {
329
+ (void)env;
330
+ (void)hint;
331
+ env_state_unref((EnvState *)data);
332
+ }
333
+
334
+ /** The state of `env`, or NULL. Not for finalizers (see conn_handle_finalize). */
335
+ static EnvState *env_state(napi_env env) {
336
+ EnvState *state = NULL;
337
+ if (env == NULL || napi_get_instance_data(env, (void **)&state) != napi_ok) {
338
+ return NULL;
339
+ }
340
+ return state;
341
+ }
342
+
343
+ typedef struct {
344
+ napi_async_work work;
345
+ SybaseConnection *conn;
346
+ } DestroyWork;
347
+
348
+ static void destroy_execute(napi_env env, void *data) {
349
+ (void)env;
350
+ destroy_connection(((DestroyWork *)data)->conn);
351
+ ((DestroyWork *)data)->conn = NULL;
352
+ }
353
+
354
+ static void destroy_complete(napi_env env, napi_status status, void *data) {
355
+ (void)status;
356
+ DestroyWork *work = (DestroyWork *)data;
357
+ if (work->conn) {
358
+ destroy_connection(work->conn); // never ran (cancelled)
359
+ }
360
+ napi_delete_async_work(env, work->work);
361
+ free(work);
362
+ }
363
+
234
364
  /**
235
365
  * Close a connection now, or — if a query is still running on it — mark it
236
366
  * so the last worker to finish frees it (see conn_release).
367
+ *
368
+ * The close itself runs on the thread pool: dbclose() sends a logout and waits
369
+ * for its answer (up to 5 s on an unresponsive network), which must not hold
370
+ * up the JS thread. Nothing can reach the connection by then — pending_close
371
+ * refuses every later call. It is Node's own async work, which Node waits for
372
+ * before tearing an environment down (a worker thread that exits right after
373
+ * a close). Only as the environment is torn down (`env` NULL or closing, as
374
+ * for a handle finalized then) does it run here.
237
375
  */
238
- static void close_connection(SybaseConnection *conn) {
376
+ static void close_connection(SybaseConnection *conn, napi_env env, const EnvState *state) {
239
377
  uv_mutex_lock(&conn->lock);
240
378
  int can_destroy_now = (conn->in_flight == 0);
241
379
  int already_pending = conn->pending_close;
@@ -243,10 +381,25 @@ static void close_connection(SybaseConnection *conn) {
243
381
  conn->dead = 1; // reject any subsequent acquire
244
382
  uv_mutex_unlock(&conn->lock);
245
383
 
246
- if (can_destroy_now && !already_pending) {
247
- destroy_connection(conn);
384
+ if (!can_destroy_now || already_pending) {
385
+ return; // the worker holding in_flight frees it in conn_release
386
+ }
387
+ int open = env != NULL && state != NULL && !state->closing;
388
+ DestroyWork *work = open ? (DestroyWork *)calloc(1, sizeof(DestroyWork)) : NULL;
389
+ if (work) {
390
+ napi_value name;
391
+ work->conn = conn;
392
+ if (napi_create_string_utf8(env, "sybase_close", NAPI_AUTO_LENGTH, &name) == napi_ok &&
393
+ napi_create_async_work(env, NULL, name, destroy_execute, destroy_complete, work,
394
+ &work->work) == napi_ok) {
395
+ if (napi_queue_async_work(env, work->work) == napi_ok) {
396
+ return;
397
+ }
398
+ napi_delete_async_work(env, work->work);
399
+ }
400
+ free(work);
248
401
  }
249
- // else: the worker holding in_flight frees it in conn_release.
402
+ destroy_connection(conn);
250
403
  }
251
404
 
252
405
  // ---------------------------------------------------------------------------
@@ -261,22 +414,109 @@ static void close_connection(SybaseConnection *conn) {
261
414
 
262
415
  typedef struct {
263
416
  SybaseConnection *conn; // NULL once closed
417
+ // 1 while a call (query, bulkBegin, bulkSend) is queued or running. A
418
+ // DBPROCESS takes one call at a time; the JS layer queues them, and this
419
+ // refuses a second one from anything that does not (the exported `native`).
420
+ int busy;
264
421
  } ConnHandle;
265
422
 
423
+ /**
424
+ * A call's hold on its handle: marks it busy, and references it so that the
425
+ * handle — where `busy` is cleared — outlives the call even if JS drops it.
426
+ */
427
+ typedef struct {
428
+ ConnHandle *handle;
429
+ napi_ref ref;
430
+ } HandleClaim;
431
+
432
+ /**
433
+ * Reject `deferred` when the error meant for it could not be built: with the
434
+ * pending JS exception if there is one (typically out of memory), else a plain
435
+ * Error — or, if even that cannot be made, `undefined`. Never with a value
436
+ * that was not created.
437
+ */
438
+ static void reject_fallback(napi_env env, napi_deferred deferred, const char *message) {
439
+ napi_value err, msg;
440
+ bool pending = false;
441
+ if (napi_is_exception_pending(env, &pending) == napi_ok && pending &&
442
+ napi_get_and_clear_last_exception(env, &err) == napi_ok) {
443
+ napi_reject_deferred(env, deferred, err);
444
+ return;
445
+ }
446
+ if (napi_create_string_utf8(env, message, NAPI_AUTO_LENGTH, &msg) != napi_ok ||
447
+ napi_create_error(env, NULL, msg, &err) != napi_ok) {
448
+ napi_get_undefined(env, &err);
449
+ }
450
+ napi_reject_deferred(env, deferred, err);
451
+ }
452
+
453
+ /** A promise already rejected because the handle is busy. */
454
+ static napi_value busy_rejection(napi_env env) {
455
+ static const char message[] =
456
+ "Another call is still running on this connection: a connection runs one call at a time";
457
+ napi_deferred deferred;
458
+ napi_value promise, msg, err;
459
+ if (napi_create_promise(env, &deferred, &promise) != napi_ok) {
460
+ napi_throw_error(env, NULL, message);
461
+ return NULL;
462
+ }
463
+ if (napi_create_string_utf8(env, message, NAPI_AUTO_LENGTH, &msg) != napi_ok ||
464
+ napi_create_error(env, NULL, msg, &err) != napi_ok) {
465
+ napi_get_undefined(env, &err);
466
+ }
467
+ napi_reject_deferred(env, deferred, err);
468
+ return promise;
469
+ }
470
+
471
+ /** Whether a call may start on `handle` now; a closed handle may (the call then fails as closed). */
472
+ static int handle_available(const ConnHandle *handle) {
473
+ return handle->conn == NULL || !handle->busy;
474
+ }
475
+
476
+ /**
477
+ * Claim `handle` for a call; -1 if it cannot be referenced — the call must then
478
+ * not start, since nothing would keep the handle alive until it settles.
479
+ */
480
+ static int claim_handle(napi_env env, napi_value value, ConnHandle *handle, HandleClaim *claim) {
481
+ if (napi_create_reference(env, value, 1, &claim->ref) != napi_ok) {
482
+ claim->ref = NULL;
483
+ return -1;
484
+ }
485
+ handle->busy = 1;
486
+ claim->handle = handle;
487
+ return 0;
488
+ }
489
+
490
+ /** Undo claim_handle. On the JS thread, when the call has settled. */
491
+ static void release_handle(napi_env env, HandleClaim *claim) {
492
+ if (claim->handle) {
493
+ claim->handle->busy = 0;
494
+ claim->handle = NULL;
495
+ }
496
+ if (claim->ref) {
497
+ napi_delete_reference(env, claim->ref);
498
+ claim->ref = NULL;
499
+ }
500
+ }
501
+
266
502
  // Identifies externals created by connect(), so an arbitrary value passed as a
267
503
  // handle is rejected rather than dereferenced.
268
504
  static const napi_type_tag CONN_HANDLE_TAG = {0x8f1c2a7d3b4e5f60ULL, 0x91a2b3c4d5e6f708ULL};
269
505
 
270
- /** GC of a handle that was never closed: close the connection it still owns. */
506
+ /**
507
+ * GC of a handle that was never closed: close the connection it still owns —
508
+ * on the thread pool, as close() does, unless the environment is being torn
509
+ * down.
510
+ */
271
511
  static void conn_handle_finalize(napi_env env, void *data, void *hint) {
272
- (void)env;
273
- (void)hint;
512
+ EnvState *state = (EnvState *)hint; // this handle's reference (see connect_complete)
274
513
  ConnHandle *handle = (ConnHandle *)data;
275
514
  if (handle->conn) {
276
- close_connection(handle->conn);
515
+ close_connection(handle->conn, env, state);
277
516
  handle->conn = NULL;
278
517
  }
279
518
  free(handle);
519
+ env_state_unref(state);
280
520
  }
281
521
 
282
522
  /**
@@ -354,6 +594,7 @@ static int err_handler(DBPROCESS *dbproc, int severity, int dberr,
354
594
  "DB-Lib error %d (severity %d): %s\n", dberr, severity, dberrstr);
355
595
  }
356
596
  conn->has_error = 1;
597
+ conn->last_dberr = dberr;
357
598
 
358
599
  if (dberr == SYBETIME) {
359
600
  conn->timed_out = 1;
@@ -366,8 +607,11 @@ static int err_handler(DBPROCESS *dbproc, int severity, int dberr,
366
607
 
367
608
  // For the per-connection DBSETTIME firing, return INT_TIMEOUT so db-lib
368
609
  // sends a cancel and does not loop. For all other fatal errors, INT_CANCEL.
610
+ // A wait on an abandoned connection (see conn_abandoned) also ends as a
611
+ // timeout: INT_CANCEL then closes the socket, so the worker returns at once
612
+ // instead of waiting for the server to answer a cancel.
369
613
  if (dberr == SYBETIME) {
370
- return INT_TIMEOUT;
614
+ return conn && conn_abandoned(conn) ? INT_CANCEL : INT_TIMEOUT;
371
615
  }
372
616
  return INT_CANCEL;
373
617
  }
@@ -419,6 +663,35 @@ static void clear_error(SybaseConnection *conn) {
419
663
  conn->last_error[0] = '\0';
420
664
  conn->has_error = 0;
421
665
  conn->timed_out = 0;
666
+ conn->last_dberr = 0;
667
+ }
668
+
669
+ /**
670
+ * Set the cooperative timeout of this connection alone.
671
+ *
672
+ * dbsetopt(DBSETTIME) sets the timeout of this DBPROCESS's own socket — unlike
673
+ * the process-wide dbsettime(), which would reach every connection. A DBPROCESS
674
+ * runs one call at a time, so a per-call value set before the call and restored
675
+ * after it (see restore_query_timeout) is seen by that call only.
676
+ */
677
+ static int set_query_timeout(SybaseConnection *conn, int seconds) {
678
+ char value[16];
679
+ snprintf(value, sizeof(value), "%d", seconds);
680
+ // db-lib may fail (out of memory) without a word: the timeout is then not set.
681
+ return dbsetopt(conn->dbproc, DBSETTIME, value, 0) == FAIL ? -1 : 0;
682
+ }
683
+
684
+ /**
685
+ * Put back the connection's own timeout after a call that overrode it. Skipped
686
+ * on a dead connection, which db-lib would report as one more error. -1 if it
687
+ * could not be: the connection keeps another call's timeout, and must not be
688
+ * used again (the caller marks it so).
689
+ */
690
+ static int restore_query_timeout(SybaseConnection *conn, int overridden) {
691
+ if (overridden && !DBDEAD(conn->dbproc)) {
692
+ return set_query_timeout(conn, conn->timeout_seconds);
693
+ }
694
+ return 0;
422
695
  }
423
696
 
424
697
  /**
@@ -820,21 +1093,29 @@ static void connect_execute(napi_env env, void *data) {
820
1093
  return;
821
1094
  }
822
1095
 
823
- DBSETLUSER(w->conn->login, w->username);
824
- DBSETLPWD(w->conn->login, w->password);
825
- DBSETLAPP(w->conn->login, "drizzle-sybase");
826
1096
  // unichar/univarchar arrive as UTF-16 and FreeTDS converts them to this. Set
827
1097
  // explicitly, or FreeTDS takes it from the process locale — which is "C" in
828
1098
  // Lambda, where a non-ASCII unichar value then comes back as '?'.
829
- DBSETLCHARSET(w->conn->login, "UTF-8");
830
-
831
- // Set TDS version to 5.0 (Sybase ASE native)
832
- DBSETLVERSION(w->conn->login, DBVERSION_100);
833
-
834
- // Negotiate a larger TDS packet so big result sets aren't shipped
835
- // 512 bytes at a time. The server may cap it, that's fine.
836
- if (w->packet_size > 0) {
837
- DBSETLPACKET(w->conn->login, w->packet_size);
1099
+ // TDS 5.0 (Sybase ASE native). A larger TDS packet, so big result sets are
1100
+ // not shipped 512 bytes at a time (the server may cap it).
1101
+ // Each setting must take: a login sent without one (a setter may fail, out of
1102
+ // memory, without a word) would be another login than the one asked for.
1103
+ const char *unset = DBSETLUSER(w->conn->login, w->username) == FAIL ? "user name"
1104
+ : DBSETLPWD(w->conn->login, w->password) == FAIL ? "password"
1105
+ : DBSETLAPP(w->conn->login, "drizzle-sybase") == FAIL ? "application name"
1106
+ : DBSETLCHARSET(w->conn->login, "UTF-8") == FAIL ? "character set"
1107
+ : DBSETLVERSION(w->conn->login, DBVERSION_100) == FAIL ? "TDS version"
1108
+ : w->packet_size > 0 && DBSETLPACKET(w->conn->login, w->packet_size) == FAIL
1109
+ ? "packet size"
1110
+ : NULL;
1111
+ if (unset) {
1112
+ snprintf(w->error, sizeof(w->error), "Could not set the login's %s", unset);
1113
+ w->success = 0;
1114
+ dbloginfree(w->conn->login);
1115
+ uv_mutex_destroy(&w->conn->lock);
1116
+ free(w->conn);
1117
+ w->conn = NULL;
1118
+ return;
838
1119
  }
839
1120
 
840
1121
  // Build host:port string for dbopen.
@@ -872,13 +1153,19 @@ static void connect_execute(napi_env env, void *data) {
872
1153
  // Associate our connection struct with the DBPROCESS so the global
873
1154
  // err/msg handlers can find it.
874
1155
  dbsetuserdata(w->conn->dbproc, (BYTE *)w->conn);
1156
+ dbsetinterrupt(w->conn->dbproc, interrupt_check, interrupt_handle);
875
1157
 
876
1158
  // Per-connection query timeout. Replaces the legacy process-global
877
- // dbsettime(). dbsetopt takes the value as a decimal string.
878
- if (w->timeout_seconds > 0) {
879
- char tval[16];
880
- snprintf(tval, sizeof(tval), "%d", w->timeout_seconds);
881
- dbsetopt(w->conn->dbproc, DBSETTIME, tval, 0);
1159
+ // dbsettime().
1160
+ if (w->timeout_seconds > 0 && set_query_timeout(w->conn, w->timeout_seconds) != 0) {
1161
+ snprintf(w->error, sizeof(w->error), "Could not set the connection's query timeout");
1162
+ dbclose(w->conn->dbproc);
1163
+ dbloginfree(w->conn->login);
1164
+ uv_mutex_destroy(&w->conn->lock);
1165
+ free(w->conn);
1166
+ w->conn = NULL;
1167
+ w->success = 0;
1168
+ return;
882
1169
  }
883
1170
 
884
1171
  // Switch database
@@ -919,28 +1206,31 @@ static void connect_complete(napi_env env, napi_status status, void *data) {
919
1206
  if (status == napi_cancelled || !w->success) {
920
1207
  // Cancelled after a successful connect: nothing will ever own it.
921
1208
  if (w->success && w->conn) {
922
- close_connection(w->conn);
1209
+ close_connection(w->conn, env, env_state(env));
923
1210
  }
924
- napi_value error_msg, error_obj;
925
- napi_create_string_utf8(env, w->error, NAPI_AUTO_LENGTH, &error_msg);
926
- napi_create_error(env, NULL, error_msg, &error_obj);
927
- napi_reject_deferred(env, w->deferred, error_obj);
1211
+ reject_fallback(env, w->deferred, w->error);
928
1212
  } else {
929
1213
  napi_value result;
930
1214
  ConnHandle *handle = (ConnHandle *)malloc(sizeof(ConnHandle));
931
1215
  if (handle == NULL ||
932
- napi_create_external(env, handle, conn_handle_finalize, NULL, &result) != napi_ok) {
933
- // No handle for JS to close it through: close it here.
1216
+ napi_create_external(env, handle, NULL, NULL, &result) != napi_ok ||
1217
+ // Untagged, the handle would be refused by every call.
1218
+ napi_type_tag_object(env, result, &CONN_HANDLE_TAG) != napi_ok) {
1219
+ // No handle for JS to close it through: close it here. (The external, if
1220
+ // made, has no finalizer and does not own the box.)
934
1221
  free(handle);
935
- close_connection(w->conn);
936
- napi_value error_msg, error_obj;
937
- napi_create_string_utf8(env, "Could not create a connection handle", NAPI_AUTO_LENGTH,
938
- &error_msg);
939
- napi_create_error(env, NULL, error_msg, &error_obj);
940
- napi_reject_deferred(env, w->deferred, error_obj);
1222
+ close_connection(w->conn, env, env_state(env));
1223
+ reject_fallback(env, w->deferred, "Could not create a connection handle");
1224
+ } else if (env_state(env) == NULL ||
1225
+ napi_add_finalizer(env, result, handle, conn_handle_finalize, env_state(env), NULL) !=
1226
+ napi_ok) {
1227
+ free(handle);
1228
+ close_connection(w->conn, env, env_state(env));
1229
+ reject_fallback(env, w->deferred, "Could not create a connection handle");
941
1230
  } else {
1231
+ env_state(env)->refs++; // released by conn_handle_finalize
942
1232
  handle->conn = w->conn;
943
- napi_type_tag_object(env, result, &CONN_HANDLE_TAG);
1233
+ handle->busy = 0;
944
1234
  napi_resolve_deferred(env, w->deferred, result);
945
1235
  }
946
1236
  }
@@ -949,62 +1239,187 @@ static void connect_complete(napi_env env, napi_status status, void *data) {
949
1239
  free(w);
950
1240
  }
951
1241
 
952
- // napi_value connect(config: { host, port, database, username, password, timeout?, packetSize? })
953
- static napi_value fn_connect(napi_env env, napi_callback_info info) {
954
- size_t argc = 1;
955
- napi_value args[1];
956
- napi_get_cb_info(env, info, &argc, args, NULL, NULL);
1242
+ /**
1243
+ * After a failed N-API call: NULL, with an exception pending — the one the call
1244
+ * left (a throwing getter's), or else a new Error saying `what` failed.
1245
+ */
1246
+ static napi_value fail_call(napi_env env, const char *what) {
1247
+ bool pending = false;
1248
+ if (napi_is_exception_pending(env, &pending) != napi_ok || !pending) {
1249
+ napi_throw_error(env, NULL, what);
1250
+ }
1251
+ return NULL;
1252
+ }
957
1253
 
958
- ConnectWork *w = (ConnectWork *)calloc(1, sizeof(ConnectWork));
1254
+ /**
1255
+ * A string option of the connection config into `out`; -1 (with a TypeError
1256
+ * pending) if it is missing, not a string, holds a NUL, or does not fit — any
1257
+ * of which would otherwise reach db-lib as a different value.
1258
+ */
1259
+ static int config_string(napi_env env, napi_value obj, const char *name, char *out, size_t size) {
1260
+ napi_value v;
1261
+ napi_valuetype type = napi_undefined;
1262
+ size_t len = 0;
1263
+ if (napi_get_named_property(env, obj, name, &v) != napi_ok) {
1264
+ return -1; // a throwing getter: its exception is pending
1265
+ }
1266
+ if (napi_typeof(env, v, &type) != napi_ok || type != napi_string ||
1267
+ napi_get_value_string_utf8(env, v, NULL, 0, &len) != napi_ok) {
1268
+ char message[96];
1269
+ snprintf(message, sizeof(message), "Expected `%s` to be a string", name);
1270
+ napi_throw_type_error(env, NULL, message);
1271
+ return -1;
1272
+ }
1273
+ if (len >= size) {
1274
+ char message[96];
1275
+ snprintf(message, sizeof(message), "`%s` is longer than %zu bytes", name, size - 1);
1276
+ napi_throw_type_error(env, NULL, message);
1277
+ return -1;
1278
+ }
1279
+ if (napi_get_value_string_utf8(env, v, out, size, &len) != napi_ok) {
1280
+ fail_call(env, "Could not read the connection config");
1281
+ return -1;
1282
+ }
1283
+ if (strlen(out) != len) {
1284
+ char message[96];
1285
+ snprintf(message, sizeof(message), "`%s` holds a NUL character", name);
1286
+ napi_throw_type_error(env, NULL, message);
1287
+ return -1;
1288
+ }
1289
+ return 0;
1290
+ }
1291
+
1292
+ /** Largest integer a JS number holds exactly (2^53 - 1). */
1293
+ #define MAX_SAFE_INTEGER 9007199254740991LL
959
1294
 
960
- napi_value val;
961
- size_t len;
962
- napi_valuetype vtype;
1295
+ /**
1296
+ * An integer option, in [min, max]; left as it is when absent and not
1297
+ * `required`. -1 (with an exception pending) otherwise — a throwing getter's
1298
+ * own, or a RangeError: a value is never truncated or wrapped into range.
1299
+ */
1300
+ static int option_int64(napi_env env, napi_value obj, const char *name, int64_t min, int64_t max,
1301
+ int required, int64_t *out) {
1302
+ napi_value v;
1303
+ napi_valuetype type = napi_undefined;
1304
+ double d = 0;
1305
+ if (napi_get_named_property(env, obj, name, &v) != napi_ok ||
1306
+ napi_typeof(env, v, &type) != napi_ok) {
1307
+ return -1;
1308
+ }
1309
+ if (type == napi_undefined && !required) {
1310
+ return 0;
1311
+ }
1312
+ // NaN fails the range test (every comparison with it is false).
1313
+ if (type != napi_number || napi_get_value_double(env, v, &d) != napi_ok ||
1314
+ !(d >= (double)min && d <= (double)max) || d != floor(d)) {
1315
+ char message[160];
1316
+ snprintf(message, sizeof(message), "Expected `%s` to be an integer in [%lld, %lld]", name,
1317
+ (long long)min, (long long)max);
1318
+ napi_throw_range_error(env, NULL, message);
1319
+ return -1;
1320
+ }
1321
+ *out = (int64_t)d;
1322
+ return 0;
1323
+ }
963
1324
 
964
- napi_get_named_property(env, args[0], "host", &val);
965
- napi_get_value_string_utf8(env, val, w->host, sizeof(w->host), &len);
1325
+ /** option_int64 for an int. */
1326
+ static int config_int(napi_env env, napi_value obj, const char *name, int32_t min, int32_t max,
1327
+ int required, int *out) {
1328
+ int64_t value = *out;
1329
+ if (option_int64(env, obj, name, min, max, required, &value) != 0) {
1330
+ return -1;
1331
+ }
1332
+ *out = (int)value;
1333
+ return 0;
1334
+ }
966
1335
 
967
- napi_get_named_property(env, args[0], "port", &val);
968
- napi_get_value_int32(env, val, &w->port);
969
1336
 
970
- napi_get_named_property(env, args[0], "database", &val);
971
- napi_get_value_string_utf8(env, val, w->database, sizeof(w->database), &len);
1337
+ /** A boolean option; left as it is when absent. -1 (exception pending) otherwise. */
1338
+ static int option_bool(napi_env env, napi_value obj, const char *name, int *out) {
1339
+ napi_value v;
1340
+ napi_valuetype type = napi_undefined;
1341
+ bool flag = false;
1342
+ if (napi_get_named_property(env, obj, name, &v) != napi_ok ||
1343
+ napi_typeof(env, v, &type) != napi_ok) {
1344
+ return -1;
1345
+ }
1346
+ if (type == napi_undefined) {
1347
+ return 0;
1348
+ }
1349
+ if (type != napi_boolean || napi_get_value_bool(env, v, &flag) != napi_ok) {
1350
+ char message[96];
1351
+ snprintf(message, sizeof(message), "Expected `%s` to be a boolean", name);
1352
+ napi_throw_type_error(env, NULL, message);
1353
+ return -1;
1354
+ }
1355
+ *out = flag ? 1 : 0;
1356
+ return 0;
1357
+ }
972
1358
 
973
- napi_get_named_property(env, args[0], "username", &val);
974
- napi_get_value_string_utf8(env, val, w->username, sizeof(w->username), &len);
1359
+ // napi_value connect(config: { host, port, database, username, password, timeout?, packetSize? })
1360
+ static napi_value fn_connect(napi_env env, napi_callback_info info) {
1361
+ size_t argc = 1;
1362
+ napi_value args[1];
1363
+ if (napi_get_cb_info(env, info, &argc, args, NULL, NULL) != napi_ok) {
1364
+ return NULL;
1365
+ }
975
1366
 
976
- napi_get_named_property(env, args[0], "password", &val);
977
- napi_get_value_string_utf8(env, val, w->password, sizeof(w->password), &len);
1367
+ ConnectWork *w = (ConnectWork *)calloc(1, sizeof(ConnectWork));
1368
+ if (w == NULL) {
1369
+ napi_throw_error(env, NULL, "Out of memory");
1370
+ return NULL;
1371
+ }
978
1372
 
1373
+ napi_valuetype vtype = napi_undefined;
1374
+ if (argc < 1 || napi_typeof(env, args[0], &vtype) != napi_ok || vtype != napi_object) {
1375
+ free(w);
1376
+ napi_throw_type_error(env, NULL, "Expected the connection config as an object");
1377
+ return NULL;
1378
+ }
979
1379
  // Optional timeout (seconds), default 30s. Values <= 0 are coerced to 1
980
1380
  // because dbsetlogintime() ignores zero/negative input but its prior
981
1381
  // process-global value would silently leak through.
982
1382
  w->timeout_seconds = 30;
983
- napi_get_named_property(env, args[0], "timeout", &val);
984
- napi_typeof(env, val, &vtype);
985
- if (vtype == napi_number) {
986
- napi_get_value_int32(env, val, &w->timeout_seconds);
1383
+ w->packet_size = DEFAULT_PACKET_SIZE;
1384
+ if (config_string(env, args[0], "host", w->host, sizeof(w->host)) != 0 ||
1385
+ config_string(env, args[0], "database", w->database, sizeof(w->database)) != 0 ||
1386
+ // The TDS 5.0 login record holds a user name of at most 30 bytes, and
1387
+ // FreeTDS cuts a longer one short — another account's name, perhaps — so
1388
+ // it is refused; db-lib takes a password of at most 128.
1389
+ config_string(env, args[0], "username", w->username, LOGIN_USER_MAX + 1) != 0 ||
1390
+ config_string(env, args[0], "password", w->password, LOGIN_PASSWORD_MAX + 1) != 0 ||
1391
+ config_int(env, args[0], "port", 1, 65535, 1, &w->port) != 0 ||
1392
+ config_int(env, args[0], "timeout", INT32_MIN, MAX_TIMEOUT_SECONDS, 0, &w->timeout_seconds) != 0 ||
1393
+ config_int(env, args[0], "packetSize", 512, 65535, 0, &w->packet_size) != 0) {
1394
+ free(w);
1395
+ return NULL; // exception pending
987
1396
  }
988
1397
  if (w->timeout_seconds <= 0) {
989
1398
  w->timeout_seconds = 1;
990
1399
  }
991
1400
 
992
- // Optional TDS packet size override.
993
- w->packet_size = DEFAULT_PACKET_SIZE;
994
- napi_get_named_property(env, args[0], "packetSize", &val);
995
- napi_typeof(env, val, &vtype);
996
- if (vtype == napi_number) {
997
- napi_get_value_int32(env, val, &w->packet_size);
1401
+ napi_value promise, work_name;
1402
+ if (napi_create_promise(env, &w->deferred, &promise) != napi_ok) {
1403
+ free(w);
1404
+ napi_throw_error(env, NULL, "Could not create the connection's promise");
1405
+ return NULL;
1406
+ }
1407
+ napi_status st = napi_create_string_utf8(env, "sybase_connect", NAPI_AUTO_LENGTH, &work_name);
1408
+ if (st == napi_ok) {
1409
+ st = napi_create_async_work(env, NULL, work_name, connect_execute, connect_complete, w,
1410
+ &w->work);
1411
+ }
1412
+ if (st == napi_ok) {
1413
+ st = napi_queue_async_work(env, w->work);
1414
+ if (st != napi_ok) {
1415
+ napi_delete_async_work(env, w->work);
1416
+ }
1417
+ }
1418
+ if (st != napi_ok) {
1419
+ // connect_complete will never run: nothing was opened, so only settle and free.
1420
+ reject_fallback(env, w->deferred, "Failed to schedule the connection");
1421
+ free(w);
998
1422
  }
999
-
1000
- napi_value promise;
1001
- napi_create_promise(env, &w->deferred, &promise);
1002
-
1003
- napi_value work_name;
1004
- napi_create_string_utf8(env, "sybase_connect", NAPI_AUTO_LENGTH, &work_name);
1005
- napi_create_async_work(env, NULL, work_name, connect_execute, connect_complete, w, &w->work);
1006
- napi_queue_async_work(env, w->work);
1007
-
1008
1423
  return promise;
1009
1424
  }
1010
1425
 
@@ -1041,9 +1456,18 @@ typedef struct Watchdog {
1041
1456
  int finished; // set by worker before signaling cond
1042
1457
  int fired; // set by watchdog when deadline elapsed
1043
1458
  int deadline_ms;
1459
+ uint64_t queued_ns; // uv_hrtime() when the call was queued: the deadline counts from there
1460
+ uint64_t end_ns; // the deadline, on uv_hrtime()'s monotonic clock
1044
1461
  SybaseConnection *conn;
1045
1462
  } Watchdog;
1046
1463
 
1464
+ /** A finished result set, kept for allResultSets. */
1465
+ typedef struct {
1466
+ ResultColumn *columns;
1467
+ int num_columns;
1468
+ int num_rows;
1469
+ } ResultSet;
1470
+
1047
1471
  typedef struct {
1048
1472
  napi_async_work work;
1049
1473
  napi_deferred deferred;
@@ -1055,6 +1479,9 @@ typedef struct {
1055
1479
  int max_rows_explicit;
1056
1480
  int truncated;
1057
1481
  int hard_timeout_ms;
1482
+ // Cooperative timeout for this query alone, in seconds; 0 keeps the
1483
+ // connection's own (see set_query_timeout).
1484
+ int timeout_seconds;
1058
1485
  // 1 to hand datetime/smalldatetime over as wall-clock numbers (KIND_WALLCLOCK)
1059
1486
  // for the JS layer to turn into Dates, instead of as canonical text.
1060
1487
  int wall_clock_datetimes;
@@ -1072,6 +1499,12 @@ typedef struct {
1072
1499
  ResultColumn *columns;
1073
1500
  int num_columns;
1074
1501
  int num_rows;
1502
+ // 1 to keep every result set with columns (allResultSets), not just the
1503
+ // first. Each finished one is moved to `sets`, including — once the batch
1504
+ // has ended — the last; `columns` is then empty.
1505
+ int all_result_sets;
1506
+ ResultSet *sets;
1507
+ int num_sets;
1075
1508
  int affected_rows;
1076
1509
  char error[ERR_BUF_SIZE];
1077
1510
  int success;
@@ -1085,6 +1518,7 @@ typedef struct {
1085
1518
  // while the work waits in the libuv queue defers the free instead of
1086
1519
  // leaving the worker with a dangling pointer.
1087
1520
  int holds_conn;
1521
+ HandleClaim claim;
1088
1522
  // Watchdog
1089
1523
  Watchdog wd;
1090
1524
  } QueryWork;
@@ -1093,23 +1527,40 @@ static void watchdog_thread(void *arg) {
1093
1527
  Watchdog *wd = (Watchdog *)arg;
1094
1528
 
1095
1529
  uv_mutex_lock(&wd->mutex);
1096
- if (!wd->finished) {
1097
- uint64_t timeout_ns = (uint64_t)wd->deadline_ms * 1000000ULL;
1098
- int rc = uv_cond_timedwait(&wd->cond, &wd->mutex, timeout_ns);
1099
- if (rc == UV_ETIMEDOUT && !wd->finished) {
1530
+ // Towards a fixed deadline: a wakeup before it (spurious, or anything else
1531
+ // but `finished`) waits again for the rest, never for a fresh full timeout.
1532
+ while (!wd->finished) {
1533
+ uint64_t now = uv_hrtime();
1534
+ if (now >= wd->end_ns) {
1100
1535
  wd->fired = 1;
1101
1536
  if (wd->conn) {
1102
1537
  conn_mark_dead(wd->conn);
1103
1538
  }
1539
+ break;
1104
1540
  }
1541
+ uv_cond_timedwait(&wd->cond, &wd->mutex, wd->end_ns - now);
1105
1542
  }
1106
1543
  uv_mutex_unlock(&wd->mutex);
1107
1544
  }
1108
1545
 
1546
+ /**
1547
+ * Start the watchdog of a call whose hard timeout is `deadline_ms` from when it
1548
+ * was queued (`queued_ns`; now, if unset): time spent waiting for a worker
1549
+ * counts. Returns 0 once started (or if disabled), -1 if it could not be, and
1550
+ * 1 if the deadline had already passed — then nothing is to be sent, and the
1551
+ * call ends as a hard timeout (`fired` is set; the connection marked dead).
1552
+ */
1109
1553
  static int watchdog_start(Watchdog *wd, SybaseConnection *conn, int deadline_ms) {
1110
1554
  if (deadline_ms <= 0) {
1111
1555
  return 0; // disabled
1112
1556
  }
1557
+ uint64_t from = wd->queued_ns ? wd->queued_ns : uv_hrtime();
1558
+ wd->end_ns = from + (uint64_t)deadline_ms * 1000000ULL;
1559
+ if (uv_hrtime() >= wd->end_ns) {
1560
+ wd->fired = 1;
1561
+ conn_mark_dead(conn);
1562
+ return 1;
1563
+ }
1113
1564
  if (uv_mutex_init(&wd->mutex) != 0) {
1114
1565
  return -1;
1115
1566
  }
@@ -1131,11 +1582,47 @@ static int watchdog_start(Watchdog *wd, SybaseConnection *conn, int deadline_ms)
1131
1582
  return 0;
1132
1583
  }
1133
1584
 
1585
+ /**
1586
+ * Whether the watchdog has fired. Read by the worker between rows, so that a
1587
+ * query whose rows keep arriving — which the cooperative timeout, a limit on
1588
+ * each wait for the server, never interrupts — still ends at its hard timeout
1589
+ * instead of reading on after the caller has been told it failed.
1590
+ */
1591
+ static int watchdog_fired(Watchdog *wd) {
1592
+ if (!wd->started) {
1593
+ return 0;
1594
+ }
1595
+ uv_mutex_lock(&wd->mutex);
1596
+ int fired = wd->fired;
1597
+ uv_mutex_unlock(&wd->mutex);
1598
+ return fired;
1599
+ }
1600
+
1601
+ /** Rows read between checks of the watchdog: often enough, at a negligible cost. */
1602
+ #define WATCHDOG_CHECK_ROWS 256
1603
+
1604
+ /**
1605
+ * Whether a call should stop between rows: its hard timeout fired, or the
1606
+ * caller closed the connection. Rows that keep arriving never let db-lib's
1607
+ * interrupt check run (it runs in waits), so a close must be seen here too.
1608
+ */
1609
+ static int call_abandoned(Watchdog *wd, SybaseConnection *conn) {
1610
+ return watchdog_fired(wd) || conn_abandoned(conn);
1611
+ }
1612
+
1134
1613
  static void watchdog_stop(Watchdog *wd) {
1135
1614
  if (!wd->started) {
1136
1615
  return;
1137
1616
  }
1138
1617
  uv_mutex_lock(&wd->mutex);
1618
+ // Past the deadline is a timeout whether or not the watchdog thread got to
1619
+ // run by then: the call must not end as if it had kept to it.
1620
+ if (!wd->fired && uv_hrtime() >= wd->end_ns) {
1621
+ wd->fired = 1;
1622
+ if (wd->conn) {
1623
+ conn_mark_dead(wd->conn);
1624
+ }
1625
+ }
1139
1626
  wd->finished = 1;
1140
1627
  uv_cond_signal(&wd->cond);
1141
1628
  uv_mutex_unlock(&wd->mutex);
@@ -1156,12 +1643,48 @@ static void work_release_conn(QueryWork *w) {
1156
1643
  }
1157
1644
  }
1158
1645
 
1646
+ /**
1647
+ * discard_rows, stopping when the hard timeout has fired: FAIL then too. The
1648
+ * connection is dead by then, so nothing is cancelled; it is closed.
1649
+ */
1650
+ static RETCODE query_discard_rows(QueryWork *w) {
1651
+ RETCODE rc;
1652
+ long n = 0;
1653
+ while ((rc = dbnextrow(w->conn->dbproc)) != NO_MORE_ROWS) {
1654
+ if (rc == FAIL || (++n % WATCHDOG_CHECK_ROWS == 0 && call_abandoned(&w->wd, w->conn))) {
1655
+ return FAIL;
1656
+ }
1657
+ }
1658
+ return SUCCEED;
1659
+ }
1660
+
1159
1661
  /** Drop whatever result the work item has collected. */
1160
1662
  static void discard_results(QueryWork *w) {
1161
1663
  free_columns(w->columns, w->num_columns);
1162
1664
  w->columns = NULL;
1163
1665
  w->num_columns = 0;
1164
1666
  w->num_rows = 0;
1667
+ for (int i = 0; i < w->num_sets; i++) {
1668
+ free_columns(w->sets[i].columns, w->sets[i].num_columns);
1669
+ }
1670
+ free(w->sets);
1671
+ w->sets = NULL;
1672
+ w->num_sets = 0;
1673
+ }
1674
+
1675
+ /** Move the result set just read to `sets`. Returns -1 (with `w->error` set) when out of memory. */
1676
+ static int keep_result_set(QueryWork *w) {
1677
+ ResultSet *grown = (ResultSet *)realloc(w->sets, (size_t)(w->num_sets + 1) * sizeof(ResultSet));
1678
+ if (!grown) {
1679
+ snprintf(w->error, sizeof(w->error), "Memory allocation failed for result sets");
1680
+ return -1;
1681
+ }
1682
+ w->sets = grown;
1683
+ w->sets[w->num_sets++] = (ResultSet){w->columns, w->num_columns, w->num_rows};
1684
+ w->columns = NULL;
1685
+ w->num_columns = 0;
1686
+ w->num_rows = 0;
1687
+ return 0;
1165
1688
  }
1166
1689
 
1167
1690
  /** How a column of this type is stored (see ValueKind). */
@@ -1814,8 +2337,10 @@ static FILE *open_output(const char *path) {
1814
2337
  }
1815
2338
  FILE *f = fdopen(fd, "wb");
1816
2339
  if (!f) {
2340
+ // The file is this call's own (O_EXCL): leave nothing behind.
1817
2341
  int saved = errno;
1818
2342
  close(fd);
2343
+ unlink(path);
1819
2344
  errno = saved;
1820
2345
  }
1821
2346
  return f;
@@ -2063,6 +2588,7 @@ static void query_execute(napi_env env, void *data) {
2063
2588
  if (!w->holds_conn) {
2064
2589
  snprintf(w->error, sizeof(w->error), "Connection is closed");
2065
2590
  w->success = 0;
2591
+ w->conn_dead = 1;
2066
2592
  return;
2067
2593
  }
2068
2594
 
@@ -2075,6 +2601,7 @@ static void query_execute(napi_env env, void *data) {
2075
2601
  if (closing) {
2076
2602
  snprintf(w->error, sizeof(w->error), "Connection is closed");
2077
2603
  w->success = 0;
2604
+ w->conn_dead = 1;
2078
2605
  work_release_conn(w);
2079
2606
  return;
2080
2607
  }
@@ -2084,6 +2611,16 @@ static void query_execute(napi_env env, void *data) {
2084
2611
  snprintf(w->error, sizeof(w->error), "Connection is dead");
2085
2612
  conn_mark_dead(w->conn);
2086
2613
  w->success = 0;
2614
+ w->conn_dead = 1;
2615
+ work_release_conn(w);
2616
+ return;
2617
+ }
2618
+
2619
+ // The DBPROCESS is in the middle of a bulk copy: SQL sent now would be read
2620
+ // by the server as bulk data.
2621
+ if (w->conn->bulk) {
2622
+ snprintf(w->error, sizeof(w->error), "A bulk copy is in progress on this connection");
2623
+ w->success = 0;
2087
2624
  work_release_conn(w);
2088
2625
  return;
2089
2626
  }
@@ -2096,15 +2633,16 @@ static void query_execute(napi_env env, void *data) {
2096
2633
  size_t tmp_size = strlen(w->out_path) + 64;
2097
2634
  w->out_buf = (char *)malloc(OUT_BUF_SIZE);
2098
2635
  w->out_tmp = (char *)malloc(tmp_size);
2099
- if (w->out_buf && w->out_tmp) {
2636
+ int named = w->out_buf && w->out_tmp;
2637
+ if (named) {
2100
2638
  snprintf(w->out_tmp, tmp_size, "%s.%llx-%p.tmp", w->out_path,
2101
2639
  (unsigned long long)uv_hrtime(), (void *)w);
2102
2640
  w->out = open_output(w->out_tmp);
2103
2641
  }
2104
2642
  if (!w->out) {
2643
+ // The temporary's name only once it has been written into its buffer.
2105
2644
  snprintf(w->error, sizeof(w->error), "Could not create %s: %s",
2106
- w->out_tmp ? w->out_tmp : w->out_path,
2107
- w->out_buf && w->out_tmp ? strerror(errno) : "out of memory");
2645
+ named ? w->out_tmp : w->out_path, named ? strerror(errno) : "out of memory");
2108
2646
  w->success = 0;
2109
2647
  w->conn_dead = conn_is_dead(w->conn);
2110
2648
  work_release_conn(w);
@@ -2112,17 +2650,37 @@ static void query_execute(napi_env env, void *data) {
2112
2650
  }
2113
2651
  }
2114
2652
 
2115
- // Spawn the hard-timeout watchdog. Failure is non-fatal — db-lib's own
2116
- // DBSETTIME still provides a cooperative timeout.
2117
- watchdog_start(&w->wd, w->conn, w->hard_timeout_ms);
2653
+ // Restored at `done`, which every path from here on goes through.
2654
+ int timeout_overridden = w->timeout_seconds > 0 && w->timeout_seconds != w->conn->timeout_seconds;
2118
2655
 
2119
- // Send SQL command
2120
- if (dbcmd(w->conn->dbproc, w->sql) == FAIL) {
2121
- if (w->conn->has_error) {
2122
- snprintf(w->error, sizeof(w->error), "%s", w->conn->last_error);
2123
- } else {
2124
- snprintf(w->error, sizeof(w->error), "dbcmd() failed");
2125
- }
2656
+ // The hard timeout depends on the watchdog: DBSETTIME bounds each wait for
2657
+ // the server, not a query whose rows keep arriving. Without one the query
2658
+ // does not start, rather than run without its deadline.
2659
+ int started = watchdog_start(&w->wd, w->conn, w->hard_timeout_ms);
2660
+ if (started != 0) {
2661
+ // Expired while queued: `done` reports the hard timeout.
2662
+ snprintf(w->error, sizeof(w->error), "Could not start the hard-timeout watchdog");
2663
+ w->success = 0;
2664
+ timeout_overridden = 0;
2665
+ goto done;
2666
+ }
2667
+
2668
+ // Nothing is sent without the timeout asked for. Not overridden, there is
2669
+ // nothing to put back (the connection's own timeout is still set).
2670
+ if (timeout_overridden && set_query_timeout(w->conn, w->timeout_seconds) != 0) {
2671
+ snprintf(w->error, sizeof(w->error), "Could not set the query's timeout");
2672
+ w->success = 0;
2673
+ timeout_overridden = 0;
2674
+ goto done;
2675
+ }
2676
+
2677
+ // Send SQL command
2678
+ if (dbcmd(w->conn->dbproc, w->sql) == FAIL) {
2679
+ if (w->conn->has_error) {
2680
+ snprintf(w->error, sizeof(w->error), "%s", w->conn->last_error);
2681
+ } else {
2682
+ snprintf(w->error, sizeof(w->error), "dbcmd() failed");
2683
+ }
2126
2684
  w->success = 0;
2127
2685
  goto done;
2128
2686
  }
@@ -2148,6 +2706,9 @@ static void query_execute(napi_env env, void *data) {
2148
2706
  int found_resultset = 0;
2149
2707
 
2150
2708
  while ((result_code = dbresults(w->conn->dbproc)) != NO_MORE_RESULTS) {
2709
+ if (call_abandoned(&w->wd, w->conn)) {
2710
+ goto rows_failed;
2711
+ }
2151
2712
  if (result_code == FAIL) {
2152
2713
  if (w->conn->has_error) {
2153
2714
  snprintf(w->error, sizeof(w->error), "%s", w->conn->last_error);
@@ -2174,20 +2735,26 @@ static void query_execute(napi_env env, void *data) {
2174
2735
  if (cnt > 0) {
2175
2736
  w->affected_rows += cnt;
2176
2737
  }
2177
- if (discard_rows(w->conn->dbproc) == FAIL) {
2738
+ if (query_discard_rows(w) == FAIL) {
2178
2739
  goto rows_failed;
2179
2740
  }
2180
2741
  continue;
2181
2742
  }
2182
2743
 
2183
- // If we already found a result set, skip subsequent ones
2184
- // (we return the FIRST result set with columns)
2185
- if (found_resultset) {
2186
- if (discard_rows(w->conn->dbproc) == FAIL) {
2744
+ // Only the first result set with columns is returned, unless every one
2745
+ // was asked for: the one read before this is then kept aside.
2746
+ if (found_resultset && !w->all_result_sets) {
2747
+ if (query_discard_rows(w) == FAIL) {
2187
2748
  goto rows_failed;
2188
2749
  }
2189
2750
  continue;
2190
2751
  }
2752
+ if (found_resultset && keep_result_set(w) != 0) {
2753
+ discard_results(w);
2754
+ dbcancel(w->conn->dbproc);
2755
+ w->success = 0;
2756
+ goto done;
2757
+ }
2191
2758
  found_resultset = 1;
2192
2759
 
2193
2760
  // Found a result set with columns — read it
@@ -2243,6 +2810,7 @@ static void query_execute(napi_env env, void *data) {
2243
2810
  }
2244
2811
 
2245
2812
  RETCODE row_code;
2813
+ long rows_read = 0;
2246
2814
  while ((row_code = dbnextrow(w->conn->dbproc)) != NO_MORE_ROWS) {
2247
2815
  if (row_code == FAIL) {
2248
2816
  if (w->conn->has_error) {
@@ -2261,11 +2829,15 @@ static void query_execute(napi_env env, void *data) {
2261
2829
  continue;
2262
2830
  }
2263
2831
 
2832
+ if (++rows_read % WATCHDOG_CHECK_ROWS == 0 && call_abandoned(&w->wd, w->conn)) {
2833
+ goto rows_failed;
2834
+ }
2835
+
2264
2836
  // Safety: cap rows
2265
2837
  if ((file_mode ? w->out_rows : w->num_rows) >= row_cap) {
2266
2838
  w->truncated = 1;
2267
2839
  // Drain remaining rows without storing
2268
- if (discard_rows(w->conn->dbproc) == FAIL) {
2840
+ if (query_discard_rows(w) == FAIL) {
2269
2841
  goto rows_failed;
2270
2842
  }
2271
2843
  break;
@@ -2312,6 +2884,12 @@ static void query_execute(napi_env env, void *data) {
2312
2884
  }
2313
2885
  }
2314
2886
 
2887
+ if (w->all_result_sets && found_resultset && keep_result_set(w) != 0) {
2888
+ discard_results(w);
2889
+ w->success = 0;
2890
+ goto done;
2891
+ }
2892
+
2315
2893
  // Rows were dropped without the caller asking for a cap: returning the rest
2316
2894
  // as if it were the whole result would be silently wrong data.
2317
2895
  if (w->truncated && !w->max_rows_explicit) {
@@ -2341,10 +2919,13 @@ static void query_execute(napi_env env, void *data) {
2341
2919
  goto done;
2342
2920
 
2343
2921
  rows_failed:
2344
- // The result stream broke while discarding rows (typically a dead
2345
- // connection). No dbcancel: there is nothing left to cancel on.
2922
+ // The result stream broke (typically a dead connection), or the hard
2923
+ // timeout fired and the rest is not read. No dbcancel: the connection is
2924
+ // dead either way, and is closed.
2346
2925
  if (w->conn->has_error) {
2347
2926
  snprintf(w->error, sizeof(w->error), "%s", w->conn->last_error);
2927
+ } else if (conn_abandoned(w->conn)) {
2928
+ snprintf(w->error, sizeof(w->error), "Connection was closed while the query ran");
2348
2929
  } else {
2349
2930
  snprintf(w->error, sizeof(w->error), "dbnextrow() failed");
2350
2931
  }
@@ -2352,6 +2933,12 @@ rows_failed:
2352
2933
  w->success = 0;
2353
2934
 
2354
2935
  done:
2936
+ // A connection left with this call's timeout must not be used again: the
2937
+ // result stands, but the connection is given up.
2938
+ if (restore_query_timeout(w->conn, timeout_overridden) != 0) {
2939
+ conn_mark_dead(w->conn);
2940
+ }
2941
+
2355
2942
  // Write out the rest of the file while the watchdog still covers it.
2356
2943
  if (w->out) {
2357
2944
  if (w->success && out_flush(w) != 0) {
@@ -2381,20 +2968,22 @@ done:
2381
2968
  // and a result appearing after the caller was told it failed would be taken
2382
2969
  // for the outcome of a later attempt.
2383
2970
  if (w->out_tmp) {
2384
- // Checked and published under the lock close_connection sets
2385
- // pending_close under: a close either lands first, and nothing is
2386
- // published, or waits for the rename, and the export finished before it.
2971
+ // Decided under the lock close_connection sets pending_close under: a close
2972
+ // that lands first stops the publishing. The rename itself runs outside it:
2973
+ // a close must never wait on the file system (a hung network share would
2974
+ // hold up the JS thread with it). A close landing after the decision finds
2975
+ // the export finished — or finishing, if the rename is slow.
2387
2976
  uv_mutex_lock(&w->conn->lock);
2388
2977
  if (w->success && w->conn->pending_close) {
2389
2978
  snprintf(w->error, sizeof(w->error), "Connection was closed before the export finished");
2390
2979
  w->success = 0;
2391
2980
  }
2981
+ uv_mutex_unlock(&w->conn->lock);
2392
2982
  if (w->success && publish_output(w->out_tmp, w->out_path) != 0) {
2393
2983
  snprintf(w->error, sizeof(w->error), "Could not move %s to %s: %s", w->out_tmp, w->out_path,
2394
2984
  strerror(errno));
2395
2985
  w->success = 0;
2396
2986
  }
2397
- uv_mutex_unlock(&w->conn->lock);
2398
2987
  if (!w->success) {
2399
2988
  remove_output(w->out_tmp);
2400
2989
  }
@@ -2537,97 +3126,130 @@ static napi_status build_column(napi_env env, const ResultColumn *col, int num_r
2537
3126
  * `{ columns, columnTypes, data, rowCount, affectedRows }`; for a result
2538
3127
  * streamed to a file, `{ columns, columnTypes, rowCount, affectedRows, bytes }`.
2539
3128
  */
2540
- static napi_status build_result(napi_env env, const QueryWork *w, napi_value *out) {
2541
- napi_value cols_arr, col_types_arr, data_arr, row_count, affected;
2542
- size_t ncols = (size_t)w->num_columns;
2543
- NAPI_TRY(napi_create_object(env, out));
3129
+ /** One result set's `columns`, `columnTypes` and (unless written to a file) `data`, set on `out`. */
3130
+ static napi_status build_set(napi_env env, const ResultColumn *columns, int num_columns,
3131
+ int num_rows, int with_data, napi_value out) {
3132
+ napi_value cols_arr, col_types_arr, data_arr;
3133
+ size_t ncols = (size_t)num_columns;
2544
3134
  NAPI_TRY(napi_create_array_with_length(env, ncols, &cols_arr));
2545
3135
  NAPI_TRY(napi_create_array_with_length(env, ncols, &col_types_arr));
2546
3136
  NAPI_TRY(napi_create_array_with_length(env, ncols, &data_arr));
2547
3137
 
2548
3138
  // Column names, Sybase type names and values, in column order
2549
- for (int i = 0; i < w->num_columns; i++) {
3139
+ for (int i = 0; i < num_columns; i++) {
2550
3140
  napi_value col_name, col_type, col_data;
2551
- NAPI_TRY(napi_create_string_utf8(env, w->columns[i].name, NAPI_AUTO_LENGTH, &col_name));
3141
+ NAPI_TRY(napi_create_string_utf8(env, columns[i].name, NAPI_AUTO_LENGTH, &col_name));
2552
3142
  NAPI_TRY(napi_set_element(env, cols_arr, (uint32_t)i, col_name));
2553
- NAPI_TRY(napi_create_string_utf8(env, syb_type_name(w->columns[i].type), NAPI_AUTO_LENGTH,
3143
+ NAPI_TRY(napi_create_string_utf8(env, syb_type_name(columns[i].type), NAPI_AUTO_LENGTH,
2554
3144
  &col_type));
2555
3145
  NAPI_TRY(napi_set_element(env, col_types_arr, (uint32_t)i, col_type));
2556
- if (!w->out_path) {
2557
- NAPI_TRY(build_column(env, &w->columns[i], w->num_rows, &col_data));
3146
+ if (with_data) {
3147
+ NAPI_TRY(build_column(env, &columns[i], num_rows, &col_data));
2558
3148
  NAPI_TRY(napi_set_element(env, data_arr, (uint32_t)i, col_data));
2559
3149
  }
2560
3150
  }
3151
+ if (with_data) {
3152
+ napi_value row_count;
3153
+ NAPI_TRY(napi_create_int32(env, num_rows, &row_count));
3154
+ NAPI_TRY(napi_set_named_property(env, out, "rowCount", row_count));
3155
+ NAPI_TRY(napi_set_named_property(env, out, "data", data_arr));
3156
+ }
3157
+ NAPI_TRY(napi_set_named_property(env, out, "columns", cols_arr));
3158
+ return napi_set_named_property(env, out, "columnTypes", col_types_arr);
3159
+ }
2561
3160
 
3161
+ /**
3162
+ * `{ columns, columnTypes, data, rowCount, affectedRows }`; for a result
3163
+ * streamed to a file, `{ columns, columnTypes, rowCount, affectedRows, bytes }`.
3164
+ * With allResultSets, also `resultSets: [{ columns, columnTypes, data, rowCount }]`,
3165
+ * the top level then describing the first of them.
3166
+ */
3167
+ static napi_status build_result(napi_env env, const QueryWork *w, napi_value *out) {
3168
+ napi_value affected;
3169
+ NAPI_TRY(napi_create_object(env, out));
3170
+ if (w->all_result_sets) {
3171
+ napi_value sets, first = NULL;
3172
+ NAPI_TRY(napi_create_array_with_length(env, (size_t)w->num_sets, &sets));
3173
+ for (int i = 0; i < w->num_sets; i++) {
3174
+ napi_value set;
3175
+ NAPI_TRY(napi_create_object(env, &set));
3176
+ NAPI_TRY(build_set(env, w->sets[i].columns, w->sets[i].num_columns, w->sets[i].num_rows, 1,
3177
+ set));
3178
+ NAPI_TRY(napi_set_element(env, sets, (uint32_t)i, set));
3179
+ if (i == 0) {
3180
+ first = set;
3181
+ }
3182
+ }
3183
+ if (first) {
3184
+ // The top level is the first set: the same arrays, not a second copy.
3185
+ static const char *const keys[] = {"columns", "columnTypes", "data", "rowCount"};
3186
+ for (size_t k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
3187
+ napi_value value;
3188
+ NAPI_TRY(napi_get_named_property(env, first, keys[k], &value));
3189
+ NAPI_TRY(napi_set_named_property(env, *out, keys[k], value));
3190
+ }
3191
+ } else {
3192
+ NAPI_TRY(build_set(env, NULL, 0, 0, 1, *out));
3193
+ }
3194
+ NAPI_TRY(napi_set_named_property(env, *out, "resultSets", sets));
3195
+ } else {
3196
+ NAPI_TRY(build_set(env, w->columns, w->num_columns, w->num_rows, !w->out_path, *out));
3197
+ }
2562
3198
  if (w->out_path) {
2563
3199
  // Past int32 for a large file; exact in a double far beyond any row count.
2564
- napi_value bytes;
3200
+ napi_value row_count, bytes;
2565
3201
  NAPI_TRY(napi_create_double(env, (double)w->out_rows, &row_count));
2566
3202
  NAPI_TRY(napi_create_double(env, (double)w->out_bytes, &bytes));
2567
3203
  NAPI_TRY(napi_set_named_property(env, *out, "bytes", bytes));
2568
- } else {
2569
- NAPI_TRY(napi_create_int32(env, w->num_rows, &row_count));
2570
- NAPI_TRY(napi_set_named_property(env, *out, "data", data_arr));
3204
+ NAPI_TRY(napi_set_named_property(env, *out, "rowCount", row_count));
2571
3205
  }
2572
3206
  NAPI_TRY(napi_create_int32(env, w->affected_rows, &affected));
2573
- NAPI_TRY(napi_set_named_property(env, *out, "columns", cols_arr));
2574
- NAPI_TRY(napi_set_named_property(env, *out, "columnTypes", col_types_arr));
2575
- NAPI_TRY(napi_set_named_property(env, *out, "rowCount", row_count));
2576
3207
  return napi_set_named_property(env, *out, "affectedRows", affected);
2577
3208
  }
2578
3209
 
3210
+ /** The error a failed query rejects with: its message and what became of the connection. */
3211
+ static napi_status query_error(napi_env env, const QueryWork *w, napi_value *error) {
3212
+ napi_value msg, flag;
3213
+ NAPI_TRY(napi_create_string_utf8(env, w->error, NAPI_AUTO_LENGTH, &msg));
3214
+ NAPI_TRY(napi_create_error(env, NULL, msg, error));
3215
+ NAPI_TRY(napi_get_boolean(env, true, &flag));
3216
+ if (w->conn_dead) {
3217
+ NAPI_TRY(napi_set_named_property(env, *error, "connectionDead", flag));
3218
+ }
3219
+ if (w->conn_timed_out) {
3220
+ NAPI_TRY(napi_set_named_property(env, *error, "timedOut", flag));
3221
+ }
3222
+ if (w->wd.fired) {
3223
+ NAPI_TRY(napi_set_named_property(env, *error, "hardTimeout", flag));
3224
+ }
3225
+ return napi_ok;
3226
+ }
3227
+
2579
3228
  static void query_complete(napi_env env, napi_status status, void *data) {
2580
3229
  QueryWork *w = (QueryWork *)data;
2581
3230
 
2582
3231
  if (status == napi_cancelled || !w->success) {
2583
- napi_value error_msg, error_obj;
2584
- napi_create_string_utf8(env, w->error, NAPI_AUTO_LENGTH, &error_msg);
2585
- napi_create_error(env, NULL, error_msg, &error_obj);
2586
-
2587
- if (w->conn_dead) {
2588
- napi_value dead_val;
2589
- napi_get_boolean(env, true, &dead_val);
2590
- napi_set_named_property(env, error_obj, "connectionDead", dead_val);
2591
- }
2592
-
2593
- if (w->conn_timed_out) {
2594
- napi_value to_val;
2595
- napi_get_boolean(env, true, &to_val);
2596
- napi_set_named_property(env, error_obj, "timedOut", to_val);
2597
- }
2598
-
2599
- if (w->wd.fired) {
2600
- napi_value forced_val;
2601
- napi_get_boolean(env, true, &forced_val);
2602
- napi_set_named_property(env, error_obj, "hardTimeout", forced_val);
3232
+ napi_value error_obj;
3233
+ if (query_error(env, w, &error_obj) == napi_ok) {
3234
+ napi_reject_deferred(env, w->deferred, error_obj);
3235
+ } else {
3236
+ reject_fallback(env, w->deferred, w->error);
2603
3237
  }
2604
-
2605
- napi_reject_deferred(env, w->deferred, error_obj);
2606
3238
  } else {
2607
3239
  napi_value result;
2608
3240
  if (build_result(env, w, &result) == napi_ok) {
2609
3241
  napi_resolve_deferred(env, w->deferred, result);
2610
3242
  } else {
2611
3243
  // Typically V8 running out of memory for a very large result.
2612
- bool pending = false;
2613
- napi_value error_obj;
2614
- napi_is_exception_pending(env, &pending);
2615
- if (pending) {
2616
- napi_get_and_clear_last_exception(env, &error_obj);
2617
- } else {
2618
- napi_value error_msg;
2619
- napi_create_string_utf8(env, "Could not hand the query result to JavaScript",
2620
- NAPI_AUTO_LENGTH, &error_msg);
2621
- napi_create_error(env, NULL, error_msg, &error_obj);
2622
- }
2623
- napi_reject_deferred(env, w->deferred, error_obj);
3244
+ reject_fallback(env, w->deferred, "Could not hand the query result to JavaScript");
2624
3245
  }
2625
3246
  }
2626
3247
 
2627
3248
  // Cleanup — always runs regardless of success/failure
2628
3249
  // A cancelled work item never ran query_execute, so its slot is still held.
2629
3250
  work_release_conn(w);
2630
- free_columns(w->columns, w->num_columns);
3251
+ release_handle(env, &w->claim);
3252
+ discard_results(w);
2631
3253
  free(w->sql);
2632
3254
  free(w->out_path);
2633
3255
  free(w->out_tmp);
@@ -2644,19 +3266,38 @@ static void query_complete(napi_env env, napi_status status, void *data) {
2644
3266
  * `serializeValue` write such strings as `U&'...'` literals, which carry any
2645
3267
  * character in ASCII escapes. Returns NULL with an exception pending.
2646
3268
  */
3269
+ /**
3270
+ * db-lib takes the SQL as a C string: a NUL would end it there, and the server
3271
+ * would run only what comes before.
3272
+ */
3273
+ static const char sql_nul_message[] =
3274
+ "The SQL contains a NUL character, which would cut it short: only what precedes it would run";
3275
+
2647
3276
  static char *sql_bytes(napi_env env, napi_value value, int latin1) {
2648
3277
  size_t len = 0;
2649
3278
  if (!latin1) {
2650
- napi_get_value_string_utf8(env, value, NULL, 0, &len);
3279
+ if (napi_get_value_string_utf8(env, value, NULL, 0, &len) != napi_ok) {
3280
+ return (char *)fail_call(env, "Could not read the SQL");
3281
+ }
2651
3282
  char *out = (char *)malloc(len + 1);
2652
3283
  if (!out) {
2653
3284
  napi_throw_error(env, NULL, "Out of memory");
2654
3285
  return NULL;
2655
3286
  }
2656
- napi_get_value_string_utf8(env, value, out, len + 1, &len);
3287
+ if (napi_get_value_string_utf8(env, value, out, len + 1, &len) != napi_ok) {
3288
+ free(out);
3289
+ return (char *)fail_call(env, "Could not read the SQL");
3290
+ }
3291
+ if (strlen(out) != len) {
3292
+ free(out);
3293
+ napi_throw_type_error(env, NULL, sql_nul_message);
3294
+ return NULL;
3295
+ }
2657
3296
  return out;
2658
3297
  }
2659
- napi_get_value_string_utf16(env, value, NULL, 0, &len);
3298
+ if (napi_get_value_string_utf16(env, value, NULL, 0, &len) != napi_ok) {
3299
+ return (char *)fail_call(env, "Could not read the SQL");
3300
+ }
2660
3301
  char16_t *units = (char16_t *)malloc((len + 1) * sizeof(char16_t));
2661
3302
  char *out = units ? (char *)malloc(len + 1) : NULL;
2662
3303
  if (!out) {
@@ -2664,8 +3305,18 @@ static char *sql_bytes(napi_env env, napi_value value, int latin1) {
2664
3305
  napi_throw_error(env, NULL, "Out of memory");
2665
3306
  return NULL;
2666
3307
  }
2667
- napi_get_value_string_utf16(env, value, units, len + 1, &len);
3308
+ if (napi_get_value_string_utf16(env, value, units, len + 1, &len) != napi_ok) {
3309
+ free(units);
3310
+ free(out);
3311
+ return (char *)fail_call(env, "Could not read the SQL");
3312
+ }
2668
3313
  for (size_t i = 0; i < len; i++) {
3314
+ if (units[i] == 0) {
3315
+ free(units);
3316
+ free(out);
3317
+ napi_throw_type_error(env, NULL, sql_nul_message);
3318
+ return NULL;
3319
+ }
2669
3320
  if (units[i] > 0xFF) {
2670
3321
  char message[400];
2671
3322
  snprintf(message, sizeof(message),
@@ -2692,6 +3343,9 @@ static char *sql_bytes(napi_env env, napi_value value, int latin1) {
2692
3343
  // we derive it from the connection's timeout (2x timeout, min 30s).
2693
3344
  // options.wallClockDateTimes: hand datetime/smalldatetime over as wall-clock
2694
3345
  // numbers ("wallclock" kind) instead of canonical text.
3346
+ // options.allResultSets: return every result set with columns as `resultSets`,
3347
+ // not only the first (the top level still describes the first).
3348
+ // options.timeoutSeconds: the cooperative timeout of this query alone.
2695
3349
  // options.outputPath: stream the rows to this CSV file instead (see
2696
3350
  // csv_write_row); the result then carries no data, and `bytes`.
2697
3351
  //
@@ -2706,105 +3360,149 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
2706
3360
  if (handle == NULL) {
2707
3361
  return NULL; // TypeError pending
2708
3362
  }
3363
+ if (!handle_available(handle)) {
3364
+ return busy_rejection(env);
3365
+ }
2709
3366
 
2710
3367
  napi_valuetype sql_type = napi_undefined;
2711
3368
  if (argc < 2 || napi_typeof(env, args[1], &sql_type) != napi_ok || sql_type != napi_string) {
2712
3369
  napi_throw_type_error(env, NULL, "Expected the SQL as a string");
2713
3370
  return NULL;
2714
3371
  }
2715
- // The SQL goes to the server in its own character set (see SybaseConnection.latin1).
2716
- char *sql = sql_bytes(env, args[1], handle->conn != NULL && handle->conn->latin1);
2717
- if (sql == NULL) {
2718
- return NULL; // error pending
2719
- }
2720
3372
 
2721
3373
  QueryWork *w = (QueryWork *)calloc(1, sizeof(QueryWork));
2722
3374
  if (w == NULL) {
2723
- free(sql);
2724
3375
  napi_throw_error(env, NULL, "Out of memory");
2725
3376
  return NULL;
2726
3377
  }
2727
- w->sql = sql;
2728
-
2729
- // A closed handle has no connection: the query is rejected as on a closed
2730
- // connection (holds_conn stays 0 below).
2731
- w->conn = handle->conn;
2732
-
2733
- // Defaults
2734
3378
  w->max_rows = DEFAULT_MAX_ROWS;
2735
- // Hard timeout: 2x the cooperative timeout, with a 30s floor so very small
2736
- // configured timeouts don't translate to absurdly tight watchdog deadlines.
2737
- int base_timeout_s = (w->conn && w->conn->timeout_seconds > 0)
2738
- ? w->conn->timeout_seconds
2739
- : 30;
2740
- w->hard_timeout_ms = base_timeout_s * 2 * 1000;
2741
- if (w->hard_timeout_ms < 30000) {
2742
- w->hard_timeout_ms = 30000;
2743
- }
3379
+ int hard_timeout_given = 0;
2744
3380
 
3381
+ // The options first: reading them may run JavaScript (a getter), which may
3382
+ // close the handle or start another call on it. Its connection is looked at
3383
+ // only after, once nothing else can run.
3384
+ // Each option is checked, never truncated or ignored: a getter that throws
3385
+ // leaves its exception pending; a value out of range is refused.
2745
3386
  if (argc >= 3) {
2746
- napi_valuetype vtype;
2747
- napi_typeof(env, args[2], &vtype);
3387
+ napi_valuetype vtype = napi_undefined;
3388
+ if (napi_typeof(env, args[2], &vtype) != napi_ok) {
3389
+ free(w);
3390
+ return fail_call(env, "Could not read the query options");
3391
+ }
2748
3392
  if (vtype == napi_object) {
2749
3393
  napi_value v;
2750
-
2751
- napi_get_named_property(env, args[2], "maxRows", &v);
2752
- napi_typeof(env, v, &vtype);
2753
- if (vtype == napi_number) {
2754
- int32_t requested = 0;
2755
- napi_get_value_int32(env, v, &requested);
2756
- if (requested > 0) {
2757
- w->max_rows = requested;
2758
- w->max_rows_explicit = 1;
2759
- }
2760
- }
2761
-
2762
- napi_get_named_property(env, args[2], "hardTimeoutMs", &v);
2763
- napi_typeof(env, v, &vtype);
2764
- if (vtype == napi_number) {
2765
- napi_get_value_int32(env, v, &w->hard_timeout_ms);
3394
+ napi_valuetype ptype = napi_undefined;
3395
+ int max_rows = 0;
3396
+ int path_ok = 1;
3397
+ if (config_int(env, args[2], "timeoutSeconds", 1, MAX_TIMEOUT_SECONDS, 0,
3398
+ &w->timeout_seconds) != 0 ||
3399
+ config_int(env, args[2], "maxRows", 1, INT32_MAX, 0, &max_rows) != 0 ||
3400
+ config_int(env, args[2], "hardTimeoutMs", 1, INT32_MAX, 0, &w->hard_timeout_ms) != 0 ||
3401
+ option_bool(env, args[2], "wallClockDateTimes", &w->wall_clock_datetimes) != 0 ||
3402
+ option_bool(env, args[2], "allResultSets", &w->all_result_sets) != 0 ||
3403
+ napi_get_named_property(env, args[2], "outputPath", &v) != napi_ok ||
3404
+ napi_typeof(env, v, &ptype) != napi_ok) {
3405
+ free(w);
3406
+ return fail_call(env, "Could not read the query options");
2766
3407
  }
2767
-
2768
- napi_get_named_property(env, args[2], "wallClockDateTimes", &v);
2769
- napi_typeof(env, v, &vtype);
2770
- if (vtype == napi_boolean) {
2771
- bool flag = false;
2772
- napi_get_value_bool(env, v, &flag);
2773
- w->wall_clock_datetimes = flag ? 1 : 0;
3408
+ if (max_rows > 0) {
3409
+ w->max_rows = max_rows;
3410
+ w->max_rows_explicit = 1;
2774
3411
  }
2775
-
2776
- napi_get_named_property(env, args[2], "outputPath", &v);
2777
- napi_typeof(env, v, &vtype);
2778
- if (vtype == napi_string) {
3412
+ hard_timeout_given = w->hard_timeout_ms > 0;
3413
+ if (ptype == napi_string) {
2779
3414
  size_t path_len = 0;
2780
- napi_get_value_string_utf8(env, v, NULL, 0, &path_len);
2781
- // Validated by the JS wrapper (non-empty, no NUL), its only caller.
2782
- w->out_path = (char *)malloc(path_len + 1);
2783
- if (w->out_path == NULL) {
2784
- free(w->sql);
3415
+ path_ok = napi_get_value_string_utf8(env, v, NULL, 0, &path_len) == napi_ok;
3416
+ w->out_path = path_ok ? (char *)malloc(path_len + 1) : NULL;
3417
+ if (path_ok && w->out_path == NULL) {
2785
3418
  free(w);
2786
3419
  napi_throw_error(env, NULL, "Out of memory");
2787
3420
  return NULL;
2788
3421
  }
2789
- napi_get_value_string_utf8(env, v, w->out_path, path_len + 1, &path_len);
2790
- // Datetimes go to the file as their canonical text.
3422
+ path_ok = path_ok &&
3423
+ napi_get_value_string_utf8(env, v, w->out_path, path_len + 1, &path_len) == napi_ok &&
3424
+ // A NUL would cut the path short in C, and write somewhere else.
3425
+ path_len > 0 && strlen(w->out_path) == path_len;
3426
+ // Datetimes go to the file as their canonical text; a file holds one result set.
2791
3427
  w->wall_clock_datetimes = 0;
3428
+ w->all_result_sets = 0;
3429
+ } else if (ptype != napi_undefined) {
3430
+ path_ok = 0;
3431
+ }
3432
+ if (!path_ok) {
3433
+ free(w->out_path);
3434
+ free(w);
3435
+ napi_throw_type_error(env, NULL, "Expected outputPath as a non-empty path without NUL");
3436
+ return NULL;
2792
3437
  }
3438
+ } else if (vtype != napi_undefined) {
3439
+ free(w);
3440
+ napi_throw_type_error(env, NULL, "Expected the query options as an object");
3441
+ return NULL;
2793
3442
  }
2794
3443
  }
2795
3444
 
2796
- napi_value promise;
2797
- napi_create_promise(env, &w->deferred, &promise);
3445
+ // Checked again now that no JavaScript can run before the call is queued.
3446
+ if (!handle_available(handle)) {
3447
+ free(w->out_path);
3448
+ free(w);
3449
+ return busy_rejection(env);
3450
+ }
3451
+ // A closed handle has no connection: the query is rejected as on a closed
3452
+ // connection (holds_conn stays 0 below).
3453
+ w->conn = handle->conn;
3454
+
3455
+ // The SQL goes to the server in its own character set (see SybaseConnection.latin1).
3456
+ w->sql = sql_bytes(env, args[1], w->conn != NULL && w->conn->latin1);
3457
+ if (w->sql == NULL) {
3458
+ free(w->out_path);
3459
+ free(w);
3460
+ return NULL; // error pending
3461
+ }
3462
+
3463
+ // Hard timeout: 2x the cooperative timeout of this call, with a 30s floor so
3464
+ // very small timeouts don't translate to absurdly tight watchdog deadlines.
3465
+ // In 64 bits, and capped: a long timeout must not wrap around to a short one.
3466
+ if (!hard_timeout_given) {
3467
+ int64_t base_timeout_s = w->timeout_seconds > 0 ? w->timeout_seconds
3468
+ : (w->conn && w->conn->timeout_seconds > 0) ? w->conn->timeout_seconds
3469
+ : 30;
3470
+ int64_t ms = base_timeout_s * 2000;
3471
+ w->hard_timeout_ms = ms < 30000 ? 30000 : ms > INT32_MAX ? INT32_MAX : (int)ms;
3472
+ }
3473
+
3474
+ // The hard timeout counts from now, not from when a worker picks it up.
3475
+ w->wd.queued_ns = uv_hrtime();
3476
+ if (claim_handle(env, args[0], handle, &w->claim) != 0) {
3477
+ free(w->sql);
3478
+ free(w->out_path);
3479
+ free(w);
3480
+ napi_throw_error(env, NULL, "Could not reference the connection handle");
3481
+ return NULL;
3482
+ }
2798
3483
 
2799
3484
  // Pin the connection now, on the JS thread, rather than when a worker picks
2800
3485
  // the job up: fn_close only defers the free while in_flight > 0, and the
2801
- // job may sit in the libuv queue behind other queries for a long time.
3486
+ // job may sit in the libuv queue behind other queries for a long time. And
3487
+ // before the promise is made: making one runs promise hooks, JavaScript that
3488
+ // may close this handle.
2802
3489
  w->holds_conn = (w->conn != NULL && conn_acquire(w->conn) == 0);
2803
3490
 
2804
- napi_value work_name;
2805
- napi_create_string_utf8(env, "sybase_query", NAPI_AUTO_LENGTH, &work_name);
2806
- napi_status st = napi_create_async_work(env, NULL, work_name, query_execute, query_complete,
2807
- w, &w->work);
3491
+ napi_value promise, work_name;
3492
+ if (napi_create_promise(env, &w->deferred, &promise) != napi_ok) {
3493
+ work_release_conn(w);
3494
+ release_handle(env, &w->claim);
3495
+ free(w->sql);
3496
+ free(w->out_path);
3497
+ free(w);
3498
+ napi_throw_error(env, NULL, "Could not create the query's promise");
3499
+ return NULL;
3500
+ }
3501
+
3502
+ napi_status st = napi_create_string_utf8(env, "sybase_query", NAPI_AUTO_LENGTH, &work_name);
3503
+ if (st == napi_ok) {
3504
+ st = napi_create_async_work(env, NULL, work_name, query_execute, query_complete, w, &w->work);
3505
+ }
2808
3506
  if (st == napi_ok) {
2809
3507
  st = napi_queue_async_work(env, w->work);
2810
3508
  if (st != napi_ok) {
@@ -2815,10 +3513,8 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
2815
3513
  // query_complete will never run, so undo here what it would have:
2816
3514
  // release the connection slot, settle the promise and free the work.
2817
3515
  work_release_conn(w);
2818
- napi_value msg, err;
2819
- napi_create_string_utf8(env, "Failed to schedule query", NAPI_AUTO_LENGTH, &msg);
2820
- napi_create_error(env, NULL, msg, &err);
2821
- napi_reject_deferred(env, w->deferred, err);
3516
+ release_handle(env, &w->claim);
3517
+ reject_fallback(env, w->deferred, "Failed to schedule query");
2822
3518
  free(w->sql);
2823
3519
  free(w->out_path);
2824
3520
  free(w);
@@ -2828,78 +3524,2373 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
2828
3524
  }
2829
3525
 
2830
3526
  // ---------------------------------------------------------------------------
2831
- // Close connection
2832
- // ---------------------------------------------------------------------------
2833
-
2834
- // fn_close: deferred-safe close.
3527
+ // Bulk copy in
2835
3528
  //
2836
- // If a query worker is currently running on this connection (e.g. JS layer
2837
- // gave up on a hard timeout but the C-level dbnextrow is still blocked), we
2838
- // do NOT free dbproc here — that would be a use-after-free in the worker.
2839
- // Instead we set pending_close; conn_release in the worker will perform the
2840
- // actual destruction once it returns.
3529
+ // db-lib's bulk copy API (bcp_init, bcp_bind, bcp_sendrow, bcp_batch,
3530
+ // bcp_done) over TDS 5.0. One copy spans several calls from JS — bulkBegin,
3531
+ // then bulkSend once per chunk of rows — and in between the DBPROCESS stays in
3532
+ // the middle of the copy (conn->bulk) and takes no other command. JS holds the
3533
+ // connection for the whole copy.
2841
3534
  //
2842
- // We deliberately do NOT call dbcancel from this thread either: dbcancel
2843
- // invokes tds_process_cancel which reads from the same socket the worker
2844
- // is reading from. We rely on db-lib's per-connection DBSETTIME to abort
2845
- // the worker; if the user wants tighter behavior they must configure a
2846
- // smaller `timeout` rather than a smaller hardTimeoutMs.
2847
- static napi_value fn_close(napi_env env, napi_callback_info info) {
2848
- size_t argc = 1;
2849
- napi_value args[1];
2850
- napi_get_cb_info(env, info, &argc, args, NULL, NULL);
3535
+ // What FreeTDS does and does not do here shapes this code:
3536
+ // - It cuts a value longer than its column to the column's size, silently.
3537
+ // So every value is checked here first: nothing is truncated, an over-long
3538
+ // value rejects its row.
3539
+ // - It does not convert text: on TDS 5.0 a row goes to the server as the
3540
+ // server's own row image. Text is encoded here — to Latin-1 for an iso_1
3541
+ // server — and a character the server's character set cannot hold rejects
3542
+ // its row instead of becoming '?'.
3543
+ // - It cannot abandon a batch: dbcancel() in the middle of bulk data is not
3544
+ // implemented ("TODO handle cancellation sending data"). A copy is
3545
+ // abandoned by closing the connection, and the server rolls back the batch
3546
+ // whose end it never received. A failed copy therefore marks its
3547
+ // connection dead.
3548
+ // - Its text conversions round (money, numeric) where a copy must fail
3549
+ // instead, so values are converted to the column's binary form here, by
3550
+ // exact parsers, and bound in that form.
3551
+ // - It substitutes the column default for a NULL, as the bcp utility does,
3552
+ // unless the KEEP_NULLS hint is given (bcp's --ignoredefaults).
3553
+ // ---------------------------------------------------------------------------
2851
3554
 
2852
- ConnHandle *handle = get_conn_handle(env, args[0]);
2853
- if (handle == NULL) {
2854
- return NULL; // TypeError pending
3555
+ /** How a column's values are converted; see bulk_classify. */
3556
+ typedef enum {
3557
+ BULK_INT, // tinyint, smallint, int, bigint: bound as a bigint
3558
+ BULK_BIT, // bit
3559
+ BULK_FLOAT, // float, real: bound as a float
3560
+ BULK_MONEY, // money, smallmoney: bound as money
3561
+ BULK_NUMERIC, // numeric, decimal
3562
+ BULK_DATETIME, // datetime
3563
+ BULK_SMALLDATETIME, // smalldatetime
3564
+ BULK_TEXT, // char, varchar (and nchar, nvarchar)
3565
+ BULK_BINARY, // binary, varbinary
3566
+ BULK_UNSUPPORTED,
3567
+ } BulkKind;
3568
+
3569
+ typedef struct {
3570
+ char *name; // UTF-8
3571
+ int type; // column_type()
3572
+ int usertype;
3573
+ BulkKind kind;
3574
+ int max_length; // bytes
3575
+ int precision;
3576
+ int scale;
3577
+ int nullable;
3578
+ int identity;
3579
+ int64_t min; // BULK_INT range
3580
+ int64_t max;
3581
+ BYTE *buf; // the value of the row being sent, when the column is bound
3582
+ } BulkColumn;
3583
+
3584
+ /** One field of a row: UTF-8 bytes (TSV input) or UTF-16 units (value rows). */
3585
+ typedef struct {
3586
+ const unsigned char *bytes;
3587
+ const uint16_t *units;
3588
+ size_t length; // in bytes or units
3589
+ } BulkField;
3590
+
3591
+ typedef struct BulkState {
3592
+ BulkColumn *columns; // every column of the table, in table order
3593
+ int num_columns;
3594
+ int *bound; // indexes into `columns`, in the order rows give their values
3595
+ int num_bound;
3596
+ int started; // 1 once bcp_sendrow has opened the copy on the server
3597
+ int reopen_failed; // a committed batch's copy could not be opened again
3598
+ char reopen_error[512];
3599
+ int timeout_overridden;
3600
+ char *table;
3601
+ int identity_insert; // 1 while `set identity_insert <table> on` is in effect
3602
+ unsigned char *text; // scratch for a text value in the server's encoding
3603
+ size_t text_cap;
3604
+ BulkField *fields; // the row being sent, one per bound column
3605
+ unsigned char *is_null; // and whether each is NULL
3606
+ } BulkState;
3607
+
3608
+ static void bulk_state_free(struct BulkState *state) {
3609
+ if (!state) {
3610
+ return;
2855
3611
  }
2856
- // Closing twice is a no-op: the second call finds no connection.
2857
- if (handle->conn) {
2858
- SybaseConnection *conn = handle->conn;
2859
- handle->conn = NULL;
2860
- close_connection(conn);
3612
+ for (int i = 0; i < state->num_columns; i++) {
3613
+ free(state->columns[i].name);
3614
+ free(state->columns[i].buf);
3615
+ }
3616
+ free(state->columns);
3617
+ free(state->bound);
3618
+ free(state->table);
3619
+ free(state->text);
3620
+ free(state->fields);
3621
+ free(state->is_null);
3622
+ free(state);
3623
+ }
3624
+
3625
+ /** The ASE usertype of `timestamp` columns, which the server fills itself. */
3626
+ #define USERTYPE_TIMESTAMP 80
3627
+
3628
+ /**
3629
+ * How values of a column are converted. Columns of any other type can stay out
3630
+ * of a copy (they then get NULL or their default) but cannot be loaded.
3631
+ */
3632
+ static void bulk_classify(BulkColumn *col) {
3633
+ col->kind = BULK_UNSUPPORTED;
3634
+ if (col->usertype == 34 || col->usertype == 35 || col->usertype == USERTYPE_TIMESTAMP) {
3635
+ return; // unichar / univarchar (UTF-16 on the wire), timestamp
3636
+ }
3637
+ switch (col->type) {
3638
+ case SYBINT1:
3639
+ col->kind = BULK_INT, col->min = 0, col->max = 255;
3640
+ break;
3641
+ case SYBINT2:
3642
+ col->kind = BULK_INT, col->min = INT16_MIN, col->max = INT16_MAX;
3643
+ break;
3644
+ case SYBINT4:
3645
+ col->kind = BULK_INT, col->min = INT32_MIN, col->max = INT32_MAX;
3646
+ break;
3647
+ case SYBINT8:
3648
+ col->kind = BULK_INT, col->min = INT64_MIN, col->max = INT64_MAX;
3649
+ break;
3650
+ case SYBBIT:
3651
+ col->kind = BULK_BIT;
3652
+ break;
3653
+ case SYBFLT8:
3654
+ case SYBREAL:
3655
+ col->kind = BULK_FLOAT;
3656
+ break;
3657
+ case SYBMONEY:
3658
+ col->kind = BULK_MONEY, col->min = INT64_MIN, col->max = INT64_MAX;
3659
+ break;
3660
+ case SYBMONEY4:
3661
+ col->kind = BULK_MONEY, col->min = INT32_MIN, col->max = INT32_MAX;
3662
+ break;
3663
+ case SYBNUMERIC:
3664
+ case SYBDECIMAL:
3665
+ if (col->precision >= 1 && col->precision <= 38 && col->scale <= col->precision) {
3666
+ col->kind = BULK_NUMERIC;
3667
+ }
3668
+ break;
3669
+ case SYBDATETIME:
3670
+ col->kind = BULK_DATETIME;
3671
+ break;
3672
+ case SYBDATETIME4:
3673
+ col->kind = BULK_SMALLDATETIME;
3674
+ break;
3675
+ case SYBCHAR:
3676
+ case SYBVARCHAR:
3677
+ col->kind = BULK_TEXT;
3678
+ break;
3679
+ case SYBBINARY:
3680
+ case SYBVARBINARY:
3681
+ col->kind = BULK_BINARY;
3682
+ break;
3683
+ default:
3684
+ break;
2861
3685
  }
3686
+ }
2862
3687
 
2863
- napi_value undefined;
2864
- napi_get_undefined(env, &undefined);
2865
- return undefined;
3688
+ /** Bytes of the bound value of each kind, and the host type it is bound as. */
3689
+ static void bulk_binding(const BulkColumn *col, int *size, int *host_type, int *varlen) {
3690
+ switch (col->kind) {
3691
+ case BULK_INT:
3692
+ *size = sizeof(DBBIGINT), *host_type = SYBINT8, *varlen = -1;
3693
+ return;
3694
+ case BULK_BIT:
3695
+ *size = sizeof(DBBIT), *host_type = SYBBIT, *varlen = -1;
3696
+ return;
3697
+ case BULK_FLOAT:
3698
+ *size = sizeof(DBFLT8), *host_type = SYBFLT8, *varlen = -1;
3699
+ return;
3700
+ case BULK_MONEY:
3701
+ *size = sizeof(DBMONEY), *host_type = SYBMONEY, *varlen = -1;
3702
+ return;
3703
+ case BULK_NUMERIC:
3704
+ *size = sizeof(DBNUMERIC), *host_type = SYBNUMERIC, *varlen = (int)sizeof(DBNUMERIC);
3705
+ return;
3706
+ case BULK_DATETIME:
3707
+ *size = sizeof(DBDATETIME), *host_type = SYBDATETIME, *varlen = -1;
3708
+ return;
3709
+ case BULK_SMALLDATETIME:
3710
+ *size = sizeof(DBDATETIME4), *host_type = SYBDATETIME4, *varlen = -1;
3711
+ return;
3712
+ case BULK_TEXT:
3713
+ *size = col->max_length > 0 ? col->max_length : 1, *host_type = SYBCHAR, *varlen = *size;
3714
+ return;
3715
+ default: // BULK_BINARY
3716
+ *size = col->max_length > 0 ? col->max_length : 1, *host_type = SYBBINARY, *varlen = *size;
3717
+ return;
3718
+ }
2866
3719
  }
2867
3720
 
2868
- // ---------------------------------------------------------------------------
2869
- // Check if connection is alive
2870
- // ---------------------------------------------------------------------------
3721
+ // --- Value parsing -----------------------------------------------------------
3722
+ //
3723
+ // Strict: a value either converts exactly or rejects its row with a reason.
3724
+ // Nothing is rounded except what the column type itself cannot hold — a
3725
+ // datetime keeps 1/300 s, a smalldatetime whole minutes — and that rounding is
3726
+ // the server's own, so a copied value equals the same literal inserted by SQL.
2871
3727
 
2872
- static napi_value fn_is_alive(napi_env env, napi_callback_info info) {
2873
- size_t argc = 1;
2874
- napi_value args[1];
2875
- napi_get_cb_info(env, info, &argc, args, NULL, NULL);
3728
+ /** Longest non-text value accepted, in characters (a numeric(38) with sign, point and zeros). */
3729
+ #define BULK_TOKEN_MAX 128
2876
3730
 
2877
- ConnHandle *handle = get_conn_handle(env, args[0]);
2878
- if (handle == NULL) {
2879
- return NULL; // TypeError pending
3731
+ /** The field as NUL-terminated ASCII in `out`; -1 if it is longer or not ASCII. */
3732
+ static int bulk_ascii(const BulkField *f, char *out) {
3733
+ if (f->length >= BULK_TOKEN_MAX) {
3734
+ return -1;
2880
3735
  }
2881
- SybaseConnection *conn = handle->conn;
2882
-
2883
- int alive = 0;
2884
- if (conn) {
2885
- uv_mutex_lock(&conn->lock);
2886
- if (conn->dbproc && !conn->dead && !conn->pending_close && !DBDEAD(conn->dbproc)) {
2887
- alive = 1;
3736
+ for (size_t i = 0; i < f->length; i++) {
3737
+ unsigned c = f->units ? f->units[i] : f->bytes[i];
3738
+ if (c == 0 || c >= 0x80) {
3739
+ return -1;
2888
3740
  }
2889
- uv_mutex_unlock(&conn->lock);
3741
+ out[i] = (char)c;
2890
3742
  }
2891
-
2892
- napi_value result;
2893
- napi_get_boolean(env, alive, &result);
2894
- return result;
3743
+ out[f->length] = '\0';
3744
+ return 0;
2895
3745
  }
2896
3746
 
2897
- // ---------------------------------------------------------------------------
2898
- // Server character set
2899
- // ---------------------------------------------------------------------------
3747
+ static int is_digit(char c) {
3748
+ return c >= '0' && c <= '9';
3749
+ }
2900
3750
 
2901
- /** The server's default character set name, or "" for a closed handle. */
2902
- static napi_value fn_server_charset(napi_env env, napi_callback_info info) {
3751
+ /** `[+-]?digits` in [min, max]. */
3752
+ static int parse_bulk_int(const char *s, int64_t min, int64_t max, int64_t *out) {
3753
+ int negative = *s == '-';
3754
+ if (*s == '-' || *s == '+') {
3755
+ s++;
3756
+ }
3757
+ if (!is_digit(*s)) {
3758
+ return -1;
3759
+ }
3760
+ uint64_t magnitude = 0;
3761
+ for (; is_digit(*s); s++) {
3762
+ unsigned d = (unsigned)(*s - '0');
3763
+ if (magnitude > (UINT64_MAX - d) / 10) {
3764
+ return -1;
3765
+ }
3766
+ magnitude = magnitude * 10 + d;
3767
+ }
3768
+ if (*s != '\0') {
3769
+ return -1;
3770
+ }
3771
+ if (negative) {
3772
+ if (magnitude > (uint64_t)INT64_MAX + 1) {
3773
+ return -1;
3774
+ }
3775
+ int64_t v = magnitude == (uint64_t)INT64_MAX + 1 ? INT64_MIN : -(int64_t)magnitude;
3776
+ if (v < min) {
3777
+ return -1;
3778
+ }
3779
+ *out = v;
3780
+ return 0;
3781
+ }
3782
+ if (magnitude > (uint64_t)max) {
3783
+ return -1;
3784
+ }
3785
+ *out = (int64_t)magnitude;
3786
+ return 0;
3787
+ }
3788
+
3789
+ /**
3790
+ * A decimal literal `[+-]?(digits[.digits?] | .digits)`, without leading zeros
3791
+ * in the integer part or trailing zeros in the fraction (they change nothing,
3792
+ * so "1.50000" fits a scale of 1). `digits` holds the integer digits, then the
3793
+ * fraction digits.
3794
+ */
3795
+ typedef struct {
3796
+ int negative;
3797
+ char digits[BULK_TOKEN_MAX];
3798
+ int int_len;
3799
+ int frac_len;
3800
+ } BulkDecimal;
3801
+
3802
+ static int parse_bulk_decimal(const char *s, BulkDecimal *d) {
3803
+ d->negative = *s == '-';
3804
+ if (*s == '-' || *s == '+') {
3805
+ s++;
3806
+ }
3807
+ const char *int_start = s;
3808
+ while (is_digit(*s)) {
3809
+ s++;
3810
+ }
3811
+ const char *int_end = s;
3812
+ const char *frac_start = s, *frac_end = s;
3813
+ if (*s == '.') {
3814
+ frac_start = ++s;
3815
+ while (is_digit(*s)) {
3816
+ s++;
3817
+ }
3818
+ frac_end = s;
3819
+ }
3820
+ if (*s != '\0' || (int_end == int_start && frac_end == frac_start)) {
3821
+ return -1;
3822
+ }
3823
+ while (int_start < int_end && *int_start == '0') {
3824
+ int_start++;
3825
+ }
3826
+ while (frac_end > frac_start && frac_end[-1] == '0') {
3827
+ frac_end--;
3828
+ }
3829
+ d->int_len = (int)(int_end - int_start);
3830
+ d->frac_len = (int)(frac_end - frac_start);
3831
+ memcpy(d->digits, int_start, (size_t)d->int_len);
3832
+ memcpy(d->digits + d->int_len, frac_start, (size_t)d->frac_len);
3833
+ if (d->int_len + d->frac_len == 0) {
3834
+ d->negative = 0; // -0
3835
+ }
3836
+ return 0;
3837
+ }
3838
+
3839
+ /** money / smallmoney: exactly, in ten-thousandths. */
3840
+ static int parse_bulk_money(const char *s, const BulkColumn *col, DBMONEY *out, const char **why) {
3841
+ BulkDecimal d;
3842
+ if (parse_bulk_decimal(s, &d) != 0) {
3843
+ *why = "is not a decimal number";
3844
+ return -1;
3845
+ }
3846
+ if (d.frac_len > 4) {
3847
+ *why = "has more than 4 decimal places (money keeps 4; nothing is rounded)";
3848
+ return -1;
3849
+ }
3850
+ uint64_t units = 0;
3851
+ int ok = d.int_len <= 15;
3852
+ for (int i = 0; ok && i < d.int_len + 4; i++) {
3853
+ units = units * 10 + (uint64_t)(i < d.int_len + d.frac_len ? d.digits[i] - '0' : 0);
3854
+ }
3855
+ uint64_t limit = d.negative ? (uint64_t)(-(col->min + 1)) + 1 : (uint64_t)col->max;
3856
+ if (!ok || units > limit) {
3857
+ *why = "is out of range for the column";
3858
+ return -1;
3859
+ }
3860
+ int64_t v = d.negative ? (int64_t)(0 - units) : (int64_t)units;
3861
+ out->mnyhigh = (DBINT)(int32_t)((uint64_t)v >> 32);
3862
+ out->mnylow = (DBUINT)(uint32_t)(uint64_t)v;
3863
+ return 0;
3864
+ }
3865
+
3866
+ /** numeric(p, s) / decimal(p, s): exactly, or not at all. */
3867
+ static int parse_bulk_numeric(const char *s, const BulkColumn *col, DBNUMERIC *out,
3868
+ const char **why) {
3869
+ BulkDecimal d;
3870
+ if (parse_bulk_decimal(s, &d) != 0) {
3871
+ *why = "is not a decimal number";
3872
+ return -1;
3873
+ }
3874
+ if (d.frac_len > col->scale) {
3875
+ *why = "has more decimal places than the column's scale (nothing is rounded)";
3876
+ return -1;
3877
+ }
3878
+ if (d.int_len > col->precision - col->scale) {
3879
+ *why = "is out of range for the column's precision";
3880
+ return -1;
3881
+ }
3882
+ // The magnitude, digits * 10^(scale - frac_len), as 32-bit limbs (most
3883
+ // significant first): 38 digits need 127 bits.
3884
+ uint32_t limbs[5] = {0};
3885
+ int total = d.int_len + col->scale;
3886
+ for (int i = 0; i < total; i++) {
3887
+ uint64_t carry = (uint64_t)(i < d.int_len + d.frac_len ? d.digits[i] - '0' : 0);
3888
+ for (int k = 4; k >= 0; k--) {
3889
+ uint64_t v = (uint64_t)limbs[k] * 10 + carry;
3890
+ limbs[k] = (uint32_t)v;
3891
+ carry = v >> 32;
3892
+ }
3893
+ }
3894
+ memset(out, 0, sizeof(*out));
3895
+ out->precision = (BYTE)col->precision;
3896
+ out->scale = (BYTE)col->scale;
3897
+ int magnitude_bytes = NUMERIC_BYTES[col->precision] - 1;
3898
+ for (int b = 0; b < magnitude_bytes; b++) {
3899
+ int from_low = magnitude_bytes - 1 - b;
3900
+ out->array[1 + b] = (BYTE)(limbs[4 - from_low / 4] >> (8 * (from_low % 4)));
3901
+ }
3902
+ out->array[0] = (BYTE)(d.negative ? 1 : 0);
3903
+ return 0;
3904
+ }
3905
+
3906
+ /** `n` digits from `s` as a number, or -1. */
3907
+ static int read_digits(const char *s, int n) {
3908
+ int v = 0;
3909
+ for (int i = 0; i < n; i++) {
3910
+ if (!is_digit(s[i])) {
3911
+ return -1;
3912
+ }
3913
+ v = v * 10 + (s[i] - '0');
3914
+ }
3915
+ return v;
3916
+ }
3917
+
3918
+ static int days_in_month(int year, int month) {
3919
+ static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3920
+ int leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
3921
+ return month == 2 && leap ? 29 : days[month - 1];
3922
+ }
3923
+
3924
+ /**
3925
+ * A wall clock `YYYY-MM-DD[( |T)HH:MM[:SS[.f{1,3}]]]`, as days since 1900-01-01
3926
+ * and milliseconds into the day. No zone, no shifting: it is stored as written.
3927
+ */
3928
+ static int parse_bulk_wall_clock(const char *s, int64_t *days, int64_t *ms) {
3929
+ size_t n = strlen(s);
3930
+ int year = read_digits(s, 4), month = -1, day = -1, hour = 0, minute = 0, second = 0, milli = 0;
3931
+ if (n < 10 || year < 0 || s[4] != '-' || s[7] != '-') {
3932
+ return -1;
3933
+ }
3934
+ month = read_digits(s + 5, 2);
3935
+ day = read_digits(s + 8, 2);
3936
+ const char *t = s + 10;
3937
+ if (*t != '\0') {
3938
+ if ((*t != ' ' && *t != 'T') || strlen(t) < 6 || t[3] != ':') {
3939
+ return -1;
3940
+ }
3941
+ hour = read_digits(t + 1, 2);
3942
+ minute = read_digits(t + 4, 2);
3943
+ t += 6;
3944
+ if (*t == ':') {
3945
+ second = read_digits(t + 1, 2);
3946
+ t += 3;
3947
+ if (second < 0 || t[-1] == '\0') {
3948
+ return -1;
3949
+ }
3950
+ if (*t == '.') {
3951
+ int digits = 0;
3952
+ milli = 0;
3953
+ for (t++; is_digit(*t) && digits < 3; t++, digits++) {
3954
+ milli = milli * 10 + (*t - '0');
3955
+ }
3956
+ if (digits == 0) {
3957
+ return -1;
3958
+ }
3959
+ for (; digits < 3; digits++) {
3960
+ milli *= 10;
3961
+ }
3962
+ }
3963
+ }
3964
+ if (*t != '\0') {
3965
+ return -1;
3966
+ }
3967
+ }
3968
+ // (second was checked as it was read.)
3969
+ if (month < 1 || month > 12 || day < 1 || day > days_in_month(year, month) || hour < 0 ||
3970
+ hour > 23 || minute < 0 || minute > 59 || second > 59) {
3971
+ return -1;
3972
+ }
3973
+ *days = days_from_civil(year, month, day) - days_from_civil(1900, 1, 1);
3974
+ *ms = (((int64_t)hour * 60 + minute) * 60 + second) * 1000 + milli;
3975
+ return 0;
3976
+ }
3977
+
3978
+ #define DATETIME_MIN_DAYS (-53690) // 1753-01-01
3979
+ #define DATETIME_MAX_DAYS 2958463 // 9999-12-31
3980
+
3981
+ /**
3982
+ * datetime: milliseconds to 1/300 s ticks as ASE converts a literal — to the
3983
+ * nearest tick, but never past the second's last one: 10:00:00.999 is stored
3984
+ * as 10:00:00.996, not 10:00:01.000 (checked against ASE 15.7, every ms).
3985
+ */
3986
+ static int parse_bulk_datetime(const char *s, DBDATETIME *out, const char **why) {
3987
+ int64_t days, ms;
3988
+ if (parse_bulk_wall_clock(s, &days, &ms) != 0) {
3989
+ *why = "is not a date and time (YYYY-MM-DD HH:MM:SS.mmm)";
3990
+ return -1;
3991
+ }
3992
+ int64_t ticks_in_second = (ms % 1000 * 3 + 5) / 10;
3993
+ int64_t ticks = ms / 1000 * 300 + (ticks_in_second > 299 ? 299 : ticks_in_second);
3994
+ if (days < DATETIME_MIN_DAYS || days > DATETIME_MAX_DAYS) {
3995
+ *why = "is out of range for datetime (1753-01-01 to 9999-12-31)";
3996
+ return -1;
3997
+ }
3998
+ out->dtdays = (DBINT)days;
3999
+ out->dttime = (DBINT)ticks;
4000
+ return 0;
4001
+ }
4002
+
4003
+ #define SMALLDATETIME_MAX_DAYS 65535 // 2079-06-06
4004
+
4005
+ /** smalldatetime: whole minutes, from 30.000 s rounding up, as ASE converts a literal. */
4006
+ static int parse_bulk_smalldatetime(const char *s, DBDATETIME4 *out, const char **why) {
4007
+ int64_t days, ms;
4008
+ if (parse_bulk_wall_clock(s, &days, &ms) != 0) {
4009
+ *why = "is not a date and time (YYYY-MM-DD HH:MM:SS)";
4010
+ return -1;
4011
+ }
4012
+ int64_t minutes = ms / 60000 + (ms % 60000 >= 30000 ? 1 : 0);
4013
+ // The written date must be in range: ASE refuses 1899-12-31 23:59:59 rather
4014
+ // than round it up to 1900-01-01 (and 2079-06-06 23:59:30, which rounds past
4015
+ // the end). Checked against ASE 15.7.
4016
+ if (days < 0) {
4017
+ *why = "is out of range for smalldatetime (1900-01-01 to 2079-06-06)";
4018
+ return -1;
4019
+ }
4020
+ if (minutes >= 1440) {
4021
+ days++, minutes -= 1440;
4022
+ }
4023
+ if (days < 0 || days > SMALLDATETIME_MAX_DAYS) {
4024
+ *why = "is out of range for smalldatetime (1900-01-01 to 2079-06-06)";
4025
+ return -1;
4026
+ }
4027
+ out->days = (DBUSMALLINT)days;
4028
+ out->minutes = (DBUSMALLINT)minutes;
4029
+ return 0;
4030
+ }
4031
+
4032
+ /** `[+-]?(digits[.digits?] | .digits)([eE][+-]?digits)?`, finite and in range. */
4033
+ static int parse_bulk_float(const char *s, int real, double *out, const char **why) {
4034
+ const char *p = s;
4035
+ int digits = 0;
4036
+ int nonzero = 0; // a mantissa digit other than 0: the value is not zero
4037
+ if (*p == '-' || *p == '+') {
4038
+ p++;
4039
+ }
4040
+ for (; is_digit(*p); p++) {
4041
+ digits++;
4042
+ nonzero |= *p != '0';
4043
+ }
4044
+ if (*p == '.') {
4045
+ for (p++; is_digit(*p); p++) {
4046
+ digits++;
4047
+ nonzero |= *p != '0';
4048
+ }
4049
+ }
4050
+ if (digits > 0 && (*p == 'e' || *p == 'E')) {
4051
+ p++;
4052
+ if (*p == '-' || *p == '+') {
4053
+ p++;
4054
+ }
4055
+ if (!is_digit(*p)) {
4056
+ digits = 0;
4057
+ }
4058
+ while (is_digit(*p)) {
4059
+ p++;
4060
+ }
4061
+ }
4062
+ char *end = NULL;
4063
+ double v = digits > 0 && *p == '\0' ? strtod(s, &end) : 0;
4064
+ // strtod reads the process locale's decimal point: under one with a comma it
4065
+ // stops at the '.', and the value is refused rather than misread.
4066
+ if (digits == 0 || *p != '\0' || end != p) {
4067
+ *why = "is not a number";
4068
+ return -1;
4069
+ }
4070
+ if (!isfinite(v) || (real && fabs(v) > FLT_MAX)) {
4071
+ *why = "is out of range for the column";
4072
+ return -1;
4073
+ }
4074
+ // Too small to hold: it would be stored as 0. (A subnormal that keeps a value
4075
+ // is fine, though strtod may flag it ERANGE: what counts is the result.)
4076
+ if (nonzero && (v == 0 || (real && (float)v == 0))) {
4077
+ *why = "is too small for the column: it would be stored as 0";
4078
+ return -1;
4079
+ }
4080
+ *out = v;
4081
+ return 0;
4082
+ }
4083
+
4084
+ static int hex_value(unsigned c) {
4085
+ if (c >= '0' && c <= '9') {
4086
+ return (int)(c - '0');
4087
+ }
4088
+ if (c >= 'a' && c <= 'f') {
4089
+ return (int)(c - 'a' + 10);
4090
+ }
4091
+ if (c >= 'A' && c <= 'F') {
4092
+ return (int)(c - 'A' + 10);
4093
+ }
4094
+ return -1;
4095
+ }
4096
+
4097
+ /**
4098
+ * A value that cannot be converted for a reason that is not the value's — out
4099
+ * of memory, db-lib refusing a binding. Unlike a refused value (-1), which
4100
+ * maxErrors may skip, this stops the copy: a valid row must not be left out.
4101
+ */
4102
+ #define BULK_FATAL (-2)
4103
+
4104
+ /** Make room for `n` bytes of text scratch. */
4105
+ static int bulk_reserve_text(BulkState *st, size_t n) {
4106
+ if (st->text_cap >= n) {
4107
+ return 0;
4108
+ }
4109
+ unsigned char *grown = (unsigned char *)realloc(st->text, n);
4110
+ if (!grown) {
4111
+ return -1;
4112
+ }
4113
+ st->text = grown;
4114
+ st->text_cap = n;
4115
+ return 0;
4116
+ }
4117
+
4118
+ /** The next code point of UTF-8 `s` (length `n`) at `*i`, or -1 if it is not valid UTF-8. */
4119
+ static int32_t utf8_next(const unsigned char *s, size_t n, size_t *i) {
4120
+ unsigned char b = s[*i];
4121
+ int needed;
4122
+ int32_t cp;
4123
+ unsigned char lower = 0x80, upper = 0xBF;
4124
+ if (b <= 0x7F) {
4125
+ (*i)++;
4126
+ return b;
4127
+ } else if (b >= 0xC2 && b <= 0xDF) {
4128
+ needed = 1, cp = b & 0x1F;
4129
+ } else if (b >= 0xE0 && b <= 0xEF) {
4130
+ needed = 2, cp = b & 0x0F;
4131
+ lower = b == 0xE0 ? 0xA0 : 0x80;
4132
+ upper = b == 0xED ? 0x9F : 0xBF;
4133
+ } else if (b >= 0xF0 && b <= 0xF4) {
4134
+ needed = 3, cp = b & 0x07;
4135
+ lower = b == 0xF0 ? 0x90 : 0x80;
4136
+ upper = b == 0xF4 ? 0x8F : 0xBF;
4137
+ } else {
4138
+ return -1;
4139
+ }
4140
+ if (n - *i <= (size_t)needed) {
4141
+ return -1;
4142
+ }
4143
+ for (int k = 1; k <= needed; k++) {
4144
+ unsigned char c = s[*i + (size_t)k];
4145
+ if (c < lower || c > upper) {
4146
+ return -1;
4147
+ }
4148
+ lower = 0x80, upper = 0xBF;
4149
+ cp = (cp << 6) | (c & 0x3F);
4150
+ }
4151
+ *i += (size_t)needed + 1;
4152
+ return cp;
4153
+ }
4154
+
4155
+ /**
4156
+ * A text field in the server's encoding, in `st->text`: Latin-1 for an iso_1
4157
+ * server, otherwise UTF-8 (as SQL text is sent; see sql_bytes). Returns the
4158
+ * length, or -1 with `why` set.
4159
+ */
4160
+ static long bulk_encode_text(BulkState *st, const BulkField *f, int latin1, char *why,
4161
+ size_t why_size) {
4162
+ if (bulk_reserve_text(st, f->length * 3 + 1) != 0) {
4163
+ snprintf(why, why_size, "could not be converted: out of memory");
4164
+ return BULK_FATAL;
4165
+ }
4166
+ unsigned char *out = st->text;
4167
+ size_t len = 0;
4168
+ size_t i = 0;
4169
+ long chars = 0;
4170
+ while (i < f->length) {
4171
+ int32_t cp;
4172
+ chars++;
4173
+ if (f->units) {
4174
+ uint32_t u = f->units[i++];
4175
+ if (u >= 0xD800 && u <= 0xDBFF && i < f->length && f->units[i] >= 0xDC00 &&
4176
+ f->units[i] <= 0xDFFF) {
4177
+ cp = (int32_t)(0x10000 + ((u - 0xD800) << 10) + (f->units[i++] - 0xDC00));
4178
+ } else if (u >= 0xD800 && u <= 0xDFFF) {
4179
+ snprintf(why, why_size, "has an unpaired surrogate at character %ld", chars);
4180
+ return -1;
4181
+ } else {
4182
+ cp = (int32_t)u;
4183
+ }
4184
+ } else {
4185
+ cp = utf8_next(f->bytes, f->length, &i);
4186
+ if (cp < 0) {
4187
+ snprintf(why, why_size, "is not valid UTF-8 (at character %ld)", chars);
4188
+ return -1;
4189
+ }
4190
+ }
4191
+ // db-lib takes a lone NUL for an empty string (stored as a space): NUL in
4192
+ // text is refused, as in SQL, rather than stored as something else.
4193
+ if (cp == 0) {
4194
+ snprintf(why, why_size, "has a NUL character at character %ld", chars);
4195
+ return -1;
4196
+ }
4197
+ if (latin1) {
4198
+ if (cp > 0xFF) {
4199
+ snprintf(why, why_size,
4200
+ "has U+%04X at character %ld, which the server's character set (iso_1) "
4201
+ "cannot hold",
4202
+ (unsigned)cp, chars);
4203
+ return -1;
4204
+ }
4205
+ out[len++] = (unsigned char)cp;
4206
+ } else if (cp < 0x80) {
4207
+ out[len++] = (unsigned char)cp;
4208
+ } else if (cp < 0x800) {
4209
+ out[len++] = (unsigned char)(0xC0 | (cp >> 6));
4210
+ out[len++] = (unsigned char)(0x80 | (cp & 0x3F));
4211
+ } else if (cp < 0x10000) {
4212
+ out[len++] = (unsigned char)(0xE0 | (cp >> 12));
4213
+ out[len++] = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
4214
+ out[len++] = (unsigned char)(0x80 | (cp & 0x3F));
4215
+ } else {
4216
+ // Never past a 4-byte sequence: f->length * 3 covers it, since a code
4217
+ // point past U+FFFF takes two units or four UTF-8 bytes of input.
4218
+ out[len++] = (unsigned char)(0xF0 | (cp >> 18));
4219
+ out[len++] = (unsigned char)(0x80 | ((cp >> 12) & 0x3F));
4220
+ out[len++] = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
4221
+ out[len++] = (unsigned char)(0x80 | (cp & 0x3F));
4222
+ }
4223
+ }
4224
+ return (long)len;
4225
+ }
4226
+
4227
+ /**
4228
+ * Put one non-NULL value into the column's bound buffer and set its length.
4229
+ * Returns -1 with `why` set if the value does not convert exactly.
4230
+ */
4231
+ static int bulk_set_value(SybaseConnection *conn, BulkState *st, const BulkColumn *col,
4232
+ int table_column, const BulkField *f, char *why, size_t why_size) {
4233
+ char token[BULK_TOKEN_MAX];
4234
+ const char *reason = NULL;
4235
+ DBINT length = -1;
4236
+
4237
+ if (col->kind == BULK_TEXT) {
4238
+ // Trailing blanks are not data (see below): left out before anything is
4239
+ // sized by the field. What is left cannot fit if even its shortest encoding
4240
+ // is too long — a byte per UTF-16 unit, or, from UTF-8 into Latin-1, a byte
4241
+ // per two (every character Latin-1 holds is at most two bytes of UTF-8).
4242
+ BulkField trimmed = *f;
4243
+ while (trimmed.length > 0 &&
4244
+ (f->units ? f->units[trimmed.length - 1] : f->bytes[trimmed.length - 1]) == ' ') {
4245
+ trimmed.length--;
4246
+ }
4247
+ size_t per_byte = !f->units && conn->latin1 ? 2 : 1;
4248
+ if (trimmed.length > (size_t)col->max_length * per_byte) {
4249
+ snprintf(why, why_size,
4250
+ "is at least %zu bytes in the server's character set; the column holds %d",
4251
+ (trimmed.length + per_byte - 1) / per_byte, col->max_length);
4252
+ return -1;
4253
+ }
4254
+ long n = bulk_encode_text(st, &trimmed, conn->latin1, why, why_size);
4255
+ if (n < 0) {
4256
+ return (int)n;
4257
+ }
4258
+ // ASE keeps no trailing blanks in varchar, and pads char with them, so they
4259
+ // are not data: dropped before the length is checked, as db-lib would.
4260
+ while (n > 0 && st->text[n - 1] == ' ') {
4261
+ n--;
4262
+ }
4263
+ if (n > col->max_length) {
4264
+ snprintf(why, why_size, "is %ld bytes in the server's character set; the column holds %d",
4265
+ n, col->max_length);
4266
+ return -1;
4267
+ }
4268
+ if (n == 0) {
4269
+ // Zero length would be NULL. ASE stores an empty string as one blank.
4270
+ col->buf[0] = ' ';
4271
+ n = 1;
4272
+ } else {
4273
+ memcpy(col->buf, st->text, (size_t)n);
4274
+ }
4275
+ return bcp_collen(conn->dbproc, (DBINT)n, table_column) == FAIL ? BULK_FATAL : 0;
4276
+ }
4277
+
4278
+ if (col->kind == BULK_BINARY) {
4279
+ size_t start = 0;
4280
+ unsigned c0 = f->length > 0 ? (f->units ? f->units[0] : f->bytes[0]) : 0;
4281
+ unsigned c1 = f->length > 1 ? (f->units ? f->units[1] : f->bytes[1]) : 0;
4282
+ if (c0 == '0' && (c1 == 'x' || c1 == 'X')) {
4283
+ start = 2;
4284
+ }
4285
+ size_t digits = f->length - start;
4286
+ if (digits == 0 || digits % 2 != 0) {
4287
+ snprintf(why, why_size, "is not hexadecimal bytes (an even, non-zero number of hex digits)");
4288
+ return -1;
4289
+ }
4290
+ if (digits / 2 > (size_t)col->max_length) {
4291
+ snprintf(why, why_size, "is %zu bytes; the column holds %d", digits / 2, col->max_length);
4292
+ return -1;
4293
+ }
4294
+ for (size_t i = 0; i < digits / 2; i++) {
4295
+ size_t at = start + i * 2;
4296
+ int hi = hex_value(f->units ? f->units[at] : f->bytes[at]);
4297
+ int lo = hex_value(f->units ? f->units[at + 1] : f->bytes[at + 1]);
4298
+ if (hi < 0 || lo < 0) {
4299
+ snprintf(why, why_size, "is not hexadecimal bytes");
4300
+ return -1;
4301
+ }
4302
+ col->buf[i] = (BYTE)(hi << 4 | lo);
4303
+ }
4304
+ return bcp_collen(conn->dbproc, (DBINT)(digits / 2), table_column) == FAIL ? BULK_FATAL : 0;
4305
+ }
4306
+
4307
+ if (bulk_ascii(f, token) != 0) {
4308
+ snprintf(why, why_size, "is not a valid %s value", syb_type_name(col->type));
4309
+ return -1;
4310
+ }
4311
+ switch (col->kind) {
4312
+ case BULK_INT: {
4313
+ int64_t v;
4314
+ if (parse_bulk_int(token, col->min, col->max, &v) != 0) {
4315
+ reason = "is not an integer in the column's range";
4316
+ break;
4317
+ }
4318
+ DBBIGINT big = v;
4319
+ memcpy(col->buf, &big, sizeof(big));
4320
+ break;
4321
+ }
4322
+ case BULK_BIT:
4323
+ if (strcmp(token, "0") != 0 && strcmp(token, "1") != 0) {
4324
+ reason = "is not a bit (0 or 1)";
4325
+ break;
4326
+ }
4327
+ col->buf[0] = (BYTE)(token[0] == '1');
4328
+ break;
4329
+ case BULK_FLOAT: {
4330
+ double v;
4331
+ if (parse_bulk_float(token, col->type == SYBREAL, &v, &reason) == 0) {
4332
+ DBFLT8 d = v;
4333
+ memcpy(col->buf, &d, sizeof(d));
4334
+ }
4335
+ break;
4336
+ }
4337
+ case BULK_MONEY: {
4338
+ DBMONEY m;
4339
+ if (parse_bulk_money(token, col, &m, &reason) == 0) {
4340
+ memcpy(col->buf, &m, sizeof(m));
4341
+ }
4342
+ break;
4343
+ }
4344
+ case BULK_NUMERIC: {
4345
+ DBNUMERIC num;
4346
+ if (parse_bulk_numeric(token, col, &num, &reason) == 0) {
4347
+ memcpy(col->buf, &num, sizeof(num));
4348
+ length = (DBINT)sizeof(num);
4349
+ }
4350
+ break;
4351
+ }
4352
+ case BULK_DATETIME: {
4353
+ DBDATETIME dt;
4354
+ if (parse_bulk_datetime(token, &dt, &reason) == 0) {
4355
+ memcpy(col->buf, &dt, sizeof(dt));
4356
+ }
4357
+ break;
4358
+ }
4359
+ case BULK_SMALLDATETIME: {
4360
+ DBDATETIME4 dt;
4361
+ if (parse_bulk_smalldatetime(token, &dt, &reason) == 0) {
4362
+ memcpy(col->buf, &dt, sizeof(dt));
4363
+ }
4364
+ break;
4365
+ }
4366
+ default:
4367
+ reason = "cannot be copied: the column type is not supported";
4368
+ break;
4369
+ }
4370
+ if (reason) {
4371
+ snprintf(why, why_size, "%s", reason);
4372
+ return -1;
4373
+ }
4374
+ return bcp_collen(conn->dbproc, length, table_column) == FAIL ? BULK_FATAL : 0;
4375
+ }
4376
+
4377
+ // --- Async work --------------------------------------------------------------
4378
+
4379
+ typedef enum { BULK_OP_BEGIN, BULK_OP_SEND } BulkOp;
4380
+
4381
+ /** What a bulkSend call ends with (see the `commit` option). */
4382
+ typedef enum { BULK_COMMIT_NONE = 0, BULK_COMMIT_BATCH = 1, BULK_COMMIT_DONE = 2 } BulkCommit;
4383
+
4384
+ /** Whether the rows of the open batch are committed after a failure. */
4385
+ typedef enum {
4386
+ BULK_OUTCOME_NONE, // no commit was attempted: the open batch is not committed
4387
+ BULK_OUTCOME_FAILED, // the server refused the batch: not committed
4388
+ BULK_OUTCOME_UNKNOWN, // the answer to the commit was lost: it may or may not have committed
4389
+ } BulkOutcome;
4390
+
4391
+ /** Most rejected rows reported with their reason per call; more are only counted. */
4392
+ #define BULK_MAX_REJECT_DETAILS 1000
4393
+
4394
+ /** A bound column as bulkBegin describes it to JS. */
4395
+ typedef struct {
4396
+ char *name;
4397
+ const char *type;
4398
+ int nullable, length, precision, scale;
4399
+ } BulkOutColumn;
4400
+
4401
+ typedef struct {
4402
+ int64_t row;
4403
+ char *column; // whole, as in error_column; NULL for the row as a whole
4404
+ char message[288]; // "value " + a reason (up to 256)
4405
+ } BulkReject;
4406
+
4407
+ typedef struct {
4408
+ napi_async_work work;
4409
+ napi_deferred deferred;
4410
+ SybaseConnection *conn;
4411
+ int holds_conn;
4412
+ HandleClaim claim;
4413
+ BulkOp op;
4414
+ int hard_timeout_ms;
4415
+ Watchdog wd;
4416
+ // bulkBegin
4417
+ char *table;
4418
+ char **names; // NULL: the default columns
4419
+ int num_names;
4420
+ int keep_identity;
4421
+ int ignore_defaults;
4422
+ int allow_constraint_bypass;
4423
+ int timeout_seconds;
4424
+ BulkState *state; // being built; handed to the connection on success
4425
+ // Description of the bound columns, for JS (copied: JS must not reach into conn).
4426
+ BulkOutColumn *out;
4427
+ int out_count;
4428
+ // bulkSend
4429
+ unsigned char *data; // TSV, UTF-8
4430
+ size_t data_len;
4431
+ uint16_t *units; // value rows: every field back to back, UTF-16
4432
+ size_t num_units;
4433
+ uint32_t *lens; // value rows: units per field, BULK_NULL for NULL
4434
+ size_t num_lens;
4435
+ int64_t rows;
4436
+ int64_t first_row; // 1-based number of the chunk's first row in the whole copy
4437
+ int64_t max_rejects; // rows that may still be rejected before the copy fails
4438
+ int commit;
4439
+ int64_t rows_sent;
4440
+ BulkReject *rejects;
4441
+ int num_rejects;
4442
+ int64_t rejected_count;
4443
+ int64_t committed; // rows the server reports committed by this call; -1 if none
4444
+ int outcome;
4445
+ int finished; // the copy has ended (BULK_COMMIT_DONE succeeded)
4446
+ int discard; // finished, but the session could not be restored
4447
+ // Result
4448
+ char error[ERR_BUF_SIZE];
4449
+ int success;
4450
+ int reusable; // failed, but the connection was left as it was
4451
+ int64_t error_row; // 0 when the failure is not about a row
4452
+ int value_refused; // failed on a refused value (past maxRejects), not the server or connection
4453
+ char *error_column; // the column a failed row failed on, if any
4454
+ int conn_dead;
4455
+ int conn_lost; // the connection itself broke (not just marked unusable)
4456
+ int conn_timed_out;
4457
+ } BulkWork;
4458
+
4459
+ /** Field length marking a NULL in value rows. */
4460
+ #define BULK_NULL 0xFFFFFFFFu
4461
+
4462
+ static void bulk_work_free(BulkWork *w) {
4463
+ free(w->table);
4464
+ for (int i = 0; i < w->num_names; i++) {
4465
+ free(w->names[i]);
4466
+ }
4467
+ free(w->names);
4468
+ bulk_state_free(w->state);
4469
+ for (int i = 0; i < w->out_count; i++) {
4470
+ free(w->out[i].name);
4471
+ }
4472
+ free(w->out);
4473
+ free(w->data);
4474
+ free(w->units);
4475
+ free(w->lens);
4476
+ for (int i = 0; i < w->num_rejects; i++) {
4477
+ free(w->rejects[i].column);
4478
+ }
4479
+ free(w->rejects);
4480
+ free(w->error_column);
4481
+ free(w);
4482
+ }
4483
+
4484
+ /** Run a statement whose results are not needed; -1 (error in conn->last_error) on failure. */
4485
+ static int run_simple(SybaseConnection *conn, const char *sql) {
4486
+ if (dbcmd(conn->dbproc, sql) == FAIL || dbsqlexec(conn->dbproc) == FAIL) {
4487
+ if (!DBDEAD(conn->dbproc)) {
4488
+ dbcancel(conn->dbproc);
4489
+ }
4490
+ return -1;
4491
+ }
4492
+ RETCODE rc;
4493
+ while ((rc = dbresults(conn->dbproc)) != NO_MORE_RESULTS) {
4494
+ if (rc == FAIL || discard_rows(conn->dbproc) == FAIL) {
4495
+ if (!DBDEAD(conn->dbproc)) {
4496
+ dbcancel(conn->dbproc);
4497
+ }
4498
+ return -1;
4499
+ }
4500
+ }
4501
+ return conn->has_error ? -1 : 0;
4502
+ }
4503
+
4504
+ /** The connection's error text, or `fallback`, as the work's error. */
4505
+ static void bulk_fail(BulkWork *w, const char *fallback) {
4506
+ if (w->conn->has_error) {
4507
+ snprintf(w->error, sizeof(w->error), "%s", w->conn->last_error);
4508
+ } else {
4509
+ snprintf(w->error, sizeof(w->error), "%s", fallback);
4510
+ }
4511
+ w->success = 0;
4512
+ }
4513
+
4514
+ /**
4515
+ * The catalog's database for `table` as SQL: the session's own temporary
4516
+ * database for a #temp table (not always `tempdb`), the named one for
4517
+ * `db.owner.table` / `db..table`, else the current one.
4518
+ */
4519
+ static void catalog_db(const char *table, char *out, size_t size) {
4520
+ const char *dot = strchr(table, '.');
4521
+ if (table[0] == '#') {
4522
+ snprintf(out, size, "db_name(@@tempdbid)");
4523
+ } else if (dot && strchr(dot + 1, '.')) {
4524
+ snprintf(out, size, "'%.*s'", (int)(dot - table), table);
4525
+ } else {
4526
+ snprintf(out, size, "db_name()");
4527
+ }
4528
+ }
4529
+
4530
+ /**
4531
+ * Read `@@trancount`, the table's columns, and what of the table a bulk copy
4532
+ * does not apply — insert triggers, CHECK and foreign key constraints, rules
4533
+ * — in one round trip. A copy is refused inside a transaction: each batch it
4534
+ * commits would then be part of that transaction, and "committed" would not
4535
+ * mean committed. And it is refused into a table with any of those, unless
4536
+ * the caller allows it: rows that break them would be stored.
4537
+ */
4538
+ static int bulk_read_table(BulkWork *w, BulkState *st) {
4539
+ SybaseConnection *conn = w->conn;
4540
+ char db[128];
4541
+ catalog_db(w->table, db, sizeof(db));
4542
+ size_t sql_size = strlen(w->table) * 2 + strlen(db) + 1024;
4543
+ char *sql = (char *)malloc(sql_size);
4544
+ if (!sql) {
4545
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4546
+ return -1;
4547
+ }
4548
+ // The table name is plain identifiers (is_table_name), so safe inside the
4549
+ // quoted dynamic SQL; the database's name need not be, so it is bracketed.
4550
+ // sysconstraints also lists computed columns (status 256): only referential
4551
+ // (64) and CHECK (128) constraints count. syscolumns.domain is the rule or
4552
+ // CHECK constraint in effect on a column (one bound to its user-defined type
4553
+ // included): only whether there are any matters here. In chained mode every
4554
+ // select opens a transaction (an IF does not): nothing is read then, and the
4555
+ // batch returns no result at all — the copy is refused with the connection
4556
+ // as it was.
4557
+ snprintf(sql, sql_size,
4558
+ "if @@tranchained = 1 or @@trancount > 0 begin\n"
4559
+ "if @@tranchained = 0 select @@trancount\n"
4560
+ "end else begin\n"
4561
+ "select @@trancount\n"
4562
+ "select * from %s where 1 = 0\n"
4563
+ "declare @db varchar(255) select @db = str_replace(%s, ']', ']]')\n"
4564
+ "exec('select o.instrig,"
4565
+ " (select count(*) from [' + @db + ']..sysconstraints c"
4566
+ " where c.tableid = o.id and c.status & 192 <> 0),"
4567
+ " (select count(*) from [' + @db + ']..syscolumns c where c.id = o.id and c.domain <> 0)"
4568
+ " from [' + @db + ']..sysobjects o where o.id = object_id(''%s'')')\n"
4569
+ "end",
4570
+ w->table, db, w->table);
4571
+ int ok = dbcmd(conn->dbproc, sql) != FAIL && dbsqlexec(conn->dbproc) != FAIL;
4572
+ free(sql);
4573
+ int set = 0;
4574
+ int trancount = -1;
4575
+ DBINT unapplied[3] = {-1, -1, -1}; // insert trigger, constraints, columns with a rule or CHECK
4576
+ RETCODE rc;
4577
+ while (ok && (rc = dbresults(conn->dbproc)) != NO_MORE_RESULTS) {
4578
+ if (rc == FAIL) {
4579
+ ok = 0;
4580
+ break;
4581
+ }
4582
+ int ncols = dbnumcols(conn->dbproc);
4583
+ if (ncols <= 0) {
4584
+ ok = discard_rows(conn->dbproc) != FAIL;
4585
+ continue;
4586
+ }
4587
+ if (++set == 1) {
4588
+ RETCODE row;
4589
+ while ((row = dbnextrow(conn->dbproc)) == REG_ROW) {
4590
+ BYTE *data = dbdata(conn->dbproc, 1);
4591
+ if (data && dbdatlen(conn->dbproc, 1) == (int)sizeof(DBINT)) {
4592
+ DBINT v;
4593
+ memcpy(&v, data, sizeof(v));
4594
+ trancount = v;
4595
+ }
4596
+ }
4597
+ ok = row != FAIL;
4598
+ continue;
4599
+ }
4600
+ if (set == 3) {
4601
+ RETCODE row;
4602
+ while ((row = dbnextrow(conn->dbproc)) == REG_ROW) {
4603
+ for (int i = 0; i < 3 && i < ncols; i++) {
4604
+ BYTE *data = dbdata(conn->dbproc, i + 1);
4605
+ if (data && dbdatlen(conn->dbproc, i + 1) == (int)sizeof(DBINT)) {
4606
+ memcpy(&unapplied[i], data, sizeof(DBINT));
4607
+ }
4608
+ }
4609
+ }
4610
+ ok = row != FAIL;
4611
+ continue;
4612
+ }
4613
+ if (st->columns == NULL) {
4614
+ st->columns = (BulkColumn *)calloc((size_t)ncols, sizeof(BulkColumn));
4615
+ if (!st->columns) {
4616
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4617
+ dbcancel(conn->dbproc);
4618
+ return -1;
4619
+ }
4620
+ st->num_columns = ncols;
4621
+ for (int i = 0; i < ncols; i++) {
4622
+ BulkColumn *col = &st->columns[i];
4623
+ DBCOL info;
4624
+ memset(&info, 0, sizeof(info));
4625
+ info.SizeOfStruct = sizeof(info);
4626
+ const char *name = dbcolname(conn->dbproc, i + 1);
4627
+ col->name = dup_text(name ? name : "", conn->latin1);
4628
+ col->type = column_type(conn->dbproc, i + 1);
4629
+ col->usertype = dbcolutype(conn->dbproc, i + 1);
4630
+ col->max_length = dbcollen(conn->dbproc, i + 1);
4631
+ // Precision, scale, nullability and identity decide how values are
4632
+ // converted and which columns may be left out: without them the copy
4633
+ // does not start, rather than take every column for a NOT NULL one.
4634
+ if (dbcolinfo(conn->dbproc, CI_REGULAR, i + 1, 0, &info) != SUCCEED) {
4635
+ snprintf(w->error, sizeof(w->error), "Could not read column %d of %s", i + 1, w->table);
4636
+ dbcancel(conn->dbproc);
4637
+ return -1;
4638
+ }
4639
+ col->precision = info.Precision;
4640
+ col->scale = info.Scale;
4641
+ col->nullable = info.Null != 0;
4642
+ col->identity = info.Identity != 0;
4643
+ if (!col->name) {
4644
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4645
+ dbcancel(conn->dbproc);
4646
+ return -1;
4647
+ }
4648
+ bulk_classify(col);
4649
+ }
4650
+ }
4651
+ ok = discard_rows(conn->dbproc) != FAIL;
4652
+ }
4653
+ if (!ok || conn->has_error) {
4654
+ bulk_fail(w, "Could not read the table's columns");
4655
+ if (!DBDEAD(conn->dbproc)) {
4656
+ dbcancel(conn->dbproc);
4657
+ }
4658
+ return -1;
4659
+ }
4660
+ // No result at all: chained mode (see above).
4661
+ if (set == 0) {
4662
+ snprintf(w->error, sizeof(w->error),
4663
+ "A bulk copy cannot run in chained transaction mode (set chained on): each statement "
4664
+ "opens a transaction, and the batches it commits must be committed for good. Run it "
4665
+ "with chained mode off");
4666
+ return -1;
4667
+ }
4668
+ if (trancount > 0) {
4669
+ snprintf(w->error, sizeof(w->error),
4670
+ "A bulk copy cannot run inside a transaction (@@trancount is %d): each batch it "
4671
+ "commits must be committed for good",
4672
+ trancount);
4673
+ return -1;
4674
+ }
4675
+ if (st->columns == NULL || trancount < 0) {
4676
+ bulk_fail(w, "Could not read the table's columns");
4677
+ return -1;
4678
+ }
4679
+ if (unapplied[0] < 0 || unapplied[1] < 0 || unapplied[2] < 0) {
4680
+ snprintf(w->error, sizeof(w->error), "Could not read whether %s has constraints or triggers",
4681
+ w->table);
4682
+ return -1;
4683
+ }
4684
+ if (!w->allow_constraint_bypass && (unapplied[0] || unapplied[1] || unapplied[2])) {
4685
+ snprintf(w->error, sizeof(w->error),
4686
+ "%s has %s, which a bulk copy does not apply (nor does the bcp utility): rows that "
4687
+ "break them would be stored. Load it through a staging table and an insert ... "
4688
+ "select, or pass allowConstraintBypass: true to copy anyway",
4689
+ w->table,
4690
+ unapplied[0] ? "an insert trigger"
4691
+ : unapplied[1] ? "CHECK or foreign key constraints"
4692
+ : "a rule or CHECK constraint on a column");
4693
+ return -1;
4694
+ }
4695
+ return 0;
4696
+ }
4697
+
4698
+ /** Choose the bound columns: the named ones, or every loadable one in table order. */
4699
+ static int bulk_bind_columns(BulkWork *w, BulkState *st) {
4700
+ int capacity = w->names ? w->num_names : st->num_columns;
4701
+ st->bound = (int *)calloc((size_t)(capacity > 0 ? capacity : 1), sizeof(int));
4702
+ if (!st->bound) {
4703
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4704
+ return -1;
4705
+ }
4706
+ if (w->names) {
4707
+ for (int n = 0; n < w->num_names; n++) {
4708
+ int found = -1;
4709
+ for (int i = 0; i < st->num_columns; i++) {
4710
+ if (strcmp(st->columns[i].name, w->names[n]) == 0) {
4711
+ found = i;
4712
+ break;
4713
+ }
4714
+ }
4715
+ if (found < 0) {
4716
+ snprintf(w->error, sizeof(w->error), "Table %s has no column '%s'", w->table, w->names[n]);
4717
+ return -1;
4718
+ }
4719
+ for (int k = 0; k < st->num_bound; k++) {
4720
+ if (st->bound[k] == found) {
4721
+ snprintf(w->error, sizeof(w->error), "Column '%s' is listed twice", w->names[n]);
4722
+ return -1;
4723
+ }
4724
+ }
4725
+ st->bound[st->num_bound++] = found;
4726
+ }
4727
+ } else {
4728
+ for (int i = 0; i < st->num_columns; i++) {
4729
+ const BulkColumn *col = &st->columns[i];
4730
+ if ((col->identity && !w->keep_identity) || col->usertype == USERTYPE_TIMESTAMP) {
4731
+ continue;
4732
+ }
4733
+ st->bound[st->num_bound++] = i;
4734
+ }
4735
+ }
4736
+ if (st->num_bound == 0) {
4737
+ snprintf(w->error, sizeof(w->error), "No columns to copy into %s", w->table);
4738
+ return -1;
4739
+ }
4740
+ for (int k = 0; k < st->num_bound; k++) {
4741
+ BulkColumn *col = &st->columns[st->bound[k]];
4742
+ if (col->kind == BULK_UNSUPPORTED) {
4743
+ snprintf(w->error, sizeof(w->error),
4744
+ "Column '%s' (%s) cannot be bulk copied; leave it out of `columns`", col->name,
4745
+ col->usertype == USERTYPE_TIMESTAMP ? "timestamp"
4746
+ : col->usertype == 34 || col->usertype == 35 ? "unichar/univarchar"
4747
+ : syb_type_name(col->type));
4748
+ return -1;
4749
+ }
4750
+ if (col->identity && !w->keep_identity) {
4751
+ snprintf(w->error, sizeof(w->error),
4752
+ "Column '%s' is an identity column: the server assigns it, unless keepIdentity is "
4753
+ "set",
4754
+ col->name);
4755
+ return -1;
4756
+ }
4757
+ }
4758
+ int variable = 0;
4759
+ for (int i = 0; i < st->num_columns; i++) {
4760
+ const BulkColumn *col = &st->columns[i];
4761
+ // keepIdentity copies the identity values from the rows: they must give them.
4762
+ if (col->identity && w->keep_identity) {
4763
+ int bound = 0;
4764
+ for (int k = 0; k < st->num_bound && !bound; k++) {
4765
+ bound = st->bound[k] == i;
4766
+ }
4767
+ if (!bound) {
4768
+ snprintf(w->error, sizeof(w->error),
4769
+ "keepIdentity copies the identity column's values, but '%s' is not among the "
4770
+ "columns",
4771
+ col->name);
4772
+ return -1;
4773
+ }
4774
+ }
4775
+ if (col->nullable || col->type == SYBVARCHAR || col->type == SYBVARBINARY ||
4776
+ col->type == SYBTEXT || col->type == SYBIMAGE) {
4777
+ variable++;
4778
+ }
4779
+ }
4780
+ // A bulk row's variable-length (or nullable) columns are counted in one byte,
4781
+ // in every row format: db-lib cannot send a row with more.
4782
+ if (variable > 255) {
4783
+ snprintf(w->error, sizeof(w->error),
4784
+ "%s has %d variable-length or nullable columns; a bulk copy supports at most 255",
4785
+ w->table, variable);
4786
+ return -1;
4787
+ }
4788
+ return 0;
4789
+ }
4790
+
4791
+ /**
4792
+ * `set identity_insert <table> on|off`, marked in `st` once the server has
4793
+ * done it; -1 if it failed. (The table name is at most 92 characters: see
4794
+ * is_table_name.)
4795
+ */
4796
+ static int bulk_identity_set(SybaseConnection *conn, BulkState *st, int on) {
4797
+ char sql[160];
4798
+ snprintf(sql, sizeof(sql), "set identity_insert %s %s", st->table, on ? "on" : "off");
4799
+ clear_error(conn);
4800
+ int rc = run_simple(conn, sql);
4801
+ if (rc == 0) {
4802
+ st->identity_insert = on;
4803
+ }
4804
+ return rc;
4805
+ }
4806
+
4807
+ /** Undo `set identity_insert on`; -1 if that failed and the session is not as it was. */
4808
+ static int bulk_identity_off(SybaseConnection *conn, BulkState *st) {
4809
+ return st->identity_insert ? bulk_identity_set(conn, st, 0) : 0;
4810
+ }
4811
+
4812
+ static void bulk_begin(BulkWork *w) {
4813
+ SybaseConnection *conn = w->conn;
4814
+ w->reusable = 1;
4815
+ if (conn->bulk) {
4816
+ snprintf(w->error, sizeof(w->error), "A bulk copy is already in progress on this connection");
4817
+ return;
4818
+ }
4819
+ // Text is sent as the server's own row image (see the section comment): only
4820
+ // a character set it is converted to can be copied into exactly.
4821
+ if (!conn->latin1 && strcmp(conn->charset, "utf8") != 0) {
4822
+ snprintf(w->error, sizeof(w->error),
4823
+ "Bulk copy supports servers whose character set is iso_1 or utf8; this one's is %s",
4824
+ conn->charset);
4825
+ return;
4826
+ }
4827
+ BulkState *st = (BulkState *)calloc(1, sizeof(BulkState));
4828
+ if (!st || !(st->table = strdup(w->table))) {
4829
+ free(st);
4830
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4831
+ return;
4832
+ }
4833
+ w->state = st;
4834
+ if (w->timeout_seconds > 0 && w->timeout_seconds != conn->timeout_seconds) {
4835
+ if (set_query_timeout(conn, w->timeout_seconds) != 0) {
4836
+ bulk_fail(w, "Could not set the copy's timeout");
4837
+ goto failed;
4838
+ }
4839
+ st->timeout_overridden = 1;
4840
+ }
4841
+ if (bulk_read_table(w, st) != 0 || bulk_bind_columns(w, st) != 0) {
4842
+ goto failed;
4843
+ }
4844
+ // identity_insert is marked on only once the server has turned it on: the
4845
+ // cleanup below turns off what is marked, and must not turn off a setting
4846
+ // this copy never made (the caller's own, say).
4847
+ for (int k = 0; k < st->num_bound; k++) {
4848
+ if (st->columns[st->bound[k]].identity) {
4849
+ if (bulk_identity_set(conn, st, 1) != 0) {
4850
+ bulk_fail(w, "Could not set identity_insert on");
4851
+ goto failed;
4852
+ }
4853
+ break;
4854
+ }
4855
+ }
4856
+
4857
+ clear_error(conn);
4858
+ // bcp_init reads the table's layout again, for db-lib's own use. From here
4859
+ // on, a failure leaves db-lib holding the bound buffers, which are freed with
4860
+ // this work item: the connection must then not be used again.
4861
+ w->reusable = 0;
4862
+ if (bcp_init(conn->dbproc, st->table, NULL, NULL, DB_IN) == FAIL) {
4863
+ bulk_fail(w, "bcp_init() failed");
4864
+ goto failed;
4865
+ }
4866
+ // ASE bcp's --ignoredefaults: a NULL stays NULL instead of taking the column
4867
+ // default (patches/freetds-*-sybase-bulk.patch, from FreeTDS master).
4868
+ if (w->ignore_defaults &&
4869
+ bcp_options(conn->dbproc, BCPHINTS, (BYTE *)"KEEP_NULLS", (int)strlen("KEEP_NULLS")) == FAIL) {
4870
+ bulk_fail(w, "Could not set the KEEP_NULLS bulk copy hint");
4871
+ goto failed;
4872
+ }
4873
+ for (int k = 0; k < st->num_bound; k++) {
4874
+ BulkColumn *col = &st->columns[st->bound[k]];
4875
+ int size, host_type, varlen;
4876
+ bulk_binding(col, &size, &host_type, &varlen);
4877
+ col->buf = (BYTE *)calloc(1, (size_t)size);
4878
+ if (!col->buf) {
4879
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4880
+ goto failed;
4881
+ }
4882
+ if (bcp_bind(conn->dbproc, col->buf, 0, varlen, NULL, 0, host_type, st->bound[k] + 1) == FAIL) {
4883
+ bulk_fail(w, "bcp_bind() failed");
4884
+ goto failed;
4885
+ }
4886
+ }
4887
+ // An identity column the rows do not give: on TDS 5.0 every row still
4888
+ // carries it, and with identity_insert off the server assigns the value in
4889
+ // place of what is sent. db-lib sends nothing for an unbound column and then
4890
+ // refuses the row, so it is bound to a constant 0.
4891
+ for (int i = 0; i < st->num_columns; i++) {
4892
+ BulkColumn *col = &st->columns[i];
4893
+ if (!col->identity || col->buf || (col->kind != BULK_INT && col->kind != BULK_NUMERIC)) {
4894
+ continue;
4895
+ }
4896
+ int size, host_type, varlen;
4897
+ bulk_binding(col, &size, &host_type, &varlen);
4898
+ col->buf = (BYTE *)calloc(1, (size_t)size);
4899
+ const char *why = NULL;
4900
+ if (!col->buf || (col->kind == BULK_NUMERIC &&
4901
+ parse_bulk_numeric("0", col, (DBNUMERIC *)col->buf, &why) != 0)) {
4902
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4903
+ goto failed;
4904
+ }
4905
+ if (bcp_bind(conn->dbproc, col->buf, 0, varlen, NULL, 0, host_type, i + 1) == FAIL) {
4906
+ bulk_fail(w, "bcp_bind() failed");
4907
+ goto failed;
4908
+ }
4909
+ }
4910
+
4911
+ st->fields = (BulkField *)calloc((size_t)st->num_bound, sizeof(BulkField));
4912
+ st->is_null = (unsigned char *)calloc((size_t)st->num_bound, 1);
4913
+ if (!st->fields || !st->is_null) {
4914
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4915
+ goto failed;
4916
+ }
4917
+
4918
+ // The description JS encodes values by.
4919
+ w->out = (BulkOutColumn *)calloc((size_t)st->num_bound, sizeof(BulkOutColumn));
4920
+ if (!w->out) {
4921
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4922
+ goto failed;
4923
+ }
4924
+ w->out_count = st->num_bound;
4925
+ for (int k = 0; k < st->num_bound; k++) {
4926
+ const BulkColumn *col = &st->columns[st->bound[k]];
4927
+ BulkOutColumn *out = &w->out[k];
4928
+ out->name = strdup(col->name);
4929
+ out->type = syb_type_name(col->type);
4930
+ out->nullable = col->nullable;
4931
+ out->length = col->max_length;
4932
+ out->precision = col->precision;
4933
+ out->scale = col->scale;
4934
+ if (!out->name) {
4935
+ snprintf(w->error, sizeof(w->error), "Out of memory");
4936
+ goto failed;
4937
+ }
4938
+ }
4939
+ conn->bulk = st;
4940
+ w->state = NULL;
4941
+ w->success = 1;
4942
+ return;
4943
+
4944
+ failed:
4945
+ // Nothing was sent to the server as bulk data yet: put the session back as it
4946
+ // was, or report that it is not.
4947
+ if (bulk_identity_off(conn, st) != 0) {
4948
+ w->reusable = 0;
4949
+ }
4950
+ if (restore_query_timeout(conn, st->timeout_overridden) != 0) {
4951
+ w->reusable = 0;
4952
+ }
4953
+ if (DBDEAD(conn->dbproc) || conn_is_dead(conn)) {
4954
+ w->reusable = 0;
4955
+ }
4956
+ w->success = 0;
4957
+ }
4958
+
4959
+ static void bulk_reject(BulkWork *w, int64_t row, const char *column, const char *message) {
4960
+ w->rejected_count++;
4961
+ if (w->num_rejects >= BULK_MAX_REJECT_DETAILS) {
4962
+ return;
4963
+ }
4964
+ if (!w->rejects) {
4965
+ w->rejects = (BulkReject *)calloc(BULK_MAX_REJECT_DETAILS, sizeof(BulkReject));
4966
+ if (!w->rejects) {
4967
+ return; // counted, without the detail
4968
+ }
4969
+ }
4970
+ BulkReject *r = &w->rejects[w->num_rejects++];
4971
+ r->row = row;
4972
+ r->column = column ? strdup(column) : NULL;
4973
+ snprintf(r->message, sizeof(r->message), "%s", message);
4974
+ }
4975
+
4976
+ /** Fail the copy on row `row` (and `column`, if any) once too many rows were rejected. */
4977
+ static int bulk_too_many_rejects(BulkWork *w, int64_t row, const char *column, const char *why) {
4978
+ if (w->rejected_count <= w->max_rejects) {
4979
+ return 0;
4980
+ }
4981
+ if (column) {
4982
+ snprintf(w->error, sizeof(w->error), "Row %lld, column '%s': %s", (long long)row, column, why);
4983
+ w->error_column = strdup(column);
4984
+ } else {
4985
+ snprintf(w->error, sizeof(w->error), "Row %lld: %s", (long long)row, why);
4986
+ }
4987
+ w->error_row = row;
4988
+ w->value_refused = 1;
4989
+ return -1;
4990
+ }
4991
+
4992
+ /**
4993
+ * Why db-lib refused a row with SYBEBCNN, which does not say which column: a
4994
+ * NULL headed for a NOT NULL column without a default. Names the candidates.
4995
+ */
4996
+ static void bulk_null_reason(BulkState *st, const unsigned char *is_null, char *out, size_t size) {
4997
+ int len = snprintf(out, size,
4998
+ "a NULL for a column that allows none and has no default (candidates:");
4999
+ int listed = 0;
5000
+ for (int i = 0; i < st->num_columns && len > 0 && (size_t)len < size; i++) {
5001
+ const BulkColumn *col = &st->columns[i];
5002
+ if (col->nullable) {
5003
+ continue;
5004
+ }
5005
+ int k = 0;
5006
+ while (k < st->num_bound && st->bound[k] != i) {
5007
+ k++;
5008
+ }
5009
+ if (k == st->num_bound || is_null[k]) {
5010
+ len += snprintf(out + len, size - (size_t)len, "%s '%s'%s", listed ? "," : "", col->name,
5011
+ k == st->num_bound ? " (not copied)" : "");
5012
+ listed++;
5013
+ }
5014
+ }
5015
+ if (len > 0 && (size_t)len < size) {
5016
+ snprintf(out + len, size - (size_t)len, ")");
5017
+ }
5018
+ }
5019
+
5020
+ /**
5021
+ * Send one row whose fields are in `fields`. Returns 0 when the row was sent or
5022
+ * rejected (and may be skipped), -1 when the copy must stop (w->error set).
5023
+ */
5024
+ static int bulk_send_row(BulkWork *w, BulkState *st, const BulkField *fields,
5025
+ const unsigned char *is_null, int64_t row) {
5026
+ SybaseConnection *conn = w->conn;
5027
+ char why[256];
5028
+ for (int k = 0; k < st->num_bound; k++) {
5029
+ const BulkColumn *col = &st->columns[st->bound[k]];
5030
+ int table_column = st->bound[k] + 1;
5031
+ int rc;
5032
+ if (is_null[k]) {
5033
+ rc = bcp_collen(conn->dbproc, 0, table_column) == FAIL ? BULK_FATAL : 0;
5034
+ if (rc != 0) {
5035
+ snprintf(why, sizeof(why), "could not be bound");
5036
+ }
5037
+ } else {
5038
+ why[0] = '\0';
5039
+ rc = bulk_set_value(conn, st, col, table_column, &fields[k], why, sizeof(why));
5040
+ if (rc != 0 && why[0] == '\0') {
5041
+ snprintf(why, sizeof(why), "could not be bound");
5042
+ }
5043
+ }
5044
+ if (rc == BULK_FATAL) {
5045
+ // Not about the value: the copy stops, and its open batch is rolled back.
5046
+ snprintf(w->error, sizeof(w->error), "Row %lld, column '%s': the value %s", (long long)row,
5047
+ col->name, why);
5048
+ w->error_row = row;
5049
+ free(w->error_column);
5050
+ w->error_column = strdup(col->name);
5051
+ return -1;
5052
+ }
5053
+ if (rc != 0) {
5054
+ char message[sizeof(why) + 8];
5055
+ snprintf(message, sizeof(message), "value %s", why);
5056
+ bulk_reject(w, row, col->name, message);
5057
+ return bulk_too_many_rejects(w, row, col->name, message);
5058
+ }
5059
+ }
5060
+
5061
+ clear_error(conn);
5062
+ if (bcp_sendrow(conn->dbproc) == SUCCEED) {
5063
+ st->started = 1;
5064
+ w->rows_sent++;
5065
+ return 0;
5066
+ }
5067
+ // Only a NULL the column refuses (db-lib reports SYBEBCNN for it) is about
5068
+ // this row. Anything else — the copy could not be opened, the connection
5069
+ // broke, or a failure db-lib did not report (out of memory) — stops the copy:
5070
+ // a row that may be valid is never skipped as a bad one.
5071
+ if (DBDEAD(conn->dbproc) || conn_is_dead(conn) || !conn->has_error ||
5072
+ conn->last_dberr != SYBEBCNN) {
5073
+ snprintf(w->error, sizeof(w->error), "Row %lld could not be sent: %.4000s", (long long)row,
5074
+ conn->has_error ? conn->last_error : "bcp_sendrow() failed");
5075
+ w->error_row = row;
5076
+ return -1;
5077
+ }
5078
+ st->started = 1; // the copy is open: only this row was refused
5079
+ bulk_null_reason(st, is_null, why, sizeof(why));
5080
+ bulk_reject(w, row, NULL, why);
5081
+ return bulk_too_many_rejects(w, row, NULL, why);
5082
+ }
5083
+
5084
+ /** End the batch (or the copy) as `w->commit` says; -1 (w->error set) on failure. */
5085
+ static int bulk_commit(BulkWork *w, BulkState *st) {
5086
+ SybaseConnection *conn = w->conn;
5087
+ // Never commit for a caller that has already been told the copy failed: the
5088
+ // JS layer closes the connection when its deadline passes.
5089
+ uv_mutex_lock(&conn->lock);
5090
+ int abandoned = conn->pending_close || conn->dead;
5091
+ uv_mutex_unlock(&conn->lock);
5092
+ if (abandoned) {
5093
+ snprintf(w->error, sizeof(w->error), "The connection was closed before the batch was committed");
5094
+ w->outcome = BULK_OUTCOME_NONE;
5095
+ return -1;
5096
+ }
5097
+ clear_error(conn);
5098
+ DBINT rows;
5099
+ if (!st->started || st->reopen_failed) {
5100
+ // No row has reached the server: nothing to commit, and nothing to end —
5101
+ // bcp_done would fail with no copy open. db-lib keeps its copy state (and
5102
+ // pointers to the bound buffers freed with `st`) until the next bcp_init or
5103
+ // dbclose, which free it without reading those buffers.
5104
+ rows = 0;
5105
+ } else if (w->commit == BULK_COMMIT_BATCH) {
5106
+ rows = bcp_batch(conn->dbproc);
5107
+ } else {
5108
+ rows = bcp_done(conn->dbproc);
5109
+ }
5110
+ if (rows < 0) {
5111
+ int lost = DBDEAD(conn->dbproc) || conn_is_dead(conn) || conn->timed_out || !conn->has_error;
5112
+ w->outcome = lost ? BULK_OUTCOME_UNKNOWN : BULK_OUTCOME_FAILED;
5113
+ bulk_fail(w, "The server did not confirm the batch");
5114
+ return -1;
5115
+ }
5116
+ w->committed = rows;
5117
+ // Committed, but db-lib could not open the copy again (see bcp_batch): said
5118
+ // when the next rows come; a copy that has none left ends here.
5119
+ if (w->commit == BULK_COMMIT_BATCH && conn->has_error && conn->last_dberr == SYBEBULKINSERT) {
5120
+ st->reopen_failed = 1;
5121
+ // As much of the reason as fits.
5122
+ snprintf(st->reopen_error, sizeof(st->reopen_error), "%.*s", (int)sizeof(st->reopen_error) - 1,
5123
+ conn->last_error);
5124
+ }
5125
+ if (w->commit == BULK_COMMIT_DONE) {
5126
+ w->finished = 1;
5127
+ // db-lib's copy state is not what it should be: the connection is not reused.
5128
+ if (st->reopen_failed) {
5129
+ w->discard = 1;
5130
+ }
5131
+ if (bulk_identity_off(conn, st) != 0 || restore_query_timeout(conn, st->timeout_overridden) != 0) {
5132
+ w->discard = 1; // the copy is done; the session could not be restored
5133
+ }
5134
+ }
5135
+ return 0;
5136
+ }
5137
+
5138
+ /** Stop sending once the hard timeout has fired or the connection was closed (checked every few rows). */
5139
+ static int bulk_watchdog_fired(BulkWork *w, int64_t r) {
5140
+ if ((r + 1) % WATCHDOG_CHECK_ROWS != 0 || !call_abandoned(&w->wd, w->conn)) {
5141
+ return 0;
5142
+ }
5143
+ if (!watchdog_fired(&w->wd) && w->error[0] == '\0') {
5144
+ snprintf(w->error, sizeof(w->error), "The connection was closed during the copy");
5145
+ }
5146
+ return 1;
5147
+ }
5148
+
5149
+ static void bulk_send(BulkWork *w) {
5150
+ SybaseConnection *conn = w->conn;
5151
+ BulkState *st = conn->bulk;
5152
+ w->committed = -1;
5153
+ if (!st) {
5154
+ snprintf(w->error, sizeof(w->error), "No bulk copy is in progress on this connection");
5155
+ w->reusable = 1;
5156
+ return;
5157
+ }
5158
+ // The copy could not be opened again after its last commit: nothing more can
5159
+ // be sent (db-lib would refuse each row without saying why).
5160
+ if (st->reopen_failed && w->rows > 0) {
5161
+ snprintf(w->error, sizeof(w->error),
5162
+ "The copy could not be opened again after its last committed batch: %s",
5163
+ st->reopen_error);
5164
+ w->error_row = w->first_row;
5165
+ w->outcome = BULK_OUTCOME_NONE;
5166
+ return;
5167
+ }
5168
+ BulkField *fields = st->fields;
5169
+ unsigned char *is_null = st->is_null;
5170
+ int failed = 0;
5171
+ int64_t row = w->first_row;
5172
+ if (w->data) {
5173
+ // TSV: fields split by tab, rows by newline (CRLF too), an empty field is NULL.
5174
+ const unsigned char *p = w->data, *end = w->data + w->data_len;
5175
+ // Exactly `rows` rows — each ending at a newline, but for a last one
5176
+ // without — checked as they are read, in the same pass: no row past the
5177
+ // data is sent, and data past the rows fails the chunk before its commit.
5178
+ // What was sent of it is then in a batch never committed (the connection
5179
+ // is given up: see BULK_OUTCOME_NONE), and rolled back.
5180
+ for (int64_t r = 0; r < w->rows && !failed; r++, row++) {
5181
+ if (p >= end) {
5182
+ snprintf(w->error, sizeof(w->error),
5183
+ "Internal error: malformed bulk rows (%lld rows said, %lld given)",
5184
+ (long long)w->rows, (long long)r);
5185
+ failed = 1;
5186
+ break;
5187
+ }
5188
+ const unsigned char *eol = p < end ? (const unsigned char *)memchr(p, '\n', (size_t)(end - p)) : NULL;
5189
+ const unsigned char *row_end = eol ? eol : end;
5190
+ const unsigned char *next = eol ? eol + 1 : end;
5191
+ // A CR only before the LF ending the row (CRLF): a last row without one
5192
+ // keeps a trailing CR as data.
5193
+ if (eol && row_end > p && row_end[-1] == '\r') {
5194
+ row_end--;
5195
+ }
5196
+ int count = 0;
5197
+ const unsigned char *field = p;
5198
+ for (;;) {
5199
+ const unsigned char *tab =
5200
+ (const unsigned char *)memchr(field, '\t', (size_t)(row_end - field));
5201
+ const unsigned char *field_end = tab ? tab : row_end;
5202
+ if (count < st->num_bound) {
5203
+ fields[count].bytes = field;
5204
+ fields[count].units = NULL;
5205
+ fields[count].length = (size_t)(field_end - field);
5206
+ is_null[count] = field_end == field;
5207
+ }
5208
+ count++;
5209
+ if (!tab) {
5210
+ break;
5211
+ }
5212
+ field = tab + 1;
5213
+ }
5214
+ p = next;
5215
+ if (count != st->num_bound) {
5216
+ char why[128];
5217
+ snprintf(why, sizeof(why), "has %d fields; %d columns are copied", count, st->num_bound);
5218
+ bulk_reject(w, row, NULL, why);
5219
+ failed = bulk_too_many_rejects(w, row, NULL, why) != 0;
5220
+ } else {
5221
+ failed = bulk_send_row(w, st, fields, is_null, row) != 0;
5222
+ }
5223
+ // Every row, sent or refused, reaches the stop check.
5224
+ failed = failed || bulk_watchdog_fired(w, r);
5225
+ }
5226
+ if (!failed && p < end) {
5227
+ snprintf(w->error, sizeof(w->error),
5228
+ "Internal error: malformed bulk rows (more data than the %lld rows said)",
5229
+ (long long)w->rows);
5230
+ failed = 1;
5231
+ }
5232
+ } else {
5233
+ // Value rows: every field's length, then all fields back to back.
5234
+ size_t unit = 0, field_index = 0;
5235
+ // Checked before multiplying: an absurd row count must not wrap around to
5236
+ // match the lengths given.
5237
+ int consistent = (uint64_t)w->rows <= SIZE_MAX / (size_t)st->num_bound &&
5238
+ w->num_lens == (size_t)w->rows * (size_t)st->num_bound;
5239
+ for (size_t i = 0; consistent && i < w->num_lens; i++) {
5240
+ if (w->lens[i] != BULK_NULL) {
5241
+ consistent = w->lens[i] <= w->num_units - unit;
5242
+ unit += w->lens[i];
5243
+ }
5244
+ }
5245
+ if (!consistent || unit != w->num_units) {
5246
+ snprintf(w->error, sizeof(w->error), "Internal error: malformed bulk rows");
5247
+ failed = 1;
5248
+ }
5249
+ unit = 0;
5250
+ for (int64_t r = 0; r < w->rows && !failed; r++, row++) {
5251
+ for (int k = 0; k < st->num_bound; k++, field_index++) {
5252
+ uint32_t len = w->lens[field_index];
5253
+ is_null[k] = len == BULK_NULL;
5254
+ fields[k].bytes = NULL;
5255
+ fields[k].units = w->units + unit;
5256
+ fields[k].length = is_null[k] ? 0 : len;
5257
+ unit += fields[k].length;
5258
+ }
5259
+ failed = bulk_send_row(w, st, fields, is_null, row) != 0 || bulk_watchdog_fired(w, r);
5260
+ }
5261
+ }
5262
+ if (failed) {
5263
+ w->outcome = BULK_OUTCOME_NONE;
5264
+ return;
5265
+ }
5266
+ if (w->commit != BULK_COMMIT_NONE && bulk_commit(w, st) != 0) {
5267
+ return;
5268
+ }
5269
+ if (w->finished) {
5270
+ conn->bulk = NULL;
5271
+ bulk_state_free(st);
5272
+ }
5273
+ w->success = 1;
5274
+ }
5275
+
5276
+ static void bulk_release_conn(BulkWork *w) {
5277
+ if (w->holds_conn) {
5278
+ w->holds_conn = 0;
5279
+ conn_release(w->conn);
5280
+ }
5281
+ }
5282
+
5283
+ static void bulk_execute(napi_env env, void *data) {
5284
+ (void)env;
5285
+ BulkWork *w = (BulkWork *)data;
5286
+ if (!w->holds_conn) {
5287
+ snprintf(w->error, sizeof(w->error), "Connection is closed");
5288
+ w->conn_dead = 1;
5289
+ w->conn_lost = 1;
5290
+ return;
5291
+ }
5292
+ uv_mutex_lock(&w->conn->lock);
5293
+ int closing = w->conn->pending_close;
5294
+ uv_mutex_unlock(&w->conn->lock);
5295
+ if (closing || conn_is_dead(w->conn) || DBDEAD(w->conn->dbproc)) {
5296
+ snprintf(w->error, sizeof(w->error), closing ? "Connection is closed" : "Connection is dead");
5297
+ conn_mark_dead(w->conn);
5298
+ w->conn_dead = 1;
5299
+ w->conn_lost = 1;
5300
+ bulk_release_conn(w);
5301
+ return;
5302
+ }
5303
+
5304
+ clear_error(w->conn);
5305
+ int started = watchdog_start(&w->wd, w->conn, w->hard_timeout_ms);
5306
+ if (started != 0) {
5307
+ // Nothing is sent without the hard timeout behind it (see query_execute),
5308
+ // nor once it has passed while the call was queued (reported below). A
5309
+ // copy already open cannot go on, though: its batch must be abandoned.
5310
+ snprintf(w->error, sizeof(w->error), "Could not start the hard-timeout watchdog");
5311
+ w->outcome = BULK_OUTCOME_NONE;
5312
+ w->reusable = w->op == BULK_OP_BEGIN && started < 0;
5313
+ } else {
5314
+ if (w->op == BULK_OP_BEGIN) {
5315
+ bulk_begin(w);
5316
+ } else {
5317
+ bulk_send(w);
5318
+ }
5319
+ watchdog_stop(&w->wd);
5320
+ }
5321
+ if (w->wd.fired) {
5322
+ // Whatever the call managed — `committed` still says what it committed —
5323
+ // the caller's deadline has passed.
5324
+ snprintf(w->error, sizeof(w->error),
5325
+ "Bulk copy exceeded hard timeout of %d ms — connection forcefully abandoned",
5326
+ w->hard_timeout_ms);
5327
+ if (w->success) {
5328
+ w->outcome = BULK_OUTCOME_NONE;
5329
+ }
5330
+ w->success = 0;
5331
+ w->reusable = 0;
5332
+ }
5333
+ // A copy that failed part way cannot go on, and its open batch is only rolled
5334
+ // back by closing the connection: never let it be used again.
5335
+ if (!w->success && !w->reusable) {
5336
+ conn_mark_dead(w->conn);
5337
+ }
5338
+ w->conn_lost = DBDEAD(w->conn->dbproc);
5339
+ w->conn_dead = conn_is_dead(w->conn);
5340
+ w->conn_timed_out = w->conn->timed_out;
5341
+ bulk_release_conn(w);
5342
+ }
5343
+
5344
+ /** Set `name` on `obj` to a number. */
5345
+ static napi_status set_number(napi_env env, napi_value obj, const char *name, double value) {
5346
+ napi_value v;
5347
+ NAPI_TRY(napi_create_double(env, value, &v));
5348
+ return napi_set_named_property(env, obj, name, v);
5349
+ }
5350
+
5351
+ static napi_status set_bool(napi_env env, napi_value obj, const char *name, bool value) {
5352
+ napi_value v;
5353
+ NAPI_TRY(napi_get_boolean(env, value, &v));
5354
+ return napi_set_named_property(env, obj, name, v);
5355
+ }
5356
+
5357
+ static napi_status set_string(napi_env env, napi_value obj, const char *name, const char *value) {
5358
+ napi_value v;
5359
+ NAPI_TRY(napi_create_string_utf8(env, value, NAPI_AUTO_LENGTH, &v));
5360
+ return napi_set_named_property(env, obj, name, v);
5361
+ }
5362
+
5363
+ /** Rows sent, rejected and committed by a bulkSend call — on success and on failure alike. */
5364
+ static napi_status set_send_progress(napi_env env, napi_value obj, const BulkWork *w) {
5365
+ NAPI_TRY(set_number(env, obj, "rowsSent", (double)w->rows_sent));
5366
+ NAPI_TRY(set_number(env, obj, "rejectedCount", (double)w->rejected_count));
5367
+ napi_value rejected, committed;
5368
+ NAPI_TRY(napi_create_array_with_length(env, (size_t)w->num_rejects, &rejected));
5369
+ for (int i = 0; i < w->num_rejects; i++) {
5370
+ napi_value item;
5371
+ NAPI_TRY(napi_create_object(env, &item));
5372
+ NAPI_TRY(set_number(env, item, "row", (double)w->rejects[i].row));
5373
+ if (w->rejects[i].column) {
5374
+ NAPI_TRY(set_string(env, item, "column", w->rejects[i].column));
5375
+ }
5376
+ NAPI_TRY(set_string(env, item, "message", w->rejects[i].message));
5377
+ NAPI_TRY(napi_set_element(env, rejected, (uint32_t)i, item));
5378
+ }
5379
+ NAPI_TRY(napi_set_named_property(env, obj, "rejected", rejected));
5380
+ if (w->committed >= 0) {
5381
+ return set_number(env, obj, "committed", (double)w->committed);
5382
+ }
5383
+ NAPI_TRY(napi_get_null(env, &committed));
5384
+ return napi_set_named_property(env, obj, "committed", committed);
5385
+ }
5386
+
5387
+ /** The error a failed bulk call rejects with, with everything known about it. */
5388
+ static napi_status bulk_error(napi_env env, const BulkWork *w, napi_value *error) {
5389
+ napi_value msg;
5390
+ NAPI_TRY(napi_create_string_utf8(env, w->error[0] ? w->error : "Bulk copy failed",
5391
+ NAPI_AUTO_LENGTH, &msg));
5392
+ NAPI_TRY(napi_create_error(env, NULL, msg, error));
5393
+ if (w->conn_dead) {
5394
+ NAPI_TRY(set_bool(env, *error, "connectionDead", true));
5395
+ }
5396
+ if (w->conn_lost) {
5397
+ NAPI_TRY(set_bool(env, *error, "connectionLost", true));
5398
+ }
5399
+ if (w->conn_timed_out) {
5400
+ NAPI_TRY(set_bool(env, *error, "timedOut", true));
5401
+ }
5402
+ if (w->wd.fired) {
5403
+ NAPI_TRY(set_bool(env, *error, "hardTimeout", true));
5404
+ }
5405
+ NAPI_TRY(set_bool(env, *error, "reusable", w->reusable && !w->conn_dead));
5406
+ if (w->op == BULK_OP_SEND) {
5407
+ static const char *const outcomes[] = {"none", "failed", "unknown"};
5408
+ NAPI_TRY(set_send_progress(env, *error, w));
5409
+ NAPI_TRY(set_string(env, *error, "outcome", outcomes[w->outcome]));
5410
+ if (w->error_row > 0) {
5411
+ NAPI_TRY(set_number(env, *error, "row", (double)w->error_row));
5412
+ }
5413
+ if (w->error_column) {
5414
+ NAPI_TRY(set_string(env, *error, "column", w->error_column));
5415
+ }
5416
+ if (w->value_refused && !w->wd.fired) {
5417
+ NAPI_TRY(set_bool(env, *error, "valueRefused", true));
5418
+ }
5419
+ }
5420
+ return napi_ok;
5421
+ }
5422
+
5423
+ /** What a successful bulk call resolves to. */
5424
+ static napi_status bulk_result(napi_env env, const BulkWork *w, napi_value *result) {
5425
+ NAPI_TRY(napi_create_object(env, result));
5426
+ if (w->op == BULK_OP_SEND) {
5427
+ NAPI_TRY(set_send_progress(env, *result, w));
5428
+ return set_bool(env, *result, "discard", w->discard != 0);
5429
+ }
5430
+ napi_value columns;
5431
+ NAPI_TRY(napi_create_array_with_length(env, (size_t)w->out_count, &columns));
5432
+ for (int k = 0; k < w->out_count; k++) {
5433
+ napi_value col;
5434
+ NAPI_TRY(napi_create_object(env, &col));
5435
+ const BulkOutColumn *out = &w->out[k];
5436
+ NAPI_TRY(set_string(env, col, "name", out->name));
5437
+ NAPI_TRY(set_string(env, col, "type", out->type));
5438
+ NAPI_TRY(set_bool(env, col, "nullable", out->nullable != 0));
5439
+ NAPI_TRY(set_number(env, col, "length", out->length));
5440
+ NAPI_TRY(set_number(env, col, "precision", out->precision));
5441
+ NAPI_TRY(set_number(env, col, "scale", out->scale));
5442
+ NAPI_TRY(napi_set_element(env, columns, (uint32_t)k, col));
5443
+ }
5444
+ return napi_set_named_property(env, *result, "columns", columns);
5445
+ }
5446
+
5447
+ static void bulk_complete(napi_env env, napi_status status, void *data) {
5448
+ BulkWork *w = (BulkWork *)data;
5449
+ napi_value value;
5450
+ int failed = status == napi_cancelled || !w->success;
5451
+ napi_status built = failed ? bulk_error(env, w, &value) : bulk_result(env, w, &value);
5452
+ if (built != napi_ok) {
5453
+ // Typically V8 out of memory. The error carries no `outcome` or
5454
+ // `committed`: what the call committed, if it committed, is unknown to JS,
5455
+ // and it reports that rather than a guess.
5456
+ reject_fallback(env, w->deferred, "Could not hand the bulk copy's answer to JavaScript");
5457
+ } else if (failed) {
5458
+ napi_reject_deferred(env, w->deferred, value);
5459
+ } else {
5460
+ napi_resolve_deferred(env, w->deferred, value);
5461
+ }
5462
+ // A cancelled work item never ran bulk_execute, so its slot is still held.
5463
+ bulk_release_conn(w);
5464
+ release_handle(env, &w->claim);
5465
+ napi_delete_async_work(env, w->work);
5466
+ bulk_work_free(w);
5467
+ }
5468
+
5469
+ /** A string option as a new UTF-8 string; NULL when absent, not a string, or holding a NUL. */
5470
+ static char *get_string_option(napi_env env, napi_value obj, const char *name) {
5471
+ napi_value v;
5472
+ napi_valuetype type;
5473
+ size_t len = 0;
5474
+ if (napi_get_named_property(env, obj, name, &v) != napi_ok ||
5475
+ napi_typeof(env, v, &type) != napi_ok || type != napi_string ||
5476
+ napi_get_value_string_utf8(env, v, NULL, 0, &len) != napi_ok) {
5477
+ return NULL;
5478
+ }
5479
+ char *out = (char *)malloc(len + 1);
5480
+ if (out) {
5481
+ napi_get_value_string_utf8(env, v, out, len + 1, &len);
5482
+ // Used as a C string: a NUL would make it another (shorter) name.
5483
+ if (strlen(out) != len) {
5484
+ free(out);
5485
+ return NULL;
5486
+ }
5487
+ }
5488
+ return out;
5489
+ }
5490
+
5491
+ /** Queue a bulk work item on the connection behind `handle`; settles its promise on failure. */
5492
+ static napi_value bulk_queue(napi_env env, BulkWork *w, napi_value handle_value,
5493
+ ConnHandle *handle, const char *name) {
5494
+ if (!handle_available(handle)) {
5495
+ bulk_work_free(w);
5496
+ return busy_rejection(env);
5497
+ }
5498
+ // The hard timeout counts from now, not from when a worker picks it up.
5499
+ w->wd.queued_ns = uv_hrtime();
5500
+ if (claim_handle(env, handle_value, handle, &w->claim) != 0) {
5501
+ bulk_work_free(w);
5502
+ napi_throw_error(env, NULL, "Could not reference the connection handle");
5503
+ return NULL;
5504
+ }
5505
+ napi_value promise, work_name;
5506
+ if (napi_create_promise(env, &w->deferred, &promise) != napi_ok) {
5507
+ release_handle(env, &w->claim);
5508
+ bulk_work_free(w);
5509
+ napi_throw_error(env, NULL, "Could not create the bulk copy's promise");
5510
+ return NULL;
5511
+ }
5512
+ w->conn = handle->conn;
5513
+ // Pinned now, on the JS thread, as fn_query does.
5514
+ w->holds_conn = w->conn != NULL && conn_acquire(w->conn) == 0;
5515
+ napi_status st = napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &work_name);
5516
+ if (st == napi_ok) {
5517
+ st = napi_create_async_work(env, NULL, work_name, bulk_execute, bulk_complete, w, &w->work);
5518
+ }
5519
+ if (st == napi_ok) {
5520
+ st = napi_queue_async_work(env, w->work);
5521
+ if (st != napi_ok) {
5522
+ napi_delete_async_work(env, w->work);
5523
+ }
5524
+ }
5525
+ if (st != napi_ok) {
5526
+ bulk_release_conn(w);
5527
+ release_handle(env, &w->claim);
5528
+ // Nothing ran: the error says so (outcome "none", nothing committed), so
5529
+ // that it is not taken for a commit whose answer was lost.
5530
+ snprintf(w->error, sizeof(w->error), "Failed to schedule the bulk copy");
5531
+ w->committed = -1;
5532
+ w->outcome = BULK_OUTCOME_NONE;
5533
+ w->reusable = w->op == BULK_OP_BEGIN;
5534
+ napi_value err;
5535
+ if (bulk_error(env, w, &err) == napi_ok) {
5536
+ napi_reject_deferred(env, w->deferred, err);
5537
+ } else {
5538
+ reject_fallback(env, w->deferred, w->error);
5539
+ }
5540
+ bulk_work_free(w);
5541
+ }
5542
+ return promise;
5543
+ }
5544
+
5545
+ /**
5546
+ * Whether `name` is a table name of plain identifiers — `table`, `owner.table`,
5547
+ * `db..table`, `db.owner.table`, `#temp` — and so safe to put in SQL as it is.
5548
+ */
5549
+ static int is_table_name(const char *name) {
5550
+ if (!name[0] || strlen(name) > 92) { // bcp_init's limit on TDS 5.0
5551
+ return 0;
5552
+ }
5553
+ for (const char *p = name; *p; p++) {
5554
+ char c = *p;
5555
+ if (!(c == '.' || c == '_' || c == '#' || c == '@' || c == '$' || is_digit(c) ||
5556
+ (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
5557
+ return 0;
5558
+ }
5559
+ }
5560
+ return 1;
5561
+ }
5562
+
5563
+ // napi_value bulkBegin(conn, { table, columns?, keepIdentity?, ignoreDefaults?,
5564
+ // allowConstraintBypass?, timeoutSeconds?, hardTimeoutMs })
5565
+ //
5566
+ // Opens a copy into `table` (see the section comment). Resolves to the bound
5567
+ // columns, `{ columns: [{ name, type, nullable, length, precision, scale }] }`,
5568
+ // in the order rows give their values. Failures carry `reusable`: whether the
5569
+ // connection was left as it was.
5570
+ static napi_value fn_bulk_begin(napi_env env, napi_callback_info info) {
5571
+ size_t argc = 2;
5572
+ napi_value args[2];
5573
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
5574
+ ConnHandle *handle = get_conn_handle(env, args[0]);
5575
+ if (handle == NULL) {
5576
+ return NULL; // TypeError pending
5577
+ }
5578
+ napi_valuetype type = napi_undefined;
5579
+ if (argc < 2 || napi_typeof(env, args[1], &type) != napi_ok || type != napi_object) {
5580
+ napi_throw_type_error(env, NULL, "Expected bulk copy options");
5581
+ return NULL;
5582
+ }
5583
+ BulkWork *w = (BulkWork *)calloc(1, sizeof(BulkWork));
5584
+ if (!w) {
5585
+ napi_throw_error(env, NULL, "Out of memory");
5586
+ return NULL;
5587
+ }
5588
+ w->op = BULK_OP_BEGIN;
5589
+ w->table = get_string_option(env, args[1], "table");
5590
+ if (!w->table || !is_table_name(w->table)) {
5591
+ bulk_work_free(w);
5592
+ napi_throw_type_error(env, NULL, "Expected a table name of the form db.owner.table");
5593
+ return NULL;
5594
+ }
5595
+ // Every read below may run JavaScript (a getter) that throws: its exception
5596
+ // is left pending and nothing is queued.
5597
+ napi_value columns;
5598
+ napi_valuetype columns_type = napi_undefined;
5599
+ bool is_array = false;
5600
+ if (napi_get_named_property(env, args[1], "columns", &columns) != napi_ok ||
5601
+ napi_typeof(env, columns, &columns_type) != napi_ok ||
5602
+ napi_is_array(env, columns, &is_array) != napi_ok) {
5603
+ bulk_work_free(w);
5604
+ return NULL;
5605
+ }
5606
+ if (columns_type != napi_undefined && !is_array) {
5607
+ bulk_work_free(w);
5608
+ napi_throw_type_error(env, NULL, "Expected `columns` to be an array of column names");
5609
+ return NULL;
5610
+ }
5611
+ if (is_array) {
5612
+ uint32_t n = 0;
5613
+ if (napi_get_array_length(env, columns, &n) != napi_ok) {
5614
+ bulk_work_free(w);
5615
+ return NULL;
5616
+ }
5617
+ w->names = (char **)calloc(n > 0 ? n : 1, sizeof(char *));
5618
+ if (!w->names) {
5619
+ bulk_work_free(w);
5620
+ napi_throw_error(env, NULL, "Out of memory");
5621
+ return NULL;
5622
+ }
5623
+ for (uint32_t i = 0; i < n; i++) {
5624
+ napi_value item;
5625
+ size_t len = 0;
5626
+ napi_valuetype item_type = napi_undefined;
5627
+ if (napi_get_element(env, columns, i, &item) != napi_ok ||
5628
+ napi_typeof(env, item, &item_type) != napi_ok) {
5629
+ bulk_work_free(w);
5630
+ return NULL;
5631
+ }
5632
+ if (item_type != napi_string) {
5633
+ bulk_work_free(w);
5634
+ napi_throw_type_error(env, NULL, "Expected column names as strings");
5635
+ return NULL;
5636
+ }
5637
+ if (napi_get_value_string_utf8(env, item, NULL, 0, &len) != napi_ok) {
5638
+ bulk_work_free(w);
5639
+ return NULL;
5640
+ }
5641
+ w->names[i] = (char *)malloc(len + 1);
5642
+ w->num_names = (int)i + 1;
5643
+ if (!w->names[i]) {
5644
+ bulk_work_free(w);
5645
+ napi_throw_error(env, NULL, "Out of memory");
5646
+ return NULL;
5647
+ }
5648
+ if (napi_get_value_string_utf8(env, item, w->names[i], len + 1, &len) != napi_ok) {
5649
+ bulk_work_free(w);
5650
+ return NULL;
5651
+ }
5652
+ if (strlen(w->names[i]) != len) {
5653
+ bulk_work_free(w);
5654
+ napi_throw_type_error(env, NULL, "Expected column names without NUL characters");
5655
+ return NULL;
5656
+ }
5657
+ }
5658
+ }
5659
+ // A hard timeout is required: a copy never runs without its watchdog.
5660
+ if (option_bool(env, args[1], "keepIdentity", &w->keep_identity) != 0 ||
5661
+ option_bool(env, args[1], "ignoreDefaults", &w->ignore_defaults) != 0 ||
5662
+ option_bool(env, args[1], "allowConstraintBypass", &w->allow_constraint_bypass) != 0 ||
5663
+ config_int(env, args[1], "timeoutSeconds", 1, MAX_TIMEOUT_SECONDS, 0, &w->timeout_seconds) != 0 ||
5664
+ config_int(env, args[1], "hardTimeoutMs", 1, INT32_MAX, 1, &w->hard_timeout_ms) != 0) {
5665
+ bulk_work_free(w);
5666
+ return NULL;
5667
+ }
5668
+ return bulk_queue(env, w, args[0], handle, "sybase_bulk_begin");
5669
+ }
5670
+
5671
+ // napi_value bulkSend(conn, { rows, firstRow, maxRejects, commit, hardTimeoutMs, tsv }
5672
+ // | { rows, firstRow, maxRejects, commit, hardTimeoutMs, text, lens })
5673
+ //
5674
+ // Sends `rows` rows of the open copy: TSV (a Uint8Array or a string, UTF-8),
5675
+ // or value rows — every field of every row back to back in the string `text`,
5676
+ // with `lens` (a Uint32Array) holding each field's length in UTF-16 units,
5677
+ // 0xFFFFFFFF for NULL. Then `commit`: 0 keeps the batch open, 1 commits it
5678
+ // (bcp_batch), 2 commits it and ends the copy (bcp_done). Resolves to
5679
+ // `{ rowsSent, rejectedCount, rejected, committed, discard }`; failures carry
5680
+ // the same progress, plus `outcome`, `row` and `column`.
5681
+ static napi_value fn_bulk_send(napi_env env, napi_callback_info info) {
5682
+ size_t argc = 2;
5683
+ napi_value args[2];
5684
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
5685
+ ConnHandle *handle = get_conn_handle(env, args[0]);
5686
+ if (handle == NULL) {
5687
+ return NULL; // TypeError pending
5688
+ }
5689
+ napi_valuetype type = napi_undefined;
5690
+ if (argc < 2 || napi_typeof(env, args[1], &type) != napi_ok || type != napi_object) {
5691
+ napi_throw_type_error(env, NULL, "Expected bulk copy rows");
5692
+ return NULL;
5693
+ }
5694
+ BulkWork *w = (BulkWork *)calloc(1, sizeof(BulkWork));
5695
+ if (!w) {
5696
+ napi_throw_error(env, NULL, "Out of memory");
5697
+ return NULL;
5698
+ }
5699
+ w->op = BULK_OP_SEND;
5700
+ w->committed = -1;
5701
+ w->first_row = 1;
5702
+ w->commit = BULK_COMMIT_NONE;
5703
+ int64_t commit = BULK_COMMIT_NONE;
5704
+ if (option_int64(env, args[1], "rows", 0, MAX_SAFE_INTEGER, 1, &w->rows) != 0 ||
5705
+ option_int64(env, args[1], "firstRow", 1, MAX_SAFE_INTEGER, 0, &w->first_row) != 0 ||
5706
+ option_int64(env, args[1], "maxRejects", 0, MAX_SAFE_INTEGER, 0, &w->max_rejects) != 0 ||
5707
+ option_int64(env, args[1], "commit", BULK_COMMIT_NONE, BULK_COMMIT_DONE, 0, &commit) != 0 ||
5708
+ config_int(env, args[1], "hardTimeoutMs", 1, INT32_MAX, 1, &w->hard_timeout_ms) != 0) {
5709
+ bulk_work_free(w);
5710
+ return NULL;
5711
+ }
5712
+ w->commit = (int)commit;
5713
+
5714
+ // The rows: TSV in `tsv`, or value rows in `text` and `lens`. Any read that
5715
+ // fails (a throwing getter) leaves its exception pending; nothing is queued.
5716
+ napi_value v, lens;
5717
+ napi_valuetype vtype = napi_undefined, ltype = napi_undefined;
5718
+ bool is_typed = false;
5719
+ int ok = 0;
5720
+ if (napi_get_named_property(env, args[1], "tsv", &v) != napi_ok ||
5721
+ napi_typeof(env, v, &vtype) != napi_ok) {
5722
+ bulk_work_free(w);
5723
+ return fail_call(env, "Could not read `tsv`");
5724
+ }
5725
+ if (vtype == napi_string) {
5726
+ size_t len = 0;
5727
+ if (napi_get_value_string_utf8(env, v, NULL, 0, &len) != napi_ok) {
5728
+ bulk_work_free(w);
5729
+ return fail_call(env, "Could not read `tsv`");
5730
+ }
5731
+ w->data = (unsigned char *)malloc(len + 1);
5732
+ if (!w->data) {
5733
+ bulk_work_free(w);
5734
+ napi_throw_error(env, NULL, "Out of memory");
5735
+ return NULL;
5736
+ }
5737
+ if (napi_get_value_string_utf8(env, v, (char *)w->data, len + 1, &len) != napi_ok) {
5738
+ bulk_work_free(w);
5739
+ return fail_call(env, "Could not read `tsv`");
5740
+ }
5741
+ w->data_len = len;
5742
+ ok = 1;
5743
+ } else if (vtype != napi_undefined) {
5744
+ if (napi_is_typedarray(env, v, &is_typed) != napi_ok) {
5745
+ bulk_work_free(w);
5746
+ return fail_call(env, "Could not read `tsv`");
5747
+ }
5748
+ napi_typedarray_type ta_type = napi_int8_array;
5749
+ size_t length = 0;
5750
+ void *bytes = NULL;
5751
+ if (is_typed) {
5752
+ if (napi_get_typedarray_info(env, v, &ta_type, &length, &bytes, NULL, NULL) != napi_ok) {
5753
+ bulk_work_free(w);
5754
+ return fail_call(env, "Could not read `tsv`");
5755
+ }
5756
+ }
5757
+ if (is_typed && ta_type == napi_uint8_array) {
5758
+ // Copied: the worker must not read memory the JS heap owns.
5759
+ w->data = (unsigned char *)malloc(length > 0 ? length : 1);
5760
+ if (!w->data) {
5761
+ bulk_work_free(w);
5762
+ napi_throw_error(env, NULL, "Out of memory");
5763
+ return NULL;
5764
+ }
5765
+ if (length > 0) {
5766
+ memcpy(w->data, bytes, length);
5767
+ }
5768
+ w->data_len = length;
5769
+ ok = 1;
5770
+ }
5771
+ } else {
5772
+ if (napi_get_named_property(env, args[1], "text", &v) != napi_ok ||
5773
+ napi_typeof(env, v, &vtype) != napi_ok ||
5774
+ napi_get_named_property(env, args[1], "lens", &lens) != napi_ok ||
5775
+ napi_typeof(env, lens, &ltype) != napi_ok || napi_is_typedarray(env, lens, &is_typed) != napi_ok) {
5776
+ bulk_work_free(w);
5777
+ return fail_call(env, "Could not read `text` and `lens`");
5778
+ }
5779
+ napi_typedarray_type ta_type = napi_int8_array;
5780
+ size_t count = 0, units = 0;
5781
+ void *values = NULL;
5782
+ if (vtype == napi_string && is_typed) {
5783
+ if (napi_get_typedarray_info(env, lens, &ta_type, &count, &values, NULL, NULL) != napi_ok ||
5784
+ napi_get_value_string_utf16(env, v, NULL, 0, &units) != napi_ok) {
5785
+ bulk_work_free(w);
5786
+ return fail_call(env, "Could not read `text` and `lens`");
5787
+ }
5788
+ }
5789
+ if (vtype == napi_string && is_typed && ta_type == napi_uint32_array) {
5790
+ w->lens = (uint32_t *)malloc((count > 0 ? count : 1) * sizeof(uint32_t));
5791
+ w->units = (uint16_t *)malloc((units + 1) * sizeof(uint16_t));
5792
+ if (!w->lens || !w->units) {
5793
+ bulk_work_free(w);
5794
+ napi_throw_error(env, NULL, "Out of memory");
5795
+ return NULL;
5796
+ }
5797
+ if (count > 0) {
5798
+ memcpy(w->lens, values, count * sizeof(uint32_t));
5799
+ }
5800
+ if (napi_get_value_string_utf16(env, v, (char16_t *)w->units, units + 1, &units) != napi_ok) {
5801
+ bulk_work_free(w);
5802
+ return fail_call(env, "Could not read `text`");
5803
+ }
5804
+ w->num_lens = count;
5805
+ w->num_units = units;
5806
+ ok = 1;
5807
+ }
5808
+ }
5809
+ if (!ok) {
5810
+ bulk_work_free(w);
5811
+ napi_throw_type_error(env, NULL, "Expected bulk copy rows as `tsv`, or as `text` and `lens`");
5812
+ return NULL;
5813
+ }
5814
+ return bulk_queue(env, w, args[0], handle, "sybase_bulk_send");
5815
+ }
5816
+
5817
+ // ---------------------------------------------------------------------------
5818
+ // Close connection
5819
+ // ---------------------------------------------------------------------------
5820
+
5821
+ // fn_close: deferred-safe close.
5822
+ //
5823
+ // If a query worker is currently running on this connection (e.g. JS layer
5824
+ // gave up on a hard timeout but the C-level dbnextrow is still blocked), we
5825
+ // do NOT free dbproc here — that would be a use-after-free in the worker.
5826
+ // Instead we set pending_close; conn_release in the worker will perform the
5827
+ // actual destruction once it returns.
5828
+ //
5829
+ // We deliberately do NOT call dbcancel from this thread either: dbcancel
5830
+ // invokes tds_process_cancel which reads from the same socket the worker
5831
+ // is reading from. We rely on db-lib's per-connection DBSETTIME to abort
5832
+ // the worker; if the user wants tighter behavior they must configure a
5833
+ // smaller `timeout` rather than a smaller hardTimeoutMs.
5834
+ static napi_value fn_close(napi_env env, napi_callback_info info) {
5835
+ size_t argc = 1;
5836
+ napi_value args[1];
5837
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
5838
+
5839
+ ConnHandle *handle = get_conn_handle(env, args[0]);
5840
+ if (handle == NULL) {
5841
+ return NULL; // TypeError pending
5842
+ }
5843
+ // Closing twice is a no-op: the second call finds no connection.
5844
+ if (handle->conn) {
5845
+ SybaseConnection *conn = handle->conn;
5846
+ handle->conn = NULL;
5847
+ close_connection(conn, env, env_state(env));
5848
+ }
5849
+
5850
+ napi_value undefined;
5851
+ napi_get_undefined(env, &undefined);
5852
+ return undefined;
5853
+ }
5854
+
5855
+ // ---------------------------------------------------------------------------
5856
+ // Check if connection is alive
5857
+ // ---------------------------------------------------------------------------
5858
+
5859
+ static napi_value fn_is_alive(napi_env env, napi_callback_info info) {
5860
+ size_t argc = 1;
5861
+ napi_value args[1];
5862
+ napi_get_cb_info(env, info, &argc, args, NULL, NULL);
5863
+
5864
+ ConnHandle *handle = get_conn_handle(env, args[0]);
5865
+ if (handle == NULL) {
5866
+ return NULL; // TypeError pending
5867
+ }
5868
+ SybaseConnection *conn = handle->conn;
5869
+
5870
+ int alive = 0;
5871
+ if (conn) {
5872
+ uv_mutex_lock(&conn->lock);
5873
+ // db-lib's own state is read only while no call is in flight: a worker
5874
+ // changes it without this lock. A running call that finds the connection
5875
+ // dead marks `dead` (or closes it) itself.
5876
+ if (conn->dbproc && !conn->dead && !conn->pending_close &&
5877
+ (conn->in_flight > 0 || !DBDEAD(conn->dbproc))) {
5878
+ alive = 1;
5879
+ }
5880
+ uv_mutex_unlock(&conn->lock);
5881
+ }
5882
+
5883
+ napi_value result;
5884
+ napi_get_boolean(env, alive, &result);
5885
+ return result;
5886
+ }
5887
+
5888
+ // ---------------------------------------------------------------------------
5889
+ // Server character set
5890
+ // ---------------------------------------------------------------------------
5891
+
5892
+ /** The server's default character set name, or "" for a closed handle. */
5893
+ static napi_value fn_server_charset(napi_env env, napi_callback_info info) {
2903
5894
  size_t argc = 1;
2904
5895
  napi_value args[1];
2905
5896
  napi_get_cb_info(env, info, &argc, args, NULL, NULL);
@@ -2931,35 +5922,47 @@ static napi_value fn_get_version(napi_env env, napi_callback_info info) {
2931
5922
 
2932
5923
  static napi_value init(napi_env env, napi_value exports) {
2933
5924
  uv_once(&global_init_once, init_globals);
2934
-
2935
- if (dbinit() == FAIL) {
5925
+ if (!dblib_ready) {
2936
5926
  napi_throw_error(env, NULL, "FreeTDS dbinit() failed");
2937
5927
  return exports;
2938
5928
  }
5929
+ // Marked closing by a cleanup hook: Node runs those in reverse order, so this
5930
+ // one runs before N-API's own, which runs the finalizers still pending.
5931
+ EnvState *state = (EnvState *)calloc(1, sizeof(EnvState));
5932
+ if (state != NULL) {
5933
+ state->refs = 1; // the environment's, released by env_state_free
5934
+ }
5935
+ if (state == NULL || napi_set_instance_data(env, state, env_state_free, NULL) != napi_ok) {
5936
+ free(state);
5937
+ napi_throw_error(env, NULL, "drizzle-sybase: could not set up the addon");
5938
+ return exports;
5939
+ }
5940
+ if (napi_add_env_cleanup_hook(env, env_cleanup, state) != napi_ok) {
5941
+ napi_throw_error(env, NULL, "drizzle-sybase: could not set up the addon");
5942
+ return exports;
5943
+ }
2939
5944
 
2940
- // Install per-process error/message handlers
2941
- dberrhandle(err_handler);
2942
- dbmsghandle(msg_handler);
2943
-
2944
- napi_value fn;
2945
-
2946
- napi_create_function(env, "connect", 0, fn_connect, NULL, &fn);
2947
- napi_set_named_property(env, exports, "connect", fn);
2948
-
2949
- napi_create_function(env, "query", 0, fn_query, NULL, &fn);
2950
- napi_set_named_property(env, exports, "query", fn);
2951
-
2952
- napi_create_function(env, "close", 0, fn_close, NULL, &fn);
2953
- napi_set_named_property(env, exports, "close", fn);
2954
-
2955
- napi_create_function(env, "isAlive", 0, fn_is_alive, NULL, &fn);
2956
- napi_set_named_property(env, exports, "isAlive", fn);
2957
-
2958
- napi_create_function(env, "serverCharset", 0, fn_server_charset, NULL, &fn);
2959
- napi_set_named_property(env, exports, "serverCharset", fn);
2960
-
2961
- napi_create_function(env, "getVersion", 0, fn_get_version, NULL, &fn);
2962
- napi_set_named_property(env, exports, "getVersion", fn);
5945
+ static const struct {
5946
+ const char *name;
5947
+ napi_callback fn;
5948
+ } functions[] = {
5949
+ {"connect", fn_connect},
5950
+ {"query", fn_query},
5951
+ {"bulkBegin", fn_bulk_begin},
5952
+ {"bulkSend", fn_bulk_send},
5953
+ {"close", fn_close},
5954
+ {"isAlive", fn_is_alive},
5955
+ {"serverCharset", fn_server_charset},
5956
+ {"getVersion", fn_get_version},
5957
+ };
5958
+ for (size_t i = 0; i < sizeof(functions) / sizeof(functions[0]); i++) {
5959
+ napi_value fn;
5960
+ if (napi_create_function(env, functions[i].name, NAPI_AUTO_LENGTH, functions[i].fn, NULL, &fn) !=
5961
+ napi_ok ||
5962
+ napi_set_named_property(env, exports, functions[i].name, fn) != napi_ok) {
5963
+ return fail_call(env, "drizzle-sybase: could not set up the addon");
5964
+ }
5965
+ }
2963
5966
 
2964
5967
  return exports;
2965
5968
  }