summaryrefslogtreecommitdiff
path: root/src/interfaces/odbc/environ.c
blob: 9aafcb14adc7cb41b5b374c225a7e41b728ee5dc (plain)
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

/* Module:			environ.c
 *
 * Description:		This module contains routines related to
 *					the environment, such as storing connection handles,
 *					and returning errors.
 *
 * Classes:			EnvironmentClass (Functions prefix: "EN_")
 *
 * API functions:	SQLAllocEnv, SQLFreeEnv, SQLError
 *
 * Comments:		See "notice.txt" for copyright and license information.
 *
 */

#include "environ.h"
#include "connection.h"
#include "dlg_specific.h"
#include "statement.h"
#include <stdlib.h>
#include <string.h>

extern GLOBAL_VALUES globals;

/* The one instance of the handles */
ConnectionClass *conns[MAX_CONNECTIONS];


RETCODE SQL_API
SQLAllocEnv(HENV FAR *phenv)
{
	static char *func = "SQLAllocEnv";

	mylog("**** in SQLAllocEnv ** \n");

	/*
	 * Hack for systems on which none of the constructor-making techniques
	 * in psqlodbc.c work: if globals appears not to have been
	 * initialized, then cause it to be initialized.  Since this should be
	 * the first function called in this shared library, doing it here
	 * should work.
	 */
	if (globals.socket_buffersize <= 0)
		getGlobalDefaults(DBMS_NAME, ODBCINST_INI, FALSE);

	*phenv = (HENV) EN_Constructor();
	if (!*phenv)
	{
		*phenv = SQL_NULL_HENV;
		EN_log_error(func, "Error allocating environment", NULL);
		return SQL_ERROR;
	}

	mylog("** exit SQLAllocEnv: phenv = %u **\n", *phenv);
	return SQL_SUCCESS;
}

RETCODE SQL_API
SQLFreeEnv(HENV henv)
{
	static char *func = "SQLFreeEnv";
	EnvironmentClass *env = (EnvironmentClass *) henv;

	mylog("**** in SQLFreeEnv: env = %u ** \n", env);

	if (env && EN_Destructor(env))
	{
		mylog("   ok\n");
		return SQL_SUCCESS;
	}

	mylog("    error\n");
	EN_log_error(func, "Error freeing environment", env);
	return SQL_ERROR;
}

/*		Returns the next SQL error information. */

