PostgreSQL Source Code  git master
misc.c
Go to the documentation of this file.
1 /* src/interfaces/ecpg/ecpglib/misc.c */
2 
3 #define POSTGRES_ECPG_INTERNAL
4 #include "postgres_fe.h"
5 
6 #include <limits.h>
7 #include <unistd.h>
8 
9 #include "ecpg-pthread-win32.h"
10 #include "ecpgerrno.h"
11 #include "ecpglib.h"
12 #include "ecpglib_extern.h"
13 #include "ecpgtype.h"
14 #include "pg_config_paths.h"
15 #include "pgtypes_date.h"
16 #include "pgtypes_interval.h"
17 #include "pgtypes_numeric.h"
18 #include "pgtypes_timestamp.h"
19 #include "sqlca.h"
20 
21 #ifndef LONG_LONG_MIN
22 #ifdef LLONG_MIN
23 #define LONG_LONG_MIN LLONG_MIN
24 #else
25 #define LONG_LONG_MIN LONGLONG_MIN
26 #endif /* LLONG_MIN */
27 #endif /* LONG_LONG_MIN */
28 
30 
31 static struct sqlca_t sqlca_init =
32 {
33  {
34  'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '
35  },
36  sizeof(struct sqlca_t),
37  0,
38  {
39  0,
40  {
41  0
42  }
43  },
44  {
45  'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '
46  },
47  {
48  0, 0, 0, 0, 0, 0
49  },
50  {
51  0, 0, 0, 0, 0, 0, 0, 0
52  },
53  {
54  '0', '0', '0', '0', '0'
55  }
56 };
57 
59 static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
60 
63 static int simple_debug = 0;
64 static FILE *debugstream = NULL;
65 
66 void
68 {
69  memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
70 }
71 
72 bool
73 ecpg_init(const struct connection *con, const char *connection_name, const int lineno)
74 {
75  struct sqlca_t *sqlca = ECPGget_sqlca();
76 
77  if (sqlca == NULL)
78  {
80  NULL);
81  return false;
82  }
83 
85  if (con == NULL)
86  {
88  connection_name ? connection_name : ecpg_gettext("NULL"));
89  return false;
90  }
91 
92  return true;
93 }
94 
95 static void
97 {
98  free(arg); /* sqlca structure allocated in ECPGget_sqlca */
99 }
100 
101 static void
103 {
104  pthread_key_create(&sqlca_key, ecpg_sqlca_key_destructor);
105 }
106 
107 struct sqlca_t *
109 {
110  struct sqlca_t *sqlca;
111 
112  pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
113 
115  if (sqlca == NULL)
116  {
117  sqlca = malloc(sizeof(struct sqlca_t));
118  if (sqlca == NULL)
119  return NULL;
122  }
123  return sqlca;
124 }
125 
126 bool
127 ECPGstatus(int lineno, const char *connection_name)
128 {
129  struct connection *con = ecpg_get_connection(connection_name);
130 
131  if (!ecpg_init(con, connection_name, lineno))
132  return false;
133 
134  /* are we connected? */
135  if (con->connection == NULL)
136  {
138  return false;
139  }
140 
141  return true;
142 }
143 
145 ECPGtransactionStatus(const char *connection_name)
146 {
147  const struct connection *con;
148 
149  con = ecpg_get_connection(connection_name);
150  if (con == NULL)
151  {
152  /* transaction status is unknown */
153  return PQTRANS_UNKNOWN;
154  }
155 
156  return PQtransactionStatus(con->connection);
157 }
158 
159 bool
160 ECPGtrans(int lineno, const char *connection_name, const char *transaction)
161 {
162  PGresult *res;
163  struct connection *con = ecpg_get_connection(connection_name);
164 
165  if (!ecpg_init(con, connection_name, lineno))
166  return false;
167 
168  ecpg_log("ECPGtrans on line %d: action \"%s\"; connection \"%s\"\n", lineno, transaction, con ? con->name : "null");
169 
170  /* if we have no connection we just simulate the command */
171  if (con && con->connection)
172  {
173  /*
174  * If we got a transaction command but have no open transaction, we
175  * have to start one, unless we are in autocommit, where the
176  * developers have to take care themselves. However, if the command is
177  * a begin statement, we just execute it once. And if the command is
178  * commit or rollback prepared, we don't execute it.
179  */
181  !con->autocommit &&
182  strncmp(transaction, "begin", 5) != 0 &&
183  strncmp(transaction, "start", 5) != 0 &&
184  strncmp(transaction, "commit prepared", 15) != 0 &&
185  strncmp(transaction, "rollback prepared", 17) != 0)
186  {
187  res = PQexec(con->connection, "begin transaction");
189  return false;
190  PQclear(res);
191  }
192 
193  res = PQexec(con->connection, transaction);
195  return false;
196  PQclear(res);
197  }
198 
199  return true;
200 }
201 
202 
203 void
204 ECPGdebug(int n, FILE *dbgs)
205 {
207 
208  if (n > 100)
209  {
211  simple_debug = n - 100;
212  }
213  else
214  simple_debug = n;
215 
216  debugstream = dbgs;
217 
218  ecpg_log("ECPGdebug: set to %d\n", simple_debug);
219 
221 }
222 
223 void
224 ecpg_log(const char *format,...)
225 {
226  va_list ap;
227  struct sqlca_t *sqlca = ECPGget_sqlca();
228  const char *intl_format;
229  int bufsize;
230  char *fmt;
231 
232  if (!simple_debug)
233  return;
234 
235  /* localize the error message string */
236  intl_format = ecpg_gettext(format);
237 
238  /*
239  * Insert PID into the format, unless ecpg_internal_regression_mode is set
240  * (regression tests want unchanging output).
241  */
242  bufsize = strlen(intl_format) + 100;
243  fmt = (char *) malloc(bufsize);
244  if (fmt == NULL)
245  return;
246 
248  snprintf(fmt, bufsize, "[NO_PID]: %s", intl_format);
249  else
250  snprintf(fmt, bufsize, "[%d]: %s", (int) getpid(), intl_format);
251 
253 
254  va_start(ap, format);
255  vfprintf(debugstream, fmt, ap);
256  va_end(ap);
257 
258  /* dump out internal sqlca variables */
259  if (ecpg_internal_regression_mode && sqlca != NULL)
260  {
261  fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
262  sqlca->sqlcode, sqlca->sqlstate);
263  }
264 
266 
268 
269  free(fmt);
270 }
271 
272 void
274 {
275  switch (type)
276  {
277  case ECPGt_char:
278  case ECPGt_unsigned_char:
279  case ECPGt_string:
280  *((char *) ptr) = '\0';
281  break;
282  case ECPGt_short:
284  *((short int *) ptr) = SHRT_MIN;
285  break;
286  case ECPGt_int:
287  case ECPGt_unsigned_int:
288  *((int *) ptr) = INT_MIN;
289  break;
290  case ECPGt_long:
291  case ECPGt_unsigned_long:
292  case ECPGt_date:
293  *((long *) ptr) = LONG_MIN;
294  break;
295  case ECPGt_long_long:
297  *((long long *) ptr) = LONG_LONG_MIN;
298  break;
299  case ECPGt_float:
300  memset((char *) ptr, 0xff, sizeof(float));
301  break;
302  case ECPGt_double:
303  memset((char *) ptr, 0xff, sizeof(double));
304  break;
305  case ECPGt_varchar:
306  *(((struct ECPGgeneric_varchar *) ptr)->arr) = 0x00;
307  ((struct ECPGgeneric_varchar *) ptr)->len = 0;
308  break;
309  case ECPGt_bytea:
310  ((struct ECPGgeneric_bytea *) ptr)->len = 0;
311  break;
312  case ECPGt_decimal:
313  memset((char *) ptr, 0, sizeof(decimal));
314  ((decimal *) ptr)->sign = NUMERIC_NULL;
315  break;
316  case ECPGt_numeric:
317  memset((char *) ptr, 0, sizeof(numeric));
318  ((numeric *) ptr)->sign = NUMERIC_NULL;
319  break;
320  case ECPGt_interval:
321  memset((char *) ptr, 0xff, sizeof(interval));
322  break;
323  case ECPGt_timestamp:
324  memset((char *) ptr, 0xff, sizeof(timestamp));
325  break;
326  default:
327  break;
328  }
329 }
330 
331 static bool
332 _check(const unsigned char *ptr, int length)
333 {
334  for (length--; length >= 0; length--)
335  if (ptr[length] != 0xff)
336  return false;
337 
338  return true;
339 }
340 
341 bool
342 ECPGis_noind_null(enum ECPGttype type, const void *ptr)
343 {
344  switch (type)
345  {
346  case ECPGt_char:
347  case ECPGt_unsigned_char:
348  case ECPGt_string:
349  if (*((const char *) ptr) == '\0')
350  return true;
351  break;
352  case ECPGt_short:
354  if (*((const short int *) ptr) == SHRT_MIN)
355  return true;
356  break;
357  case ECPGt_int:
358  case ECPGt_unsigned_int:
359  if (*((const int *) ptr) == INT_MIN)
360  return true;
361  break;
362  case ECPGt_long:
363  case ECPGt_unsigned_long:
364  case ECPGt_date:
365  if (*((const long *) ptr) == LONG_MIN)
366  return true;
367  break;
368  case ECPGt_long_long:
370  if (*((const long long *) ptr) == LONG_LONG_MIN)
371  return true;
372  break;
373  case ECPGt_float:
374  return _check(ptr, sizeof(float));
375  break;
376  case ECPGt_double:
377  return _check(ptr, sizeof(double));
378  break;
379  case ECPGt_varchar:
380  if (*(((const struct ECPGgeneric_varchar *) ptr)->arr) == 0x00)
381  return true;
382  break;
383  case ECPGt_bytea:
384  if (((const struct ECPGgeneric_bytea *) ptr)->len == 0)
385  return true;
386  break;
387  case ECPGt_decimal:
388  if (((const decimal *) ptr)->sign == NUMERIC_NULL)
389  return true;
390  break;
391  case ECPGt_numeric:
392  if (((const numeric *) ptr)->sign == NUMERIC_NULL)
393  return true;
394  break;
395  case ECPGt_interval:
396  return _check(ptr, sizeof(interval));
397  break;
398  case ECPGt_timestamp:
399  return _check(ptr, sizeof(timestamp));
400  break;
401  default:
402  break;
403  }
404 
405  return false;
406 }
407 
408 #ifdef WIN32
409 
410 int
411 pthread_mutex_init(pthread_mutex_t *mp, void *attr)
412 {
413  mp->initstate = 0;
414  return 0;
415 }
416 
417 int
419 {
420  /* Initialize the csection if not already done */
421  if (mp->initstate != 1)
422  {
423  LONG istate;
424 
425  while ((istate = InterlockedExchange(&mp->initstate, 2)) == 2)
426  Sleep(0); /* wait, another thread is doing this */
427  if (istate != 1)
428  InitializeCriticalSection(&mp->csection);
429  InterlockedExchange(&mp->initstate, 1);
430  }
431  EnterCriticalSection(&mp->csection);
432  return 0;
433 }
434 
435 int
437 {
438  if (mp->initstate != 1)
439  return EINVAL;
440  LeaveCriticalSection(&mp->csection);
441  return 0;
442 }
443 
444 static pthread_mutex_t win32_pthread_once_lock = PTHREAD_MUTEX_INITIALIZER;
445 
446 void
447 win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void))
448 {
449  if (!*once)
450  {
451  pthread_mutex_lock(&win32_pthread_once_lock);
452  if (!*once)
453  {
454  fn();
455  *once = true;
456  }
457  pthread_mutex_unlock(&win32_pthread_once_lock);
458  }
459 }
460 #endif /* WIN32 */
461 
462 #ifdef ENABLE_NLS
463 
464 char *
465 ecpg_gettext(const char *msgid)
466 {
467  /*
468  * At least on Windows, there are gettext implementations that fail if
469  * multiple threads call bindtextdomain() concurrently. Use a mutex and
470  * flag variable to ensure that we call it just once per process. It is
471  * not known that similar bugs exist on non-Windows platforms, but we
472  * might as well do it the same way everywhere.
473  */
474  static volatile bool already_bound = false;
475  static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
476 
477  if (!already_bound)
478  {
479  /* dgettext() preserves errno, but bindtextdomain() doesn't */
480 #ifdef WIN32
481  int save_errno = GetLastError();
482 #else
483  int save_errno = errno;
484 #endif
485 
486  (void) pthread_mutex_lock(&binddomain_mutex);
487 
488  if (!already_bound)
489  {
490  const char *ldir;
491 
492  /*
493  * No relocatable lookup here because the calling executable could
494  * be anywhere
495  */
496  ldir = getenv("PGLOCALEDIR");
497  if (!ldir)
498  ldir = LOCALEDIR;
499  bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
500  already_bound = true;
501  }
502 
503  (void) pthread_mutex_unlock(&binddomain_mutex);
504 
505 #ifdef WIN32
506  SetLastError(save_errno);
507 #else
508  errno = save_errno;
509 #endif
510  }
511 
512  return dgettext(PG_TEXTDOMAIN("ecpglib"), msgid);
513 }
514 #endif /* ENABLE_NLS */
515 
516 struct var_list *ivlist = NULL;
517 
518 void
519 ECPGset_var(int number, void *pointer, int lineno)
520 {
521  struct var_list *ptr;
522 
523  struct sqlca_t *sqlca = ECPGget_sqlca();
524 
525  if (sqlca == NULL)
526  {
529  return;
530  }
531 
533 
534  for (ptr = ivlist; ptr != NULL; ptr = ptr->next)
535  {
536  if (ptr->number == number)
537  {
538  /* already known => just change pointer value */
539  ptr->pointer = pointer;
540  return;
541  }
542  }
543 
544  /* a new one has to be added */
545  ptr = (struct var_list *) calloc(1L, sizeof(struct var_list));
546  if (!ptr)
547  {
548  sqlca = ECPGget_sqlca();
549 
550  if (sqlca == NULL)
551  {
554  return;
555  }
556 
557  sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
558  strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
559  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);
560  sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
561  /* free all memory we have allocated for the user */
563  }
564  else
565  {
566  ptr->number = number;
567  ptr->pointer = pointer;
568  ptr->next = ivlist;
569  ivlist = ptr;
570  }
571 }
572 
573 void *
575 {
576  struct var_list *ptr;
577 
578  for (ptr = ivlist; ptr != NULL && ptr->number != number; ptr = ptr->next);
579  return (ptr) ? ptr->pointer : NULL;
580 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1201
#define dgettext(d, x)
Definition: c.h:1167
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:71
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
#define ECPG_NOT_CONN
Definition: ecpgerrno.h:37
#define ECPG_NO_CONN
Definition: ecpgerrno.h:36
bool ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
Definition: error.c:281
#define ecpg_gettext(x)
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
@ ECPG_COMPAT_PGSQL
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
ECPGttype
Definition: ecpgtype.h:42
@ ECPGt_float
Definition: ecpgtype.h:47
@ ECPGt_long_long
Definition: ecpgtype.h:45
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_decimal
Definition: ecpgtype.h:51
@ ECPGt_bytea
Definition: ecpgtype.h:67
@ ECPGt_numeric
Definition: ecpgtype.h:49
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_timestamp
Definition: ecpgtype.h:54
@ ECPGt_unsigned_short
Definition: ecpgtype.h:43
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_char
Definition: ecpgtype.h:43
@ ECPGt_double
Definition: ecpgtype.h:47
@ ECPGt_date
Definition: ecpgtype.h:53
@ ECPGt_interval
Definition: ecpgtype.h:55
@ ECPGt_unsigned_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_long_long
Definition: ecpgtype.h:45
@ ECPGt_unsigned_int
Definition: ecpgtype.h:44
@ ECPGt_char
Definition: ecpgtype.h:43
@ ECPGt_string
Definition: ecpgtype.h:65
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6903
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
#define calloc(a, b)
Definition: header.h:55
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
#define bufsize
Definition: indent_globs.h:36
char sign
Definition: informix.c:668
void ECPGset_var(int number, void *pointer, int lineno)
Definition: misc.c:519
PGTransactionStatusType ECPGtransactionStatus(const char *connection_name)
Definition: misc.c:145
static pthread_key_t sqlca_key
Definition: misc.c:58
static void ecpg_sqlca_key_destructor(void *arg)
Definition: misc.c:96
void ECPGdebug(int n, FILE *dbgs)
Definition: misc.c:204
void ECPGset_noind_null(enum ECPGttype type, void *ptr)
Definition: misc.c:273
static struct sqlca_t sqlca_init
Definition: misc.c:31
static pthread_mutex_t debug_init_mutex
Definition: misc.c:62
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
static bool _check(const unsigned char *ptr, int length)
Definition: misc.c:332
static int simple_debug
Definition: misc.c:63
static pthread_mutex_t debug_mutex
Definition: misc.c:61
bool ECPGis_noind_null(enum ECPGttype type, const void *ptr)
Definition: misc.c:342
void ecpg_log(const char *format,...)
Definition: misc.c:224
bool ecpg_init(const struct connection *con, const char *connection_name, const int lineno)
Definition: misc.c:73
static FILE * debugstream
Definition: misc.c:64
#define LONG_LONG_MIN
Definition: misc.c:25
void * ECPGget_var(int number)
Definition: misc.c:574
bool ECPGstatus(int lineno, const char *connection_name)
Definition: misc.c:127
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:108
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:67
struct var_list * ivlist
Definition: misc.c:516
bool ecpg_internal_regression_mode
Definition: misc.c:29
static void ecpg_sqlca_key_init(void)
Definition: misc.c:102
static pthread_once_t sqlca_key_once
Definition: misc.c:59
PGTransactionStatusType
Definition: libpq-fe.h:120
@ PQTRANS_IDLE
Definition: libpq-fe.h:121
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:125
static void const char * fmt
static void const char fflush(stdout)
va_end(args)
vfprintf(stderr, fmt, args)
va_start(args, fmt)
void ECPGfree_auto_mem(void)
Definition: memory.c:131
void * arg
static char format
const void size_t len
#define NUMERIC_NULL
int64 timestamp
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:60
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:42
void pthread_setspecific(pthread_key_t key, void *val)
Definition: pthread-win32.c:24
int pthread_mutex_init(pthread_mutex_t *mp, void *attr)
Definition: pthread-win32.c:35
#define PTHREAD_MUTEX_INITIALIZER
Definition: pthread-win32.h:16
ULONG pthread_key_t
Definition: pthread-win32.h:7
int pthread_once_t
Definition: pthread-win32.h:18
#define sqlca
Definition: sqlca.h:59
char arr[FLEXIBLE_ARRAY_MEMBER]
PGconn * connection
CRITICAL_SECTION csection
Definition: pthread-win32.h:13
Definition: sqlca.h:20
struct var_list * next
void * pointer
static void * fn(void *arg)
Definition: thread-alloc.c:119
const char * type