vsql-node 0.0.0-stage → 1.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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -2
  3. package/binding.gyp +61 -0
  4. package/index.d.ts +133 -0
  5. package/internal/amalgamation/LICENSE +21 -0
  6. package/internal/amalgamation/LICENSE-V +21 -0
  7. package/internal/amalgamation/afunix.h +26 -0
  8. package/internal/amalgamation/atomic.h +571 -0
  9. package/internal/amalgamation/core_windows.c +25 -0
  10. package/internal/amalgamation/core_windows.h +9 -0
  11. package/internal/amalgamation/execute_capture_nix.c +7 -0
  12. package/internal/amalgamation/execute_capture_nix.h +266 -0
  13. package/internal/amalgamation/gc/cord.h +426 -0
  14. package/internal/amalgamation/gc/cord_pos.h +151 -0
  15. package/internal/amalgamation/gc/ec.h +91 -0
  16. package/internal/amalgamation/gc/gc.h +2826 -0
  17. package/internal/amalgamation/gc/gc_allocator.h +480 -0
  18. package/internal/amalgamation/gc/gc_backptr.h +109 -0
  19. package/internal/amalgamation/gc/gc_config_macros.h +548 -0
  20. package/internal/amalgamation/gc/gc_disclaim.h +85 -0
  21. package/internal/amalgamation/gc/gc_gcj.h +123 -0
  22. package/internal/amalgamation/gc/gc_inline.h +261 -0
  23. package/internal/amalgamation/gc/gc_mark.h +516 -0
  24. package/internal/amalgamation/gc/gc_pthread_redirects.h +140 -0
  25. package/internal/amalgamation/gc/gc_tiny_fl.h +107 -0
  26. package/internal/amalgamation/gc/gc_typed.h +196 -0
  27. package/internal/amalgamation/gc/gc_version.h +55 -0
  28. package/internal/amalgamation/gc/javaxfc.h +76 -0
  29. package/internal/amalgamation/gc/leak_detector.h +120 -0
  30. package/internal/amalgamation/gc.c +45932 -0
  31. package/internal/amalgamation/gc.h +4 -0
  32. package/internal/amalgamation/gc_boehm_helpers.c +64 -0
  33. package/internal/amalgamation/gc_debugger_linux.h +61 -0
  34. package/internal/amalgamation/getrandom_linux.h +33 -0
  35. package/internal/amalgamation/ipv6_v6only.h +5 -0
  36. package/internal/amalgamation/libvsql.c +27 -0
  37. package/internal/amalgamation/libvsql.h +1772 -0
  38. package/internal/amalgamation/libvsql_linux_amd64.c +106591 -0
  39. package/internal/amalgamation/libvsql_linux_arm64.c +106571 -0
  40. package/internal/amalgamation/libvsql_macos_amd64.c +106728 -0
  41. package/internal/amalgamation/libvsql_macos_arm64.c +106708 -0
  42. package/internal/amalgamation/libvsql_windows_amd64.c +108235 -0
  43. package/internal/amalgamation/segfault_handler_nix.c +56 -0
  44. package/internal/amalgamation/segfault_handler_nix.h +449 -0
  45. package/internal/amalgamation/segfault_handler_windows.h +52 -0
  46. package/internal/amalgamation/thread_helper.c +12 -0
  47. package/internal/amalgamation/thread_helper.h +96 -0
  48. package/internal/amalgamation/ticks_windows.c +3 -0
  49. package/internal/amalgamation/ticks_windows.h +31 -0
  50. package/internal/amalgamation/ticks_windows_api.h +6 -0
  51. package/internal/amalgamation/vsql_runtime.c +14 -0
  52. package/internal/amalgamation/vsql_runtime_linux.c +6 -0
  53. package/internal/amalgamation/vsql_runtime_macos.c +6 -0
  54. package/internal/amalgamation/vsql_runtime_windows.c +6 -0
  55. package/lib/binding.js +13 -0
  56. package/lib/connection.js +492 -0
  57. package/lib/convert.js +213 -0
  58. package/lib/errors.js +17 -0
  59. package/lib/index.js +41 -0
  60. package/libvsql_flags.gypi +30 -0
  61. package/package.json +45 -4
  62. package/prebuilds/darwin-arm64/vsql-node.glibc.node +0 -0
  63. package/prebuilds/darwin-x64/vsql-node.glibc.node +0 -0
  64. package/prebuilds/linux-arm64/vsql-node.glibc.node +0 -0
  65. package/prebuilds/linux-x64/vsql-node.glibc.node +0 -0
  66. package/prebuilds/win32-x64/vsql-node.glibc.node +0 -0
  67. package/src/binding.c +773 -0
  68. package/src/runtime.c +20 -0