RETCODE SQL_API
SQLError(
		 HENV henv,
		 HDBC hdbc,
		 HSTMT hstmt,
		 UCHAR FAR *szSqlState,
		 SDWORD FAR *pfNativeError,
		 UCHAR FAR *szErrorMsg,
		 SWORD cbErrorMsgMax,
		 SWORD FAR *pcbErrorMsg)
{
	char	   *msg;
	int			status;

	mylog("**** SQLError: henv=%u, hdbc=%u, hstmt=%u\n", henv, hdbc, hstmt);

	if (SQL_NULL_HSTMT != hstmt)
	{
		/* CC: return an error of a hstmt  */
		StatementClass *stmt = (StatementClass *) hstmt;

		if (SC_get_error(stmt, &status, &msg))
		{
			mylog("SC_get_error: status = %d, msg = #%s#\n", status, msg);
			if (NULL == msg)
			{
				if (NULL != szSqlState)
					strcpy(szSqlState, "00000");
				if (NULL != pcbErrorMsg)
					*pcbErrorMsg = 0;
				if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
					szErrorMsg[0] = '\0';

				return SQL_NO_DATA_FOUND;
			}
			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = (SWORD) strlen(msg);

			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				strncpy_null(szErrorMsg, msg, cbErrorMsgMax);

			if (NULL != pfNativeError)
				*pfNativeError = status;

			if (NULL != szSqlState)

				switch (status)
				{
						/* now determine the SQLSTATE to be returned */
					case STMT_TRUNCATED:
						strcpy(szSqlState, "01004");
						/* data truncated */
						break;
					case STMT_INFO_ONLY:
						strcpy(szSqlState, "00000");
						/* just information that is returned, no error */
						break;
					case STMT_BAD_ERROR:
						strcpy(szSqlState, "08S01");
						/* communication link failure */
						break;
					case STMT_CREATE_TABLE_ERROR:
						strcpy(szSqlState, "S0001");
						/* table already exists */
						break;
					case STMT_STATUS_ERROR:
					case STMT_SEQUENCE_ERROR:
						strcpy(szSqlState, "S1010");
						/* Function sequence error */
						break;
					case STMT_NO_MEMORY_ERROR:
						strcpy(szSqlState, "S1001");
						/* memory allocation failure */
						break;
					case STMT_COLNUM_ERROR:
						strcpy(szSqlState, "S1002");
						/* invalid column number */
						break;
					case STMT_NO_STMTSTRING:
						strcpy(szSqlState, "S1001");
						/* having no stmtstring is also a malloc problem */
						break;
					case STMT_ERROR_TAKEN_FROM_BACKEND:
						strcpy(szSqlState, "S1000");
						/* general error */
						break;
					case STMT_INTERNAL_ERROR:
						strcpy(szSqlState, "S1000");
						/* general error */
						break;
					case STMT_ROW_OUT_OF_RANGE:
						strcpy(szSqlState, "S1107");
						break;

					case STMT_OPERATION_CANCELLED:
						strcpy(szSqlState, "S1008");
						break;

					case STMT_NOT_IMPLEMENTED_ERROR:
						strcpy(szSqlState, "S1C00");	/* == 'driver not
														 * capable' */
						break;
					case STMT_OPTION_OUT_OF_RANGE_ERROR:
						strcpy(szSqlState, "S1092");
						break;
					case STMT_BAD_PARAMETER_NUMBER_ERROR:
						strcpy(szSqlState, "S1093");
						break;
					case STMT_INVALID_COLUMN_NUMBER_ERROR:
						strcpy(szSqlState, "S1002");
						break;
					case STMT_RESTRICTED_DATA_TYPE_ERROR:
						strcpy(szSqlState, "07006");
						break;
					case STMT_INVALID_CURSOR_STATE_ERROR:
						strcpy(szSqlState, "24000");
						break;
					case STMT_OPTION_VALUE_CHANGED:
						strcpy(szSqlState, "01S02");
						break;
					case STMT_INVALID_CURSOR_NAME:
						strcpy(szSqlState, "34000");
						break;
					case STMT_NO_CURSOR_NAME:
						strcpy(szSqlState, "S1015");
						break;
					case STMT_INVALID_ARGUMENT_NO:
						strcpy(szSqlState, "S1009");
						/* invalid argument value */
						break;
					case STMT_INVALID_CURSOR_POSITION:
						strcpy(szSqlState, "S1109");
						break;

					case STMT_VALUE_OUT_OF_RANGE:
						strcpy(szSqlState, "22003");
						break;

					case STMT_OPERATION_INVALID:
						strcpy(szSqlState, "S1011");
						break;

					case STMT_EXEC_ERROR:
					default:
						strcpy(szSqlState, "S1000");
						/* also a general error */
						break;
				}

			mylog("       szSqlState = '%s', szError='%s'\n", szSqlState, szErrorMsg);

		}
		else
		{
			if (NULL != szSqlState)
				strcpy(szSqlState, "00000");
			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = 0;
			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				szErrorMsg[0] = '\0';

			mylog("       returning NO_DATA_FOUND\n");
			return SQL_NO_DATA_FOUND;
		}
		return SQL_SUCCESS;

	}
	else if (SQL_NULL_HDBC != hdbc)
	{
		ConnectionClass *conn = (ConnectionClass *) hdbc;

		mylog("calling CC_get_error\n");
		if (CC_get_error(conn, &status, &msg))
		{
			mylog("CC_get_error: status = %d, msg = #%s#\n", status, msg);
			if (NULL == msg)
			{
				if (NULL != szSqlState)
					strcpy(szSqlState, "00000");
				if (NULL != pcbErrorMsg)
					*pcbErrorMsg = 0;
				if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
					szErrorMsg[0] = '\0';

				return SQL_NO_DATA_FOUND;
			}

			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = (SWORD) strlen(msg);
			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				strncpy_null(szErrorMsg, msg, cbErrorMsgMax);
			if (NULL != pfNativeError)
				*pfNativeError = status;

			if (NULL != szSqlState)
				switch (status)
				{
					case STMT_OPTION_VALUE_CHANGED:
					case CONN_OPTION_VALUE_CHANGED:
						strcpy(szSqlState, "01S02");
						break;
					case STMT_TRUNCATED:
					case CONN_TRUNCATED:
						strcpy(szSqlState, "01004");
						/* data truncated */
						break;
					case CONN_INIREAD_ERROR:
						strcpy(szSqlState, "IM002");
						/* data source not found */
						break;
					case CONN_OPENDB_ERROR:
						strcpy(szSqlState, "08001");
						/* unable to connect to data source */
						break;
					case CONN_INVALID_AUTHENTICATION:
					case CONN_AUTH_TYPE_UNSUPPORTED:
						strcpy(szSqlState, "28000");
						break;
					case CONN_STMT_ALLOC_ERROR:
						strcpy(szSqlState, "S1001");
						/* memory allocation failure */
						break;
					case CONN_IN_USE:
						strcpy(szSqlState, "S1000");
						/* general error */
						break;
					case CONN_UNSUPPORTED_OPTION:
						strcpy(szSqlState, "IM001");
						/* driver does not support this function */
					case CONN_INVALID_ARGUMENT_NO:
						strcpy(szSqlState, "S1009");
						/* invalid argument value */
						break;
					case CONN_TRANSACT_IN_PROGRES:
						strcpy(szSqlState, "S1010");

						/*
						 * when the user tries to switch commit mode in a
						 * transaction
						 */
						/* -> function sequence error */
						break;
					case CONN_NO_MEMORY_ERROR:
						strcpy(szSqlState, "S1001");
						break;
					case CONN_NOT_IMPLEMENTED_ERROR:
					case STMT_NOT_IMPLEMENTED_ERROR:
						strcpy(szSqlState, "S1C00");
						break;

					case CONN_VALUE_OUT_OF_RANGE:
					case STMT_VALUE_OUT_OF_RANGE:
						strcpy(szSqlState, "22003");
						break;

					default:
						strcpy(szSqlState, "S1000");
						/* general error */
						break;
				}

		}
		else
		{
			mylog("CC_Get_error returned nothing.\n");
			if (NULL != szSqlState)
				strcpy(szSqlState, "00000");
			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = 0;
			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				szErrorMsg[0] = '\0';

			return SQL_NO_DATA_FOUND;
		}
		return SQL_SUCCESS;

	}
	else if (SQL_NULL_HENV != henv)
	{
		EnvironmentClass *env = (EnvironmentClass *) henv;

		if (EN_get_error(env, &status, &msg))
		{
			mylog("EN_get_error: status = %d, msg = #%s#\n", status, msg);
			if (NULL == msg)
			{
				if (NULL != szSqlState)
					strcpy(szSqlState, "00000");
				if (NULL != pcbErrorMsg)
					*pcbErrorMsg = 0;
				if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
					szErrorMsg[0] = '\0';

				return SQL_NO_DATA_FOUND;
			}

			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = (SWORD) strlen(msg);
			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				strncpy_null(szErrorMsg, msg, cbErrorMsgMax);
			if (NULL != pfNativeError)
				*pfNativeError = status;

			if (szSqlState)
			{
				switch (status)
				{
					case ENV_ALLOC_ERROR:
						/* memory allocation failure */
						strcpy(szSqlState, "S1001");
						break;
					default:
						strcpy(szSqlState, "S1000");
						/* general error */
						break;
				}
			}
		}
		else
		{
			if (NULL != szSqlState)
				strcpy(szSqlState, "00000");
			if (NULL != pcbErrorMsg)
				*pcbErrorMsg = 0;
			if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
				szErrorMsg[0] = '\0';

			return SQL_NO_DATA_FOUND;
		}

		return SQL_SUCCESS;
	}

	if (NULL != szSqlState)
		strcpy(szSqlState, "00000");
	if (NULL != pcbErrorMsg)
		*pcbErrorMsg = 0;
	if ((NULL != szErrorMsg) && (cbErrorMsgMax > 0))
		szErrorMsg[0] = '\0';

	return SQL_NO_DATA_FOUND;
}


