drizzle-sybase 3.2.0 → 4.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.
- package/README.md +62 -1
- package/binding.gyp +2 -1
- package/dist/connection.d.ts +15 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +46 -2
- package/dist/connection.js.map +1 -1
- package/dist/db.d.ts +29 -9
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +9 -6
- package/dist/db.js.map +1 -1
- package/dist/dialect.d.ts +6 -0
- package/dist/dialect.d.ts.map +1 -1
- package/dist/dialect.js +38 -2
- package/dist/dialect.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/native/index.d.ts +19 -1
- package/dist/native/index.d.ts.map +1 -1
- package/dist/native/index.js +21 -4
- package/dist/native/index.js.map +1 -1
- package/dist/native/sybase_native.darwin-arm64.node +0 -0
- package/dist/native/sybase_native.darwin-x64.node +0 -0
- package/dist/native/sybase_native.linux-arm64.node +0 -0
- package/dist/native/sybase_native.linux-x64.node +0 -0
- package/dist/native/sybase_native.win32-x64.node +0 -0
- package/dist/pool.d.ts +7 -2
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +9 -1
- package/dist/pool.js.map +1 -1
- package/dist/result.d.ts +40 -2
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +28 -20
- package/dist/result.js.map +1 -1
- package/dist/session.d.ts +14 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +18 -0
- package/dist/session.js.map +1 -1
- package/package.json +1 -1
- package/src/native/binding.c +758 -54
package/src/native/binding.c
CHANGED
|
@@ -45,12 +45,19 @@
|
|
|
45
45
|
#include <sybfront.h>
|
|
46
46
|
#include <sybdb.h>
|
|
47
47
|
#include <uv.h>
|
|
48
|
+
#include <errno.h>
|
|
48
49
|
#include <limits.h>
|
|
49
50
|
#include <stdarg.h>
|
|
50
51
|
#include <stdint.h>
|
|
51
52
|
#include <string.h>
|
|
52
53
|
#include <stdlib.h>
|
|
53
54
|
#include <stdio.h>
|
|
55
|
+
#if defined(_WIN32)
|
|
56
|
+
#include <windows.h>
|
|
57
|
+
#else
|
|
58
|
+
#include <fcntl.h>
|
|
59
|
+
#include <unistd.h>
|
|
60
|
+
#endif
|
|
54
61
|
|
|
55
62
|
// ---------------------------------------------------------------------------
|
|
56
63
|
// Constants
|
|
@@ -149,6 +156,12 @@ typedef struct {
|
|
|
149
156
|
uv_mutex_t lock;
|
|
150
157
|
int in_flight; // # of queries pinning this connection, from enqueue to completion
|
|
151
158
|
int pending_close; // 1 if fn_close was called while in_flight > 0
|
|
159
|
+
// 1 when the server's character set is iso_1: its single-byte
|
|
160
|
+
// text is Latin-1, decoded to UTF-8 on the way in and encoded on the way out.
|
|
161
|
+
// Fixed once the connection is open; see detect_server_charset.
|
|
162
|
+
int latin1;
|
|
163
|
+
// The server's default character set name, as syscharsets spells it.
|
|
164
|
+
char charset[32];
|
|
152
165
|
} SybaseConnection;
|
|
153
166
|
|
|
154
167
|
// Forward declarations.
|
|
@@ -288,6 +301,37 @@ static ConnHandle *get_conn_handle(napi_env env, napi_value value) {
|
|
|
288
301
|
// state via dbgetuserdata().
|
|
289
302
|
// ---------------------------------------------------------------------------
|
|
290
303
|
|
|
304
|
+
/**
|
|
305
|
+
* Latin-1 bytes as UTF-8: one code point per byte, so it cannot fail and loses
|
|
306
|
+
* nothing. `out` needs room for 2 bytes per input byte. Returns the length.
|
|
307
|
+
*/
|
|
308
|
+
static size_t latin1_to_utf8(const unsigned char *in, size_t n, unsigned char *out) {
|
|
309
|
+
size_t len = 0;
|
|
310
|
+
for (size_t i = 0; i < n; i++) {
|
|
311
|
+
unsigned char b = in[i];
|
|
312
|
+
if (b < 0x80) {
|
|
313
|
+
out[len++] = b;
|
|
314
|
+
} else {
|
|
315
|
+
out[len++] = (unsigned char)(0xC0 | (b >> 6));
|
|
316
|
+
out[len++] = (unsigned char)(0x80 | (b & 0x3F));
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return len;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** `text` as a new NUL-terminated string, decoded from Latin-1 when `latin1` is set. */
|
|
323
|
+
static char *dup_text(const char *text, int latin1) {
|
|
324
|
+
if (!latin1) {
|
|
325
|
+
return strdup(text);
|
|
326
|
+
}
|
|
327
|
+
size_t n = strlen(text);
|
|
328
|
+
char *out = (char *)malloc(n * 2 + 1);
|
|
329
|
+
if (out) {
|
|
330
|
+
out[latin1_to_utf8((const unsigned char *)text, n, (unsigned char *)out)] = '\0';
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
|
|
291
335
|
static int err_handler(DBPROCESS *dbproc, int severity, int dberr,
|
|
292
336
|
int oserr, char *dberrstr, char *oserrstr) {
|
|
293
337
|
(void)oserr;
|
|
@@ -354,9 +398,12 @@ static int msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
|
|
|
354
398
|
if (conn) {
|
|
355
399
|
size_t cur_len = strlen(conn->last_error);
|
|
356
400
|
if (cur_len < ERR_BUF_SIZE - 200) {
|
|
401
|
+
// Server messages quote names and values in the server's character set.
|
|
402
|
+
char *text = dup_text(msgtext ? msgtext : "", conn->latin1);
|
|
357
403
|
snprintf(conn->last_error + cur_len, ERR_BUF_SIZE - cur_len,
|
|
358
404
|
"Msg %ld, Level %d, State %d: %s\n",
|
|
359
|
-
(long)msgno, severity, msgstate, msgtext);
|
|
405
|
+
(long)msgno, severity, msgstate, text ? text : msgtext);
|
|
406
|
+
free(text);
|
|
360
407
|
}
|
|
361
408
|
conn->has_error = 1;
|
|
362
409
|
}
|
|
@@ -421,18 +468,22 @@ typedef struct {
|
|
|
421
468
|
char *name;
|
|
422
469
|
int type;
|
|
423
470
|
ValueKind kind;
|
|
471
|
+
// Single-byte text in the server's Latin-1, to decode (see SybaseConnection.latin1).
|
|
472
|
+
int latin1;
|
|
424
473
|
// One byte per row, 1 where the value is NULL.
|
|
425
474
|
unsigned char *nulls;
|
|
426
475
|
int has_nulls;
|
|
427
476
|
// Fixed-width kinds: one value per row (0 where NULL).
|
|
428
477
|
unsigned char *fixed;
|
|
429
478
|
// Text and binary: the values back to back, and the offset just past each
|
|
430
|
-
// one.
|
|
431
|
-
// the `\0`-joined string JS splits.
|
|
479
|
+
// one. Text is always valid UTF-8 here (see store_text), and may hold NULs.
|
|
432
480
|
char *bytes;
|
|
433
481
|
size_t bytes_len;
|
|
434
482
|
size_t bytes_cap;
|
|
435
483
|
size_t *ends;
|
|
484
|
+
// Text: each value's length in UTF-16 code units, which is how JS cuts a run
|
|
485
|
+
// of values back into one string per value.
|
|
486
|
+
uint32_t *units;
|
|
436
487
|
} ResultColumn;
|
|
437
488
|
|
|
438
489
|
/** Whether a column carries raw bytes, handed to JS as a Buffer. */
|
|
@@ -659,6 +710,7 @@ static void free_columns(ResultColumn *columns, int num_columns) {
|
|
|
659
710
|
free(columns[i].fixed);
|
|
660
711
|
free(columns[i].bytes);
|
|
661
712
|
free(columns[i].ends);
|
|
713
|
+
free(columns[i].units);
|
|
662
714
|
free(columns[i].name);
|
|
663
715
|
}
|
|
664
716
|
free(columns);
|
|
@@ -683,6 +735,62 @@ typedef struct {
|
|
|
683
735
|
int success;
|
|
684
736
|
} ConnectWork;
|
|
685
737
|
|
|
738
|
+
/**
|
|
739
|
+
* Learn the server's default character set and set `conn->latin1` from it.
|
|
740
|
+
*
|
|
741
|
+
* Single-byte text (char, varchar, text) reaches the client as the server's own
|
|
742
|
+
* bytes: ASE converts only to a character set it has installed, and FreeTDS only
|
|
743
|
+
* when the server announces one, which ASE does not do here. So the bytes have
|
|
744
|
+
* to be decoded knowing what they are. iso_1 bytes are Latin-1,
|
|
745
|
+
* decoded one byte to one code point — losslessly, and the way other readers of
|
|
746
|
+
* the same data (e.g. a Postgres replica loaded from it) decode them. A utf8
|
|
747
|
+
* server's bytes are already UTF-8. Any other character set is passed through
|
|
748
|
+
* as it always was; the JS layer warns about it (see `serverCharset`).
|
|
749
|
+
*
|
|
750
|
+
* Returns -1 (with `error` set) when the character set cannot be read: decoding
|
|
751
|
+
* text without knowing it would silently garble every non-ASCII value.
|
|
752
|
+
*/
|
|
753
|
+
static int detect_server_charset(SybaseConnection *conn, char *error, size_t error_size) {
|
|
754
|
+
conn->latin1 = 0;
|
|
755
|
+
conn->charset[0] = '\0';
|
|
756
|
+
clear_error(conn);
|
|
757
|
+
int ok = dbcmd(conn->dbproc,
|
|
758
|
+
"select name from master..syscharsets where id = "
|
|
759
|
+
"(select value from master..sysconfigures where config = 131)") != FAIL &&
|
|
760
|
+
dbsqlexec(conn->dbproc) != FAIL;
|
|
761
|
+
RETCODE rc = SUCCEED;
|
|
762
|
+
while (ok && (rc = dbresults(conn->dbproc)) != NO_MORE_RESULTS) {
|
|
763
|
+
if (rc == FAIL) {
|
|
764
|
+
ok = 0;
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
RETCODE row;
|
|
768
|
+
while ((row = dbnextrow(conn->dbproc)) == REG_ROW) {
|
|
769
|
+
int len = dbdatlen(conn->dbproc, 1);
|
|
770
|
+
const char *name = (const char *)dbdata(conn->dbproc, 1);
|
|
771
|
+
if (name && len > 0 && (size_t)len < sizeof(conn->charset)) {
|
|
772
|
+
memcpy(conn->charset, name, (size_t)len);
|
|
773
|
+
conn->charset[len] = '\0';
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (row == FAIL) {
|
|
777
|
+
ok = 0;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
if (!ok || conn->has_error || conn->charset[0] == '\0') {
|
|
781
|
+
// Bounded: the server's message may fill the whole buffer on its own.
|
|
782
|
+
snprintf(error, error_size, "Could not read the server's character set: %.3000s",
|
|
783
|
+
conn->has_error ? conn->last_error : "no name in master..syscharsets");
|
|
784
|
+
if (!DBDEAD(conn->dbproc)) {
|
|
785
|
+
dbcancel(conn->dbproc);
|
|
786
|
+
}
|
|
787
|
+
return -1;
|
|
788
|
+
}
|
|
789
|
+
conn->latin1 = strcmp(conn->charset, "iso_1") == 0;
|
|
790
|
+
clear_error(conn);
|
|
791
|
+
return 0;
|
|
792
|
+
}
|
|
793
|
+
|
|
686
794
|
static void connect_execute(napi_env env, void *data) {
|
|
687
795
|
(void)env;
|
|
688
796
|
ConnectWork *w = (ConnectWork *)data;
|
|
@@ -715,6 +823,10 @@ static void connect_execute(napi_env env, void *data) {
|
|
|
715
823
|
DBSETLUSER(w->conn->login, w->username);
|
|
716
824
|
DBSETLPWD(w->conn->login, w->password);
|
|
717
825
|
DBSETLAPP(w->conn->login, "drizzle-sybase");
|
|
826
|
+
// unichar/univarchar arrive as UTF-16 and FreeTDS converts them to this. Set
|
|
827
|
+
// explicitly, or FreeTDS takes it from the process locale — which is "C" in
|
|
828
|
+
// Lambda, where a non-ASCII unichar value then comes back as '?'.
|
|
829
|
+
DBSETLCHARSET(w->conn->login, "UTF-8");
|
|
718
830
|
|
|
719
831
|
// Set TDS version to 5.0 (Sybase ASE native)
|
|
720
832
|
DBSETLVERSION(w->conn->login, DBVERSION_100);
|
|
@@ -789,6 +901,15 @@ static void connect_execute(napi_env env, void *data) {
|
|
|
789
901
|
}
|
|
790
902
|
}
|
|
791
903
|
|
|
904
|
+
if (detect_server_charset(w->conn, w->error, sizeof(w->error)) != 0) {
|
|
905
|
+
dbclose(w->conn->dbproc);
|
|
906
|
+
dbloginfree(w->conn->login);
|
|
907
|
+
uv_mutex_destroy(&w->conn->lock);
|
|
908
|
+
free(w->conn);
|
|
909
|
+
w->conn = NULL;
|
|
910
|
+
w->success = 0;
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
792
913
|
w->success = 1;
|
|
793
914
|
}
|
|
794
915
|
|
|
@@ -937,6 +1058,16 @@ typedef struct {
|
|
|
937
1058
|
// 1 to hand datetime/smalldatetime over as wall-clock numbers (KIND_WALLCLOCK)
|
|
938
1059
|
// for the JS layer to turn into Dates, instead of as canonical text.
|
|
939
1060
|
int wall_clock_datetimes;
|
|
1061
|
+
// Set to stream the result to a CSV file instead of collecting it (see
|
|
1062
|
+
// csv_write_row); the rows are then never held, so no default row cap applies.
|
|
1063
|
+
char *out_path;
|
|
1064
|
+
// Written first, renamed to out_path only once the whole result is in it.
|
|
1065
|
+
char *out_tmp;
|
|
1066
|
+
FILE *out;
|
|
1067
|
+
char *out_buf;
|
|
1068
|
+
size_t out_len;
|
|
1069
|
+
int64_t out_rows;
|
|
1070
|
+
int64_t out_bytes;
|
|
940
1071
|
// Results
|
|
941
1072
|
ResultColumn *columns;
|
|
942
1073
|
int num_columns;
|
|
@@ -1306,6 +1437,13 @@ static int grow_rows(ResultColumn *columns, int num_columns, int capacity) {
|
|
|
1306
1437
|
return -1;
|
|
1307
1438
|
}
|
|
1308
1439
|
col->ends = ends;
|
|
1440
|
+
if (col->kind == KIND_TEXT) {
|
|
1441
|
+
uint32_t *units = (uint32_t *)realloc(col->units, (size_t)capacity * sizeof(uint32_t));
|
|
1442
|
+
if (!units) {
|
|
1443
|
+
return -1;
|
|
1444
|
+
}
|
|
1445
|
+
col->units = units;
|
|
1446
|
+
}
|
|
1309
1447
|
}
|
|
1310
1448
|
}
|
|
1311
1449
|
return 0;
|
|
@@ -1402,23 +1540,103 @@ static int store_fixed(QueryWork *w, ResultColumn *col, int coltype, const BYTE
|
|
|
1402
1540
|
return 0;
|
|
1403
1541
|
}
|
|
1404
1542
|
|
|
1543
|
+
/**
|
|
1544
|
+
* Copy text as valid UTF-8, replacing each maximal invalid subsequence with
|
|
1545
|
+
* U+FFFD — the WHATWG decoder's rule, which is what V8 applies when the same
|
|
1546
|
+
* bytes become a JS string, so a file and a row result hold the same value.
|
|
1547
|
+
* `out` needs room for 3 bytes per input byte. Returns the length written.
|
|
1548
|
+
*/
|
|
1549
|
+
static size_t utf8_sanitize(const unsigned char *in, size_t n, unsigned char *out) {
|
|
1550
|
+
size_t len = 0, seq_start = 0;
|
|
1551
|
+
int needed = 0, seen = 0;
|
|
1552
|
+
unsigned char lower = 0x80, upper = 0xBF;
|
|
1553
|
+
for (size_t i = 0; i < n; i++) {
|
|
1554
|
+
unsigned char b = in[i];
|
|
1555
|
+
if (needed > 0) {
|
|
1556
|
+
if (b >= lower && b <= upper) {
|
|
1557
|
+
lower = 0x80, upper = 0xBF;
|
|
1558
|
+
if (++seen == needed) {
|
|
1559
|
+
memcpy(out + len, in + seq_start, (size_t)needed + 1);
|
|
1560
|
+
len += (size_t)needed + 1;
|
|
1561
|
+
needed = 0, seen = 0;
|
|
1562
|
+
}
|
|
1563
|
+
continue;
|
|
1564
|
+
}
|
|
1565
|
+
// The sequence so far is one invalid subpart; this byte starts afresh.
|
|
1566
|
+
out[len++] = 0xEF, out[len++] = 0xBF, out[len++] = 0xBD;
|
|
1567
|
+
needed = 0, seen = 0, lower = 0x80, upper = 0xBF;
|
|
1568
|
+
}
|
|
1569
|
+
seq_start = i;
|
|
1570
|
+
if (b <= 0x7F) {
|
|
1571
|
+
out[len++] = b;
|
|
1572
|
+
} else if (b >= 0xC2 && b <= 0xDF) {
|
|
1573
|
+
needed = 1;
|
|
1574
|
+
} else if (b >= 0xE0 && b <= 0xEF) {
|
|
1575
|
+
lower = b == 0xE0 ? 0xA0 : 0x80;
|
|
1576
|
+
upper = b == 0xED ? 0x9F : 0xBF;
|
|
1577
|
+
needed = 2;
|
|
1578
|
+
} else if (b >= 0xF0 && b <= 0xF4) {
|
|
1579
|
+
lower = b == 0xF0 ? 0x90 : 0x80;
|
|
1580
|
+
upper = b == 0xF4 ? 0x8F : 0xBF;
|
|
1581
|
+
needed = 3;
|
|
1582
|
+
} else {
|
|
1583
|
+
out[len++] = 0xEF, out[len++] = 0xBF, out[len++] = 0xBD;
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
if (needed > 0) {
|
|
1587
|
+
out[len++] = 0xEF, out[len++] = 0xBF, out[len++] = 0xBD;
|
|
1588
|
+
}
|
|
1589
|
+
return len;
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
static int is_ascii(const char *text, size_t n) {
|
|
1593
|
+
for (size_t i = 0; i < n; i++) {
|
|
1594
|
+
if ((unsigned char)text[i] >= 0x80) {
|
|
1595
|
+
return 0;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
return 1;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
/** Length in UTF-16 code units of valid UTF-8: one per code point, two past U+FFFF. */
|
|
1602
|
+
static uint32_t utf16_length(const unsigned char *utf8, size_t n) {
|
|
1603
|
+
uint32_t units = 0;
|
|
1604
|
+
for (size_t i = 0; i < n; i++) {
|
|
1605
|
+
unsigned char b = utf8[i];
|
|
1606
|
+
units += (b & 0xC0) != 0x80; // a code point starts here
|
|
1607
|
+
units += b >= 0xF0; // ... and needs a surrogate pair
|
|
1608
|
+
}
|
|
1609
|
+
return units;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1405
1612
|
/**
|
|
1406
1613
|
* Append a value as text, followed by the NUL that separates it from the next.
|
|
1407
1614
|
* Returns -1 (with `w->error` set) on failure.
|
|
1408
1615
|
*/
|
|
1409
|
-
static int store_text(QueryWork *w, ResultColumn *col, int coltype, const BYTE *src,
|
|
1616
|
+
static int store_text(QueryWork *w, ResultColumn *col, int r, int coltype, const BYTE *src,
|
|
1617
|
+
int datalen) {
|
|
1410
1618
|
char *dest;
|
|
1411
1619
|
int len;
|
|
1412
1620
|
|
|
1413
1621
|
if (coltype == SYBCHAR || coltype == SYBVARCHAR || coltype == SYBTEXT) {
|
|
1414
1622
|
// Already text: dbconvert would only copy it (after blank-padding the
|
|
1415
|
-
// whole destination buffer).
|
|
1416
|
-
|
|
1623
|
+
// whole destination buffer). Latin-1 grows to at most 2 bytes per byte,
|
|
1624
|
+
// and the result must still fit an int length.
|
|
1625
|
+
if (col->latin1 && datalen > (INT_MAX - 1) / 2) {
|
|
1626
|
+
snprintf(w->error, sizeof(w->error), "%s value in column '%s' is too large (%d bytes)",
|
|
1627
|
+
syb_type_name(coltype), col->name, datalen);
|
|
1628
|
+
return -1;
|
|
1629
|
+
}
|
|
1630
|
+
if (reserve_bytes(col, (size_t)datalen * (col->latin1 ? 2 : 1) + 1) != 0) {
|
|
1417
1631
|
goto oom;
|
|
1418
1632
|
}
|
|
1419
1633
|
dest = col->bytes + col->bytes_len;
|
|
1420
|
-
|
|
1421
|
-
|
|
1634
|
+
if (col->latin1) {
|
|
1635
|
+
len = (int)latin1_to_utf8(src, (size_t)datalen, (unsigned char *)dest);
|
|
1636
|
+
} else {
|
|
1637
|
+
memcpy(dest, src, (size_t)datalen);
|
|
1638
|
+
len = datalen;
|
|
1639
|
+
}
|
|
1422
1640
|
} else if (is_temporal_type(coltype)) {
|
|
1423
1641
|
// Date/time values are formatted here, never by dbconvert, which renders
|
|
1424
1642
|
// them through a locale-dependent format string that in several locales
|
|
@@ -1476,16 +1694,28 @@ static int store_text(QueryWork *w, ResultColumn *col, int coltype, const BYTE *
|
|
|
1476
1694
|
len--;
|
|
1477
1695
|
}
|
|
1478
1696
|
}
|
|
1479
|
-
//
|
|
1480
|
-
//
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1697
|
+
// Make it valid UTF-8, the way V8 would when the bytes become a JS string,
|
|
1698
|
+
// so that every path — rows, columns, a CSV file — holds the same value and
|
|
1699
|
+
// its UTF-16 length is known here. Latin-1 decoding is valid already.
|
|
1700
|
+
size_t n = (size_t)len;
|
|
1701
|
+
if (!col->latin1 && !is_ascii(dest, n)) {
|
|
1702
|
+
char *raw = (char *)malloc(n);
|
|
1703
|
+
if (!raw) {
|
|
1704
|
+
goto oom;
|
|
1705
|
+
}
|
|
1706
|
+
memcpy(raw, dest, n);
|
|
1707
|
+
if (reserve_bytes(col, n * 3) != 0) {
|
|
1708
|
+
free(raw);
|
|
1709
|
+
goto oom;
|
|
1710
|
+
}
|
|
1711
|
+
dest = col->bytes + col->bytes_len;
|
|
1712
|
+
n = utf8_sanitize((const unsigned char *)raw, n, (unsigned char *)dest);
|
|
1713
|
+
free(raw);
|
|
1484
1714
|
}
|
|
1485
|
-
|
|
1486
|
-
col->
|
|
1487
|
-
col->
|
|
1488
|
-
col->
|
|
1715
|
+
// NULs are kept: a value is cut out by length, never at a terminator.
|
|
1716
|
+
col->units[r] = utf16_length((const unsigned char *)dest, n);
|
|
1717
|
+
col->bytes_len += n;
|
|
1718
|
+
col->ends[r] = col->bytes_len;
|
|
1489
1719
|
return 0;
|
|
1490
1720
|
|
|
1491
1721
|
oom:
|
|
@@ -1494,12 +1724,11 @@ oom:
|
|
|
1494
1724
|
}
|
|
1495
1725
|
|
|
1496
1726
|
/**
|
|
1497
|
-
* Store column `i` of the current row
|
|
1498
|
-
* failure.
|
|
1727
|
+
* Store column `i` of the current row in slot `r`. Returns -1 (with
|
|
1728
|
+
* `w->error` set) on failure.
|
|
1499
1729
|
*/
|
|
1500
|
-
static int store_cell(QueryWork *w, int i) {
|
|
1730
|
+
static int store_cell(QueryWork *w, int i, int r) {
|
|
1501
1731
|
ResultColumn *col = &w->columns[i];
|
|
1502
|
-
int r = w->num_rows;
|
|
1503
1732
|
int colno = i + 1;
|
|
1504
1733
|
BYTE *src = dbdata(w->conn->dbproc, colno);
|
|
1505
1734
|
int datalen = dbdatlen(w->conn->dbproc, colno);
|
|
@@ -1510,15 +1739,11 @@ static int store_cell(QueryWork *w, int i) {
|
|
|
1510
1739
|
col->has_nulls = 1;
|
|
1511
1740
|
if (size > 0) {
|
|
1512
1741
|
memset(col->fixed + (size_t)r * size, 0, size);
|
|
1513
|
-
} else if (col->kind == KIND_TEXT) {
|
|
1514
|
-
if (reserve_bytes(col, 1) != 0) {
|
|
1515
|
-
snprintf(w->error, sizeof(w->error), "Memory allocation failed for column value");
|
|
1516
|
-
return -1;
|
|
1517
|
-
}
|
|
1518
|
-
col->ends[r] = col->bytes_len;
|
|
1519
|
-
col->bytes[col->bytes_len++] = '\0';
|
|
1520
1742
|
} else {
|
|
1521
1743
|
col->ends[r] = col->bytes_len;
|
|
1744
|
+
if (col->kind == KIND_TEXT) {
|
|
1745
|
+
col->units[r] = 0;
|
|
1746
|
+
}
|
|
1522
1747
|
}
|
|
1523
1748
|
return 0;
|
|
1524
1749
|
}
|
|
@@ -1544,7 +1769,289 @@ static int store_cell(QueryWork *w, int i) {
|
|
|
1544
1769
|
col->ends[r] = col->bytes_len;
|
|
1545
1770
|
return 0;
|
|
1546
1771
|
}
|
|
1547
|
-
return store_text(w, col, dbcoltype(w->conn->dbproc, colno), src, datalen);
|
|
1772
|
+
return store_text(w, col, r, dbcoltype(w->conn->dbproc, colno), src, datalen);
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
// ---------------------------------------------------------------------------
|
|
1776
|
+
// Streaming a result to a CSV file
|
|
1777
|
+
//
|
|
1778
|
+
// RFC 4180: comma-separated, a header row of column names, fields quoted when
|
|
1779
|
+
// they hold a comma, quote, line break or NUL (or are empty, or start or end with a
|
|
1780
|
+
// blank), quotes doubled. NULL is an empty unquoted field, so it stays distinct
|
|
1781
|
+
// from an empty string (""). Values are the ones a row result holds, rendered
|
|
1782
|
+
// as text: the same conversion code produces them. Binary is lowercase hex.
|
|
1783
|
+
// ---------------------------------------------------------------------------
|
|
1784
|
+
|
|
1785
|
+
#define OUT_BUF_SIZE ((size_t)1 << 20)
|
|
1786
|
+
|
|
1787
|
+
#if defined(_WIN32)
|
|
1788
|
+
/** A UTF-8 path as the UTF-16 Windows wants; NULL if it cannot be converted. */
|
|
1789
|
+
static wchar_t *wide_path(const char *path) {
|
|
1790
|
+
int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);
|
|
1791
|
+
wchar_t *wide = n > 0 ? (wchar_t *)malloc((size_t)n * sizeof(wchar_t)) : NULL;
|
|
1792
|
+
if (wide) {
|
|
1793
|
+
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wide, n);
|
|
1794
|
+
}
|
|
1795
|
+
return wide;
|
|
1796
|
+
}
|
|
1797
|
+
#endif
|
|
1798
|
+
|
|
1799
|
+
/**
|
|
1800
|
+
* Create (exclusively: never an existing file) `path` for writing. On POSIX it
|
|
1801
|
+
* is readable by its owner only, since it holds query results, and it is not
|
|
1802
|
+
* inherited by child processes. On Windows it takes its directory's ACL.
|
|
1803
|
+
*/
|
|
1804
|
+
static FILE *open_output(const char *path) {
|
|
1805
|
+
#if defined(_WIN32)
|
|
1806
|
+
wchar_t *wide = wide_path(path);
|
|
1807
|
+
FILE *f = wide ? _wfopen(wide, L"wbx") : NULL;
|
|
1808
|
+
free(wide);
|
|
1809
|
+
return f;
|
|
1810
|
+
#else
|
|
1811
|
+
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
|
1812
|
+
if (fd < 0) {
|
|
1813
|
+
return NULL;
|
|
1814
|
+
}
|
|
1815
|
+
FILE *f = fdopen(fd, "wb");
|
|
1816
|
+
if (!f) {
|
|
1817
|
+
int saved = errno;
|
|
1818
|
+
close(fd);
|
|
1819
|
+
errno = saved;
|
|
1820
|
+
}
|
|
1821
|
+
return f;
|
|
1822
|
+
#endif
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
static void remove_output(const char *path) {
|
|
1826
|
+
#if defined(_WIN32)
|
|
1827
|
+
wchar_t *wide = wide_path(path);
|
|
1828
|
+
if (wide) {
|
|
1829
|
+
_wremove(wide);
|
|
1830
|
+
free(wide);
|
|
1831
|
+
}
|
|
1832
|
+
#else
|
|
1833
|
+
remove(path);
|
|
1834
|
+
#endif
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
/** Replace `to` with `from` in one step: a reader sees the old file or the new one. */
|
|
1838
|
+
static int publish_output(const char *from, const char *to) {
|
|
1839
|
+
#if defined(_WIN32)
|
|
1840
|
+
wchar_t *wfrom = wide_path(from), *wto = wide_path(to);
|
|
1841
|
+
int ok = wfrom && wto && MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
|
|
1842
|
+
free(wfrom);
|
|
1843
|
+
free(wto);
|
|
1844
|
+
return ok ? 0 : -1;
|
|
1845
|
+
#else
|
|
1846
|
+
return rename(from, to);
|
|
1847
|
+
#endif
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
static int out_flush(QueryWork *w) {
|
|
1851
|
+
if (w->out_len > 0) {
|
|
1852
|
+
if (fwrite(w->out_buf, 1, w->out_len, w->out) != w->out_len) {
|
|
1853
|
+
snprintf(w->error, sizeof(w->error), "Could not write to %s: %s", w->out_path, strerror(errno));
|
|
1854
|
+
return -1;
|
|
1855
|
+
}
|
|
1856
|
+
w->out_bytes += (int64_t)w->out_len;
|
|
1857
|
+
w->out_len = 0;
|
|
1858
|
+
}
|
|
1859
|
+
return 0;
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
static int out_write(QueryWork *w, const char *data, size_t n) {
|
|
1863
|
+
if (OUT_BUF_SIZE - w->out_len < n) {
|
|
1864
|
+
if (out_flush(w) != 0) {
|
|
1865
|
+
return -1;
|
|
1866
|
+
}
|
|
1867
|
+
if (n > OUT_BUF_SIZE) {
|
|
1868
|
+
if (fwrite(data, 1, n, w->out) != n) {
|
|
1869
|
+
snprintf(w->error, sizeof(w->error), "Could not write to %s: %s", w->out_path,
|
|
1870
|
+
strerror(errno));
|
|
1871
|
+
return -1;
|
|
1872
|
+
}
|
|
1873
|
+
w->out_bytes += (int64_t)n;
|
|
1874
|
+
return 0;
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
memcpy(w->out_buf + w->out_len, data, n);
|
|
1878
|
+
w->out_len += n;
|
|
1879
|
+
return 0;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
static int out_char(QueryWork *w, char c) {
|
|
1883
|
+
if (w->out_len == OUT_BUF_SIZE && out_flush(w) != 0) {
|
|
1884
|
+
return -1;
|
|
1885
|
+
}
|
|
1886
|
+
w->out_buf[w->out_len++] = c;
|
|
1887
|
+
return 0;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
/** A text field (valid UTF-8, see store_text), quoted only when it has to be. */
|
|
1891
|
+
static int out_text_field(QueryWork *w, const char *text, size_t n) {
|
|
1892
|
+
int quote = n == 0 || text[0] == ' ' || text[0] == '\t' || text[n - 1] == ' ' ||
|
|
1893
|
+
text[n - 1] == '\t';
|
|
1894
|
+
for (size_t i = 0; !quote && i < n; i++) {
|
|
1895
|
+
char c = text[i];
|
|
1896
|
+
quote = c == ',' || c == '"' || c == '\n' || c == '\r' || c == '\0';
|
|
1897
|
+
}
|
|
1898
|
+
if (!quote) {
|
|
1899
|
+
return out_write(w, text, n);
|
|
1900
|
+
}
|
|
1901
|
+
if (out_char(w, '"') != 0) {
|
|
1902
|
+
return -1;
|
|
1903
|
+
}
|
|
1904
|
+
size_t start = 0;
|
|
1905
|
+
for (size_t i = 0; i < n; i++) {
|
|
1906
|
+
if (text[i] == '"') {
|
|
1907
|
+
// Through the quote, then the quote again.
|
|
1908
|
+
if (out_write(w, text + start, i + 1 - start) != 0 || out_char(w, '"') != 0) {
|
|
1909
|
+
return -1;
|
|
1910
|
+
}
|
|
1911
|
+
start = i + 1;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
if (out_write(w, text + start, n - start) != 0) {
|
|
1915
|
+
return -1;
|
|
1916
|
+
}
|
|
1917
|
+
return out_char(w, '"');
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
/**
|
|
1921
|
+
* The first of `%.15g`, `%.16g`, `%.17g` that reads back as exactly `v`, with
|
|
1922
|
+
* `.` as the decimal point: `snprintf` writes the process locale's, which may
|
|
1923
|
+
* be a comma — a field separator here — or a multibyte character. Whatever
|
|
1924
|
+
* sits between the digits that is not a sign or an exponent is that point.
|
|
1925
|
+
*/
|
|
1926
|
+
static int format_double(double v, char *buf, size_t size) {
|
|
1927
|
+
int n = 0;
|
|
1928
|
+
for (int precision = 15; precision <= 17; precision++) {
|
|
1929
|
+
n = snprintf(buf, size, "%.*g", precision, v);
|
|
1930
|
+
if (strtod(buf, NULL) == v) {
|
|
1931
|
+
break;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
int len = 0, point = 0;
|
|
1935
|
+
for (int i = 0; i < n; i++) {
|
|
1936
|
+
char c = buf[i];
|
|
1937
|
+
if ((c >= '0' && c <= '9') || c == '-' || c == '+' || c == 'e') {
|
|
1938
|
+
buf[len++] = c;
|
|
1939
|
+
} else if (!point) {
|
|
1940
|
+
buf[len++] = '.';
|
|
1941
|
+
point = 1;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
return len;
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
static int out_hex(QueryWork *w, const unsigned char *bytes, size_t n) {
|
|
1948
|
+
static const char digits[] = "0123456789abcdef";
|
|
1949
|
+
char chunk[1024];
|
|
1950
|
+
size_t used = 0;
|
|
1951
|
+
for (size_t i = 0; i < n; i++) {
|
|
1952
|
+
chunk[used++] = digits[bytes[i] >> 4];
|
|
1953
|
+
chunk[used++] = digits[bytes[i] & 15];
|
|
1954
|
+
if (used == sizeof(chunk)) {
|
|
1955
|
+
if (out_write(w, chunk, used) != 0) {
|
|
1956
|
+
return -1;
|
|
1957
|
+
}
|
|
1958
|
+
used = 0;
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
return out_write(w, chunk, used);
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
static int csv_write_header(QueryWork *w) {
|
|
1965
|
+
for (int i = 0; i < w->num_columns; i++) {
|
|
1966
|
+
const char *name = w->columns[i].name;
|
|
1967
|
+
size_t n = strlen(name);
|
|
1968
|
+
// A name is made valid UTF-8 like a value (see store_text).
|
|
1969
|
+
char *clean = is_ascii(name, n) ? NULL : (char *)malloc(n * 3 + 1);
|
|
1970
|
+
if (!is_ascii(name, n) && !clean) {
|
|
1971
|
+
snprintf(w->error, sizeof(w->error), "Memory allocation failed for column names");
|
|
1972
|
+
return -1;
|
|
1973
|
+
}
|
|
1974
|
+
if (clean) {
|
|
1975
|
+
n = utf8_sanitize((const unsigned char *)name, n, (unsigned char *)clean);
|
|
1976
|
+
}
|
|
1977
|
+
int rc = (i > 0 && out_char(w, ',') != 0) || out_text_field(w, clean ? clean : name, n) != 0;
|
|
1978
|
+
free(clean);
|
|
1979
|
+
if (rc) {
|
|
1980
|
+
return -1;
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
return out_char(w, '\n');
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
/** Write the row held in slot 0 of every column, then empty the slot for the next. */
|
|
1987
|
+
static int csv_write_row(QueryWork *w) {
|
|
1988
|
+
char num[40];
|
|
1989
|
+
for (int i = 0; i < w->num_columns; i++) {
|
|
1990
|
+
ResultColumn *col = &w->columns[i];
|
|
1991
|
+
if (i > 0 && out_char(w, ',') != 0) {
|
|
1992
|
+
return -1;
|
|
1993
|
+
}
|
|
1994
|
+
int rc = 0;
|
|
1995
|
+
if (!col->nulls[0]) {
|
|
1996
|
+
switch (col->kind) {
|
|
1997
|
+
case KIND_INT32: {
|
|
1998
|
+
int32_t v;
|
|
1999
|
+
memcpy(&v, col->fixed, sizeof(v));
|
|
2000
|
+
int len = 0;
|
|
2001
|
+
if (v < 0) {
|
|
2002
|
+
num[len++] = '-';
|
|
2003
|
+
}
|
|
2004
|
+
len += write_decimal(num + len, v < 0 ? (uint64_t)0 - (uint64_t)(int64_t)v : (uint64_t)v);
|
|
2005
|
+
rc = out_write(w, num, (size_t)len);
|
|
2006
|
+
break;
|
|
2007
|
+
}
|
|
2008
|
+
case KIND_INT64: {
|
|
2009
|
+
int64_t v;
|
|
2010
|
+
memcpy(&v, col->fixed, sizeof(v));
|
|
2011
|
+
int len = 0;
|
|
2012
|
+
if (v < 0) {
|
|
2013
|
+
num[len++] = '-';
|
|
2014
|
+
}
|
|
2015
|
+
len += write_decimal(num + len, v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v);
|
|
2016
|
+
rc = out_write(w, num, (size_t)len);
|
|
2017
|
+
break;
|
|
2018
|
+
}
|
|
2019
|
+
case KIND_UINT64: {
|
|
2020
|
+
uint64_t v;
|
|
2021
|
+
memcpy(&v, col->fixed, sizeof(v));
|
|
2022
|
+
rc = out_write(w, num, (size_t)write_decimal(num, v));
|
|
2023
|
+
break;
|
|
2024
|
+
}
|
|
2025
|
+
case KIND_FLOAT64: {
|
|
2026
|
+
double v;
|
|
2027
|
+
memcpy(&v, col->fixed, sizeof(v));
|
|
2028
|
+
rc = out_write(w, num, (size_t)format_double(v, num, sizeof(num)));
|
|
2029
|
+
break;
|
|
2030
|
+
}
|
|
2031
|
+
case KIND_BOOL:
|
|
2032
|
+
rc = col->fixed[0] ? out_write(w, "true", 4) : out_write(w, "false", 5);
|
|
2033
|
+
break;
|
|
2034
|
+
case KIND_TEXT:
|
|
2035
|
+
rc = out_text_field(w, col->bytes, col->ends[0]);
|
|
2036
|
+
break;
|
|
2037
|
+
case KIND_BINARY:
|
|
2038
|
+
// Quoted when empty, like an empty string: an empty unquoted field is NULL.
|
|
2039
|
+
rc = col->ends[0] == 0 ? out_write(w, "\"\"", 2)
|
|
2040
|
+
: out_hex(w, (const unsigned char *)col->bytes, col->ends[0]);
|
|
2041
|
+
break;
|
|
2042
|
+
case KIND_WALLCLOCK:
|
|
2043
|
+
// Never chosen for a file: datetimes are written as their canonical text.
|
|
2044
|
+
snprintf(w->error, sizeof(w->error), "Internal error: wall-clock column in a CSV file");
|
|
2045
|
+
rc = -1;
|
|
2046
|
+
break;
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
col->bytes_len = 0;
|
|
2050
|
+
if (rc != 0) {
|
|
2051
|
+
return -1;
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
return out_char(w, '\n');
|
|
1548
2055
|
}
|
|
1549
2056
|
|
|
1550
2057
|
static void query_execute(napi_env env, void *data) {
|
|
@@ -1583,6 +2090,28 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1583
2090
|
|
|
1584
2091
|
clear_error(w->conn);
|
|
1585
2092
|
|
|
2093
|
+
if (w->out_path) {
|
|
2094
|
+
// Beside the target, so the final rename stays on one file system; unique,
|
|
2095
|
+
// so concurrent exports to the same path never share a temporary.
|
|
2096
|
+
size_t tmp_size = strlen(w->out_path) + 64;
|
|
2097
|
+
w->out_buf = (char *)malloc(OUT_BUF_SIZE);
|
|
2098
|
+
w->out_tmp = (char *)malloc(tmp_size);
|
|
2099
|
+
if (w->out_buf && w->out_tmp) {
|
|
2100
|
+
snprintf(w->out_tmp, tmp_size, "%s.%llx-%p.tmp", w->out_path,
|
|
2101
|
+
(unsigned long long)uv_hrtime(), (void *)w);
|
|
2102
|
+
w->out = open_output(w->out_tmp);
|
|
2103
|
+
}
|
|
2104
|
+
if (!w->out) {
|
|
2105
|
+
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");
|
|
2108
|
+
w->success = 0;
|
|
2109
|
+
w->conn_dead = conn_is_dead(w->conn);
|
|
2110
|
+
work_release_conn(w);
|
|
2111
|
+
return;
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
|
|
1586
2115
|
// Spawn the hard-timeout watchdog. Failure is non-fatal — db-lib's own
|
|
1587
2116
|
// DBSETTIME still provides a cooperative timeout.
|
|
1588
2117
|
watchdog_start(&w->wd, w->conn, w->hard_timeout_ms);
|
|
@@ -1674,9 +2203,13 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1674
2203
|
|
|
1675
2204
|
for (int i = 0; i < w->num_columns; i++) {
|
|
1676
2205
|
const char *colname = dbcolname(w->conn->dbproc, i + 1);
|
|
1677
|
-
w->columns[i].name =
|
|
2206
|
+
w->columns[i].name = dup_text(colname ? colname : "", w->conn->latin1);
|
|
1678
2207
|
w->columns[i].type = column_type(w->conn->dbproc, i + 1);
|
|
1679
2208
|
w->columns[i].kind = column_kind(w->columns[i].type, w->wall_clock_datetimes);
|
|
2209
|
+
// unichar/univarchar (user types 34/35) also report as char types, but
|
|
2210
|
+
// FreeTDS has already converted them from UTF-16 to UTF-8.
|
|
2211
|
+
int usertype = dbcolutype(w->conn->dbproc, i + 1);
|
|
2212
|
+
w->columns[i].latin1 = w->conn->latin1 && usertype != 34 && usertype != 35;
|
|
1680
2213
|
if (!w->columns[i].name) {
|
|
1681
2214
|
snprintf(w->error, sizeof(w->error), "Memory allocation failed for column names");
|
|
1682
2215
|
discard_results(w);
|
|
@@ -1686,10 +2219,13 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1686
2219
|
}
|
|
1687
2220
|
}
|
|
1688
2221
|
|
|
1689
|
-
// Read rows
|
|
1690
|
-
|
|
2222
|
+
// Read rows. Streamed to a file, each row is written as soon as it is read
|
|
2223
|
+
// and its slot reused, so no default cap is needed: only maxRows limits it.
|
|
2224
|
+
int file_mode = w->out != NULL;
|
|
2225
|
+
int capacity = file_mode ? 1 : 64;
|
|
1691
2226
|
w->num_rows = 0;
|
|
1692
2227
|
int max_rows = w->max_rows > 0 ? w->max_rows : DEFAULT_MAX_ROWS;
|
|
2228
|
+
int64_t row_cap = !file_mode ? max_rows : w->max_rows_explicit ? w->max_rows : INT64_MAX;
|
|
1693
2229
|
|
|
1694
2230
|
if (grow_rows(w->columns, w->num_columns, capacity) != 0) {
|
|
1695
2231
|
snprintf(w->error, sizeof(w->error), "Memory allocation failed for rows");
|
|
@@ -1699,6 +2235,13 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1699
2235
|
goto done;
|
|
1700
2236
|
}
|
|
1701
2237
|
|
|
2238
|
+
if (file_mode && csv_write_header(w) != 0) {
|
|
2239
|
+
discard_results(w);
|
|
2240
|
+
dbcancel(w->conn->dbproc);
|
|
2241
|
+
w->success = 0;
|
|
2242
|
+
goto done;
|
|
2243
|
+
}
|
|
2244
|
+
|
|
1702
2245
|
RETCODE row_code;
|
|
1703
2246
|
while ((row_code = dbnextrow(w->conn->dbproc)) != NO_MORE_ROWS) {
|
|
1704
2247
|
if (row_code == FAIL) {
|
|
@@ -1719,7 +2262,7 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1719
2262
|
}
|
|
1720
2263
|
|
|
1721
2264
|
// Safety: cap rows
|
|
1722
|
-
if (w->num_rows >=
|
|
2265
|
+
if ((file_mode ? w->out_rows : w->num_rows) >= row_cap) {
|
|
1723
2266
|
w->truncated = 1;
|
|
1724
2267
|
// Drain remaining rows without storing
|
|
1725
2268
|
if (discard_rows(w->conn->dbproc) == FAIL) {
|
|
@@ -1729,7 +2272,7 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1729
2272
|
}
|
|
1730
2273
|
|
|
1731
2274
|
// Grow arrays if needed
|
|
1732
|
-
if (w->num_rows >= capacity) {
|
|
2275
|
+
if (!file_mode && w->num_rows >= capacity) {
|
|
1733
2276
|
capacity = capacity > max_rows / 2 ? max_rows : capacity * 2;
|
|
1734
2277
|
if (grow_rows(w->columns, w->num_columns, capacity) != 0) {
|
|
1735
2278
|
snprintf(w->error, sizeof(w->error), "Memory realloc failed at row %d", w->num_rows);
|
|
@@ -1741,15 +2284,25 @@ static void query_execute(napi_env env, void *data) {
|
|
|
1741
2284
|
}
|
|
1742
2285
|
|
|
1743
2286
|
// Read column values
|
|
2287
|
+
int slot = file_mode ? 0 : w->num_rows;
|
|
1744
2288
|
for (int i = 0; i < w->num_columns; i++) {
|
|
1745
|
-
if (store_cell(w, i) != 0) {
|
|
2289
|
+
if (store_cell(w, i, slot) != 0) {
|
|
1746
2290
|
discard_results(w);
|
|
1747
2291
|
dbcancel(w->conn->dbproc);
|
|
1748
2292
|
w->success = 0;
|
|
1749
2293
|
goto done;
|
|
1750
2294
|
}
|
|
1751
2295
|
}
|
|
1752
|
-
|
|
2296
|
+
if (!file_mode) {
|
|
2297
|
+
w->num_rows++;
|
|
2298
|
+
} else if (csv_write_row(w) == 0) {
|
|
2299
|
+
w->out_rows++;
|
|
2300
|
+
} else {
|
|
2301
|
+
discard_results(w);
|
|
2302
|
+
dbcancel(w->conn->dbproc);
|
|
2303
|
+
w->success = 0;
|
|
2304
|
+
goto done;
|
|
2305
|
+
}
|
|
1753
2306
|
}
|
|
1754
2307
|
|
|
1755
2308
|
// Capture affected rows for this result set too
|
|
@@ -1799,6 +2352,18 @@ rows_failed:
|
|
|
1799
2352
|
w->success = 0;
|
|
1800
2353
|
|
|
1801
2354
|
done:
|
|
2355
|
+
// Write out the rest of the file while the watchdog still covers it.
|
|
2356
|
+
if (w->out) {
|
|
2357
|
+
if (w->success && out_flush(w) != 0) {
|
|
2358
|
+
w->success = 0;
|
|
2359
|
+
}
|
|
2360
|
+
if (fclose(w->out) != 0 && w->success) {
|
|
2361
|
+
snprintf(w->error, sizeof(w->error), "Could not write to %s: %s", w->out_tmp, strerror(errno));
|
|
2362
|
+
w->success = 0;
|
|
2363
|
+
}
|
|
2364
|
+
w->out = NULL;
|
|
2365
|
+
}
|
|
2366
|
+
|
|
1802
2367
|
// Stop the watchdog before returning. If it already fired (forced cancel),
|
|
1803
2368
|
// overwrite the error so the JS layer knows it was a hard timeout.
|
|
1804
2369
|
watchdog_stop(&w->wd);
|
|
@@ -1810,6 +2375,31 @@ done:
|
|
|
1810
2375
|
w->success = 0;
|
|
1811
2376
|
}
|
|
1812
2377
|
|
|
2378
|
+
// Publish the file, or drop it. Only a complete result replaces the target —
|
|
2379
|
+
// a failed query leaves whatever was there before — and not one the caller has
|
|
2380
|
+
// given up on: the JS layer closes the connection when its own deadline passes,
|
|
2381
|
+
// and a result appearing after the caller was told it failed would be taken
|
|
2382
|
+
// for the outcome of a later attempt.
|
|
2383
|
+
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.
|
|
2387
|
+
uv_mutex_lock(&w->conn->lock);
|
|
2388
|
+
if (w->success && w->conn->pending_close) {
|
|
2389
|
+
snprintf(w->error, sizeof(w->error), "Connection was closed before the export finished");
|
|
2390
|
+
w->success = 0;
|
|
2391
|
+
}
|
|
2392
|
+
if (w->success && publish_output(w->out_tmp, w->out_path) != 0) {
|
|
2393
|
+
snprintf(w->error, sizeof(w->error), "Could not move %s to %s: %s", w->out_tmp, w->out_path,
|
|
2394
|
+
strerror(errno));
|
|
2395
|
+
w->success = 0;
|
|
2396
|
+
}
|
|
2397
|
+
uv_mutex_unlock(&w->conn->lock);
|
|
2398
|
+
if (!w->success) {
|
|
2399
|
+
remove_output(w->out_tmp);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
|
|
1813
2403
|
// Snapshot connection flags before releasing — w->conn may be freed
|
|
1814
2404
|
// inside conn_release if a close was pending.
|
|
1815
2405
|
w->conn_dead = conn_is_dead(w->conn);
|
|
@@ -1847,11 +2437,12 @@ static napi_status copy_typed_array(napi_env env, napi_typedarray_type type, con
|
|
|
1847
2437
|
}
|
|
1848
2438
|
|
|
1849
2439
|
/**
|
|
1850
|
-
* A text column as an array of strings, each holding consecutive values
|
|
1851
|
-
*
|
|
1852
|
-
* per cell; one string for the whole
|
|
1853
|
-
* limit. Bounding the chunks also bounds
|
|
1854
|
-
* to, since V8 may let a value
|
|
2440
|
+
* A text column as an array of strings, each holding consecutive values back
|
|
2441
|
+
* to back, for JS to cut by the values' UTF-16 lengths (`units`). One string per
|
|
2442
|
+
* value would cost several N-API calls per cell; one string for the whole
|
|
2443
|
+
* column could pass V8's string length limit. Bounding the chunks also bounds
|
|
2444
|
+
* what a single kept value can hold on to, since V8 may let a value sliced off a
|
|
2445
|
+
* string share that string's storage.
|
|
1855
2446
|
*/
|
|
1856
2447
|
static napi_status build_text_chunks(napi_env env, const ResultColumn *col, int num_rows,
|
|
1857
2448
|
napi_value *out) {
|
|
@@ -1864,7 +2455,7 @@ static napi_status build_text_chunks(napi_env env, const ResultColumn *col, int
|
|
|
1864
2455
|
napi_value chunk;
|
|
1865
2456
|
NAPI_TRY(napi_create_string_utf8(env, col->bytes + start, end - start, &chunk));
|
|
1866
2457
|
NAPI_TRY(napi_set_element(env, *out, count++, chunk));
|
|
1867
|
-
start = end
|
|
2458
|
+
start = end;
|
|
1868
2459
|
}
|
|
1869
2460
|
}
|
|
1870
2461
|
return napi_ok;
|
|
@@ -1901,9 +2492,13 @@ static napi_status build_column(napi_env env, const ResultColumn *col, int num_r
|
|
|
1901
2492
|
NAPI_TRY(napi_set_named_property(env, *out, "kind", kind));
|
|
1902
2493
|
|
|
1903
2494
|
switch (col->kind) {
|
|
1904
|
-
case KIND_TEXT:
|
|
2495
|
+
case KIND_TEXT: {
|
|
2496
|
+
napi_value units;
|
|
1905
2497
|
NAPI_TRY(build_text_chunks(env, col, num_rows, &values));
|
|
2498
|
+
NAPI_TRY(copy_typed_array(env, napi_uint32_array, col->units, n, sizeof(uint32_t), &units));
|
|
2499
|
+
NAPI_TRY(napi_set_named_property(env, *out, "units", units));
|
|
1906
2500
|
break;
|
|
2501
|
+
}
|
|
1907
2502
|
case KIND_BINARY: {
|
|
1908
2503
|
napi_value ends;
|
|
1909
2504
|
NAPI_TRY(build_binary(env, col, num_rows, &values, &ends));
|
|
@@ -1938,7 +2533,10 @@ static napi_status build_column(napi_env env, const ResultColumn *col, int num_r
|
|
|
1938
2533
|
return napi_set_named_property(env, *out, "nulls", nulls);
|
|
1939
2534
|
}
|
|
1940
2535
|
|
|
1941
|
-
/**
|
|
2536
|
+
/**
|
|
2537
|
+
* `{ columns, columnTypes, data, rowCount, affectedRows }`; for a result
|
|
2538
|
+
* streamed to a file, `{ columns, columnTypes, rowCount, affectedRows, bytes }`.
|
|
2539
|
+
*/
|
|
1942
2540
|
static napi_status build_result(napi_env env, const QueryWork *w, napi_value *out) {
|
|
1943
2541
|
napi_value cols_arr, col_types_arr, data_arr, row_count, affected;
|
|
1944
2542
|
size_t ncols = (size_t)w->num_columns;
|
|
@@ -1955,15 +2553,25 @@ static napi_status build_result(napi_env env, const QueryWork *w, napi_value *ou
|
|
|
1955
2553
|
NAPI_TRY(napi_create_string_utf8(env, syb_type_name(w->columns[i].type), NAPI_AUTO_LENGTH,
|
|
1956
2554
|
&col_type));
|
|
1957
2555
|
NAPI_TRY(napi_set_element(env, col_types_arr, (uint32_t)i, col_type));
|
|
1958
|
-
|
|
1959
|
-
|
|
2556
|
+
if (!w->out_path) {
|
|
2557
|
+
NAPI_TRY(build_column(env, &w->columns[i], w->num_rows, &col_data));
|
|
2558
|
+
NAPI_TRY(napi_set_element(env, data_arr, (uint32_t)i, col_data));
|
|
2559
|
+
}
|
|
1960
2560
|
}
|
|
1961
2561
|
|
|
1962
|
-
|
|
2562
|
+
if (w->out_path) {
|
|
2563
|
+
// Past int32 for a large file; exact in a double far beyond any row count.
|
|
2564
|
+
napi_value bytes;
|
|
2565
|
+
NAPI_TRY(napi_create_double(env, (double)w->out_rows, &row_count));
|
|
2566
|
+
NAPI_TRY(napi_create_double(env, (double)w->out_bytes, &bytes));
|
|
2567
|
+
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));
|
|
2571
|
+
}
|
|
1963
2572
|
NAPI_TRY(napi_create_int32(env, w->affected_rows, &affected));
|
|
1964
2573
|
NAPI_TRY(napi_set_named_property(env, *out, "columns", cols_arr));
|
|
1965
2574
|
NAPI_TRY(napi_set_named_property(env, *out, "columnTypes", col_types_arr));
|
|
1966
|
-
NAPI_TRY(napi_set_named_property(env, *out, "data", data_arr));
|
|
1967
2575
|
NAPI_TRY(napi_set_named_property(env, *out, "rowCount", row_count));
|
|
1968
2576
|
return napi_set_named_property(env, *out, "affectedRows", affected);
|
|
1969
2577
|
}
|
|
@@ -2021,10 +2629,62 @@ static void query_complete(napi_env env, napi_status status, void *data) {
|
|
|
2021
2629
|
work_release_conn(w);
|
|
2022
2630
|
free_columns(w->columns, w->num_columns);
|
|
2023
2631
|
free(w->sql);
|
|
2632
|
+
free(w->out_path);
|
|
2633
|
+
free(w->out_tmp);
|
|
2634
|
+
free(w->out_buf);
|
|
2024
2635
|
napi_delete_async_work(env, w->work);
|
|
2025
2636
|
free(w);
|
|
2026
2637
|
}
|
|
2027
2638
|
|
|
2639
|
+
/**
|
|
2640
|
+
* The SQL text as bytes for dbcmd: UTF-8, or Latin-1 for a Latin-1 server.
|
|
2641
|
+
*
|
|
2642
|
+
* A character Latin-1 cannot hold is refused rather than sent: it would reach
|
|
2643
|
+
* the server as two or more unrelated characters. The query builders and
|
|
2644
|
+
* `serializeValue` write such strings as `U&'...'` literals, which carry any
|
|
2645
|
+
* character in ASCII escapes. Returns NULL with an exception pending.
|
|
2646
|
+
*/
|
|
2647
|
+
static char *sql_bytes(napi_env env, napi_value value, int latin1) {
|
|
2648
|
+
size_t len = 0;
|
|
2649
|
+
if (!latin1) {
|
|
2650
|
+
napi_get_value_string_utf8(env, value, NULL, 0, &len);
|
|
2651
|
+
char *out = (char *)malloc(len + 1);
|
|
2652
|
+
if (!out) {
|
|
2653
|
+
napi_throw_error(env, NULL, "Out of memory");
|
|
2654
|
+
return NULL;
|
|
2655
|
+
}
|
|
2656
|
+
napi_get_value_string_utf8(env, value, out, len + 1, &len);
|
|
2657
|
+
return out;
|
|
2658
|
+
}
|
|
2659
|
+
napi_get_value_string_utf16(env, value, NULL, 0, &len);
|
|
2660
|
+
char16_t *units = (char16_t *)malloc((len + 1) * sizeof(char16_t));
|
|
2661
|
+
char *out = units ? (char *)malloc(len + 1) : NULL;
|
|
2662
|
+
if (!out) {
|
|
2663
|
+
free(units);
|
|
2664
|
+
napi_throw_error(env, NULL, "Out of memory");
|
|
2665
|
+
return NULL;
|
|
2666
|
+
}
|
|
2667
|
+
napi_get_value_string_utf16(env, value, units, len + 1, &len);
|
|
2668
|
+
for (size_t i = 0; i < len; i++) {
|
|
2669
|
+
if (units[i] > 0xFF) {
|
|
2670
|
+
char message[400];
|
|
2671
|
+
snprintf(message, sizeof(message),
|
|
2672
|
+
"The SQL contains U+%04X at offset %zu, which the server's character set (iso_1) "
|
|
2673
|
+
"cannot hold. Write such text as a U&'...' literal (e.g. U&'\\%04x'); the query "
|
|
2674
|
+
"builders and serializeValue do this for string values.",
|
|
2675
|
+
(unsigned)units[i], i, (unsigned)units[i]);
|
|
2676
|
+
free(units);
|
|
2677
|
+
free(out);
|
|
2678
|
+
napi_throw_range_error(env, NULL, message);
|
|
2679
|
+
return NULL;
|
|
2680
|
+
}
|
|
2681
|
+
out[i] = (char)units[i];
|
|
2682
|
+
}
|
|
2683
|
+
out[len] = '\0';
|
|
2684
|
+
free(units);
|
|
2685
|
+
return out;
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2028
2688
|
// napi_value query(conn, sql, options?)
|
|
2029
2689
|
//
|
|
2030
2690
|
// options.maxRows: cap rows materialized into JS (defaults to 1,000,000).
|
|
@@ -2032,6 +2692,8 @@ static void query_complete(napi_env env, napi_status status, void *data) {
|
|
|
2032
2692
|
// we derive it from the connection's timeout (2x timeout, min 30s).
|
|
2033
2693
|
// options.wallClockDateTimes: hand datetime/smalldatetime over as wall-clock
|
|
2034
2694
|
// numbers ("wallclock" kind) instead of canonical text.
|
|
2695
|
+
// options.outputPath: stream the rows to this CSV file instead (see
|
|
2696
|
+
// csv_write_row); the result then carries no data, and `bytes`.
|
|
2035
2697
|
//
|
|
2036
2698
|
// Resolves to the result column by column — see build_result; the JS layer
|
|
2037
2699
|
// (src/native/index.ts) turns it into rows.
|
|
@@ -2045,22 +2707,24 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
|
|
|
2045
2707
|
return NULL; // TypeError pending
|
|
2046
2708
|
}
|
|
2047
2709
|
|
|
2048
|
-
|
|
2049
|
-
if (argc < 2 ||
|
|
2710
|
+
napi_valuetype sql_type = napi_undefined;
|
|
2711
|
+
if (argc < 2 || napi_typeof(env, args[1], &sql_type) != napi_ok || sql_type != napi_string) {
|
|
2050
2712
|
napi_throw_type_error(env, NULL, "Expected the SQL as a string");
|
|
2051
2713
|
return NULL;
|
|
2052
2714
|
}
|
|
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
|
+
}
|
|
2053
2720
|
|
|
2054
2721
|
QueryWork *w = (QueryWork *)calloc(1, sizeof(QueryWork));
|
|
2055
|
-
|
|
2056
|
-
if (w == NULL || sql == NULL) {
|
|
2057
|
-
free(w);
|
|
2722
|
+
if (w == NULL) {
|
|
2058
2723
|
free(sql);
|
|
2059
2724
|
napi_throw_error(env, NULL, "Out of memory");
|
|
2060
2725
|
return NULL;
|
|
2061
2726
|
}
|
|
2062
2727
|
w->sql = sql;
|
|
2063
|
-
napi_get_value_string_utf8(env, args[1], w->sql, sql_len + 1, &sql_len);
|
|
2064
2728
|
|
|
2065
2729
|
// A closed handle has no connection: the query is rejected as on a closed
|
|
2066
2730
|
// connection (holds_conn stays 0 below).
|
|
@@ -2108,6 +2772,24 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
|
|
|
2108
2772
|
napi_get_value_bool(env, v, &flag);
|
|
2109
2773
|
w->wall_clock_datetimes = flag ? 1 : 0;
|
|
2110
2774
|
}
|
|
2775
|
+
|
|
2776
|
+
napi_get_named_property(env, args[2], "outputPath", &v);
|
|
2777
|
+
napi_typeof(env, v, &vtype);
|
|
2778
|
+
if (vtype == napi_string) {
|
|
2779
|
+
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);
|
|
2785
|
+
free(w);
|
|
2786
|
+
napi_throw_error(env, NULL, "Out of memory");
|
|
2787
|
+
return NULL;
|
|
2788
|
+
}
|
|
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.
|
|
2791
|
+
w->wall_clock_datetimes = 0;
|
|
2792
|
+
}
|
|
2111
2793
|
}
|
|
2112
2794
|
}
|
|
2113
2795
|
|
|
@@ -2138,6 +2820,7 @@ static napi_value fn_query(napi_env env, napi_callback_info info) {
|
|
|
2138
2820
|
napi_create_error(env, NULL, msg, &err);
|
|
2139
2821
|
napi_reject_deferred(env, w->deferred, err);
|
|
2140
2822
|
free(w->sql);
|
|
2823
|
+
free(w->out_path);
|
|
2141
2824
|
free(w);
|
|
2142
2825
|
}
|
|
2143
2826
|
|
|
@@ -2211,6 +2894,24 @@ static napi_value fn_is_alive(napi_env env, napi_callback_info info) {
|
|
|
2211
2894
|
return result;
|
|
2212
2895
|
}
|
|
2213
2896
|
|
|
2897
|
+
// ---------------------------------------------------------------------------
|
|
2898
|
+
// Server character set
|
|
2899
|
+
// ---------------------------------------------------------------------------
|
|
2900
|
+
|
|
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) {
|
|
2903
|
+
size_t argc = 1;
|
|
2904
|
+
napi_value args[1];
|
|
2905
|
+
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
2906
|
+
ConnHandle *handle = get_conn_handle(env, args[0]);
|
|
2907
|
+
if (handle == NULL) {
|
|
2908
|
+
return NULL; // TypeError pending
|
|
2909
|
+
}
|
|
2910
|
+
napi_value result;
|
|
2911
|
+
napi_create_string_utf8(env, handle->conn ? handle->conn->charset : "", NAPI_AUTO_LENGTH, &result);
|
|
2912
|
+
return result;
|
|
2913
|
+
}
|
|
2914
|
+
|
|
2214
2915
|
// ---------------------------------------------------------------------------
|
|
2215
2916
|
// Get version
|
|
2216
2917
|
// ---------------------------------------------------------------------------
|
|
@@ -2254,6 +2955,9 @@ static napi_value init(napi_env env, napi_value exports) {
|
|
|
2254
2955
|
napi_create_function(env, "isAlive", 0, fn_is_alive, NULL, &fn);
|
|
2255
2956
|
napi_set_named_property(env, exports, "isAlive", fn);
|
|
2256
2957
|
|
|
2958
|
+
napi_create_function(env, "serverCharset", 0, fn_server_charset, NULL, &fn);
|
|
2959
|
+
napi_set_named_property(env, exports, "serverCharset", fn);
|
|
2960
|
+
|
|
2257
2961
|
napi_create_function(env, "getVersion", 0, fn_get_version, NULL, &fn);
|
|
2258
2962
|
napi_set_named_property(env, exports, "getVersion", fn);
|
|
2259
2963
|
|