Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3 : * Copyright (C) 2006-2015 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : #include <config.h>
22 :
23 : #include <stdarg.h>
24 :
25 : #include <freetds/time.h>
26 :
27 : #include <assert.h>
28 : #include <stdio.h>
29 :
30 : #if HAVE_STDLIB_H
31 : #include <stdlib.h>
32 : #endif /* HAVE_STDLIB_H */
33 :
34 : #if HAVE_STRING_H
35 : #include <string.h>
36 : #endif /* HAVE_STRING_H */
37 :
38 : #if HAVE_UNISTD_H
39 : #include <unistd.h>
40 : #endif /* HAVE_UNISTD_H */
41 :
42 : #if HAVE_ERRNO_H
43 : # include <errno.h>
44 : #endif /* HAVE_ERRNO_H */
45 :
46 : /**
47 : * \ingroup dblib_core
48 : * \remarks Either SYBDBLIB or MSDBLIB (not both) must be defined.
49 : * This affects how certain application-addressable
50 : * strucures are defined.
51 : */
52 : #include <freetds/tds.h>
53 : #include <freetds/thread.h>
54 : #include <freetds/tds/convert.h>
55 : #include <freetds/utils/string.h>
56 : #include <freetds/tds/data.h>
57 : #include <freetds/replacements.h>
58 : #include <sybfront.h>
59 : #include <sybdb.h>
60 : #include <syberror.h>
61 : #include <dblib.h>
62 :
63 : static RETCODE _dbresults(DBPROCESS * dbproc);
64 : static BYTE *_dbcoldata(TDSCOLUMN *colinfo);
65 : static int _get_printable_size(TDSCOLUMN * colinfo);
66 : static char *_dbprdate(char *timestr);
67 : static int _dbnullable(DBPROCESS * dbproc, int column);
68 : static const char *tds_prdatatype(int datatype_token);
69 :
70 : static int default_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr);
71 :
72 : void copy_data_to_host_var(DBPROCESS *, TDS_SERVER_TYPE, const BYTE *, int, BYTE *, DBINT, int, DBINT *);
73 : RETCODE dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr);
74 :
75 : /**
76 : * \file dblib.c
77 : * Main implementation file for \c db-lib.
78 : */
79 : /**
80 : * \file bcp.c
81 : * Implementation of \c db-lib bulk copy functions.
82 : */
83 : /**
84 : * \defgroup dblib_api The db-lib API
85 : * Functions callable by \c db-lib client programs
86 : *
87 : * The \c db_lib interface is implemented by both Sybase and Microsoft. FreeTDS seeks to implement
88 : * first the intersection of the functions defined by the vendors.
89 : */
90 :
91 : /**
92 : * \ingroup dblib_api
93 : * \defgroup dblib_core Primary functions
94 : * Core functions needed by most db-lib programs.
95 : */
96 : /**
97 : * \ingroup dblib_api
98 : * \defgroup dblib_rpc Remote Procedure functions
99 : * Functions used with stored procedures.
100 : * Especially useful for OUTPUT parameters, because modern Microsoft servers do not
101 : * return output parameter data to the client unless the procedure was invoked
102 : * with dbrpcsend().
103 : */
104 : /**
105 : * \ingroup dblib_api
106 : * \defgroup dblib_bcp Bulk copy functions
107 : * Functions to bulk-copy (a/k/a \em bcp) data to/from the database.
108 : */
109 : /**
110 : * \ingroup dblib_bcp
111 : * \defgroup dblib_bcp_internal Internal bcp functions
112 : * Static functions internal to the bcp library.
113 : */
114 : /**
115 : * \ingroup dblib_api
116 : * \defgroup dblib_money Money functions
117 : * Functions to manipulate the MONEY datatype.
118 : */
119 : /**
120 : * \ingroup dblib_api
121 : * \defgroup dblib_datetime Datetime functions
122 : * Functions to manipulate DBDATETIME structures. Defined by Sybase only.
123 : * These are not implemented:
124 : * - dbdate4cmp()
125 : * - dbdate4zero()
126 : * - dbdatechar()
127 : * - dbdatename()
128 : * - dbdateorder()
129 : * - dbdatepart()
130 : * - dbdatezero()
131 : * - dbdayname()
132 : */
133 : /**
134 : * \ingroup dblib_api
135 : * \defgroup dblib_internal Internals
136 : * Functions called within \c db-lib for self-help.
137 : * These functions are of interest only to people hacking on the FreeTDS db-lib implementation.
138 : */
139 : /**
140 : * \ingroup dblib_api
141 : * \defgroup dblib_unimplemented Unimplemented
142 : * Functions thus far not implemented in the FreeTDS db-lib implementation.
143 : * While some of these are simply awaiting someone with time and skill (and inclination)
144 : * it might be noted here that the old browse functions (e.g. dbcolbrowse())
145 : * are on the never-to-do list.
146 : * They were defined by Sybase and were superseded long ago, although they're still
147 : * present in Microsoft's implementation.
148 : * They were never popular and today better alternatives are available.
149 : * For completeness, they are:
150 : * - dbcolbrowse()
151 : * - dbcolsource()
152 : * - dbfreequal()
153 : * - dbqual()
154 : * - dbtabbrowse()
155 : * - dbtabcount()
156 : * - dbtabname()
157 : * - dbtabsource()
158 : * - dbtsnewlen()
159 : * - dbtsnewval()
160 : * - dbtsput()
161 : */
162 :
163 : /* info/err message handler functions (or rather pointers to them) */
164 : MHANDLEFUNC _dblib_msg_handler = NULL;
165 : EHANDLEFUNC _dblib_err_handler = default_err_handler;
166 :
167 : /** \internal
168 : * \dblib_internal
169 : * \remarks A db-lib connection has an implicit TDS context.
170 : */
171 : typedef struct dblib_context
172 : {
173 : /** reference count, time dbinit called */
174 : int ref_count;
175 :
176 : /** libTDS context */
177 : TDSCONTEXT *tds_ctx;
178 : /** libTDS context reference counter */
179 : int tds_ctx_ref_count;
180 :
181 : /* save all connection in a list */
182 : TDSSOCKET **connection_list;
183 : int connection_list_size;
184 : int connection_list_size_represented;
185 : char *recftos_filename;
186 : int recftos_filenum;
187 : int login_timeout; /**< not used unless positive */
188 : int query_timeout; /**< not used unless positive */
189 : }
190 : DBLIBCONTEXT;
191 :
192 : static DBLIBCONTEXT g_dblib_ctx;
193 : static tds_mutex dblib_mutex = TDS_MUTEX_INITIALIZER;
194 :
195 : static int g_dblib_version =
196 : #if TDS50
197 : DBVERSION_100;
198 : #elif TDS71
199 : DBVERSION_71;
200 : #elif TDS72
201 : DBVERSION_72;
202 : #elif TDS73
203 : DBVERSION_73;
204 : #elif TDS74
205 : DBVERSION_74;
206 : #else
207 : DBVERSION_UNKNOWN;
208 : #endif
209 :
210 :
211 : static int
212 874 : dblib_add_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
213 : {
214 874 : int i = 0;
215 874 : const int list_size = ctx->connection_list_size_represented;
216 :
217 874 : tdsdump_log(TDS_DBG_FUNC, "dblib_add_connection(%p, %p)\n", ctx, tds);
218 :
219 1476 : while (i < list_size && ctx->connection_list[i])
220 602 : i++;
221 874 : if (i == list_size) {
222 : return 1;
223 : } else {
224 864 : ctx->connection_list[i] = tds;
225 864 : return 0;
226 : }
227 : }
228 :
229 : static void
230 466 : dblib_del_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
231 : {
232 466 : int i = 0;
233 466 : const int list_size = ctx->connection_list_size;
234 :
235 466 : tdsdump_log(TDS_DBG_FUNC, "dblib_del_connection(%p, %p)\n", ctx, tds);
236 :
237 41976 : while (i < list_size && ctx->connection_list[i] != tds)
238 41510 : i++;
239 466 : if (i == list_size) {
240 : /* connection wasn't on the free list...now what */
241 : } else {
242 : /* remove it */
243 456 : ctx->connection_list[i] = NULL;
244 : }
245 466 : }
246 :
247 : /**
248 : * Get global dblib TDS context.
249 : * dblib_mutex should be locked.
250 : */
251 : static TDSCONTEXT *
252 1386 : dblib_get_tds_ctx(void)
253 : {
254 1386 : tdsdump_log(TDS_DBG_FUNC, "dblib_get_tds_ctx(void)\n");
255 :
256 1386 : ++g_dblib_ctx.tds_ctx_ref_count;
257 1386 : if (g_dblib_ctx.tds_ctx == NULL) {
258 512 : TDSCONTEXT *tds_ctx = tds_alloc_context(&g_dblib_ctx);
259 :
260 512 : if (!tds_ctx)
261 : return NULL;
262 :
263 : /*
264 : * Set the functions in the TDS layer to point to the correct handler functions
265 : */
266 512 : tds_ctx->msg_handler = _dblib_handle_info_message;
267 512 : tds_ctx->err_handler = _dblib_handle_err_message;
268 512 : tds_ctx->int_handler = _dblib_check_and_handle_interrupt;
269 :
270 512 : if (tds_ctx->locale && !tds_ctx->locale->datetime_fmt) {
271 : /* set default in case there's no locale file */
272 : static const char datetime_format[] = "%b %e %Y %l:%M:%S:%z%p";
273 0 : tds_ctx->locale->datetime_fmt = strdup(datetime_format);
274 0 : if (!tds_ctx->locale->datetime_fmt) {
275 0 : tds_free_context(tds_ctx);
276 0 : return NULL;
277 : }
278 : }
279 512 : g_dblib_ctx.tds_ctx = tds_ctx;
280 : }
281 1386 : return g_dblib_ctx.tds_ctx;
282 : }
283 :
284 : /**
285 : * Decrease global dblib TDS context.
286 : * dblib_mutex should be locked.
287 : *
288 : * @param count number of references to decrease.
289 : */
290 : static void
291 948 : dblib_release_tds_ctx(int count)
292 : {
293 948 : tdsdump_log(TDS_DBG_FUNC, "dblib_release_tds_ctx(%d)\n", count);
294 :
295 948 : g_dblib_ctx.tds_ctx_ref_count -= count;
296 948 : if (g_dblib_ctx.tds_ctx_ref_count <= 0) {
297 482 : tds_free_context(g_dblib_ctx.tds_ctx);
298 482 : g_dblib_ctx.tds_ctx = NULL;
299 : }
300 948 : }
301 :
302 : #include "buffering.h"
303 :
304 : static void
305 3228 : db_env_chg(TDSSOCKET * tds, int type, char *oldval, char *newval)
306 : {
307 : DBPROCESS *dbproc;
308 :
309 3228 : assert(oldval != NULL && newval != NULL);
310 3228 : if (strlen(oldval) == 1 && *oldval == 1)
311 0 : oldval = "(0x1)";
312 :
313 3228 : tdsdump_log(TDS_DBG_FUNC, "db_env_chg(%p, %d, %s, %s)\n", tds, type, oldval, newval);
314 :
315 3228 : if (!tds || !(dbproc = (DBPROCESS *) tds_get_parent(tds)))
316 : return;
317 :
318 3228 : dbproc->envchange_rcv |= (1 << (type - 1));
319 3228 : switch (type) {
320 1676 : case TDS_ENV_DATABASE:
321 1676 : strlcpy(dbproc->dbcurdb, newval, sizeof(dbproc->dbcurdb));
322 1676 : break;
323 0 : case TDS_ENV_CHARSET:
324 0 : strlcpy(dbproc->servcharset, newval, sizeof(dbproc->servcharset));
325 0 : break;
326 : default:
327 : break;
328 : }
329 : return;
330 : }
331 :
332 : /** \internal
333 : * \ingroup dblib_internal
334 : * \brief Sanity checks for column-oriented functions.
335 : *
336 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
337 : * \param pcolinfo address of pointer to a TDSCOLUMN structure.
338 : * \remarks Makes sure dbproc and the requested column are valid.
339 : * Calls dbperror() if not.
340 : * \returns appropriate error or SUCCEED
341 : */
342 : static TDSCOLUMN*
343 12074 : dbcolptr(DBPROCESS* dbproc, int column)
344 : {
345 : TDSSOCKET *tds;
346 : TDSRESULTINFO *info;
347 :
348 12074 : if (!dbproc) {
349 0 : dbperror(dbproc, SYBENULL, 0);
350 0 : return NULL;
351 : }
352 12074 : tds = dbproc->tds_socket;
353 12074 : if (IS_TDSDEAD(tds)) {
354 0 : dbperror(dbproc, SYBEDDNE, 0);
355 0 : return NULL;
356 : }
357 12074 : info = tds->res_info;
358 12074 : if (!info)
359 : return NULL;
360 11814 : if (column < 1 || column > info->num_cols) {
361 0 : dbperror(dbproc, SYBECNOR, 0);
362 0 : return NULL;
363 : }
364 :
365 11814 : return info->columns[column - 1];
366 : }
367 :
368 : static TDSCOLUMN*
369 64 : dbacolptr(DBPROCESS* dbproc, int computeid, int column, bool is_bind)
370 : {
371 : unsigned int i;
372 : TDSSOCKET *tds;
373 : TDSCOMPUTEINFO *info;
374 :
375 64 : if (!dbproc) {
376 0 : dbperror(dbproc, SYBENULL, 0);
377 0 : return NULL;
378 : }
379 64 : tds = dbproc->tds_socket;
380 64 : if (IS_TDSDEAD(tds)) {
381 0 : dbperror(dbproc, SYBEDDNE, 0);
382 0 : return NULL;
383 : }
384 32 : for (i = 0;; ++i) {
385 128 : if (i >= tds->num_comp_info) {
386 : /* Attempt to bind user variable to a non-existent compute row */
387 0 : if (is_bind)
388 0 : dbperror(dbproc, SYBEBNCR, 0);
389 : return NULL;
390 : }
391 96 : info = tds->comp_info[i];
392 96 : if (info->computeid == computeid)
393 : break;
394 : }
395 : /* Fail if either the compute id or the column number is invalid. */
396 64 : if (column < 1 || column > info->num_cols) {
397 0 : dbperror(dbproc, is_bind ? SYBEABNC : SYBECNOR, 0);
398 0 : return NULL;
399 : }
400 :
401 64 : return info->columns[column - 1];
402 : }
403 :
404 : /*
405 : * Default null substitution values
406 : * Binding Type Null Substitution Value
407 : * TINYBIND 0
408 : * SMALLBIND 0
409 : * INTBIND 0
410 : * CHARBIND Empty string (padded with blanks)
411 : * STRINGBIND Empty string (padded with blanks, null-terminated)
412 : * NTBSTRINGBIND Empty string (null-terminated)
413 : * VARYCHARBIND Empty string
414 : * BINARYBIND Empty array (padded with zeros)
415 : * VARYBINBIND Empty array
416 : * DATETIMEBIND 8 bytes of zeros
417 : * SMALLDATETIMEBIND 8 bytes of zeros
418 : * MONEYBIND $0.00
419 : * SMALLMONEYBIND $0.00
420 : * FLT8BIND 0.0
421 : * REALBIND 0.0
422 : * DECIMALBIND 0.0 (with default scale and precision)
423 : * NUMERICBIND 0.0 (with default scale and precision)
424 : * BOUNDARYBIND Empty string (null-terminated)
425 : * SENSITIVITYBIND Empty string (null-terminated)
426 : */
427 :
428 : static const DBBIT null_BIT = 0;
429 : static const DBTINYINT null_TINYINT = 0;
430 : static const DBSMALLINT null_SMALLINT = 0;
431 : static const DBINT null_INT = 0;
432 : static const DBBIGINT null_BIGINT = 0;
433 : static const DBFLT8 null_FLT8 = 0;
434 : static const DBREAL null_REAL = 0;
435 :
436 : static const DBCHAR null_CHAR = '\0';
437 : static const DBVARYCHAR null_VARYCHAR = { 0, {0} };
438 :
439 : static const DBDATETIME null_DATETIME = { 0, 0 };
440 : static const DBDATETIME4 null_SMALLDATETIME = { 0, 0 };
441 : static const DBMONEY null_MONEY = { 0, 0 };
442 : static const DBMONEY4 null_SMALLMONEY = {0};
443 : static const DBNUMERIC null_NUMERIC = { 0, 0, {0} };
444 : static const TDS_DATETIMEALL null_DATETIMEALL = { 0, 0, 0, 0 };
445 :
446 : static NULLREP default_null_representations[MAXBINDTYPES] = {
447 : /* CHARBIND 0 */ { NULL, 0 }
448 : /* STRINGBIND 1 */ , { NULL, 0 }
449 : /* NTBSTRINGBIND 2 */ , { (BYTE*) &null_CHAR, sizeof(null_CHAR) }
450 : /* VARYCHARBIND 3 */ , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
451 : /* VARYBINBIND 4 */ , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
452 : /* no such bind 5 */ , { NULL, 0 }
453 : /* TINYBIND 6 */ , { &null_TINYINT, sizeof(null_TINYINT) }
454 : /* SMALLBIND 7 */ , { (BYTE*) &null_SMALLINT, sizeof(null_SMALLINT) }
455 : /* INTBIND 8 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
456 : /* FLT8BIND 9 */ , { (BYTE*) &null_FLT8, sizeof(null_FLT8) }
457 : /* REALBIND 10 */ , { (BYTE*) &null_REAL, sizeof(null_REAL) }
458 : /* DATETIMEBIND 11 */ , { (BYTE*) &null_DATETIME, sizeof(null_DATETIME) }
459 : /* SMALLDATETIMEBIND 12 */ , { (BYTE*) &null_SMALLDATETIME, sizeof(null_SMALLDATETIME) }
460 : /* MONEYBIND 13 */ , { (BYTE*) &null_MONEY, sizeof(null_MONEY) }
461 : /* SMALLMONEYBIND 14 */ , { (BYTE*) &null_SMALLMONEY, sizeof(null_SMALLMONEY) }
462 : /* BINARYBIND 15 */ , { NULL, 0 }
463 : /* BITBIND 16 */ , { &null_BIT, sizeof(null_BIT) }
464 : /* NUMERICBIND 17 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
465 : /* DECIMALBIND 18 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
466 : /* SRCNUMERICBIND 19 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
467 : /* SRCDECIMALBIND 20 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
468 : /* DATEBIND 21 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
469 : /* TIMEBIND 22 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
470 : /* BIGDATETIMEBIND 23 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
471 : /* BIGTIMEBIND 24 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
472 : /* 25 */ , { NULL, 0 }
473 : /* 26 */ , { NULL, 0 }
474 : /* 27 */ , { NULL, 0 }
475 : /* 28 */ , { NULL, 0 }
476 : /* 29 */ , { NULL, 0 }
477 : /* BIGINTBIND 30 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
478 : /* DATETIME2BIND 31 */ , { (BYTE*) &null_DATETIMEALL, sizeof(null_DATETIMEALL) }
479 : /* MAXBINDTYPES 32 */
480 : };
481 :
482 : static int
483 122 : dbbindtype(int datatype)
484 : {
485 122 : switch (datatype) {
486 : case SYBIMAGE:
487 : case SYBVARBINARY:
488 : case SYBBINARY: return BINARYBIND;
489 :
490 0 : case SYBBIT: return BITBIND;
491 :
492 22 : case SYBLONGCHAR:
493 : case SYBTEXT:
494 : case SYBVARCHAR:
495 22 : case SYBCHAR: return NTBSTRINGBIND;
496 :
497 0 : case SYBDATETIME: return DATETIMEBIND;
498 0 : case SYBDATETIME4: return SMALLDATETIMEBIND;
499 :
500 0 : case SYBDATE: return DATEBIND;
501 0 : case SYBTIME: return TIMEBIND;
502 :
503 0 : case SYB5BIGDATETIME: return BIGDATETIMEBIND;
504 0 : case SYB5BIGTIME: return BIGTIMEBIND;
505 :
506 0 : case SYBDECIMAL: return DECIMALBIND;
507 0 : case SYBNUMERIC: return NUMERICBIND;
508 :
509 0 : case SYBFLT8: return FLT8BIND;
510 0 : case SYBREAL: return REALBIND;
511 :
512 0 : case SYBINT1: return TINYBIND;
513 0 : case SYBINT2: return SMALLBIND;
514 0 : case SYBINT4: return INTBIND;
515 0 : case SYBINT8: return BIGINTBIND;
516 :
517 0 : case SYBMONEY: return MONEYBIND;
518 0 : case SYBMONEY4: return SMALLMONEYBIND;
519 :
520 0 : case SYBMSDATE:
521 : case SYBMSTIME:
522 : case SYBMSDATETIME2:
523 : case SYBMSDATETIMEOFFSET:
524 0 : return DATETIME2BIND;
525 :
526 : default:
527 0 : assert(0 == "no such datatype");
528 : }
529 :
530 : return 0;
531 : }
532 :
533 : /** \internal
534 : * dbbind() says: "Note that if varlen is 0, no padding takes place"
535 : * dbgetnull() will not pad varaddr unless varlen is positive.
536 : * Vartype Program Type Padding Terminator
537 : * ------------------- -------------- -------------- ----------
538 : * CHARBIND DBCHAR blanks none
539 : * STRINGBIND DBCHAR blanks \0
540 : * NTBSTRINGBIND DBCHAR none \0
541 : * VARYCHARBIND DBVARYCHAR none none
542 : * BOUNDARYBIND DBCHAR none \0
543 : * SENSITIVITYBIND DBCHAR none \0
544 : */
545 : RETCODE
546 420 : dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr)
547 : {
548 420 : NULLREP *pnullrep = default_null_representations + bindtype;
549 :
550 420 : tdsdump_log(TDS_DBG_FUNC, "dbgetnull(%p, %d, %d, %p)\n", dbproc, bindtype, varlen, varaddr);
551 :
552 420 : CHECK_PARAMETER(varaddr, SYBENULL, FAIL);
553 420 : CHECK_PARAMETER(0 <= bindtype && bindtype < MAXBINDTYPES, SYBEBTYP, FAIL);
554 :
555 : /* dbproc can be NULL */
556 420 : if (NULL != dbproc)
557 320 : pnullrep = dbproc->nullreps + bindtype;
558 :
559 : /*
560 : * Fixed types: ignore varlen
561 : * Other types: ignore varlen if <= 0, else varlen must be >= pnullrep->len.
562 : */
563 : switch (bindtype) {
564 10 : case DATETIMEBIND:
565 : case DATETIME2BIND:
566 : case DECIMALBIND:
567 : case SRCDECIMALBIND:
568 : case FLT8BIND:
569 : case INTBIND:
570 : case MONEYBIND:
571 : case NUMERICBIND:
572 : case SRCNUMERICBIND:
573 : case REALBIND:
574 : case SMALLBIND:
575 : case SMALLDATETIMEBIND:
576 : case SMALLMONEYBIND:
577 : case TINYBIND:
578 : case BIGINTBIND:
579 : case BITBIND:
580 : case TIMEBIND:
581 : case DATEBIND:
582 : case BIGDATETIMEBIND:
583 : case BIGTIMEBIND:
584 10 : memcpy(varaddr, pnullrep->bindval, pnullrep->len);
585 10 : return SUCCEED;
586 410 : case CHARBIND:
587 : case STRINGBIND:
588 : case NTBSTRINGBIND:
589 : case BINARYBIND:
590 : case VARYCHARBIND:
591 : case VARYBINBIND:
592 410 : if (pnullrep->bindval && (varlen <= 0 || (size_t)varlen >= pnullrep->len)) {
593 232 : memcpy(varaddr, pnullrep->bindval, pnullrep->len);
594 : }
595 : break;
596 0 : default:
597 0 : dbperror(dbproc, SYBEBTYP, 0);
598 0 : return FAIL;
599 : }
600 :
601 : /*
602 : * For variable-length types, nonpositive varlen indicates
603 : * buffer is "big enough" but also not to pad.
604 : * Apply terminator (if applicable) and go home.
605 : */
606 410 : if (varlen <= 0) {
607 258 : varlen = pnullrep->len;
608 258 : switch (bindtype) {
609 188 : case STRINGBIND:
610 : case NTBSTRINGBIND:
611 188 : ++varlen;
612 188 : break;
613 : #if 0
614 : case BOUNDARYBIND:
615 : case SENSITIVITYBIND:
616 : #endif
617 : }
618 : }
619 :
620 410 : if (varlen < (long)pnullrep->len) {
621 0 : tdsdump_log(TDS_DBG_FUNC, "dbgetnull: error: not setting varaddr(%p) because %d < %lu\n",
622 : varaddr, varlen, (unsigned long int) pnullrep->len);
623 : return FAIL;
624 : }
625 :
626 410 : tdsdump_log(TDS_DBG_FUNC, "varaddr(%p) varlen %d < %lu?\n",
627 : varaddr, varlen, (unsigned long int) pnullrep->len);
628 :
629 410 : assert(varlen >= 0);
630 :
631 : /*
632 : * CHARBIND Empty string (padded with blanks)
633 : * STRINGBIND Empty string (padded with blanks, null-terminated)
634 : * NTBSTRINGBIND Empty string (unpadded, null-terminated)
635 : * BINARYBIND Empty array (padded with zeros)
636 : */
637 410 : varaddr += pnullrep->len;
638 410 : varlen -= (int)pnullrep->len;
639 410 : if (varlen > 0) {
640 326 : switch (bindtype) {
641 48 : case CHARBIND:
642 48 : memset(varaddr, ' ', varlen);
643 48 : break;
644 40 : case STRINGBIND:
645 40 : memset(varaddr, ' ', varlen);
646 40 : varaddr[varlen-1] = '\0';
647 40 : break;
648 148 : case NTBSTRINGBIND:
649 148 : varaddr[0] = '\0';
650 148 : break;
651 90 : case BINARYBIND:
652 90 : memset(varaddr, 0, varlen);
653 90 : break;
654 : case VARYCHARBIND:
655 : case VARYBINBIND:
656 : break;
657 : default:
658 0 : assert(!"unknown bindtype");
659 : }
660 : }
661 : return SUCCEED;
662 : }
663 :
664 : /**
665 : * \ingroup dblib_core
666 : * \brief Initialize db-lib.
667 : *
668 : * \remarks Call this function before trying to use db-lib in any way.
669 : * Allocates various internal structures and reads \c locales.conf (if any) to determine the default
670 : * date format.
671 : * \retval SUCCEED normal.
672 : * \retval FAIL cannot allocate an array of \c TDS_MAX_CONN \c TDSSOCKET pointers.
673 : */
674 : RETCODE
675 512 : dbinit(void)
676 : {
677 512 : _dblib_err_handler = default_err_handler;
678 :
679 512 : tds_mutex_lock(&dblib_mutex);
680 :
681 512 : tdsdump_log(TDS_DBG_FUNC, "dbinit(void)\n");
682 :
683 512 : if (++g_dblib_ctx.ref_count != 1) {
684 0 : tds_mutex_unlock(&dblib_mutex);
685 0 : return SUCCEED;
686 : }
687 :
688 512 : if (dblib_get_tds_ctx() == NULL) {
689 0 : tdsdump_log(TDS_DBG_FUNC, "dbinit: out of memory\n");
690 0 : tds_mutex_unlock(&dblib_mutex);
691 0 : return FAIL;
692 : }
693 :
694 : /*
695 : * DBLIBCONTEXT stores a list of current connections so they may be closed with dbexit()
696 : */
697 :
698 512 : g_dblib_ctx.connection_list = tds_new0(TDSSOCKET *, TDS_MAX_CONN);
699 512 : if (g_dblib_ctx.connection_list == NULL) {
700 0 : tdsdump_log(TDS_DBG_FUNC, "dbinit: out of memory\n");
701 0 : dblib_release_tds_ctx(1);
702 0 : tds_mutex_unlock(&dblib_mutex);
703 0 : return FAIL;
704 : }
705 512 : g_dblib_ctx.connection_list_size = TDS_MAX_CONN;
706 512 : g_dblib_ctx.connection_list_size_represented = TDS_MAX_CONN;
707 :
708 512 : g_dblib_ctx.login_timeout = -1;
709 512 : g_dblib_ctx.query_timeout = -1;
710 :
711 512 : tds_mutex_unlock(&dblib_mutex);
712 :
713 512 : return SUCCEED;
714 : }
715 :
716 : /**
717 : * \ingroup dblib_core
718 : * \brief Allocate a \c LOGINREC structure.
719 : *
720 : * \remarks A \c LOGINREC structure is passed to \c dbopen() to create a connection to the database.
721 : * Does not communicate to the server; interacts strictly with library.
722 : * \retval NULL the \c LOGINREC cannot be allocated.
723 : * \retval LOGINREC* to valid memory, otherwise.
724 : */
725 : LOGINREC *
726 842 : dblogin(void)
727 : {
728 : LOGINREC *loginrec;
729 :
730 842 : tdsdump_log(TDS_DBG_FUNC, "dblogin(void)\n");
731 :
732 842 : if ((loginrec = tds_new0(LOGINREC, 1)) == NULL) {
733 0 : dbperror(NULL, SYBEMEM, errno);
734 0 : return NULL;
735 : }
736 842 : if ((loginrec->tds_login = tds_alloc_login(true)) == NULL) {
737 0 : dbperror(NULL, SYBEMEM, errno);
738 0 : free(loginrec);
739 0 : return NULL;
740 : }
741 :
742 : /* set default values for loginrec */
743 842 : if (!tds_set_library(loginrec->tds_login, "DB-Library")) {
744 0 : dbperror(NULL, SYBEMEM, errno);
745 0 : tds_free_login(loginrec->tds_login);
746 0 : free(loginrec);
747 0 : return NULL;
748 : }
749 :
750 : return loginrec;
751 : }
752 :
753 : /**
754 : * \ingroup dblib_core
755 : * \brief free the \c LOGINREC
756 : *
757 : */
758 : void
759 812 : dbloginfree(LOGINREC * login)
760 : {
761 812 : tdsdump_log(TDS_DBG_FUNC, "dbloginfree(%p)\n", login);
762 :
763 812 : if (login) {
764 812 : tds_free_login(login->tds_login);
765 812 : free(login);
766 : }
767 812 : }
768 :
769 : /** \internal
770 : * \ingroup dblib_internal
771 : * \brief Set the value of a string in a \c LOGINREC structure.
772 : *
773 : * Called by various macros to populate \a login.
774 : * \param login the \c LOGINREC* to modify.
775 : * \param value the value to set it to.
776 : * \param which the field to set.
777 : * \retval SUCCEED the value was set.
778 : * \retval FAIL \c DBSETHID or other invalid \a which was tried.
779 : */
780 : RETCODE
781 2876 : dbsetlname(LOGINREC * login, const char *value, int which)
782 : {
783 : bool copy_ret;
784 2876 : const char *value_nonull = value ? value : "";
785 :
786 2876 : tdsdump_log(TDS_DBG_FUNC, "dbsetlname(%p, %s, %d)\n", login, value, which);
787 :
788 2876 : if (login == NULL) {
789 0 : dbperror(NULL, SYBEASNL, 0);
790 0 : return FAIL;
791 : }
792 :
793 2876 : if (TDS_MAX_LOGIN_STR_SZ < strlen(value_nonull)) {
794 0 : dbperror(NULL, SYBENTLL, 0);
795 0 : return FAIL;
796 : }
797 :
798 2876 : switch (which) {
799 330 : case DBSETHOST:
800 330 : copy_ret = tds_set_host(login->tds_login, value_nonull);
801 330 : break;
802 832 : case DBSETUSER:
803 832 : copy_ret = tds_set_user(login->tds_login, value_nonull);
804 832 : break;
805 832 : case DBSETPWD:
806 832 : copy_ret = tds_set_passwd(login->tds_login, value_nonull);
807 832 : break;
808 832 : case DBSETAPP:
809 832 : copy_ret = tds_set_app(login->tds_login, value_nonull);
810 832 : break;
811 10 : case DBSETCHARSET:
812 : /* TODO NULL == no conversion desired */
813 10 : copy_ret = tds_set_client_charset(login->tds_login, value_nonull);
814 10 : break;
815 0 : case DBSETNATLANG:
816 0 : copy_ret = tds_set_language(login->tds_login, value_nonull);
817 0 : break;
818 40 : case DBSETDBNAME:
819 40 : copy_ret = !!tds_dstr_copy(&login->tds_login->database, value_nonull);
820 40 : break;
821 0 : case DBSETSERVERPRINCIPAL:
822 0 : copy_ret = !!tds_dstr_copy(&login->tds_login->server_spn, value_nonull);
823 0 : break;
824 0 : case DBSETENCRYPTION:
825 0 : copy_ret = tds_parse_conf_section(TDS_STR_ENCRYPTION, value_nonull, login->tds_login);
826 0 : break;
827 0 : default:
828 0 : dbperror(NULL, SYBEASUL, 0); /* Attempt to set unknown LOGINREC field */
829 0 : return FAIL;
830 : break;
831 : }
832 :
833 2876 : if (!copy_ret)
834 : return FAIL;
835 2876 : return SUCCEED;
836 : }
837 :
838 : /** \internal
839 : * \ingroup dblib_internal
840 : * \brief Set an integer value in a \c LOGINREC structure.
841 : *
842 : * Called by various macros to populate \a login.
843 : * \param login the \c LOGINREC* to modify.
844 : * \param value the value to set it to.
845 : * \param which the field to set.
846 : * \retval SUCCEED the value was set.
847 : * \retval FAIL anything other than \c DBSETPACKET was passed for \a which.
848 : */
849 : RETCODE
850 10 : dbsetllong(LOGINREC * login, long value, int which)
851 : {
852 10 : tdsdump_log(TDS_DBG_FUNC, "dbsetllong(%p, %ld, %d)\n", login, value, which);
853 :
854 10 : if( login == NULL ) {
855 0 : dbperror(NULL, SYBEASNL, 0);
856 0 : return FAIL;
857 : }
858 :
859 10 : switch (which) {
860 10 : case DBSETPACKET:
861 10 : if (0 <= value && value <= 999999) {
862 0 : tds_set_packet(login->tds_login, (int) value);
863 0 : return SUCCEED;
864 : }
865 10 : dbperror(0, SYBEBADPK, 0, (int) value, (int) login->tds_login->block_size);
866 10 : return FAIL;
867 : break;
868 0 : default:
869 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetllong() which = %d\n", which);
870 : return FAIL;
871 : break;
872 : }
873 : }
874 :
875 : /** \internal
876 : * \ingroup dblib_internal
877 : * \brief Set an integer value in a \c LOGINREC structure.
878 : *
879 : * Called by various macros to populate \a login.
880 : * \param login the \c LOGINREC* to modify.
881 : * \param value the value to set it to.
882 : * \param which the field to set.
883 : * \retval SUCCEED the value was set.
884 : * \retval FAIL if invalid field in \a which or invalid \a value.
885 : */
886 : RETCODE
887 0 : dbsetlshort(LOGINREC * login, int value, int which)
888 : {
889 0 : tdsdump_log(TDS_DBG_FUNC, "dbsetlshort(%p, %d, %d)\n", login, value, which);
890 :
891 0 : if( login == NULL ) {
892 0 : dbperror(NULL, SYBEASNL, 0);
893 0 : return FAIL;
894 : }
895 :
896 0 : switch (which) {
897 0 : case DBSETPORT:
898 0 : tds_set_port(login->tds_login, value);
899 0 : return SUCCEED;
900 : /* case DBSETHIER: */
901 0 : default:
902 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlshort() which = %d\n", which);
903 : return FAIL;
904 : break;
905 : }
906 : }
907 :
908 : /** \internal
909 : * \ingroup dblib_internal
910 : * \brief Set a boolean value in a \c LOGINREC structure.
911 : *
912 : * Called by various macros to populate \a login.
913 : * \param login the \c LOGINREC* to modify.
914 : * \param value the value to set it to.
915 : * \param which the field to set.
916 : * \remark Only DBSETBCP is implemented.
917 : * \retval SUCCEED the value was set.
918 : * \retval FAIL invalid value passed for \a which.
919 : * \todo DBSETNOSHORT, DBSETENCRYPT, DBSETLABELED
920 : */
921 : RETCODE
922 60 : dbsetlbool(LOGINREC * login, int value, int which)
923 : {
924 : bool b_value;
925 :
926 60 : tdsdump_log(TDS_DBG_FUNC, "dbsetlbool(%p, %d, %d)\n", login, value, which);
927 :
928 60 : if (login == NULL) {
929 0 : dbperror(NULL, SYBEASNL, 0);
930 0 : return FAIL;
931 : }
932 :
933 60 : b_value = (value != 0);
934 :
935 60 : switch (which) {
936 60 : case DBSETBCP:
937 60 : tds_set_bulk(login->tds_login, b_value);
938 60 : return SUCCEED;
939 0 : case DBSETUTF16:
940 0 : login->tds_login->use_utf16 = b_value;
941 0 : return SUCCEED;
942 0 : case DBSETNTLMV2:
943 0 : login->tds_login->use_ntlmv2 = b_value;
944 0 : login->tds_login->use_ntlmv2_specified = 1;
945 0 : return SUCCEED;
946 0 : case DBSETREADONLY:
947 0 : login->tds_login->readonly_intent = b_value;
948 0 : return SUCCEED;
949 0 : case DBSETNETWORKAUTH:
950 0 : login->network_auth = b_value;
951 0 : return SUCCEED;
952 0 : case DBSETMUTUALAUTH:
953 0 : login->tds_login->mutual_authentication = b_value;
954 0 : return SUCCEED;
955 0 : case DBSETDELEGATION:
956 0 : login->tds_login->gssapi_use_delegation = b_value;
957 0 : return SUCCEED;
958 0 : case DBSETENCRYPT:
959 : case DBSETLABELED:
960 : default:
961 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlbool() which = %d\n", which);
962 : return FAIL;
963 : break;
964 : }
965 : }
966 :
967 : /**
968 : * \ingroup dblib_core
969 : * \brief Set TDS version for future connections
970 : *
971 : */
972 : RETCODE
973 0 : dbsetlversion(LOGINREC * login, BYTE version)
974 : {
975 0 : tdsdump_log(TDS_DBG_FUNC, "dbsetlversion(%p, %x)\n", login, version);
976 :
977 0 : if( login == NULL ) {
978 0 : dbperror(NULL, SYBEASNL, 0);
979 0 : return FAIL;
980 : }
981 :
982 0 : assert(login->tds_login != NULL);
983 :
984 0 : switch (version) {
985 0 : case DBVERSION_UNKNOWN:
986 0 : tds_set_version(login->tds_login, 0, 0);
987 0 : return SUCCEED;
988 0 : case DBVERSION_42:
989 0 : tds_set_version(login->tds_login, 4, 2);
990 0 : return SUCCEED;
991 0 : case DBVERSION_100:
992 0 : tds_set_version(login->tds_login, 5, 0);
993 0 : return SUCCEED;
994 0 : case DBVERSION_70:
995 0 : tds_set_version(login->tds_login, 7, 0);
996 0 : return SUCCEED;
997 0 : case DBVERSION_71:
998 0 : tds_set_version(login->tds_login, 7, 1);
999 0 : return SUCCEED;
1000 0 : case DBVERSION_72:
1001 0 : tds_set_version(login->tds_login, 7, 2);
1002 0 : return SUCCEED;
1003 0 : case DBVERSION_73:
1004 0 : tds_set_version(login->tds_login, 7, 3);
1005 0 : return SUCCEED;
1006 0 : case DBVERSION_74:
1007 0 : tds_set_version(login->tds_login, 7, 4);
1008 0 : return SUCCEED;
1009 : }
1010 :
1011 : return FAIL;
1012 : }
1013 :
1014 : static void
1015 36502 : dbstring_free(DBSTRING ** dbstrp)
1016 : {
1017 : DBSTRING *curr, *next;
1018 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_free(%p)\n", dbstrp); */
1019 :
1020 36502 : if (!dbstrp)
1021 : return;
1022 :
1023 36502 : curr = *dbstrp;
1024 36502 : *dbstrp = NULL;
1025 78098 : for (; curr; ) {
1026 5094 : next = curr->strnext;
1027 5094 : free(curr->strtext);
1028 5094 : free(curr);
1029 5094 : curr = next;
1030 : }
1031 : }
1032 :
1033 : static RETCODE
1034 5274 : dbstring_concat(DBSTRING ** dbstrp, const char *p)
1035 : {
1036 5274 : DBSTRING **strp = dbstrp;
1037 :
1038 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_concat(%p, %s)\n", *dbstrp, p); */
1039 :
1040 10548 : while (*strp != NULL) {
1041 0 : strp = &((*strp)->strnext);
1042 : }
1043 5274 : if ((*strp = tds_new(DBSTRING, 1)) == NULL) {
1044 0 : dbperror(NULL, SYBEMEM, errno);
1045 0 : return FAIL;
1046 : }
1047 5274 : (*strp)->strtotlen = (DBINT)strlen(p);
1048 5274 : if (((*strp)->strtext = tds_new(BYTE, (*strp)->strtotlen)) == NULL) {
1049 0 : TDS_ZERO_FREE(*strp);
1050 0 : dbperror(NULL, SYBEMEM, errno);
1051 0 : return FAIL;
1052 : }
1053 5274 : memcpy((*strp)->strtext, p, (*strp)->strtotlen);
1054 5274 : (*strp)->strnext = NULL;
1055 5274 : return SUCCEED;
1056 : }
1057 :
1058 : static RETCODE
1059 : dbstring_assign(DBSTRING ** dbstrp, const char *p)
1060 : {
1061 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_assign(%p, %s)\n", *dbstrp, p); */
1062 :
1063 5274 : dbstring_free(dbstrp);
1064 5274 : return dbstring_concat(dbstrp, p);
1065 : }
1066 :
1067 : static DBINT
1068 : dbstring_length(DBSTRING * dbstr)
1069 : {
1070 0 : DBINT len = 0;
1071 : DBSTRING *next;
1072 :
1073 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_length(%p)\n", dbstr); */
1074 :
1075 0 : for (next = dbstr; next != NULL; next = next->strnext) {
1076 0 : len += next->strtotlen;
1077 : }
1078 : return len;
1079 : }
1080 :
1081 : static int
1082 : dbstring_getchar(DBSTRING * dbstr, ptrdiff_t i)
1083 : {
1084 :
1085 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_getchar(%p, %d)\n", dbstr, i); */
1086 :
1087 6160 : if (dbstr == NULL) {
1088 : return -1;
1089 : }
1090 4600 : if (i < 0) {
1091 : return -1;
1092 : }
1093 4600 : if (i < dbstr->strtotlen) {
1094 3040 : return dbstr->strtext[i];
1095 : }
1096 1560 : return dbstring_getchar(dbstr->strnext, i - dbstr->strtotlen);
1097 : }
1098 :
1099 : static char *
1100 0 : dbstring_get(DBSTRING * dbstr)
1101 : {
1102 : DBSTRING *next;
1103 : int len;
1104 : char *ret;
1105 : char *cp;
1106 :
1107 : /* tdsdump_log(TDS_DBG_FUNC, "dbstring_get(%p)\n", dbstr); */
1108 :
1109 0 : if (dbstr == NULL) {
1110 : return NULL;
1111 : }
1112 0 : len = dbstring_length(dbstr);
1113 0 : if ((ret = tds_new(char, len + 1)) == NULL) {
1114 0 : dbperror(NULL, SYBEMEM, errno);
1115 0 : return NULL;
1116 : }
1117 : cp = ret;
1118 0 : for (next = dbstr; next != NULL; next = next->strnext) {
1119 0 : memcpy(cp, next->strtext, next->strtotlen);
1120 0 : cp += next->strtotlen;
1121 : }
1122 0 : *cp = '\0';
1123 0 : return ret;
1124 : }
1125 :
1126 : static const char *const opttext[DBNUMOPTIONS] = {
1127 : "parseonly",
1128 : "estimate",
1129 : "showplan",
1130 : "noexec",
1131 : "arithignore",
1132 : "nocount",
1133 : "arithabort",
1134 : "textlimit",
1135 : "browse",
1136 : "offsets",
1137 : "statistics",
1138 : "errlvl",
1139 : "confirm",
1140 : "spid",
1141 : "buffer",
1142 : "noautofree",
1143 : "rowcount",
1144 : "textsize",
1145 : "language",
1146 : "dateformat",
1147 : "prpad",
1148 : "prcolsep",
1149 : "prlinelen",
1150 : "prlinesep",
1151 : "lfconvert",
1152 : "datefirst",
1153 : "chained",
1154 : "fipsflagger",
1155 : "transaction isolation level",
1156 : "auth",
1157 : "identity_insert",
1158 : "no_identity_column",
1159 : "cnv_date2char_short",
1160 : "client cursors",
1161 : "set time",
1162 : "quoted_identifier"
1163 : };
1164 :
1165 : static DBOPTION *
1166 874 : init_dboptions(void)
1167 : {
1168 : DBOPTION *dbopts;
1169 : int i;
1170 :
1171 874 : if ((dbopts = tds_new0(DBOPTION, DBNUMOPTIONS)) == NULL) {
1172 0 : dbperror(NULL, SYBEMEM, errno);
1173 0 : return NULL;
1174 : }
1175 31464 : for (i = 0; i < DBNUMOPTIONS; i++) {
1176 31464 : dbopts[i].text = opttext[i];
1177 31464 : dbopts[i].param = NULL;
1178 31464 : dbopts[i].factive = FALSE;
1179 : }
1180 1748 : dbstring_assign(&(dbopts[DBPRPAD].param), " ");
1181 1748 : dbstring_assign(&(dbopts[DBPRCOLSEP].param), " ");
1182 1748 : dbstring_assign(&(dbopts[DBPRLINELEN].param), "80");
1183 1748 : dbstring_assign(&(dbopts[DBPRLINESEP].param), "\n");
1184 1748 : dbstring_assign(&(dbopts[DBCLIENTCURSORS].param), " ");
1185 1748 : dbstring_assign(&(dbopts[DBSETTIME].param), " ");
1186 874 : return dbopts;
1187 : }
1188 :
1189 : /** \internal
1190 : * \ingroup dblib_internal
1191 : * \brief Form a connection with the server.
1192 : *
1193 : * Called by the \c dbopen() macro, normally. If FreeTDS was configured with \c --enable-msdblib, this
1194 : * function is called by (exported) \c dbopen() function. \c tdsdbopen is so-named to avoid
1195 : * namespace conflicts with other database libraries that use the same function name.
1196 : * \param login \c LOGINREC* carrying the account information.
1197 : * \param server name of the dataserver to connect to.
1198 : * \return valid pointer on successful login.
1199 : * \retval NULL insufficient memory, unable to connect for any reason.
1200 : * \sa dbopen()
1201 : * \todo use \c asprintf() to avoid buffer overflow.
1202 : * \todo separate error messages for \em no-such-server and \em no-such-user.
1203 : */
1204 : DBPROCESS *
1205 874 : tdsdbopen(LOGINREC * login, const char *server, int msdblib)
1206 : {
1207 874 : DBPROCESS *dbproc = NULL;
1208 874 : TDSCONTEXT *tds_ctx = NULL;
1209 : TDSLOGIN *connection;
1210 : int add_connection_res;
1211 :
1212 874 : tds_dir_char *tdsdump = tds_dir_getenv(TDS_DIR("TDSDUMP"));
1213 874 : if (tdsdump && *tdsdump) {
1214 0 : tdsdump_topen(tdsdump);
1215 0 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen(%p, %s, [%s])\n", login, server? server : "0x0", msdblib? "microsoft" : "sybase");
1216 : }
1217 :
1218 : /*
1219 : * Sybase supports the DSQUERY environment variable and falls back to "SYBASE" if server is NULL.
1220 : * Microsoft uses a NULL or "" server to indicate a local server.
1221 : * FIXME: support local server for win32.
1222 : */
1223 874 : if (!server && !msdblib) {
1224 0 : if ((server = getenv("TDSQUERY")) == NULL)
1225 0 : if ((server = getenv("DSQUERY")) == NULL)
1226 0 : server = "SYBASE";
1227 0 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: servername set to %s\n", server);
1228 : }
1229 :
1230 874 : if ((dbproc = tds_new0(DBPROCESS, 1)) == NULL)
1231 : goto memory_error;
1232 874 : dbproc->msdblib = msdblib;
1233 :
1234 874 : dbproc->dbopts = init_dboptions();
1235 874 : if (dbproc->dbopts == NULL)
1236 : goto cleanup;
1237 874 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: dbproc->dbopts = %p\n", dbproc->dbopts);
1238 :
1239 874 : dbproc->dboptcmd = NULL;
1240 874 : dbproc->avail_flag = TRUE;
1241 874 : dbproc->command_state = DBCMDNONE;
1242 :
1243 874 : if (!tds_set_server(login->tds_login, server))
1244 : goto memory_error;
1245 874 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: tds_set_server(%p, \"%s\")\n", login->tds_login, server);
1246 :
1247 874 : tds_mutex_lock(&dblib_mutex);
1248 874 : tds_ctx = dblib_get_tds_ctx();
1249 874 : tds_mutex_unlock(&dblib_mutex);
1250 874 : if (!tds_ctx)
1251 : goto memory_error;
1252 :
1253 874 : dbproc->tds_socket = tds_alloc_socket(tds_ctx, 512);
1254 874 : if (dbproc->tds_socket == NULL)
1255 : goto memory_error;
1256 874 : tds_ctx = NULL;
1257 :
1258 874 : tds_set_parent(dbproc->tds_socket, dbproc);
1259 :
1260 874 : dbproc->tds_socket->env_chg_func = db_env_chg;
1261 874 : dbproc->envchange_rcv = 0;
1262 :
1263 874 : dbproc->dbcurdb[0] = '\0';
1264 874 : dbproc->servcharset[0] = '\0';
1265 :
1266 874 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: About to call tds_read_config_info...\n");
1267 :
1268 874 : tds_mutex_lock(&dblib_mutex);
1269 874 : add_connection_res = dblib_add_connection(&g_dblib_ctx, dbproc->tds_socket);
1270 874 : tds_mutex_unlock(&dblib_mutex);
1271 874 : if (add_connection_res) {
1272 10 : dbperror(dbproc, SYBEDBPS, 0);
1273 10 : dbclose(dbproc);
1274 10 : return NULL;
1275 : }
1276 :
1277 864 : connection = tds_read_config_info(dbproc->tds_socket, login->tds_login, g_dblib_ctx.tds_ctx->locale);
1278 864 : if (!connection) {
1279 0 : dbclose(dbproc);
1280 0 : return NULL;
1281 : }
1282 864 : connection->option_flag2 &= ~TDS_ODBC_ON; /* we're not an ODBC driver */
1283 864 : tds_fix_login(connection); /* initialize from Environment variables */
1284 :
1285 864 : dbproc->chkintr = NULL;
1286 864 : dbproc->hndlintr = NULL;
1287 :
1288 864 : tds_mutex_lock(&dblib_mutex);
1289 :
1290 : /* override connection timeout if dbsetlogintime() was called */
1291 864 : if (g_dblib_ctx.login_timeout > 0) {
1292 20 : connection->connect_timeout = g_dblib_ctx.login_timeout;
1293 : }
1294 :
1295 : /* override query timeout if dbsettime() was called */
1296 864 : if (g_dblib_ctx.query_timeout > 0) {
1297 10 : connection->query_timeout = g_dblib_ctx.query_timeout;
1298 : }
1299 :
1300 864 : tds_mutex_unlock(&dblib_mutex);
1301 :
1302 864 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: Calling tds_connect_and_login(%p, %p)\n",
1303 : dbproc->tds_socket, connection);
1304 :
1305 864 : if (login->network_auth) {
1306 0 : tds_dstr_empty(&connection->user_name);
1307 0 : tds_dstr_empty(&connection->password);
1308 : }
1309 :
1310 864 : if (TDS_FAILED(tds_connect_and_login(dbproc->tds_socket, connection))) {
1311 0 : tdsdump_log(TDS_DBG_ERROR, "tdsdbopen: tds_connect_and_login failed for \"%s\"!\n",
1312 : tds_dstr_cstr(&connection->server_name));
1313 0 : tds_free_login(connection);
1314 0 : dbclose(dbproc);
1315 0 : return NULL;
1316 : }
1317 864 : tds_free_login(connection);
1318 :
1319 864 : dbproc->dbbuf = NULL;
1320 864 : dbproc->dbbufsz = 0;
1321 :
1322 : /* set the DBBUFFER capacity to nil */
1323 864 : buffer_set_capacity(dbproc, 0);
1324 :
1325 864 : memcpy(dbproc->nullreps, default_null_representations, sizeof(default_null_representations));
1326 :
1327 864 : tds_mutex_lock(&dblib_mutex);
1328 :
1329 864 : if (g_dblib_ctx.recftos_filename != NULL) {
1330 0 : char *temp_filename = NULL;
1331 0 : const int len = asprintf(&temp_filename, "%s.%d",
1332 : g_dblib_ctx.recftos_filename, g_dblib_ctx.recftos_filenum);
1333 0 : if (len >= 0) {
1334 0 : dbproc->ftos = fopen(temp_filename, "w");
1335 0 : free(temp_filename);
1336 0 : if (dbproc->ftos != NULL) {
1337 : char timestr[256];
1338 0 : fprintf(dbproc->ftos, "/* dbopen() at %s */\n", _dbprdate(timestr));
1339 0 : fflush(dbproc->ftos);
1340 0 : g_dblib_ctx.recftos_filenum++;
1341 : }
1342 : }
1343 : }
1344 :
1345 864 : tds_mutex_unlock(&dblib_mutex);
1346 :
1347 864 : tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: Returning dbproc = %p\n", dbproc);
1348 :
1349 : return dbproc;
1350 :
1351 0 : memory_error:
1352 0 : dbperror(NULL, SYBEMEM, errno);
1353 :
1354 0 : cleanup:
1355 0 : if (tds_ctx) {
1356 0 : tds_mutex_lock(&dblib_mutex);
1357 0 : dblib_release_tds_ctx(1);
1358 0 : tds_mutex_unlock(&dblib_mutex);
1359 : }
1360 0 : if (dbproc) {
1361 0 : free(dbproc->dbopts);
1362 0 : free(dbproc);
1363 : }
1364 : return NULL;
1365 : }
1366 :
1367 : /**
1368 : * \ingroup dblib_core
1369 : * \brief \c printf-like way to form SQL to send to the server.
1370 : *
1371 : * Forms a command string and writes to the command buffer with dbcmd().
1372 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1373 : * \param fmt <tt> man vasprintf</tt> for details.
1374 : * \retval SUCCEED success.
1375 : * \retval FAIL insufficient memory, or dbcmd() failed.
1376 : * \sa dbcmd(), dbfreebuf(), dbgetchar(), dbopen(), dbstrcpy(), dbstrlen().
1377 : */
1378 : RETCODE
1379 870 : dbfcmd(DBPROCESS * dbproc, const char *fmt, ...)
1380 : {
1381 : va_list ap;
1382 : char *s;
1383 : int len;
1384 : RETCODE ret;
1385 :
1386 870 : tdsdump_log(TDS_DBG_FUNC, "dbfcmd(%p, %s, ...)\n", dbproc, fmt);
1387 870 : CHECK_CONN(FAIL);
1388 870 : CHECK_NULP(fmt, "dbfcmd", 2, FAIL);
1389 :
1390 870 : va_start(ap, fmt);
1391 870 : len = vasprintf(&s, fmt, ap);
1392 870 : va_end(ap);
1393 :
1394 870 : if (len < 0) {
1395 0 : dbperror(dbproc, SYBEMEM, errno);
1396 0 : return FAIL;
1397 : }
1398 :
1399 870 : ret = dbcmd(dbproc, s);
1400 870 : free(s);
1401 :
1402 870 : return ret;
1403 : }
1404 :
1405 : /**
1406 : * \ingroup dblib_core
1407 : * \brief \c Append SQL to the command buffer.
1408 : *
1409 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1410 : * \param cmdstring SQL to append to the command buffer.
1411 : * \retval SUCCEED success.
1412 : * \retval FAIL insufficient memory.
1413 : * \remarks set command state to \c DBCMDPEND unless the command state is DBCMDSENT, in which case
1414 : * it frees the command buffer. This latter may or may not be the Right Thing to do.
1415 : * \sa dbfcmd(), dbfreebuf(), dbgetchar(), dbopen(), dbstrcpy(), dbstrlen().
1416 : */
1417 : RETCODE
1418 25464 : dbcmd(DBPROCESS * dbproc, const char cmdstring[])
1419 : {
1420 : size_t cmd_len, buf_len, newsz;
1421 :
1422 25464 : tdsdump_log(TDS_DBG_FUNC, "dbcmd(%p, %s)\n", dbproc, cmdstring);
1423 25464 : CHECK_CONN(FAIL);
1424 25464 : CHECK_NULP(cmdstring, "dbcmd", 2, FAIL);
1425 :
1426 25464 : dbproc->avail_flag = FALSE;
1427 :
1428 25464 : tdsdump_log(TDS_DBG_FUNC, "dbcmd() bufsz = %d\n", dbproc->dbbufsz);
1429 :
1430 25464 : if (dbproc->command_state == DBCMDSENT) {
1431 19986 : if (!dbproc->noautofree) {
1432 19986 : dbfreebuf(dbproc);
1433 : }
1434 : }
1435 :
1436 25464 : buf_len = (dbproc->dbbufsz == 0) ? 0 : dbproc->dbbufsz - 1;
1437 25464 : cmd_len = strlen(cmdstring);
1438 25464 : newsz = buf_len + cmd_len + 1;
1439 25464 : if (newsz > 0x7fffffffu || !TDS_RESIZE(dbproc->dbbuf, newsz)) {
1440 0 : dbperror(dbproc, SYBEMEM, errno);
1441 0 : return FAIL;
1442 : }
1443 25464 : memcpy(dbproc->dbbuf + buf_len, cmdstring, cmd_len);
1444 25464 : dbproc->dbbuf[newsz - 1] = 0;
1445 25464 : dbproc->dbbufsz = (int) newsz;
1446 :
1447 25464 : dbproc->command_state = DBCMDPEND;
1448 :
1449 25464 : return SUCCEED;
1450 : }
1451 :
1452 : /**
1453 : * \ingroup dblib_core
1454 : * \brief send the SQL command to the server and wait for an answer.
1455 : *
1456 : * Please be patient. This function waits for the server to respond. \c dbsqlexec is equivalent
1457 : * to dbsqlsend() followed by dbsqlok().
1458 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1459 : * \retval SUCCEED query was processed without errors.
1460 : * \retval FAIL was returned by dbsqlsend() or dbsqlok().
1461 : * \sa dbcmd(), dbfcmd(), dbnextrow(), dbresults(), dbretstatus(), dbsettime(), dbsqlok(), dbsqlsend()
1462 : */
1463 : RETCODE
1464 20540 : dbsqlexec(DBPROCESS * dbproc)
1465 : {
1466 20540 : RETCODE rc = FAIL;
1467 :
1468 20540 : tdsdump_log(TDS_DBG_FUNC, "dbsqlexec(%p)\n", dbproc);
1469 20540 : CHECK_CONN(FAIL);
1470 :
1471 20540 : if (SUCCEED == (rc = dbsqlsend(dbproc))) {
1472 20460 : rc = dbsqlok(dbproc);
1473 : }
1474 : return rc;
1475 : }
1476 :
1477 : /**
1478 : * \ingroup dblib_core
1479 : * \brief Change current database.
1480 : *
1481 : * Analagous to the unix command \c cd, dbuse() makes \a name the default database. Waits for an answer
1482 : * from the server.
1483 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1484 : * \param name database to use.
1485 : * \retval SUCCEED query was processed without errors.
1486 : * \retval FAIL query was not processed
1487 : * \sa dbchange(), dbname().
1488 : */
1489 : RETCODE
1490 804 : dbuse(DBPROCESS * dbproc, const char *name)
1491 : {
1492 : RETCODE rc;
1493 : char *query;
1494 :
1495 804 : tdsdump_log(TDS_DBG_FUNC, "dbuse(%p, %s)\n", dbproc, name);
1496 804 : CHECK_CONN(FAIL);
1497 804 : CHECK_NULP(name, "dbuse", 2, FAIL);
1498 :
1499 : /* quote name */
1500 804 : query = tds_new(char, tds_quote_id(dbproc->tds_socket, NULL, name, -1) + 6);
1501 804 : if (!query) {
1502 0 : dbperror(dbproc, SYBEMEM, errno);
1503 0 : return FAIL;
1504 : }
1505 804 : strcpy(query, "use ");
1506 : /* TODO PHP suggest to quote by yourself with []... what should I do ?? quote or not ?? */
1507 804 : if (name[0] == '[' && name[strlen(name)-1] == ']')
1508 0 : strcat(query, name);
1509 : else
1510 804 : tds_quote_id(dbproc->tds_socket, query + 4, name, -1);
1511 :
1512 804 : rc = SUCCEED;
1513 804 : if ((dbcmd(dbproc, query) == FAIL)
1514 804 : || (dbsqlexec(dbproc) == FAIL)
1515 804 : || (dbresults(dbproc) == FAIL)
1516 804 : || (dbcanquery(dbproc) == FAIL))
1517 : rc = FAIL;
1518 804 : free(query);
1519 804 : return rc;
1520 : }
1521 :
1522 : /**
1523 : * \ingroup dblib_core
1524 : * \brief Close a connection to the server and free associated resources.
1525 : *
1526 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1527 : * \sa dbexit(), dbopen().
1528 : */
1529 : void
1530 844 : dbclose(DBPROCESS * dbproc)
1531 : {
1532 : TDSSOCKET *tds;
1533 : int i;
1534 : char timestr[256];
1535 :
1536 844 : tdsdump_log(TDS_DBG_FUNC, "dbclose(%p)\n", dbproc);
1537 844 : CHECK_PARAMETER(dbproc, SYBENULL, );
1538 :
1539 844 : tds = dbproc->tds_socket;
1540 844 : if (tds) {
1541 : /*
1542 : * this MUST be done before socket destruction
1543 : * it is possible that a TDSSOCKET is allocated on same position
1544 : */
1545 466 : tds_mutex_lock(&dblib_mutex);
1546 466 : dblib_del_connection(&g_dblib_ctx, dbproc->tds_socket);
1547 466 : tds_mutex_unlock(&dblib_mutex);
1548 :
1549 466 : tds_close_socket(tds);
1550 466 : tds_free_socket(tds);
1551 :
1552 466 : tds_mutex_lock(&dblib_mutex);
1553 466 : dblib_release_tds_ctx(1);
1554 466 : tds_mutex_unlock(&dblib_mutex);
1555 : }
1556 844 : buffer_free(&(dbproc->row_buf));
1557 :
1558 844 : if (dbproc->ftos != NULL) {
1559 0 : fprintf(dbproc->ftos, "/* dbclose() at %s */\n", _dbprdate(timestr));
1560 0 : fclose(dbproc->ftos);
1561 : }
1562 :
1563 844 : tds_free_bcpinfo(dbproc->bcpinfo);
1564 844 : if (dbproc->hostfileinfo) {
1565 0 : free(dbproc->hostfileinfo->hostfile);
1566 0 : free(dbproc->hostfileinfo->errorfile);
1567 0 : if (dbproc->hostfileinfo->host_columns) {
1568 0 : for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
1569 0 : free(dbproc->hostfileinfo->host_columns[i]->terminator);
1570 0 : free(dbproc->hostfileinfo->host_columns[i]);
1571 : }
1572 0 : free(dbproc->hostfileinfo->host_columns);
1573 : }
1574 : }
1575 :
1576 30384 : for (i = 0; i < DBNUMOPTIONS; i++) {
1577 30384 : dbstring_free(&(dbproc->dbopts[i].param));
1578 : }
1579 844 : free(dbproc->dbopts);
1580 :
1581 844 : dbstring_free(&(dbproc->dboptcmd));
1582 :
1583 27852 : for (i=0; i < MAXBINDTYPES; i++) {
1584 27008 : if (dbproc->nullreps[i].bindval != default_null_representations[i].bindval)
1585 250 : free((BYTE*)dbproc->nullreps[i].bindval);
1586 : }
1587 :
1588 844 : dbfreebuf(dbproc);
1589 844 : free(dbproc);
1590 : }
1591 :
1592 : /**
1593 : * \ingroup dblib_core
1594 : * \brief Close server connections and free all related structures.
1595 : *
1596 : * \sa dbclose(), dbinit(), dbopen().
1597 : * \todo breaks if ctlib/dblib used in same process.
1598 : */
1599 : void
1600 482 : dbexit(void)
1601 : {
1602 : TDSSOCKET *tds;
1603 : DBPROCESS *dbproc;
1604 482 : int i, list_size, count = 1;
1605 :
1606 482 : tdsdump_log(TDS_DBG_FUNC, "dbexit(void)\n");
1607 :
1608 482 : tds_mutex_lock(&dblib_mutex);
1609 :
1610 482 : if (--g_dblib_ctx.ref_count != 0) {
1611 0 : tds_mutex_unlock(&dblib_mutex);
1612 0 : return;
1613 : }
1614 :
1615 482 : list_size = g_dblib_ctx.connection_list_size;
1616 :
1617 1974754 : for (i = 0; i < list_size; i++) {
1618 1974272 : tds = g_dblib_ctx.connection_list[i];
1619 1974272 : g_dblib_ctx.connection_list[i] = NULL;
1620 1974272 : if (tds) {
1621 378 : ++count;
1622 378 : dbproc = (DBPROCESS *) tds_get_parent(tds);
1623 378 : tds_close_socket(tds);
1624 378 : tds_free_socket(tds);
1625 378 : if (dbproc) {
1626 : /* avoid locking in dbclose */
1627 378 : dbproc->tds_socket = NULL;
1628 378 : dbclose(dbproc);
1629 : }
1630 : }
1631 : }
1632 482 : if (g_dblib_ctx.connection_list) {
1633 482 : TDS_ZERO_FREE(g_dblib_ctx.connection_list);
1634 482 : g_dblib_ctx.connection_list_size = 0;
1635 482 : g_dblib_ctx.connection_list_size_represented = 0;
1636 : }
1637 :
1638 482 : dblib_release_tds_ctx(count);
1639 :
1640 482 : tds_mutex_unlock(&dblib_mutex);
1641 : }
1642 :
1643 : static const char *
1644 : prdbresults_state(int retcode)
1645 : {
1646 0 : switch(retcode) {
1647 : case _DB_RES_INIT: return "_DB_RES_INIT";
1648 0 : case _DB_RES_RESULTSET_EMPTY: return "_DB_RES_RESULTSET_EMPTY";
1649 0 : case _DB_RES_RESULTSET_ROWS: return "_DB_RES_RESULTSET_ROWS";
1650 0 : case _DB_RES_NEXT_RESULT: return "_DB_RES_NEXT_RESULT";
1651 0 : case _DB_RES_NO_MORE_RESULTS: return "_DB_RES_NO_MORE_RESULTS";
1652 0 : case _DB_RES_SUCCEED: return "_DB_RES_SUCCEED";
1653 : default: break;
1654 : }
1655 : return "??";
1656 : }
1657 :
1658 : static const char *
1659 : prdbretcode(RETCODE retcode)
1660 : {
1661 0 : switch(retcode) {
1662 : case REG_ROW: return "REG_ROW/MORE_ROWS";
1663 0 : case NO_MORE_ROWS: return "NO_MORE_ROWS";
1664 0 : case BUF_FULL: return "BUF_FULL";
1665 0 : case NO_MORE_RESULTS: return "NO_MORE_RESULTS";
1666 0 : case SUCCEED: return "SUCCEED";
1667 0 : case FAIL: return "FAIL";
1668 : default: break;
1669 : }
1670 : return "??";
1671 : }
1672 :
1673 : static const char *
1674 : prretcode(int retcode)
1675 : {
1676 0 : switch(retcode) {
1677 : case TDS_SUCCESS: return "TDS_SUCCESS";
1678 0 : case TDS_FAIL: return "TDS_FAIL";
1679 0 : case TDS_NO_MORE_RESULTS: return "TDS_NO_MORE_RESULTS";
1680 0 : case TDS_CANCELLED: return "TDS_CANCELLED";
1681 : default: break;
1682 : }
1683 : return "??";
1684 : }
1685 :
1686 : static const char *
1687 0 : prresult_type(int result_type)
1688 : {
1689 0 : switch(result_type) {
1690 : case TDS_ROW_RESULT: return "TDS_ROW_RESULT";
1691 0 : case TDS_PARAM_RESULT: return "TDS_PARAM_RESULT";
1692 0 : case TDS_STATUS_RESULT: return "TDS_STATUS_RESULT";
1693 0 : case TDS_MSG_RESULT: return "TDS_MSG_RESULT";
1694 0 : case TDS_COMPUTE_RESULT: return "TDS_COMPUTE_RESULT";
1695 0 : case TDS_CMD_DONE: return "TDS_CMD_DONE";
1696 0 : case TDS_CMD_SUCCEED: return "TDS_CMD_SUCCEED";
1697 0 : case TDS_CMD_FAIL: return "TDS_CMD_FAIL";
1698 0 : case TDS_ROWFMT_RESULT: return "TDS_ROWFMT_RESULT";
1699 0 : case TDS_COMPUTEFMT_RESULT: return "TDS_COMPUTEFMT_RESULT";
1700 0 : case TDS_DESCRIBE_RESULT: return "TDS_DESCRIBE_RESULT";
1701 0 : case TDS_DONE_RESULT: return "TDS_DONE_RESULT";
1702 0 : case TDS_DONEPROC_RESULT: return "TDS_DONEPROC_RESULT";
1703 0 : case TDS_DONEINPROC_RESULT: return "TDS_DONEINPROC_RESULT";
1704 0 : case TDS_OTHERS_RESULT: return "TDS_OTHERS_RESULT";
1705 : default: break;
1706 : }
1707 0 : return "??";
1708 : }
1709 :
1710 : /**
1711 : * \ingroup dblib_core
1712 : * \brief Set up query results.
1713 : *
1714 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1715 : * \retval SUCCEED Some results are available.
1716 : * \retval FAIL query was not processed successfully by the server
1717 : * \retval NO_MORE_RESULTS query produced no results.
1718 : *
1719 : * \remarks Call dbresults() after calling dbsqlexec() or dbsqlok(), or dbrpcsend() returns SUCCEED. Unless
1720 : * one of them fails, dbresults will return either SUCCEED or NO_MORE_RESULTS.
1721 : *
1722 : * The meaning of \em results is very specific and not very intuitive. Results are created by either
1723 : * - a SELECT statement
1724 : * - a stored procedure
1725 : *
1726 : * When dbresults returns SUCCEED, therefore, it indicates the server processed the query successfully and
1727 : * that one or more of these is present:
1728 : * - metadata -- dbnumcols() returns 1 or more
1729 : * - data -- dbnextrow() returns SUCCEED
1730 : * - return status -- dbhasretstat() returns TRUE
1731 : * - output parameters -- dbnumrets() returns 1 or more
1732 : *
1733 : * If none of the above are present, dbresults() returns NO_MORE_RESULTS.
1734 : *
1735 : * SUCCEED does not imply that DBROWS() will return TRUE or even that dbnumcols() will return nonzero.
1736 : * A general algorithm for reading results will call dbresults() until it return NO_MORE_RESULTS (or FAIL).
1737 : * An application should check for all the above kinds of results within the dbresults() loop.
1738 : *
1739 : * \sa dbsqlexec(), dbsqlok(), dbrpcsend(), dbcancel(), DBROWS(), dbnextrow(), dbnumcols(), dbhasretstat(), dbretstatus(), dbnumrets()
1740 : */
1741 : RETCODE
1742 30003 : dbresults(DBPROCESS * dbproc)
1743 : {
1744 30003 : RETCODE erc = _dbresults(dbproc);
1745 :
1746 30003 : tdsdump_log(TDS_DBG_FUNC, "dbresults returning %d (%s)\n", erc, prdbretcode(erc));
1747 30003 : return erc;
1748 : }
1749 :
1750 : static RETCODE
1751 30003 : _dbresults(DBPROCESS * dbproc)
1752 : {
1753 : TDSSOCKET *tds;
1754 30003 : int result_type = 0, done_flags;
1755 :
1756 30003 : tdsdump_log(TDS_DBG_FUNC, "dbresults(%p)\n", dbproc);
1757 30003 : CHECK_CONN(FAIL);
1758 :
1759 30003 : tds = dbproc->tds_socket;
1760 :
1761 30003 : tdsdump_log(TDS_DBG_FUNC, "dbresults: dbresults_state is %d (%s)\n",
1762 : dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
1763 30003 : switch ( dbproc->dbresults_state ) {
1764 8224 : case _DB_RES_SUCCEED:
1765 8224 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
1766 8224 : return SUCCEED;
1767 : break;
1768 0 : case _DB_RES_RESULTSET_ROWS:
1769 0 : dbperror(dbproc, SYBERPND, 0); /* dbresults called while rows outstanding.... */
1770 0 : return FAIL;
1771 : break;
1772 : case _DB_RES_NO_MORE_RESULTS:
1773 : return NO_MORE_RESULTS;
1774 : break;
1775 : default:
1776 : break;
1777 : }
1778 :
1779 : for (;;) {
1780 31376 : TDSRET retcode = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS);
1781 :
1782 31376 : tdsdump_log(TDS_DBG_FUNC, "dbresults() tds_process_tokens returned %d (%s),\n\t\t\tresult_type %d (%s)\n",
1783 : retcode, prretcode(retcode), result_type, prresult_type(result_type));
1784 :
1785 31376 : switch (retcode) {
1786 :
1787 22914 : case TDS_SUCCESS:
1788 :
1789 22914 : switch (result_type) {
1790 :
1791 298 : case TDS_ROWFMT_RESULT:
1792 298 : buffer_free(&dbproc->row_buf);
1793 298 : buffer_alloc(dbproc);
1794 298 : dbproc->dbresults_state = _DB_RES_RESULTSET_EMPTY;
1795 298 : break;
1796 :
1797 : case TDS_COMPUTEFMT_RESULT:
1798 : break;
1799 :
1800 12080 : case TDS_ROW_RESULT:
1801 : case TDS_COMPUTE_RESULT:
1802 :
1803 12080 : dbproc->dbresults_state = _DB_RES_RESULTSET_ROWS;
1804 12080 : return SUCCEED;
1805 : break;
1806 :
1807 1229 : case TDS_DONE_RESULT:
1808 : case TDS_DONEPROC_RESULT:
1809 1229 : tdsdump_log(TDS_DBG_FUNC, "dbresults(): dbresults_state is %d (%s)\n",
1810 : dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
1811 :
1812 : /* A done token signifies the end of a logical command.
1813 : * There are three possibilities:
1814 : * 1. Simple command with no result set, i.e. update, delete, insert
1815 : * 2. Command with result set but no rows
1816 : * 3. Command with result set and rows
1817 : */
1818 1229 : switch (dbproc->dbresults_state) {
1819 :
1820 815 : case _DB_RES_INIT:
1821 : case _DB_RES_NEXT_RESULT:
1822 815 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
1823 815 : if (done_flags & TDS_DONE_ERROR)
1824 : return FAIL;
1825 799 : if (result_type == TDS_DONE_RESULT) {
1826 693 : tds_free_all_results(tds);
1827 693 : return SUCCEED;
1828 : }
1829 : break;
1830 :
1831 414 : case _DB_RES_RESULTSET_EMPTY:
1832 : case _DB_RES_RESULTSET_ROWS:
1833 414 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
1834 414 : return SUCCEED;
1835 : break;
1836 : default:
1837 0 : assert(0);
1838 : break;
1839 : }
1840 : break;
1841 :
1842 9253 : case TDS_DONEINPROC_RESULT:
1843 : /*
1844 : * Return SUCCEED on a command within a stored procedure
1845 : * only if the command returned a result set.
1846 : */
1847 9253 : switch (dbproc->dbresults_state) {
1848 9241 : case _DB_RES_INIT:
1849 : case _DB_RES_NEXT_RESULT:
1850 9241 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
1851 9241 : break;
1852 12 : case _DB_RES_RESULTSET_EMPTY :
1853 : case _DB_RES_RESULTSET_ROWS :
1854 12 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
1855 12 : return SUCCEED;
1856 : break;
1857 : case _DB_RES_NO_MORE_RESULTS:
1858 : case _DB_RES_SUCCEED:
1859 : break;
1860 : }
1861 : break;
1862 :
1863 : case TDS_STATUS_RESULT:
1864 : case TDS_MSG_RESULT:
1865 : case TDS_DESCRIBE_RESULT:
1866 : case TDS_PARAM_RESULT:
1867 : default:
1868 : break;
1869 : }
1870 :
1871 : break;
1872 :
1873 8462 : case TDS_NO_MORE_RESULTS:
1874 8462 : dbproc->dbresults_state = _DB_RES_NO_MORE_RESULTS;
1875 8462 : return NO_MORE_RESULTS;
1876 : break;
1877 :
1878 0 : default:
1879 0 : assert(TDS_FAILED(retcode));
1880 0 : dbproc->dbresults_state = _DB_RES_INIT;
1881 0 : return FAIL;
1882 : break;
1883 : }
1884 : }
1885 : }
1886 :
1887 :
1888 : /**
1889 : * \ingroup dblib_core
1890 : * \brief Return number of regular columns in a result set.
1891 : *
1892 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1893 : * \sa dbcollen(), dbcolname(), dbnumalts().
1894 : */
1895 : int
1896 4058 : dbnumcols(DBPROCESS * dbproc)
1897 : {
1898 4058 : tdsdump_log(TDS_DBG_FUNC, "dbnumcols(%p)\n", dbproc);
1899 4058 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
1900 :
1901 4058 : if (dbproc && dbproc->tds_socket && dbproc->tds_socket->res_info)
1902 3798 : return dbproc->tds_socket->res_info->num_cols;
1903 : return 0;
1904 : }
1905 :
1906 : /**
1907 : * \ingroup dblib_core
1908 : * \brief Return name of a regular result column.
1909 : *
1910 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1911 : * \param column Nth in the result set, starting with 1.
1912 : * \return pointer to ASCII null-terminated string, the name of the column.
1913 : * \retval NULL \a column is not in range.
1914 : * \sa dbcollen(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
1915 : * \bug Relies on ASCII column names, post iconv conversion.
1916 : * Will not work as described for UTF-8 or UCS-2 clients.
1917 : * But maybe it shouldn't.
1918 : */
1919 : char *
1920 2858 : dbcolname(DBPROCESS * dbproc, int column)
1921 : {
1922 : TDSCOLUMN *colinfo;
1923 :
1924 2858 : tdsdump_log(TDS_DBG_FUNC, "dbcolname(%p, %d)\n", dbproc, column);
1925 :
1926 2858 : colinfo = dbcolptr(dbproc, column);
1927 2858 : if (!colinfo)
1928 : return NULL;
1929 :
1930 5716 : return tds_dstr_buf(&colinfo->column_name);
1931 : }
1932 :
1933 : /**
1934 : * \ingroup dblib_core
1935 : * \brief Return name of a computed result column.
1936 : *
1937 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1938 : * \param computeid identifies which one of potientially many compute rows is meant. The first compute
1939 : * clause has \a computeid == 1.
1940 : * \param column Nth in the result set, starting with 1.
1941 : * \return pointer to ASCII null-terminated string, the name of the column.
1942 : * \retval NULL \a column is not in range.
1943 : * \sa dbcollen(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
1944 : */
1945 : const char *
1946 0 : dbacolname(DBPROCESS * dbproc, int computeid, int column)
1947 : {
1948 : TDSCOLUMN *colinfo;
1949 :
1950 0 : tdsdump_log(TDS_DBG_FUNC, "dbacolname(%p, %d, %d)\n", dbproc, computeid, column);
1951 :
1952 0 : colinfo = dbacolptr(dbproc, computeid, column, true);
1953 0 : if (!colinfo)
1954 : return NULL;
1955 :
1956 0 : return tds_dstr_cstr(&colinfo->column_name);
1957 : }
1958 :
1959 : /**
1960 : * \ingroup dblib_core
1961 : * \brief Read a row from the row buffer.
1962 : *
1963 : * When row buffering is enabled (DBBUFFER option is on), the client can use dbgetrow() to re-read a row previously fetched
1964 : * with dbnextrow(). The effect is to move the row pointer -- analogous to fseek() -- back to \a row.
1965 : * Calls to dbnextrow() read from \a row + 1 until the buffer is exhausted, at which point it resumes
1966 : * its normal behavior, except that as each row is fetched from the server, it is added to the row
1967 : * buffer (in addition to being returned to the client). When the buffer is filled, dbnextrow() returns
1968 : * \c FAIL until the buffer is at least partially emptied with dbclrbuf().
1969 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1970 : * \param row Nth row to read, starting with 1.
1971 : * \retval REG_ROW returned row is a regular row.
1972 : * \returns computeid when returned row is a compute row.
1973 : * \retval NO_MORE_ROWS no such row in the row buffer. Current row is unchanged.
1974 : * \retval FAIL unsuccessful; row buffer may be full.
1975 : * \sa dbaltbind(), dbbind(), dbclrbuf(), DBCURROW(), DBFIRSTROW(), DBLASTROW(), dbnextrow(), dbsetrow().
1976 : */
1977 : RETCODE
1978 60 : dbgetrow(DBPROCESS * dbproc, DBINT row)
1979 : {
1980 60 : RETCODE result = FAIL;
1981 60 : const int idx = buffer_row2idx(&dbproc->row_buf, row);
1982 :
1983 60 : tdsdump_log(TDS_DBG_FUNC, "dbgetrow(%p, %d)\n", dbproc, row);
1984 60 : CHECK_CONN(FAIL);
1985 :
1986 60 : if (-1 == idx)
1987 : return NO_MORE_ROWS;
1988 :
1989 40 : dbproc->row_buf.current = idx;
1990 40 : buffer_transfer_bound_data(&dbproc->row_buf, TDS_ROW_RESULT, 0, dbproc, idx);
1991 40 : result = REG_ROW;
1992 :
1993 40 : return result;
1994 : }
1995 :
1996 : /**
1997 : * \ingroup dblib_core
1998 : * \brief Define substitution values to be used when binding null values.
1999 : *
2000 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2001 : * \param bindtype type of binding to which the substitute value will apply.
2002 : * \param bindlen size of the substitute value you are supplying, in bytes.
2003 : * Ignored except for CHARBIND and BINARYBIND.
2004 : * \param bindval pointer to a buffer containing the substitute value.
2005 : * \retval SUCCEED query was processed without errors.
2006 : * \retval FAIL query was not processed
2007 : * \sa dbaltbind(), dbbind(), dbconvert(), dbnullbind().
2008 : */
2009 : RETCODE
2010 400 : dbsetnull(DBPROCESS * dbproc, int bindtype, int bindlen, BYTE *bindval)
2011 : {
2012 : BYTE *pval;
2013 :
2014 400 : tdsdump_log(TDS_DBG_FUNC, "dbsetnull(%p, %d, %d, %p)\n", dbproc, bindtype, bindlen, bindval);
2015 :
2016 400 : CHECK_CONN(FAIL);
2017 400 : CHECK_PARAMETER(bindval, SYBENBVP, FAIL);
2018 :
2019 400 : switch (bindtype) {
2020 330 : case DATETIMEBIND:
2021 : case DECIMALBIND:
2022 : case SRCDECIMALBIND:
2023 : case FLT8BIND:
2024 : case INTBIND:
2025 : case MONEYBIND:
2026 : case NUMERICBIND:
2027 : case SRCNUMERICBIND:
2028 : case REALBIND:
2029 : case SMALLBIND:
2030 : case SMALLDATETIMEBIND:
2031 : case SMALLMONEYBIND:
2032 : case TINYBIND:
2033 : case BIGINTBIND:
2034 : case DATEBIND:
2035 : case TIMEBIND:
2036 : case BIGDATETIMEBIND:
2037 : case BIGTIMEBIND:
2038 330 : bindlen = (int)default_null_representations[bindtype].len;
2039 330 : break;
2040 :
2041 70 : case CHARBIND:
2042 : case BINARYBIND:
2043 70 : CHECK_PARAMETER(bindlen >= 0, SYBEBBL, FAIL);
2044 : break;
2045 :
2046 0 : case NTBSTRINGBIND: bindlen = (int)strlen((char *) bindval);
2047 0 : break;
2048 0 : case STRINGBIND: bindlen = (int)strlen((char *) bindval);
2049 0 : break;
2050 0 : case VARYBINBIND: bindlen = ((DBVARYBIN*) bindval)->len;
2051 0 : break;
2052 0 : case VARYCHARBIND: bindlen = ((DBVARYCHAR*) bindval)->len;
2053 0 : break;
2054 :
2055 : #if 0
2056 : case SENSITIVITYBIND:
2057 : case BOUNDARYBIND:
2058 : #endif
2059 0 : default:
2060 0 : dbperror(dbproc, SYBEBTYP, 0);
2061 0 : return FAIL;
2062 : }
2063 :
2064 400 : if ((pval = tds_new(BYTE, bindlen)) == NULL) {
2065 0 : dbperror(dbproc, SYBEMEM, errno);
2066 0 : return FAIL;
2067 : }
2068 :
2069 : /* free any prior allocation */
2070 400 : if (dbproc->nullreps[bindtype].bindval != default_null_representations[bindtype].bindval)
2071 380 : free((BYTE*)dbproc->nullreps[bindtype].bindval);
2072 :
2073 400 : memcpy(pval, bindval, bindlen);
2074 :
2075 400 : dbproc->nullreps[bindtype].bindval = pval;
2076 400 : dbproc->nullreps[bindtype].len = bindlen;
2077 :
2078 400 : tdsdump_dump_buf(TDS_DBG_NETWORK, "null representation set ", pval, bindlen);
2079 : return SUCCEED;
2080 : }
2081 :
2082 : /**
2083 : * \ingroup dblib_core
2084 : * \brief Make a buffered row "current" without fetching it into bound variables.
2085 : *
2086 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2087 : * \retval MORE_ROWS row found
2088 : * \retval NO_MORE_ROWS row not found
2089 : * \retval FAIL \a dbproc is dead or not enabled
2090 : * \sa dbaltbind(), dbbind(), dbcanquery(), dbclrbuf(), dbgetrow(), dbnextrow(), dbprrow().
2091 : */
2092 : STATUS
2093 0 : dbsetrow(DBPROCESS * dbproc, DBINT row)
2094 : {
2095 0 : const int idx = buffer_row2idx(&dbproc->row_buf, row);
2096 :
2097 0 : tdsdump_log(TDS_DBG_FUNC, "dbsetrow(%p, %d)\n", dbproc, row);
2098 0 : CHECK_CONN(FAIL);
2099 :
2100 0 : if (-1 == idx)
2101 : return NO_MORE_ROWS;
2102 :
2103 0 : dbproc->row_buf.current = idx;
2104 :
2105 : /* FIXME: should determine REG_ROW or compute_id; */
2106 0 : return REG_ROW;
2107 : }
2108 :
2109 : /**
2110 : * \ingroup dblib_core
2111 : * \brief Read result row into the row buffer and into any bound host variables.
2112 : *
2113 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2114 : * \retval REG_ROW regular row has been read.
2115 : * \returns computeid when a compute row is read.
2116 : * \retval BUF_FULL reading next row would cause the buffer to be exceeded (and buffering is turned on).
2117 : * No row was read from the server
2118 : * \sa dbaltbind(), dbbind(), dbcanquery(), dbclrbuf(), dbgetrow(), dbprrow(), dbsetrow().
2119 : */
2120 : struct pivot_t;
2121 : STATUS
2122 220609 : dbnextrow(DBPROCESS * dbproc)
2123 : {
2124 : TDSRESULTINFO *resinfo;
2125 : TDSSOCKET *tds;
2126 220609 : STATUS result = FAIL;
2127 : TDS_INT res_type;
2128 : TDS_INT computeid;
2129 : int idx; /* row buffer index. Unless DBUFFER is on, idx will always be 0. */
2130 : struct pivot_t *pivot;
2131 :
2132 220609 : tdsdump_log(TDS_DBG_FUNC, "dbnextrow(%p)\n", dbproc);
2133 220609 : CHECK_CONN(FAIL);
2134 :
2135 220609 : tds = dbproc->tds_socket;
2136 220609 : resinfo = tds->res_info;
2137 :
2138 220609 : tdsdump_log(TDS_DBG_FUNC, "dbnextrow() dbresults_state = %d (%s)\n",
2139 : dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
2140 :
2141 220609 : if (!resinfo || dbproc->dbresults_state != _DB_RES_RESULTSET_ROWS) {
2142 : /* no result set or result set empty (no rows) */
2143 874 : tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning %d (NO_MORE_ROWS)\n", NO_MORE_ROWS);
2144 874 : return dbproc->row_type = NO_MORE_ROWS;
2145 : }
2146 :
2147 : /*
2148 : * Try to get the dbproc->row_buf.current item from the buffered rows, if any.
2149 : * Else read from the stream, unless the buffer is exhausted.
2150 : * If no rows are read, DBROWTYPE() will report NO_MORE_ROWS.
2151 : */
2152 219735 : dbproc->row_type = NO_MORE_ROWS;
2153 219735 : computeid = REG_ROW;
2154 219735 : if (-1 != (idx = buffer_current_index(dbproc))) {
2155 : /*
2156 : * Cool, the item we want is already there
2157 : */
2158 10 : result = dbproc->row_type = REG_ROW;
2159 10 : res_type = TDS_ROW_RESULT;
2160 :
2161 219725 : } else if (buffer_is_full(&dbproc->row_buf)) {
2162 :
2163 120 : result = BUF_FULL;
2164 120 : res_type = TDS_ROWFMT_RESULT;
2165 :
2166 219605 : } else if ((pivot = dbrows_pivoted(dbproc)) != NULL) {
2167 :
2168 0 : tdsdump_log(TDS_DBG_FUNC, "returning pivoted row\n");
2169 0 : return dbnextrow_pivoted(dbproc, pivot);
2170 :
2171 : } else {
2172 219605 : const int mask = TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE;
2173 219605 : TDS_INT8 row_count = TDS_NO_COUNT;
2174 219605 : bool rows_set = false;
2175 219605 : buffer_save_row(dbproc);
2176 :
2177 : /* Get the row from the TDS stream. */
2178 718 : again:
2179 220323 : switch (tds_process_tokens(tds, &res_type, NULL, mask)) {
2180 220293 : case TDS_SUCCESS:
2181 220293 : if (res_type == TDS_ROW_RESULT || res_type == TDS_COMPUTE_RESULT) {
2182 207705 : if (res_type == TDS_COMPUTE_RESULT)
2183 32 : computeid = tds->current_results->computeid;
2184 : /* Add the row to the row buffer, whose capacity is always at least 1 */
2185 207705 : resinfo = tds->current_results;
2186 207705 : idx = buffer_add_row(dbproc, resinfo);
2187 207705 : assert(idx != -1);
2188 207705 : result = dbproc->row_type = (res_type == TDS_ROW_RESULT)? REG_ROW : computeid;
2189 : #if 0 /* TODO */
2190 : tds_process_tokens(tds, &res_type, NULL, TDS_TOKEN_TRAILING);
2191 : #endif
2192 207705 : break;
2193 : }
2194 : /* allows to process trailing tokens */
2195 12588 : if (res_type == TDS_DONEINPROC_RESULT) {
2196 718 : if (!rows_set)
2197 462 : row_count = tds->rows_affected;
2198 : rows_set = true;
2199 : goto again;
2200 : }
2201 : /* fall through */
2202 : case TDS_NO_MORE_RESULTS:
2203 11900 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
2204 11900 : result = NO_MORE_ROWS;
2205 11900 : break;
2206 0 : default:
2207 0 : tdsdump_log(TDS_DBG_FUNC, "unexpected: leaving dbnextrow() returning FAIL\n");
2208 : return FAIL;
2209 : break;
2210 : }
2211 219605 : if (rows_set)
2212 462 : tds->rows_affected = row_count;
2213 : }
2214 :
2215 219735 : if (res_type == TDS_ROW_RESULT || res_type == TDS_COMPUTE_RESULT) {
2216 : /*
2217 : * Transfer the data from the row buffer to the bound variables.
2218 : */
2219 207715 : buffer_transfer_bound_data(&dbproc->row_buf, res_type, computeid, dbproc, idx);
2220 : }
2221 :
2222 219735 : if (res_type == TDS_COMPUTE_RESULT) {
2223 32 : tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning compute_id %d\n", result);
2224 : } else {
2225 219703 : tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning %d (%s)\n", result, prdbretcode(result));
2226 : }
2227 : return result;
2228 : } /* dbnextrow() */
2229 :
2230 : static TDS_SERVER_TYPE
2231 : dblib_bound_type(int bindtype)
2232 : {
2233 : switch (bindtype) {
2234 : case CHARBIND:
2235 : case STRINGBIND:
2236 : case NTBSTRINGBIND:
2237 : return SYBCHAR;
2238 : break;
2239 : case FLT8BIND:
2240 : return SYBFLT8;
2241 : break;
2242 : case REALBIND:
2243 : return SYBREAL;
2244 : break;
2245 : case INTBIND:
2246 : return SYBINT4;
2247 : break;
2248 : case SMALLBIND:
2249 : return SYBINT2;
2250 : break;
2251 : case TINYBIND:
2252 : return SYBINT1;
2253 : break;
2254 : case BIGINTBIND:
2255 : return SYBINT8;
2256 : break;
2257 : case DATETIMEBIND:
2258 : return SYBDATETIME;
2259 : break;
2260 : case SMALLDATETIMEBIND:
2261 : return SYBDATETIME4;
2262 : break;
2263 : case DATEBIND:
2264 : return SYBDATE;
2265 : break;
2266 : case TIMEBIND:
2267 : return SYBTIME;
2268 : break;
2269 : case BIGDATETIMEBIND:
2270 : return SYB5BIGDATETIME;
2271 : break;
2272 : case BIGTIMEBIND:
2273 : return SYB5BIGTIME;
2274 : break;
2275 : case MONEYBIND:
2276 : return SYBMONEY;
2277 : break;
2278 : case SMALLMONEYBIND:
2279 : return SYBMONEY4;
2280 : break;
2281 : case BINARYBIND:
2282 : return SYBBINARY;
2283 : break;
2284 : case VARYBINBIND:
2285 : return SYBVARBINARY;
2286 : break;
2287 : case VARYCHARBIND:
2288 : return SYBVARCHAR;
2289 : break;
2290 : case BITBIND:
2291 : return SYBBIT;
2292 : break;
2293 : case NUMERICBIND:
2294 : case SRCNUMERICBIND:
2295 : case DECIMALBIND:
2296 : case SRCDECIMALBIND:
2297 : return SYBNUMERIC;
2298 : break;
2299 : case DATETIME2BIND:
2300 : return SYBMSDATETIMEOFFSET;
2301 : break;
2302 : default:
2303 : return TDS_INVALID_TYPE;
2304 : }
2305 : }
2306 :
2307 : /**
2308 : * \ingroup dblib_core
2309 : * \brief Convert one datatype to another.
2310 : *
2311 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2312 : * \param srctype datatype of the data to convert.
2313 : * \param src buffer to convert
2314 : * \param srclen length of \a src
2315 : * \param desttype target datatype
2316 : * \param dest output buffer
2317 : * \param destlen size of \a dest
2318 : * \returns On success, the count of output bytes in \a dest, else -1. On failure, it will call any user-supplied error handler.
2319 : * \remarks Causes of failure:
2320 : * - No such conversion unavailable.
2321 : * - Character data output was truncated, or numerical data overflowed or lost precision.
2322 : * - In converting character data to one of the numeric types, the string could not be interpreted as a number.
2323 : *
2324 : * Conversion functions are handled in the TDS layer.
2325 : *
2326 : * The main reason for this is that \c ct-lib and \c ODBC (and presumably \c DBI) need
2327 : * to be able to do conversions between datatypes. This is possible because
2328 : * the format of complex data (dates, money, numeric, decimal) is defined by
2329 : * its representation on the wire; thus what we call \c DBMONEY is exactly its
2330 : * format on the wire. CLIs that need a different representation (ODBC?)
2331 : * need to convert from this format anyway, so the code would already be in
2332 : * place.
2333 : *
2334 : * Each datatype is also defined by its Server-type so all CLIs should be
2335 : * able to map native types to server types as well.
2336 : *
2337 : * tds_convert() copies from src to dest and returns the output data length,
2338 : * period. All padding and termination is the responsibility of the API library
2339 : * and is done post-conversion. The peculiar rule in dbconvert() is that
2340 : * a \a destlen of -1 and a \a desttype of \c SYBCHAR means the output buffer
2341 : * should be null-terminated.
2342 : *
2343 : * \sa dbaltbind(), dbaltbind_ps(), dbbind(), dbbind_ps(), dbconvert_ps(), dberrhandle(), dbsetnull(), dbsetversion(), dbwillconvert().
2344 : * \todo What happens if client does not reset values?
2345 : * \todo Microsoft and Sybase define this function differently.
2346 : */
2347 : DBINT
2348 2388 : dbconvert_ps(DBPROCESS * dbproc, int db_srctype, const BYTE * src, DBINT srclen,
2349 : int db_desttype, BYTE * dest, DBINT destlen, DBTYPEINFO * typeinfo)
2350 : {
2351 : CONV_RESULT dres;
2352 : DBINT ret;
2353 : int i;
2354 : int len;
2355 : TDS_SERVER_TYPE srctype, desttype;
2356 :
2357 2388 : tdsdump_log(TDS_DBG_FUNC, "dbconvert_ps(%p, %s, %p, %d, %s, %p, %d, %p)\n",
2358 : dbproc, tds_prdatatype(db_srctype), src, srclen,
2359 : tds_prdatatype(db_desttype), dest, destlen, typeinfo);
2360 : /* dbproc and src can be NULLs */
2361 2388 : CHECK_PARAMETER(dest, SYBEACNV, -1);
2362 :
2363 2388 : DBPERROR_RETURN(!is_tds_type_valid(db_srctype), SYBEUDTY);
2364 2388 : srctype = (TDS_SERVER_TYPE) db_srctype;
2365 2388 : DBPERROR_RETURN(!is_tds_type_valid(db_desttype), SYBEUDTY);
2366 2388 : desttype = (TDS_SERVER_TYPE) db_desttype;
2367 :
2368 2388 : if (is_numeric_type(desttype)) {
2369 480 : TDS_NUMERIC *d = &dres.n;
2370 :
2371 480 : if (typeinfo == NULL) {
2372 0 : if (is_numeric_type(srctype)) {
2373 0 : DBNUMERIC *s = (DBNUMERIC *) src;
2374 0 : d->precision = s->precision;
2375 0 : d->scale = s->scale;
2376 : } else {
2377 0 : d->precision = 18;
2378 0 : d->scale = 0;
2379 : }
2380 : } else {
2381 480 : d->precision = typeinfo->precision;
2382 480 : d->scale = typeinfo->scale;
2383 : }
2384 : }
2385 :
2386 2388 : if (0 == destlen)
2387 : return 0;
2388 :
2389 2388 : if (src == NULL || srclen == 0) {
2390 122 : int bind = dbbindtype(desttype);
2391 122 : int size = tds_get_size_by_type(desttype);
2392 :
2393 122 : if (bind == NTBSTRINGBIND) {
2394 22 : if (destlen > 0) {
2395 : size = destlen;
2396 : bind = CHARBIND;
2397 : } else {
2398 14 : size = 1;
2399 14 : bind = NTBSTRINGBIND;
2400 : }
2401 100 : } else if (bind == BINARYBIND) {
2402 100 : if (destlen > 0)
2403 : size = destlen;
2404 : else
2405 10 : size = 0;
2406 : }
2407 :
2408 122 : dbgetnull(dbproc, bind, size, dest);
2409 122 : return size;
2410 : }
2411 :
2412 : /* srclen of -1 means the source data is definitely NULL terminated */
2413 2266 : if (srclen == -1)
2414 480 : srclen = (int)strlen((const char *) src);
2415 :
2416 : /* often times we are asked to convert a data type to itself */
2417 2266 : if (srctype == desttype && !is_numeric_type(desttype)) {
2418 150 : ret = -2; /* to make sure we always set it */
2419 150 : tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() srctype == desttype\n");
2420 150 : switch (desttype) {
2421 :
2422 10 : case SYBBINARY:
2423 : case SYBVARBINARY:
2424 : case SYBIMAGE:
2425 10 : if (srclen > destlen && destlen >= 0) {
2426 0 : dbperror(dbproc, SYBECOFL, 0);
2427 0 : ret = -1;
2428 : } else {
2429 10 : memcpy(dest, src, srclen);
2430 10 : if (srclen < destlen)
2431 0 : memset(dest + srclen, 0, destlen - srclen);
2432 : ret = srclen;
2433 : }
2434 : break;
2435 :
2436 140 : case SYBCHAR:
2437 : case SYBVARCHAR:
2438 : case SYBTEXT:
2439 : /* srclen of -1 means the source data is definitely NULL terminated */
2440 140 : if (srclen == -1)
2441 0 : srclen = (int)strlen((const char *) src);
2442 :
2443 140 : switch (destlen) {
2444 : case 0: /* nothing to copy */
2445 : ret = 0;
2446 : break;
2447 : case -1: /* rtrim and null terminate */
2448 80 : while (srclen && src[srclen - 1] == ' ') {
2449 20 : --srclen;
2450 : }
2451 : /* fall thru */
2452 : case -2: /* just null terminate */
2453 80 : memcpy(dest, src, srclen);
2454 80 : dest[srclen] = '\0';
2455 80 : ret = srclen;
2456 80 : break;
2457 60 : default:
2458 60 : assert(destlen > 0);
2459 60 : if (destlen < 0 || srclen > destlen) {
2460 20 : dbperror(dbproc, SYBECOFL, 0);
2461 20 : ret = -1;
2462 : } else {
2463 40 : memcpy(dest, src, srclen);
2464 40 : if (srclen < destlen)
2465 20 : memset(dest + srclen, ' ', destlen - srclen);
2466 : ret = srclen;
2467 : }
2468 : break;
2469 : }
2470 : break;
2471 0 : case SYBINT1:
2472 : case SYBINT2:
2473 : case SYBINT4:
2474 : case SYBINT8:
2475 : case SYBFLT8:
2476 : case SYBREAL:
2477 : case SYBBIT:
2478 : case SYBBITN:
2479 : case SYBMONEY:
2480 : case SYBMONEY4:
2481 : case SYBDATETIME:
2482 : case SYBDATETIME4:
2483 : case SYBDATE:
2484 : case SYBTIME:
2485 : case SYB5BIGDATETIME:
2486 : case SYB5BIGTIME:
2487 : case SYBUNIQUE:
2488 0 : ret = tds_get_size_by_type(desttype);
2489 0 : memcpy(dest, src, ret);
2490 0 : break;
2491 :
2492 0 : case SYBMSDATE:
2493 : case SYBMSTIME:
2494 : case SYBMSDATETIME2:
2495 : case SYBMSDATETIMEOFFSET:
2496 0 : ret = sizeof(TDS_DATETIMEALL);
2497 0 : memcpy(dest, src, ret);
2498 : break;
2499 :
2500 : default:
2501 : ret = -1;
2502 : break;
2503 : }
2504 130 : assert(ret > -2);
2505 : return ret;
2506 : }
2507 : /* end srctype == desttype */
2508 :
2509 : /*
2510 : * Character types need no conversion. Just move the data.
2511 : */
2512 2116 : if (is_similar_type(srctype, desttype)) {
2513 692 : if (src && dest && srclen > 0 && destlen >= srclen) {
2514 692 : memcpy(dest, src, srclen);
2515 692 : return srclen;
2516 : }
2517 : }
2518 :
2519 1424 : tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() calling tds_convert\n");
2520 :
2521 1424 : len = tds_convert(g_dblib_ctx.tds_ctx, srctype, src, srclen, desttype, &dres);
2522 1424 : tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() called tds_convert returned %d\n", len);
2523 :
2524 1424 : if (len < 0) {
2525 0 : _dblib_convert_err(dbproc, len);
2526 0 : return -1;
2527 : }
2528 :
2529 1424 : switch (desttype) {
2530 40 : case SYBBINARY:
2531 : case SYBVARBINARY:
2532 : case SYBIMAGE:
2533 40 : if (len > destlen && destlen >= 0) {
2534 0 : dbperror(dbproc, SYBECOFL, 0);
2535 0 : ret = -1;
2536 : } else {
2537 40 : memcpy(dest, dres.ib, len);
2538 40 : if (len < destlen)
2539 40 : memset(dest + len, 0, destlen - len);
2540 : ret = len;
2541 : }
2542 40 : free(dres.ib);
2543 40 : break;
2544 480 : case SYBINT1:
2545 : case SYBINT2:
2546 : case SYBINT4:
2547 : case SYBINT8:
2548 : case SYBFLT8:
2549 : case SYBREAL:
2550 : case SYBBIT:
2551 : case SYBBITN:
2552 : case SYBMONEY:
2553 : case SYBMONEY4:
2554 : case SYBDATETIME:
2555 : case SYBDATETIME4:
2556 : case SYBTIME:
2557 : case SYBDATE:
2558 : case SYB5BIGDATETIME:
2559 : case SYB5BIGTIME:
2560 : case SYBUNIQUE:
2561 : case SYBMSDATE:
2562 : case SYBMSTIME:
2563 : case SYBMSDATETIME2:
2564 : case SYBMSDATETIMEOFFSET:
2565 : case SYBNUMERIC:
2566 : case SYBDECIMAL:
2567 480 : memcpy(dest, &(dres.ti), len);
2568 480 : ret = len;
2569 480 : break;
2570 904 : case SYBCHAR:
2571 : case SYBVARCHAR:
2572 : case SYBTEXT:
2573 904 : tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() outputting %d bytes character data destlen = %d \n", len, destlen);
2574 :
2575 904 : if (destlen < -2)
2576 : destlen = 0; /* failure condition */
2577 :
2578 904 : switch (destlen) {
2579 : case 0:
2580 : ret = -1;
2581 : break;
2582 204 : case -1: /* rtrim and null terminate */
2583 204 : for (i = len - 1; i >= 0 && dres.c[i] == ' '; --i) {
2584 0 : len = i;
2585 : }
2586 204 : memcpy(dest, dres.c, len);
2587 204 : dest[len] = '\0';
2588 204 : ret = len;
2589 204 : break;
2590 0 : case -2: /* just null terminate */
2591 0 : memcpy(dest, dres.c, len);
2592 0 : dest[len] = 0;
2593 0 : ret = len;
2594 0 : break;
2595 700 : default:
2596 700 : assert(destlen > 0);
2597 700 : if (destlen < 0 || len > destlen) {
2598 0 : dbperror(dbproc, SYBECOFL, 0);
2599 0 : ret = -1;
2600 0 : tdsdump_log(TDS_DBG_INFO1, "%d bytes type %d -> %d, destlen %d < %d required\n",
2601 : srclen, srctype, desttype, destlen, len);
2602 : break;
2603 : }
2604 : /* else pad with blanks */
2605 700 : memcpy(dest, dres.c, len);
2606 700 : if (len < destlen)
2607 700 : memset(dest + len, ' ', destlen - len);
2608 : ret = len;
2609 :
2610 : break;
2611 : }
2612 :
2613 904 : free(dres.c);
2614 :
2615 904 : break;
2616 0 : default:
2617 0 : tdsdump_log(TDS_DBG_INFO1, "error: dbconvert_ps(): unrecognized desttype %d \n", desttype);
2618 : ret = -1;
2619 : break;
2620 :
2621 : }
2622 : return (ret);
2623 : }
2624 :
2625 : /**
2626 : * \ingroup dblib_core
2627 : * \brief cf. dbconvert_ps(), above
2628 : *
2629 : * \em Sybase: Convert numeric types.
2630 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2631 : * \param srctype datatype of the data to convert.
2632 : * \param src buffer to convert
2633 : * \param srclen length of \a src
2634 : * \param desttype target datatype
2635 : * \param dest output buffer
2636 : * \param destlen size of \a dest
2637 : * \param typeinfo address of a \c DBTYPEINFO structure that governs the precision & scale of the output, may be \c NULL.
2638 : * \sa dbaltbind(), dbaltbind_ps(), dbbind(), dbbind_ps(), dbconvert_ps(), dberrhandle(), dbsetnull(), dbsetversion(), dbwillconvert().
2639 : */
2640 : DBINT
2641 1908 : dbconvert(DBPROCESS * dbproc,
2642 : int srctype, const BYTE * src, DBINT srclen, int desttype, BYTE * dest, DBINT destlen)
2643 : {
2644 1908 : DBTYPEINFO ti, *pti = NULL;
2645 :
2646 1908 : tdsdump_log(TDS_DBG_FUNC, "dbconvert(%p)\n", dbproc);
2647 : /* dbproc can be NULL*/
2648 :
2649 1908 : DBPERROR_RETURN(!is_tds_type_valid(desttype), SYBEUDTY);
2650 :
2651 1908 : if (is_numeric_type(desttype)) {
2652 : DBNUMERIC *num;
2653 :
2654 : /* FIXME what happen if client do not reset values ??? */
2655 0 : if (dbproc->msdblib) {
2656 0 : num = (DBNUMERIC *) dest;
2657 0 : ti.precision = num->precision;
2658 0 : ti.scale = num->scale;
2659 0 : pti = &ti;
2660 : } else {
2661 : /* for Sybase passing NULL as DBTYPEINFO is fine */
2662 : }
2663 : }
2664 :
2665 1908 : return dbconvert_ps(dbproc, srctype, src, srclen, desttype, dest, destlen, pti);
2666 : }
2667 :
2668 : /**
2669 : * \ingroup dblib_core
2670 : * \brief Tie a host variable to a resultset column.
2671 : *
2672 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2673 : * \param column Nth column, starting at 1.
2674 : * \param vartype datatype of the host variable that will receive the data
2675 : * \param varlen size of host variable pointed to \a varaddr
2676 : * \param varaddr address of host variable
2677 : * \retval SUCCEED everything worked.
2678 : * \retval FAIL no such \a column or no such conversion possible, or target buffer too small.
2679 : * \sa
2680 : */
2681 : RETCODE
2682 21718 : dbbind(DBPROCESS * dbproc, int column, int vartype, DBINT varlen, BYTE * varaddr)
2683 : {
2684 21718 : TDSCOLUMN *colinfo = NULL;
2685 : TDSRESULTINFO* results;
2686 : TDS_SERVER_TYPE srctype, desttype;
2687 :
2688 21718 : tdsdump_log(TDS_DBG_FUNC, "dbbind(%p, %d, %d, %d, %p)\n", dbproc, column, vartype, varlen, varaddr);
2689 21718 : CHECK_CONN(FAIL);
2690 21718 : CHECK_PARAMETER(varaddr, SYBEABNV, FAIL);
2691 :
2692 21718 : results = dbproc->tds_socket->res_info;
2693 :
2694 21718 : if (results == NULL || results->num_cols < column || column < 1) {
2695 0 : dbperror(dbproc, SYBEABNC, 0);
2696 0 : return FAIL;
2697 : }
2698 :
2699 21718 : if (varlen < 0) {
2700 154 : switch (vartype) {
2701 90 : case CHARBIND:
2702 : case STRINGBIND:
2703 : case NTBSTRINGBIND:
2704 : case VARYCHARBIND:
2705 : case VARYBINBIND:
2706 : /*
2707 : * No message for this error. Documentation doesn't define varlen < 0, but
2708 : * experimentation with Sybase db-lib shows it's accepted as if zero.
2709 : */
2710 90 : tdsdump_log(TDS_DBG_FUNC, "dbbind: setting varlen (%d) to 0\n", varlen);
2711 : varlen = 0;
2712 : break;
2713 : }
2714 : }
2715 :
2716 21628 : if (0 == varlen) { /* "Note that if varlen is 0, no padding takes place." */
2717 21354 : switch (vartype) {
2718 10906 : case CHARBIND:
2719 : case STRINGBIND:
2720 : case NTBSTRINGBIND:
2721 10906 : varlen = -1;
2722 10906 : break;
2723 : default:
2724 : break; /* dbconvert: "The destlen is ignored for all fixed-length, non-NULL data types." */
2725 : }
2726 364 : }
2727 :
2728 21718 : dbproc->avail_flag = FALSE;
2729 :
2730 21718 : colinfo = dbproc->tds_socket->res_info->columns[column - 1];
2731 21718 : srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
2732 21718 : desttype = dblib_bound_type(vartype);
2733 21718 : if (desttype == TDS_INVALID_TYPE) {
2734 0 : dbperror(dbproc, SYBEBTYP, 0);
2735 0 : return FAIL;
2736 : }
2737 :
2738 21718 : if (!dbwillconvert(srctype, desttype)) {
2739 0 : dbperror(dbproc, SYBEABMT, 0);
2740 0 : return FAIL;
2741 : }
2742 :
2743 21718 : colinfo->column_varaddr = (char *) varaddr;
2744 21718 : colinfo->column_bindtype = vartype;
2745 21718 : colinfo->column_bindlen = varlen;
2746 :
2747 21718 : return SUCCEED;
2748 : } /* dbbind() */
2749 :
2750 : /**
2751 : * \ingroup dblib_core
2752 : * \brief set name and location of the \c interfaces file FreeTDS should use to look up a servername.
2753 : *
2754 : * Does not affect lookups or location of \c freetds.conf.
2755 : * \param filename name of \c interfaces. It can be NULL (reset to default)
2756 : * \sa dbopen()
2757 : */
2758 : void
2759 0 : dbsetifile(char *filename)
2760 : {
2761 0 : tdsdump_log(TDS_DBG_FUNC, "dbsetifile(%s)\n", filename? filename : "0x00");
2762 :
2763 0 : tds_set_interfaces_file_loc(filename);
2764 0 : }
2765 :
2766 : /**
2767 : * \ingroup dblib_core
2768 : * \brief Tie a null-indicator to a regular result column.
2769 : *
2770 : *
2771 : * When a row is fetched, the indicator variable tells the state of the column's data.
2772 : *
2773 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2774 : * \param column Nth column in the result set, starting with 1.
2775 : * \param indicator address of host variable.
2776 : * \retval SUCCEED variable accepted.
2777 : * \retval FAIL \a indicator is NULL or \a column is out of range.
2778 : * \remarks Contents of \a indicator are set with \c dbnextrow(). Possible values are:
2779 : * - 0 \a column bound successfully
2780 : * - -1 \a column is NULL.
2781 : * - >0 true length of data, had \a column not been truncated due to insufficient space in the columns bound host variable .
2782 : * \sa dbanullbind(), dbbind(), dbdata(), dbdatlen(), dbnextrow().
2783 : */
2784 : RETCODE
2785 240 : dbnullbind(DBPROCESS * dbproc, int column, DBINT * indicator)
2786 : {
2787 : TDSCOLUMN *colinfo;
2788 :
2789 240 : tdsdump_log(TDS_DBG_FUNC, "dbnullbind(%p, %d, %p)\n", dbproc, column, indicator);
2790 :
2791 240 : colinfo = dbcolptr(dbproc, column);
2792 240 : if (!colinfo)
2793 : return FAIL; /* dbcolptr sent SYBECNOR, Column number out of range */
2794 :
2795 240 : colinfo->column_nullbind = (TDS_SMALLINT *)indicator;
2796 240 : return SUCCEED;
2797 : }
2798 :
2799 : /**
2800 : * \ingroup dblib_core
2801 : * \brief Tie a null-indicator to a compute result column.
2802 : *
2803 : *
2804 : * When a row is fetched, the indicator variable tells the state of the column's data.
2805 : *
2806 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2807 : * \param computeid identifies which one of potientially many compute rows is meant. The first compute
2808 : * clause has \a computeid == 1.
2809 : * \param column Nth column in the result set, starting with 1.
2810 : * \param indicator address of host variable.
2811 : * \retval SUCCEED variable accepted.
2812 : * \retval FAIL \a indicator is NULL or \a column is out of range.
2813 : * \remarks Contents of \a indicator are set with \c dbnextrow(). Possible values are:
2814 : * - 0 \a column bound successfully
2815 : * - -1 \a column is NULL.
2816 : * - >0 true length of data, had \a column not been truncated due to insufficient space in the columns bound host variable .
2817 : * \sa dbadata(), dbadlen(), dbaltbind(), dbnextrow(), dbnullbind().
2818 : * \todo Never fails, but only because failure conditions aren't checked.
2819 : */
2820 : RETCODE
2821 0 : dbanullbind(DBPROCESS * dbproc, int computeid, int column, DBINT * indicator)
2822 : {
2823 : TDSCOLUMN *curcol;
2824 :
2825 0 : tdsdump_log(TDS_DBG_FUNC, "dbanullbind(%p, %d, %d, %p)\n", dbproc, computeid, column, indicator);
2826 :
2827 0 : curcol = dbacolptr(dbproc, computeid, column, true);
2828 0 : if (!curcol)
2829 : return FAIL;
2830 :
2831 : /*
2832 : * XXX Need to check for possibly problems before assuming
2833 : * everything is okay
2834 : */
2835 0 : curcol->column_nullbind = (TDS_SMALLINT *)indicator;
2836 :
2837 0 : return SUCCEED;
2838 : }
2839 :
2840 : /**
2841 : * \ingroup dblib_core
2842 : * \brief Indicates whether or not the count returned by dbcount is real (Microsoft-compatibility feature).
2843 : *
2844 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2845 : * \returns TRUE if the count returned by dbcount is real or FALSE if the count returned by dbcount is not real.
2846 : * \sa DBCOUNT(), dbcount().
2847 : */
2848 : BOOL
2849 0 : dbiscount(DBPROCESS * dbproc)
2850 : {
2851 0 : tdsdump_log(TDS_DBG_FUNC, "dbiscount(%p)\n", dbproc);
2852 0 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
2853 :
2854 0 : return dbproc->tds_socket && dbproc->tds_socket->rows_affected != TDS_NO_COUNT;
2855 : }
2856 :
2857 : /**
2858 : * \ingroup dblib_core
2859 : * \brief Get count of rows processed
2860 : *
2861 : *
2862 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2863 : * \returns
2864 : * - for insert/update/delete, count of rows affected.
2865 : * - for select, count of rows returned, after all rows have been fetched.
2866 : * \sa DBCOUNT(), dbnextrow(), dbresults().
2867 : */
2868 : DBINT
2869 1294 : dbcount(DBPROCESS * dbproc)
2870 : {
2871 1294 : tdsdump_log(TDS_DBG_FUNC, "dbcount(%p)\n", dbproc);
2872 1294 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
2873 :
2874 1294 : if (!dbproc->tds_socket || dbproc->tds_socket->rows_affected == TDS_NO_COUNT)
2875 : return -1;
2876 796 : return (DBINT)dbproc->tds_socket->rows_affected;
2877 : }
2878 :
2879 : /**
2880 : * \ingroup dblib_core
2881 : * \brief Clear \a n rows from the row buffer.
2882 : *
2883 : *
2884 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2885 : * \param n number of rows to remove, >= 0.
2886 : * \sa dbgetrow(), dbnextrow(), dbsetopt().
2887 : */
2888 : void
2889 200 : dbclrbuf(DBPROCESS * dbproc, DBINT n)
2890 : {
2891 200 : tdsdump_log(TDS_DBG_FUNC, "dbclrbuf(%p, %d)\n", dbproc, n);
2892 200 : CHECK_PARAMETER(dbproc, SYBENULL, );
2893 :
2894 200 : if (n <= 0)
2895 : return;
2896 :
2897 200 : if (dbproc->dbopts[DBBUFFER].factive) {
2898 160 : DBPROC_ROWBUF * buf = &(dbproc->row_buf);
2899 160 : int count = buffer_count(buf);
2900 160 : if (n >= count)
2901 140 : n = count - 1;
2902 160 : buffer_delete_rows(&(dbproc->row_buf), n);
2903 : }
2904 : }
2905 :
2906 : /**
2907 : * \ingroup dblib_core
2908 : * \brief Test whether or not a datatype can be converted to another datatype
2909 : *
2910 : * \param srctype type converting from
2911 : * \param desttype type converting to
2912 : * \remarks dbwillconvert() lies sometimes. Some datatypes \em should be convertible but aren't yet in our implementation.
2913 : * Legal unimplemented conversions return \em TRUE.
2914 : * \retval TRUE convertible, or should be.
2915 : * \retval FAIL not convertible.
2916 : * \sa dbaltbind(), dbbind(), dbconvert(), dbconvert_ps(), \c src/dblib/unittests/convert().c().
2917 : */
2918 : DBBOOL
2919 22594 : dbwillconvert(int srctype, int desttype)
2920 : {
2921 22594 : tdsdump_log(TDS_DBG_FUNC, "dbwillconvert(%s, %s)\n", tds_prdatatype(srctype), tds_prdatatype(desttype));
2922 22594 : return tds_willconvert(srctype, desttype) ? TRUE : FALSE;
2923 : }
2924 :
2925 : static int
2926 1448 : dblib_coltype(TDSCOLUMN *colinfo)
2927 : {
2928 1448 : switch (colinfo->column_type) {
2929 : case SYBVARCHAR:
2930 : return SYBCHAR;
2931 0 : case SYBVARBINARY:
2932 : return SYBBINARY;
2933 0 : case SYBLONGCHAR:
2934 : case SYBUNITEXT:
2935 : case SYBMSXML:
2936 : return SYBTEXT;
2937 : /* these types are handled by tds_get_conversion_type */
2938 : case SYBBITN:
2939 : case SYBDATEN:
2940 : case SYBDATETIMN:
2941 : case SYBFLTN:
2942 : case SYBINTN:
2943 : case SYBMONEYN:
2944 : case SYBTIMEN:
2945 : case SYBUINTN:
2946 : case SYBMSTABLE:
2947 : break;
2948 : /* these types are supported */
2949 : case SYBCHAR:
2950 : case SYBINT1:
2951 : case SYBINT2:
2952 : case SYBINT4:
2953 : case SYBINT8:
2954 : case SYBFLT8:
2955 : case SYBDATETIME:
2956 : case SYBBIT:
2957 : case SYBTEXT:
2958 : case SYBNTEXT:
2959 : case SYBIMAGE:
2960 : case SYBMONEY4:
2961 : case SYBMONEY:
2962 : case SYBDATETIME4:
2963 : case SYBREAL:
2964 : case SYBBINARY:
2965 : case SYBVOID:
2966 : case SYBNUMERIC:
2967 : case SYBDECIMAL:
2968 : case SYBNVARCHAR:
2969 : case SYBDATE:
2970 : case SYBTIME:
2971 : case SYB5BIGDATETIME:
2972 : case SYB5BIGTIME:
2973 : case SYBMSDATE:
2974 : case SYBMSTIME:
2975 : case SYBMSDATETIME2:
2976 : case SYBMSDATETIMEOFFSET:
2977 : break;
2978 : /* exclude cardinal types */
2979 : case XSYBVARBINARY:
2980 : case XSYBBINARY:
2981 : case XSYBNVARCHAR:
2982 : case XSYBVARCHAR:
2983 : case XSYBNCHAR:
2984 : case SYB5INT8:
2985 : break;
2986 : #if !ENABLE_EXTRA_CHECKS
2987 : /* TODO these should be handled in some ways */
2988 : case SYBUNIQUE: /* or SYBBLOB */
2989 : case SYBVARIANT:
2990 : case SYBMSUDT:
2991 : case SYBLONGBINARY:
2992 : case SYBUINT1:
2993 : case SYBUINT2:
2994 : case SYBUINT4:
2995 : case SYBUINT8:
2996 : case SYBINTERVAL:
2997 : case SYBSINT1:
2998 : case SYBXML:
2999 : break;
3000 : #endif
3001 : }
3002 328 : return tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
3003 : }
3004 :
3005 : /**
3006 : * \ingroup dblib_core
3007 : * \brief Get the datatype of a regular result set column.
3008 : *
3009 : *
3010 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3011 : * \param column Nth in the result set, starting from 1.
3012 : * \returns \c SYB* datetype token value, or zero if \a column out of range
3013 : * \sa dbcollen(), dbcolname(), dbdata(), dbdatlen(), dbnumcols(), dbprtype(), dbvarylen().
3014 : */
3015 : int
3016 1562 : dbcoltype(DBPROCESS * dbproc, int column)
3017 : {
3018 : TDSCOLUMN *colinfo;
3019 :
3020 1562 : tdsdump_log(TDS_DBG_FUNC, "dbcoltype(%p, %d)\n", dbproc, column);
3021 :
3022 1562 : colinfo = dbcolptr(dbproc, column);
3023 1562 : if (!colinfo)
3024 : return -1;
3025 :
3026 1432 : return dblib_coltype(colinfo);
3027 : }
3028 :
3029 : /**
3030 : * \ingroup dblib_core
3031 : * \brief Get user-defined datatype of a regular result column.
3032 : *
3033 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3034 : * \param column Nth in the result set, starting from 1.
3035 : * \returns \c SYB* datetype token value, or -1 if \a column out of range
3036 : * \sa dbaltutype(), dbcoltype().
3037 : */
3038 : int
3039 0 : dbcolutype(DBPROCESS * dbproc, int column)
3040 : {
3041 : TDSCOLUMN *colinfo;
3042 :
3043 0 : tdsdump_log(TDS_DBG_FUNC, "dbcolutype(%p, %d)\n", dbproc, column);
3044 :
3045 0 : colinfo = dbcolptr(dbproc, column);
3046 0 : if (!colinfo)
3047 : return -1;
3048 :
3049 0 : return colinfo->column_usertype;
3050 : }
3051 :
3052 : /**
3053 : * \ingroup dblib_core
3054 : * \brief Get precision and scale information for a regular result column.
3055 : *
3056 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3057 : * \param column Nth in the result set, starting from 1.
3058 : * \return Pointer to a DBTYPEINFO structure . NULL \a column is out of range.
3059 : * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols(), dbprtype(), dbvarylen().
3060 : */
3061 : DBTYPEINFO *
3062 0 : dbcoltypeinfo(DBPROCESS * dbproc, int column)
3063 : {
3064 : /* moved typeinfo from static into dbproc structure to make thread safe. (mlilback 11/7/01) */
3065 : TDSCOLUMN *colinfo;
3066 :
3067 0 : tdsdump_log(TDS_DBG_FUNC, "dbcoltypeinfo(%p, %d)\n", dbproc, column);
3068 :
3069 0 : colinfo = dbcolptr(dbproc, column);
3070 0 : if (!colinfo)
3071 : return NULL;
3072 :
3073 0 : dbproc->typeinfo.precision = colinfo->column_prec;
3074 0 : dbproc->typeinfo.scale = colinfo->column_scale;
3075 0 : return &dbproc->typeinfo;
3076 : }
3077 :
3078 : /**
3079 : * \brief Get a bunch of column attributes with a single call (Microsoft-compatibility feature).
3080 : *
3081 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3082 : * \param type must be CI_REGULAR or CI_ALTERNATE (CI_CURSOR is defined by the vendor, but is not yet implemented).
3083 : * \param column Nth in the result set, starting from 1.
3084 : * \param computeid (ignored)
3085 : * \param pdbcol address of structure to be populated by this function.
3086 : * \return SUCCEED or FAIL.
3087 : * \sa dbcolbrowse(), dbqual(), dbtabbrowse(), dbtabcount(), dbtabname(), dbtabsource(), dbtsnewlen(), dbtsnewval(), dbtsput().
3088 : * \todo Support cursor rows.
3089 : */
3090 : RETCODE
3091 0 : dbcolinfo(DBPROCESS *dbproc, CI_TYPE type, DBINT column, DBINT computeid, DBCOL *pdbcol)
3092 : {
3093 : DBTYPEINFO *ps;
3094 : TDSCOMPUTEINFO *info;
3095 : TDSCOLUMN *colinfo;
3096 : unsigned int i;
3097 :
3098 0 : tdsdump_log(TDS_DBG_FUNC, "dbcolinfo(%p, %d, %d, %d, %p)\n", dbproc, type, column, computeid, pdbcol);
3099 :
3100 0 : colinfo = dbcolptr(dbproc, column);
3101 0 : if (!colinfo)
3102 : return FAIL;
3103 :
3104 0 : CHECK_NULP(pdbcol, "dbcolinfo", 5, FAIL);
3105 :
3106 0 : if (type == CI_REGULAR) {
3107 :
3108 0 : strlcpy(pdbcol->Name, dbcolname(dbproc, column), sizeof(pdbcol->Name));
3109 0 : strlcpy(pdbcol->ActualName, dbcolname(dbproc, column), sizeof(pdbcol->ActualName));
3110 0 : strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
3111 :
3112 0 : pdbcol->Type = dbcoltype(dbproc, column);
3113 0 : pdbcol->UserType = dbcolutype(dbproc, column);
3114 0 : pdbcol->MaxLength = dbcollen(dbproc, column);
3115 0 : pdbcol->Null = _dbnullable(dbproc, column);
3116 0 : pdbcol->VarLength = dbvarylen(dbproc, column);
3117 :
3118 0 : ps = dbcoltypeinfo(dbproc, column);
3119 :
3120 0 : if( ps ) {
3121 0 : pdbcol->Precision = ps->precision;
3122 0 : pdbcol->Scale = ps->scale;
3123 : }
3124 :
3125 0 : pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE;
3126 0 : pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE;
3127 :
3128 0 : return SUCCEED;
3129 : }
3130 :
3131 0 : if (type == CI_ALTERNATE) {
3132 :
3133 0 : if (computeid == 0)
3134 : return FAIL;
3135 :
3136 0 : for (i = 0;; ++i) {
3137 0 : if (i >= dbproc->tds_socket->num_comp_info)
3138 : return FAIL;
3139 0 : info = dbproc->tds_socket->comp_info[i];
3140 0 : if (info->computeid == computeid)
3141 : break;
3142 : }
3143 :
3144 : /* if either the compute id or the column number are invalid, return -1 */
3145 0 : if (column < 1 || column > info->num_cols)
3146 : return FAIL;
3147 :
3148 0 : colinfo = info->columns[column - 1];
3149 :
3150 0 : strlcpy(pdbcol->Name, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->Name));
3151 0 : strlcpy(pdbcol->ActualName, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->ActualName));
3152 0 : strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
3153 :
3154 0 : pdbcol->Type = dbalttype(dbproc, computeid, column);
3155 0 : pdbcol->UserType = dbaltutype(dbproc, computeid, column);
3156 0 : pdbcol->MaxLength = dbaltlen(dbproc, computeid, column);
3157 0 : if (colinfo->column_nullable)
3158 0 : pdbcol->Null = TRUE;
3159 : else
3160 0 : pdbcol->Null = FALSE;
3161 :
3162 0 : pdbcol->VarLength = FALSE;
3163 :
3164 0 : if (colinfo->column_nullable
3165 0 : || is_nullable_type(colinfo->column_type))
3166 0 : pdbcol->VarLength = TRUE;
3167 :
3168 0 : pdbcol->Precision = colinfo->column_prec;
3169 0 : pdbcol->Scale = colinfo->column_scale;
3170 :
3171 0 : pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE ;
3172 0 : pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE ;
3173 :
3174 0 : return SUCCEED;
3175 : }
3176 :
3177 : return FAIL;
3178 : }
3179 :
3180 : /**
3181 : * \ingroup dblib_core
3182 : * \brief Get base database column name for a result set column.
3183 : *
3184 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3185 : * \param column Nth in the result set, starting from 1.
3186 : * \return pointer to ASCII null-terminated string, the name of the column. On error, NULL.
3187 : * \sa dbcolbrowse(), dbqual(), dbtabbrowse(), dbtabcount(), dbtabname(), dbtabsource(), dbtsnewlen(), dbtsnewval(), dbtsput().
3188 : */
3189 : char *
3190 0 : dbcolsource(DBPROCESS * dbproc, int column)
3191 : {
3192 : TDSCOLUMN *colinfo;
3193 :
3194 0 : tdsdump_log(TDS_DBG_FUNC, "dbcolsource(%p, %d)\n", dbproc, column);
3195 :
3196 0 : colinfo = dbcolptr(dbproc, column);
3197 0 : if (!colinfo)
3198 : return NULL;
3199 :
3200 0 : return tds_dstr_buf(tds_dstr_isempty(&colinfo->table_column_name) ?
3201 : &colinfo->column_name :
3202 : &colinfo->table_column_name);
3203 : }
3204 :
3205 : /**
3206 : * \ingroup dblib_core
3207 : * \brief Get size of a regular result column.
3208 : *
3209 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3210 : * \param column Nth in the result set, starting from 1.
3211 : * \return size of the column (not of data in any particular row). On error, -1.
3212 : * \sa dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
3213 : */
3214 : DBINT
3215 0 : dbcollen(DBPROCESS * dbproc, int column)
3216 : {
3217 : TDSCOLUMN *colinfo;
3218 :
3219 0 : tdsdump_log(TDS_DBG_FUNC, "dbcollen(%p, %d)\n", dbproc, column);
3220 :
3221 0 : colinfo = dbcolptr(dbproc, column);
3222 0 : if (!colinfo)
3223 : return -1;
3224 :
3225 0 : return colinfo->column_size;
3226 : }
3227 :
3228 : /**
3229 : * \ingroup dblib_core
3230 : * \brief Get size of a result column needed to print column.
3231 : *
3232 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3233 : * \param column Nth in the result set, starting from 1.
3234 : * \return size of the column in characters (not of data in any particular row). On error, -1.
3235 : * \sa dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
3236 : */
3237 : DBINT
3238 0 : dbprcollen(DBPROCESS * dbproc, int column)
3239 : {
3240 : TDSCOLUMN *colinfo;
3241 :
3242 0 : tdsdump_log(TDS_DBG_FUNC, "dbprcollen(%p, %d)\n", dbproc, column);
3243 :
3244 0 : colinfo = dbcolptr(dbproc, column);
3245 0 : if (!colinfo)
3246 : return 0;
3247 :
3248 0 : return _get_printable_size(colinfo);
3249 : }
3250 :
3251 :
3252 : /* dbvarylen(), pkleef@openlinksw.com 01/21/02 */
3253 : /**
3254 : * \ingroup dblib_core
3255 : * \brief Determine whether a column can vary in size.
3256 : *
3257 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3258 : * \param column Nth in the result set, starting from 1.
3259 : * \retval TRUE datatype of column can vary in size, or is nullable.
3260 : * \retval FALSE datatype of column is fixed and is not nullable.
3261 : * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols(), dbprtype().
3262 : */
3263 : DBINT
3264 1806 : dbvarylen(DBPROCESS * dbproc, int column)
3265 : {
3266 : TDSCOLUMN *colinfo;
3267 :
3268 1806 : tdsdump_log(TDS_DBG_FUNC, "dbvarylen(%p, %d)\n", dbproc, column);
3269 :
3270 1806 : colinfo = dbcolptr(dbproc, column);
3271 1806 : if (!colinfo)
3272 : return FALSE;
3273 :
3274 1806 : if (colinfo->column_nullable)
3275 : return TRUE;
3276 :
3277 1342 : switch (colinfo->column_type) {
3278 : /* variable length fields */
3279 : case SYBNVARCHAR:
3280 : case SYBVARBINARY:
3281 : case SYBVARCHAR:
3282 : case XSYBVARCHAR:
3283 : case XSYBNVARCHAR:
3284 : case XSYBVARBINARY:
3285 : return TRUE;
3286 :
3287 : /* types that can be null */
3288 : case SYBBITN:
3289 : case SYBDATETIMN:
3290 : case SYBDECIMAL:
3291 : case SYBFLTN:
3292 : case SYBINTN:
3293 : case SYBMONEYN:
3294 : case SYBNUMERIC:
3295 : case SYBDATEN:
3296 : case SYBTIMEN:
3297 : case SYBUINTN:
3298 : case SYB5BIGDATETIME:
3299 : case SYB5BIGTIME:
3300 : case SYBMSDATE:
3301 : case SYBMSTIME:
3302 : case SYBMSDATETIME2:
3303 : case SYBMSDATETIMEOFFSET:
3304 : case SYBVARIANT:
3305 : case SYBUNIQUE:
3306 : return TRUE;
3307 :
3308 : /* blob types */
3309 : case SYBIMAGE:
3310 : case SYBNTEXT:
3311 : case SYBTEXT:
3312 : case SYBLONGBINARY:
3313 : /* case SYBBLOB: */ /* same as SYBUNIQUE */
3314 : case SYB5INT8:
3315 : case SYBUNITEXT:
3316 : case SYBXML:
3317 : case SYBMSUDT:
3318 : case SYBMSXML:
3319 : return TRUE;
3320 : case SYBMSTABLE:
3321 : return TRUE;
3322 :
3323 0 : case SYBLONGCHAR:
3324 : /* case XSYBCHAR: */ /* same as SYBLONGCHAR */
3325 0 : if (colinfo->column_varint_size >= 4)
3326 : return TRUE;
3327 : break;
3328 :
3329 : /* with defined size*/
3330 : case SYBCHAR:
3331 : case SYBBINARY:
3332 : case XSYBNCHAR:
3333 : case XSYBBINARY:
3334 : break;
3335 :
3336 : /* fixed */
3337 : case SYBINT1:
3338 : case SYBINT2:
3339 : case SYBINT4:
3340 : case SYBFLT8:
3341 : case SYBDATETIME:
3342 : case SYBBIT:
3343 : case SYBMONEY4:
3344 : case SYBMONEY:
3345 : case SYBDATETIME4:
3346 : case SYBREAL:
3347 : case SYBINT8:
3348 : case SYBUINT1:
3349 : case SYBUINT2:
3350 : case SYBUINT4:
3351 : case SYBUINT8:
3352 : case SYBSINT1:
3353 : case SYBTIME:
3354 : case SYBDATE:
3355 : case SYBVOID:
3356 : case SYBINTERVAL:
3357 : break;
3358 : }
3359 738 : return FALSE;
3360 : }
3361 :
3362 : /**
3363 : * \ingroup dblib_core
3364 : * \brief Get size of current row's data in a regular result column.
3365 : *
3366 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3367 : * \param column Nth in the result set, starting from 1.
3368 : * \return size of the data, in bytes.
3369 : * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbnumcols().
3370 : */
3371 : DBINT
3372 2404 : dbdatlen(DBPROCESS * dbproc, int column)
3373 : {
3374 : DBINT len;
3375 : TDSCOLUMN *colinfo;
3376 :
3377 2404 : tdsdump_log(TDS_DBG_FUNC, "dbdatlen(%p, %d)\n", dbproc, column);
3378 :
3379 2404 : colinfo = dbcolptr(dbproc, column);
3380 2404 : if (!colinfo)
3381 : return -1;
3382 :
3383 2404 : len = TDS_MAX(colinfo->column_cur_size, 0);
3384 :
3385 2404 : tdsdump_log(TDS_DBG_FUNC, "dbdatlen() type = %d, len= %d\n", colinfo->column_type, len);
3386 :
3387 : return len;
3388 : }
3389 :
3390 : /**
3391 : * \ingroup dblib_core
3392 : * \brief Get address of data in a regular result column.
3393 : *
3394 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3395 : * \param column Nth in the result set, starting from 1.
3396 : * \return pointer the data, or NULL if data are NULL, or if \a column is out of range.
3397 : * \sa dbbind(), dbcollen(), dbcolname(), dbcoltype(), dbdatlen(), dbnumcols().
3398 : */
3399 : BYTE *
3400 3062 : dbdata(DBPROCESS * dbproc, int column)
3401 : {
3402 3062 : tdsdump_log(TDS_DBG_FUNC, "dbdata(%p, %d)\n", dbproc, column);
3403 :
3404 6124 : return _dbcoldata(dbcolptr(dbproc, column));
3405 : }
3406 :
3407 : /** \internal
3408 : * \ingroup dblib_internal
3409 : * \brief Return data from a column
3410 : *
3411 : * \param colinfo contains information on a result column.
3412 : * \return pointer to the data, or NULL if data are NULL
3413 : * \sa dbdata(), dbretdata()
3414 : */
3415 : static BYTE *
3416 : _dbcoldata(TDSCOLUMN *colinfo)
3417 : {
3418 : BYTE *res;
3419 : static const BYTE empty[1] = { 0 };
3420 :
3421 3132 : if (!colinfo || colinfo->column_cur_size < 0)
3422 : return NULL;
3423 :
3424 2588 : res = colinfo->column_data;
3425 2588 : if (is_blob_col(colinfo))
3426 274 : res = (BYTE *) ((TDSBLOB *) res)->textvalue;
3427 2588 : if (!res)
3428 : return (BYTE *) empty;
3429 : return res;
3430 : }
3431 :
3432 : /**
3433 : * \ingroup dblib_core
3434 : * \brief Cancel the current command batch.
3435 : *
3436 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3437 : * \retval SUCCEED always.
3438 : * \sa dbcanquery(), dbnextrow(), dbresults(), dbsetinterrupt(), dbsqlexec(), dbsqlok(), dbsqlsend().
3439 : * \todo Check for failure and return accordingly.
3440 : */
3441 : RETCODE
3442 10320 : dbcancel(DBPROCESS * dbproc)
3443 : {
3444 : TDSSOCKET *tds;
3445 :
3446 10320 : tdsdump_log(TDS_DBG_FUNC, "dbcancel(%p)\n", dbproc);
3447 10320 : CHECK_CONN(FAIL);
3448 :
3449 10320 : tds = dbproc->tds_socket;
3450 :
3451 10320 : tds_send_cancel(tds);
3452 10320 : tds_process_cancel(tds);
3453 :
3454 10320 : return SUCCEED;
3455 : }
3456 :
3457 : /**
3458 : * \ingroup dblib_core
3459 : * \brief Determine size buffer required to hold the results returned by dbsprhead(), dbsprline(), and dbspr1row().
3460 : *
3461 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3462 : * \return size of buffer requirement, in bytes.
3463 : * \remarks An esoteric function.
3464 : * \sa dbprhead(), dbprrow(), dbspr1row(), dbsprhead(), dbsprline().
3465 : */
3466 : DBINT
3467 0 : dbspr1rowlen(DBPROCESS * dbproc)
3468 : {
3469 : TDSSOCKET *tds;
3470 0 : int col, len = 0;
3471 :
3472 0 : tdsdump_log(TDS_DBG_FUNC, "dbspr1rowlen(%p)\n", dbproc);
3473 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
3474 0 : CHECK_PARAMETER(dbproc->tds_socket, SYBEDDNE, 0);
3475 :
3476 : tds = dbproc->tds_socket;
3477 :
3478 0 : for (col = 0; col < tds->res_info->num_cols; col++) {
3479 0 : TDSCOLUMN *colinfo = tds->res_info->columns[col];
3480 0 : int collen = _get_printable_size(colinfo);
3481 0 : int namlen = (int) tds_dstr_len(&colinfo->column_name);
3482 :
3483 0 : len += TDS_MAX(collen, namlen);
3484 :
3485 0 : if (col > 0) /* allow for the space between columns */
3486 0 : len += dbstring_length(dbproc->dbopts[DBPRCOLSEP].param);
3487 : }
3488 :
3489 0 : return ++len; /* allow for the nul */
3490 : }
3491 :
3492 : /**
3493 : * \ingroup dblib_core
3494 : * \brief Print a regular result row to a buffer.
3495 : *
3496 : * Fills a buffer with one data row, represented as a null-terminated ASCII string. Helpful for debugging.
3497 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3498 : * \param buffer \em output: Address of a buffer to hold ASCII null-terminated string.
3499 : * \param buf_len size of \a buffer, in bytes.
3500 : * \retval SUCCEED on success.
3501 : * \retval FAIL trouble encountered.
3502 : * \sa dbclropt(), dbisopt(), dbprhead(), dbprrow(), dbspr1rowlen(), dbsprhead(), dbsprline().
3503 : */
3504 : RETCODE
3505 0 : dbspr1row(DBPROCESS * dbproc, char *buffer, DBINT buf_len)
3506 : {
3507 : TDSSOCKET *tds;
3508 : TDSDATEREC when;
3509 : int i, c, col;
3510 : DBINT len;
3511 :
3512 0 : tdsdump_log(TDS_DBG_FUNC, "dbspr1row(%p, %s, %d)\n", dbproc, buffer, buf_len);
3513 0 : CHECK_CONN(FAIL);
3514 0 : CHECK_NULP(buffer, "dbspr1row", 2, FAIL);
3515 :
3516 : if (!dbproc->tds_socket)
3517 : return FAIL;
3518 :
3519 : tds = dbproc->tds_socket;
3520 :
3521 0 : for (col = 0; col < tds->res_info->num_cols; col++) {
3522 : size_t padlen, collen, namlen;
3523 0 : TDSCOLUMN *colinfo = tds->res_info->columns[col];
3524 0 : if (colinfo->column_cur_size < 0) {
3525 0 : len = 4;
3526 0 : if (buf_len <= len) {
3527 : return FAIL;
3528 : }
3529 0 : strcpy(buffer, "NULL");
3530 : } else {
3531 : TDS_SERVER_TYPE desttype, srctype;
3532 :
3533 0 : desttype = dblib_bound_type(STRINGBIND);
3534 0 : srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
3535 0 : if (is_datetime_type(srctype)) {
3536 0 : tds_datecrack(srctype, dbdata(dbproc, col + 1), &when);
3537 0 : len = (int)tds_strftime(buffer, buf_len, STD_DATETIME_FMT, &when, 3);
3538 : } else {
3539 0 : len = dbconvert(dbproc, srctype, dbdata(dbproc, col + 1), dbdatlen(dbproc, col + 1),
3540 : desttype, (BYTE *) buffer, buf_len);
3541 : }
3542 0 : if (len == -1) {
3543 : return FAIL;
3544 : }
3545 : }
3546 0 : buffer += len;
3547 0 : buf_len -= len;
3548 0 : collen = _get_printable_size(colinfo);
3549 0 : namlen = tds_dstr_len(&colinfo->column_name);
3550 0 : padlen = TDS_MAX(collen, namlen) - len;
3551 0 : if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
3552 0 : c = ' ';
3553 : }
3554 0 : for (; padlen > 0; padlen--) {
3555 0 : if (buf_len < 1) {
3556 : return FAIL;
3557 : }
3558 0 : *buffer++ = c;
3559 0 : buf_len--;
3560 : }
3561 0 : if ((col + 1) < tds->res_info->num_cols) {
3562 : i = 0;
3563 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
3564 0 : if (buf_len < 1) {
3565 : return FAIL;
3566 : }
3567 0 : *buffer++ = c;
3568 0 : buf_len--;
3569 0 : i++;
3570 : }
3571 : }
3572 : }
3573 0 : if (buf_len < 1) {
3574 : return FAIL;
3575 : }
3576 0 : *buffer = '\0';
3577 0 : return SUCCEED;
3578 : }
3579 :
3580 : /**
3581 : * \ingroup dblib_core
3582 : * \brief Print a result set to stdout.
3583 : *
3584 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3585 : * \sa dbbind(), dbnextrow(), dbprhead(), dbresults(), dbspr1row(), dbsprhead(), dbsprline().
3586 : */
3587 : RETCODE
3588 60 : dbprrow(DBPROCESS * dbproc)
3589 : {
3590 : TDSCOLUMN *colinfo;
3591 : TDSRESULTINFO *resinfo;
3592 : TDSSOCKET *tds;
3593 : int i, col;
3594 : size_t collen, namlen, len;
3595 : char dest[8192];
3596 : int desttype, srctype;
3597 : TDSDATEREC when;
3598 : STATUS status;
3599 : ptrdiff_t padlen;
3600 : int c;
3601 : int selcol;
3602 : int linechar;
3603 : int op;
3604 : const char *opname, *p;
3605 :
3606 : /* these are for compute rows */
3607 : DBINT computeid, num_cols, colid;
3608 60 : TDS_SMALLINT *col_printlens = NULL;
3609 :
3610 60 : tdsdump_log(TDS_DBG_FUNC, "dbprrow(%p)\n", dbproc);
3611 60 : CHECK_CONN(FAIL);
3612 :
3613 : tds = dbproc->tds_socket;
3614 :
3615 410 : while ((status = dbnextrow(dbproc)) != NO_MORE_ROWS) {
3616 :
3617 350 : if (status == FAIL) {
3618 0 : free(col_printlens);
3619 0 : return FAIL;
3620 : }
3621 :
3622 350 : if (status == REG_ROW) {
3623 :
3624 350 : resinfo = tds->res_info;
3625 :
3626 350 : if (col_printlens == NULL) {
3627 20 : if ((col_printlens = tds_new0(TDS_SMALLINT, resinfo->num_cols)) == NULL) {
3628 0 : dbperror(dbproc, SYBEMEM, errno);
3629 0 : return FAIL;
3630 : }
3631 : }
3632 :
3633 1750 : for (col = 0; col < resinfo->num_cols; col++) {
3634 1400 : colinfo = resinfo->columns[col];
3635 1400 : if (colinfo->column_cur_size < 0) {
3636 0 : len = 4;
3637 0 : strcpy(dest, "NULL");
3638 : } else {
3639 1400 : desttype = dblib_bound_type(STRINGBIND);
3640 1400 : srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
3641 1400 : if (is_datetime_type(srctype)) {
3642 0 : tds_datecrack(srctype, dbdata(dbproc, col + 1), &when);
3643 0 : len = (int)tds_strftime(dest, sizeof(dest), STD_DATETIME_FMT, &when, 3);
3644 : } else {
3645 1400 : len = dbconvert(dbproc, srctype, dbdata(dbproc, col + 1), dbdatlen(dbproc, col + 1),
3646 : desttype, (BYTE *) dest, sizeof(dest));
3647 : }
3648 : }
3649 :
3650 1400 : p = memchr(dest, '\0', len);
3651 1400 : fwrite(dest, 1, p == NULL ? len : (p - dest), stdout);
3652 1400 : collen = _get_printable_size(colinfo);
3653 2800 : namlen = tds_dstr_len(&colinfo->column_name);
3654 1400 : padlen = TDS_MAX(collen, namlen) - len;
3655 :
3656 2800 : c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0);
3657 10578 : for (; c > -1 && padlen > 0; padlen--) {
3658 9178 : putchar(c);
3659 : }
3660 :
3661 1400 : if ((col + 1) < resinfo->num_cols) {
3662 : i = 0;
3663 4200 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3664 : putchar(c);
3665 : }
3666 : }
3667 1400 : col_printlens[col] = collen;
3668 : }
3669 : i = 0;
3670 1400 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
3671 : putchar(c);
3672 : }
3673 :
3674 0 : } else if (col_printlens == NULL) {
3675 : return FAIL;
3676 : } else {
3677 :
3678 : computeid = status;
3679 :
3680 0 : for (i = 0;; ++i) {
3681 0 : if ((TDS_UINT) i >= tds->num_comp_info) {
3682 0 : free(col_printlens);
3683 0 : return FAIL;
3684 : }
3685 0 : resinfo = tds->comp_info[i];
3686 0 : if (resinfo->computeid == computeid)
3687 : break;
3688 : }
3689 :
3690 0 : num_cols = dbnumalts(dbproc, computeid);
3691 0 : tdsdump_log(TDS_DBG_FUNC, "dbprrow num compute cols = %d\n", num_cols);
3692 :
3693 : i = 0;
3694 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
3695 : putchar(c);
3696 : }
3697 0 : for (selcol = col = 1; col <= num_cols; col++) {
3698 0 : tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
3699 0 : colid = dbaltcolid(dbproc, computeid, col);
3700 : /*
3701 : * The pad character is pointed to by dbopts[DBPRPAD].param. If that pointer
3702 : * is NULL -- meaning padding is turned off -- dbstring_getchar returns -1.
3703 : */
3704 0 : while (selcol < colid) {
3705 0 : for (i = 0; i < col_printlens[selcol - 1]; i++) {
3706 0 : if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) >= 0)
3707 : putchar(c);
3708 : }
3709 0 : selcol++;
3710 0 : i = 0;
3711 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3712 : putchar(c);
3713 : }
3714 : }
3715 0 : op = dbaltop(dbproc, computeid, col);
3716 0 : opname = dbprtype(op);
3717 0 : printf("%s", opname);
3718 0 : for (i = 0; i < (col_printlens[selcol - 1] - (int) strlen(opname)); i++) {
3719 0 : if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) >= 0)
3720 : putchar(c);
3721 : }
3722 0 : selcol++;
3723 0 : if ((colid + 1) < num_cols) {
3724 : i = 0;
3725 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3726 : putchar(c);
3727 : }
3728 : }
3729 : }
3730 : i = 0;
3731 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
3732 : putchar(c);
3733 : }
3734 :
3735 0 : for (selcol = col = 1; col <= num_cols; col++) {
3736 0 : tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
3737 0 : colid = dbaltcolid(dbproc, computeid, col);
3738 0 : while (selcol < colid) {
3739 0 : for (i = 0; i < col_printlens[selcol - 1]; i++) {
3740 0 : putchar(' ');
3741 : }
3742 0 : selcol++;
3743 0 : i = 0;
3744 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3745 : putchar(c);
3746 : }
3747 : }
3748 0 : if (resinfo->by_cols > 0) {
3749 : linechar = '-';
3750 : } else {
3751 0 : linechar = '=';
3752 : }
3753 0 : for (i = 0; i < col_printlens[colid - 1]; i++)
3754 0 : putchar(linechar);
3755 0 : selcol++;
3756 0 : if ((colid + 1) < num_cols) {
3757 : i = 0;
3758 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3759 : putchar(c);
3760 : }
3761 : }
3762 : }
3763 : i = 0;
3764 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
3765 : putchar(c);
3766 : }
3767 :
3768 0 : for (selcol = col = 1; col <= num_cols; col++) {
3769 0 : colinfo = resinfo->columns[col - 1];
3770 :
3771 0 : desttype = dblib_bound_type(STRINGBIND);
3772 0 : srctype = dbalttype(dbproc, computeid, col);
3773 :
3774 0 : if (is_datetime_type(srctype)) {
3775 0 : tds_datecrack(srctype, dbadata(dbproc, computeid, col), &when);
3776 0 : len = (int)tds_strftime(dest, sizeof(dest), STD_DATETIME_FMT, &when, 3);
3777 : } else {
3778 0 : len = dbconvert(dbproc, srctype, dbadata(dbproc, computeid, col), -1, desttype,
3779 : (BYTE *) dest, sizeof(dest));
3780 : }
3781 :
3782 0 : tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
3783 0 : colid = dbaltcolid(dbproc, computeid, col);
3784 0 : tdsdump_log(TDS_DBG_FUNC, "dbprrow select column = %d\n", colid);
3785 :
3786 0 : while (selcol < colid) {
3787 0 : for (i = 0; i < col_printlens[selcol - 1]; i++) {
3788 0 : putchar(' ');
3789 : }
3790 0 : selcol++;
3791 0 : i = 0;
3792 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3793 : putchar(c);
3794 : }
3795 : }
3796 0 : p = memchr(dest, '\0', len);
3797 0 : fwrite(dest, 1, p == NULL ? len : (p - dest), stdout);
3798 0 : collen = _get_printable_size(colinfo);
3799 0 : namlen = tds_dstr_len(&colinfo->column_name);
3800 0 : padlen = TDS_MAX(collen, namlen) - len;
3801 0 : if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
3802 0 : c = ' ';
3803 : }
3804 0 : for (; padlen > 0; padlen--) {
3805 0 : putchar(c);
3806 : }
3807 0 : selcol++;
3808 0 : if ((colid + 1) < num_cols) {
3809 : i = 0;
3810 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
3811 : putchar(c);
3812 : }
3813 : }
3814 : }
3815 : i = 0;
3816 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
3817 : putchar(c);
3818 : }
3819 : }
3820 : }
3821 :
3822 60 : free(col_printlens);
3823 :
3824 60 : return SUCCEED;
3825 : }
3826 :
3827 : static int
3828 1560 : _get_printable_size(TDSCOLUMN * colinfo)
3829 : {
3830 1560 : switch (tds_get_conversion_type(colinfo->column_type, colinfo->column_size)) {
3831 : case SYBUINT1:
3832 : case SYBINT1:
3833 : return 3;
3834 0 : case SYBSINT1:
3835 0 : return 4;
3836 0 : case SYBUINT2:
3837 0 : return 5;
3838 522 : case SYBINT2:
3839 522 : return 6;
3840 0 : case SYBUINT4:
3841 0 : return 10;
3842 258 : case SYBINT4:
3843 258 : return 11;
3844 0 : case SYBUINT8:
3845 0 : return 20;
3846 0 : case SYBINT8:
3847 0 : return 21;
3848 780 : case SYBVARCHAR:
3849 : case SYBCHAR:
3850 : case SYBTEXT:
3851 : case SYBNTEXT:
3852 : case SYBUNITEXT:
3853 : case SYBNVARCHAR:
3854 : case SYBLONGCHAR:
3855 780 : return colinfo->column_size;
3856 0 : case SYBBINARY:
3857 : case SYBIMAGE:
3858 : case SYBLONGBINARY:
3859 : case SYBVARBINARY:
3860 0 : return colinfo->column_size * 2u;
3861 0 : case SYBREAL:
3862 0 : return 14; /* FIX ME -- we do not track precision */
3863 0 : case SYBFLT8:
3864 0 : return 24; /* FIX ME -- we do not track precision */
3865 0 : case SYBMONEY4:
3866 0 : return 12;
3867 0 : case SYBMONEY:
3868 0 : return 22;
3869 0 : case SYB5BIGDATETIME:
3870 : case SYBDATETIME:
3871 : case SYBDATETIME4:
3872 0 : return 26;
3873 0 : case SYBTIME:
3874 : case SYB5BIGTIME:
3875 0 : return 15;
3876 0 : case SYBMSTIME:
3877 0 : return 16;
3878 0 : case SYBDATE:
3879 : case SYBMSDATE:
3880 0 : return 10;
3881 0 : case SYBMSDATETIME2:
3882 0 : return 27;
3883 0 : case SYBMSDATETIMEOFFSET:
3884 0 : return 33;
3885 0 : case SYBUNIQUE:
3886 0 : return 36;
3887 0 : case SYBBIT:
3888 0 : return 1;
3889 0 : case SYBNUMERIC:
3890 : case SYBDECIMAL:
3891 0 : return colinfo->column_prec + 2;
3892 : /* FIX ME -- not all types present */
3893 0 : case SYBVOID: /* or 4 bytes for "null" ? */
3894 : default:
3895 0 : return 0;
3896 : }
3897 : }
3898 :
3899 : /**
3900 : * \ingroup dblib_core
3901 : * \brief Get formatted string for underlining dbsprhead() column names.
3902 : *
3903 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3904 : * \param buffer output buffer
3905 : * \param buf_len size of \a buffer
3906 : * \param line_char character to use to represent underlining.
3907 : * \retval SUCCEED \a buffer filled.
3908 : * \retval FAIL insufficient space in \a buffer, usually.
3909 : * \sa dbprhead(), dbprrow(), dbspr1row(), dbspr1rowlen(), dbsprhead().
3910 : */
3911 : RETCODE
3912 0 : dbsprline(DBPROCESS * dbproc, char *buffer, DBINT buf_len, DBCHAR line_char)
3913 : {
3914 : TDSCOLUMN *colinfo;
3915 : TDSRESULTINFO *resinfo;
3916 : TDSSOCKET *tds;
3917 : size_t i, col, len, collen, namlen;
3918 : int c;
3919 :
3920 0 : tdsdump_log(TDS_DBG_FUNC, "dbsprline(%p, %s, %d, '%c')\n", dbproc, buffer, buf_len, line_char);
3921 0 : CHECK_CONN(FAIL);
3922 0 : CHECK_NULP(buffer, "dbsprline", 2, FAIL);
3923 :
3924 0 : tds = dbproc->tds_socket;
3925 0 : resinfo = tds->res_info;
3926 :
3927 0 : for (col = 0; col < resinfo->num_cols; col++) {
3928 0 : colinfo = resinfo->columns[col];
3929 0 : collen = _get_printable_size(colinfo);
3930 0 : namlen = tds_dstr_len(&colinfo->column_name);
3931 0 : len = TDS_MAX(collen, namlen);
3932 0 : for (i = 0; i < len; i++) {
3933 0 : if (buf_len < 1) {
3934 : return FAIL;
3935 : }
3936 0 : *buffer++ = line_char;
3937 0 : buf_len--;
3938 : }
3939 0 : if ((col + 1) < resinfo->num_cols) {
3940 : i = 0;
3941 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
3942 0 : if (buf_len < 1) {
3943 : return FAIL;
3944 : }
3945 0 : *buffer++ = c;
3946 0 : buf_len--;
3947 0 : i++;
3948 : }
3949 : }
3950 : }
3951 0 : if (buf_len < 1) {
3952 : return FAIL;
3953 : }
3954 0 : *buffer = '\0';
3955 0 : return SUCCEED;
3956 : }
3957 :
3958 : /**
3959 : * \ingroup dblib_core
3960 : * \brief Print result set headings to a buffer.
3961 : *
3962 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
3963 : * \param buffer output buffer
3964 : * \param buf_len size of \a buffer
3965 : * \retval SUCCEED \a buffer filled.
3966 : * \retval FAIL insufficient space in \a buffer, usually.
3967 : * \sa dbprhead(), dbprrow(), dbsetopt(), dbspr1row(), dbspr1rowlen(), dbsprline().
3968 : */
3969 : RETCODE
3970 0 : dbsprhead(DBPROCESS * dbproc, char *buffer, DBINT buf_len)
3971 : {
3972 : TDSCOLUMN *colinfo;
3973 : TDSRESULTINFO *resinfo;
3974 : TDSSOCKET *tds;
3975 : int i, collen, namlen;
3976 : TDS_USMALLINT col;
3977 : int padlen;
3978 : int c;
3979 :
3980 0 : tdsdump_log(TDS_DBG_FUNC, "dbsprhead(%p, %p, %d)\n", dbproc, buffer, buf_len);
3981 0 : CHECK_CONN(FAIL);
3982 0 : CHECK_NULP(buffer, "dbsprhead", 2, FAIL);
3983 :
3984 0 : tds = dbproc->tds_socket;
3985 0 : resinfo = tds->res_info;
3986 :
3987 0 : for (col = 0; col < resinfo->num_cols; col++) {
3988 0 : colinfo = resinfo->columns[col];
3989 0 : collen = _get_printable_size(colinfo);
3990 0 : namlen = (int) tds_dstr_len(&colinfo->column_name);
3991 0 : padlen = TDS_MAX(collen, namlen) - namlen;
3992 0 : if (buf_len < namlen) {
3993 : return FAIL;
3994 : }
3995 0 : memcpy(buffer, tds_dstr_cstr(&colinfo->column_name), namlen);
3996 0 : buffer += namlen;
3997 0 : buf_len -= namlen;
3998 0 : if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
3999 0 : c = ' ';
4000 : }
4001 0 : for (; padlen > 0; padlen--) {
4002 0 : if (buf_len < 1) {
4003 : return FAIL;
4004 : }
4005 0 : *buffer++ = c;
4006 0 : buf_len--;
4007 : }
4008 0 : if ((col + 1) < resinfo->num_cols) {
4009 : i = 0;
4010 0 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
4011 0 : if (buf_len < 1) {
4012 : return FAIL;
4013 : }
4014 0 : *buffer++ = c;
4015 0 : buf_len--;
4016 0 : i++;
4017 : }
4018 : }
4019 : }
4020 0 : if (buf_len < 1) {
4021 : return FAIL;
4022 : }
4023 0 : *buffer = '\0';
4024 0 : return SUCCEED;
4025 : }
4026 :
4027 : /**
4028 : * \ingroup dblib_core
4029 : * \brief Print result set headings to stdout.
4030 : *
4031 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4032 : * \sa
4033 : */
4034 : void
4035 60 : dbprhead(DBPROCESS * dbproc)
4036 : {
4037 : TDSCOLUMN *colinfo;
4038 : TDSRESULTINFO *resinfo;
4039 : TDSSOCKET *tds;
4040 : size_t i, col, len, collen, namlen;
4041 : size_t padlen;
4042 : int c;
4043 :
4044 60 : tdsdump_log(TDS_DBG_FUNC, "dbprhead(%p)\n", dbproc);
4045 60 : CHECK_PARAMETER(dbproc, SYBENULL, );
4046 :
4047 60 : tds = dbproc->tds_socket;
4048 60 : resinfo = tds->res_info;
4049 60 : if (resinfo == NULL) {
4050 : return;
4051 : }
4052 100 : for (col = 0; col < resinfo->num_cols; col++) {
4053 80 : colinfo = resinfo->columns[col];
4054 80 : collen = _get_printable_size(colinfo);
4055 160 : namlen = tds_dstr_len(&colinfo->column_name);
4056 80 : padlen = TDS_MAX(collen, namlen) - namlen;
4057 160 : printf("%s", tds_dstr_cstr(&colinfo->column_name));
4058 :
4059 160 : c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0);
4060 80 : if (c == -1) {
4061 0 : c = ' ';
4062 : }
4063 600 : for (; padlen > 0; padlen--) {
4064 600 : putchar(c);
4065 : }
4066 :
4067 80 : if ((col + 1) < resinfo->num_cols) {
4068 : i = 0;
4069 240 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
4070 60 : putchar(c);
4071 60 : i++;
4072 : }
4073 : }
4074 : }
4075 : i = 0;
4076 80 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i)) != -1) {
4077 20 : putchar(c);
4078 20 : i++;
4079 : }
4080 100 : for (col = 0; col < resinfo->num_cols; col++) {
4081 80 : colinfo = resinfo->columns[col];
4082 80 : collen = _get_printable_size(colinfo);
4083 160 : namlen = tds_dstr_len(&colinfo->column_name);
4084 80 : len = TDS_MAX(collen, namlen);
4085 1040 : for (i = 0; i < len; i++)
4086 960 : putchar('-');
4087 80 : if ((col + 1) < resinfo->num_cols) {
4088 : i = 0;
4089 240 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
4090 60 : putchar(c);
4091 60 : i++;
4092 : }
4093 : }
4094 : }
4095 : i = 0;
4096 80 : while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i)) != -1) {
4097 20 : putchar(c);
4098 20 : i++;
4099 : }
4100 : }
4101 :
4102 : /** \internal
4103 : * \ingroup dblib_internal
4104 : * \brief Indicate whether a query returned rows.
4105 : *
4106 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4107 : * \sa DBROWS(), DBCMDROW(), dbnextrow(), dbresults(), DBROWTYPE().
4108 : */
4109 : RETCODE
4110 522 : dbrows(DBPROCESS * dbproc)
4111 : {
4112 : TDSSOCKET *tds;
4113 :
4114 522 : tdsdump_log(TDS_DBG_FUNC, "dbrows(%p)\n", dbproc);
4115 522 : CHECK_CONN(FAIL);
4116 :
4117 522 : if (!(tds=dbproc->tds_socket))
4118 : return FAIL;
4119 :
4120 522 : return (tds->res_info && tds->res_info->rows_exist)? SUCCEED : FAIL;
4121 : }
4122 :
4123 : #if defined(DBLIB_UNIMPLEMENTED)
4124 : /**
4125 : * \ingroup dblib_core
4126 : * \brief Set the default character set for an application.
4127 : *
4128 : * \param language ASCII null-terminated string.
4129 : * \sa dbsetdeflang(), dbsetdefcharset(), dblogin(), dbopen().
4130 : * \retval SUCCEED Always.
4131 : * \todo Unimplemented.
4132 : */
4133 : RETCODE
4134 : dbsetdeflang(char *language)
4135 : {
4136 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetdeflang(%s)\n", language);
4137 : CHECK_PARAMETER_NOPROC(language, SYBENULP);
4138 : return SUCCEED;
4139 : }
4140 : #endif
4141 :
4142 : /**
4143 : * \ingroup dblib_core
4144 : * \brief Get TDS packet size for the connection.
4145 : *
4146 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4147 : * \return TDS packet size, in bytes.
4148 : * \sa DBSETLPACKET()
4149 : */
4150 : int
4151 0 : dbgetpacket(DBPROCESS * dbproc)
4152 : {
4153 : TDSSOCKET *tds;
4154 :
4155 0 : tdsdump_log(TDS_DBG_FUNC, "dbgetpacket(%p)\n", dbproc);
4156 0 : CHECK_PARAMETER(dbproc, SYBENULL, TDS_DEF_BLKSZ);
4157 :
4158 0 : tds = dbproc->tds_socket;
4159 0 : if (!tds) {
4160 : return TDS_DEF_BLKSZ;
4161 : } else {
4162 0 : return tds->conn->env.block_size;
4163 : }
4164 : }
4165 :
4166 : /**
4167 : * \ingroup dblib_core
4168 : * \brief Set maximum simultaneous connections db-lib will open to the server.
4169 : *
4170 : * \param maxprocs Limit for process.
4171 : * \retval SUCCEED Always.
4172 : * \sa dbgetmaxprocs(), dbopen()
4173 : */
4174 : RETCODE
4175 250 : dbsetmaxprocs(int maxprocs)
4176 : {
4177 : int i, j;
4178 : TDSSOCKET **old_list;
4179 :
4180 250 : tdsdump_log(TDS_DBG_FUNC, "UNTESTED dbsetmaxprocs(%d)\n", maxprocs);
4181 :
4182 : /* not too few elements */
4183 250 : if (maxprocs <= 0)
4184 : return FAIL;
4185 :
4186 250 : tds_mutex_lock(&dblib_mutex);
4187 :
4188 250 : old_list = g_dblib_ctx.connection_list;
4189 :
4190 : /* "compress" array */
4191 1024250 : for (i = 0, j = 0; i < g_dblib_ctx.connection_list_size; ++i) {
4192 1024000 : if (!old_list[i])
4193 1024000 : continue;
4194 0 : if (i != j) {
4195 0 : old_list[j] = old_list[i];
4196 0 : old_list[i] = NULL;
4197 : }
4198 0 : ++j;
4199 : }
4200 : /* do not restrict too much, j here contains minimun size */
4201 250 : if (maxprocs < j)
4202 0 : maxprocs = j;
4203 :
4204 : /*
4205 : * Don't reallocate less memory.
4206 : * If maxprocs is less than was initially allocated, just reduce the represented list size.
4207 : * If larger, reallocate and copy.
4208 : * We probably should check for valid connections beyond the new max.
4209 : */
4210 250 : if (maxprocs <= g_dblib_ctx.connection_list_size) {
4211 250 : g_dblib_ctx.connection_list_size_represented = maxprocs;
4212 250 : tds_mutex_unlock(&dblib_mutex);
4213 250 : return SUCCEED;
4214 : }
4215 :
4216 0 : g_dblib_ctx.connection_list = tds_new0(TDSSOCKET *, maxprocs);
4217 :
4218 0 : if (g_dblib_ctx.connection_list == NULL) {
4219 0 : g_dblib_ctx.connection_list = old_list;
4220 0 : tds_mutex_unlock(&dblib_mutex);
4221 0 : dbperror(NULL, SYBEMEM, errno);
4222 0 : return FAIL;
4223 : }
4224 :
4225 0 : for (i = 0; i < g_dblib_ctx.connection_list_size; i++) {
4226 0 : g_dblib_ctx.connection_list[i] = old_list[i];
4227 : }
4228 :
4229 0 : g_dblib_ctx.connection_list_size = maxprocs;
4230 0 : g_dblib_ctx.connection_list_size_represented = maxprocs;
4231 :
4232 0 : tds_mutex_unlock(&dblib_mutex);
4233 :
4234 0 : free(old_list);
4235 :
4236 0 : return SUCCEED;
4237 : }
4238 :
4239 : /**
4240 : * \ingroup dblib_core
4241 : * \brief get maximum simultaneous connections db-lib will open to the server.
4242 : *
4243 : * \return Current maximum.
4244 : * \sa dbsetmaxprocs(), dbopen()
4245 : */
4246 : int
4247 0 : dbgetmaxprocs(void)
4248 : {
4249 : int r;
4250 :
4251 0 : tdsdump_log(TDS_DBG_FUNC, "dbgetmaxprocs(void)\n");
4252 :
4253 0 : tds_mutex_lock(&dblib_mutex);
4254 0 : r = g_dblib_ctx.connection_list_size_represented;
4255 0 : tds_mutex_unlock(&dblib_mutex);
4256 0 : return r;
4257 : }
4258 :
4259 : /**
4260 : * \ingroup dblib_core
4261 : * \brief Set maximum seconds db-lib waits for a server response to query.
4262 : *
4263 : * \param seconds New limit for application.
4264 : * \retval SUCCEED Always.
4265 : * \sa dberrhandle(), DBGETTIME(), dbsetlogintime(), dbsqlexec(), dbsqlok(), dbsqlsend().
4266 : */
4267 : RETCODE
4268 70 : dbsettime(int seconds)
4269 : {
4270 : TDSSOCKET **tds;
4271 : int i;
4272 : DBPROCESS *dbproc;
4273 70 : tdsdump_log(TDS_DBG_FUNC, "dbsettime(%d)\n", seconds);
4274 :
4275 70 : tds_mutex_lock(&dblib_mutex);
4276 70 : g_dblib_ctx.query_timeout = seconds;
4277 :
4278 70 : tds = g_dblib_ctx.connection_list;
4279 286790 : for (i = 0; i < TDS_MAX_CONN; i++) {
4280 286720 : if (tds[i]) {
4281 100 : dbproc = (DBPROCESS *) tds_get_parent(tds[i]);
4282 100 : if (!dbisopt(dbproc, DBSETTIME, 0))
4283 80 : tds[i]->query_timeout = seconds;
4284 : }
4285 : }
4286 :
4287 70 : tds_mutex_unlock(&dblib_mutex);
4288 70 : return SUCCEED;
4289 : }
4290 :
4291 : /**
4292 : * \ingroup dblib_core
4293 : * \brief Get maximum seconds db-lib waits for a server response to query.
4294 : *
4295 : * \retval query timeout limit, in seconds
4296 : * \sa dberrhandle(), DBSETTIME(), dbsetlogintime(), dbsqlexec(), dbsqlok(), dbsqlsend().
4297 : */
4298 : int
4299 0 : dbgettime(void)
4300 : {
4301 0 : tdsdump_log(TDS_DBG_FUNC, "dbgettime()\n");
4302 :
4303 0 : return g_dblib_ctx.query_timeout;
4304 : }
4305 :
4306 : /**
4307 : * \ingroup dblib_core
4308 : * \brief Set maximum seconds db-lib waits for a server response to a login attempt.
4309 : *
4310 : * \param seconds New limit for application.
4311 : * \retval SUCCEED Always.
4312 : * \sa dberrhandle(), dbsettime()
4313 : */
4314 : RETCODE
4315 20 : dbsetlogintime(int seconds)
4316 : {
4317 20 : tdsdump_log(TDS_DBG_FUNC, "dbsetlogintime(%d)\n", seconds);
4318 :
4319 20 : tds_mutex_lock(&dblib_mutex);
4320 20 : g_dblib_ctx.login_timeout = seconds;
4321 20 : tds_mutex_unlock(&dblib_mutex);
4322 20 : return SUCCEED;
4323 : }
4324 :
4325 : /** \internal
4326 : * \ingroup dblib_internal
4327 : * \brief See if the current command can return rows.
4328 : *
4329 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4330 : * \retval SUCCEED Yes, it can.
4331 : * \retval FAIL No, it can't.
4332 : * \remarks Use DBCMDROW() macro instead.
4333 : * \sa DBCMDROW(), dbnextrow(), dbresults(), DBROWS(), DBROWTYPE().
4334 : */
4335 : RETCODE
4336 0 : dbcmdrow(DBPROCESS * dbproc)
4337 : {
4338 : TDSSOCKET *tds;
4339 :
4340 0 : tdsdump_log(TDS_DBG_FUNC, "dbcmdrow(%p)\n", dbproc);
4341 0 : CHECK_CONN(FAIL);
4342 :
4343 0 : tds = dbproc->tds_socket;
4344 0 : if (tds->res_info)
4345 : return SUCCEED;
4346 0 : return FAIL;
4347 : }
4348 :
4349 : /**
4350 : * \ingroup dblib_core
4351 : * \brief Get column ID of a compute column.
4352 : *
4353 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4354 : * \param computeid of \c COMPUTE clause to which we're referring.
4355 : * \param column Nth column in \a computeid, starting from 1.
4356 : * \return Nth column in the base result set, on which \a column was computed.
4357 : * \sa dbadata(), dbadlen(), dbaltlen(), dbgetrow(), dbnextrow(), dbnumalts(), dbprtype().
4358 : */
4359 : int
4360 16 : dbaltcolid(DBPROCESS * dbproc, int computeid, int column)
4361 : {
4362 : TDSCOLUMN *curcol;
4363 :
4364 16 : tdsdump_log(TDS_DBG_FUNC, "dbaltcolid(%p, %d, %d)\n", dbproc, computeid, column);
4365 :
4366 16 : curcol = dbacolptr(dbproc, computeid, column, false);
4367 16 : if (!curcol)
4368 : return -1;
4369 :
4370 16 : return curcol->column_operand;
4371 : }
4372 :
4373 : /**
4374 : * \ingroup dblib_core
4375 : * \brief Get size of data in a compute column.
4376 : *
4377 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4378 : * \param computeid of \c COMPUTE clause to which we're referring.
4379 : * \param column Nth column in \a computeid, starting from 1.
4380 : * \return size of the data, in bytes.
4381 : * \retval -1 no such \a column or \a computeid.
4382 : * \retval 0 data are NULL.
4383 : * \sa dbadata(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
4384 : */
4385 : DBINT
4386 0 : dbadlen(DBPROCESS * dbproc, int computeid, int column)
4387 : {
4388 : TDSCOLUMN *colinfo;
4389 : DBINT len;
4390 :
4391 0 : tdsdump_log(TDS_DBG_FUNC, "dbadlen(%p, %d, %d)\n", dbproc, computeid, column);
4392 :
4393 0 : colinfo = dbacolptr(dbproc, computeid, column, false);
4394 0 : if (!colinfo)
4395 : return -1;
4396 :
4397 0 : len = TDS_MAX(colinfo->column_cur_size, 0);
4398 :
4399 0 : tdsdump_log(TDS_DBG_FUNC, "leaving dbadlen() type = %d, returning %d\n", colinfo->column_type, len);
4400 :
4401 : return len;
4402 : }
4403 :
4404 : /**
4405 : * \ingroup dblib_core
4406 : * \brief Get datatype for a compute column.
4407 : *
4408 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4409 : * \param computeid of \c COMPUTE clause to which we're referring.
4410 : * \param column Nth column in \a computeid, starting from 1.
4411 : * \return \c SYB* dataype token.
4412 : * \retval -1 no such \a column or \a computeid.
4413 : * \sa dbadata(), dbadlen(), dbaltlen(), dbnextrow(), dbnumalts(), dbprtype().
4414 : */
4415 : int
4416 16 : dbalttype(DBPROCESS * dbproc, int computeid, int column)
4417 : {
4418 : TDSCOLUMN *colinfo;
4419 :
4420 16 : tdsdump_log(TDS_DBG_FUNC, "dbalttype(%p, %d, %d)\n", dbproc, computeid, column);
4421 :
4422 16 : colinfo = dbacolptr(dbproc, computeid, column, false);
4423 16 : if (!colinfo)
4424 : return -1;
4425 :
4426 16 : return dblib_coltype(colinfo);
4427 : }
4428 :
4429 : /**
4430 : * \ingroup dblib_core
4431 : * \brief Bind a compute column to a program variable.
4432 : *
4433 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4434 : * \param computeid of \c COMPUTE clause to which we're referring.
4435 : * \param column Nth column in \a computeid, starting from 1.
4436 : * \param vartype datatype of the host variable that will receive the data
4437 : * \param varlen size of host variable pointed to \a varaddr
4438 : * \param varaddr address of host variable
4439 : * \retval SUCCEED everything worked.
4440 : * \retval FAIL no such \a computeid or \a column, or no such conversion possible, or target buffer too small.
4441 : * \sa dbadata(), dbaltbind_ps(), dbanullbind(), dbbind(), dbbind_ps(), dbconvert(),
4442 : * dbconvert_ps(), dbnullbind(), dbsetnull(), dbsetversion(), dbwillconvert().
4443 : */
4444 : RETCODE
4445 16 : dbaltbind(DBPROCESS * dbproc, int computeid, int column, int vartype, DBINT varlen, BYTE * varaddr)
4446 : {
4447 : TDS_SERVER_TYPE srctype, desttype;
4448 16 : TDSCOLUMN *colinfo = NULL;
4449 :
4450 16 : tdsdump_log(TDS_DBG_FUNC, "dbaltbind(%p, %d, %d, %d, %d, %p)\n", dbproc, computeid, column, vartype, varlen, varaddr);
4451 :
4452 16 : colinfo = dbacolptr(dbproc, computeid, column, true);
4453 16 : if (!colinfo)
4454 : return FAIL;
4455 16 : CHECK_PARAMETER(varaddr, SYBEABNV, FAIL);
4456 :
4457 16 : dbproc->avail_flag = FALSE;
4458 :
4459 16 : srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
4460 16 : desttype = dblib_bound_type(vartype);
4461 16 : if (desttype == TDS_INVALID_TYPE) {
4462 0 : dbperror(dbproc, SYBEBTYP, 0);
4463 0 : return FAIL;
4464 : }
4465 :
4466 16 : if (!dbwillconvert(srctype, desttype)) {
4467 0 : dbperror(dbproc, SYBEAAMT, 0);
4468 0 : return FAIL;
4469 : }
4470 :
4471 16 : colinfo->column_varaddr = (char *) varaddr;
4472 16 : colinfo->column_bindtype = vartype;
4473 16 : colinfo->column_bindlen = varlen;
4474 :
4475 16 : return SUCCEED;
4476 : }
4477 :
4478 :
4479 : /**
4480 : * \ingroup dblib_core
4481 : * \brief Get address of compute column data.
4482 : *
4483 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4484 : * \param computeid of \c COMPUTE clause to which we're referring.
4485 : * \param column Nth column in \a computeid, starting from 1.
4486 : * \return pointer to columns's data buffer.
4487 : * \retval NULL no such \a computeid or \a column.
4488 : * \sa dbadlen(), dbaltbind(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
4489 : */
4490 : BYTE *
4491 0 : dbadata(DBPROCESS * dbproc, int computeid, int column)
4492 : {
4493 : TDSCOLUMN *colinfo;
4494 :
4495 0 : tdsdump_log(TDS_DBG_FUNC, "dbadata(%p, %d, %d)\n", dbproc, computeid, column);
4496 :
4497 0 : colinfo = dbacolptr(dbproc, computeid, column, false);
4498 0 : if (!colinfo)
4499 : return NULL;
4500 :
4501 0 : if (is_blob_col(colinfo)) {
4502 0 : return (BYTE *) ((TDSBLOB *) colinfo->column_data)->textvalue;
4503 : }
4504 :
4505 0 : return (BYTE *) colinfo->column_data;
4506 : }
4507 :
4508 : /**
4509 : * \ingroup dblib_core
4510 : * \brief Get aggregation operator for a compute column.
4511 : *
4512 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4513 : * \param computeid of \c COMPUTE clause to which we're referring.
4514 : * \param column Nth column in \a computeid, starting from 1.
4515 : * \return token value for the type of the compute column's aggregation operator.
4516 : * \retval -1 no such \a computeid or \a column.
4517 : * \sa dbadata(), dbadlen(), dbaltlen(), dbnextrow(), dbnumalts(), dbprtype().
4518 : */
4519 : int
4520 16 : dbaltop(DBPROCESS * dbproc, int computeid, int column)
4521 : {
4522 : TDSCOLUMN *curcol;
4523 :
4524 16 : tdsdump_log(TDS_DBG_FUNC, "dbaltop(%p, %d, %d)\n", dbproc, computeid, column);
4525 :
4526 16 : if ((curcol=dbacolptr(dbproc, computeid, column, false)) == NULL)
4527 : return -1;
4528 :
4529 16 : return curcol->column_operator;
4530 : }
4531 :
4532 : /**
4533 : * \ingroup dblib_core
4534 : * \brief Set db-lib or server option.
4535 : *
4536 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4537 : * \param option option to set.
4538 : * \param char_param value to set \a option to, if it wants a null-teminated ASCII string.
4539 : * \param int_param value to set \a option to, if it wants an integer value.
4540 : * \retval SUCCEED everything worked.
4541 : * \retval FAIL no such \a option, or insufficient memory, or unimplemented.
4542 : * \remarks Many are unimplemented.
4543 : * \sa dbclropt(), dbisopt().
4544 : * \todo Implement more options.
4545 : */
4546 : RETCODE
4547 90 : dbsetopt(DBPROCESS * dbproc, int option, const char *char_param, int int_param)
4548 : {
4549 : char *cmd;
4550 : RETCODE rc;
4551 : int i;
4552 :
4553 90 : tdsdump_log(TDS_DBG_FUNC, "dbsetopt(%p, %d, %s, %d)\n", dbproc, option, char_param, int_param);
4554 90 : CHECK_CONN(FAIL);
4555 90 : CHECK_NULP(char_param, "dbsetopt", 3, FAIL);
4556 :
4557 90 : if ((option < 0) || (option >= DBNUMOPTIONS)) {
4558 0 : dbperror(dbproc, SYBEUNOP, 0);
4559 0 : return FAIL;
4560 : }
4561 :
4562 90 : rc = FAIL;
4563 90 : switch (option) {
4564 0 : case DBARITHABORT:
4565 : case DBARITHIGNORE:
4566 : case DBCHAINXACTS:
4567 : case DBFIPSFLAG:
4568 : case DBISOLATION:
4569 : case DBNOCOUNT:
4570 : case DBNOEXEC:
4571 : case DBPARSEONLY:
4572 : case DBSHOWPLAN:
4573 : case DBSTORPROCID:
4574 : case DBQUOTEDIDENT:
4575 : /* server options (on/off) */
4576 0 : if (asprintf(&cmd, "set %s on\n", dbproc->dbopts[option].text) < 0) {
4577 : return FAIL;
4578 : }
4579 0 : rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
4580 0 : free(cmd);
4581 0 : break;
4582 0 : case DBNATLANG:
4583 : case DBDATEFIRST:
4584 : case DBDATEFORMAT:
4585 : /* server options (char_param) */
4586 0 : if (asprintf(&cmd, "set %s %s\n", dbproc->dbopts[option].text, char_param) < 0) {
4587 : return FAIL;
4588 : }
4589 0 : rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
4590 0 : free(cmd);
4591 0 : break;
4592 : case DBOFFSET:
4593 : /* server option */
4594 : /* requires param
4595 : * "select", "from", "table", "order", "compute",
4596 : * "statement", "procedure", "execute", or "param"
4597 : */
4598 : rc = SUCCEED;
4599 : break;
4600 : case DBROWCOUNT:
4601 : /* server option */
4602 : /* requires param "0" to "2147483647" */
4603 : rc = SUCCEED;
4604 : break;
4605 : case DBSTAT:
4606 : /* server option */
4607 : /* requires param "io" or "time" */
4608 : rc = SUCCEED;
4609 : break;
4610 : case DBTEXTLIMIT:
4611 : /* dblib option */
4612 : /* requires param "0" to "2147483647" */
4613 : /* dblib do not return more than this length from text/image */
4614 : /* TODO required for PHP */
4615 : rc = SUCCEED;
4616 : break;
4617 : case DBTEXTSIZE:
4618 : /* server option */
4619 : /* requires param "0" to "2147483647" */
4620 : /* limit text/image from network */
4621 : if (!char_param)
4622 : char_param = "0";
4623 0 : i = atoi(char_param);
4624 0 : if (i < 0 || i > 2147483647)
4625 : return FAIL;
4626 0 : if (asprintf(&cmd, "set textsize %d\n", i) < 0)
4627 : return FAIL;
4628 0 : rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
4629 0 : free(cmd);
4630 0 : break;
4631 : case DBAUTH:
4632 : /* ??? */
4633 : rc = SUCCEED;
4634 : break;
4635 : case DBNOAUTOFREE:
4636 : /* dblib option */
4637 : rc = SUCCEED;
4638 : break;
4639 : case DBBUFFER:
4640 : /*
4641 : * Requires param "2" to "2147483647"
4642 : * (0 or 1 is an error, < 0 yields the default 100)
4643 : */
4644 : {
4645 : int nrows;
4646 :
4647 : /* 100 is the default, according to Microsoft */
4648 : if( !char_param )
4649 : char_param = "100";
4650 :
4651 60 : nrows = atoi(char_param);
4652 :
4653 60 : nrows = (nrows < 0 )? 100 : nrows;
4654 :
4655 60 : if( 1 < nrows && nrows <= 2147483647 ) {
4656 60 : buffer_set_capacity(dbproc, nrows);
4657 60 : rc = SUCCEED;
4658 : }
4659 : }
4660 : break;
4661 0 : case DBPRCOLSEP:
4662 : case DBPRLINELEN:
4663 : case DBPRLINESEP:
4664 0 : rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param);
4665 0 : break;
4666 0 : case DBPRPAD:
4667 : /*
4668 : * "If the character is not specified, the ASCII space character is used."
4669 : * A NULL pointer to the pad character signifies that padding is turned off.
4670 : */
4671 0 : if (int_param) {
4672 0 : rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param? char_param : " ");
4673 : } else {
4674 0 : rc = dbstring_assign(&(dbproc->dbopts[option].param), NULL);
4675 : }
4676 : break;
4677 : case DBSETTIME:
4678 : if (char_param) {
4679 30 : i = atoi(char_param);
4680 30 : if (0 < i) {
4681 60 : rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param);
4682 30 : if (rc == SUCCEED) {
4683 30 : dbproc->tds_socket->query_timeout = i;
4684 : }
4685 : }
4686 : }
4687 : break;
4688 0 : default:
4689 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetopt(option = %d)\n", option);
4690 : return FAIL;
4691 : }
4692 30 : if (rc == SUCCEED)
4693 90 : dbproc->dbopts[option].factive = 1;
4694 : return rc;
4695 : }
4696 :
4697 : /**
4698 : * \ingroup dblib_core
4699 : * \brief Set interrupt handler for db-lib to use while blocked against a read from the server.
4700 : *
4701 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4702 : * \param chkintr
4703 : * \param hndlintr
4704 : * \sa dbcancel(), dbgetuserdata(), dbsetuserdata(), dbsetbusy(), dbsetidle().
4705 : */
4706 : void
4707 20 : dbsetinterrupt(DBPROCESS * dbproc, DB_DBCHKINTR_FUNC chkintr, DB_DBHNDLINTR_FUNC hndlintr)
4708 : {
4709 20 : tdsdump_log(TDS_DBG_FUNC, "dbsetinterrupt(%p, %p, %p)\n", dbproc, chkintr, hndlintr);
4710 20 : CHECK_PARAMETER(dbproc, SYBENULL, );
4711 :
4712 20 : dbproc->chkintr = chkintr;
4713 20 : dbproc->hndlintr = hndlintr;
4714 : }
4715 :
4716 : /**
4717 : * \ingroup dblib_rpc
4718 : * \brief Determine if query generated a return status number.
4719 : *
4720 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4721 : * \retval TRUE fetch return status with dbretstatus().
4722 : * \retval FALSE no return status.
4723 : * \sa dbnextrow(), dbresults(), dbretdata(), dbretstatus(), dbrpcinit(), dbrpcparam(), dbrpcsend().
4724 : */
4725 : DBBOOL
4726 62 : dbhasretstat(DBPROCESS * dbproc)
4727 : {
4728 : TDSSOCKET *tds;
4729 :
4730 62 : tdsdump_log(TDS_DBG_FUNC, "dbhasretstat(%p)\n", dbproc);
4731 62 : CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
4732 :
4733 62 : tds = dbproc->tds_socket;
4734 62 : if (tds->has_status) {
4735 : return TRUE;
4736 : } else {
4737 30 : return FALSE;
4738 : }
4739 : }
4740 :
4741 : /**
4742 : * \ingroup dblib_rpc
4743 : * \brief Fetch status value returned by query or remote procedure call.
4744 : *
4745 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4746 : * \return return value
4747 : * \sa dbhasretstat(), dbnextrow(), dbresults(), dbretdata(), dbrpcinit(), dbrpcparam(), dbrpcsend().
4748 : */
4749 : DBINT
4750 518 : dbretstatus(DBPROCESS * dbproc)
4751 : {
4752 518 : tdsdump_log(TDS_DBG_FUNC, "dbretstatus(%p)\n", dbproc);
4753 518 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
4754 :
4755 518 : return dbproc->tds_socket->ret_status;
4756 : }
4757 :
4758 : /**
4759 : * \ingroup dblib_rpc
4760 : * \brief Get count of output parameters filled by a stored procedure.
4761 : *
4762 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4763 : * \return How many, possibly zero.
4764 : * \remarks This name sounds funny.
4765 : * \sa
4766 : */
4767 : int
4768 806 : dbnumrets(DBPROCESS * dbproc)
4769 : {
4770 : TDSSOCKET *tds;
4771 : TDS_INT result_type;
4772 :
4773 806 : tdsdump_log(TDS_DBG_FUNC, "dbnumrets(%p)\n", dbproc);
4774 806 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
4775 :
4776 806 : tds = dbproc->tds_socket;
4777 :
4778 806 : tdsdump_log(TDS_DBG_FUNC, "dbnumrets() finds %d columns\n", (tds->param_info? tds->param_info->num_cols : 0));
4779 :
4780 : /* try to fetch output parameters and return status, if we have not already done so */
4781 806 : if (!tds->param_info)
4782 508 : tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_TRAILING);
4783 :
4784 806 : if (!tds->param_info)
4785 : return 0;
4786 :
4787 300 : return tds->param_info->num_cols;
4788 : }
4789 :
4790 : /**
4791 : * \ingroup dblib_rpc
4792 : * \brief Get name of an output parameter filled by a stored procedure.
4793 : *
4794 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4795 : * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
4796 : * \returns ASCII null-terminated string, \c NULL if no such \a retnum.
4797 : * \sa dbnextrow(), dbnumrets(), dbresults(), dbretdata(), dbretlen(), dbrettype(), dbrpcinit(), dbrpcparam().
4798 : */
4799 : char *
4800 66 : dbretname(DBPROCESS * dbproc, int retnum)
4801 : {
4802 : TDSPARAMINFO *param_info;
4803 :
4804 66 : tdsdump_log(TDS_DBG_FUNC, "dbretname(%p, %d)\n", dbproc, retnum);
4805 66 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
4806 :
4807 66 : if (!dbproc->tds_socket)
4808 : return NULL;
4809 :
4810 66 : dbnumrets(dbproc);
4811 :
4812 66 : param_info = dbproc->tds_socket->param_info;
4813 66 : if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
4814 : return NULL;
4815 132 : return tds_dstr_buf(¶m_info->columns[retnum - 1]->column_name);
4816 : }
4817 :
4818 : /**
4819 : * \ingroup dblib_rpc
4820 : * \brief Get value of an output parameter filled by a stored procedure.
4821 : *
4822 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4823 : * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
4824 : * \returns Address of a return parameter value, or \c NULL if no such \a retnum.
4825 : * \sa dbnextrow(), dbnumrets(), dbresults(), dbretlen(), dbretname(), dbrettype(), dbrpcinit(), dbrpcparam().
4826 : * \todo Handle blobs.
4827 : */
4828 : BYTE *
4829 70 : dbretdata(DBPROCESS * dbproc, int retnum)
4830 : {
4831 : TDSPARAMINFO *param_info;
4832 :
4833 70 : tdsdump_log(TDS_DBG_FUNC, "dbretdata(%p, %d)\n", dbproc, retnum);
4834 70 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
4835 :
4836 70 : dbnumrets(dbproc);
4837 :
4838 70 : param_info = dbproc->tds_socket->param_info;
4839 70 : if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
4840 : return NULL;
4841 :
4842 70 : return _dbcoldata(param_info->columns[retnum - 1]);
4843 : }
4844 :
4845 : /**
4846 : * \ingroup dblib_rpc
4847 : * \brief Get size of an output parameter filled by a stored procedure.
4848 : *
4849 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4850 : * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
4851 : * \returns Size of a return parameter value, or \c NULL if no such \a retnum.
4852 : * \sa dbnextrow(), dbnumrets(), dbresults(), dbretdata(), dbretname(), dbrettype(), dbrpcinit(), dbrpcparam().
4853 : */
4854 : int
4855 66 : dbretlen(DBPROCESS * dbproc, int retnum)
4856 : {
4857 : TDSCOLUMN *column;
4858 : TDSPARAMINFO *param_info;
4859 : TDSSOCKET *tds;
4860 :
4861 66 : tdsdump_log(TDS_DBG_FUNC, "dbretlen(%p, %d)\n", dbproc, retnum);
4862 66 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
4863 :
4864 66 : dbnumrets(dbproc);
4865 :
4866 66 : tds = dbproc->tds_socket;
4867 66 : param_info = tds->param_info;
4868 66 : if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
4869 : return -1;
4870 :
4871 66 : column = param_info->columns[retnum - 1];
4872 66 : if (column->column_cur_size < 0)
4873 : return 0;
4874 :
4875 52 : return column->column_cur_size;
4876 : }
4877 :
4878 : /**
4879 : * \ingroup dblib_core
4880 : * \brief Wait for results of a query from the server.
4881 : *
4882 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
4883 : * \retval SUCCEED everything worked, fetch results with \c dbnextresults().
4884 : * \retval FAIL SQL syntax error, typically.
4885 : * \sa dbcmd(), dbfcmd(), DBIORDESC(), DBIOWDESC(), dbmoretext(), dbnextrow(),
4886 : dbpoll(), DBRBUF(), dbresults(), dbretstatus(), dbrpcsend(), dbsettime(), dbsqlexec(),
4887 : dbsqlsend(), dbwritetext().
4888 : */
4889 : RETCODE
4890 20814 : dbsqlok(DBPROCESS * dbproc)
4891 : {
4892 : TDSSOCKET *tds;
4893 : TDS_INT result_type;
4894 20814 : RETCODE return_code = SUCCEED;
4895 :
4896 20814 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok(%p)\n", dbproc);
4897 20814 : CHECK_CONN(FAIL);
4898 :
4899 20814 : tds = dbproc->tds_socket;
4900 :
4901 : /*
4902 : * dbsqlok has been called after dbmoretext()
4903 : * This is the trigger to send the text data.
4904 : */
4905 :
4906 20814 : if (dbproc->text_sent) {
4907 0 : tds_flush_packet(tds);
4908 0 : dbproc->text_sent = 0;
4909 : }
4910 :
4911 : /*
4912 : * See what the next packet from the server is.
4913 : * We want to skip any messages which are not processable.
4914 : * We're looking for a result token or a done token.
4915 : */
4916 290 : for (;;) {
4917 : TDSRET tds_code;
4918 21104 : int done_flags = 0;
4919 :
4920 : /*
4921 : * If we hit an end token -- e.g. if the command
4922 : * submitted returned no data (like an insert) -- then
4923 : * we process the end token to extract the status code.
4924 : */
4925 21104 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok() not done, calling tds_process_tokens()\n");
4926 :
4927 21104 : tds_code = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS);
4928 :
4929 : /*
4930 : * The error flag may be set for any intervening DONEINPROC packet, in particular
4931 : * by a RAISERROR statement. Microsoft db-lib returns FAIL in that case.
4932 : */
4933 21104 : if (done_flags & TDS_DONE_ERROR) {
4934 86 : return_code = FAIL;
4935 : }
4936 :
4937 21104 : switch (tds_code) {
4938 : case TDS_NO_MORE_RESULTS:
4939 20814 : return SUCCEED;
4940 : break;
4941 :
4942 21046 : case TDS_SUCCESS:
4943 21046 : switch (result_type) {
4944 12234 : case TDS_ROWFMT_RESULT:
4945 12234 : buffer_free(&dbproc->row_buf);
4946 12234 : buffer_alloc(dbproc);
4947 12234 : case TDS_COMPUTEFMT_RESULT:
4948 12234 : dbproc->dbresults_state = _DB_RES_RESULTSET_EMPTY;
4949 12234 : case TDS_COMPUTE_RESULT:
4950 : case TDS_ROW_RESULT:
4951 12234 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok() found result token\n");
4952 : return SUCCEED;
4953 : break;
4954 : case TDS_DONEINPROC_RESULT:
4955 : break;
4956 8522 : case TDS_DONE_RESULT:
4957 : case TDS_DONEPROC_RESULT:
4958 8522 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok() end status is %d (%s)\n", return_code, prdbretcode(return_code));
4959 : #if 1
4960 8522 : if (done_flags & TDS_DONE_ERROR) {
4961 :
4962 86 : if (done_flags & TDS_DONE_MORE_RESULTS) {
4963 0 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
4964 : } else {
4965 86 : dbproc->dbresults_state = _DB_RES_NO_MORE_RESULTS;
4966 : }
4967 :
4968 : } else {
4969 8436 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok() end status was success\n");
4970 :
4971 8436 : dbproc->dbresults_state = _DB_RES_SUCCEED;
4972 : }
4973 :
4974 : return return_code;
4975 : break;
4976 : #else
4977 : int retcode = (done_flags & TDS_DONE_ERROR)? FAIL : SUCCEED;
4978 : dbproc->dbresults_state = (done_flags & TDS_DONE_MORE_RESULTS)?
4979 : _DB_RES_NEXT_RESULT : _DB_RES_NO_MORE_RESULTS;
4980 :
4981 : tdsdump_log(TDS_DBG_FUNC, "dbsqlok: returning %s with %s (%#x)\n",
4982 : prdbretcode(retcode), prdbresults_state(dbproc->dbresults_state), done_flags);
4983 :
4984 : if (retcode == SUCCEED && (done_flags & TDS_DONE_MORE_RESULTS))
4985 : continue;
4986 :
4987 : return retcode;
4988 : #endif
4989 40 : default:
4990 40 : tdsdump_log(TDS_DBG_FUNC, "%s %d: logic error: tds_process_tokens result_type %d\n",
4991 : __FILE__, __LINE__, result_type);
4992 : break;
4993 : }
4994 : break;
4995 :
4996 40 : default:
4997 40 : assert(TDS_FAILED(tds_code));
4998 : return FAIL;
4999 : break;
5000 : }
5001 : }
5002 :
5003 : return SUCCEED;
5004 : }
5005 :
5006 : /**
5007 : * \ingroup dblib_core
5008 : * \brief Get count of columns in a compute row.
5009 : *
5010 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5011 : * \param computeid of \c COMPUTE clause to which we're referring.
5012 : * \return number of columns, else -1 if no such \a computeid.
5013 : * \sa dbadata(), dbadlen(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumcols().
5014 : */
5015 : int
5016 16 : dbnumalts(DBPROCESS * dbproc, int computeid)
5017 : {
5018 : TDSSOCKET *tds;
5019 : TDSCOMPUTEINFO *info;
5020 : TDS_SMALLINT compute_id;
5021 : TDS_UINT i;
5022 :
5023 16 : tdsdump_log(TDS_DBG_FUNC, "dbnumalts(%p, %d)\n", dbproc, computeid);
5024 16 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
5025 :
5026 16 : tds = dbproc->tds_socket;
5027 16 : compute_id = computeid;
5028 :
5029 24 : for (i = 0;; ++i) {
5030 32 : if (i >= tds->num_comp_info)
5031 : return -1;
5032 24 : info = tds->comp_info[i];
5033 24 : if (info->computeid == compute_id)
5034 : break;
5035 : }
5036 :
5037 16 : return info->num_cols;
5038 : }
5039 :
5040 : /**
5041 : * \ingroup dblib_core
5042 : * \brief Get count of \c COMPUTE clauses for a result set.
5043 : *
5044 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5045 : * \return number of compute clauses for the current query, possibly zero.
5046 : * \sa dbnumalts(), dbresults().
5047 : */
5048 : int
5049 0 : dbnumcompute(DBPROCESS * dbproc)
5050 : {
5051 : TDSSOCKET *tds;
5052 :
5053 0 : tdsdump_log(TDS_DBG_FUNC, "dbnumcompute(%p)\n", dbproc);
5054 0 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
5055 :
5056 0 : tds = dbproc->tds_socket;
5057 :
5058 0 : return tds->num_comp_info;
5059 : }
5060 :
5061 :
5062 : /**
5063 : * \ingroup dblib_core
5064 : * \brief Get \c bylist for a compute row.
5065 : *
5066 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5067 : * \param computeid of \c COMPUTE clause to which we're referring.
5068 : * \param size \em output: size of \c bylist buffer whose address is returned, possibly zero.
5069 : * \return address of \c bylist for \a computeid.
5070 : * \retval NULL no such \a computeid.
5071 : * \remarks Do not free returned pointer.
5072 : * \sa dbadata(), dbadlen(), dbaltlen(), dbalttype(), dbcolname(), dbgetrow(), dbnextrow().
5073 : */
5074 : BYTE *
5075 0 : dbbylist(DBPROCESS * dbproc, int computeid, int *size)
5076 : {
5077 : TDSSOCKET *tds;
5078 : TDSCOMPUTEINFO *info;
5079 : TDS_UINT i;
5080 0 : const TDS_SMALLINT byte_flag = -0x8000;
5081 :
5082 0 : tdsdump_log(TDS_DBG_FUNC, "dbbylist(%p, %d, %p)\n", dbproc, computeid, size);
5083 0 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
5084 :
5085 0 : tds = dbproc->tds_socket;
5086 :
5087 0 : for (i = 0;; ++i) {
5088 0 : if (i >= tds->num_comp_info) {
5089 0 : if (size)
5090 0 : *size = 0;
5091 : return NULL;
5092 : }
5093 0 : info = tds->comp_info[i];
5094 0 : if (info->computeid == computeid)
5095 : break;
5096 : }
5097 :
5098 0 : if (size)
5099 0 : *size = info->by_cols;
5100 :
5101 : /*
5102 : * libtds stores this information using TDS_SMALLINT so we
5103 : * have to convert it. We can do this because libtds just
5104 : * stores these data.
5105 : */
5106 0 : if (info->by_cols > 0 && info->bycolumns[0] != byte_flag) {
5107 : int n;
5108 0 : TDS_TINYINT *p = (TDS_TINYINT*) malloc(sizeof(info->bycolumns[0]) + info->by_cols);
5109 0 : if (!p) {
5110 0 : dbperror(dbproc, SYBEMEM, errno);
5111 0 : return NULL;
5112 : }
5113 0 : for (n = 0; n < info->by_cols; ++n)
5114 0 : p[sizeof(info->bycolumns[0]) + n] = TDS_MIN(info->bycolumns[n], 255);
5115 0 : *((TDS_SMALLINT *)p) = byte_flag;
5116 0 : free(info->bycolumns);
5117 0 : info->bycolumns = (TDS_SMALLINT *) p;
5118 : }
5119 0 : return (BYTE *) (&info->bycolumns[1]);
5120 : }
5121 :
5122 : /** \internal
5123 : * \ingroup dblib_internal
5124 : * \brief Check if \a dbproc is an ex-parrot.
5125 : *
5126 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5127 : * \retval TRUE process has been marked \em dead.
5128 : * \retval FALSE process is OK.
5129 : * \remarks dbdead() does not communicate with the server.
5130 : * Unless a previously db-lib marked \a dbproc \em dead, dbdead() returns \c FALSE.
5131 : * \sa dberrhandle().
5132 : */
5133 : DBBOOL
5134 10 : dbdead(DBPROCESS *dbproc)
5135 : {
5136 10 : bool is_dead = (dbproc == NULL || IS_TDSDEAD(dbproc->tds_socket));
5137 :
5138 10 : tdsdump_log(TDS_DBG_FUNC, "dbdead(%p) [%s]\n", dbproc, is_dead ? "dead" : "alive");
5139 :
5140 10 : return is_dead ? TRUE : FALSE;
5141 : }
5142 :
5143 : /** \internal
5144 : * \ingroup dblib_internal
5145 : * \brief default error handler for db-lib (handles library-generated errors)
5146 : *
5147 : * The default error handler doesn't print anything. If you want to see your messages printed,
5148 : * install an error handler. If you think that should be an optional compile- or run-time default,
5149 : * submit a patch. It could be done.
5150 : *
5151 : * \sa DBDEAD(), dberrhandle().
5152 : */
5153 : /* Thus saith Sybase:
5154 : * "If the user does not supply an error handler (or passes a NULL pointer to
5155 : * dberrhandle), DB-Library will exhibit its default error-handling
5156 : * behavior: It will abort the program if the error has made the affected
5157 : * DBPROCESS unusable (the user can call DBDEAD to determine whether
5158 : * or not a DBPROCESS has become unusable). If the error has not made the
5159 : * DBPROCESS unusable, DB-Library will simply return an error code to its caller."
5160 : *
5161 : * It is not the error handler, however, that aborts anything. It is db-lib, cf. dbperror().
5162 : */
5163 : static int
5164 10 : default_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
5165 : {
5166 10 : tdsdump_log(TDS_DBG_FUNC, "default_err_handler %p, %d, %d, %d, %p, %p", dbproc, severity, dberr, oserr, dberrstr, oserrstr);
5167 :
5168 10 : if (DBDEAD(dbproc) && (!dbproc || !dbproc->msdblib)) {
5169 : return INT_EXIT;
5170 : }
5171 :
5172 10 : if (!dbproc || !dbproc->msdblib) { /* i.e. Sybase behavior */
5173 10 : switch(dberr) {
5174 : case SYBETIME:
5175 : return INT_EXIT;
5176 : default:
5177 : break;
5178 : }
5179 0 : }
5180 10 : return INT_CANCEL;
5181 : }
5182 :
5183 : /**
5184 : * \ingroup dblib_core
5185 : * \brief Set an error handler, for messages from db-lib.
5186 : *
5187 : * \param handler pointer to callback function that will handle errors.
5188 : * Pass NULL to restore the default handler.
5189 : * \return address of prior handler, or NULL if none was previously installed.
5190 : * \sa DBDEAD(), dbmsghandle().
5191 : */
5192 : EHANDLEFUNC
5193 1222 : dberrhandle(EHANDLEFUNC handler)
5194 : {
5195 1222 : EHANDLEFUNC old_handler = _dblib_err_handler;
5196 :
5197 1222 : tdsdump_log(TDS_DBG_FUNC, "dberrhandle(%p)\n", handler);
5198 :
5199 1222 : _dblib_err_handler = handler? handler : default_err_handler;
5200 :
5201 1222 : return (old_handler == default_err_handler)? NULL : old_handler;
5202 : }
5203 :
5204 : /**
5205 : * \ingroup dblib_core
5206 : * \brief Set a message handler, for messages from the server.
5207 : *
5208 : * \param handler address of the function that will process the messages.
5209 : * \sa DBDEAD(), dberrhandle().
5210 : */
5211 : MHANDLEFUNC
5212 1242 : dbmsghandle(MHANDLEFUNC handler)
5213 : {
5214 1242 : MHANDLEFUNC retFun = _dblib_msg_handler;
5215 :
5216 1242 : tdsdump_log(TDS_DBG_FUNC, "dbmsghandle(%p)\n", handler);
5217 :
5218 1242 : _dblib_msg_handler = handler;
5219 1242 : return retFun;
5220 : }
5221 :
5222 : #if defined(DBLIB_UNIMPLEMENTED)
5223 : /**
5224 : * \ingroup dblib_money
5225 : * \brief Add two DBMONEY values.
5226 : *
5227 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5228 : * \param m1 first operand.
5229 : * \param m2 other operand.
5230 : * \param sum \em output: result of computation.
5231 : * \retval SUCCEED Always.
5232 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5233 : * \todo Unimplemented.
5234 : */
5235 : RETCODE
5236 : dbmnyadd(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * sum)
5237 : {
5238 : tdsdump_log(TDS_DBG_FUNC, "dbmnyadd(%p, %p, %p, %p)\n", dbproc, m1, m2, sum);
5239 : CHECK_CONN(FAIL);
5240 : CHECK_NULP(m1, "dbmnyadd", 2, FAIL);
5241 : CHECK_NULP(m2, "dbmnyadd", 3, FAIL);
5242 : CHECK_NULP(sum, "dbmnyadd", 4, FAIL);
5243 :
5244 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyadd()\n");
5245 : return SUCCEED;
5246 : }
5247 :
5248 :
5249 : /**
5250 : * \ingroup dblib_money
5251 : * \brief Subtract two DBMONEY values.
5252 : *
5253 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5254 : * \param m1 first operand.
5255 : * \param m2 other operand, subtracted from \a m1.
5256 : * \param difference \em output: result of computation.
5257 : * \retval SUCCEED Always.
5258 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5259 : * \todo Unimplemented.
5260 : */
5261 : RETCODE
5262 : dbmnysub(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * difference)
5263 : {
5264 : tdsdump_log(TDS_DBG_FUNC, "dbmnysub(%p, %p, %p, %p)\n", dbproc, m1, m2, difference);
5265 : CHECK_CONN(FAIL);
5266 : CHECK_NULP(m1, "dbmnysub", 2, FAIL);
5267 : CHECK_NULP(m2, "dbmnysub", 3, FAIL);
5268 : CHECK_NULP(difference, "dbmnysub", 4, FAIL);
5269 :
5270 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnysyb()\n");
5271 : return SUCCEED;
5272 : }
5273 :
5274 : /**
5275 : * \ingroup dblib_money
5276 : * \brief Multiply two DBMONEY values.
5277 : *
5278 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5279 : * \param m1 first operand.
5280 : * \param m2 other operand.
5281 : * \param prod \em output: result of computation.
5282 : * \retval SUCCEED Always.
5283 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5284 : * \todo Unimplemented.
5285 : */
5286 : RETCODE
5287 : dbmnymul(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * prod)
5288 : {
5289 : tdsdump_log(TDS_DBG_FUNC, "dbmnymul(%p, %p, %p, %p)\n", dbproc, m1, m2, prod);
5290 : CHECK_CONN(FAIL);
5291 : CHECK_NULP(m1, "dbmnymul", 2, FAIL);
5292 : CHECK_NULP(m2, "dbmnymul", 3, FAIL);
5293 : CHECK_NULP(prod, "dbmnymul", 4, FAIL);
5294 :
5295 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnymul()\n");
5296 : return SUCCEED;
5297 : }
5298 :
5299 : /**
5300 : * \ingroup dblib_money
5301 : * \brief Divide two DBMONEY values.
5302 : *
5303 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5304 : * \param m1 dividend.
5305 : * \param m2 divisor.
5306 : * \param quotient \em output: result of computation.
5307 : * \retval SUCCEED Always.
5308 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5309 : * \todo Unimplemented.
5310 : */
5311 : RETCODE
5312 : dbmnydivide(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * quotient)
5313 : {
5314 : tdsdump_log(TDS_DBG_FUNC, "dbmnydivide(%p, %p, %p, %p)\n", dbproc, m1, m2, quotient);
5315 : CHECK_CONN(FAIL);
5316 : CHECK_NULP(m1, "dbmnydivide", 2, FAIL);
5317 : CHECK_NULP(m2, "dbmnydivide", 3, FAIL);
5318 : CHECK_NULP(quotient, "dbmnydivide", 4, FAIL);
5319 :
5320 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnydivide()\n");
5321 : return SUCCEED;
5322 : }
5323 : #endif
5324 :
5325 : /**
5326 : * \ingroup dblib_money
5327 : * \brief Compare two DBMONEY values.
5328 : *
5329 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5330 : * \param m1 some money.
5331 : * \param m2 some other money.
5332 : * \retval 0 m1 == m2.
5333 : * \retval -1 m1 < m2.
5334 : * \retval 1 m1 > m2.
5335 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5336 : */
5337 : int
5338 0 : dbmnycmp(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2)
5339 : {
5340 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnycmp(%p, %p, %p)\n", dbproc, m1, m2);
5341 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
5342 0 : CHECK_NULP(m1, "dbmnycmp", 2, 0);
5343 0 : CHECK_NULP(m2, "dbmnycmp", 3, 0);
5344 :
5345 0 : if (m1->mnyhigh < m2->mnyhigh) {
5346 : return -1;
5347 : }
5348 0 : if (m1->mnyhigh > m2->mnyhigh) {
5349 : return 1;
5350 : }
5351 0 : if (m1->mnylow < m2->mnylow) {
5352 : return -1;
5353 : }
5354 0 : if (m1->mnylow > m2->mnylow) {
5355 : return 1;
5356 : }
5357 0 : return 0;
5358 : }
5359 :
5360 : #if defined(DBLIB_UNIMPLEMENTED)
5361 : /**
5362 : * \ingroup dblib_money
5363 : * \brief Multiply a DBMONEY value by a positive integer, and add an amount.
5364 : *
5365 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5366 : * \param amount starting amount of money, also holds output.
5367 : * \param multiplier amount to multiply \a amount by.
5368 : * \param addend amount to add to \a amount, after multiplying by \a multiplier.
5369 : * \retval SUCCEED Always.
5370 : * \remarks This function is goofy.
5371 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5372 : * \todo Unimplemented.
5373 : */
5374 : RETCODE
5375 : dbmnyscale(DBPROCESS * dbproc, DBMONEY * amount, int multiplier, int addend)
5376 : {
5377 : tdsdump_log(TDS_DBG_FUNC, "dbmnyscale(%p, %p, %d, %d)\n", dbproc, amount, multiplier, addend);
5378 : CHECK_CONN(FAIL);
5379 : CHECK_NULP(amount, "dbmnyscale", 2, FAIL);
5380 :
5381 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyscale()\n");
5382 : return SUCCEED;
5383 : }
5384 : #endif
5385 :
5386 : /**
5387 : * \ingroup dblib_money
5388 : * \brief Set a DBMONEY value to zero.
5389 : *
5390 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5391 : * \param dest address of a DBMONEY structure.
5392 : * \retval SUCCEED unless \a amount is NULL.
5393 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5394 : */
5395 : RETCODE
5396 0 : dbmnyzero(DBPROCESS * dbproc, DBMONEY * dest)
5397 : {
5398 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnyzero(%p, %p)\n", dbproc, dest);
5399 0 : CHECK_CONN(FAIL);
5400 0 : CHECK_NULP(dest, "dbmnyzero", 2, FAIL);
5401 :
5402 0 : dest->mnylow = 0;
5403 0 : dest->mnyhigh = 0;
5404 0 : return SUCCEED;
5405 : }
5406 :
5407 : /**
5408 : * \ingroup dblib_money
5409 : * \brief Get maximum positive DBMONEY value supported.
5410 : *
5411 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5412 : * \param amount address of a DBMONEY structure.
5413 : * \retval SUCCEED Always.
5414 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5415 : */
5416 : RETCODE
5417 0 : dbmnymaxpos(DBPROCESS * dbproc, DBMONEY * amount)
5418 : {
5419 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnymaxpos(%p, %p)\n", dbproc, amount);
5420 0 : CHECK_CONN(FAIL);
5421 0 : CHECK_NULP(amount, "dbmnymaxpos", 2, FAIL);
5422 :
5423 0 : amount->mnylow = 0xFFFFFFFFlu;
5424 0 : amount->mnyhigh = 0x7FFFFFFFl;
5425 0 : return SUCCEED;
5426 : }
5427 :
5428 : /**
5429 : * \ingroup dblib_money
5430 : * \brief Get maximum negative DBMONEY value supported.
5431 : *
5432 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5433 : * \param amount address of a DBMONEY structure.
5434 : * \retval SUCCEED Always.
5435 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5436 : */
5437 : RETCODE
5438 0 : dbmnymaxneg(DBPROCESS * dbproc, DBMONEY * amount)
5439 : {
5440 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnymaxneg(%p, %p)\n", dbproc, amount);
5441 0 : CHECK_CONN(FAIL);
5442 0 : CHECK_NULP(amount, "dbmnymaxneg", 2, FAIL);
5443 :
5444 0 : amount->mnylow = 0;
5445 0 : amount->mnyhigh = -0x7FFFFFFFL - 1;
5446 0 : return SUCCEED;
5447 : }
5448 :
5449 : #if defined(DBLIB_UNIMPLEMENTED)
5450 : /**
5451 : * \ingroup dblib_money
5452 : * \brief Get the least significant digit of a DBMONEY value, represented as a character.
5453 : *
5454 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5455 : * \param mnyptr \em input the money amount, \em and \em output: \a mnyptr divided by 10.
5456 : * \param digit the character value (between '0' and '9') of the rightmost digit in \a mnyptr.
5457 : * \param zero \em output: \c TRUE if \a mnyptr is zero on output, else \c FALSE.
5458 : * \retval SUCCEED Always.
5459 : * \sa dbconvert(), dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5460 : * \remarks Unimplemented and likely to remain so. We'd be amused to learn anyone wants this function.
5461 : * \todo Unimplemented.
5462 : */
5463 : RETCODE
5464 : dbmnyndigit(DBPROCESS * dbproc, DBMONEY * mnyptr, DBCHAR * digit, DBBOOL * zero)
5465 : {
5466 : tdsdump_log(TDS_DBG_FUNC, "dbmnyndigit(%p, %p, %s, %p)\n", dbproc, mnyptr, digit, zero);
5467 : CHECK_CONN(FAIL);
5468 : CHECK_NULP(mnyptr, "dbmnyndigit", 2, FAIL);
5469 : CHECK_NULP(digit, "dbmnyndigit", 3, FAIL);
5470 : CHECK_NULP(zero, "dbmnyndigit", 4, FAIL);
5471 :
5472 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyndigit()\n");
5473 : return SUCCEED;
5474 : }
5475 :
5476 : /**
5477 : * \ingroup dblib_money
5478 : * \brief Prepare a DBMONEY value for use with dbmnyndigit().
5479 : *
5480 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5481 : * \param amount address of a DBMONEY structure.
5482 : * \param trim number of digits to trim from \a amount.
5483 : * \param negative \em output: \c TRUE if \a amount < 0.
5484 : * \retval SUCCEED Always.
5485 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5486 : * \todo Unimplemented.
5487 : */
5488 : RETCODE
5489 : dbmnyinit(DBPROCESS * dbproc, DBMONEY * amount, int trim, DBBOOL * negative)
5490 : {
5491 : tdsdump_log(TDS_DBG_FUNC, "dbmnyinit(%p, %p, %d, %p)\n", dbproc, amount, trim, negative);
5492 : CHECK_CONN(FAIL);
5493 : CHECK_NULP(amount, "dbmnyinit", 2, FAIL);
5494 : CHECK_NULP(negative, "dbmnyinit", 4, FAIL);
5495 :
5496 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyinit()\n");
5497 : return SUCCEED;
5498 : }
5499 :
5500 :
5501 : /**
5502 : * \ingroup dblib_money
5503 : * \brief Divide a DBMONEY value by a positive integer.
5504 : *
5505 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5506 : * \param amount address of a DBMONEY structure.
5507 : * \param divisor of \a amount.
5508 : * \param remainder \em output: modulo of integer division.
5509 : * \retval SUCCEED Always.
5510 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5511 : * \todo Unimplemented.
5512 : */
5513 : RETCODE
5514 : dbmnydown(DBPROCESS * dbproc, DBMONEY * amount, int divisor, int *remainder)
5515 : {
5516 : tdsdump_log(TDS_DBG_FUNC, "dbmnydown(%p, %p, %d, %p)\n", dbproc, amount, divisor, remainder);
5517 : CHECK_CONN(FAIL);
5518 : CHECK_NULP(amount, "dbmnydown", 2, FAIL);
5519 : CHECK_NULP(remainder, "dbmnydown", 4, FAIL);
5520 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnydown()\n");
5521 : return SUCCEED;
5522 : }
5523 : #endif
5524 :
5525 : /**
5526 : * \ingroup dblib_money
5527 : * \brief Add $0.0001 to a DBMONEY value.
5528 : *
5529 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5530 : * \param amount address of a DBMONEY structure.
5531 : * \retval SUCCEED or FAIL if overflow or amount NULL.
5532 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5533 : */
5534 : RETCODE
5535 0 : dbmnyinc(DBPROCESS * dbproc, DBMONEY * amount)
5536 : {
5537 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnyinc(%p, %p)\n", dbproc, amount);
5538 0 : CHECK_CONN(FAIL);
5539 0 : CHECK_NULP(amount, "dbmnyinc", 2, FAIL);
5540 :
5541 0 : if (amount->mnylow != 0xFFFFFFFFlu) {
5542 0 : ++amount->mnylow;
5543 0 : return SUCCEED;
5544 : }
5545 0 : if (amount->mnyhigh == 0x7FFFFFFFl)
5546 : return FAIL;
5547 0 : amount->mnylow = 0;
5548 0 : ++amount->mnyhigh;
5549 0 : return SUCCEED;
5550 : }
5551 :
5552 :
5553 : /**
5554 : * \ingroup dblib_money
5555 : * \brief Subtract $0.0001 from a DBMONEY value.
5556 : *
5557 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5558 : * \param amount address of a DBMONEY structure.
5559 : * \retval SUCCEED or FAIL if overflow or amount NULL.
5560 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5561 : */
5562 : RETCODE
5563 0 : dbmnydec(DBPROCESS * dbproc, DBMONEY * amount)
5564 : {
5565 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnydec(%p, %p)\n", dbproc, amount);
5566 0 : CHECK_CONN(FAIL);
5567 0 : CHECK_NULP(amount, "dbmnydec", 2, FAIL);
5568 :
5569 0 : if (amount->mnylow != 0) {
5570 0 : --amount->mnylow;
5571 0 : return SUCCEED;
5572 : }
5573 0 : if (amount->mnyhigh == -0x7FFFFFFFL - 1)
5574 : return FAIL;
5575 0 : amount->mnylow = 0xFFFFFFFFlu;
5576 0 : --amount->mnyhigh;
5577 0 : return SUCCEED;
5578 : }
5579 :
5580 : /**
5581 : * \ingroup dblib_money
5582 : * \brief Negate a DBMONEY value.
5583 : *
5584 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5585 : * \param src address of a DBMONEY structure.
5586 : * \param dest \em output: result of negation.
5587 : * \retval SUCCEED or FAIL if overflow or src/dest NULL.
5588 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5589 : */
5590 : RETCODE
5591 0 : dbmnyminus(DBPROCESS * dbproc, DBMONEY * src, DBMONEY * dest)
5592 : {
5593 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnyminus(%p, %p, %p)\n", dbproc, src, dest);
5594 0 : CHECK_CONN(FAIL);
5595 0 : CHECK_NULP(src, "dbmnyminus", 2, FAIL);
5596 0 : CHECK_NULP(dest, "dbmnyminus", 3, FAIL);
5597 :
5598 0 : if (src->mnyhigh == -0x7FFFFFFFL - 1 && src->mnylow == 0)
5599 : return FAIL;
5600 0 : dest->mnyhigh = -src->mnyhigh;
5601 0 : dest->mnylow = (~src->mnylow) + 1u;
5602 0 : return SUCCEED;
5603 : }
5604 :
5605 :
5606 : /**
5607 : * \ingroup dblib_money
5608 : * \brief Negate a DBMONEY4 value.
5609 : *
5610 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5611 : * \param src address of a DBMONEY4 structure.
5612 : * \param dest \em output: result of negation.
5613 : * \retval SUCCEED usually.
5614 : * \retval FAIL on overflow.
5615 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5616 : */
5617 : RETCODE
5618 0 : dbmny4minus(DBPROCESS * dbproc, DBMONEY4 * src, DBMONEY4 * dest)
5619 : {
5620 : DBMONEY4 zero;
5621 :
5622 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4minus(%p, %p, %p)\n", dbproc, src, dest);
5623 0 : CHECK_CONN(FAIL);
5624 0 : CHECK_NULP(src, "dbmny4minus", 2, FAIL);
5625 0 : CHECK_NULP(dest, "dbmny4minus", 3, FAIL);
5626 :
5627 0 : dbmny4zero(dbproc, &zero);
5628 0 : return (dbmny4sub(dbproc, &zero, src, dest));
5629 : }
5630 :
5631 : /**
5632 : * \ingroup dblib_money
5633 : * \brief Zero a DBMONEY4 value.
5634 : *
5635 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5636 : * \param dest address of a DBMONEY structure.
5637 : * \retval SUCCEED usually.
5638 : * \retval FAIL \a dest is NULL.
5639 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5640 : */
5641 : RETCODE
5642 0 : dbmny4zero(DBPROCESS * dbproc, DBMONEY4 * dest)
5643 : {
5644 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4zero(%p, %p)\n", dbproc, dest);
5645 0 : CHECK_CONN(FAIL);
5646 0 : CHECK_NULP(dest, "dbmny4zero", 2, FAIL);
5647 :
5648 0 : dest->mny4 = 0;
5649 0 : return SUCCEED;
5650 : }
5651 :
5652 : /**
5653 : * \ingroup dblib_money
5654 : * \brief Add two DBMONEY4 values.
5655 : *
5656 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5657 : * \param m1 first operand.
5658 : * \param m2 other operand.
5659 : * \param sum \em output: result of computation.
5660 : * \retval SUCCEED usually.
5661 : * \retval FAIL on overflow.
5662 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5663 : */
5664 : RETCODE
5665 0 : dbmny4add(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * sum)
5666 : {
5667 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4add(%p, %p, %p, %p)\n", dbproc, m1, m2, sum);
5668 0 : CHECK_CONN(FAIL);
5669 0 : CHECK_NULP(m1, "dbmny4add", 2, FAIL);
5670 0 : CHECK_NULP(m2, "dbmny4add", 3, FAIL);
5671 0 : CHECK_NULP(sum, "dbmny4add", 4, FAIL);
5672 :
5673 0 : sum->mny4 = m1->mny4 + m2->mny4;
5674 0 : if (((m1->mny4 < 0) && (m2->mny4 < 0) && (sum->mny4 >= 0))
5675 0 : || ((m1->mny4 > 0) && (m2->mny4 > 0) && (sum->mny4 <= 0))) {
5676 : /* overflow */
5677 0 : sum->mny4 = 0;
5678 0 : return FAIL;
5679 : }
5680 : return SUCCEED;
5681 : }
5682 :
5683 : /**
5684 : * \ingroup dblib_money
5685 : * \brief Subtract two DBMONEY4 values.
5686 : *
5687 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5688 : * \param m1 first operand.
5689 : * \param m2 other operand, subtracted from \a m1.
5690 : * \param diff \em output: result of computation.
5691 : * \retval SUCCEED usually.
5692 : * \retval FAIL on overflow.
5693 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5694 : */
5695 : RETCODE
5696 0 : dbmny4sub(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * diff)
5697 : {
5698 :
5699 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4sub(%p, %p, %p, %p)\n", dbproc, m1, m2, diff);
5700 0 : CHECK_CONN(FAIL);
5701 0 : CHECK_NULP(m1, "dbmny4sub", 2, FAIL);
5702 0 : CHECK_NULP(m2, "dbmny4sub", 3, FAIL);
5703 0 : CHECK_NULP(diff, "dbmny4sub", 4, FAIL);
5704 :
5705 0 : diff->mny4 = m1->mny4 - m2->mny4;
5706 0 : if (((m1->mny4 <= 0) && (m2->mny4 > 0) && (diff->mny4 > 0))
5707 0 : || ((m1->mny4 >= 0) && (m2->mny4 < 0) && (diff->mny4 < 0))) {
5708 : /* overflow */
5709 0 : diff->mny4 = 0;
5710 0 : return FAIL;
5711 : }
5712 : return SUCCEED;
5713 : }
5714 :
5715 : #if defined(DBLIB_UNIMPLEMENTED)
5716 : /**
5717 : * \ingroup dblib_money
5718 : * \brief Multiply two DBMONEY4 values.
5719 : *
5720 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5721 : * \param m1 first operand.
5722 : * \param m2 other operand.
5723 : * \param prod \em output: result of computation.
5724 : * \retval SUCCEED usually.
5725 : * \retval FAIL a parameter is NULL.
5726 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5727 : * \todo Unimplemented.
5728 : */
5729 : RETCODE
5730 : dbmny4mul(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * prod)
5731 : {
5732 :
5733 : tdsdump_log(TDS_DBG_FUNC, "dbmny4mul(%p, %p, %p, %p)\n", dbproc, m1, m2, prod);
5734 : CHECK_CONN(FAIL);
5735 : CHECK_NULP(m1, "dbmny4mul", 2, FAIL);
5736 : CHECK_NULP(m2, "dbmny4mul", 3, FAIL);
5737 : CHECK_NULP(prod, "dbmny4mul", 4, FAIL);
5738 :
5739 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmny4mul()\n");
5740 : return FAIL;
5741 : }
5742 :
5743 : /**
5744 : * \ingroup dblib_money
5745 : * \brief Divide two DBMONEY4 values.
5746 : *
5747 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5748 : * \param m1 dividend.
5749 : * \param m2 divisor.
5750 : * \param quotient \em output: result of computation.
5751 : * \retval SUCCEED usually.
5752 : * \retval FAIL a parameter is NULL.
5753 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5754 : * \todo Unimplemented.
5755 : */
5756 : RETCODE
5757 : dbmny4divide(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * quotient)
5758 : {
5759 :
5760 : tdsdump_log(TDS_DBG_FUNC, "dbmny4divide(%p, %p, %p, %p)\n", dbproc, m1, m2, quotient);
5761 : CHECK_CONN(FAIL);
5762 : CHECK_NULP(m1, "dbmny4divide", 2, FAIL);
5763 : CHECK_NULP(m2, "dbmny4divide", 3, FAIL);
5764 : CHECK_NULP(quotient, "dbmny4divide", 4, FAIL);
5765 :
5766 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmny4divide()\n");
5767 : return FAIL;
5768 : }
5769 : #endif
5770 :
5771 : /**
5772 : * \ingroup dblib_money
5773 : * \brief Compare two DBMONEY4 values.
5774 : *
5775 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5776 : * \param m1 some money.
5777 : * \param m2 some other money.
5778 : * \retval 0 m1 == m2.
5779 : * \retval -1 m1 < m2.
5780 : * \retval 1 m1 > m2.
5781 : * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
5782 : */
5783 : int
5784 0 : dbmny4cmp(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2)
5785 : {
5786 :
5787 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4cmp(%p, %p, %p)\n", dbproc, m1, m2);
5788 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
5789 0 : CHECK_NULP(m1, "dbmny4cmp", 2, 0);
5790 0 : CHECK_NULP(m2, "dbmny4cmp", 3, 0);
5791 :
5792 0 : if (m1->mny4 < m2->mny4) {
5793 : return -1;
5794 : }
5795 0 : if (m1->mny4 > m2->mny4) {
5796 : return 1;
5797 : }
5798 0 : return 0;
5799 : }
5800 :
5801 : /**
5802 : * \ingroup dblib_money
5803 : * \brief Copy a DBMONEY4 value.
5804 : *
5805 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5806 : * \param src address of a DBMONEY4 structure.
5807 : * \param dest \em output: new money.
5808 : * \retval SUCCEED or FAIL if src/dest NULL.
5809 : * \sa dbmnycopy(), dbmnyminus(), dbmny4minus().
5810 : */
5811 : RETCODE
5812 0 : dbmny4copy(DBPROCESS * dbproc, DBMONEY4 * src, DBMONEY4 * dest)
5813 : {
5814 :
5815 0 : tdsdump_log(TDS_DBG_FUNC, "dbmny4copy(%p, %p, %p)\n", dbproc, src, dest);
5816 0 : CHECK_CONN(FAIL);
5817 0 : CHECK_NULP(src, "dbmny4copy", 2, FAIL);
5818 0 : CHECK_NULP(dest, "dbmny4copy", 3, FAIL);
5819 :
5820 0 : dest->mny4 = src->mny4;
5821 0 : return SUCCEED;
5822 : }
5823 :
5824 : /**
5825 : * \ingroup dblib_datetime
5826 : * \brief Compare DBDATETIME values, similar to strcmp(3).
5827 : *
5828 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5829 : * \param d1 a \c DBDATETIME structure address
5830 : * \param d2 another \c DBDATETIME structure address
5831 : * \retval 0 d1 = d2.
5832 : * \retval -1 d1 < d2.
5833 : * \retval 1 d1 > d2.
5834 : * \sa dbdate4cmp(), dbmnycmp(), dbmny4cmp().
5835 : */
5836 : int
5837 0 : dbdatecmp(DBPROCESS * dbproc, DBDATETIME * d1, DBDATETIME * d2)
5838 : {
5839 0 : tdsdump_log(TDS_DBG_FUNC, "dbdatecmp(%p, %p, %p)\n", dbproc, d1, d2);
5840 0 : CHECK_CONN(FAIL);
5841 0 : CHECK_NULP(d1, "dbdatecmp", 2, 0);
5842 0 : CHECK_NULP(d2, "dbdatecmp", 3, 0);
5843 :
5844 0 : if (d1->dtdays == d2->dtdays) {
5845 0 : if (d1->dttime == d2->dttime)
5846 : return 0;
5847 0 : return d1->dttime > d2->dttime ? 1 : -1;
5848 : }
5849 :
5850 : /* date 1 is before 1900 */
5851 0 : if (d1->dtdays > 2958463) {
5852 :
5853 0 : if (d2->dtdays > 2958463) /* date 2 is before 1900 */
5854 0 : return d1->dtdays > d2->dtdays ? 1 : -1;
5855 : return -1;
5856 : }
5857 :
5858 : /* date 1 is after 1900 */
5859 0 : if (d2->dtdays < 2958463) /* date 2 is after 1900 */
5860 0 : return d1->dtdays > d2->dtdays ? 1 : -1;
5861 : return 1;
5862 : }
5863 :
5864 : static RETCODE
5865 24 : dblib_datecrack(DBPROCESS * dbproc, BOOL nano_precision, DBDATEREC * output, int type, const void * data)
5866 : {
5867 : TDSDATEREC dr;
5868 24 : struct tds_sybase_dbdaterec *di = (struct tds_sybase_dbdaterec*) output;
5869 :
5870 24 : tdsdump_log(TDS_DBG_FUNC, "dblib_datecrack(%p, %d, %p, %d, %p)\n", dbproc, nano_precision, output, type, data);
5871 24 : CHECK_NULP(output, "dbdatecrack", 2, FAIL);
5872 24 : CHECK_PARAMETER(data, SYBENDTP, FAIL);
5873 :
5874 24 : if (TDS_FAILED(tds_datecrack(type, data, &dr)))
5875 : return FAIL;
5876 :
5877 24 : di->dateyear = dr.year;
5878 24 : di->quarter = dr.quarter;
5879 24 : di->datemonth = dr.month;
5880 24 : di->datedmonth = dr.day;
5881 24 : di->datedyear = dr.dayofyear;
5882 24 : di->datedweek = dr.weekday;
5883 24 : di->datehour = dr.hour;
5884 24 : di->dateminute = dr.minute;
5885 24 : di->datesecond = dr.second;
5886 24 : di->datetzone = dr.timezone;
5887 24 : if (nano_precision)
5888 : /* here we are writing to nanosecond field */
5889 4 : di->datemsecond = dr.decimicrosecond * 100u;
5890 : else
5891 20 : di->datemsecond = dr.decimicrosecond / 10000u;
5892 : /* Revert to compiled-in default if dbproc can't be used to find the runtime override. */
5893 24 : if (dbproc ? dbproc->msdblib : dblib_msdblib) {
5894 0 : ++di->quarter;
5895 0 : ++di->datemonth;
5896 0 : ++di->datedweek;
5897 : }
5898 : return SUCCEED;
5899 : }
5900 :
5901 : /**
5902 : * \ingroup dblib_core
5903 : * \brief Break a DBDATETIME value into useful pieces.
5904 : *
5905 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5906 : * \param di \em output: structure to contain the exploded parts of \a datetime.
5907 : * \param datetime \em input: \c DBDATETIME to be converted.
5908 : * \retval SUCCEED always.
5909 : * \remarks The members of \a di have different names, depending on whether \c --with-msdblib was configured.
5910 : *
5911 : * If DBPROCESS is NULL, dbdatecrack() uses the compiled in default
5912 : * value of MSDBLIB as of when libsybdb was compiled, irrespective of its value when the
5913 : * application is compiled. This can lead to incorrect results because Sybase and Microsoft use different
5914 : * ranges -- [0,11] vs. [1,12] -- for the month.
5915 : *
5916 : * \sa dbconvert(), dbdata(), dbdatechar(), dbdatename(), dbdatepart(), tdsdbopen().
5917 : */
5918 : RETCODE
5919 20 : dbdatecrack(DBPROCESS * dbproc, DBDATEREC * di, DBDATETIME * datetime)
5920 : {
5921 20 : return dblib_datecrack(dbproc, FALSE, di, SYBDATETIME, datetime);
5922 : }
5923 :
5924 : /**
5925 : * \ingroup dblib_core
5926 : * \brief Break any kind of date or time value into useful pieces.
5927 : *
5928 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5929 : * \param di \em output: structure to contain the exploded parts of \a datetime.
5930 : * \param type \em input: \c type of date/time value returned by dbcoltype().
5931 : * \param data \em input: \c date/time value to be converted.
5932 : * \retval SUCCEED always.
5933 : * \remarks The members of \a di have different names, depending on whether \c --with-msdblib was configured.
5934 : *
5935 : * This is an extension to dbdatecrack(), see it for more information.
5936 : *
5937 : * \sa dbdatecrack(), dbconvert(), dbdata(), dbdatechar(), dbdatename(), dbdatepart(), tdsdbopen().
5938 : */
5939 : RETCODE
5940 4 : dbanydatecrack(DBPROCESS * dbproc, DBDATEREC2 * di, int type, const void *data)
5941 : {
5942 4 : return dblib_datecrack(dbproc, TRUE, (DBDATEREC *) di, type, data);
5943 : }
5944 :
5945 : #if defined(DBLIB_UNIMPLEMENTED)
5946 : /**
5947 : * \ingroup dblib_core
5948 : * \brief Clear remote passwords from the LOGINREC structure.
5949 : *
5950 : * \param login structure to pass to dbopen().
5951 : * \sa dblogin(), dbopen(), dbrpwset(), DBSETLAPP(), DBSETLHOST(), DBSETLPWD(), DBSETLUSER().
5952 : * \remarks Useful for remote stored procedure calls, but not in high demand from FreeTDS.
5953 : * \todo Unimplemented.
5954 : */
5955 : void
5956 : dbrpwclr(LOGINREC * login)
5957 : {
5958 : tdsdump_log(TDS_DBG_FUNC, "dbrpwclr(%p)\n", login);
5959 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbrpwclr()\n");
5960 : }
5961 :
5962 : /**
5963 : * \ingroup dblib_core
5964 : * \brief Add a remote password to the LOGINREC structure.
5965 : *
5966 : * \param login structure to pass to dbopen().
5967 : * \param srvname server for which \a password should be used.
5968 : * \param password you guessed it, let's hope no else does.
5969 : * \param pwlen count of \a password, in bytes.
5970 : * \remarks Useful for remote stored procedure calls, but not in high demand from FreeTDS.
5971 : * \sa dblogin(), dbopen(), dbrpwclr(), DBSETLAPP(), DBSETLHOST(), DBSETLPWD(), DBSETLUSER().
5972 : * \todo Unimplemented.
5973 : */
5974 : RETCODE
5975 : dbrpwset(LOGINREC * login, char *srvname, char *password, int pwlen)
5976 : {
5977 : tdsdump_log(TDS_DBG_FUNC, "dbrpwset(%p, %s, %s, %d)\n", login, srvname, password, pwlen);
5978 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbrpwset()\n");
5979 : return SUCCEED;
5980 : }
5981 : #endif
5982 :
5983 : /**
5984 : * \ingroup dblib_core
5985 : * \brief Get server process ID for a \c DBPROCESS.
5986 : *
5987 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
5988 : * \return \em "spid", the server's process ID.
5989 : * \sa dbopen().
5990 : */
5991 : int
5992 10 : dbspid(DBPROCESS * dbproc)
5993 : {
5994 10 : tdsdump_log(TDS_DBG_FUNC, "dbspid(%p)\n", dbproc);
5995 10 : CHECK_CONN(-1);
5996 :
5997 10 : return dbproc->tds_socket->conn->spid;
5998 : }
5999 :
6000 : /**
6001 : * \ingroup dblib_core
6002 : * \brief Associate client-allocated (and defined) data with a \c DBPROCESS.
6003 : *
6004 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6005 : * \param ptr address of client-defined data.
6006 : * \remarks \a ptr is the location of user data that \c db-lib will associate with \a dbproc.
6007 : * The client allocates the buffer addressed by \a ptr. \c db-lib never examines or uses the information;
6008 : * it just stashes the pointer for later retrieval by the application with \c dbgetuserdata().
6009 : * \sa dbgetuserdata().
6010 : */
6011 : void
6012 262 : dbsetuserdata(DBPROCESS * dbproc, BYTE * ptr)
6013 : {
6014 262 : tdsdump_log(TDS_DBG_FUNC, "dbsetuserdata(%p, %p)\n", dbproc, ptr);
6015 262 : CHECK_PARAMETER(dbproc, SYBENULL, );
6016 :
6017 262 : dbproc->user_data = ptr;
6018 : }
6019 :
6020 : /**
6021 : * \ingroup dblib_core
6022 : * \brief Get address of user-allocated data from a \c DBPROCESS.
6023 : *
6024 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6025 : * \return address of user-defined data that \c db-lib associated with \a dbproc when the client called dbsetuserdata().
6026 : * \retval undefined (probably \c NULL) dbsetuserdata() was not previously called.
6027 : * \sa dbsetuserdata().
6028 : */
6029 : BYTE *
6030 194 : dbgetuserdata(DBPROCESS * dbproc)
6031 : {
6032 194 : tdsdump_log(TDS_DBG_FUNC, "dbgetuserdata(%p)\n", dbproc);
6033 194 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
6034 :
6035 194 : return dbproc->user_data;
6036 : }
6037 :
6038 : /**
6039 : * \ingroup dblib_core
6040 : * \brief Specify a db-lib version level.
6041 : *
6042 : * \param version anything, really.
6043 : * \retval SUCCEED Always.
6044 : * \remarks No effect on behavior of \c db-lib in \c FreeTDS.
6045 : * \sa
6046 : */
6047 : RETCODE
6048 40 : dbsetversion(DBINT version)
6049 : {
6050 40 : tdsdump_log(TDS_DBG_FUNC, "dbsetversion(%d)\n", version);
6051 :
6052 40 : switch (version) {
6053 40 : case DBVERSION_42:
6054 : case DBVERSION_46:
6055 : case DBVERSION_100:
6056 : case DBVERSION_70:
6057 : case DBVERSION_71:
6058 : case DBVERSION_72:
6059 : case DBVERSION_73:
6060 : case DBVERSION_74:
6061 40 : g_dblib_version = version;
6062 40 : return SUCCEED;
6063 : default:
6064 : break;
6065 : }
6066 :
6067 0 : dbperror(NULL, SYBEIVERS, 0);
6068 0 : return FAIL;
6069 : }
6070 :
6071 : /**
6072 : * \ingroup dblib_money
6073 : * \brief Copy a DBMONEY value.
6074 : *
6075 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6076 : * \param src address of a DBMONEY structure.
6077 : * \param dest \em output: new money.
6078 : * \retval SUCCEED always, unless \a src or \a dest is \c NULL.
6079 : * \sa
6080 : */
6081 : RETCODE
6082 0 : dbmnycopy(DBPROCESS * dbproc, DBMONEY * src, DBMONEY * dest)
6083 : {
6084 0 : tdsdump_log(TDS_DBG_FUNC, "dbmnycopy(%p, %p, %p)\n", dbproc, src, dest);
6085 0 : CHECK_CONN(FAIL);
6086 0 : CHECK_NULP(src, "dbmnycopy", 2, FAIL);
6087 0 : CHECK_NULP(dest, "dbmnycopy", 3, FAIL);
6088 :
6089 0 : dest->mnylow = src->mnylow;
6090 0 : dest->mnyhigh = src->mnyhigh;
6091 0 : return SUCCEED;
6092 : }
6093 :
6094 :
6095 : /**
6096 : * \ingroup dblib_core
6097 : * \brief Cancel the query currently being retrieved, discarding all pending rows.
6098 : *
6099 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6100 : * \sa
6101 : */
6102 : RETCODE
6103 814 : dbcanquery(DBPROCESS * dbproc)
6104 : {
6105 : TDSRET rc;
6106 : TDS_INT result_type;
6107 :
6108 814 : tdsdump_log(TDS_DBG_FUNC, "dbcanquery(%p)\n", dbproc);
6109 814 : CHECK_CONN(FAIL);
6110 :
6111 : /* Just throw away all pending rows from the last query */
6112 :
6113 814 : rc = tds_process_tokens(dbproc->tds_socket, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE);
6114 :
6115 814 : if (TDS_FAILED(rc))
6116 : return FAIL;
6117 :
6118 814 : dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
6119 :
6120 814 : return SUCCEED;
6121 : }
6122 :
6123 : /**
6124 : * \ingroup dblib_core
6125 : * \brief Erase the command buffer, in case \c DBNOAUTOFREE was set with dbsetopt().
6126 : *
6127 : *
6128 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6129 : * \sa dbcmd(), dbfcmd(), dbgetchar(), dbsqlexec(), dbsqlsend(), dbsetopt(), dbstrcpy(), dbstrlen().
6130 : */
6131 : void
6132 20830 : dbfreebuf(DBPROCESS * dbproc)
6133 : {
6134 20830 : tdsdump_log(TDS_DBG_FUNC, "dbfreebuf(%p)\n", dbproc);
6135 20830 : CHECK_PARAMETER(dbproc, SYBENULL, );
6136 :
6137 20830 : if (dbproc->dbbuf)
6138 20810 : TDS_ZERO_FREE(dbproc->dbbuf);
6139 20830 : dbproc->dbbufsz = 0;
6140 : }
6141 :
6142 : /**
6143 : * \ingroup dblib_core
6144 : * \brief Reset an option.
6145 : *
6146 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6147 : * \param option to be turned off.
6148 : * \param param clearing some options requires a parameter, believe it or not.
6149 : * \retval SUCCEED \a option and \a parameter seem sane.
6150 : * \retval FAIL no such \a option.
6151 : * \remarks Only the following options are recognized:
6152 : - DBARITHABORT
6153 : - DBARITHIGNORE
6154 : - DBCHAINXACTS
6155 : - DBFIPSFLAG
6156 : - DBISOLATION
6157 : - DBNOCOUNT
6158 : - DBNOEXEC
6159 : - DBPARSEONLY
6160 : - DBSHOWPLAN
6161 : - DBSTORPROCID
6162 : - DBQUOTEDIDENT
6163 : - DBSETTIME
6164 : * \sa dbisopt(), dbsetopt().
6165 : */
6166 : RETCODE
6167 20 : dbclropt(DBPROCESS * dbproc, int option, const char param[])
6168 : {
6169 : char *cmd;
6170 :
6171 20 : tdsdump_log(TDS_DBG_FUNC, "dbclropt(%p, %d, %s)\n", dbproc, option, param);
6172 20 : CHECK_CONN(FAIL);
6173 20 : if (option != DBSETTIME) {
6174 0 : CHECK_NULP(param, "dbclropt", 3, FAIL);
6175 : }
6176 :
6177 20 : if ((option < 0) || (option >= DBNUMOPTIONS)) {
6178 : return FAIL;
6179 : }
6180 20 : dbproc->dbopts[option].factive = 0;
6181 : switch (option) {
6182 0 : case DBARITHABORT:
6183 : case DBARITHIGNORE:
6184 : case DBCHAINXACTS:
6185 : case DBFIPSFLAG:
6186 : case DBISOLATION:
6187 : case DBNOCOUNT:
6188 : case DBNOEXEC:
6189 : case DBPARSEONLY:
6190 : case DBSHOWPLAN:
6191 : case DBSTORPROCID:
6192 : case DBQUOTEDIDENT:
6193 : /* server options (on/off) */
6194 0 : if (asprintf(&cmd, "set %s off\n", dbproc->dbopts[option].text) < 0) {
6195 : return FAIL;
6196 : }
6197 0 : dbstring_concat(&(dbproc->dboptcmd), cmd);
6198 0 : free(cmd);
6199 0 : break;
6200 0 : case DBBUFFER:
6201 0 : buffer_set_capacity(dbproc, 1); /* frees row_buf->rows */
6202 0 : return SUCCEED;
6203 : break;
6204 20 : case DBSETTIME:
6205 20 : tds_mutex_lock(&dblib_mutex);
6206 : /*
6207 : * Use global value of query timeout set by dbsettime() if any,
6208 : * otherwise set it to zero just like tds_init_socket() does.
6209 : */
6210 20 : if (g_dblib_ctx.query_timeout > 0) {
6211 10 : dbproc->tds_socket->query_timeout = g_dblib_ctx.query_timeout;
6212 : } else {
6213 10 : dbproc->tds_socket->query_timeout = 0;
6214 : }
6215 20 : tds_mutex_unlock(&dblib_mutex);
6216 20 : return SUCCEED;
6217 : break;
6218 : default:
6219 : break;
6220 : }
6221 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbclropt(option = %d)\n", option);
6222 : return FAIL;
6223 : }
6224 :
6225 : /**
6226 : * \ingroup dblib_core
6227 : * \brief Get value of an option
6228 : *
6229 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6230 : * \param option the option
6231 : * \param param a parameter to \a option.
6232 : * \sa dbclropt(), dbsetopt().
6233 : */
6234 : DBBOOL
6235 180 : dbisopt(DBPROCESS * dbproc, int option, const char param[])
6236 : {
6237 180 : tdsdump_log(TDS_DBG_FUNC, "dbisopt(%p, %d, %s)\n", dbproc, option, param);
6238 180 : CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
6239 : /* sometimes param can be NULL */
6240 :
6241 180 : if ((option < 0) || (option >= DBNUMOPTIONS)) {
6242 : return FALSE;
6243 : }
6244 180 : return dbproc->dbopts[option].factive;
6245 : }
6246 :
6247 : /** \internal
6248 : * \ingroup dblib_internal
6249 : * \brief Get number of the row currently being read.
6250 : *
6251 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6252 : * \return ostensibly the row number, or 0 if no rows have been read yet.
6253 : * \retval 0 Always.
6254 : * \sa DBCURROW(), dbclrbuf(), DBFIRSTROW(), dbgetrow(), DBLASTROW(), dbnextrow(), dbsetopt(),.
6255 : * \todo Unimplemented.
6256 : */
6257 : DBINT
6258 0 : dbcurrow(DBPROCESS * dbproc)
6259 : {
6260 0 : tdsdump_log(TDS_DBG_FUNC, "dbcurrow(%p)\n", dbproc);
6261 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
6262 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbcurrow()\n");
6263 : return 0;
6264 : }
6265 :
6266 :
6267 : /** \internal
6268 : * \ingroup dblib_internal
6269 : * \brief Get returned row's type.
6270 : *
6271 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6272 : * \sa DBROWTYPE().
6273 : */
6274 : STATUS
6275 0 : dbrowtype(DBPROCESS * dbproc)
6276 : {
6277 0 : tdsdump_log(TDS_DBG_FUNC, "dbrowtype(%p)\n", dbproc);
6278 0 : CHECK_PARAMETER(dbproc, SYBENULL, NO_MORE_ROWS);
6279 0 : return dbproc->row_type;
6280 : }
6281 :
6282 :
6283 : /** \internal
6284 : * \ingroup dblib_internal
6285 : * \brief Get number of the row just returned.
6286 : *
6287 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6288 : * \sa DBCURROW().
6289 : * \todo Unimplemented.
6290 : */
6291 : int
6292 0 : dbcurcmd(DBPROCESS * dbproc)
6293 : {
6294 0 : tdsdump_log(TDS_DBG_FUNC, "dbcurcmd(%p)\n", dbproc);
6295 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
6296 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbcurcmd()\n");
6297 : return 0;
6298 : }
6299 :
6300 :
6301 : /**
6302 : * \ingroup dblib_core
6303 : * \brief See if more commands are to be processed.
6304 : *
6305 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6306 : * \sa DBMORECMDS(). DBCMDROW(), dbresults(), DBROWS(), DBROWTYPE().
6307 : */
6308 : RETCODE
6309 30 : dbmorecmds(DBPROCESS * dbproc)
6310 : {
6311 30 : tdsdump_log(TDS_DBG_FUNC, "dbmorecmds(%p)\n", dbproc);
6312 30 : CHECK_CONN(FAIL);
6313 :
6314 30 : if (dbproc->tds_socket->res_info == NULL) {
6315 : return FAIL;
6316 : }
6317 :
6318 30 : if (!dbproc->tds_socket->res_info->more_results) {
6319 20 : tdsdump_log(TDS_DBG_FUNC, "more_results is false; returns FAIL\n");
6320 : return FAIL;
6321 : }
6322 :
6323 10 : tdsdump_log(TDS_DBG_FUNC, "more_results is true; returns SUCCEED\n");
6324 :
6325 : return SUCCEED;
6326 : }
6327 :
6328 : /**
6329 : * \ingroup dblib_rpc
6330 : * \brief Get datatype of a stored procedure's return parameter.
6331 : *
6332 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6333 : * \param retnum Nth return parameter, between 1 and \c dbnumrets().
6334 : * \return SYB* datatype token, or -1 if \a retnum is out of range.
6335 : * \sa dbnextrow(), dbnumrets(), dbprtype(), dbresults(), dbretdata(), dbretlen(), dbretname(), dbrpcinit(), dbrpcparam().
6336 : */
6337 : int
6338 66 : dbrettype(DBPROCESS * dbproc, int retnum)
6339 : {
6340 : TDSCOLUMN *colinfo;
6341 :
6342 66 : tdsdump_log(TDS_DBG_FUNC, "dbrettype(%p, %d)\n", dbproc, retnum);
6343 66 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
6344 66 : assert(dbproc->tds_socket);
6345 66 : assert(dbproc->tds_socket->param_info);
6346 :
6347 66 : if (retnum < 1 || retnum > dbproc->tds_socket->param_info->num_cols)
6348 : return -1;
6349 :
6350 66 : colinfo = dbproc->tds_socket->param_info->columns[retnum - 1];
6351 :
6352 66 : return tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
6353 : }
6354 :
6355 : /**
6356 : * \ingroup dblib_core
6357 : * \brief Get size of the command buffer, in bytes.
6358 : *
6359 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6360 : * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbgetchar(), dbstrcpy().
6361 : */
6362 : int
6363 0 : dbstrlen(DBPROCESS * dbproc)
6364 : {
6365 0 : tdsdump_log(TDS_DBG_FUNC, "dbstrlen(%p)\n", dbproc);
6366 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
6367 :
6368 0 : return dbproc->dbbufsz;
6369 : }
6370 :
6371 :
6372 : /**
6373 : * \ingroup dblib_core
6374 : * \brief Get address of a position in the command buffer.
6375 : *
6376 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6377 : * \param pos offset within the command buffer, starting at \em 0.
6378 : * \remarks A bit overspecialized, this one.
6379 : * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbstrcpy(), dbstrlen(),
6380 : */
6381 : char *
6382 490 : dbgetchar(DBPROCESS * dbproc, int pos)
6383 : {
6384 490 : tdsdump_log(TDS_DBG_FUNC, "dbgetchar(%p, %d)\n", dbproc, pos);
6385 490 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
6386 490 : tdsdump_log(TDS_DBG_FUNC, "dbgetchar() bufsz = %d, pos = %d\n", dbproc->dbbufsz, pos);
6387 :
6388 490 : if (dbproc->dbbufsz > 0) {
6389 490 : if (pos >= 0 && pos < (dbproc->dbbufsz - 1))
6390 480 : return (char *) &dbproc->dbbuf[pos];
6391 : return NULL;
6392 : }
6393 : return NULL;
6394 : }
6395 :
6396 : /**
6397 : * \ingroup dblib_core
6398 : * \brief Get a copy of a chunk of the command buffer.
6399 : *
6400 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6401 : * \param start position in the command buffer to start copying from, starting from \em 0.
6402 : * If start is past the end of the command buffer, dbstrcpy() inserts a null terminator at dest[0].
6403 : * \param numbytes number of bytes to copy.
6404 : - If -1, dbstrcpy() copies the whole command buffer.
6405 : - If 0 dbstrcpy() writes a \c NULL to dest[0].
6406 : - If the command buffer contains fewer than \a numbytes (taking \a start into account) dbstrcpy()
6407 : copies the rest of it.
6408 : * \param dest \em output: the buffer to write to. Make sure it's big enough.
6409 : * \retval SUCCEED the inputs were valid and \a dest was affected.
6410 : * \retval FAIL \a start < 0 or \a numbytes < -1.
6411 : * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbgetchar(), dbstrlen().
6412 : */
6413 : RETCODE
6414 0 : dbstrcpy(DBPROCESS * dbproc, int start, int numbytes, char *dest)
6415 : {
6416 0 : tdsdump_log(TDS_DBG_FUNC, "dbstrcpy(%p, %d, %d, %s)\n", dbproc, start, numbytes, dest);
6417 0 : CHECK_CONN(FAIL);
6418 0 : CHECK_NULP(dest, "dbstrcpy", 4, FAIL);
6419 :
6420 0 : if (start < 0) {
6421 0 : dbperror(dbproc, SYBENSIP, 0);
6422 0 : return FAIL;
6423 : }
6424 0 : if (numbytes < -1) {
6425 0 : dbperror(dbproc, SYBEBNUM, 0);
6426 0 : return FAIL;
6427 : }
6428 0 : dest[0] = 0; /* start with empty string being returned */
6429 0 : if (dbproc->dbbufsz > 0 && start < dbproc->dbbufsz) {
6430 0 : if (numbytes == -1)
6431 0 : numbytes = dbproc->dbbufsz - start;
6432 0 : if (start + numbytes > dbproc->dbbufsz)
6433 0 : numbytes = dbproc->dbbufsz - start;
6434 0 : memcpy(dest, (char *) &dbproc->dbbuf[start], numbytes);
6435 0 : dest[numbytes] = '\0';
6436 : }
6437 : return SUCCEED;
6438 : }
6439 :
6440 : /**
6441 : * \ingroup dblib_core
6442 : * \brief safely quotes character values in SQL text.
6443 : *
6444 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6445 : * \param src input string.
6446 : * \param srclen length of \a src in bytes, or -1 to indicate it's null-terminated.
6447 : * \param dest \em output: client-provided output buffer.
6448 : * \param destlen size of \a dest in bytes, or -1 to indicate it's "big enough" and the data should be null-terminated.
6449 : * \param quotetype
6450 : - \c DBSINGLE Doubles all single quotes (').
6451 : - \c DBDOUBLE Doubles all double quotes (").
6452 : - \c DBBOTH Doubles all single and double quotes.
6453 : * \retval SUCCEED everything worked.
6454 : * \retval FAIL no such \a quotetype, or insufficient room in \a dest.
6455 : * \sa dbcmd(), dbfcmd().
6456 : */
6457 : RETCODE
6458 50 : dbsafestr(DBPROCESS * dbproc, const char *src, DBINT srclen, char *dest, DBINT destlen, int quotetype)
6459 : {
6460 50 : int i, j = 0;
6461 50 : bool squote = false, dquote = false;
6462 :
6463 50 : tdsdump_log(TDS_DBG_FUNC, "dbsafestr(%p, %s, %d, %s, %d, %d)\n", dbproc, src, srclen, dest, destlen, quotetype);
6464 50 : CHECK_NULP(src, "dbsafestr", 2, FAIL);
6465 50 : CHECK_NULP(dest, "dbsafestr", 4, FAIL);
6466 :
6467 : /* check parameters */
6468 50 : if (srclen < -1 || destlen < -1)
6469 : return FAIL;
6470 :
6471 50 : if (srclen == -1)
6472 50 : srclen = (int)strlen(src);
6473 :
6474 50 : if (quotetype == DBSINGLE || quotetype == DBBOTH)
6475 40 : squote = true;
6476 50 : if (quotetype == DBDOUBLE || quotetype == DBBOTH)
6477 30 : dquote = true;
6478 :
6479 : /* return FAIL if invalid quotetype */
6480 50 : if (!dquote && !squote)
6481 : return FAIL;
6482 :
6483 :
6484 1790 : for (i = 0; i < srclen; i++) {
6485 :
6486 : /* dbsafestr returns fail if the deststr is not big enough */
6487 : /* need one char + one for terminator */
6488 1800 : if (destlen >= 0 && j >= destlen)
6489 : return FAIL;
6490 :
6491 1790 : if (squote && src[i] == '\'')
6492 40 : dest[j++] = '\'';
6493 1750 : else if (dquote && src[i] == '\"')
6494 30 : dest[j++] = '\"';
6495 :
6496 1790 : if (destlen >= 0 && j >= destlen)
6497 : return FAIL;
6498 :
6499 1790 : dest[j++] = src[i];
6500 : }
6501 :
6502 40 : if (destlen >= 0 && j >= destlen)
6503 : return FAIL;
6504 :
6505 30 : dest[j] = '\0';
6506 30 : return SUCCEED;
6507 : }
6508 :
6509 : /**
6510 : * \ingroup dblib_core
6511 : * \brief Print a token value's name to a buffer
6512 : *
6513 : * \param token server SYB* value, e.g. SYBINT.
6514 :
6515 : * \return ASCII null-terminated string.
6516 : * \sa dbaltop(), dbalttype(), dbcoltype(), dbrettype().
6517 : */
6518 : const char *
6519 24 : dbprtype(int token)
6520 : {
6521 24 : tdsdump_log(TDS_DBG_FUNC, "dbprtype(%d)\n", token);
6522 24 : return tds_prtype(token);
6523 : }
6524 :
6525 : /**
6526 : * \ingroup dblib_core
6527 : * \brief describe table column attributes with a single call (Freetds-only API function modelled on dbcolinfo)
6528 : *
6529 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6530 : * \param column Nth in the result set, starting from 1.
6531 : * \param pdbcol address of structure to be populated by this function.
6532 : * \return SUCCEED or FAIL.
6533 : * \sa dbcolinfo().
6534 : */
6535 : RETCODE
6536 30 : dbtablecolinfo(DBPROCESS *dbproc, DBINT column, DBCOL *pdbcol)
6537 : {
6538 : TDSCOLUMN *colinfo;
6539 :
6540 30 : tdsdump_log(TDS_DBG_FUNC, "dbtablecolinfo(%p, %d, %p)\n", dbproc, column, pdbcol);
6541 30 : CHECK_CONN(FAIL);
6542 30 : CHECK_NULP(pdbcol, "dbtablecolinfo", 3, FAIL);
6543 30 : DBPERROR_RETURN(pdbcol->SizeOfStruct != sizeof(DBCOL)
6544 : && pdbcol->SizeOfStruct != sizeof(DBCOL2), SYBECOLSIZE);
6545 :
6546 30 : colinfo = dbcolptr(dbproc, column);
6547 30 : if (!colinfo)
6548 : return FAIL;
6549 :
6550 60 : strlcpy(pdbcol->Name, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->Name));
6551 : /* returns table_column_name if available */
6552 60 : if (tds_dstr_isempty(&colinfo->table_column_name))
6553 16 : strlcpy(pdbcol->ActualName, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->ActualName));
6554 : else
6555 44 : strlcpy(pdbcol->ActualName, tds_dstr_cstr(&colinfo->table_column_name), sizeof(pdbcol->ActualName));
6556 60 : strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
6557 :
6558 30 : pdbcol->Type = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
6559 30 : pdbcol->UserType = colinfo->column_usertype;
6560 30 : pdbcol->MaxLength = colinfo->column_size;
6561 30 : if (colinfo->column_nullable)
6562 0 : pdbcol->Null = TRUE;
6563 : else
6564 30 : pdbcol->Null = FALSE;
6565 :
6566 30 : pdbcol->VarLength = FALSE;
6567 :
6568 30 : if (colinfo->column_nullable
6569 30 : || is_nullable_type(colinfo->column_type))
6570 0 : pdbcol->VarLength = TRUE;
6571 :
6572 30 : pdbcol->Precision = colinfo->column_prec;
6573 30 : pdbcol->Scale = colinfo->column_scale;
6574 :
6575 30 : pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE;
6576 30 : pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE;
6577 :
6578 30 : if (pdbcol->SizeOfStruct >= sizeof(DBCOL2)) {
6579 30 : DBCOL2 *col = (DBCOL2 *) pdbcol;
6580 : TDSRET rc;
6581 :
6582 30 : col->ServerType = colinfo->on_server.column_type;
6583 30 : col->ServerMaxLength = colinfo->on_server.column_size;
6584 :
6585 30 : rc = tds_get_column_declaration(dbproc->tds_socket, colinfo, col->ServerTypeDeclaration);
6586 30 : if (TDS_FAILED(rc))
6587 : return FAIL;
6588 : }
6589 :
6590 : return SUCCEED;
6591 : }
6592 :
6593 : /**
6594 : * \ingroup dblib_core
6595 : * \brief Get text timestamp for a column in the current row.
6596 : *
6597 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6598 : * \param column number of the column in the \c SELECT statement, starting at 1.
6599 : * \return timestamp for \a column, may be NULL.
6600 : * \sa dbtxptr(), dbwritetext().
6601 : */
6602 : DBBINARY *
6603 56 : dbtxtimestamp(DBPROCESS * dbproc, int column)
6604 : {
6605 : TDSCOLUMN *colinfo;
6606 : TDSBLOB *blob;
6607 :
6608 56 : tdsdump_log(TDS_DBG_FUNC, "dbtxtimestamp(%p, %d)\n", dbproc, column);
6609 :
6610 56 : colinfo = dbcolptr(dbproc, column);
6611 56 : if (!colinfo || !is_blob_col(colinfo))
6612 : return NULL;
6613 :
6614 56 : blob = (TDSBLOB *) colinfo->column_data;
6615 :
6616 : /* test if valid */
6617 56 : if (!blob->valid_ptr)
6618 : return NULL;
6619 :
6620 36 : return (DBBINARY *) blob->timestamp;
6621 : }
6622 :
6623 : /**
6624 : * \ingroup dblib_core
6625 : * \brief Get text pointer for a column in the current row.
6626 : *
6627 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6628 : * \param column number of the column in the \c SELECT statement, starting at 1.
6629 : * \return text pointer for \a column, may be NULL.
6630 : * \sa dbtxtimestamp(), dbwritetext().
6631 : */
6632 : DBBINARY *
6633 56 : dbtxptr(DBPROCESS * dbproc, int column)
6634 : {
6635 : TDSCOLUMN *colinfo;
6636 : TDSBLOB *blob;
6637 :
6638 56 : tdsdump_log(TDS_DBG_FUNC, "dbtxptr(%p, %d)\n", dbproc, column);
6639 :
6640 56 : colinfo = dbcolptr(dbproc, column);
6641 56 : if (!colinfo || !is_blob_col(colinfo))
6642 : return NULL;
6643 :
6644 56 : blob = (TDSBLOB *) colinfo->column_data;
6645 :
6646 : /* test if valid */
6647 56 : if (!blob->valid_ptr)
6648 : return NULL;
6649 :
6650 36 : return (DBBINARY *) blob->textptr;
6651 : }
6652 :
6653 : /**
6654 : * \ingroup dblib_core
6655 : * \brief Send text or image data to the server.
6656 : *
6657 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6658 : * \param objname table name
6659 : * \param textptr text pointer to be modified, obtained from dbtxptr().
6660 : * \param textptrlen \em Ignored. Supposed to be \c DBTXPLEN.
6661 : * \param timestamp text timestamp to be modified, obtained from dbtxtimestamp() or dbtxtsnewval(), may be \c NULL.
6662 : * \param log \c TRUE if the operation is to be recorded in the transaction log.
6663 : * \param size overall size of the data (in total, not just for this call), in bytes. A guideline, must not overstate the case.
6664 : * \param text the chunk of data to write.
6665 : * \retval SUCCEED everything worked.
6666 : * \retval FAIL not sent, possibly because \a timestamp is invalid or was changed in the database since it was fetched.
6667 : * \sa dbmoretext(), dbtxptr(), dbtxtimestamp(), dbwritetext(), dbtxtsput().
6668 : */
6669 : RETCODE
6670 36 : dbwritetext(DBPROCESS * dbproc, char *objname, DBBINARY * textptr, DBTINYINT textptrlen, DBBINARY * timestamp, DBBOOL log,
6671 : DBINT size, BYTE * text)
6672 : {
6673 : char textptr_string[35]; /* 16 * 2 + 2 (0x) + 1 */
6674 : char timestamp_string[19]; /* 8 * 2 + 2 (0x) + 1 */
6675 : TDS_INT result_type;
6676 :
6677 36 : tdsdump_log(TDS_DBG_FUNC, "dbwritetext(%p, %s, %p, %d, %p, %d)\n",
6678 : dbproc, objname, textptr, textptrlen, timestamp, log);
6679 36 : CHECK_CONN(FAIL);
6680 36 : CHECK_NULP(objname, "dbwritetext", 2, FAIL);
6681 36 : CHECK_NULP(textptr, "dbwritetext", 3, FAIL);
6682 36 : CHECK_NULP(timestamp, "dbwritetext", 5, FAIL);
6683 36 : CHECK_PARAMETER(size, SYBEZTXT, FAIL);
6684 :
6685 : if (IS_TDSDEAD(dbproc->tds_socket))
6686 : return FAIL;
6687 :
6688 36 : if (textptrlen > DBTXPLEN)
6689 : return FAIL;
6690 :
6691 36 : dbconvert(dbproc, SYBBINARY, (BYTE *) textptr, textptrlen, SYBCHAR, (BYTE *) textptr_string, -1);
6692 36 : dbconvert(dbproc, SYBBINARY, (BYTE *) timestamp, 8, SYBCHAR, (BYTE *) timestamp_string, -1);
6693 :
6694 36 : dbproc->dbresults_state = _DB_RES_INIT;
6695 :
6696 36 : if (dbproc->tds_socket->state == TDS_PENDING) {
6697 0 : const TDSRET ret = tds_process_tokens(dbproc->tds_socket, &result_type, NULL, TDS_TOKEN_TRAILING);
6698 0 : if (ret != TDS_NO_MORE_RESULTS) {
6699 0 : dbperror(dbproc, SYBERPND, 0);
6700 0 : dbproc->command_state = DBCMDSENT;
6701 0 : return FAIL;
6702 : }
6703 : }
6704 :
6705 36 : if (TDS_FAILED(tds_writetext_start(dbproc->tds_socket, objname,
6706 : textptr_string, timestamp_string, (log == TRUE), size)))
6707 : return FAIL;
6708 :
6709 36 : if (!text) {
6710 18 : dbproc->text_size = size;
6711 18 : dbproc->text_sent = 0;
6712 18 : return SUCCEED;
6713 : }
6714 :
6715 18 : tds_writetext_continue(dbproc->tds_socket, text, size);
6716 18 : tds_writetext_end(dbproc->tds_socket);
6717 18 : dbproc->text_sent = 0;
6718 :
6719 18 : if (dbsqlok(dbproc) == SUCCEED && dbresults(dbproc) == SUCCEED)
6720 : return SUCCEED;
6721 : return FAIL;
6722 : }
6723 :
6724 : /**
6725 : * \ingroup dblib_core
6726 : * \brief Fetch part of a text or image value from the server.
6727 : *
6728 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6729 : * \param buf \em output: buffer into which text will be placed.
6730 : * \param bufsize size of \a buf, in bytes.
6731 : * \return
6732 : - \c >0 count of bytes placed in \a buf.
6733 : - \c 0 end of row.
6734 : - \c -1 \em error, no result set ready for \a dbproc.
6735 : - \c NO_MORE_ROWS all rows read, no further data.
6736 : * \sa dbmoretext(), dbnextrow(), dbwritetext().
6737 : */
6738 : STATUS
6739 636 : dbreadtext(DBPROCESS * dbproc, void *buf, DBINT bufsize)
6740 : {
6741 : TDSSOCKET *tds;
6742 : TDSCOLUMN *curcol;
6743 : int cpbytes, bytes_avail;
6744 : TDS_INT result_type;
6745 : TDSRESULTINFO *resinfo;
6746 :
6747 636 : tdsdump_log(TDS_DBG_FUNC, "dbreadtext(%p, %p, %d)\n", dbproc, buf, bufsize);
6748 636 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
6749 636 : CHECK_NULP(buf, "dbreadtext", 2, -1);
6750 :
6751 636 : tds = dbproc->tds_socket;
6752 :
6753 636 : if (!tds || !tds->res_info || !tds->res_info->columns[0])
6754 : return -1;
6755 :
6756 636 : resinfo = tds->res_info;
6757 636 : curcol = resinfo->columns[0];
6758 :
6759 : /*
6760 : * if the current position is beyond the end of the text
6761 : * set pos to 0 and return 0 to denote the end of the
6762 : * text
6763 : */
6764 636 : if (curcol->column_textpos && curcol->column_textpos >= curcol->column_cur_size) {
6765 36 : curcol->column_textpos = 0;
6766 36 : return 0;
6767 : }
6768 :
6769 : /*
6770 : * if pos is 0 (first time through or last call exhausted the text)
6771 : * then read another row
6772 : */
6773 :
6774 600 : if (curcol->column_textpos == 0) {
6775 96 : const int mask = TDS_STOPAT_ROWFMT|TDS_STOPAT_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE;
6776 96 : buffer_save_row(dbproc);
6777 96 : switch (tds_process_tokens(dbproc->tds_socket, &result_type, NULL, mask)) {
6778 96 : case TDS_SUCCESS:
6779 96 : if (result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT)
6780 : break;
6781 : case TDS_NO_MORE_RESULTS:
6782 : return NO_MORE_ROWS;
6783 0 : default:
6784 0 : return -1;
6785 : }
6786 552 : }
6787 :
6788 : /* find the number of bytes to return */
6789 552 : bytes_avail = TDS_MAX(curcol->column_cur_size, 0) - curcol->column_textpos;
6790 552 : cpbytes = bytes_avail > bufsize ? bufsize : bytes_avail;
6791 552 : memcpy(buf, &((TDSBLOB *) curcol->column_data)->textvalue[curcol->column_textpos], cpbytes);
6792 552 : curcol->column_textpos += cpbytes;
6793 552 : return cpbytes;
6794 : }
6795 :
6796 : /**
6797 : * \ingroup dblib_core
6798 : * \brief Send chunk of a text/image value to the server.
6799 : *
6800 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6801 : * \param size count of bytes to send.
6802 : * \param text textpointer, obtained from dbtxptr.
6803 : * \retval SUCCEED always.
6804 : * \sa dbtxptr(), dbtxtimestamp(), dbwritetext().
6805 : * \todo Check return value of called functions and return \c FAIL if appropriate.
6806 : */
6807 : RETCODE
6808 270 : dbmoretext(DBPROCESS * dbproc, DBINT size, const BYTE text[])
6809 : {
6810 270 : tdsdump_log(TDS_DBG_FUNC, "dbmoretext(%p, %d, %p)\n", dbproc, size, text);
6811 270 : CHECK_CONN(FAIL);
6812 270 : CHECK_NULP(text, "dbmoretext", 3, FAIL);
6813 :
6814 270 : assert(dbproc->text_size >= dbproc->text_sent);
6815 :
6816 : /* TODO this test should be inside tds_writetext_continue, currently not */
6817 270 : if (size < 0 || size > dbproc->text_size - dbproc->text_sent)
6818 : return FAIL;
6819 :
6820 270 : if (size) {
6821 270 : if (TDS_FAILED(tds_writetext_continue(dbproc->tds_socket, text, size)))
6822 : return FAIL;
6823 270 : dbproc->text_sent += size;
6824 :
6825 270 : if (dbproc->text_sent == dbproc->text_size) {
6826 18 : tds_writetext_end(dbproc->tds_socket);
6827 18 : dbproc->text_sent = 0;
6828 : }
6829 : }
6830 :
6831 : return SUCCEED;
6832 : }
6833 :
6834 : /**
6835 : * \ingroup dblib_core
6836 : * \brief Record to a file all SQL commands sent to the server
6837 : *
6838 : * \param filename name of file to write to.
6839 : * \remarks Files are named \em filename.n, where n is an integer, starting with 0, and incremented with each callto dbopen().
6840 : * \sa dbopen(), TDSDUMP environment variable().
6841 : */
6842 : void
6843 0 : dbrecftos(const char filename[])
6844 : {
6845 : char *f, *old;
6846 :
6847 0 : tdsdump_log(TDS_DBG_FUNC, "dbrecftos(%s)\n", filename);
6848 0 : if (filename == NULL) {
6849 0 : dbperror(NULL, SYBENULP, 0);
6850 0 : return;
6851 : }
6852 :
6853 0 : f = strdup(filename);
6854 0 : if (!f) {
6855 0 : dbperror(NULL, SYBEMEM, 0);
6856 0 : return;
6857 : }
6858 :
6859 0 : tds_mutex_lock(&dblib_mutex);
6860 0 : old = g_dblib_ctx.recftos_filename;
6861 0 : g_dblib_ctx.recftos_filename = f;
6862 0 : g_dblib_ctx.recftos_filenum = 0;
6863 0 : tds_mutex_unlock(&dblib_mutex);
6864 0 : free(old);
6865 : }
6866 :
6867 : /** \internal
6868 : * \ingroup dblib_internal
6869 : * \brief Get the TDS version in use for \a dbproc.
6870 : *
6871 : *
6872 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6873 : * \return a \c DBTDS* token.
6874 : * \remarks The integer values of the constants are counterintuitive.
6875 : * \sa DBTDS().
6876 : */
6877 : int
6878 464 : dbtds(DBPROCESS * dbproc)
6879 : {
6880 464 : tdsdump_log(TDS_DBG_FUNC, "dbtds(%p)\n", dbproc);
6881 464 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
6882 :
6883 464 : if (dbproc->tds_socket) {
6884 464 : switch (dbproc->tds_socket->conn->tds_version) {
6885 : case 0x402:
6886 : return DBTDS_4_2;
6887 0 : case 0x406:
6888 0 : return DBTDS_4_6;
6889 78 : case 0x500:
6890 78 : return DBTDS_5_0;
6891 0 : case 0x700:
6892 0 : return DBTDS_7_0;
6893 184 : case 0x701:
6894 184 : return DBTDS_7_1;
6895 0 : case 0x702:
6896 0 : return DBTDS_7_2;
6897 100 : case 0x703:
6898 100 : return DBTDS_7_3;
6899 102 : case 0x704:
6900 102 : return DBTDS_7_4;
6901 0 : case 0x800:
6902 0 : return DBTDS_8_0_;
6903 0 : default:
6904 0 : return DBTDS_UNKNOWN;
6905 : }
6906 : }
6907 : return -1;
6908 : }
6909 :
6910 : /**
6911 : * \ingroup dblib_core
6912 : * \brief See which version of db-lib is in use.
6913 : *
6914 : * \return null-terminated ASCII string representing the version of db-lib.
6915 : * \remarks FreeTDS returns the CVS version string of dblib.c.
6916 : * \sa
6917 : */
6918 : const char *
6919 0 : dbversion(void)
6920 : {
6921 0 : tdsdump_log(TDS_DBG_FUNC, "dbversion(void)\n");
6922 0 : return TDS_VERSION_NO;
6923 : }
6924 :
6925 : #if defined(DBLIB_UNIMPLEMENTED)
6926 : /**
6927 : * \ingroup dblib_core
6928 : * \brief Set the default character set.
6929 : *
6930 : * \param charset null-terminated ASCII string, matching a row in master..syscharsets.
6931 : * \sa dbsetdeflang(), dbsetdefcharset(), dblogin(), dbopen().
6932 : * \todo Unimplemented.
6933 : */
6934 : RETCODE
6935 : dbsetdefcharset(char *charset)
6936 : {
6937 : tdsdump_log(TDS_DBG_FUNC, "dbsetdefcharset(%s)\n", charset);
6938 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetdefcharset()\n");
6939 : return SUCCEED;
6940 : }
6941 :
6942 : /**
6943 : * \ingroup dblib_core
6944 : * \brief Ready execution of a registered procedure.
6945 : *
6946 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6947 : * \param procedure_name to call.
6948 : * \param namelen size of \a procedure_name, in bytes.
6949 : * \sa dbregparam(), dbregexec(), dbregwatch(), dbreglist(), dbregwatchlist
6950 : * \todo Unimplemented.
6951 : */
6952 : RETCODE
6953 : dbreginit(DBPROCESS * dbproc, DBCHAR * procedure_name, DBSMALLINT namelen)
6954 : {
6955 : tdsdump_log(TDS_DBG_FUNC, "dbreginit(%p, %s, %d)\n", dbproc, procedure_name, namelen);
6956 : CHECK_CONN(FAIL);
6957 : CHECK_NULP(procedure_name, "dbreginit", 2, FAIL);
6958 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbreginit()\n");
6959 : return SUCCEED;
6960 : }
6961 :
6962 :
6963 : /**
6964 : * \ingroup dblib_core
6965 : * \brief Get names of Open Server registered procedures.
6966 : *
6967 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6968 : * \sa dbregparam(), dbregexec(), dbregwatch(), dbreglist(), dbregwatchlist().
6969 : * \todo Unimplemented.
6970 : */
6971 : RETCODE
6972 : dbreglist(DBPROCESS * dbproc)
6973 : {
6974 : tdsdump_log(TDS_DBG_FUNC, "dbreglist(%p)\n", dbproc);
6975 : CHECK_CONN(FAIL);
6976 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbreglist()\n");
6977 : return SUCCEED;
6978 : }
6979 :
6980 :
6981 : /**
6982 : * \ingroup dblib_core
6983 : * \brief Describe parameter of registered procedure .
6984 : *
6985 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
6986 : * \param param_name
6987 : * \param type \c SYB* datatype.
6988 : * \param datalen size of \a data.
6989 : * \param data address of buffer holding value for the parameter.
6990 : * \sa dbreginit(), dbregexec(), dbnpdefine(), dbnpcreate(), dbregwatch().
6991 : * \todo Unimplemented.
6992 : */
6993 : RETCODE
6994 : dbregparam(DBPROCESS * dbproc, char *param_name, int type, DBINT datalen, BYTE * data)
6995 : {
6996 : tdsdump_log(TDS_DBG_FUNC, "dbregparam(%p, %s, %d, %d, %p)\n", dbproc, param_name, type, datalen, data);
6997 : CHECK_CONN(FAIL);
6998 : CHECK_NULP(param_name, "dbregparam", 2, FAIL);
6999 : CHECK_NULP(data, "dbregparam", 5, FAIL);
7000 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbregparam()\n");
7001 : return SUCCEED;
7002 : }
7003 :
7004 :
7005 : /**
7006 : * \ingroup dblib_core
7007 : * \brief Execute a registered procedure.
7008 : *
7009 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7010 : * \param options
7011 : * \sa dbreginit(), dbregparam(), dbregwatch(), dbregnowatch
7012 : * \todo Unimplemented.
7013 : */
7014 : RETCODE
7015 : dbregexec(DBPROCESS * dbproc, DBUSMALLINT options)
7016 : {
7017 : tdsdump_log(TDS_DBG_FUNC, "dbregexec(%p, %d)\n", dbproc, options);
7018 : CHECK_CONN(FAIL);
7019 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbregexec()\n");
7020 : return SUCCEED;
7021 : }
7022 : #endif
7023 :
7024 :
7025 : /**
7026 : * \ingroup dblib_datetime
7027 : * \brief Get name of a month, in some human language.
7028 : *
7029 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7030 : * \param language \em ignored.
7031 : * \param monthnum number of the month, starting with 1.
7032 : * \param shortform set to \c TRUE for a three letter output ("Jan" - "Dec"), else zero.
7033 : * \return address of null-terminated ASCII string, or \c NULL on error.
7034 : * \sa db12hour(), dbdateorder(), dbdayname(), DBSETLNATLANG(), dbsetopt().
7035 : */
7036 : const char *
7037 0 : dbmonthname(DBPROCESS * dbproc, char *language, int monthnum, DBBOOL shortform)
7038 : {
7039 : static const char shortmon[][4] = {
7040 : "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
7041 : };
7042 : static const char longmon[][12] = {
7043 : "January", "February", "March", "April", "May", "June",
7044 : "July", "August", "September", "October", "November", "December"
7045 : };
7046 :
7047 0 : tdsdump_log(TDS_DBG_FUNC, "dbmonthname(%p, %s, %d, %d)\n", dbproc, language, monthnum, shortform);
7048 0 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
7049 0 : CHECK_NULP(language, "dbmonthname", 2, NULL);
7050 :
7051 0 : if (monthnum < 1 || monthnum > 12)
7052 : return NULL;
7053 0 : return (shortform) ? shortmon[monthnum - 1] : longmon[monthnum - 1];
7054 : }
7055 :
7056 : /**
7057 : * \ingroup dblib_core
7058 : * \brief See if a command caused the current database to change.
7059 : *
7060 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7061 : * \return name of new database, if changed, as a null-terminated ASCII string, else \c NULL.
7062 :
7063 : * \sa dbname(), dbresults(), dbsqlexec(), dbsqlsend(), dbuse().
7064 : */
7065 : char *
7066 0 : dbchange(DBPROCESS * dbproc)
7067 : {
7068 0 : tdsdump_log(TDS_DBG_FUNC, "dbchange(%p)\n", dbproc);
7069 0 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
7070 :
7071 0 : if (dbproc->envchange_rcv & (1 << (TDS_ENV_DATABASE - 1))) {
7072 0 : return dbproc->dbcurdb;
7073 : }
7074 : return NULL;
7075 : }
7076 :
7077 : /**
7078 : * \ingroup dblib_core
7079 : * \brief Get name of current database.
7080 : *
7081 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7082 : * \return current database name, as null-terminated ASCII string.
7083 : * \sa dbchange(), dbuse().
7084 : */
7085 : char *
7086 0 : dbname(DBPROCESS * dbproc)
7087 : {
7088 0 : tdsdump_log(TDS_DBG_FUNC, "dbname(%p)\n", dbproc);
7089 0 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
7090 0 : return dbproc->dbcurdb;
7091 : }
7092 :
7093 : /**
7094 : * \ingroup dblib_core
7095 : * \brief Get \c syscharset name of the server character set.
7096 : *
7097 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7098 : * \return name of server's charset, as null-terminated ASCII string.
7099 : * \sa dbcharsetconv(), dbgetcharset(), DBSETLCHARSET().
7100 : */
7101 : char *
7102 0 : dbservcharset(DBPROCESS * dbproc)
7103 : {
7104 :
7105 0 : tdsdump_log(TDS_DBG_FUNC, "dbservcharset(%p)\n", dbproc);
7106 0 : CHECK_PARAMETER(dbproc, SYBENULL, NULL);
7107 :
7108 0 : return dbproc->servcharset;
7109 : }
7110 :
7111 : /**
7112 : * \ingroup dblib_core
7113 : * \brief Transmit the command buffer to the server. \em Non-blocking, does not wait for a response.
7114 : *
7115 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7116 : * \retval SUCCEED SQL sent.
7117 : * \retval FAIL protocol problem, unless dbsqlsend() when it's not supposed to be (in which case a db-lib error
7118 : message will be emitted).
7119 : * \sa dbcmd(), dbfcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), dbresults(), dbsettime(), dbsqlexec(), dbsqlok().
7120 : */
7121 : RETCODE
7122 20590 : dbsqlsend(DBPROCESS * dbproc)
7123 : {
7124 : TDSSOCKET *tds;
7125 : char *cmdstr;
7126 : TDSRET rc;
7127 : TDS_INT result_type;
7128 : char timestr[256];
7129 :
7130 20590 : tdsdump_log(TDS_DBG_FUNC, "dbsqlsend(%p)\n", dbproc);
7131 20590 : CHECK_CONN(FAIL);
7132 :
7133 20590 : tds = dbproc->tds_socket;
7134 :
7135 20590 : if (tds->state == TDS_PENDING) {
7136 :
7137 130 : if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_TRAILING) != TDS_NO_MORE_RESULTS) {
7138 70 : dbperror(dbproc, SYBERPND, 0);
7139 70 : dbproc->command_state = DBCMDSENT;
7140 70 : return FAIL;
7141 : }
7142 : }
7143 :
7144 20520 : if (dbproc->dboptcmd) {
7145 0 : if ((cmdstr = dbstring_get(dbproc->dboptcmd)) == NULL) {
7146 0 : dbperror(dbproc, SYBEASEC, 0); /* Attempt to send an empty command buffer to the server */
7147 0 : return FAIL;
7148 : }
7149 0 : rc = tds_submit_query(dbproc->tds_socket, cmdstr);
7150 0 : free(cmdstr);
7151 0 : dbstring_free(&(dbproc->dboptcmd));
7152 0 : if (TDS_FAILED(rc)) {
7153 : return FAIL;
7154 : }
7155 0 : dbproc->avail_flag = FALSE;
7156 0 : dbproc->envchange_rcv = 0;
7157 0 : dbproc->dbresults_state = _DB_RES_INIT;
7158 0 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
7159 : == TDS_SUCCESS);
7160 0 : if (rc != TDS_NO_MORE_RESULTS) {
7161 : return FAIL;
7162 : }
7163 : }
7164 20520 : dbproc->more_results = TRUE;
7165 :
7166 20520 : if (dbproc->ftos != NULL) {
7167 0 : fprintf(dbproc->ftos, "%s\n", dbproc->dbbuf);
7168 0 : fprintf(dbproc->ftos, "go /* %s */\n", _dbprdate(timestr));
7169 0 : fflush(dbproc->ftos);
7170 : }
7171 :
7172 20520 : if (TDS_FAILED(tds_submit_query(dbproc->tds_socket, (char *) dbproc->dbbuf))) {
7173 : return FAIL;
7174 : }
7175 20510 : dbproc->avail_flag = FALSE;
7176 20510 : dbproc->envchange_rcv = 0;
7177 20510 : dbproc->dbresults_state = _DB_RES_INIT;
7178 20510 : dbproc->command_state = DBCMDSENT;
7179 20510 : return SUCCEED;
7180 : }
7181 :
7182 : /**
7183 : * \ingroup dblib_core
7184 : * \brief Get user-defined datatype of a compute column.
7185 : *
7186 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7187 : * \param computeid of \c COMPUTE clause to which we're referring.
7188 : * \param column Nth column in \a computeid, starting from 1.
7189 : * \returns user-defined datatype of compute column, else -1.
7190 : * \sa dbalttype(), dbcolutype().
7191 : */
7192 : DBINT
7193 0 : dbaltutype(DBPROCESS * dbproc, int computeid, int column)
7194 : {
7195 : TDSCOLUMN *colinfo;
7196 :
7197 0 : tdsdump_log(TDS_DBG_FUNC, "dbaltutype(%p, %d, %d)\n", dbproc, computeid, column);
7198 :
7199 0 : colinfo = dbacolptr(dbproc, computeid, column, false);
7200 0 : if (!colinfo)
7201 : return -1;
7202 :
7203 0 : return colinfo->column_usertype;
7204 : }
7205 :
7206 : /**
7207 : * \ingroup dblib_core
7208 : * \brief Get size of data in compute column.
7209 : *
7210 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7211 : * \param computeid of \c COMPUTE clause to which we're referring.
7212 : * \param column Nth column in \a computeid, starting from 1.
7213 : * \sa dbadata(), dbadlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
7214 : */
7215 : DBINT
7216 0 : dbaltlen(DBPROCESS * dbproc, int computeid, int column)
7217 : {
7218 : TDSCOLUMN *colinfo;
7219 :
7220 0 : tdsdump_log(TDS_DBG_FUNC, "dbaltlen(%p, %d, %d)\n", dbproc, computeid, column);
7221 :
7222 0 : colinfo = dbacolptr(dbproc, computeid, column, false);
7223 0 : if (!colinfo)
7224 : return -1;
7225 :
7226 0 : return colinfo->column_size;
7227 :
7228 : }
7229 :
7230 : /**
7231 : * \ingroup dblib_core
7232 : * \brief See if a server response has arrived.
7233 : *
7234 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7235 : * \param milliseconds how long to wait for the server before returning:
7236 : - \c 0 return immediately.
7237 : - \c -1 do not return until the server responds or a system interrupt occurs.
7238 : * \param ready_dbproc \em output: DBPROCESS for which a response arrived, of \c NULL.
7239 : * \param return_reason \em output:
7240 : - \c DBRESULT server responded.
7241 : - \c DBNOTIFICATION registered procedure notification has arrived. dbpoll() the registered handler, if
7242 : any, before it returns.
7243 : - \c DBTIMEOUT \a milliseconds elapsed before the server responded.
7244 : - \c DBINTERRUPT operating-system interrupt occurred before the server responded.
7245 : * \retval SUCCEED everything worked.
7246 : * \retval FAIL a server connection died.
7247 : * \sa DBIORDESC(), DBRBUF(), dbresults(), dbreghandle(), dbsqlok().
7248 : * \todo Unimplemented.
7249 : */
7250 : #if defined(DBLIB_UNIMPLEMENTED)
7251 : RETCODE
7252 : dbpoll(DBPROCESS * dbproc, long milliseconds, DBPROCESS ** ready_dbproc, int *return_reason)
7253 : {
7254 : tdsdump_log(TDS_DBG_FUNC, "dbpoll(%p, %ld, %p, %p)\n", dbproc, milliseconds, ready_dbproc, return_reason);
7255 : CHECK_CONN(FAIL);
7256 : CHECK_NULP(ready_dbproc, "dbpoll", 3, FAIL);
7257 : CHECK_NULP(return_reason, "dbpoll", 4, FAIL);
7258 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbpoll()\n");
7259 : return SUCCEED;
7260 : }
7261 : #endif
7262 :
7263 : /** \internal
7264 : * \ingroup dblib_internal
7265 : * \brief Get number of the first row in the row buffer.
7266 : *
7267 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7268 : * \sa DBFIRSTROW(), dbclrbuf(), DBCURROW(), dbgetrow(), DBLASTROW(), dbnextrow(), dbsetopt().
7269 : */
7270 : DBINT
7271 0 : dbfirstrow(DBPROCESS * dbproc)
7272 : {
7273 0 : tdsdump_log(TDS_DBG_FUNC, "dbfirstrow(%p)\n", dbproc);
7274 0 : CHECK_CONN(0);
7275 0 : return buffer_idx2row(&dbproc->row_buf, dbproc->row_buf.tail);
7276 : }
7277 :
7278 : /** \internal
7279 : * \ingroup dblib_internal
7280 : * \brief Get number of the last row in the row buffer.
7281 : *
7282 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7283 : * \sa DBLASTROW(), dbclrbuf(), DBCURROW(), DBFIRSTROW(), dbgetrow(), dbnextrow(), dbsetopt().
7284 : */
7285 : DBINT
7286 0 : dblastrow(DBPROCESS * dbproc)
7287 : {
7288 : int idx;
7289 :
7290 0 : tdsdump_log(TDS_DBG_FUNC, "dblastrow(%p)\n", dbproc);
7291 0 : CHECK_PARAMETER(dbproc, SYBENULL, 0);
7292 0 : idx = dbproc->row_buf.head;
7293 0 : if (dbproc->row_buf.head != dbproc->row_buf.tail) {
7294 0 : if (--idx < 0)
7295 0 : idx = dbproc->row_buf.capacity - 1;
7296 : }
7297 0 : assert(idx >= 0);
7298 0 : return buffer_idx2row(&dbproc->row_buf, idx);
7299 : }
7300 :
7301 :
7302 : /** \internal
7303 : * \ingroup dblib_internal
7304 : * \brief Get file descriptor of the socket used by a \c DBPROCESS to read data coming from the server. (!)
7305 : *
7306 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7307 : * \sa dbcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), DBRBUF(), dbresults(), dbsqlok(), dbsqlsend().
7308 : */
7309 : int
7310 0 : dbiordesc(DBPROCESS * dbproc)
7311 : {
7312 0 : tdsdump_log(TDS_DBG_FUNC, "dbiordesc(%p)\n", dbproc);
7313 0 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
7314 0 : return (int) tds_get_s(dbproc->tds_socket);
7315 : }
7316 :
7317 :
7318 : /** \internal
7319 : * \ingroup dblib_internal
7320 : * \brief Get file descriptor of the socket used by a \c DBPROCESS to write data coming to the server. (!)
7321 : *
7322 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7323 : * \sa dbcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), DBRBUF(), dbresults(), dbsqlok(), dbsqlsend().
7324 : */
7325 : int
7326 20 : dbiowdesc(DBPROCESS * dbproc)
7327 : {
7328 20 : tdsdump_log(TDS_DBG_FUNC, "dbiowdesc(%p)\n", dbproc);
7329 20 : CHECK_PARAMETER(dbproc, SYBENULL, -1);
7330 :
7331 20 : return (int) tds_get_s(dbproc->tds_socket);
7332 : }
7333 :
7334 : DBBOOL
7335 0 : dbisavail(DBPROCESS * dbproc)
7336 : {
7337 0 : tdsdump_log(TDS_DBG_FUNC, "dbisavail(%p)\n", dbproc);
7338 0 : CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
7339 0 : return dbproc->avail_flag;
7340 : }
7341 :
7342 :
7343 : /** \internal
7344 : * \ingroup dblib_internal
7345 : * \brief Mark a \c DBPROCESS as "available".
7346 : *
7347 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7348 : * \remarks Basically bogus. \c FreeTDS behaves the way Sybase's implementation does, but so what?
7349 : Many \c db-lib functions set the \c DBPROCESS to "not available", but only
7350 : dbsetavail() resets it to "available".
7351 : * \sa DBISAVAIL(). DBSETAVAIL().
7352 : */
7353 : void
7354 0 : dbsetavail(DBPROCESS * dbproc)
7355 : {
7356 0 : tdsdump_log(TDS_DBG_FUNC, "dbsetavail(%p)\n", dbproc);
7357 0 : CHECK_PARAMETER(dbproc, SYBENULL, );
7358 0 : dbproc->avail_flag = TRUE;
7359 : }
7360 :
7361 :
7362 : /**
7363 : * \ingroup dblib_core
7364 : * \brief Build a printable string from text containing placeholders for variables.
7365 : *
7366 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
7367 : * \param charbuf \em output: buffer that will contain the ASCII null-terminated string built by \c dbstrbuild().
7368 : * \param bufsize size of \a charbuf, in bytes.
7369 : * \param text null-terminated ASCII string, with \em placeholders for variables. \em A Placeholder is a
7370 : * three-byte string, made up of:
7371 : - '%' a percent sign
7372 : - 0-9 an integer (designates the argument number to use, starting with 1.)
7373 : - '!' an exclamation point
7374 : * \param formats null-terminated ASCII sprintf-style string. Has one format specifier for each placeholder in \a text.
7375 : * \remarks Following \a formats are the arguments, the values to substitute for the placeholders.
7376 : * \sa dbconvert(), dbdatename(), dbdatepart().
7377 : */
7378 : int
7379 80 : dbstrbuild(DBPROCESS * dbproc, char *charbuf, int bufsize, char *text, char *formats, ...)
7380 : {
7381 : va_list ap;
7382 : TDSRET rc;
7383 : int resultlen;
7384 :
7385 80 : tdsdump_log(TDS_DBG_FUNC, "dbstrbuild(%p, %s, %d, %s, %s, ...)\n", dbproc, charbuf, bufsize, text, formats);
7386 80 : CHECK_NULP(charbuf, "dbstrbuild", 2, FAIL);
7387 80 : CHECK_NULP(text, "dbstrbuild", 4, FAIL);
7388 80 : CHECK_NULP(formats, "dbstrbuild", 5, FAIL);
7389 :
7390 80 : va_start(ap, formats);
7391 80 : rc = tds_vstrbuild(charbuf, bufsize, &resultlen, text, TDS_NULLTERM, formats, TDS_NULLTERM, ap);
7392 80 : charbuf[resultlen] = '\0';
7393 80 : va_end(ap);
7394 80 : return TDS_SUCCEED(rc) ? resultlen : -1;
7395 : }
7396 :
7397 : static char *
7398 0 : _dbprdate(char *timestr)
7399 : {
7400 0 : time_t currtime = time(NULL);
7401 :
7402 0 : assert(timestr);
7403 :
7404 0 : strcpy(timestr, asctime(gmtime(&currtime)));
7405 0 : timestr[strlen(timestr) - 1] = '\0'; /* remove newline */
7406 0 : return timestr;
7407 :
7408 : }
7409 :
7410 : static DBINT
7411 0 : _dbnullable(DBPROCESS * dbproc, int column)
7412 : {
7413 : TDSCOLUMN *colinfo;
7414 : TDSRESULTINFO *resinfo;
7415 :
7416 0 : assert(dbproc && dbproc->tds_socket);
7417 0 : resinfo = dbproc->tds_socket->res_info;
7418 0 : if (!resinfo || column < 1 || column > resinfo->num_cols)
7419 : return FALSE;
7420 0 : colinfo = resinfo->columns[column - 1];
7421 :
7422 0 : if (colinfo->column_nullable)
7423 : return TRUE;
7424 0 : return FALSE;
7425 : }
7426 :
7427 : /** Returns type in string. Used for debugging purpose */
7428 : static const char *
7429 0 : tds_prdatatype(int datatype_token)
7430 : {
7431 0 : switch ((TDS_SERVER_TYPE) datatype_token) {
7432 : case SYBCHAR: return "SYBCHAR";
7433 0 : case SYBVARCHAR: return "SYBVARCHAR";
7434 0 : case SYBINTN: return "SYBINTN";
7435 0 : case SYBINT1: return "SYBINT1";
7436 0 : case SYBINT2: return "SYBINT2";
7437 0 : case SYBINT4: return "SYBINT4";
7438 0 : case SYBINT8: return "SYBINT8";
7439 0 : case SYBFLT8: return "SYBFLT8";
7440 0 : case SYBDATETIME: return "SYBDATETIME";
7441 0 : case SYBBIT: return "SYBBIT";
7442 0 : case SYBTEXT: return "SYBTEXT";
7443 0 : case SYBNTEXT: return "SYBNTEXT";
7444 0 : case SYBIMAGE: return "SYBIMAGE";
7445 0 : case SYBMONEY4: return "SYBMONEY4";
7446 0 : case SYBMONEY: return "SYBMONEY";
7447 0 : case SYBDATETIME4: return "SYBDATETIME4";
7448 0 : case SYBREAL: return "SYBREAL";
7449 0 : case SYBBINARY: return "SYBBINARY";
7450 0 : case SYBVOID: return "SYBVOID";
7451 0 : case SYBVARBINARY: return "SYBVARBINARY";
7452 0 : case SYBNVARCHAR: return "SYBNVARCHAR";
7453 0 : case SYBBITN: return "SYBBITN";
7454 0 : case SYBNUMERIC: return "SYBNUMERIC";
7455 0 : case SYBDECIMAL: return "SYBDECIMAL";
7456 0 : case SYBFLTN: return "SYBFLTN";
7457 0 : case SYBMONEYN: return "SYBMONEYN";
7458 0 : case SYBDATETIMN: return "SYBDATETIMN";
7459 0 : case XSYBCHAR: return "XSYBCHAR";
7460 0 : case XSYBVARCHAR: return "XSYBVARCHAR";
7461 0 : case XSYBNVARCHAR: return "XSYBNVARCHAR";
7462 0 : case XSYBNCHAR: return "XSYBNCHAR";
7463 0 : case XSYBVARBINARY: return "XSYBVARBINARY";
7464 0 : case XSYBBINARY: return "XSYBBINARY";
7465 0 : case SYBLONGBINARY: return "SYBLONGBINARY";
7466 0 : case SYBSINT1: return "SYBSINT1";
7467 0 : case SYBUINT2: return "SYBUINT2";
7468 0 : case SYBUINT4: return "SYBUINT4";
7469 0 : case SYBUINT8: return "SYBUINT8";
7470 0 : case SYBUNIQUE: return "SYBUNIQUE";
7471 0 : case SYBVARIANT: return "SYBVARIANT";
7472 0 : case SYBMSXML: return "SYBMSXML";
7473 0 : case SYBMSDATE: return "SYBMSDATE";
7474 0 : case SYBMSTIME: return "SYBMSTIME";
7475 0 : case SYBMSDATETIME2: return "SYBMSDATETIME2";
7476 0 : case SYBMSDATETIMEOFFSET: return "SYBMSDATETIMEOFFSET";
7477 0 : case SYBDATE: return "SYBDATE";
7478 0 : case SYBTIME: return "SYBTIME";
7479 0 : case SYB5BIGDATETIME: return "SYBBIGDATETIME";
7480 0 : case SYB5BIGTIME: return "SYBBIGTIME";
7481 0 : case SYBMSUDT: return "SYBMSUDT";
7482 0 : case SYBUINT1: return "SYBUINT1";
7483 0 : case SYBDATEN: return "SYBDATEN";
7484 0 : case SYB5INT8: return "SYB5INT8";
7485 0 : case SYBINTERVAL: return "SYBINTERVAL";
7486 0 : case SYBTIMEN: return "SYBTIMEN";
7487 0 : case SYBUINTN: return "SYBUINTN";
7488 0 : case SYBUNITEXT: return "SYBUNITEXT";
7489 0 : case SYBXML: return "SYBXML";
7490 0 : case SYBMSTABLE: return "SYBMSTABLE";
7491 : }
7492 0 : return "(unknown)";
7493 : }
7494 : #if 1
7495 : void
7496 410744 : copy_data_to_host_var(DBPROCESS * dbproc, TDS_SERVER_TYPE srctype, const BYTE * src, DBINT srclen,
7497 : BYTE * dest, DBINT destlen,
7498 : int bindtype, DBINT *indicator)
7499 : {
7500 : CONV_RESULT dres;
7501 : DBINT ret;
7502 : int len;
7503 410744 : DBINT indicator_value = 0;
7504 :
7505 410744 : bool limited_dest_space = false;
7506 410744 : TDS_SERVER_TYPE desttype = dblib_bound_type(bindtype);
7507 :
7508 410744 : tdsdump_log(TDS_DBG_FUNC, "copy_data_to_host_var(%d [%s] len %d => %d [%s] len %d)\n",
7509 : srctype, tds_prdatatype(srctype), srclen, desttype, tds_prdatatype(desttype), destlen);
7510 820990 : CHECK_NULP(src, "copy_data_to_host_var", 3, );
7511 410744 : CHECK_NULP(dest, "copy_data_to_host_var", 6, );
7512 410744 : if (desttype == TDS_INVALID_TYPE)
7513 : return;
7514 : /* indicator can be NULL */
7515 :
7516 410744 : assert(srclen >= 0);
7517 :
7518 410744 : if (destlen > 0) {
7519 290 : limited_dest_space = true;
7520 : }
7521 :
7522 : /* oft times we are asked to convert a data type to itself */
7523 :
7524 410744 : if (desttype == SYBNUMERIC) {
7525 240 : DBNUMERIC *num = NULL; /* num->scale is unsigned */
7526 :
7527 : /* only MS, use always source */
7528 240 : if (bindtype == SRCNUMERICBIND || bindtype == SRCDECIMALBIND) {
7529 120 : if (is_numeric_type(srctype))
7530 : num = (DBNUMERIC*) src;
7531 : else
7532 40 : num = (DBNUMERIC*) dest;
7533 120 : } else if (dbproc->msdblib) {
7534 : /* MS by default use only destination information */
7535 : num = (DBNUMERIC*) dest;
7536 : } else {
7537 : /* Sybase, dbbind means source or default */
7538 : /* TODO if dbbind_ps is used is more complicated */
7539 60 : if (is_numeric_type(srctype))
7540 : num = (DBNUMERIC*) src;
7541 : }
7542 : if (!num) {
7543 20 : dres.n.precision = 18;
7544 20 : dres.n.scale = 0;
7545 : } else {
7546 220 : dres.n.precision = num->precision;
7547 220 : dres.n.scale = num->scale;
7548 : }
7549 411850 : } else if ((srctype == desttype) ||
7550 2434 : (is_similar_type(srctype, desttype))) {
7551 :
7552 410246 : tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var() srctype == desttype\n");
7553 410246 : switch (desttype) {
7554 :
7555 10 : case SYBVARBINARY:
7556 : case SYBBINARY:
7557 : case SYBIMAGE:
7558 10 : if (bindtype == VARYBINBIND) {
7559 10 : DBVARYBIN *bin = (DBVARYBIN*) dest;
7560 10 : if (limited_dest_space) {
7561 10 : if (srclen > sizeof(bin->array)) {
7562 0 : dbperror(dbproc, SYBECOFL, 0);
7563 0 : indicator_value = srclen;
7564 0 : srclen = sizeof(bin->array);
7565 : }
7566 : }
7567 10 : memcpy(bin->array, src, srclen);
7568 10 : bin->len = srclen;
7569 10 : break;
7570 : }
7571 :
7572 0 : if (srclen > destlen && destlen >= 0) {
7573 0 : dbperror(dbproc, SYBECOFL, 0);
7574 : } else {
7575 0 : memcpy(dest, src, srclen);
7576 0 : if (srclen < destlen)
7577 0 : memset(dest + srclen, 0, destlen - srclen);
7578 : }
7579 : break;
7580 :
7581 205622 : case SYBCHAR:
7582 : case SYBVARCHAR:
7583 : case SYBTEXT:
7584 :
7585 205622 : switch (bindtype) {
7586 : case NTBSTRINGBIND: /* strip trailing blanks, null term */
7587 574 : while (srclen && src[srclen - 1] == ' ') {
7588 252 : --srclen;
7589 : }
7590 322 : if (limited_dest_space) {
7591 40 : if (srclen + 1 > destlen) {
7592 10 : dbperror(dbproc, SYBECOFL, 0);
7593 10 : indicator_value = srclen + 1;
7594 10 : srclen = destlen - 1;
7595 : }
7596 : }
7597 322 : memcpy(dest, src, srclen);
7598 322 : dest[srclen] = '\0';
7599 322 : break;
7600 205240 : case STRINGBIND: /* pad with blanks, NUL term */
7601 205240 : if (limited_dest_space) {
7602 30 : if (srclen + 1 > destlen) {
7603 20 : dbperror(dbproc, SYBECOFL, 0);
7604 20 : indicator_value = srclen + 1;
7605 20 : srclen = destlen - 1;
7606 : }
7607 : } else {
7608 205210 : destlen = srclen + 1;
7609 : }
7610 205240 : memcpy(dest, src, srclen);
7611 205240 : if (srclen < destlen)
7612 205240 : memset(dest + srclen, ' ', destlen - srclen - 1);
7613 205240 : dest[destlen - 1] = '\0';
7614 205240 : break;
7615 40 : case CHARBIND: /* pad with blanks, NO NUL term */
7616 40 : if (limited_dest_space) {
7617 30 : if (srclen > destlen) {
7618 10 : dbperror(dbproc, SYBECOFL, 0);
7619 10 : indicator_value = srclen;
7620 10 : srclen = destlen;
7621 : }
7622 : } else {
7623 : destlen = srclen;
7624 : }
7625 40 : memcpy(dest, src, srclen);
7626 40 : if (srclen < destlen)
7627 10 : memset(dest + srclen, ' ', destlen - srclen);
7628 : break;
7629 20 : case VARYCHARBIND: /* strip trailing blanks, NO NUL term */
7630 20 : if (limited_dest_space) {
7631 20 : if (srclen > destlen) {
7632 0 : dbperror(dbproc, SYBECOFL, 0);
7633 0 : indicator_value = srclen;
7634 0 : srclen = destlen;
7635 : }
7636 : }
7637 20 : memcpy(((DBVARYCHAR *)dest)->str, src, srclen);
7638 20 : ((DBVARYCHAR *)dest)->len = srclen;
7639 20 : break;
7640 : }
7641 : break;
7642 204614 : case SYBINT1:
7643 : case SYBINT2:
7644 : case SYBINT4:
7645 : case SYBINT8:
7646 : case SYBFLT8:
7647 : case SYBREAL:
7648 : case SYBBIT:
7649 : case SYBBITN:
7650 : case SYBMONEY:
7651 : case SYBMONEY4:
7652 : case SYBDATETIME:
7653 : case SYBDATETIME4:
7654 : case SYBDATE:
7655 : case SYBTIME:
7656 : case SYB5BIGDATETIME:
7657 : case SYB5BIGTIME:
7658 : case SYBUNIQUE:
7659 204614 : ret = tds_get_size_by_type(desttype);
7660 204614 : memcpy(dest, src, ret);
7661 204614 : break;
7662 :
7663 0 : case SYBMSDATE:
7664 : case SYBMSTIME:
7665 : case SYBMSDATETIME2:
7666 : case SYBMSDATETIMEOFFSET:
7667 0 : ret = sizeof(TDS_DATETIMEALL);
7668 0 : memcpy(dest, src, ret);
7669 0 : break;
7670 :
7671 : default:
7672 : break;
7673 : }
7674 410246 : if (indicator)
7675 136 : *indicator = indicator_value;
7676 :
7677 : return;
7678 :
7679 : } /* end srctype == desttype */
7680 :
7681 498 : len = tds_convert(g_dblib_ctx.tds_ctx, srctype, src, srclen, desttype, &dres);
7682 :
7683 498 : tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var(): tds_convert returned %d\n", len);
7684 :
7685 498 : if (len < 0) {
7686 0 : _dblib_convert_err(dbproc, len);
7687 0 : return;
7688 : }
7689 :
7690 498 : switch (desttype) {
7691 60 : case SYBVARBINARY:
7692 : case SYBBINARY:
7693 : case SYBIMAGE:
7694 60 : if (bindtype == VARYBINBIND) {
7695 30 : if (limited_dest_space) {
7696 30 : if (len > sizeof(((DBVARYBIN *)dest)->array)) {
7697 0 : dbperror(dbproc, SYBECOFL, 0);
7698 0 : indicator_value = len;
7699 0 : len = sizeof(((DBVARYBIN *)dest)->array);
7700 : }
7701 : }
7702 30 : memcpy(((DBVARYBIN *)dest)->array, dres.c, len);
7703 30 : ((DBVARYBIN *)dest)->len = len;
7704 : } else {
7705 30 : if (len > destlen && destlen >= 0) {
7706 0 : dbperror(dbproc, SYBECOFL, 0);
7707 : } else {
7708 30 : memcpy(dest, dres.ib, len);
7709 30 : if (len < destlen)
7710 10 : memset(dest + len, 0, destlen - len);
7711 : }
7712 : }
7713 60 : TDS_ZERO_FREE(dres.ib);
7714 60 : break;
7715 250 : case SYBINT1:
7716 : case SYBINT2:
7717 : case SYBINT4:
7718 : case SYBINT8:
7719 : case SYBFLT8:
7720 : case SYBREAL:
7721 : case SYBBIT:
7722 : case SYBBITN:
7723 : case SYBMONEY:
7724 : case SYBMONEY4:
7725 : case SYBDATETIME:
7726 : case SYBDATETIME4:
7727 : case SYBDATE:
7728 : case SYBTIME:
7729 : case SYBNUMERIC:
7730 : case SYBDECIMAL:
7731 : case SYBUNIQUE:
7732 : case SYBMSDATE:
7733 : case SYBMSTIME:
7734 : case SYB5BIGDATETIME:
7735 : case SYB5BIGTIME:
7736 : case SYBMSDATETIME2:
7737 : case SYBMSDATETIMEOFFSET:
7738 250 : memcpy(dest, &(dres.ti), len);
7739 250 : break;
7740 188 : case SYBCHAR:
7741 : case SYBVARCHAR:
7742 : case SYBTEXT:
7743 188 : tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var() outputs %d bytes char data destlen = %d \n", len, destlen);
7744 188 : switch (bindtype) {
7745 : case NTBSTRINGBIND: /* strip trailing blanks, null term */
7746 40 : while (len && dres.c[len - 1] == ' ') {
7747 0 : --len;
7748 : }
7749 40 : if (limited_dest_space) {
7750 30 : if (len + 1 > destlen) {
7751 10 : dbperror(dbproc, SYBECOFL, 0);
7752 10 : len = destlen - 1;
7753 : }
7754 : }
7755 40 : memcpy(dest, dres.c, len);
7756 40 : dest[len] = '\0';
7757 40 : break;
7758 98 : case STRINGBIND: /* pad with blanks, null term */
7759 98 : if (limited_dest_space) {
7760 30 : if (len + 1 > destlen) {
7761 10 : dbperror(dbproc, SYBECOFL, 0);
7762 10 : len = destlen - 1;
7763 : }
7764 : } else {
7765 68 : destlen = len + 1;
7766 : }
7767 98 : memcpy(dest, dres.c, len);
7768 98 : if (len < destlen)
7769 98 : memset(dest + len, ' ', destlen - len - 1);
7770 98 : dest[destlen - 1] = '\0';
7771 98 : break;
7772 50 : case CHARBIND: /* pad with blanks, NO null term */
7773 50 : if (limited_dest_space) {
7774 40 : if (len > destlen) {
7775 10 : dbperror(dbproc, SYBECOFL, 0);
7776 10 : indicator_value = len;
7777 10 : len = destlen;
7778 : }
7779 : } else {
7780 : destlen = len;
7781 : }
7782 50 : memcpy(dest, dres.c, len);
7783 50 : if (len < destlen)
7784 20 : memset(dest + len, ' ', destlen - len);
7785 : break;
7786 0 : case VARYCHARBIND: /* strip trailing blanks, NO null term */
7787 0 : if (limited_dest_space) {
7788 0 : if (len > sizeof(((DBVARYCHAR *)dest)->str)) {
7789 0 : dbperror(dbproc, SYBECOFL, 0);
7790 0 : indicator_value = len;
7791 0 : len = sizeof(((DBVARYCHAR *)dest)->str);
7792 : }
7793 : }
7794 0 : memcpy(((DBVARYCHAR *)dest)->str, dres.c, len);
7795 0 : ((DBVARYCHAR *)dest)->len = len;
7796 0 : break;
7797 : }
7798 :
7799 188 : free(dres.c);
7800 188 : break;
7801 0 : default:
7802 0 : tdsdump_log(TDS_DBG_INFO1, "error: copy_data_to_host_var(): unrecognized desttype %d \n", desttype);
7803 : break;
7804 :
7805 : }
7806 498 : if (indicator)
7807 0 : *indicator = indicator_value;
7808 : }
7809 : #endif
7810 :
7811 : /** \internal
7812 : * \ingroup dblib_internal
7813 : * \remarks member msgno Vendor-defined message number
7814 : * \remarks member severity Is passed to the error handler
7815 : * \remarks member msgtext Text of message
7816 : */
7817 : typedef struct _dblib_error_message
7818 : {
7819 : DBINT msgno;
7820 : int severity;
7821 : const char *msgtext;
7822 : } DBLIB_ERROR_MESSAGE;
7823 :
7824 : /*
7825 : * The msgtext member holds up to two strings. The first one is the message text, which may contain placeholders.
7826 : * The second one, if it exists, is the format string for dbstrbuild(). Messages containing no placeholders still need
7827 : * an extra NULL to indicate a zero-length format string.
7828 : */
7829 : static const DBLIB_ERROR_MESSAGE dblib_error_messages[] =
7830 : { { SYBEVERDOWN, EXINFO, "TDS version downgraded to 7.1!\0" }
7831 : , { SYBEICONVIU, EXCONVERSION, "Some character(s) could not be converted into client's character set\0" }
7832 : , { SYBEICONVAVAIL, EXCONVERSION, "Character set conversion is not available between client character set '%1!' and "
7833 : "server character set '%2!'\0%s %s" }
7834 : , { SYBEICONVO, EXCONVERSION, "Error converting characters into server's character set. Some character(s) could "
7835 : "not be converted\0" }
7836 : , { SYBEICONVI, EXCONVERSION, "Some character(s) could not be converted into client's character set. Unconverted "
7837 : "bytes were changed to question marks ('?')\0" }
7838 : , { SYBEICONV2BIG, EXCONVERSION, "Buffer overflow converting characters from client into server's character set\0" }
7839 :
7840 :
7841 : , { SYBEPORT, EXUSER, "Both port and instance specified\0" }
7842 : , { SYBETDSVER, EXUSER, "Cannot bcp with TDSVER < 5.0\0" }
7843 : , { SYBEAAMT, EXPROGRAM, "User attempted a dbaltbind with mismatched column and variable types\0" }
7844 : , { SYBEABMT, EXPROGRAM, "User attempted a dbbind with mismatched column and variable types\0" }
7845 : , { SYBEABNC, EXPROGRAM, "Attempt to bind to a non-existent column\0" }
7846 : , { SYBEABNP, EXPROGRAM, "Attempt to bind using NULL pointers\0" }
7847 : , { SYBEABNV, EXPROGRAM, "Attempt to bind to a NULL program variable\0" }
7848 : , { SYBEACNV, EXCONVERSION, "Attempt to do data-conversion with NULL destination variable.\0" }
7849 : , { SYBEADST, EXCONSISTENCY, "International Release: Error in attempting to determine the size of a pair of "
7850 : "translation tables\0" }
7851 : , { SYBEAICF, EXCONSISTENCY, "International Release: Error in attempting to install custom format\0" }
7852 : , { SYBEALTT, EXCONSISTENCY, "International Release: Error in attempting to load a pair of translation tables\0" }
7853 : , { SYBEAOLF, EXRESOURCE, "International Release: Error in attempting to open a localization file\0" }
7854 : , { SYBEAPCT, EXCONSISTENCY, "International Release: Error in attempting to perform a character set translation\0" }
7855 : , { SYBEAPUT, EXPROGRAM, "Attempt to print unknown token\0" }
7856 : , { SYBEARDI, EXRESOURCE, "International Release: Error in attempting to read datetime information from a "
7857 : "localization file\0" }
7858 : , { SYBEARDL, EXRESOURCE, "International Release: Error in attempting to read the dblib.loc localization file\0" }
7859 : , { SYBEASEC, EXPROGRAM, "Attempt to send an empty command buffer to the server\0" }
7860 : , { SYBEASNL, EXPROGRAM, "Attempt to set fields in a null LOGINREC\0" }
7861 : , { SYBEASTL, EXPROGRAM, "Synchronous I/O attempted at AST level\0" }
7862 : , { SYBEASUL, EXPROGRAM, "Attempt to set unknown LOGINREC field\0" }
7863 : , { SYBEAUTN, EXPROGRAM, "Attempt to update the timestamp of a table that has no timestamp column\0" }
7864 : , { SYBEBADPK, EXINFO, "Packet size of %1! not supported -- size of %2! used instead!\0%d %d" }
7865 : , { SYBEBBCI, EXINFO, "Batch successfully bulk copied to the server\0" }
7866 : , { SYBEBBL, EXPROGRAM, "Bad bindlen parameter passed to dbsetnull\0" }
7867 : , { SYBEBCBC, EXPROGRAM, "bcp_columns must be called before bcp_colfmt and bcp_colfmt_ps\0" }
7868 : , { SYBEBCBNPR, EXPROGRAM, "bcp_bind: if varaddr is NULL, prefixlen must be 0 "
7869 : "and no terminator should be specified\0" }
7870 : , { SYBEBCBNTYP, EXPROGRAM, "bcp_bind: if varaddr is NULL and varlen greater than 0, the table column type "
7871 : "must be SYBTEXT or SYBIMAGE and the program variable type must be SYBTEXT, SYBCHAR, "
7872 : "SYBIMAGE or SYBBINARY\0" }
7873 : , { SYBEBCBPREF, EXPROGRAM, "Illegal prefix length. Legal values are 0, 1, 2 or 4\0" }
7874 : , { SYBEBCFO, EXUSER, "bcp host files must contain at least one column\0" }
7875 : , { SYBEBCHLEN, EXPROGRAM, "host_collen should be greater than or equal to -1\0" }
7876 : , { SYBEBCIS, EXCONSISTENCY, "Attempt to bulk copy an illegally-sized column value to the server\0" }
7877 : , { SYBEBCIT, EXPROGRAM, "It is illegal to use BCP terminators with program variables other than SYBCHAR, "
7878 : "SYBBINARY, SYBTEXT, or SYBIMAGE\0" }
7879 : , { SYBEBCITBLEN, EXPROGRAM, "bcp_init: tblname parameter is too long\0" }
7880 : , { SYBEBCITBNM, EXPROGRAM, "bcp_init: tblname parameter cannot be NULL\0" }
7881 : , { SYBEBCMTXT, EXPROGRAM, "bcp_moretext may be used only when there is at least one text or image column in "
7882 : "the server table\0" }
7883 : , { SYBEBCNL, EXNONFATAL, "Negative length-prefix found in BCP data-file\0" }
7884 : , { SYBEBCNN, EXUSER, "Attempt to bulk copy a NULL value into a Server column "
7885 : "which does not accept null values\0" }
7886 : , { SYBEBCNT, EXUSER, "Attempt to use Bulk Copy with a non-existent Server table\0" }
7887 : , { SYBEBCOR, EXCONSISTENCY, "Attempt to bulk copy an oversized row to the server\0" }
7888 : , { SYBEBCPB, EXPROGRAM, "bcp_bind, bcp_moretext and bcp_sendrow may not be used after bcp_init has been "
7889 : "passed a non-NULL input file name\0" }
7890 : , { SYBEBCPCTYP, EXPROGRAM, "bcp_colfmt: If table_colnum is 0, host_type cannot be 0\0" }
7891 : , { SYBEBCPI, EXPROGRAM, "bcp_init must be called before any other bcp routines\0" }
7892 : , { SYBEBCPN, EXPROGRAM, "bcp_bind, bcp_collen, bcp_colptr, bcp_moretext and bcp_sendrow may be used only "
7893 : "after bcp_init has been called with the copy direction set to DB_IN\0" }
7894 : , { SYBEBCPREC, EXNONFATAL, "Column %1!: Illegal precision value encountered\0%d" }
7895 : , { SYBEBCPREF, EXPROGRAM, "Illegal prefix length. Legal values are -1, 0, 1, 2 or 4\0" }
7896 : , { SYBEBCRE, EXNONFATAL, "I/O error while reading bcp datafile\0" }
7897 : , { SYBEBCRO, EXINFO, "The BCP hostfile '%1!' contains only %2! rows. It was impossible to read the "
7898 : "requested %3! rows\0%s %d %d" }
7899 : , { SYBEBCSA, EXUSER, "The BCP hostfile '%1!' contains only %2! rows. "
7900 : "Skipping all of these rows is not allowed\0%s %d" }
7901 : , { SYBEBCSET, EXCONSISTENCY, "Unknown character-set encountered\0" }
7902 : , { SYBEBCSI, EXPROGRAM, "Host-file columns may be skipped only when copying into the Server\0" }
7903 : , { SYBEBCSNDROW, EXPROGRAM, "bcp_sendrow may not be called unless all text data for the previous row has been "
7904 : "sent using bcp_moretext\0" }
7905 : , { SYBEBCSNTYP, EXPROGRAM, "column number %1!: if varaddr is NULL and varlen greater than 0, the table column "
7906 : "type must be SYBTEXT or SYBIMAGE and the program variable type must be SYBTEXT, "
7907 : "SYBCHAR, SYBIMAGE or SYBBINARY\0%d" }
7908 : , { SYBEBCUC, EXRESOURCE, "bcp: Unable to close host datafile\0" }
7909 : , { SYBEBCUO, EXRESOURCE, "bcp: Unable to open host datafile\0" }
7910 : , { SYBEBCVH, EXPROGRAM, "bcp_exec may be called only after bcp_init has been passed a valid host file\0" }
7911 : , { SYBEBCVLEN, EXPROGRAM, "varlen should be greater than or equal to -1\0" }
7912 : , { SYBEBCWE, EXNONFATAL, "I/O error while writing bcp datafile\0" }
7913 : , { SYBEBDIO, EXPROGRAM, "Bad bulk copy direction. Must be either IN or OUT\0" }
7914 : , { SYBEBEOF, EXNONFATAL, "Unexpected EOF encountered in bcp datafile\0" }
7915 : , { SYBEBIHC, EXPROGRAM, "Incorrect host-column number found in bcp format file\0" }
7916 : , { SYBEBIVI, EXPROGRAM, "bcp_columns, bcp_colfmt and bcp_colfmt_ps may be used only after bcp_init has been "
7917 : "passed a valid input file\0" }
7918 : , { SYBEBNCR, EXPROGRAM, "Attempt to bind user variable to a non-existent compute row\0" }
7919 : , { SYBEBNUM, EXPROGRAM, "Bad numbytes parameter passed to dbstrcpy\0" }
7920 : , { SYBEBPKS, EXPROGRAM, "In DBSETLPACKET, the packet size parameter must be between 0 and 999999\0" }
7921 : , { SYBEBPREC, EXPROGRAM, "Illegal precision specified\0" }
7922 : , { SYBEBPROBADDEF, EXCONSISTENCY, "bcp protocol error: illegal default column id received\0" }
7923 : , { SYBEBPROCOL, EXCONSISTENCY, "bcp protocol error: returned column count differs from the actual number of "
7924 : "columns received\0" }
7925 : , { SYBEBPRODEF, EXCONSISTENCY, "bcp protocol error: expected default information and got none\0" }
7926 : , { SYBEBPRODEFID, EXCONSISTENCY, "bcp protocol error: default column id and actual column id are not same\0" }
7927 : , { SYBEBPRODEFTYP, EXCONSISTENCY, "bcp protocol error: default value datatype differs from column datatype\0" }
7928 : , { SYBEBPROEXTDEF, EXCONSISTENCY, "bcp protocol error: more than one row of default information received\0" }
7929 : , { SYBEBPROEXTRES, EXCONSISTENCY, "bcp protocol error: unexpected set of results received\0" }
7930 : , { SYBEBPRONODEF, EXCONSISTENCY, "bcp protocol error: default value received for column that does not have default\0" }
7931 : , { SYBEBPRONUMDEF, EXCONSISTENCY, "bcp protocol error: expected number of defaults differs from the actual number of "
7932 : "defaults received\0" }
7933 : , { SYBEBRFF, EXRESOURCE, "I/O error while reading bcp format file\0" }
7934 : , { SYBEBSCALE, EXPROGRAM, "Illegal scale specified\0" }
7935 : , { SYBEBTMT, EXPROGRAM, "Attempt to send too much text data via the bcp_moretext call\0" }
7936 : , { SYBEBTOK, EXCOMM, "Bad token from the server: Datastream processing out of sync\0" }
7937 : , { SYBEBTYP, EXPROGRAM, "Unknown bind type passed to DB-Library function\0" }
7938 : , { SYBEBTYPSRV, EXPROGRAM, "Datatype is not supported by the server\0" }
7939 : , { SYBEBUCE, EXRESOURCE, "bcp: Unable to close error file\0" }
7940 : , { SYBEBUCF, EXPROGRAM, "bcp: Unable to close format file\0" }
7941 : , { SYBEBUDF, EXPROGRAM, "bcp: Unrecognized datatype found in format file\0" }
7942 : , { SYBEBUFF, EXPROGRAM, "bcp: Unable to create format file\0" }
7943 : , { SYBEBUFL, EXCONSISTENCY, "DB-Library internal error-send buffer length corrupted\0" }
7944 : , { SYBEBUOE, EXRESOURCE, "bcp: Unable to open error file\0" }
7945 : , { SYBEBUOF, EXPROGRAM, "bcp: Unable to open format file\0" }
7946 : , { SYBEBWEF, EXNONFATAL, "I/O error while writing bcp error file\0" }
7947 : , { SYBEBWFF, EXRESOURCE, "I/O error while writing bcp format file\0" }
7948 : , { SYBECAP, EXCOMM, "DB-Library capabilities not accepted by the Server\0" }
7949 : , { SYBECAPTYP, EXCOMM, "Unexpected capability type in CAPABILITY datastream\0" }
7950 : , { SYBECDNS, EXCONSISTENCY, "Datastream indicates that a compute column is derived from a non-existent select "
7951 : "list member\0" }
7952 : , { SYBECDOMAIN, EXCONVERSION, "Source field value is not within the domain of legal values\0" }
7953 : , { SYBECINTERNAL, EXCONVERSION, "Internal Conversion error\0" }
7954 : , { SYBECLOS, EXCOMM, "Error in closing network connection\0" }
7955 : , { SYBECLPR, EXCONVERSION, "Data conversion resulted in loss of precision\0" }
7956 : , { SYBECNOR, EXPROGRAM, "Column number out of range\0" }
7957 : , { SYBECNOV, EXCONVERSION, "Attempt to set variable to NULL resulted in overflow\0" }
7958 : , { SYBECOFL, EXCONVERSION, "Data conversion resulted in overflow\0" }
7959 : , { SYBECONN, EXCOMM, "Unable to connect: TDS server is unavailable or does not exist\0" }
7960 : , { SYBECRNC, EXPROGRAM, "The current row is not a result of compute clause %1!, so it is illegal to attempt "
7961 : "to extract that data from this row\0%d" }
7962 : , { SYBECRSAGR, EXPROGRAM, "Aggregate functions are not allowed in a cursor statement\0" }
7963 : , { SYBECRSBROL, EXPROGRAM, "Backward scrolling cannot be used in a forward scrolling cursor\0" }
7964 : , { SYBECRSBSKEY, EXPROGRAM, "Keyset cannot be scrolled backward in mixed cursors with a previous fetch type\0" }
7965 : , { SYBECRSBUFR, EXPROGRAM, "Row buffering should not be turned on when using cursor APIs\0" }
7966 : , { SYBECRSDIS, EXPROGRAM, "Cursor statement contains one of the disallowed phrases compute, union, for "
7967 : "browse, or select into\0" }
7968 : , { SYBECRSFLAST, EXPROGRAM, "Fetch type LAST requires fully keyset driven cursors\0" }
7969 : , { SYBECRSFRAND, EXPROGRAM, "Fetch types RANDOM and RELATIVE can only be used within the keyset of keyset "
7970 : "driven cursors\0" }
7971 : , { SYBECRSFROWN, EXPROGRAM, "Row number to be fetched is outside valid range\0" }
7972 : , { SYBECRSFTYPE, EXRESOURCE, "Unknown fetch type\0" }
7973 : , { SYBECRSINV, EXPROGRAM, "Invalid cursor statement\0" }
7974 : , { SYBECRSINVALID, EXRESOURCE, "The cursor handle is invalid\0" }
7975 : , { SYBECRSMROWS, EXRESOURCE, "Multiple rows are returned, only one is expected while retrieving dbname\0" }
7976 : , { SYBECRSNOBIND, EXPROGRAM, "Cursor bind must be called prior to dbcursor invocation\0" }
7977 : , { SYBECRSNOCOUNT, EXPROGRAM, "The DBNOCOUNT option should not be turned on "
7978 : "when doing updates or deletes with dbcursor\0" }
7979 : , { SYBECRSNOFREE, EXPROGRAM, "The DBNOAUTOFREE option should not be turned on when using cursor APIs\0" }
7980 : , { SYBECRSNOIND, EXPROGRAM, "One of the tables involved in the cursor statement does not have a unique index\0" }
7981 : , { SYBECRSNOKEYS, EXRESOURCE, "The entire keyset must be defined for KEYSET type cursors\0" }
7982 : , { SYBECRSNOLEN, EXRESOURCE, "No unique index found\0" }
7983 : , { SYBECRSNOPTCC, EXRESOURCE, "No OPTCC was found\0" }
7984 : , { SYBECRSNORDER, EXRESOURCE, "The order of clauses must be from, where, and order by\0" }
7985 : , { SYBECRSNORES, EXPROGRAM, "Cursor statement generated no results\0" }
7986 : , { SYBECRSNROWS, EXRESOURCE, "No rows returned, at least one is expected\0" }
7987 : , { SYBECRSNOTABLE, EXRESOURCE, "Table name is NULL\0" }
7988 : , { SYBECRSNOUPD, EXPROGRAM, "Update or delete operation did not affect any rows\0" }
7989 : , { SYBECRSNOWHERE, EXPROGRAM, "A WHERE clause is not allowed in a cursor update or insert\0" }
7990 : , { SYBECRSNUNIQUE, EXRESOURCE, "No unique keys associated with this view\0" }
7991 : , { SYBECRSORD, EXPROGRAM, "Only fully keyset driven cursors can have order by, group by, or having phrases\0" }
7992 : , { SYBECRSRO, EXPROGRAM, "Data locking or modifications cannot be made in a READONLY cursor\0" }
7993 : , { SYBECRSSET, EXPROGRAM, "A SET clause is required for a cursor update or insert\0" }
7994 : , { SYBECRSTAB, EXPROGRAM, "Table name must be determined in operations involving data locking or modifications\0" }
7995 : , { SYBECRSVAR, EXRESOURCE, "There is no valid address associated with this bind\0" }
7996 : , { SYBECRSVIEW, EXPROGRAM, "A view cannot be joined with another table or a view in a cursor statement\0" }
7997 : , { SYBECRSVIIND, EXPROGRAM, "The view used in the cursor statement does not include all the unique index "
7998 : "columns of the underlying tables\0" }
7999 : , { SYBECRSUPDNB, EXPROGRAM, "Update or insert operations cannot use bind variables when binding type is NOBIND\0" }
8000 : , { SYBECRSUPDTAB, EXPROGRAM, "Update or insert operations using bind variables require single table cursors\0" }
8001 : , { SYBECSYN, EXCONVERSION, "Attempt to convert data stopped by syntax error in source field\0" }
8002 : , { SYBECUFL, EXCONVERSION, "Data conversion resulted in underflow\0" }
8003 : , { SYBECWLL, EXPROGRAM, "Attempt to set column width less than 1\0" }
8004 : , { SYBEDBPS, EXRESOURCE, "Maximum number of DBPROCESSes already allocated\0" }
8005 : , { SYBEDDNE, EXCOMM, "DBPROCESS is dead or not enabled\0" }
8006 : , { SYBEDIVZ, EXUSER, "Attempt to divide by $0.00 in function %1!\0%s" }
8007 : , { SYBEDNTI, EXPROGRAM, "Attempt to use dbtxtsput to put a new text timestamp into a column whose datatype "
8008 : "is neither SYBTEXT nor SYBIMAGE\0" }
8009 : , { SYBEDPOR, EXPROGRAM, "Out-of-range datepart constant\0" }
8010 : , { SYBEDVOR, EXPROGRAM, "Day values must be between 1 and 7\0" }
8011 : , { SYBEECAN, EXINFO, "Attempted to cancel unrequested event notification\0" }
8012 : , { SYBEEINI, EXINFO, "Must call dbreginit before dbregexec\0" }
8013 : , { SYBEETD, EXPROGRAM, "Failure to send the expected amount of TEXT or IMAGE data via dbmoretext\0" }
8014 : , { SYBEEUNR, EXCOMM, "Unsolicited event notification received\0" }
8015 : , { SYBEEVOP, EXINFO, "Called dbregwatch with a bad options parameter\0" }
8016 : , { SYBEEVST, EXINFO, "Must initiate a transaction before calling dbregparam\0" }
8017 : , { SYBEFCON, EXCOMM, "TDS server connection failed\0" }
8018 : , { SYBEFRES, EXFATAL, "Challenge-Response function failed\0" }
8019 : , { SYBEFSHD, EXRESOURCE, "Error in attempting to find the Sybase home directory\0" }
8020 : , { SYBEFUNC, EXPROGRAM, "Functionality not supported at the specified version level\0" }
8021 : , { SYBEICN, EXPROGRAM, "Invalid computeid or compute column number\0" }
8022 : , { SYBEIDCL, EXCONSISTENCY, "Illegal datetime column length returned by TDS server. Legal datetime lengths "
8023 : "are 4 and 8 bytes\0" }
8024 : , { SYBEIDECCL, EXCONSISTENCY, "Invalid decimal column length returned by the server\0" }
8025 : , { SYBEIFCL, EXCONSISTENCY, "Illegal floating-point column length returned by TDS server. Legal "
8026 : "floating-point lengths are 4 and 8 bytes\0" }
8027 : , { SYBEIFNB, EXPROGRAM, "Illegal field number passed to bcp_control\0" }
8028 : , { SYBEIICL, EXCONSISTENCY, "Illegal integer column length returned by TDS server. Legal integer lengths "
8029 : "are 1, 2, and 4 bytes\0" }
8030 : , { SYBEIMCL, EXCONSISTENCY, "Illegal money column length returned by TDS server. Legal money lengths are 4 "
8031 : "and 8 bytes\0" }
8032 : , { SYBEINLN, EXUSER, "Interface file: unexpected end-of-line\0" }
8033 : , { SYBEINTF, EXUSER, "Server name not found in configuration files\0" }
8034 : , { SYBEINUMCL, EXCONSISTENCY, "Invalid numeric column length returned by the server\0" }
8035 : , { SYBEIPV, EXINFO, "%1! is an illegal value for the %2! parameter of %3!\0%d %s %s" }
8036 : , { SYBEISOI, EXCONSISTENCY, "International Release: Invalid sort-order information found\0" }
8037 : , { SYBEISRVPREC, EXCONSISTENCY, "Illegal precision value returned by the server\0" }
8038 : , { SYBEISRVSCL, EXCONSISTENCY, "Illegal scale value returned by the server\0" }
8039 : , { SYBEITIM, EXPROGRAM, "Illegal timeout value specified\0" }
8040 : , { SYBEIVERS, EXPROGRAM, "Illegal version level specified\0" }
8041 : , { SYBEKBCI, EXINFO, "1000 rows sent to the server\0" }
8042 : , { SYBEKBCO, EXINFO, "1000 rows successfully bulk copied to host file\0" }
8043 : , { SYBEMEM, EXRESOURCE, "Unable to allocate sufficient memory\0" }
8044 : , { SYBEMOV, EXUSER, "Money arithmetic resulted in overflow in function %1!\0%s" }
8045 : , { SYBEMPLL, EXUSER, "Attempt to set maximum number of DBPROCESSes lower than 1\0" }
8046 : , { SYBEMVOR, EXPROGRAM, "Month values must be between 1 and 12\0" }
8047 : , { SYBENBUF, EXINFO, "Called dbsendpassthru with a NULL buf parameter\0" }
8048 : , { SYBENBVP, EXPROGRAM, "Cannot pass dbsetnull a NULL bindval pointer\0" }
8049 : , { SYBENDC, EXPROGRAM, "Cannot have negative component in date in numeric form\0" }
8050 : , { SYBENDTP, EXPROGRAM, "Called dbdatecrack with NULL datetime parameter\0" }
8051 : , { SYBENEG, EXCOMM, "Negotiated login attempt failed\0" }
8052 : , { SYBENHAN, EXINFO, "Called dbrecvpassthru with a NULL handle parameter\0" }
8053 : , { SYBENMOB, EXPROGRAM, "No such member of order by clause\0" }
8054 : , { SYBENOEV, EXINFO, "DBPOLL can not be called when registered procedure notifications have been disabled\0" }
8055 : , { SYBENPRM, EXPROGRAM, "NULL parameter not allowed for this dboption\0" }
8056 : , { SYBENSIP, EXPROGRAM, "Negative starting index passed to dbstrcpy\0" }
8057 : , { SYBENTLL, EXUSER, "Name too long for LOGINREC field\0" }
8058 : , { SYBENTTN, EXPROGRAM, "Attempt to use dbtxtsput to put a new text timestamp into a non-existent data row\0" }
8059 : , { SYBENULL, EXINFO, "NULL DBPROCESS pointer passed to DB-Library\0" }
8060 : , { SYBENULP, EXPROGRAM, "Called %1! with parameter %2! NULL\0%s %d" }
8061 : , { SYBENXID, EXNONFATAL, "The Server did not grant us a distributed-transaction ID\0" }
8062 : , { SYBEONCE, EXPROGRAM, "Function can be called only once\0" }
8063 : , { SYBEOOB, EXCOMM, "Error in sending out-of-band data to the server\0" }
8064 : , { SYBEOPIN, EXNONFATAL, "Could not open interface file\0" }
8065 : , { SYBEOPNA, EXNONFATAL, "Option is not available with current server\0" }
8066 : , { SYBEOREN, EXINFO, "International Release: Warning: an out-of-range error-number was encountered in "
8067 : "dblib.loc. The maximum permissible error-number is defined as DBERRCOUNT in sybdb.h\0" }
8068 : , { SYBEORPF, EXUSER, "Attempt to set remote password would overflow "
8069 : "the login record's remote password field\0" }
8070 : , { SYBEPOLL, EXINFO, "There is already an active dbpoll\0" }
8071 : , { SYBEPRTF, EXINFO, "dbtracestring may only be called from a printfunc\0" }
8072 : , { SYBEPWD, EXUSER, "Login incorrect\0" }
8073 : , { SYBERDCN, EXCONVERSION, "Requested data conversion does not exist\0" }
8074 : , { SYBERDNR, EXPROGRAM, "Attempt to retrieve data from a non-existent row\0" }
8075 : , { SYBEREAD, EXCOMM, "Read from the server failed\0" }
8076 : , { SYBERESP, EXPROGRAM, "Response function address passed to dbresponse must be non-NULL\0" }
8077 : , { SYBERPCS, EXINFO, "Must call dbrpcinit before dbrpcparam or dbrpcsend\0" }
8078 : , { SYBERPIL, EXPROGRAM, "It is illegal to pass -1 to dbrpcparam for the datalen of parameters which are of "
8079 : "type SYBCHAR, SYBVARCHAR, SYBBINARY, or SYBVARBINARY\0" }
8080 : , { SYBERPNA, EXNONFATAL, "The RPC facility is available only when using a server whose version number is 4.0 "
8081 : "or later\0" }
8082 : , { SYBERPND, EXPROGRAM, "Attempt to initiate a new TDS server operation with results pending\0" }
8083 : , { SYBERPNULL, EXPROGRAM, "value parameter for dbrpcparam can be NULL, only if the datalen parameter is 0\0" }
8084 : , { SYBERPTXTIM, EXPROGRAM, "RPC parameters cannot be of type text or image\0" }
8085 : , { SYBERPUL, EXPROGRAM, "When passing a SYBINTN, SYBDATETIMN, SYBMONEYN, or SYBFLTN parameter via "
8086 : "dbrpcparam, it is necessary to specify the parameter's maximum or actual length so "
8087 : "that DB-Library can recognize it as a SYINT1, SYBINT2, SYBINT4, SYBMONEY, SYBMONEY4, "
8088 : "and so on\0" }
8089 : , { SYBERTCC, EXPROGRAM, "dbreadtext may not be used to receive the results of a query that contains a "
8090 : "COMPUTE clause\0" }
8091 : , { SYBERTSC, EXPROGRAM, "dbreadtext may be used only to receive the results of a query that contains a "
8092 : "single result column\0" }
8093 : , { SYBERXID, EXNONFATAL, "The Server did not recognize our distributed-transaction ID\0" }
8094 : , { SYBESECURE, EXPROGRAM, "Secure SQL Server function not supported in this version\0" }
8095 : , { SYBESEFA, EXPROGRAM, "DBSETNOTIFS cannot be called if connections are present\0" }
8096 : , { SYBESEOF, EXCOMM, "Unexpected EOF from the server\0" }
8097 : , { SYBESFOV, EXPROGRAM, "International Release: dbsafestr overflowed its destination buffer\0" }
8098 : , { SYBESMSG, EXSERVER, "General TDS server error: Check messages from the server\0" }
8099 : , { SYBESOCK, EXCOMM, "Unable to open socket\0" }
8100 : , { SYBESPID, EXPROGRAM, "Called dbspid with a NULL dbproc\0" }
8101 : , { SYBESYNC, EXCOMM, "Read attempted while out of synchronization with TDS server\0" }
8102 : , { SYBETEXS, EXINFO, "Called dbmoretext with a bad size parameter\0" }
8103 : , { SYBETIME, EXTIME, "TDS server connection timed out\0" }
8104 : , { SYBETMCF, EXPROGRAM, "Attempt to install too many custom formats via dbfmtinstall\0" }
8105 : , { SYBETMTD, EXPROGRAM, "Attempt to send too much TEXT data via the dbmoretext call\0" }
8106 : , { SYBETPAR, EXPROGRAM, "No SYBTEXT or SYBIMAGE parameters were defined\0" }
8107 : , { SYBETPTN, EXUSER, "Syntax error: only two periods are permitted in table names\0" }
8108 : , { SYBETRAC, EXINFO, "Attempted to turn off a trace flag that was not on\0" }
8109 : , { SYBETRAN, EXINFO, "DBPROCESS is being used for another transaction\0" }
8110 : , { SYBETRAS, EXINFO, "DB-Library internal error - trace structure not found\0" }
8111 : , { SYBETRSN, EXINFO, "Bad numbytes parameter passed to dbtracestring\0" }
8112 : , { SYBETSIT, EXINFO, "Attempt to call dbtsput with an invalid timestamp\0" }
8113 : , { SYBETTS, EXUSER, "The table which bulk copy is attempting to copy to a host file is shorter than the "
8114 : "number of rows which bulk copy was instructed to skip\0" }
8115 : , { SYBETYPE, EXINFO, "Invalid argument type given to Hyper/DB-Library\0" }
8116 : , { SYBEUCPT, EXUSER, "Unrecognized custom-format parameter-type encountered in dbstrbuild\0" }
8117 : , { SYBEUCRR, EXCONSISTENCY, "Internal software error: Unknown connection result reported by dbpasswd\0" }
8118 : , { SYBEUDTY, EXCONSISTENCY, "Unknown datatype encountered\0" }
8119 : , { SYBEUFDS, EXUSER, "Unrecognized format encountered in dbstrbuild\0" }
8120 : , { SYBEUFDT, EXCONSISTENCY, "Unknown fixed-length datatype encountered\0" }
8121 : , { SYBEUHST, EXUSER, "Unknown host machine name\0" }
8122 : , { SYBEUMSG, EXCOMM, "Unknown message-id in MSG datastream\0" }
8123 : , { SYBEUNAM, EXFATAL, "Unable to get current user name from operating system\0" }
8124 : , { SYBEUNOP, EXNONFATAL, "Unknown option passed to dbsetopt\0" }
8125 : , { SYBEUNT, EXUSER, "Unknown network type found in interface file\0" }
8126 : , { SYBEURCI, EXRESOURCE, "International Release: Unable to read copyright information from the DB-Library "
8127 : "localization file\0" }
8128 : , { SYBEUREI, EXRESOURCE, "International Release: Unable to read error information from the DB-Library "
8129 : "localization file\0" }
8130 : , { SYBEUREM, EXRESOURCE, "International Release: Unable to read error mnemonic from the DB-Library "
8131 : "localization file\0" }
8132 : , { SYBEURES, EXRESOURCE, "International Release: Unable to read error string from the DB-Library "
8133 : "localization file. 401 Error severities\0" }
8134 : , { SYBEURMI, EXRESOURCE, "International Release: Unable to read money-format information from the DB-Library "
8135 : "localization file\0" }
8136 : , { SYBEUSCT, EXCOMM, "Unable to set communications timer\0" }
8137 : , { SYBEUTDS, EXCOMM, "Unrecognized TDS version received from the server\0" }
8138 : , { SYBEUVBF, EXPROGRAM, "Attempt to read an unknown version of bcp format file\0" }
8139 : , { SYBEUVDT, EXCONSISTENCY, "Unknown variable-length datatype encountered\0" }
8140 : , { SYBEVDPT, EXUSER, "For bulk copy, all variable-length data must have either a length-prefix or a "
8141 : "terminator specified\0" }
8142 : , { SYBEWAID, EXCONSISTENCY, "DB-Library internal error: ALTFMT following ALTNAME has wrong id\0" }
8143 : , { SYBEWRIT, EXCOMM, "Write to the server failed\0" }
8144 : , { SYBEXOCI, EXNONFATAL, "International Release: A character-set translation overflowed its destination "
8145 : "buffer while using bcp to copy data from a host-file to the server\0" }
8146 : , { SYBEXTDN, EXPROGRAM, "Warning: the xlt_todisp parameter to dbfree_xlate was NULL. The space associated "
8147 : "with the xlt_tosrv parameter has been freed\0" }
8148 : , { SYBEXTN, EXPROGRAM, "The xlt_tosrv and xlt_todisp parameters to dbfree_xlate were NULL\0" }
8149 : , { SYBEXTSN, EXPROGRAM, "Warning: the xlt_tosrv parameter to dbfree_xlate was NULL. The space associated "
8150 : "with the xlt_todisp parameter has been freed\0" }
8151 : , { SYBEZTXT, EXINFO, "Attempt to send zero length TEXT or IMAGE to dataserver via dbwritetext\0" }
8152 : , { SYBECOLSIZE, EXINFO, "Invalid column information structure size\0" }
8153 : };
8154 :
8155 : /** \internal
8156 : * \ingroup dblib_internal
8157 : * \brief Call client-installed error handler
8158 : *
8159 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
8160 : * \param msgno identifies the error message to be passed to the client's handler.
8161 : * \param errnum identifies the OS error (errno), if any. Use 0 if not applicable.
8162 : * \returns the handler's return code, subject to correction and adjustment for vendor style:
8163 : * - INT_CANCEL The db-lib function that encountered the error will return FAIL.
8164 : * - INT_TIMEOUT The db-lib function will cancel the operation and return FAIL. \a dbproc remains useable.
8165 : * - INT_CONTINUE The db-lib function will retry the operation.
8166 : * \remarks
8167 : * The client-installed handler may also return INT_EXIT. If Sybase semantics are used, this function notifies
8168 : * the user and calls exit(3). If Microsoft semantics are used, this function returns INT_CANCEL.
8169 : *
8170 : * If the client-installed handler returns something other than these four INT_* values, or returns timeout-related
8171 : * value for anything but SYBETIME, it's treated here as INT_EXIT (see above).
8172 : *
8173 : * Instead of sprinkling error text all over db-lib, we consolidate it here,
8174 : * where it can be translated (one day), and where it can be mapped to the TDS error number.
8175 : * The libraries don't use consistent error numbers or messages, so when libtds has to emit
8176 : * an error message, it can't include the text. It can pass its error number to a client-library
8177 : * function, which will interpret it, add the text, call the application's installed handler
8178 : * (if any) and return the handler's return code back to the caller.
8179 : *
8180 : * The call stack may look something like this:
8181 : *
8182 : * -# application
8183 : * -# db-lib function (encounters error)
8184 : * -# dbperror
8185 : * -# error handler (installed by application)
8186 : *
8187 : * The error handling in this case is unambiguous: the caller invokes this function, the client's handler returns its
8188 : * instruction, which the caller receives. Quite often the caller will get INT_CANCEL, in which case it should put its
8189 : * house in order and return FAIL.
8190 : *
8191 : * The call stack may otherwise look something like this:
8192 : *
8193 : * -# application
8194 : * -# db-lib function
8195 : * -# libtds function (encounters error)
8196 : * -# _dblib_handle_err_message
8197 : * -# dbperror
8198 : * -# error handler (installed by application)
8199 : *
8200 : * Because different client libraries specify their handler semantics differently,
8201 : * and because libtds doesn't know which client library is in charge of any given connection, it cannot interpret the
8202 : * raw return code from a db-lib error handler. For these reasons,
8203 : * libtds calls _dblib_handle_err_message, which translates between libtds and db-lib semantics.
8204 : * \sa dberrhandle(), _dblib_handle_err_message().
8205 : */
8206 : int
8207 280 : dbperror(DBPROCESS *dbproc, DBINT msgno, long errnum, ...)
8208 : {
8209 : static const char int_exit_text[] = "FreeTDS: db-lib: exiting because client error handler returned %s for msgno %d\n";
8210 : static const char int_invalid_text[] = "%s (%d) received from client-installed error handler for nontimeout for error %d."
8211 : " Treating as INT_EXIT\n";
8212 : static const DBLIB_ERROR_MESSAGE default_message = { 0, EXCONSISTENCY, "unrecognized msgno" };
8213 280 : DBLIB_ERROR_MESSAGE constructed_message = { 0, EXCONSISTENCY, NULL };
8214 280 : const DBLIB_ERROR_MESSAGE *msg = &default_message;
8215 :
8216 280 : int i, rc = INT_CANCEL;
8217 280 : const char *os_msgtext = strerror((int) errnum);
8218 280 : const char *rc_name = "logic error";
8219 : char rc_buf[16];
8220 :
8221 280 : tdsdump_log(TDS_DBG_FUNC, "dbperror(%p, %d, %ld)\n", dbproc, msgno, errnum); /* dbproc can be NULL */
8222 :
8223 : #ifdef _WIN32
8224 : /*
8225 : * Unfortunately MinGW uses the "old" msvcrt.dll (Visual C++ 2005 uses
8226 : * a newer version) which does not set errno when allocation functions
8227 : * cannot allocate memory, so we do it for them.
8228 : */
8229 : if (msgno == SYBEMEM)
8230 : errnum = ENOMEM;
8231 : #endif
8232 :
8233 280 : if (os_msgtext == NULL)
8234 0 : os_msgtext = "no OS error";
8235 :
8236 280 : assert(_dblib_err_handler != NULL); /* always installed by dbinit() or dberrhandle() */
8237 :
8238 : /* look up the error message */
8239 95540 : for (i = 0; i < TDS_VECTOR_SIZE(dblib_error_messages); i++) {
8240 : const char *ptext, *pformats;
8241 :
8242 47910 : if (dblib_error_messages[i].msgno != msgno)
8243 47630 : continue;
8244 :
8245 : /*
8246 : * See if the message has placeholders. If so, build a message string on the heap.
8247 : * The presence of placeholders is indicated by the existence of a "string after the string",
8248 : * i.e., a format string (for dbstrbuild) after a null "terminator" in the message.
8249 : * On error -- can't allocate, can't build the string -- give up and call the client handler anyway.
8250 : */
8251 280 : ptext = dblib_error_messages[i].msgtext;
8252 280 : pformats = ptext + strlen(ptext) + 1;
8253 280 : msg = &dblib_error_messages[i];
8254 280 : assert(*(pformats - 1) == '\0');
8255 280 : if (*pformats != '\0') {
8256 : va_list ap;
8257 10 : int result_len, len = 2 * (int) strlen(ptext);
8258 10 : char *buffer = tds_new0(char, len);
8259 :
8260 10 : if (buffer == NULL)
8261 : break;
8262 10 : va_start(ap, errnum);
8263 10 : rc = tds_vstrbuild(buffer, len, &result_len, ptext, TDS_NULLTERM, pformats, TDS_NULLTERM, ap);
8264 10 : buffer[result_len] = '\0';
8265 10 : va_end(ap);
8266 10 : if (TDS_FAILED(rc)) {
8267 0 : free(buffer);
8268 0 : break;
8269 : }
8270 10 : constructed_message.msgtext = buffer;
8271 10 : constructed_message.severity = msg->severity;
8272 10 : msg = &constructed_message;
8273 : }
8274 : break;
8275 : }
8276 :
8277 280 : if (dbproc && dbproc->tds_socket && dbproc->tds_socket->login) {
8278 0 : DSTR server_name_dstr = dbproc->tds_socket->login->server_name;
8279 0 : if (!tds_dstr_isempty(&server_name_dstr)) {
8280 0 : char * buffer = NULL;
8281 0 : if (asprintf(&buffer, "%s (%s)", msg->msgtext,
8282 : tds_dstr_cstr(&server_name_dstr)) >= 0) {
8283 0 : free((char*) constructed_message.msgtext);
8284 0 : constructed_message.msgtext = buffer;
8285 0 : constructed_message.severity = msg->severity;
8286 0 : msg = &constructed_message;
8287 : }
8288 : }
8289 : }
8290 :
8291 280 : tdsdump_log(TDS_DBG_FUNC, "dbperror: Calling dblib_err_handler with msgno = %d; msg->msgtext = \"%s\"\n",
8292 : msgno, msg->msgtext);
8293 :
8294 : /* call the error handler */
8295 280 : rc = (*_dblib_err_handler)(dbproc, msg->severity, msgno, (int) errnum, (char*) msg->msgtext, (char*) os_msgtext);
8296 280 : switch (rc) {
8297 : case INT_EXIT:
8298 : rc_name = "INT_EXIT";
8299 : break;
8300 40 : case INT_CONTINUE:
8301 40 : rc_name = "INT_CONTINUE";
8302 40 : break;
8303 220 : case INT_CANCEL:
8304 220 : rc_name = "INT_CANCEL";
8305 220 : break;
8306 20 : case INT_TIMEOUT:
8307 20 : rc_name = "INT_TIMEOUT";
8308 20 : break;
8309 0 : default:
8310 0 : rc_name = "invalid";
8311 0 : break;
8312 : }
8313 280 : tdsdump_log(TDS_DBG_FUNC, "dbperror: dblib_err_handler for msgno = %d; msg->msgtext = \"%s\" -- returns %d (%s)\n",
8314 : msgno, msg->msgtext, rc, rc_name);
8315 :
8316 : /* we're done with the dynamic string now. */
8317 280 : free((char*) constructed_message.msgtext);
8318 :
8319 : /* Timeout return codes are errors for non-timeout conditions. */
8320 280 : if (msgno != SYBETIME) {
8321 210 : switch (rc) {
8322 0 : case INT_CONTINUE:
8323 0 : tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "INT_CONTINUE", rc, msgno);
8324 : rc = INT_EXIT;
8325 : break;
8326 0 : case INT_TIMEOUT:
8327 0 : tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "INT_TIMEOUT", rc, msgno);
8328 : rc = INT_EXIT;
8329 : break;
8330 : default:
8331 : break;
8332 : }
8333 70 : }
8334 :
8335 : /*
8336 : * Sybase exits on INT_EXIT; Microsoft converts to INT_CANCEL.
8337 : * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dblibc/dbc_pdc04c_6v39.asp
8338 : */
8339 280 : switch (rc) {
8340 40 : case INT_CONTINUE:
8341 : /* Microsoft does not define INT_TIMEOUT. Instead, two consecutive INT_CONTINUEs yield INT_CANCEL. */
8342 40 : if (dbproc && dbproc->msdblib && ++dbproc->ntimeouts >=2) {
8343 0 : dbproc->ntimeouts = 0;
8344 0 : rc = INT_CANCEL;
8345 : } /* fall through */
8346 : case INT_CANCEL:
8347 : case INT_TIMEOUT:
8348 : return rc; /* normal case */
8349 : break;
8350 0 : default:
8351 0 : sprintf(rc_buf, "%d", rc);
8352 0 : rc_name = rc_buf;
8353 0 : tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "Invalid return code", rc, msgno);
8354 : /* fall through */
8355 : case INT_EXIT:
8356 0 : if (dbproc && dbproc->msdblib) {
8357 : /* Microsoft behavior */
8358 : return INT_CANCEL;
8359 : }
8360 : /* fprintf(stderr, int_exit_text, rc_name, msgno); */
8361 0 : tdsdump_log(TDS_DBG_SEVERE, int_exit_text, rc_name, msgno);
8362 : break;
8363 : }
8364 0 : exit(EXIT_FAILURE);
8365 : return rc; /* not reached */
8366 : }
8367 :
|