File: mssql_basic.c

package info (click to toggle)
libopendbx 1.4.6-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,024 kB
  • sloc: sh: 10,899; ansic: 7,284; xml: 1,837; cpp: 1,696; makefile: 316; sed: 16
file content (807 lines) | stat: -rw-r--r-- 15,829 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 *  OpenDBX - A simple but extensible database abstraction layer
 *  Copyright (C) 2006-2007 Norbert Sendetzky and others
 *
 *  Distributed under the terms of the GNU Library General Public Licence
 * version 2 or (at your option) any later version.
 */


#define DBLIB_UNIMPLEMENTED

#include "mssql_basic.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



/*
 *  Declaration of MSSQL capabilities
 */

struct odbx_basic_ops mssql_odbx_basic_ops = {
	.init = mssql_odbx_init,
	.bind = mssql_odbx_bind,
	.unbind = mssql_odbx_unbind,
	.finish = mssql_odbx_finish,
	.get_option = mssql_odbx_get_option,
	.set_option = mssql_odbx_set_option,
	.error = mssql_odbx_error,
	.error_type = mssql_odbx_error_type,
	.escape = mssql_odbx_escape,
	.query = mssql_odbx_query,
	.result = mssql_odbx_result,
	.result_finish = mssql_odbx_result_finish,
	.rows_affected = mssql_odbx_rows_affected,
	.row_fetch = mssql_odbx_row_fetch,
	.column_count = mssql_odbx_column_count,
	.column_name = mssql_odbx_column_name,
	.column_type = mssql_odbx_column_type,
	.field_length = mssql_odbx_field_length,
	.field_value = mssql_odbx_field_value,
};



/*
 *  Private mssql error messages
 */

static const char* mssql_odbx_errmsg[] = {
	gettext_noop("Connecting to server failed")
};



/*
 *  ODBX basic operations
 *  MSSQL (dblib) style
 */