/*********************************************************************/
/*
 * EnvironmentClass implementation
 */



EnvironmentClass
		   *
EN_Constructor(void)
{
	EnvironmentClass *rv;

	rv = (EnvironmentClass *) malloc(sizeof(EnvironmentClass));
	if (rv)
	{
		rv->errormsg = 0;
		rv->errornumber = 0;
	}

	return rv;
}


char
EN_Destructor(EnvironmentClass *self)
{
	int			lf;
	char		rv = 1;

	mylog("in EN_Destructor, self=%u\n", self);

	/* the error messages are static strings distributed throughout */
	/* the source--they should not be freed */

	/* Free any connections belonging to this environment */
	for (lf = 0; lf < MAX_CONNECTIONS; lf++)
	{
		if (conns[lf] && conns[lf]->henv == self)
			rv = rv && CC_Destructor(conns[lf]);
	}
	free(self);

	mylog("exit EN_Destructor: rv = %d\n", rv);
	return rv;
}

char
EN_get_error(EnvironmentClass *self, int *number, char **message)
{
	if (self && self->errormsg && self->errornumber)
	{
		*message = self->errormsg;
		*number = self->errornumber;
		self->errormsg = 0;
		self->errornumber = 0;
		return 1;
	}
	else
		return 0;
}

char
EN_add_connection(EnvironmentClass *self, ConnectionClass *conn)
{
	int			i;

	mylog("EN_add_connection: self = %u, conn = %u\n", self, conn);

	for (i = 0; i < MAX_CONNECTIONS; i++)
	{
		if (!conns[i])
		{
			conn->henv = self;
			conns[i] = conn;

			mylog("       added at i =%d, conn->henv = %u, conns[i]->henv = %u\n", i, conn->henv, conns[i]->henv);

			return TRUE;
		}
	}

	return FALSE;
}

char
EN_remove_connection(EnvironmentClass *self, ConnectionClass *conn)
{
	int			i;

	for (i = 0; i < MAX_CONNECTIONS; i++)
		if (conns[i] == conn && conns[i]->status != CONN_EXECUTING)
		{
			conns[i] = NULL;
			return TRUE;
		}

	return FALSE;
}

void
EN_log_error(char *func, char *desc, EnvironmentClass *self)
{
	if (self)
		qlog("ENVIRON ERROR: func=%s, desc='%s', errnum=%d, errmsg='%s'\n", func, desc, self->errornumber, self->errormsg);
	else
		qlog("INVALID ENVIRON HANDLE ERROR: func=%s, desc='%s'\n", func, desc);
}