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