static int mssql_odbx_init( odbx_t* handle, const char* host, const char* port )
{
	int len;
	struct tdsconn* tc;


	if( host == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	handle->aux = NULL;
	handle->generic = NULL;

	if( dbinit() == FAIL )
	{
		return -ODBX_ERR_NOMEM;
	}

	dbmsghandle( mssql_msg_handler );
	dberrhandle( mssql_err_handler );

	if( ( tc = (struct tdsconn*) malloc( sizeof( struct tdsconn ) ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	tc->msg = 0;
	tc->errtype = 0;
	tc->firstresult = 0;

	len = strlen( host ) + 1;

	if( ( tc->host = malloc( len ) ) == NULL )
	{
		free( tc );
		return -ODBX_ERR_NOMEM;
	}

	memcpy( tc->host, host, len );

	if( ( tc->login = dblogin() ) == NULL )
	{
		free( tc->host );
		free( tc );

		return -ODBX_ERR_NOMEM;
	}

	DBSETLHOST( tc->login, (char*) host );

	handle->aux = (void*) tc;

	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_bind( odbx_t* handle, const char* database, const char* who, const char* cred, int method )
{
	struct tdsconn* tc = (struct tdsconn*) handle->aux;


	if( tc == NULL ) { return -ODBX_ERR_PARAM; }
	if( method != ODBX_BIND_SIMPLE ) { return -ODBX_ERR_NOTSUP; }

	DBSETLUSER( tc->login, (char*) who );
	DBSETLPWD( tc->login, (char*) cred );
	DBSETLAPP( tc->login, "OpenDBX" );

	if( ( handle->generic = (void*) dbopen( tc->login, tc->host ) ) == NULL )
	{
		memcpy( tc->errmsg, dgettext( "opendbx", mssql_odbx_errmsg[0] ), strlen( dgettext( "opendbx", mssql_odbx_errmsg[0] ) ) + 1 );
		tc->errtype = 1;

		return -ODBX_ERR_BACKEND;
	}

	dbsetuserdata( (DBPROCESS*) handle->generic, (BYTE*) tc );

	if( dbuse( (DBPROCESS*) handle->generic, (char*) database ) == FAIL )
	{
		dbclose( (DBPROCESS*) handle->generic );
		handle->generic = NULL;
		return -ODBX_ERR_BACKEND;
	}

	int err;

	if( ( err = mssql_priv_ansimode( handle ) ) < 0 )
	{
		dbclose( (DBPROCESS*) handle->generic );
		handle->generic = NULL;
		return err;
	}

	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_unbind( odbx_t* handle )
{
	dbclose( (DBPROCESS*) handle->generic );
	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_finish( odbx_t* handle )
{
	if( handle->aux != NULL )
	{
		dbloginfree( (LOGINREC*) ((struct tdsconn*) handle->aux)->login );
		dbexit();

		free( ((struct tdsconn*) handle->aux)->host );
		free( handle->aux );
		handle->aux = NULL;

		return ODBX_ERR_SUCCESS;
	}

	return -ODBX_ERR_PARAM;
}



static int mssql_odbx_get_option( odbx_t* handle, unsigned int option, void* value )
{
	switch( option )
	{
		case ODBX_OPT_API_VERSION:
			*(int*) value = APINUMBER;
			break;
		case ODBX_OPT_TLS:
		case ODBX_OPT_PAGED_RESULTS:
		case ODBX_OPT_COMPRESS:
			*((int*) value) = ODBX_DISABLE;
			break;
		case ODBX_OPT_THREAD_SAFE:
		case ODBX_OPT_MULTI_STATEMENTS:
		case ODBX_OPT_CONNECT_TIMEOUT:
			*((int*) value) = ODBX_ENABLE;
			break;
		default:
			return -ODBX_ERR_OPTION;
	}

	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_set_option( odbx_t* handle, unsigned int option, void* value )
{
	switch( option )
	{
		case ODBX_OPT_API_VERSION:
		case ODBX_OPT_THREAD_SAFE:
			return -ODBX_ERR_OPTRO;
		case ODBX_OPT_TLS:
		case ODBX_OPT_PAGED_RESULTS:
		case ODBX_OPT_COMPRESS:
			return -ODBX_ERR_OPTWR;
		case ODBX_OPT_MULTI_STATEMENTS:
			return ODBX_ERR_SUCCESS;
		case ODBX_OPT_CONNECT_TIMEOUT:
			if( dbsetlogintime( *((int*) value) ) != SUCCEED ) { return ODBX_ERR_OPTWR; }
			break;
		default:
			return -ODBX_ERR_OPTION;
	}

	return ODBX_ERR_SUCCESS;
}



static const char* mssql_odbx_error( odbx_t* handle )
{
	struct tdsconn* aux = (struct tdsconn*) handle->aux;

	if( aux != NULL )
	{
		aux->msg = 0;
		return aux->errmsg;
	}

	return NULL;
}



static int mssql_odbx_error_type( odbx_t* handle )
{
	if( handle->aux != NULL )
	{
		return ((struct tdsconn*) handle->aux)->errtype;
	}

	return -1;
}



static int mssql_odbx_escape( odbx_t* handle, const char* from, unsigned long fromlen, char* to, unsigned long* tolen )
{
	if( tolen == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	if( dbsafestr( (DBPROCESS*) handle->generic, (char*) from, fromlen, to, *tolen, DBSINGLE ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	*tolen = strlen( to );
	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_query( odbx_t* handle, const char* query, unsigned long length )
{
	DBPROCESS* dbproc = (DBPROCESS*) handle->generic;
	struct tdsconn* aux = (struct tdsconn*) handle->aux;


	if( aux == NULL ) { return -ODBX_ERR_HANDLE; }

	if( dbcmd( dbproc, (char*) query ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	if( dbsqlsend( dbproc ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	aux->firstresult = 1;

	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_result( odbx_t* handle, odbx_result_t** result, struct timeval* timeout, unsigned long chunk )
{
	DBPROCESS* dbproc = (DBPROCESS*) handle->generic;
	struct tdsconn* caux = (struct tdsconn*) handle->aux;


	if( caux->firstresult )
	{
		long ms = -1;
		int reason = DBRESULT;
		DBPROCESS* cdbproc;

		if( timeout != NULL ) { ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; }
		if( dbpoll( dbproc, ms, &cdbproc, &reason ) == FAIL ) { return -ODBX_ERR_BACKEND; }
		if( reason != DBRESULT ) { return ODBX_RES_TIMEOUT; }   // timeout

		caux->firstresult = 0;
		if( dbsqlok( dbproc ) == FAIL ) { return -ODBX_ERR_BACKEND; }
	}

	switch( dbresults( dbproc ) )
	{
		case SUCCEED:
			break;
		case NO_MORE_RESULTS:
			return ODBX_RES_DONE; // no more results
		default:
			return -ODBX_ERR_BACKEND;
	}

	if( ( *result = (odbx_result_t*) malloc( sizeof( odbx_result_t ) ) ) == NULL )
	{
		return -ODBX_ERR_NOMEM;
	}

	(*result)->generic = NULL;
	(*result)->aux = NULL;

	if( DBCMDROW( dbproc ) == FAIL )
	{
		return ODBX_RES_NOROWS;   // Not SELECT like query
	}

	if( ( (*result)->aux = malloc( sizeof( struct tdsares ) ) ) == NULL )
	{
		free( (*result)->aux );
		return -ODBX_ERR_NOMEM;
	}

	struct tdsares* aux = (struct tdsares*) (*result)->aux;

	if( ( aux->cols = dbnumcols( (DBPROCESS*) handle->generic ) ) == FAIL )
	{
		free( (*result)->aux );
		free( *result );
		return -ODBX_ERR_BACKEND;
	}

	if( ( (*result)->generic = malloc( aux->cols * sizeof( struct tdsgres ) ) ) == NULL )
	{
		free( (*result)->aux );
		free( *result );
		*result = NULL;

		return -ODBX_ERR_NOMEM;
	}

	struct tdsgres* gres = (struct tdsgres*) (*result)->generic;

	for( int i = 0; i < aux->cols; i++ )
	{
		gres[i].mlen = mssql_priv_collength( (DBPROCESS*) handle->generic, i );
		gres[i].length = 0;
		gres[i].ind = 0;

		if( ( gres[i].value = malloc( gres[i].mlen ) ) == NULL )
		{
			gres[i].mlen = 0;
			mssql_odbx_result_finish( *result );
			return -ODBX_ERR_NOMEM;
		}
	}

	return ODBX_RES_ROWS;   // result is available
}



static int mssql_odbx_result_finish( odbx_result_t* result )
{
	DBINT i, cols = 0;

	if( result->aux != NULL )
	{
		cols = ((struct tdsares*) result->aux)->cols;
		free( result->aux );
		result->aux = NULL;
	}

	if( result->generic != NULL )
	{
		struct tdsgres* gres = (struct tdsgres*) result->generic;

		for( i = 0; i < cols; i++ )
		{
			if( gres[i].value != NULL )
			{
				free( gres[i].value );
				gres[i].value = NULL;
			}
		}

		free( result->generic );
		result->generic = NULL;
	}

	free( result );

	return ODBX_ERR_SUCCESS;
}



static int mssql_odbx_row_fetch( odbx_result_t* result )
{
	if( result->handle == NULL || result->aux == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	DBPROCESS* dbproc = (DBPROCESS*) result->handle->generic;

	switch( dbnextrow( dbproc ) )
	{
		case BUF_FULL:
		case REG_ROW:
			break;
		case NO_MORE_ROWS:
			return ODBX_ROW_DONE;
		case FAIL:
		default:
			return -ODBX_ERR_BACKEND;
	}

	DBINT i, dlen;
	BYTE* data;
	DBDATEREC di;
	struct tdsgres* gres = (struct tdsgres*) result->generic;
	struct tdsares* ares = (struct tdsares*) result->aux;

	for( i = 0; i < ares->cols; i++ )
	{
		if( ( dlen = dbdatlen( dbproc, i+1 ) ) == -1 )
 		{
				return -ODBX_ERR_SIZE;
		}

		if( ( data = dbdata( dbproc, i+1 ) ) == NULL && dlen == 0 )
		{
			gres[i].ind = 1;   // column is NULL
			gres[i].length = 0;
			continue;
		}

		switch( dbcoltype( dbproc, i+1 ) )
		{
			case SYBDATETIME4:
			case SYBDATETIME:
			case SYBDATETIMN:
				if( dbdatecrack( dbproc, &di, (DBDATETIME*) data ) != FAIL )
				{
#ifdef HAVE_LIBSYBDB_MSLIB
					gres[i].length = snprintf( (char*) gres[i].value, gres[i].mlen, "%.4ld-%.2ld-%.2ld %.2ld:%.2ld:%.2ld",
						(long) di.year, (long) di.month+1, (long) di.day, (long) di.hour, (long) di.minute, (long) di.second );
#else
					gres[i].length = snprintf( (char*) gres[i].value, gres[i].mlen, "%.4ld-%.2ld-%.2ld %.2ld:%.2ld:%.2ld",
						(long) di.dateyear, (long) di.datemonth+1, (long) di.datedmonth, (long) di.datehour, (long) di.dateminute, (long) di.datesecond );
#endif
				}
				continue;
		}

		if( gres[i].mlen < dlen + 1 )
		{
			if( ( gres[i].value = realloc( gres[i].value, dlen + 1 ) ) == NULL )
			{
				gres[i].mlen = 0;
				return -ODBX_ERR_NOMEM;
			}
			gres[i].mlen = dlen + 1;
		}

		gres[i].length = dbconvert( dbproc, dbcoltype( dbproc, i+1 ), data, dlen, SYBVARCHAR, gres[i].value, gres[i].mlen );
		gres[i].value[gres[i].length] = 0;
		gres[i].ind = 0;   // column is not NULL
	}

	return ODBX_ROW_NEXT;
}



static uint64_t mssql_odbx_rows_affected( odbx_result_t* result )
{
	if( result->handle != NULL )
	{
		int count;

		if( ( count = DBCOUNT( (DBPROCESS*) result->handle->generic ) ) > 0 )
		{
			return (uint64_t) count;
		}
	}

	return 0;
}



static unsigned long mssql_odbx_column_count( odbx_result_t* result )
{
	if( result->aux != NULL )
	{
		return (unsigned long) ((struct tdsares*) result->aux)->cols;
	}

	return 0;
}



static const char* mssql_odbx_column_name( odbx_result_t* result, unsigned long pos )
{
	if( result->handle != NULL )
	{
		return dbcolname( (DBPROCESS*) result->handle->generic, pos+1 );
	}

	return NULL;
}



static int mssql_odbx_column_type( odbx_result_t* result, unsigned long pos )
{
	if( result->handle == NULL )
	{
		return -ODBX_ERR_PARAM;
	}

	switch( dbcoltype( (DBPROCESS*) result->handle->generic, pos+1 ) )
	{
		case -1:
		case 0:
			return -ODBX_ERR_BACKEND;

		case SYBBIT:
			return ODBX_TYPE_BOOLEAN;
		case SYBINT1:
		case SYBINT2:
			return ODBX_TYPE_SMALLINT;
		case SYBINT4:
			return ODBX_TYPE_INTEGER;
#ifdef SYBINT8
		case SYBINT8:
#endif
		case SYBINTN:
			return ODBX_TYPE_BIGINT;

		case SYBMONEY:
		case SYBMONEY4:
		case SYBMONEYN:
		case SYBNUMERIC:
		case SYBDECIMAL:
			return ODBX_TYPE_DECIMAL;

		case SYBREAL:
			return ODBX_TYPE_REAL;
		case SYBFLT8:
			return ODBX_TYPE_DOUBLE;
		case SYBFLTN:
			return ODBX_TYPE_FLOAT;

		case SYBCHAR:
#ifdef SYBLONGCHAR
		case SYBLONGCHAR:
#endif
			return ODBX_TYPE_CHAR;
		case SYBVARCHAR:
			return ODBX_TYPE_VARCHAR;

		case SYBTEXT:
			return ODBX_TYPE_CLOB;
		case SYBIMAGE:
		case SYBBINARY:
		case SYBVARBINARY:
#ifdef SYBLONGBINARY
		case SYBLONGBINARY:
#endif
			return ODBX_TYPE_BLOB;

		case SYBDATETIME4:
		case SYBDATETIME:
		case SYBDATETIMN:
			return ODBX_TYPE_TIMESTAMP;

		default:
			return ODBX_TYPE_UNKNOWN;
	}
}



static unsigned long mssql_odbx_field_length( odbx_result_t* result, unsigned long pos )
{
	struct tdsares* ares = (struct tdsares*) result->aux;

	if( result->generic != NULL && ares != NULL && pos < ares->cols )
	{
		return (unsigned long) ((struct tdsgres*) result->generic)[pos].length;
	}

	return -ODBX_ERR_PARAM;
}



static const char* mssql_odbx_field_value( odbx_result_t* result, unsigned long pos )
{
	struct tdsgres* gres = (struct tdsgres*) result->generic;
	struct tdsares* ares = (struct tdsares*) result->aux;

	if( gres != NULL && ares != NULL && pos < ares->cols && !gres[pos].ind )
	{
		return (const char*) gres[pos].value;
	}

	return NULL;
}



static int mssql_priv_ansimode( odbx_t* handle )
{
	int err;
	DBPROCESS* dbproc = (DBPROCESS*) handle->generic;


	if( dbsetopt( dbproc, DBDATEFORMAT, "ymd", -1 ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	if( dbcmd( dbproc, "SET QUOTED_IDENTIFIER ON" ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	if( dbsqlexec( dbproc ) == FAIL )
	{
		return -ODBX_ERR_BACKEND;
	}

	while( ( err = dbresults( dbproc ) ) != NO_MORE_RESULTS )
	{
		switch( err )
		{
			case SUCCEED:
				if( DBCMDROW( dbproc ) == SUCCEED )
				{ while( dbnextrow( dbproc ) != NO_MORE_ROWS ); }
				break;
			default:
				return -ODBX_ERR_BACKEND;
		}
	}

	return ODBX_ERR_SUCCESS;
}



static int mssql_priv_collength( DBPROCESS* dbproc, unsigned long pos )
{
	switch( dbcoltype( dbproc, pos+1 ) )
	{
		case -1:
		case 0:
			return 0;

		case SYBBIT:
			return 2;

		case SYBINT1:
			return 5;

		case SYBINT2:
			return 7;

		case SYBINT4:
			return 12;

#ifdef SYBINT8
		case SYBINT8:
#endif
		case SYBINTN:
			return 22;

		case SYBMONEY:
		case SYBMONEY4:
		case SYBMONEYN:
			return 25;

		case SYBNUMERIC:
		case SYBDECIMAL:
			return 80;

		case SYBREAL:
			return 42;

		case SYBFLT8:
		case SYBFLTN:
			return 312;

		case SYBDATETIME4:
		case SYBDATETIME:
		case SYBDATETIMN:
			return 20;

		case SYBCHAR:
		case SYBVARCHAR:
		case SYBTEXT:
		case SYBIMAGE:
		case SYBBINARY:
		case SYBVARBINARY:
			return 32;   // Variable length types, initial size

		default:
			return 12;
	}
}



static int mssql_err_handler( DBPROCESS* dbproc, int severity, int dberr, int oserr, char* dberrstr, char* oserrstr )
{
	struct tdsconn* tc = (struct tdsconn*) dbgetuserdata( dbproc );

	if( tc == NULL || tc->errmsg == NULL )
	{
		fprintf( stderr, "mssql_err_handler(): error = %s\n", dberrstr );
		if( oserr != DBNOERR ) { fprintf( stderr, "mssql_err_handler():  OS error = %s\n", dberrstr ); }
		return INT_CANCEL;
	}

	int len;

	if( !(tc->msg) && ( len = snprintf( tc->errmsg, MSSQL_MSGLEN, "%s", dberrstr ) ) < MSSQL_MSGLEN && oserr != DBNOERR )
	{
		snprintf( tc->errmsg + len, MSSQL_MSGLEN - len, ", %s", oserrstr );
	}

	tc->errtype = 1;
	if( severity > 16 ) { tc->errtype = -1; }

	return INT_CANCEL;
}



static int mssql_msg_handler( DBPROCESS* dbproc, DBINT num, int state, int lvl, char* dberrstr, char* srvname, char* proc, int line )
{
	switch( num )
	{
		case 5701:   // Changed database to
		case 5703:   // Changed language to
			return 0;
	}

	struct tdsconn* tc = (struct tdsconn*) dbgetuserdata( dbproc );

	if( tc == NULL || tc->errmsg == NULL )
	{
		fprintf( stderr, "mssql_msg_handler(): msg = %s\n", dberrstr );
		return 0;
	}

	int len;

	if( ( len = snprintf( tc->errmsg, MSSQL_MSGLEN, "[%s]", srvname ) ) < MSSQL_MSGLEN )
	{
		snprintf( tc->errmsg + len, MSSQL_MSGLEN - len, " %s", dberrstr );
	}

	tc->msg = 1;
	return 0;
}