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
@@ -0,0 +1,592 @@
1
+ Sybase ASE bulk copy fixes from FreeTDS master, backported to FreeTDS 1.5.17.
2
+
3
+ None of these are in a FreeTDS release yet (1.5.21 is the latest at the time of
4
+ writing); they are expected in 1.6. Remove this patch once FreeTDS is upgraded to
5
+ a release that contains them.
6
+
7
+ Each file this patch changes carries a notice saying so. Changes made on
8
+ 2026-11-11 (the drizzle-sybase git history dates each one).
9
+
10
+ Upstream commits (https://github.com/FreeTDS/freetds):
11
+ e1133a1685 [DOL BULK] Add TDS_REQ_DOL_BULK to capabilities.
12
+ c431b958d2 [DOL BULK] Probe for datarows-lock on the table
13
+ 6f65b6e1ba [DOL BULK] Row data packet layout
14
+ 4b0ff06729 [DOL BULK] Also support freebcp being invoked with database.owner.tablename
15
+ c78eb67371 [DOL BULK] Perform the locktype query before the columns query - hidden dependency
16
+ 29de16d9c4 [DOL BULK] Documentation for sysstat2; warn about slow index for datarows-locked table
17
+ 556003bb85 tds: Detect data rows locking looking to capabilities
18
+ 797b63215a bcp: (ASE) Correctly detect Datarows Locking for temporary tables
19
+ 0f34ecbd52 bcp in: (ASE) Adjust row buffer size formula to allow for datarows-locked format
20
+ afc0b85cd8 freebcp: Implement -k (meaning -hKEEP_NULLS) (libTDS part only)
21
+ 5703d0b96b tds: Do not ignore conversion errors in Sybase bulk copy (libTDS part only)
22
+ bce8b3791e tds: Remove trailing zeroes from binary data in Sybase bulk copy (libTDS part only)
23
+
24
+ Why drizzle-sybase needs them:
25
+ - Without the DOL_BULK capability and row format, ASE refuses a bulk copy into a
26
+ data-only-locked (datarows) table: Msg 4845, "Client does not have the
27
+ capability to bulk copy into the data-only locked table".
28
+ - KEEP_NULLS (bcp_options BCPHINTS) is ASE bcp's --ignoredefaults: a NULL stays
29
+ NULL instead of becoming the column default (bulkInsert's emptyAsNull).
30
+ - A value that fails conversion must fail its row, not be sent as NULL/default.
31
+
32
+ Adaptation to 1.5.17: c431b958d2's own int conversion is kept (master later
33
+ switched to tds_try_get_int_col, which 1.5.17 does not have); otherwise the code
34
+ is master's final form of each hunk, with these fixes master does not have yet
35
+ (all checked against ASE 15.7):
36
+ - datapages-locked tables are data-only locked too, and take the same DOL
37
+ format; master only detects datarows (sysstat2 0x8000, not 0x4000), so a
38
+ copy into a datapages table fails with Msg 4837 (bad row length).
39
+ - With KEEP_NULLS, master skips reading the default values but still
40
+ substitutes them, as empty values: a NULL char column with a default gets a
41
+ blank instead of NULL. Here a column is treated as having no default.
42
+ - A #temp table lives in the session's temporary database, which on a server
43
+ with several (tempdb, tempdb_2, ...) is often not tempdb: master looks it up
44
+ in tempdb..sysobjects, misses it, and sends the wrong row format (Msg 4837).
45
+ Here its lock scheme is read with lockscheme(object_id, @@tempdbid).
46
+ - The lock scheme probe looked the table up by bare name in sysobjects, which
47
+ matches every owner's table of that name (owner.table also put the owner
48
+ in front of sysobjects); it now reads lockscheme() of exactly the target
49
+ object, in its database. A failed probe, or an unknown lock scheme, fails
50
+ the copy instead of being taken for an allpages table (the probe's own
51
+ statement errors are checked too).
52
+ - The row buffer was sized by a formula that did not bound what the row
53
+ writer writes: in the DOL format it left out the 2-byte offset per variable
54
+ column (100 varbinary(8) columns given 8 bytes each wrote 93 bytes past
55
+ it), and in the allpages format it counted the adjustment table from the
56
+ variable data alone, not from the end of the whole row (six full
57
+ binary(248) and a full varbinary(8) wrote 3 bytes past it). It is now sized
58
+ for the largest row of either format, whichever columns the server makes
59
+ fixed or variable.
60
+ - The variable-column offsets are a 256-entry array and their count one
61
+ byte: a row with more than 255 variable columns wrote past the array. It
62
+ now fails instead (drizzle-sybase refuses such a table before starting).
63
+ - Reading the column defaults ignored a failed allocation, leaving a default
64
+ size without its value (later copied from a NULL or stale buffer); and a
65
+ numeric column's buffer was used before its allocation was checked. Both
66
+ now fail the copy. A default of NULL is treated as no default.
67
+ - A NULL for a NOT NULL column without a default failed the row without a
68
+ report when no default had been read; it is now reported (SYBEBCNN) as
69
+ for any other column, so a failure without a report is never a refused
70
+ value (the caller can tell an out-of-memory failure from a bad row).
71
+ - bcp_batch() ignored a failure to open the copy again after committing a
72
+ batch: every row sent next then failed without an error, as if refused on
73
+ its own. It now reports SYBEBULKINSERT, still returning the batch's count
74
+ (the batch is committed).
75
+
76
+ diff --git a/include/freetds/tds.h b/include/freetds/tds.h
77
+ --- a/include/freetds/tds.h
78
+ +++ b/include/freetds/tds.h
79
+ @@ -18,6 +18,12 @@
80
+ * Boston, MA 02111-1307, USA.
81
+ */
82
+
83
+ +/*
84
+ + * Modified by the drizzle-sybase project, last on 2026-11-11.
85
+ + * The changes, and why, are listed in patches/freetds-1.5.17-sybase-bulk.patch
86
+ + * of drizzle-sybase; this file is otherwise FreeTDS 1.5.17's.
87
+ + */
88
+ +
89
+ #ifndef _tdsguard_hfOrWb5znoUCWdBPoNQvqN_
90
+ #define _tdsguard_hfOrWb5znoUCWdBPoNQvqN_
91
+
92
+ @@ -1700,6 +1706,7 @@
93
+ TDS_INT direction;
94
+ bool identity_insert_on;
95
+ bool xfer_init;
96
+ + bool datarows_locking;
97
+ TDS_INT bind_count;
98
+ TDSRESULTINFO *bindinfo;
99
+ TDS5COLINFO *sybase_colinfo;
100
+ diff --git a/src/dblib/bcp.c b/src/dblib/bcp.c
101
+ --- a/src/dblib/bcp.c
102
+ +++ b/src/dblib/bcp.c
103
+ @@ -16,6 +16,12 @@
104
+ * License along with this library; if not, write to the
105
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
106
+ * Boston, MA 02111-1307, USA.
107
+ + */
108
+ +
109
+ +/*
110
+ + * Modified by the drizzle-sybase project, last on 2026-11-11.
111
+ + * The changes, and why, are listed in patches/freetds-1.5.17-sybase-bulk.patch
112
+ + * of drizzle-sybase; this file is otherwise FreeTDS 1.5.17's.
113
+ */
114
+
115
+ #include <config.h>
116
+ @@ -2022,7 +2028,11 @@
117
+ if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
118
+ return -1;
119
+
120
+ - tds_bcp_start(dbproc->tds_socket, dbproc->bcpinfo);
121
+ + /* The batch is committed whatever follows: its count stands. A copy that
122
+ + * cannot be opened again is reported, so that rows sent next are not taken
123
+ + * for rows refused one by one. */
124
+ + if (TDS_FAILED(tds_bcp_start(dbproc->tds_socket, dbproc->bcpinfo)))
125
+ + dbperror(dbproc, SYBEBULKINSERT, 0);
126
+
127
+ return rows_copied;
128
+ }
129
+ diff --git a/src/tds/bulk.c b/src/tds/bulk.c
130
+ --- a/src/tds/bulk.c
131
+ +++ b/src/tds/bulk.c
132
+ @@ -17,6 +17,12 @@
133
+ * Boston, MA 02111-1307, USA.
134
+ */
135
+
136
+ +/*
137
+ + * Modified by the drizzle-sybase project, last on 2026-11-11.
138
+ + * The changes, and why, are listed in patches/freetds-1.5.17-sybase-bulk.patch
139
+ + * of drizzle-sybase; this file is otherwise FreeTDS 1.5.17's.
140
+ + */
141
+ +
142
+ /**
143
+ * \file
144
+ * \brief Handle bulk copy
145
+ @@ -46,6 +52,7 @@
146
+ #include <freetds/convert.h>
147
+ #include <freetds/utils/string.h>
148
+ #include <freetds/replacements.h>
149
+ +#include <freetds/enum_cap.h>
150
+
151
+ /** \cond HIDDEN_SYMBOLS */
152
+ #ifndef MAX
153
+ @@ -74,6 +81,14 @@
154
+ int offset, TDS_UCHAR *rowbuffer, int start, int *pncols);
155
+ static void tds_bcp_row_free(TDSRESULTINFO* result, unsigned char *row);
156
+ static TDSRET tds5_process_insert_bulk_reply(TDSSOCKET * tds, TDSBCPINFO *bcpinfo);
157
+ +static TDSRET probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
158
+ +
159
+ +/** Whether the KEEP_NULLS hint (ASE bcp's --ignoredefaults) was given. */
160
+ +static bool
161
+ +tds5_keep_nulls(TDSBCPINFO *bcpinfo)
162
+ +{
163
+ + return !tds_dstr_isempty(&bcpinfo->hint) && strstr(tds_dstr_cstr(&bcpinfo->hint), "KEEP_NULLS") != NULL;
164
+ +}
165
+ static TDSRET tds5_get_col_data_or_dflt(tds_bcp_get_col_data get_col_data,
166
+ TDSBCPINFO * bulk, TDSCOLUMN * bcpcol, int offset, int colnum);
167
+
168
+ @@ -97,6 +112,12 @@
169
+
170
+ /* FIXME don't leave state in processing state */
171
+
172
+ + /* Check table locking type. Do this first, because the code in bcp_init() which
173
+ + * calls us seems to depend on the information from the columns query still being
174
+ + * active in the context.
175
+ + */
176
+ + TDS_PROPAGATE(probe_sap_locking(tds, bcpinfo));
177
+ +
178
+ /* TODO quote tablename if needed */
179
+ if (bcpinfo->direction != TDS_BCP_QUERYOUT)
180
+ fmt = "SET FMTONLY ON select * from %s SET FMTONLY OFF";
181
+ @@ -177,6 +198,8 @@
182
+
183
+ if (is_numeric_type(curcol->column_type)) {
184
+ curcol->bcp_column_data = tds_alloc_bcp_column_data(sizeof(TDS_NUMERIC));
185
+ + if (!curcol->bcp_column_data)
186
+ + goto cleanup;
187
+ ((TDS_NUMERIC *) curcol->bcp_column_data->data)->precision = curcol->column_prec;
188
+ ((TDS_NUMERIC *) curcol->bcp_column_data->data)->scale = curcol->column_scale;
189
+ } else {
190
+ @@ -218,6 +241,116 @@
191
+ }
192
+
193
+ /**
194
+ + * Detect if table we're writing to uses 'datarows' lockmode.
195
+ + * \tds
196
+ + * \param bcpinfo BCP information already prepared
197
+ + * \return TDS_SUCCESS or TDS_FAIL.
198
+ + */
199
+ +static TDSRET
200
+ +probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
201
+ +{
202
+ + TDSRET rc;
203
+ + unsigned int value;
204
+ + bool value_found;
205
+ + TDS_INT resulttype;
206
+ + int done_flags;
207
+ + bool failed;
208
+ + const char *full_tablename, *dot;
209
+ +
210
+ + /* Only needed for inward data */
211
+ + if (bcpinfo->direction != TDS_BCP_IN)
212
+ + return TDS_SUCCESS;
213
+ +
214
+ + /* Only needed for SAP ASE versions which support datarows-locking. */
215
+ + if (!TDS_IS_SYBASE(tds) || !tds_capability_has_req(tds->conn, TDS_REQ_DOL_BULK))
216
+ + return TDS_SUCCESS;
217
+ +
218
+ + /* The lock scheme of exactly the target object -- 1 for allpages, 2 for
219
+ + * data-only locking (datapages or datarows), 0 if unknown: looked up by its full name (owner included), in its own
220
+ + * database -- the named one for db.owner.table / db..table, the session's
221
+ + * temporary database for a #temp table (on a server with several, not always
222
+ + * tempdb), else the current one. A lookup by bare name in sysobjects could
223
+ + * match another owner's table of the same name. */
224
+ + full_tablename = tds_dstr_cstr(&bcpinfo->tablename);
225
+ + dot = strchr(full_tablename, '.');
226
+ + if (full_tablename[0] == '#') {
227
+ + rc = tds_submit_queryf(tds, "select case lockscheme(object_id('%s'), @@tempdbid) "
228
+ + "when 'allpages' then 1 when 'datarows' then 2 when 'datapages' then 2 else 0 end",
229
+ + full_tablename);
230
+ + } else if (dot != NULL && strchr(dot + 1, '.') != NULL) {
231
+ + rc = tds_submit_queryf(tds, "select case lockscheme(object_id('%s'), db_id('%.*s')) "
232
+ + "when 'allpages' then 1 when 'datarows' then 2 when 'datapages' then 2 else 0 end",
233
+ + full_tablename, (int) (dot - full_tablename), full_tablename);
234
+ + } else {
235
+ + rc = tds_submit_queryf(tds, "select case lockscheme(object_id('%s')) "
236
+ + "when 'allpages' then 1 when 'datarows' then 2 when 'datapages' then 2 else 0 end",
237
+ + full_tablename);
238
+ + }
239
+ + TDS_PROPAGATE(rc);
240
+ +
241
+ + value = 0;
242
+ + value_found = false;
243
+ + failed = false;
244
+ +
245
+ + while ((rc = tds_process_tokens(tds, &resulttype, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
246
+ + const unsigned int stop_mask = TDS_RETURN_DONE | TDS_RETURN_ROW;
247
+ +
248
+ + if (resulttype == TDS_DONE_RESULT && (done_flags & TDS_DONE_ERROR))
249
+ + failed = true;
250
+ + if (resulttype != TDS_ROW_RESULT)
251
+ + continue;
252
+ +
253
+ + /* We must keep processing result tokens (even if we've found what we're looking for) so that the
254
+ + * stream is ready for subsequent queries. */
255
+ + while ((rc = tds_process_tokens(tds, &resulttype, &done_flags, stop_mask)) == TDS_SUCCESS) {
256
+ + TDSCOLUMN *col;
257
+ + TDS_SERVER_TYPE ctype;
258
+ + CONV_RESULT dres;
259
+ + TDS_INT res;
260
+ +
261
+ + if (resulttype != TDS_ROW_RESULT) {
262
+ + if (resulttype == TDS_DONE_RESULT && (done_flags & TDS_DONE_ERROR))
263
+ + failed = true;
264
+ + break;
265
+ + }
266
+ +
267
+ + /* Get INT4 from column 0 */
268
+ + if (!tds->current_results || tds->current_results->num_cols < 1)
269
+ + continue;
270
+ +
271
+ + col = tds->current_results->columns[0];
272
+ + if (col->column_cur_size < 0)
273
+ + continue;
274
+ +
275
+ + ctype = tds_get_conversion_type(col->column_type, col->column_size);
276
+ + res = tds_convert(tds_get_ctx(tds), ctype, col->column_data, col->column_cur_size, SYBINT4, &dres);
277
+ + if (res < 0)
278
+ + continue;
279
+ +
280
+ + value = dres.i;
281
+ + value_found = true;
282
+ + }
283
+ + }
284
+ + if (TDS_FAILED(rc))
285
+ + return rc;
286
+ +
287
+ + /* The row format depends on the answer: without a definite one (the probe
288
+ + * failed, or the table's lock scheme is unknown), nothing is sent. Servers
289
+ + * without data-only locking were left out above, by their capabilities. */
290
+ + if (failed || !value_found || (value != 1 && value != 2)) {
291
+ + tdsdump_log(TDS_DBG_ERROR, "[DOL BULK] No lock scheme found for '%s'.\n", full_tablename);
292
+ + return TDS_FAIL;
293
+ + }
294
+ +
295
+ + tdsdump_log(TDS_DBG_INFO1, "lock scheme %u for '%s'\n", value, full_tablename);
296
+ + if (value == 2) {
297
+ + bcpinfo->datarows_locking = true;
298
+ + tdsdump_log(TDS_DBG_INFO1, "Table has data-only locking; enabling DOL BULK format.\n");
299
+ + }
300
+ + return TDS_SUCCESS;
301
+ +}
302
+ +
303
+ +/**
304
+ * Help to build query to be sent to server.
305
+ * Append column declaration to the query.
306
+ * Only for TDS 7.0+.
307
+ @@ -406,21 +539,27 @@
308
+ tds5_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo,
309
+ tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
310
+ {
311
+ - int row_pos;
312
+ + int row_pos = 0;
313
+ int row_sz_pos;
314
+ int blob_cols = 0;
315
+ + int var_cols_pos;
316
+ int var_cols_written = 0;
317
+ TDS_INT old_record_size = bcpinfo->bindinfo->row_size;
318
+ unsigned char *record = bcpinfo->bindinfo->current_row;
319
+ int i;
320
+
321
+ memset(record, '\0', old_record_size); /* zero the rowbuffer */
322
+ +
323
+ + /* SAP ASE Datarows-locked tables expect additional 4 blank bytes before everything else */
324
+ + if (bcpinfo->datarows_locking)
325
+ + row_pos += 4;
326
+
327
+ /*
328
+ * offset 0 = number of var columns
329
+ * offset 1 = row number. zeroed (datasever assigns)
330
+ */
331
+ - row_pos = 2;
332
+ + var_cols_pos = row_pos;
333
+ + row_pos += 2;
334
+
335
+ if ((row_pos = tds5_bcp_add_fixed_columns(bcpinfo, get_col_data, null_error, offset, record, row_pos)) < 0)
336
+ return TDS_FAIL;
337
+ @@ -436,7 +575,7 @@
338
+
339
+ if (var_cols_written) {
340
+ TDS_PUT_UA2LE(&record[row_sz_pos], row_pos);
341
+ - record[0] = var_cols_written;
342
+ + record[var_cols_pos] = var_cols_written;
343
+ }
344
+
345
+ tdsdump_log(TDS_DBG_INFO1, "old_record_size = %d new size = %d \n", old_record_size, row_pos);
346
+ @@ -704,7 +843,15 @@
347
+ rowbuffer[row_pos] = ' ';
348
+ } else {
349
+ cpbytes = bcpcol->bcp_column_data->datalen > bcpcol->column_size ?
350
+ - bcpcol->column_size : bcpcol->bcp_column_data->datalen;
351
+ + bcpcol->column_size : bcpcol->bcp_column_data->datalen;
352
+ + /*
353
+ + * Sybase stores variable binary data without trailing
354
+ + * zeroes (keeping at least a byte), do the same as
355
+ + * a normal insert.
356
+ + */
357
+ + if (is_binary_type(bcpcol->on_server.column_type))
358
+ + while (cpbytes > 1 && bcpcol->bcp_column_data->data[cpbytes - 1] == 0)
359
+ + --cpbytes;
360
+ memcpy(&rowbuffer[row_pos], bcpcol->bcp_column_data->data, cpbytes);
361
+ tds5_swap_data(bcpcol, &rowbuffer[row_pos]);
362
+ }
363
+ @@ -713,6 +860,10 @@
364
+ }
365
+
366
+ row_pos += cpbytes;
367
+ + /* The offset table, and the row's one-byte count of variable columns,
368
+ + * hold at most 255 columns. */
369
+ + if (ncols >= 255)
370
+ + return -1;
371
+ offsets[++ncols] = row_pos;
372
+ tdsdump_dump_buf(TDS_DBG_NETWORK, "BCP row buffer so far", rowbuffer, row_pos);
373
+ }
374
+ @@ -740,7 +891,7 @@
375
+ while (ncols && offsets[ncols] == offsets[ncols-1])
376
+ ncols--; /* trailing NULL columns are not sent and are not included in the offset table */
377
+
378
+ - if (ncols) {
379
+ + if (ncols && !bcpinfo->datarows_locking) {
380
+ TDS_UCHAR *poff = rowbuffer + row_pos;
381
+ unsigned int pfx_top = offsets[ncols] >> 8;
382
+
383
+ @@ -764,6 +915,17 @@
384
+ for (i=0; i <= ncols; i++)
385
+ *poff++ = offsets[ncols-i] & 0xFF;
386
+ row_pos = (unsigned int)(poff - rowbuffer);
387
+ + } else { /* Datarows-locking */
388
+ + unsigned int col;
389
+ +
390
+ + for (col = ncols; col-- > 0;) {
391
+ + /* The DOL BULK format has a much simpler row table -- it's just a 2-byte length for every
392
+ + * non-fixed column (does not have the extra "offset after the end" that the basic format has) */
393
+ + rowbuffer[row_pos++] = offsets[col] % 256u;
394
+ + rowbuffer[row_pos++] = offsets[col] / 256u;
395
+ +
396
+ + tdsdump_log(TDS_DBG_FUNC, "[DOL BULK offset table] col=%u offset=%u\n", col, offsets[col]);
397
+ + }
398
+ }
399
+
400
+ tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d\n", i, ncols, row_pos);
401
+ @@ -991,13 +1153,20 @@
402
+ return -1;
403
+ }
404
+
405
+ -static void
406
+ +static TDSRET
407
+ tds5_read_bulk_defaults(TDSRESULTINFO *res_info, TDSBCPINFO *bcpinfo)
408
+ {
409
+ int i;
410
+ TDS5COLINFO *syb_info = bcpinfo->sybase_colinfo;
411
+ TDS5COLINFO *const syb_info_end = syb_info + bcpinfo->sybase_count;
412
+
413
+ + /* testing of behaviour of --ignoredefaults in ASE BCP client appears
414
+ + * to show it behaves exactly as if defaults did not exist. For example
415
+ + * a null value or omitted value for a default non-null column will
416
+ + * give an error, instead of using the default. */
417
+ + if (tds5_keep_nulls(bcpinfo))
418
+ + return TDS_SUCCESS;
419
+ +
420
+ for (i = 0; i < res_info->num_cols; ++i, ++syb_info) {
421
+ TDSCOLUMN *col = res_info->columns[i];
422
+ TDS_UCHAR *src = col->column_data;
423
+ @@ -1010,17 +1179,27 @@
424
+ for (;;) {
425
+ /* avoid overflows */
426
+ if (syb_info >= syb_info_end)
427
+ - return;
428
+ + return TDS_SUCCESS;
429
+
430
+ if (syb_info->dflt)
431
+ break;
432
+ ++syb_info;
433
+ }
434
+
435
+ + /* A default of NULL is no default: a NULL stays NULL. */
436
+ + if (len < 0) {
437
+ + syb_info->dflt = false;
438
+ + continue;
439
+ + }
440
+ + /* The size is set only with the value it describes: a default whose
441
+ + * value could not be kept must not be substituted from a stale or
442
+ + * missing buffer. */
443
+ + if (!TDS_RESIZE(syb_info->dflt_value, len))
444
+ + return TDS_FAIL;
445
+ + memcpy(syb_info->dflt_value, src, len);
446
+ syb_info->dflt_size = len;
447
+ - if (TDS_RESIZE(syb_info->dflt_value, len))
448
+ - memcpy(syb_info->dflt_value, src, len);
449
+ }
450
+ + return TDS_SUCCESS;
451
+ }
452
+
453
+ static TDSRET
454
+ @@ -1073,7 +1252,9 @@
455
+ continue;
456
+ if (!row_match) {
457
+ tds_extra_assert(res_info->num_cols == num_defs);
458
+ - tds5_read_bulk_defaults(res_info, bcpinfo);
459
+ + /* Read on to the end of the reply either way. */
460
+ + if (TDS_FAILED(tds5_read_bulk_defaults(res_info, bcpinfo)))
461
+ + ret = TDS_FAIL;
462
+ continue;
463
+ }
464
+ for (icol = 0; icol < BULKCOL_COUNT; ++icol) {
465
+ @@ -1145,14 +1326,17 @@
466
+ BCPCOLDATA *coldata;
467
+
468
+ ret = get_col_data(bulk, bcpcol, offset);
469
+ + /* errors must be reported, defaults are used only for NULL values */
470
+ + TDS_PROPAGATE(ret);
471
+ coldata = bcpcol->bcp_column_data;
472
+ if (coldata->is_null && bulk->sybase_colinfo != NULL && !is_blob_type(bcpcol->column_type)) {
473
+ const TDS5COLINFO *syb_info = &bulk->sybase_colinfo[colnum];
474
+
475
+ - if (!syb_info->dflt) {
476
+ + /* With KEEP_NULLS no default was read: the column is as if it had none.
477
+ + * A NULL is left as it is: for a NOT NULL column the caller reports it
478
+ + * (null_error), so that a failure without a report is never one. */
479
+ + if (!syb_info->dflt || tds5_keep_nulls(bulk)) {
480
+ /* leave to NULL */
481
+ - if (!bcpcol->column_nullable)
482
+ - return TDS_FAIL;
483
+ } else {
484
+ if (!TDS_RESIZE(coldata->data, syb_info->dflt_size))
485
+ return TDS_FAIL;
486
+ @@ -1186,7 +1370,6 @@
487
+ {
488
+ TDSCOLUMN *bcpcol;
489
+ int i;
490
+ - int fixed_col_len_tot = 0;
491
+ int variable_col_len_tot = 0;
492
+ int column_bcp_data_size = 0;
493
+ int bcp_record_size = 0;
494
+ @@ -1203,52 +1386,41 @@
495
+ return rc;
496
+ }
497
+
498
+ - /*
499
+ - * Work out the number of "variable" columns. These are either nullable or of
500
+ - * varying length type e.g. varchar.
501
+ + /*
502
+ + * Size the row buffer for the largest row tds5_send_record can write, in
503
+ + * either format, whichever columns the server makes fixed or variable:
504
+ + * [4 DOL bytes] 2 (count, row number) | fixed data | 2 (row length) |
505
+ + * variable data | offset table
506
+ + * The APL offset table holds one byte per variable column plus one, a count
507
+ + * byte, and one adjustment byte per 256 bytes of the whole row up to its
508
+ + * end (fixed data included); the DOL one two bytes per variable column.
509
+ */
510
+ var_cols = 0;
511
+
512
+ if (IS_TDS50(tds->conn)) {
513
+ + int row_end;
514
+ +
515
+ for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
516
+ -
517
+ bcpcol = bcpinfo->bindinfo->columns[i];
518
+
519
+ - /*
520
+ - * work out storage required for this datatype
521
+ - * blobs always require 16, numerics vary, the
522
+ - * rest can be taken from the server
523
+ - */
524
+ -
525
+ + /* the most this column's value can take in the row */
526
+ if (is_blob_type(bcpcol->on_server.column_type))
527
+ - column_bcp_data_size = 16;
528
+ + column_bcp_data_size = 16;
529
+ else if (is_numeric_type(bcpcol->on_server.column_type))
530
+ - column_bcp_data_size = tds_numeric_bytes_per_prec[bcpcol->column_prec];
531
+ + column_bcp_data_size = tds_numeric_bytes_per_prec[MAXPRECISION];
532
+ else
533
+ - column_bcp_data_size = bcpcol->column_size;
534
+ + column_bcp_data_size = MAX(bcpcol->column_size, bcpcol->on_server.column_size);
535
+ + if (column_bcp_data_size < 1)
536
+ + column_bcp_data_size = 1; /* an empty char is sent as a space */
537
+
538
+ - /*
539
+ - * now add that size into either fixed or variable
540
+ - * column totals...
541
+ - */
542
+ -
543
+ - if (is_nullable_type(bcpcol->on_server.column_type) || bcpcol->column_nullable) {
544
+ - var_cols++;
545
+ - variable_col_len_tot += column_bcp_data_size;
546
+ - } else {
547
+ - fixed_col_len_tot += column_bcp_data_size;
548
+ - }
549
+ + variable_col_len_tot += column_bcp_data_size; /* fixed or variable: all count */
550
+ + var_cols++;
551
+ }
552
+
553
+ - /* this formula taken from sybase manual... */
554
+ + row_end = (bcpinfo->datarows_locking ? 4 : 0) + 2 + variable_col_len_tot + 2;
555
+ + bcp_record_size = row_end +
556
+ + (bcpinfo->datarows_locking ? 2 * var_cols : 1 + (row_end >> 8) + var_cols + 1);
557
+
558
+ - bcp_record_size = 4 +
559
+ - fixed_col_len_tot +
560
+ - variable_col_len_tot +
561
+ - ( (int)(variable_col_len_tot / 256 ) + 1 ) +
562
+ - (var_cols + 1) +
563
+ - 2;
564
+ -
565
+ tdsdump_log(TDS_DBG_FUNC, "current_record_size = %d\n", bcpinfo->bindinfo->row_size);
566
+ tdsdump_log(TDS_DBG_FUNC, "bcp_record_size = %d\n", bcp_record_size);
567
+
568
+ diff --git a/src/tds/mem.c b/src/tds/mem.c
569
+ --- a/src/tds/mem.c
570
+ +++ b/src/tds/mem.c
571
+ @@ -16,6 +16,12 @@
572
+ * License along with this library; if not, write to the
573
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
574
+ * Boston, MA 02111-1307, USA.
575
+ + */
576
+ +
577
+ +/*
578
+ + * Modified by the drizzle-sybase project, last on 2026-11-11.
579
+ + * The changes, and why, are listed in patches/freetds-1.5.17-sybase-bulk.patch
580
+ + * of drizzle-sybase; this file is otherwise FreeTDS 1.5.17's.
581
+ */
582
+
583
+ #include <config.h>
584
+ @@ -775,7 +781,7 @@
585
+ REQ(i,CSR_PREV) REQ(i,CSR_FIRST) REQ(i,CSR_LAST) REQ(i,CSR_ABS) REQ(i,CSR_REL) \
586
+ REQ(i,CSR_MULTI) REQ(i,CON_INBAND) REQ(i,PROTO_TEXT) REQ(i,PROTO_BULK) \
587
+ REQ(i,DATA_SENSITIVITY) REQ(i,DATA_BOUNDARY) REQ(i,PROTO_DYNPROC) REQ(i,DATA_FLTN) \
588
+ - REQ(i,DATA_BITN) REQ(i,DATA_INT8) REQ(i,WIDETABLE) \
589
+ + REQ(i,DATA_BITN) REQ(i,DATA_INT8) REQ(i,DOL_BULK) REQ(i,WIDETABLE) \
590
+ REQ(i,DATA_UINT2) REQ(i,DATA_UINT4) REQ(i,DATA_UINT8) REQ(i,DATA_UINTN) REQ(i,LARGEIDENT) \
591
+ REQ(i,SRVPKTSIZE) REQ(i,DATA_DATE) REQ(i,DATA_TIME) REQ(i,DATA_BIGTIME) REQ(i,DATA_BIGDATETIME)
592
+ #define REQ(i,n) |(((TDS_REQ_ ## n / 8) == i)?(1<<(TDS_REQ_ ## n & 7)):0)
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Rebuild the native addon (`node-gyp rebuild`) and put it where the loader
3
+ * looks: `src/native/sybase_native.<platform>-<arch>.node`, and `dist/native/`
4
+ * when it exists (the installed package loads from there).
5
+ *
6
+ * node scripts/build-native.ts
7
+ *
8
+ * node-gyp alone leaves the result in `build/Release/`, which nothing loads: a
9
+ * rebuild would seem to succeed while the old addon kept running. Needs the
10
+ * FreeTDS static library in `deps/` (`scripts/build.sh` or `build-win.ps1`
11
+ * builds it, and the addon, from scratch).
12
+ */
13
+
14
+ import { execFileSync } from "child_process";
15
+ import { copyFileSync, existsSync } from "fs";
16
+ import { join, resolve } from "path";
17
+
18
+ const root = resolve(import.meta.dirname, "..");
19
+ const target = `sybase_native.${process.platform}-${process.arch}.node`;
20
+
21
+ execFileSync(process.platform === "win32" ? "npx.cmd" : "npx", ["node-gyp", "rebuild"], {
22
+ cwd: root,
23
+ stdio: "inherit",
24
+ shell: process.platform === "win32"
25
+ });
26
+
27
+ const built = join(root, "build", "Release", "sybase_native.node");
28
+ for (const dir of ["src/native", "dist/native"]) {
29
+ const dest = join(root, dir);
30
+ if (existsSync(dest)) {
31
+ copyFileSync(built, join(dest, target));
32
+ console.log(`==> ${dir}/${target}`);
33
+ }
34
+ }