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
|
/* src/interfaces/ecpg/ecpglib/misc.c */
#define POSTGRES_ECPG_INTERNAL
#include "postgres_fe.h"
#include <limits.h>
#include <unistd.h>
#include "ecpg-pthread-win32.h"
#include "ecpgerrno.h"
#include "ecpglib.h"
#include "ecpglib_extern.h"
#include "ecpgtype.h"
#include "pg_config_paths.h"
#include "pgtypes_date.h"
#include "pgtypes_interval.h"
#include "pgtypes_numeric.h"
#include "pgtypes_timestamp.h"
#include "sqlca.h"
#ifndef LONG_LONG_MIN
#ifdef LLONG_MIN
#define LONG_LONG_MIN LLONG_MIN
#else
#define LONG_LONG_MIN LONGLONG_MIN
#endif /* LLONG_MIN */
#endif /* LONG_LONG_MIN */
bool ecpg_internal_regression_mode = false;
static struct sqlca_t sqlca_init =
{
{
'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '
},
sizeof(struct sqlca_t),
0,
{
0,
{
0
}
},
{
'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '
},
{
0, 0, 0, 0, 0, 0
},
{
0, 0, 0, 0, 0, 0, 0, 0
},
{
'0', '0', '0', '0', '0'
}
};
#ifdef ENABLE_THREAD_SAFETY
static pthread_key_t sqlca_key;
static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
#else
static struct sqlca_t sqlca =
{
{
'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '
},
sizeof(struct sqlca_t),
0,
{
0,
{
0
}
},
{
'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '
},
{
0, 0, 0, 0, 0, 0
},
{
0, 0, 0, 0, 0, 0, 0, 0
},
{
'0', '0', '0', '0', '0'
}
};
#endif
#ifdef ENABLE_THREAD_SAFETY
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_init_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static int simple_debug = 0;
static FILE *debugstream = NULL;
void
ecpg_init_sqlca(struct sqlca_t *sqlca)
{
memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
}
bool
ecpg_init(const struct connection *con, const char *connection_name, const int lineno)
{
struct sqlca_t *sqlca = ECPGget_sqlca();
if (sqlca == NULL)
{
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY,
NULL);
return false;
}
ecpg_init_sqlca(sqlca);
if (con == NULL)
{
ecpg_raise(lineno, ECPG_NO_CONN, ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST,
connection_name ? connection_name : ecpg_gettext("NULL"));
return false;
}
return true;
}
#ifdef ENABLE_THREAD_SAFETY
static void
ecpg_sqlca_key_destructor(void *arg)
{
free(arg); /* sqlca structure allocated in ECPGget_sqlca */
}
static void
ecpg_sqlca_key_init(void)
{
pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor);
}
#endif
struct sqlca_t *
ECPGget_sqlca(void)
{
#ifdef ENABLE_THREAD_SAFETY
struct sqlca_t *sqlca;
pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
sqlca = pthread_getspecific(sqlca_key);
if (sqlca == NULL)
{
sqlca = malloc(sizeof(struct sqlca_t));
if (sqlca == NULL)
return NULL;
ecpg_init_sqlca(sqlca);
pthread_setspecific(sqlca_key, sqlca);
}
return sqlca;
#else
return &sqlca;
#endif
}
bool
ECPGstatus(int lineno, const char *connection_name)
{
struct connection *con = ecpg_get_connection(connection_name);
if (!ecpg_init(con, connection_name, lineno))
return false;
/* are we connected? */
if (con->connection == NULL)
{
ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, con->name);
return false;
}
return true;
}
PGTransactionStatusType
ECPGtransactionStatus(const char *connection_name)
{
const struct connection *con;
con = ecpg_get_connection(connection_name);
if (con == NULL)
{
/* transaction status is unknown */
return PQTRANS_UNKNOWN;
}
return PQtransactionStatus(con->connection);
}
bool
ECPGtrans(int lineno, const char *connection_name, const char *transaction)
{
PGresult *res;
struct connection *con = ecpg_get_connection(connection_name);
if (!ecpg_init(con, connection_name, lineno))
return false;
ecpg_log("ECPGtrans on line %d: action \"%s\"; connection \"%s\"\n", lineno, transaction, con ? con->name : "null");
/* if we have no connection we just simulate the command */
if (con && con->connection)
{
/*
* If we got a transaction command but have no open transaction, we
* have to start one, unless we are in autocommit, where the
* developers have to take care themselves. However, if the command is
* a begin statement, we just execute it once. And if the command is
* commit or rollback prepared, we don't execute it.
*/
if (PQtransactionStatus(con->connection) == PQTRANS_IDLE &&
!con->autocommit &&
strncmp(transaction, "begin", 5) != 0 &&
strncmp(transaction, "start", 5) != 0 &&
strncmp(transaction, "commit prepared", 15) != 0 &&
strncmp(transaction, "rollback prepared", 17) != 0)
{
res = PQexec(con->connection, "begin transaction");
if (!ecpg_check_PQresult(res, lineno, con->connection, ECPG_COMPAT_PGSQL))
return false;
PQclear(res);
}
res = PQexec(con->connection, transaction);
if (!ecpg_check_PQresult(res, lineno, con->connection, ECPG_COMPAT_PGSQL))
return false;
PQclear(res);
}
return true;
}
void
ECPGdebug(int n, FILE *dbgs)
{
#ifdef ENABLE_THREAD_SAFETY
pthread_mutex_lock(&debug_init_mutex);
#endif
if (n > 100)
{
ecpg_internal_regression_mode = true;
simple_debug = n - 100;
}
else
simple_debug = n;
debugstream = dbgs;
ecpg_log("ECPGdebug: set to %d\n", simple_debug);
#ifdef ENABLE_THREAD_SAFETY
pthread_mutex_unlock(&debug_init_mutex);
#endif
}
void
ecpg_log(const char *format,...)
{
va_list ap;
struct sqlca_t *sqlca = ECPGget_sqlca();
const char *intl_format;
int bufsize;
char *fmt;
if (!simple_debug)
return;
/* localize the error message string */
intl_format = ecpg_gettext(format);
/*
* Insert PID into the format, unless ecpg_internal_regression_mode is set
* (regression tests want unchanging output).
*/
bufsize = strlen(intl_format) + 100;
fmt = (char *) malloc(bufsize);
if (fmt == NULL)
return;
if (ecpg_internal_regression_mode)
snprintf(fmt, bufsize, "[NO_PID]: %s", intl_format);
else
snprintf(fmt, bufsize, "[%d]: %s", (int) getpid(), intl_format);
#ifdef ENABLE_THREAD_SAFETY
pthread_mutex_lock(&debug_mutex);
#endif
va_start(ap, format);
vfprintf(debugstream, fmt, ap);
va_end(ap);
/* dump out internal sqlca variables */
if (ecpg_internal_regression_mode && sqlca != NULL)
{
fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
sqlca->sqlcode, sqlca->sqlstate);
}
fflush(debugstream);
#ifdef ENABLE_THREAD_SAFETY
pthread_mutex_unlock(&debug_mutex);
#endif
free(fmt);
}
void
ECPGset_noind_null(enum ECPGttype type, void *ptr)
{
switch (type)
{
case ECPGt_char:
case ECPGt_unsigned_char:
case ECPGt_string:
*((char *) ptr) = '\0';
break;
case ECPGt_short:
case ECPGt_unsigned_short:
*((short int *) ptr) = SHRT_MIN;
break;
case ECPGt_int:
case ECPGt_unsigned_int:
*((int *) ptr) = INT_MIN;
break;
case ECPGt_long:
case ECPGt_unsigned_long:
case ECPGt_date:
*((long *) ptr) = LONG_MIN;
break;
case ECPGt_long_long:
case ECPGt_unsigned_long_long:
*((long long *) ptr) = LONG_LONG_MIN;
break;
case ECPGt_float:
memset((char *) ptr, 0xff, sizeof(float));
break;
case ECPGt_double:
memset((char *) ptr, 0xff, sizeof(double));
break;
case ECPGt_varchar:
*(((struct ECPGgeneric_varchar *) ptr)->arr) = 0x00;
((struct ECPGgeneric_varchar *) ptr)->len = 0;
break;
case ECPGt_bytea:
((struct ECPGgeneric_bytea *) ptr)->len = 0;
break;
case ECPGt_decimal:
memset((char *) ptr, 0, sizeof(decimal));
((decimal *) ptr)->sign = NUMERIC_NULL;
break;
case ECPGt_numeric:
memset((char *) ptr, 0, sizeof(numeric));
((numeric *) ptr)->sign = NUMERIC_NULL;
break;
case ECPGt_interval:
memset((char *) ptr, 0xff, sizeof(interval));
break;
case ECPGt_timestamp:
memset((char *) ptr, 0xff, sizeof(timestamp));
break;
default:
break;
}
}
static bool
_check(const unsigned char *ptr, int length)
{
for (length--; length >= 0; length--)
if (ptr[length] != 0xff)
return false;
return true;
}
bool
ECPGis_noind_null(enum ECPGttype type, const void *ptr)
{
switch (type)
{
case ECPGt_char:
case ECPGt_unsigned_char:
case ECPGt_string:
if (*((const char *) ptr) == '\0')
return true;
break;
case ECPGt_short:
case ECPGt_unsigned_short:
if (*((const short int *) ptr) == SHRT_MIN)
return true;
break;
case ECPGt_int:
case ECPGt_unsigned_int:
if (*((const int *) ptr) == INT_MIN)
return true;
break;
case ECPGt_long:
case ECPGt_unsigned_long:
case ECPGt_date:
if (*((const long *) ptr) == LONG_MIN)
return true;
break;
case ECPGt_long_long:
case ECPGt_unsigned_long_long:
if (*((const long long *) ptr) == LONG_LONG_MIN)
return true;
break;
case ECPGt_float:
return _check(ptr, sizeof(float));
break;
case ECPGt_double:
return _check(ptr, sizeof(double));
break;
case ECPGt_varchar:
if (*(((const struct ECPGgeneric_varchar *) ptr)->arr) == 0x00)
return true;
break;
case ECPGt_bytea:
if (((const struct ECPGgeneric_bytea *) ptr)->len == 0)
return true;
break;
case ECPGt_decimal:
if (((const decimal *) ptr)->sign == NUMERIC_NULL)
return true;
break;
case ECPGt_numeric:
if (((const numeric *) ptr)->sign == NUMERIC_NULL)
return true;
break;
case ECPGt_interval:
return _check(ptr, sizeof(interval));
break;
case ECPGt_timestamp:
return _check(ptr, sizeof(timestamp));
break;
default:
break;
}
return false;
}
#ifdef WIN32
#ifdef ENABLE_THREAD_SAFETY
int
pthread_mutex_init(pthread_mutex_t *mp, void *attr)
{
mp->initstate = 0;
return 0;
}
int
pthread_mutex_lock(pthread_mutex_t *mp)
{
/* Initialize the csection if not already done */
if (mp->initstate != 1)
{
LONG istate;
while ((istate = InterlockedExchange(&mp->initstate, 2)) == 2)
Sleep(0); /* wait, another thread is doing this */
if (istate != 1)
InitializeCriticalSection(&mp->csection);
InterlockedExchange(&mp->initstate, 1);
}
EnterCriticalSection(&mp->csection);
return 0;
}
int
pthread_mutex_unlock(pthread_mutex_t *mp)
{
if (mp->initstate != 1)
return EINVAL;
LeaveCriticalSection(&mp->csection);
return 0;
}
static pthread_mutex_t win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER;
void
win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void))
{
if (!*once)
{
pthread_mutex_lock(&win32_pthread_once_lock);
if (!*once)
{
fn();
*once = true;
}
pthread_mutex_unlock(&win32_pthread_once_lock);
}
}
#endif /* ENABLE_THREAD_SAFETY */
#endif /* WIN32 */
#ifdef ENABLE_NLS
char *
ecpg_gettext(const char *msgid)
{
/*
* At least on Windows, there are gettext implementations that fail if
* multiple threads call bindtextdomain() concurrently. Use a mutex and
* flag variable to ensure that we call it just once per process. It is
* not known that similar bugs exist on non-Windows platforms, but we
* might as well do it the same way everywhere.
*/
static volatile bool already_bound = false;
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
if (!already_bound)
{
/* dgettext() preserves errno, but bindtextdomain() doesn't */
#ifdef WIN32
int save_errno = GetLastError();
#else
int save_errno = errno;
#endif
(void) pthread_mutex_lock(&binddomain_mutex);
if (!already_bound)
{
const char *ldir;
/*
* No relocatable lookup here because the calling executable could
* be anywhere
*/
ldir = getenv("PGLOCALEDIR");
if (!ldir)
ldir = LOCALEDIR;
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
already_bound = true;
}
(void) pthread_mutex_unlock(&binddomain_mutex);
#ifdef WIN32
SetLastError(save_errno);
#else
errno = save_errno;
#endif
}
return dgettext(PG_TEXTDOMAIN("ecpglib"), msgid);
}
#endif /* ENABLE_NLS */
struct var_list *ivlist = NULL;
void
ECPGset_var(int number, void *pointer, int lineno)
{
struct var_list *ptr;
struct sqlca_t *sqlca = ECPGget_sqlca();
if (sqlca == NULL)
{
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
return;
}
ecpg_init_sqlca(sqlca);
for (ptr = ivlist; ptr != NULL; ptr = ptr->next)
{
if (ptr->number == number)
{
/* already known => just change pointer value */
ptr->pointer = pointer;
return;
}
}
/* a new one has to be added */
ptr = (struct var_list *) calloc(1L, sizeof(struct var_list));
if (!ptr)
{
sqlca = ECPGget_sqlca();
if (sqlca == NULL)
{
ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
return;
}
sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);
sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
/* free all memory we have allocated for the user */
ECPGfree_auto_mem();
}
else
{
ptr->number = number;
ptr->pointer = pointer;
ptr->next = ivlist;
ivlist = ptr;
}
}
void *
ECPGget_var(int number)
{
struct var_list *ptr;
for (ptr = ivlist; ptr != NULL && ptr->number != number; ptr = ptr->next);
return (ptr) ? ptr->pointer : NULL;
}
|