package/src/binding.c ADDED
@@ -0,0 +1,773 @@
1
+ // binding.c is the vsql addon, the only code that calls libvsql (see
2
+ // libvsql.h), which is compiled into the addon from the amalgamation by
3
+ // binding.gyp. Each function is a thin wrapper around a libvsql routine, with
4
+ // handles as numbers. Everything else (such as converting values) is in
5
+ // JavaScript (see lib).
6
+ //
7
+ // The functions are synchronous: libvsql runs on the thread that called it.
8
+ //
9
+ // When a routine fails, the error is made by the function given to
10
+ // setErrorFactory, from the SQLSTATE and message of the first diagnostic
11
+ // record.
12
+
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+
16
+ #include <node_api.h>
17
+
18
+ #include "libvsql.h"
19
+
20
+ // See runtime.c.
21
+ void vsql_gc_after_node_init(void);
22
+
23
+ // The largest integer that a number can hold exactly (2^53 - 1).
24
+ #define MAX_SAFE_INTEGER 9007199254740991LL
25
+
26
+ // The data of each instance of the addon (one for each thread that loads it).
27
+ typedef struct {
28
+ napi_ref error_factory;
29
+ } instance;
30
+
31
+ // NAPI_CALL returns NULL from the function if a Node-API call fails, which
32
+ // has thrown an exception.
33
+ #define NAPI_CALL(call) \
34
+ do { \
35
+ if ((call) != napi_ok) { \
36
+ return NULL; \
37
+ } \
38
+ } while (0)
39
+
40
+ // throw_error throws the error for an SQLSTATE and message.
41
+ static void throw_error(napi_env env, const char *sqlstate, const char *message, size_t length) {
42
+ instance *inst = NULL;
43
+ napi_value factory = NULL;
44
+ napi_get_instance_data(env, (void **)&inst);
45
+ if (inst == NULL || inst->error_factory == NULL ||
46
+ napi_get_reference_value(env, inst->error_factory, &factory) != napi_ok || factory == NULL) {
47
+ napi_throw_error(env, sqlstate, message);
48
+ return;
49
+ }
50
+
51
+ napi_value args[2], global, err;
52
+ if (napi_create_string_utf8(env, sqlstate, NAPI_AUTO_LENGTH, &args[0]) != napi_ok ||
53
+ napi_create_string_utf8(env, message, length, &args[1]) != napi_ok ||
54
+ napi_get_global(env, &global) != napi_ok ||
55
+ napi_call_function(env, global, factory, 2, args, &err) != napi_ok) {
56
+ return;
57
+ }
58
+ napi_throw(env, err);
59
+ }
60
+
61
+ // check returns 0 for a return code that isn't an error (SQL_NO_DATA isn't),
62
+ // otherwise it throws the error from the diagnostics of the handle and returns
63
+ // -1.
64
+ static int check(napi_env env, SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
65
+ switch (rc) {
66
+ case SQL_SUCCESS:
67
+ case SQL_SUCCESS_WITH_INFO:
68
+ case SQL_NO_DATA:
69
+ return 0;
70
+ case SQL_INVALID_HANDLE:
71
+ throw_error(env, "HY000", "invalid handle", NAPI_AUTO_LENGTH);
72
+ return -1;
73
+ }
74
+
75
+ SQLCHAR sqlstate[6];
76
+ SQLSMALLINT size = 512, length = 0;
77
+ SQLCHAR *message = malloc(size);
78
+ for (;;) {
79
+ if (message == NULL) {
80
+ napi_throw_error(env, NULL, "out of memory");
81
+ return -1;
82
+ }
83
+
84
+ rc = SQLGetDiagRec(handle_type, handle, 1, sqlstate, NULL, message, size, &length);
85
+ if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
86
+ free(message);
87
+ throw_error(env, "HY000", "unknown error", NAPI_AUTO_LENGTH);
88
+ return -1;
89
+ }
90
+
91
+ // The message was truncated.
92
+ if (length >= size) {
93
+ size = length + 1;
94
+ free(message);
95
+ message = malloc(size);
96
+ continue;
97
+ }
98
+
99
+ throw_error(env, (const char *)sqlstate, (const char *)message, length);
100
+ free(message);
101
+ return -1;
102
+ }
103
+ }
104
+
105
+ // CHECK returns NULL from the function if a libvsql routine fails.
106
+ #define CHECK(handle_type, handle, routine) \
107
+ do { \
108
+ if (check(env, (routine), (handle_type), (handle)) < 0) { \
109
+ return NULL; \
110
+ } \
111
+ } while (0)
112
+
113
+ // args gets the arguments of a function, which must have at least n.
114
+ static int args(napi_env env, napi_callback_info info, size_t n, napi_value *argv) {
115
+ size_t argc = n;
116
+ if (napi_get_cb_info(env, info, &argc, argv, NULL, NULL) != napi_ok) {
117
+ return -1;
118
+ }
119
+ if (argc < n) {
120
+ napi_throw_type_error(env, NULL, "not enough arguments");
121
+ return -1;
122
+ }
123
+
124
+ return 0;
125
+ }
126
+
127
+ // ARGS gets the arguments of a function, or returns NULL.
128
+ #define ARGS(n) \
129
+ napi_value argv[n]; \
130
+ if (args(env, info, (n), argv) < 0) { \
131
+ return NULL; \
132
+ }
133
+
134
+ // get_string returns a string as UTF-8 (null terminated), which must be freed,
135
+ // or NULL if it isn't a string (which has thrown an exception).
136
+ static char *get_string(napi_env env, napi_value value, size_t *length) {
137
+ if (napi_get_value_string_utf8(env, value, NULL, 0, length) != napi_ok) {
138
+ return NULL;
139
+ }
140
+
141
+ char *s = malloc(*length + 1);
142
+ if (s == NULL) {
143
+ napi_throw_error(env, NULL, "out of memory");
144
+ return NULL;
145
+ }
146
+ if (napi_get_value_string_utf8(env, value, s, *length + 1, length) != napi_ok) {
147
+ free(s);
148
+ return NULL;
149
+ }
150
+
151
+ return s;
152
+ }
153
+
154
+ static napi_value undefined(napi_env env) {
155
+ napi_value result;
156
+ napi_get_undefined(env, &result);
157
+ return result;
158
+ }
159
+
160
+ static napi_value number(napi_env env, double n) {
161
+ napi_value result;
162
+ NAPI_CALL(napi_create_double(env, n, &result));
163
+ return result;
164
+ }
165
+
166
+ // setErrorFactory(factory)
167
+ static napi_value set_error_factory(napi_env env, napi_callback_info info) {
168
+ ARGS(1);
169
+ instance *inst = NULL;
170
+ NAPI_CALL(napi_get_instance_data(env, (void **)&inst));
171
+ if (inst->error_factory != NULL) {
172
+ napi_delete_reference(env, inst->error_factory);
173
+ inst->error_factory = NULL;
174
+ }
175
+ NAPI_CALL(napi_create_reference(env, argv[0], 1, &inst->error_factory));
176
+
177
+ return undefined(env);
178
+ }
179
+
180
+ // allocHandle(handleType, inputHandle) -> handle
181
+ static napi_value alloc_handle(napi_env env, napi_callback_info info) {
182
+ ARGS(2);
183
+ int32_t handle_type, input;
184
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &handle_type));
185
+ NAPI_CALL(napi_get_value_int32(env, argv[1], &input));
186
+
187
+ SQLINTEGER output = 0;
188
+ SQLRETURN rc = SQLAllocHandle((SQLSMALLINT)handle_type, input, &output);
189
+
190
+ // An environment doesn't have an input handle with diagnostics.
191
+ if (handle_type == SQL_HANDLE_ENV) {
192
+ if (rc != SQL_SUCCESS) {
193
+ throw_error(env, "HY001", "can't allocate an environment", NAPI_AUTO_LENGTH);
194
+ return NULL;
195
+ }
196
+ } else {
197
+ CHECK(handle_type == SQL_HANDLE_DBC ? SQL_HANDLE_ENV : SQL_HANDLE_DBC, input, rc);
198
+ }
199
+
200
+ return number(env, output);
201
+ }
202
+
203
+ // freeHandle(handleType, handle)
204
+ static napi_value free_handle(napi_env env, napi_callback_info info) {
205
+ ARGS(2);
206
+ int32_t handle_type, handle;
207
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &handle_type));
208
+ NAPI_CALL(napi_get_value_int32(env, argv[1], &handle));
209
+
210
+ CHECK(handle_type, handle, SQLFreeHandle((SQLSMALLINT)handle_type, handle));
211
+ return undefined(env);
212
+ }
213
+
214
+ // connect(dbc, name)
215
+ static napi_value connect(napi_env env, napi_callback_info info) {
216
+ ARGS(2);
217
+ int32_t dbc;
218
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
219
+ size_t length;
220
+ char *name = get_string(env, argv[1], &length);
221
+ if (name == NULL) {
222
+ return NULL;
223
+ }
224
+
225
+ SQLRETURN rc = SQLConnect(dbc, (SQLCHAR *)name, (SQLSMALLINT)length, NULL, 0, NULL, 0);
226
+ free(name);
227
+ CHECK(SQL_HANDLE_DBC, dbc, rc);
228
+ return undefined(env);
229
+ }
230
+
231
+ // disconnect(dbc)
232
+ static napi_value disconnect(napi_env env, napi_callback_info info) {
233
+ ARGS(1);
234
+ int32_t dbc;
235
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
236
+
237
+ CHECK(SQL_HANDLE_DBC, dbc, SQLDisconnect(dbc));
238
+ return undefined(env);
239
+ }
240
+
241
+ // setAutocommit(dbc, on)
242
+ static napi_value set_autocommit(napi_env env, napi_callback_info info) {
243
+ ARGS(2);
244
+ int32_t dbc;
245
+ bool on;
246
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
247
+ NAPI_CALL(napi_get_value_bool(env, argv[1], &on));
248
+
249
+ // libvsql reads the value after SQLSetConnectAttr returns, so it can't be
250
+ // on the stack.
251
+ static SQLINTEGER autocommit_on = SQL_AUTOCOMMIT_ON, autocommit_off = SQL_AUTOCOMMIT_OFF;
252
+ CHECK(SQL_HANDLE_DBC, dbc,
253
+ SQLSetConnectAttr(dbc, SQL_ATTR_AUTOCOMMIT,
254
+ on ? (SQLPOINTER)&autocommit_on : (SQLPOINTER)&autocommit_off, 0));
255
+ return undefined(env);
256
+ }
257
+
258
+ // startTran(dbc, accessMode, isolationLevel)
259
+ static napi_value start_tran(napi_env env, napi_callback_info info) {
260
+ ARGS(3);
261
+ int32_t dbc, access_mode, isolation_level;
262
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
263
+ NAPI_CALL(napi_get_value_int32(env, argv[1], &access_mode));
264
+ NAPI_CALL(napi_get_value_int32(env, argv[2], &isolation_level));
265
+
266
+ CHECK(SQL_HANDLE_DBC, dbc, SQLStartTran(SQL_HANDLE_DBC, dbc, access_mode, isolation_level));
267
+ return undefined(env);
268
+ }
269
+
270
+ // endTran(dbc, commit)
271
+ static napi_value end_tran(napi_env env, napi_callback_info info) {
272
+ ARGS(2);
273
+ int32_t dbc;
274
+ bool commit;
275
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &dbc));
276
+ NAPI_CALL(napi_get_value_bool(env, argv[1], &commit));
277
+
278
+ CHECK(SQL_HANDLE_DBC, dbc, SQLEndTran(SQL_HANDLE_DBC, dbc, commit ? SQL_COMMIT : SQL_ROLLBACK));
279
+ return undefined(env);
280
+ }
281
+
282
+ // execDirect(stmt, query)
283
+ static napi_value exec_direct(napi_env env, napi_callback_info info) {
284
+ ARGS(2);
285
+ int32_t stmt;
286
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
287
+ size_t length;
288
+ char *query = get_string(env, argv[1], &length);
289
+ if (query == NULL) {
290
+ return NULL;
291
+ }
292
+
293
+ SQLRETURN rc = SQLExecDirect(stmt, (SQLCHAR *)query, (SQLINTEGER)length);
294
+ free(query);
295
+ CHECK(SQL_HANDLE_STMT, stmt, rc);
296
+ return undefined(env);
297
+ }
298
+
299
+ // prepare(stmt, query)
300
+ static napi_value prepare(napi_env env, napi_callback_info info) {
301
+ ARGS(2);
302
+ int32_t stmt;
303
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
304
+ size_t length;
305
+ char *query = get_string(env, argv[1], &length);
306
+ if (query == NULL) {
307
+ return NULL;
308
+ }
309
+
310
+ SQLRETURN rc = SQLPrepare(stmt, (SQLCHAR *)query, (SQLINTEGER)length);
311
+ free(query);
312
+ CHECK(SQL_HANDLE_STMT, stmt, rc);
313
+ return undefined(env);
314
+ }
315
+
316
+ // A parameter's value, which must stay allocated until SQLExecute has read it.
317
+ typedef struct {
318
+ SQLBIGINT i;
319
+ SQLDOUBLE d;
320
+ char *s;
321
+ SQLINTEGER ind;
322
+ } param;
323
+
324
+ // characters is the number of characters (code points) in UTF-8.
325
+ static SQLINTEGER characters(const char *s, size_t length) {
326
+ SQLINTEGER n = 0;
327
+ for (size_t i = 0; i < length; i++) {
328
+ // Each character has one byte that isn't a continuation byte (10xxxxxx).
329
+ if (((unsigned char)s[i] & 0xC0) != 0x80) {
330
+ n++;
331
+ }
332
+ }
333
+
334
+ return n;
335
+ }
336
+
337
+ // bind_param binds one parameter, an array of [valueType, parameterType,
338
+ // columnSize, decimalDigits, value], where the value is null, a number or
339
+ // bigint (SQL_BIGINT), a number (SQL_DOUBLE) or a string (SQL_CHAR). A
340
+ // columnSize of 0 for a string is its number of characters (at least 1).
341
+ static int bind_param(napi_env env, SQLINTEGER stmt, SQLSMALLINT n, napi_value array, param *p) {
342
+ napi_value fields[5];
343
+ for (uint32_t i = 0; i < 5; i++) {
344
+ if (napi_get_element(env, array, i, &fields[i]) != napi_ok) {
345
+ return -1;
346
+ }
347
+ }
348
+
349
+ int32_t value_type, parameter_type, column_size, decimal_digits;
350
+ if (napi_get_value_int32(env, fields[0], &value_type) != napi_ok ||
351
+ napi_get_value_int32(env, fields[1], &parameter_type) != napi_ok ||
352
+ napi_get_value_int32(env, fields[2], &column_size) != napi_ok ||
353
+ napi_get_value_int32(env, fields[3], &decimal_digits) != napi_ok) {
354
+ return -1;
355
+ }
356
+
357
+ napi_valuetype type;
358
+ if (napi_typeof(env, fields[4], &type) != napi_ok) {
359
+ return -1;
360
+ }
361
+
362
+ SQLPOINTER pointer = NULL;
363
+ SQLINTEGER buffer_length = 0;
364
+ if (type == napi_null || type == napi_undefined) {
365
+ p->ind = SQL_NULL_DATA;
366
+ } else if (value_type == SQL_BIGINT) {
367
+ if (type == napi_bigint) {
368
+ bool lossless;
369
+ if (napi_get_value_bigint_int64(env, fields[4], &p->i, &lossless) != napi_ok) {
370
+ return -1;
371
+ }
372
+ } else if (napi_get_value_int64(env, fields[4], &p->i) != napi_ok) {
373
+ return -1;
374
+ }
375
+ pointer = &p->i;
376
+ buffer_length = sizeof(SQLBIGINT);
377
+ } else if (value_type == SQL_DOUBLE) {
378
+ if (napi_get_value_double(env, fields[4], &p->d) != napi_ok) {
379
+ return -1;
380
+ }
381
+ pointer = &p->d;
382
+ buffer_length = sizeof(SQLDOUBLE);
383
+ } else {
384
+ size_t length;
385
+ p->s = get_string(env, fields[4], &length);
386
+ if (p->s == NULL) {
387
+ return -1;
388
+ }
389
+ if (column_size == 0) {
390
+ column_size = characters(p->s, length);
391
+ if (column_size == 0) {
392
+ column_size = 1;
393
+ }
394
+ }
395
+ pointer = p->s;
396
+ buffer_length = (SQLINTEGER)length + 1;
397
+ p->ind = (SQLINTEGER)length;
398
+ }
399
+
400
+ return check(env,
401
+ SQLBindParameter(stmt, n, SQL_PARAM_MODE_IN, (SQLSMALLINT)value_type,
402
+ (SQLSMALLINT)parameter_type, column_size,
403
+ (SQLSMALLINT)decimal_digits, pointer, buffer_length, &p->ind),
404
+ SQL_HANDLE_STMT, stmt);
405
+ }
406
+
407
+ // execute(stmt, params) executes the prepared statement with the parameters
408
+ // (see bind_param), which are bound for this execution only.
409
+ static napi_value execute(napi_env env, napi_callback_info info) {
410
+ ARGS(2);
411
+ int32_t stmt;
412
+ uint32_t n;
413
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
414
+ NAPI_CALL(napi_get_array_length(env, argv[1], &n));
415
+
416
+ param *params = calloc(n ? n : 1, sizeof(param));
417
+ if (params == NULL) {
418
+ napi_throw_error(env, NULL, "out of memory");
419
+ return NULL;
420
+ }
421
+
422
+ napi_value result = NULL;
423
+ for (uint32_t i = 0; i < n; i++) {
424
+ napi_value array;
425
+ if (napi_get_element(env, argv[1], i, &array) != napi_ok ||
426
+ bind_param(env, stmt, (SQLSMALLINT)(i + 1), array, &params[i]) < 0) {
427
+ goto done;
428
+ }
429
+ }
430
+
431
+ if (check(env, SQLExecute(stmt), SQL_HANDLE_STMT, stmt) == 0) {
432
+ result = undefined(env);
433
+ }
434
+
435
+ done:
436
+ // The values are freed, so they can't stay bound for the next execution.
437
+ if (n > 0) {
438
+ SQLFreeStmt(stmt, SQL_RESET_PARAMS);
439
+ }
440
+ for (uint32_t i = 0; i < n; i++) {
441
+ free(params[i].s);
442
+ }
443
+ free(params);
444
+
445
+ return result;
446
+ }
447
+
448
+ // rowCount(stmt) -> number
449
+ static napi_value row_count(napi_env env, napi_callback_info info) {
450
+ ARGS(1);
451
+ int32_t stmt;
452
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
453
+
454
+ SQLINTEGER n = 0;
455
+ CHECK(SQL_HANDLE_STMT, stmt, SQLRowCount(stmt, &n));
456
+ return number(env, n);
457
+ }
458
+
459
+ // numResultCols(stmt) -> number
460
+ static napi_value num_result_cols(napi_env env, napi_callback_info info) {
461
+ ARGS(1);
462
+ int32_t stmt;
463
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
464
+
465
+ SQLSMALLINT n = 0;
466
+ CHECK(SQL_HANDLE_STMT, stmt, SQLNumResultCols(stmt, &n));
467
+ return number(env, n);
468
+ }
469
+
470
+ // describeCol(stmt, n) -> [name, dataType, columnSize, decimalDigits,
471
+ // nullable]
472
+ static napi_value describe_col(napi_env env, napi_callback_info info) {
473
+ ARGS(2);
474
+ int32_t stmt, n;
475
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
476
+ NAPI_CALL(napi_get_value_int32(env, argv[1], &n));
477
+
478
+ SQLSMALLINT size = 129, length = 0, data_type = 0, decimal_digits = 0, nullable = 0;
479
+ SQLINTEGER column_size = 0;
480
+ for (;;) {
481
+ SQLCHAR *name = malloc(size);
482
+ if (name == NULL) {
483
+ napi_throw_error(env, NULL, "out of memory");
484
+ return NULL;
485
+ }
486
+
487
+ SQLRETURN rc = SQLDescribeCol(stmt, (SQLSMALLINT)n, name, size, &length, &data_type,
488
+ &column_size, &decimal_digits, &nullable);
489
+ if (check(env, rc, SQL_HANDLE_STMT, stmt) < 0) {
490
+ free(name);
491
+ return NULL;
492
+ }
493
+
494
+ // The name was truncated.
495
+ if (length >= size) {
496
+ size = length + 1;
497
+ free(name);
498
+ continue;
499
+ }
500
+
501
+ napi_value result, values[5];
502
+ napi_status status = napi_create_string_utf8(env, (const char *)name, length, &values[0]);
503
+ free(name);
504
+ NAPI_CALL(status);
505
+ NAPI_CALL(napi_create_int32(env, data_type, &values[1]));
506
+ NAPI_CALL(napi_create_int32(env, column_size, &values[2]));
507
+ NAPI_CALL(napi_create_int32(env, decimal_digits, &values[3]));
508
+ NAPI_CALL(napi_create_int32(env, nullable, &values[4]));
509
+ NAPI_CALL(napi_create_array_with_length(env, 5, &result));
510
+ for (uint32_t i = 0; i < 5; i++) {
511
+ NAPI_CALL(napi_set_element(env, result, i, values[i]));
512
+ }
513
+
514
+ return result;
515
+ }
516
+ }
517
+
518
+ // closeCursor(stmt)
519
+ static napi_value close_cursor(napi_env env, napi_callback_info info) {
520
+ ARGS(1);
521
+ int32_t stmt;
522
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
523
+
524
+ CHECK(SQL_HANDLE_STMT, stmt, SQLCloseCursor(stmt));
525
+ return undefined(env);
526
+ }
527
+
528
+ // get_chars reads a column as SQL_CHAR into a string, or null. A string that
529
+ // doesn't fit in the buffer is read in pieces, into a buffer that's the size of
530
+ // the rest of it.
531
+ static napi_value get_chars(napi_env env, SQLINTEGER stmt, SQLSMALLINT n) {
532
+ char small[256];
533
+ char *buf = small, *large = NULL;
534
+ SQLINTEGER size = sizeof(small), ind = 0;
535
+ size_t length = 0;
536
+ for (;;) {
537
+ SQLRETURN rc = SQLGetData(stmt, n, SQL_CHAR, buf + length, size - (SQLINTEGER)length, &ind);
538
+ if (check(env, rc, SQL_HANDLE_STMT, stmt) < 0) {
539
+ free(large);
540
+ return NULL;
541
+ }
542
+ if (ind == SQL_NULL_DATA) {
543
+ free(large);
544
+ napi_value result;
545
+ NAPI_CALL(napi_get_null(env, &result));
546
+ return result;
547
+ }
548
+
549
+ // The rest of the string (ind) didn't fit, so the buffer has a null
550
+ // terminated piece of it.
551
+ if (rc == SQL_SUCCESS_WITH_INFO && ind >= size - (SQLINTEGER)length) {
552
+ size_t piece = strlen(buf + length);
553
+ size_t new_size = length + piece + (size_t)ind + 1;
554
+ char *grown = malloc(new_size);
555
+ if (grown == NULL) {
556
+ free(large);
557
+ napi_throw_error(env, NULL, "out of memory");
558
+ return NULL;
559
+ }
560
+ memcpy(grown, buf, length + piece);
561
+ free(large);
562
+ buf = large = grown;
563
+ length += piece;
564
+ size = (SQLINTEGER)new_size;
565
+ continue;
566
+ }
567
+
568
+ napi_value result;
569
+ napi_status status = napi_create_string_utf8(env, buf, length + ind, &result);
570
+ free(large);
571
+ NAPI_CALL(status);
572
+ return result;
573
+ }
574
+ }
575
+
576
+ // get_value reads a column of the current row (see fetch for the kinds).
577
+ static napi_value get_value(napi_env env, SQLINTEGER stmt, SQLSMALLINT column, char kind) {
578
+ SQLINTEGER ind = 0;
579
+ napi_value result;
580
+ switch (kind) {
581
+ case 'i':
582
+ case 'I':
583
+ case 'b': {
584
+ SQLBIGINT v = 0;
585
+ CHECK(SQL_HANDLE_STMT, stmt, SQLGetData(stmt, column, SQL_BIGINT, &v, sizeof(v), &ind));
586
+ if (ind == SQL_NULL_DATA) {
587
+ break;
588
+ }
589
+ if (kind == 'b') {
590
+ NAPI_CALL(napi_create_bigint_int64(env, v, &result));
591
+ return result;
592
+ }
593
+ if (v > MAX_SAFE_INTEGER || v < -MAX_SAFE_INTEGER) {
594
+ napi_throw_range_error(env, NULL,
595
+ "a BIGINT is out of the range of a number, connect with "
596
+ "{ bigint: true } to read it as a bigint");
597
+ return NULL;
598
+ }
599
+ return number(env, (double)v);
600
+ }
601
+ case 'd': {
602
+ SQLDOUBLE v = 0;
603
+ CHECK(SQL_HANDLE_STMT, stmt, SQLGetData(stmt, column, SQL_DOUBLE, &v, sizeof(v), &ind));
604
+ if (ind == SQL_NULL_DATA) {
605
+ break;
606
+ }
607
+ return number(env, v);
608
+ }
609
+ case 'r': {
610
+ SQLREAL v = 0;
611
+ CHECK(SQL_HANDLE_STMT, stmt, SQLGetData(stmt, column, SQL_REAL, &v, sizeof(v), &ind));
612
+ if (ind == SQL_NULL_DATA) {
613
+ break;
614
+ }
615
+ return number(env, v);
616
+ }
617
+ case 'o': {
618
+ // TRUE or FALSE.
619
+ char v[6] = {0};
620
+ CHECK(SQL_HANDLE_STMT, stmt, SQLGetData(stmt, column, SQL_CHAR, v, sizeof(v), &ind));
621
+ if (ind == SQL_NULL_DATA) {
622
+ break;
623
+ }
624
+ NAPI_CALL(napi_get_boolean(env, v[0] == 'T', &result));
625
+ return result;
626
+ }
627
+ default:
628
+ return get_chars(env, stmt, column);
629
+ }
630
+
631
+ NAPI_CALL(napi_get_null(env, &result));
632
+ return result;
633
+ }
634
+
635
+ // fetch(stmt, kinds, max) -> rows fetches the next rows (at most max, or all
636
+ // of them if max is 0), each as an array. Fewer than max rows means that there
637
+ // are no more. kinds has a character for how each column is read:
638
+ //
639
+ // i SQL_BIGINT as a number
640
+ // I SQL_BIGINT as a number (a RangeError if it doesn't fit)
641
+ // b SQL_BIGINT as a bigint
642
+ // d SQL_DOUBLE as a number
643
+ // r SQL_REAL as a number
644
+ // o SQL_CHAR as a boolean
645
+ // s SQL_CHAR as a string
646
+ //
647
+ // A NULL is null.
648
+ static napi_value fetch(napi_env env, napi_callback_info info) {
649
+ ARGS(3);
650
+ int32_t stmt, max;
651
+ NAPI_CALL(napi_get_value_int32(env, argv[0], &stmt));
652
+ NAPI_CALL(napi_get_value_int32(env, argv[2], &max));
653
+ char kinds[1024];
654
+ size_t n;
655
+ NAPI_CALL(napi_get_value_string_latin1(env, argv[1], kinds, sizeof(kinds), &n));
656
+ if (n >= sizeof(kinds) - 1) {
657
+ napi_throw_range_error(env, NULL, "too many columns");
658
+ return NULL;
659
+ }
660
+
661
+ napi_value rows;
662
+ NAPI_CALL(napi_create_array(env, &rows));
663
+ for (uint32_t r = 0; max == 0 || r < (uint32_t)max; r++) {
664
+ SQLRETURN rc = SQLFetch(stmt);
665
+ if (rc == SQL_NO_DATA) {
666
+ break;
667
+ }
668
+ CHECK(SQL_HANDLE_STMT, stmt, rc);
669
+
670
+ napi_value row;
671
+ NAPI_CALL(napi_create_array_with_length(env, n, &row));
672
+ for (size_t i = 0; i < n; i++) {
673
+ napi_value value = get_value(env, stmt, (SQLSMALLINT)(i + 1), kinds[i]);
674
+ if (value == NULL) {
675
+ return NULL;
676
+ }
677
+ NAPI_CALL(napi_set_element(env, row, (uint32_t)i, value));
678
+ }
679
+ NAPI_CALL(napi_set_element(env, rows, r, row));
680
+ }
681
+
682
+ return rows;
683
+ }
684
+
685
+ static void free_instance(napi_env env, void *data, void *hint) {
686
+ instance *inst = data;
687
+ if (inst->error_factory != NULL) {
688
+ napi_delete_reference(env, inst->error_factory);
689
+ }
690
+ free(inst);
691
+ }
692
+
693
+ // The constants that the JavaScript code needs.
694
+ static const struct {
695
+ const char *name;
696
+ int32_t value;
697
+ } constants[] = {
698
+ {"SQL_HANDLE_ENV", SQL_HANDLE_ENV},
699
+ {"SQL_HANDLE_DBC", SQL_HANDLE_DBC},
700
+ {"SQL_HANDLE_STMT", SQL_HANDLE_STMT},
701
+ {"SQL_NULL_HANDLE", SQL_NULL_HANDLE},
702
+ {"SQL_CHAR", SQL_CHAR},
703
+ {"SQL_NUMERIC", SQL_NUMERIC},
704
+ {"SQL_DECIMAL", SQL_DECIMAL},
705
+ {"SQL_INTEGER", SQL_INTEGER},
706
+ {"SQL_SMALLINT", SQL_SMALLINT},
707
+ {"SQL_FLOAT", SQL_FLOAT},
708
+ {"SQL_REAL", SQL_REAL},
709
+ {"SQL_DOUBLE", SQL_DOUBLE},
710
+ {"SQL_VARCHAR", SQL_VARCHAR},
711
+ {"SQL_BOOLEAN", SQL_BOOLEAN},
712
+ {"SQL_BIGINT", SQL_BIGINT},
713
+ {"SQL_TYPE_DATE", SQL_TYPE_DATE},
714
+ {"SQL_TYPE_TIME", SQL_TYPE_TIME},
715
+ {"SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP},
716
+ {"SQL_TYPE_TIME_WITH_TIMEZONE", SQL_TYPE_TIME_WITH_TIMEZONE},
717
+ {"SQL_TYPE_TIMESTAMP_WITH_TIMEZONE", SQL_TYPE_TIMESTAMP_WITH_TIMEZONE},
718
+ {"SQL_NO_NULLS", SQL_NO_NULLS},
719
+ {"SQL_NULLABLE", SQL_NULLABLE},
720
+ {"SQL_TRANSACTION_READ_WRITE", SQL_TRANSACTION_READ_WRITE},
721
+ {"SQL_TRANSACTION_READ_COMMITTED", SQL_TRANSACTION_READ_COMMITTED},
722
+ {NULL, 0},
723
+ };
724
+
725
+ NAPI_MODULE_INIT() {
726
+ // The thread that loaded the addon is the only one that's registered with
727
+ // the garbage collector, and only until the first time the addon is
728
+ // initialized (by any thread).
729
+ static int gc_initialized = 0;
730
+ if (!gc_initialized) {
731
+ gc_initialized = 1;
732
+ vsql_gc_after_node_init();
733
+ }
734
+
735
+ instance *inst = calloc(1, sizeof(instance));
736
+ if (inst == NULL) {
737
+ napi_throw_error(env, NULL, "out of memory");
738
+ return NULL;
739
+ }
740
+ if (napi_set_instance_data(env, inst, free_instance, NULL) != napi_ok) {
741
+ free(inst);
742
+ return NULL;
743
+ }
744
+
745
+ napi_property_descriptor functions[] = {
746
+ {"setErrorFactory", NULL, set_error_factory, NULL, NULL, NULL, napi_default, NULL},
747
+ {"allocHandle", NULL, alloc_handle, NULL, NULL, NULL, napi_default, NULL},
748
+ {"freeHandle", NULL, free_handle, NULL, NULL, NULL, napi_default, NULL},
749
+ {"connect", NULL, connect, NULL, NULL, NULL, napi_default, NULL},
750
+ {"disconnect", NULL, disconnect, NULL, NULL, NULL, napi_default, NULL},
751
+ {"setAutocommit", NULL, set_autocommit, NULL, NULL, NULL, napi_default, NULL},
752
+ {"startTran", NULL, start_tran, NULL, NULL, NULL, napi_default, NULL},
753
+ {"endTran", NULL, end_tran, NULL, NULL, NULL, napi_default, NULL},
754
+ {"execDirect", NULL, exec_direct, NULL, NULL, NULL, napi_default, NULL},
755
+ {"prepare", NULL, prepare, NULL, NULL, NULL, napi_default, NULL},
756
+ {"execute", NULL, execute, NULL, NULL, NULL, napi_default, NULL},
757
+ {"rowCount", NULL, row_count, NULL, NULL, NULL, napi_default, NULL},
758
+ {"numResultCols", NULL, num_result_cols, NULL, NULL, NULL, napi_default, NULL},
759
+ {"describeCol", NULL, describe_col, NULL, NULL, NULL, napi_default, NULL},
760
+ {"closeCursor", NULL, close_cursor, NULL, NULL, NULL, napi_default, NULL},
761
+ {"fetch", NULL, fetch, NULL, NULL, NULL, napi_default, NULL},
762
+ };
763
+ NAPI_CALL(napi_define_properties(env, exports, sizeof(functions) / sizeof(functions[0]),
764
+ functions));
765
+
766
+ for (int i = 0; constants[i].name != NULL; i++) {
767
+ napi_value value;
768
+ NAPI_CALL(napi_create_int32(env, constants[i].value, &value));
769
+ NAPI_CALL(napi_set_named_property(env, exports, constants[i].name, value));
770
+ }
771
+
772
+ return exports;
773
+ }