PostgreSQL Source Code  git master
descriptor.c File Reference
#include "postgres_fe.h"
#include "catalog/pg_type_d.h"
#include "ecpg-pthread-win32.h"
#include "ecpgerrno.h"
#include "ecpglib.h"
#include "ecpglib_extern.h"
#include "ecpgtype.h"
#include "sql3types.h"
#include "sqlca.h"
#include "sqlda.h"
Include dependency graph for descriptor.c:

Go to the source code of this file.

Macros

#define POSTGRES_ECPG_INTERNAL
 
#define get_descriptors()   (all_descriptors)
 
#define set_descriptors(value)   do { all_descriptors = (value); } while(0)
 
#define RETURN_IF_NO_DATA
 

Functions

static void descriptor_free (struct descriptor *desc)
 
static PGresultecpg_result_by_descriptor (int line, const char *name)
 
static unsigned int ecpg_dynamic_type_DDT (Oid type)
 
bool ECPGget_desc_header (int lineno, const char *desc_name, int *count)
 
static bool get_int_item (int lineno, void *var, enum ECPGttype vartype, int value)
 
static bool set_int_item (int lineno, int *target, const void *var, enum ECPGttype vartype)
 
static bool get_char_item (int lineno, void *var, enum ECPGttype vartype, char *value, int varcharsize)
 
bool ECPGget_desc (int lineno, const char *desc_name, int index,...)
 
bool ECPGset_desc_header (int lineno, const char *desc_name, int count)
 
static void set_desc_attr (struct descriptor_item *desc_item, struct variable *var, char *tobeinserted)
 
bool ECPGset_desc (int lineno, const char *desc_name, int index,...)
 
bool ECPGdeallocate_desc (int line, const char *name)
 
bool ECPGallocate_desc (int line, const char *name)
 
struct descriptorecpg_find_desc (int line, const char *name)
 
bool ECPGdescribe (int line, int compat, bool input, const char *connection_name, const char *stmt_name,...)
 

Variables

static struct descriptorall_descriptors = NULL
 

Macro Definition Documentation

◆ get_descriptors

#define get_descriptors ( )    (all_descriptors)

Definition at line 55 of file descriptor.c.

◆ POSTGRES_ECPG_INTERNAL

#define POSTGRES_ECPG_INTERNAL

Definition at line 6 of file descriptor.c.

◆ RETURN_IF_NO_DATA

#define RETURN_IF_NO_DATA
Value:
if (ntuples < 1) \
{ \
va_end(args); \
ecpg_raise(lineno, ECPG_NOT_FOUND, ECPG_SQLSTATE_NO_DATA, NULL); \
return false; \
}
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
#define ECPG_SQLSTATE_NO_DATA

Definition at line 233 of file descriptor.c.

◆ set_descriptors

#define set_descriptors (   value)    do { all_descriptors = (value); } while(0)

Definition at line 56 of file descriptor.c.

Function Documentation

◆ descriptor_free()

static void descriptor_free ( struct descriptor desc)
static

Definition at line 735 of file descriptor.c.

736 {
737  struct descriptor_item *desc_item;
738 
739  for (desc_item = desc->items; desc_item;)
740  {
741  struct descriptor_item *di;
742 
743  ecpg_free(desc_item->data);
744  di = desc_item;
745  desc_item = desc_item->next;
746  ecpg_free(di);
747  }
748 
749  ecpg_free(desc->name);
750  PQclear(desc->result);
751  ecpg_free(desc);
752 }
void ecpg_free(void *ptr)
Definition: memory.c:13
struct descriptor_item * next
struct descriptor_item * items
PGresult * result

References descriptor_item::data, ecpg_free(), descriptor::items, descriptor::name, descriptor_item::next, PQclear(), and descriptor::result.

Referenced by ECPGdeallocate_desc().

◆ ecpg_dynamic_type_DDT()

static unsigned int ecpg_dynamic_type_DDT ( Oid  type)
static

Definition at line 71 of file descriptor.c.

72 {
73  switch (type)
74  {
75  case DATEOID:
76  return SQL3_DDT_DATE;
77  case TIMEOID:
78  return SQL3_DDT_TIME;
79  case TIMESTAMPOID:
80  return SQL3_DDT_TIMESTAMP;
81  case TIMESTAMPTZOID:
83  case TIMETZOID:
85  default:
86  return SQL3_DDT_ILLEGAL;
87  }
88 }
@ SQL3_DDT_TIME_WITH_TIME_ZONE
Definition: sql3types.h:36
@ SQL3_DDT_DATE
Definition: sql3types.h:33
@ SQL3_DDT_TIMESTAMP
Definition: sql3types.h:35
@ SQL3_DDT_ILLEGAL
Definition: sql3types.h:39
@ SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE
Definition: sql3types.h:37
@ SQL3_DDT_TIME
Definition: sql3types.h:34

References SQL3_DDT_DATE, SQL3_DDT_ILLEGAL, SQL3_DDT_TIME, SQL3_DDT_TIME_WITH_TIME_ZONE, SQL3_DDT_TIMESTAMP, SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE, and generate_unaccent_rules::type.

Referenced by ECPGget_desc().

◆ ecpg_find_desc()

struct descriptor* ecpg_find_desc ( int  line,
const char *  name 
)

Definition at line 842 of file descriptor.c.

843 {
844  struct descriptor *desc;
845 
846  for (desc = get_descriptors(); desc; desc = desc->next)
847  {
848  if (strcmp(name, desc->name) == 0)
849  return desc;
850  }
851 
853  return NULL; /* not found */
854 }
#define ECPG_UNKNOWN_DESCRIPTOR
Definition: ecpgerrno.h:42
#define get_descriptors()
Definition: descriptor.c:55
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
const char * name
Definition: encode.c:571
struct descriptor * next

References ecpg_raise(), ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, ECPG_UNKNOWN_DESCRIPTOR, get_descriptors, name, descriptor::name, and descriptor::next.

Referenced by ecpg_build_params(), ecpg_process_output(), ecpg_result_by_descriptor(), ECPGdescribe(), ECPGset_desc(), and ECPGset_desc_header().

◆ ecpg_result_by_descriptor()

static PGresult* ecpg_result_by_descriptor ( int  line,
const char *  name 
)
static

Definition at line 61 of file descriptor.c.

62 {
63  struct descriptor *desc = ecpg_find_desc(line, name);
64 
65  if (desc == NULL)
66  return NULL;
67  return desc->result;
68 }
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:842

References ecpg_find_desc(), name, and descriptor::result.

Referenced by ECPGget_desc(), and ECPGget_desc_header().

◆ ECPGallocate_desc()

bool ECPGallocate_desc ( int  line,
const char *  name 
)

Definition at line 802 of file descriptor.c.

803 {
804  struct descriptor *new;
805  struct sqlca_t *sqlca = ECPGget_sqlca();
806 
807  if (sqlca == NULL)
808  {
811  return false;
812  }
813 
815  new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
816  if (!new)
817  return false;
818  new->next = get_descriptors();
819  new->name = ecpg_alloc(strlen(name) + 1, line);
820  if (!new->name)
821  {
822  ecpg_free(new);
823  return false;
824  }
825  new->count = -1;
826  new->items = NULL;
827  new->result = PQmakeEmptyPGresult(NULL, 0);
828  if (!new->result)
829  {
830  ecpg_free(new->name);
831  ecpg_free(new);
833  return false;
834  }
835  strcpy(new->name, name);
836  set_descriptors(new);
837  return true;
838 }
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
#define set_descriptors(value)
Definition: descriptor.c:56
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:98
PGresult * PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
Definition: fe-exec.c:157
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:141
#define sqlca
Definition: sqlca.h:59
Definition: sqlca.h:20

References ecpg_alloc(), ecpg_free(), ecpg_init_sqlca(), ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPGget_sqlca(), get_descriptors, name, PQmakeEmptyPGresult(), set_descriptors, and sqlca.

Referenced by execute_test(), fn(), and main().

◆ ECPGdeallocate_desc()

bool ECPGdeallocate_desc ( int  line,
const char *  name 
)

Definition at line 755 of file descriptor.c.

756 {
757  struct descriptor *desc;
758  struct descriptor *prev;
759  struct sqlca_t *sqlca = ECPGget_sqlca();
760 
761  if (sqlca == NULL)
762  {
765  return false;
766  }
767 
769  for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
770  {
771  if (strcmp(name, desc->name) == 0)
772  {
773  if (prev)
774  prev->next = desc->next;
775  else
776  set_descriptors(desc->next);
777  descriptor_free(desc);
778  return true;
779  }
780  }
782  return false;
783 }
static void descriptor_free(struct descriptor *desc)
Definition: descriptor.c:735

References descriptor_free(), ecpg_init_sqlca(), ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME, ECPG_UNKNOWN_DESCRIPTOR, ECPGget_sqlca(), get_descriptors, name, descriptor::name, descriptor::next, set_descriptors, and sqlca.

Referenced by execute_test(), fn(), and main().

◆ ECPGdescribe()

bool ECPGdescribe ( int  line,
int  compat,
bool  input,
const char *  connection_name,
const char *  stmt_name,
  ... 
)

Definition at line 857 of file descriptor.c.

858 {
859  bool ret = false;
860  struct connection *con;
861  struct prepared_statement *prep;
862  PGresult *res;
863  va_list args;
864 
865  /* DESCRIBE INPUT is not yet supported */
866  if (input)
867  {
869  return ret;
870  }
871 
872  con = ecpg_get_connection(connection_name);
873  if (!con)
874  {
876  connection_name ? connection_name : ecpg_gettext("NULL"));
877  return ret;
878  }
879  prep = ecpg_find_prepared_statement(stmt_name, con, NULL);
880  if (!prep)
881  {
883  return ret;
884  }
885 
886  va_start(args, stmt_name);
887 
888  for (;;)
889  {
890  enum ECPGttype type;
891  void *ptr;
892 
893  /* variable type */
894  type = va_arg(args, enum ECPGttype);
895 
896  if (type == ECPGt_EORT)
897  break;
898 
899  /* rest of variable parameters */
900  ptr = va_arg(args, void *);
901  (void) va_arg(args, long); /* skip args */
902  (void) va_arg(args, long);
903  (void) va_arg(args, long);
904 
905  /* variable indicator */
906  (void) va_arg(args, enum ECPGttype);
907  (void) va_arg(args, void *); /* skip args */
908  (void) va_arg(args, long);
909  (void) va_arg(args, long);
910  (void) va_arg(args, long);
911 
912  switch (type)
913  {
914  case ECPGt_descriptor:
915  {
916  char *name = ptr;
917  struct descriptor *desc = ecpg_find_desc(line, name);
918 
919  if (desc == NULL)
920  break;
921 
922  res = PQdescribePrepared(con->connection, stmt_name);
923  if (!ecpg_check_PQresult(res, line, con->connection, compat))
924  break;
925 
926  PQclear(desc->result);
927 
928  desc->result = res;
929  ret = true;
930  break;
931  }
932  case ECPGt_sqlda:
933  {
934  if (INFORMIX_MODE(compat))
935  {
936  struct sqlda_compat **_sqlda = ptr;
937  struct sqlda_compat *sqlda;
938 
939  res = PQdescribePrepared(con->connection, stmt_name);
940  if (!ecpg_check_PQresult(res, line, con->connection, compat))
941  break;
942 
943  sqlda = ecpg_build_compat_sqlda(line, res, -1, compat);
944  if (sqlda)
945  {
946  struct sqlda_compat *sqlda_old = *_sqlda;
947  struct sqlda_compat *sqlda_old1;
948 
949  while (sqlda_old)
950  {
951  sqlda_old1 = sqlda_old->desc_next;
952  free(sqlda_old);
953  sqlda_old = sqlda_old1;
954  }
955 
956  *_sqlda = sqlda;
957  ret = true;
958  }
959 
960  PQclear(res);
961  }
962  else
963  {
964  struct sqlda_struct **_sqlda = ptr;
965  struct sqlda_struct *sqlda;
966 
967  res = PQdescribePrepared(con->connection, stmt_name);
968  if (!ecpg_check_PQresult(res, line, con->connection, compat))
969  break;
970 
971  sqlda = ecpg_build_native_sqlda(line, res, -1, compat);
972  if (sqlda)
973  {
974  struct sqlda_struct *sqlda_old = *_sqlda;
975  struct sqlda_struct *sqlda_old1;
976 
977  while (sqlda_old)
978  {
979  sqlda_old1 = sqlda_old->desc_next;
980  free(sqlda_old);
981  sqlda_old = sqlda_old1;
982  }
983 
984  *_sqlda = sqlda;
985  ret = true;
986  }
987 
988  PQclear(res);
989  }
990  break;
991  }
992  default:
993  /* nothing else may come */
994  ;
995  }
996  }
997 
998  va_end(args);
999 
1000  return ret;
1001 }
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:79
enum COMPAT_MODE compat
Definition: ecpg.c:25
#define ECPG_UNSUPPORTED
Definition: ecpgerrno.h:18
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
#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)
struct sqlda_struct * ecpg_build_native_sqlda(int line, PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:412
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
struct prepared_statement * ecpg_find_prepared_statement(const char *name, struct connection *con, struct prepared_statement **prev_)
Definition: prepare.c:239
#define INFORMIX_MODE(X)
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
struct sqlda_compat * ecpg_build_compat_sqlda(int line, PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:205
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
ECPGttype
Definition: ecpgtype.h:42
@ ECPGt_sqlda
Definition: ecpgtype.h:66
@ ECPGt_EORT
Definition: ecpgtype.h:63
@ ECPGt_descriptor
Definition: ecpgtype.h:59
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2418
#define free(a)
Definition: header.h:65
va_end(args)
va_start(args, fmt)
PGconn * connection
Definition: type.h:108
struct sqlda_compat * desc_next
Definition: sqlda-compat.h:43
struct sqlda_struct * desc_next
Definition: sqlda-native.h:39

References generate_unaccent_rules::args, compat, connection::connection, sqlda_compat::desc_next, sqlda_struct::desc_next, ecpg_build_compat_sqlda(), ecpg_build_native_sqlda(), ecpg_check_PQresult(), ecpg_find_desc(), ecpg_find_prepared_statement(), ecpg_get_connection(), ecpg_gettext, ECPG_INVALID_STMT, ECPG_NO_CONN, ecpg_raise(), ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, ECPG_UNSUPPORTED, ECPGt_descriptor, ECPGt_EORT, ECPGt_sqlda, free, INFORMIX_MODE, name, PQclear(), PQdescribePrepared(), res, descriptor::result, generate_unaccent_rules::type, va_end(), and va_start().

Referenced by execute_test(), and main().

◆ ECPGget_desc()

bool ECPGget_desc ( int  lineno,
const char *  desc_name,
int  index,
  ... 
)

Definition at line 241 of file descriptor.c.

242 {
243  va_list args;
244  PGresult *ECPGresult;
245  enum ECPGdtype type;
246  int ntuples,
247  act_tuple;
248  struct variable data_var;
249  struct sqlca_t *sqlca = ECPGget_sqlca();
250 
251  if (sqlca == NULL)
252  {
255  return false;
256  }
257 
258  va_start(args, index);
260  ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
261  if (!ECPGresult)
262  {
263  va_end(args);
264  return false;
265  }
266 
267  ntuples = PQntuples(ECPGresult);
268 
269  if (index < 1 || index > PQnfields(ECPGresult))
270  {
272  va_end(args);
273  return false;
274  }
275 
276  ecpg_log("ECPGget_desc: reading items for tuple %d\n", index);
277  --index;
278 
279  type = va_arg(args, enum ECPGdtype);
280 
281  memset(&data_var, 0, sizeof data_var);
282  data_var.type = ECPGt_EORT;
283  data_var.ind_type = ECPGt_NO_INDICATOR;
284 
285  while (type != ECPGd_EODT)
286  {
287  char type_str[20];
288  long varcharsize;
289  long offset;
290  long arrsize;
291  enum ECPGttype vartype;
292  void *var;
293 
294  vartype = va_arg(args, enum ECPGttype);
295  var = va_arg(args, void *);
296  varcharsize = va_arg(args, long);
297  arrsize = va_arg(args, long);
298  offset = va_arg(args, long);
299 
300  switch (type)
301  {
302  case (ECPGd_indicator):
304  data_var.ind_type = vartype;
305  data_var.ind_pointer = var;
306  data_var.ind_varcharsize = varcharsize;
307  data_var.ind_arrsize = arrsize;
308  data_var.ind_offset = offset;
309  if (data_var.ind_arrsize == 0 || data_var.ind_varcharsize == 0)
310  data_var.ind_value = *((void **) (data_var.ind_pointer));
311  else
312  data_var.ind_value = data_var.ind_pointer;
313  break;
314 
315  case ECPGd_data:
317  data_var.type = vartype;
318  data_var.pointer = var;
319  data_var.varcharsize = varcharsize;
320  data_var.arrsize = arrsize;
321  data_var.offset = offset;
322  if (data_var.arrsize == 0 || data_var.varcharsize == 0)
323  data_var.value = *((void **) (data_var.pointer));
324  else
325  data_var.value = data_var.pointer;
326  break;
327 
328  case ECPGd_name:
329  if (!get_char_item(lineno, var, vartype, PQfname(ECPGresult, index), varcharsize))
330  {
331  va_end(args);
332  return false;
333  }
334 
335  ecpg_log("ECPGget_desc: NAME = %s\n", PQfname(ECPGresult, index));
336  break;
337 
338  case ECPGd_nullable:
339  if (!get_int_item(lineno, var, vartype, 1))
340  {
341  va_end(args);
342  return false;
343  }
344 
345  break;
346 
347  case ECPGd_key_member:
348  if (!get_int_item(lineno, var, vartype, 0))
349  {
350  va_end(args);
351  return false;
352  }
353 
354  break;
355 
356  case ECPGd_scale:
357  if (!get_int_item(lineno, var, vartype, (PQfmod(ECPGresult, index) - VARHDRSZ) & 0xffff))
358  {
359  va_end(args);
360  return false;
361  }
362 
363  ecpg_log("ECPGget_desc: SCALE = %d\n", (PQfmod(ECPGresult, index) - VARHDRSZ) & 0xffff);
364  break;
365 
366  case ECPGd_precision:
367  if (!get_int_item(lineno, var, vartype, PQfmod(ECPGresult, index) >> 16))
368  {
369  va_end(args);
370  return false;
371  }
372 
373  ecpg_log("ECPGget_desc: PRECISION = %d\n", PQfmod(ECPGresult, index) >> 16);
374  break;
375 
376  case ECPGd_octet:
377  if (!get_int_item(lineno, var, vartype, PQfsize(ECPGresult, index)))
378  {
379  va_end(args);
380  return false;
381  }
382 
383  ecpg_log("ECPGget_desc: OCTET_LENGTH = %d\n", PQfsize(ECPGresult, index));
384  break;
385 
386  case ECPGd_length:
387  if (!get_int_item(lineno, var, vartype, PQfmod(ECPGresult, index) - VARHDRSZ))
388  {
389  va_end(args);
390  return false;
391  }
392 
393  ecpg_log("ECPGget_desc: LENGTH = %d\n", PQfmod(ECPGresult, index) - VARHDRSZ);
394  break;
395 
396  case ECPGd_type:
397  if (!get_int_item(lineno, var, vartype, ecpg_dynamic_type(PQftype(ECPGresult, index))))
398  {
399  va_end(args);
400  return false;
401  }
402 
403  ecpg_log("ECPGget_desc: TYPE = %d\n", ecpg_dynamic_type(PQftype(ECPGresult, index)));
404  break;
405 
406  case ECPGd_di_code:
407  if (!get_int_item(lineno, var, vartype, ecpg_dynamic_type_DDT(PQftype(ECPGresult, index))))
408  {
409  va_end(args);
410  return false;
411  }
412 
413  ecpg_log("ECPGget_desc: TYPE = %d\n", ecpg_dynamic_type_DDT(PQftype(ECPGresult, index)));
414  break;
415 
416  case ECPGd_cardinality:
417  if (!get_int_item(lineno, var, vartype, PQntuples(ECPGresult)))
418  {
419  va_end(args);
420  return false;
421  }
422 
423  ecpg_log("ECPGget_desc: CARDINALITY = %d\n", PQntuples(ECPGresult));
424  break;
425 
426  case ECPGd_ret_length:
427  case ECPGd_ret_octet:
428 
430 
431  /*
432  * this is like ECPGstore_result
433  */
434  if (arrsize > 0 && ntuples > arrsize)
435  {
436  ecpg_log("ECPGget_desc on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
437  lineno, ntuples, arrsize);
439  va_end(args);
440  return false;
441  }
442  /* allocate storage if needed */
443  if (arrsize == 0 && *(void **) var == NULL)
444  {
445  void *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
446 
447  if (!mem)
448  {
449  va_end(args);
450  return false;
451  }
452  *(void **) var = mem;
453  var = mem;
454  }
455 
456  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
457  {
458  if (!get_int_item(lineno, var, vartype, PQgetlength(ECPGresult, act_tuple, index)))
459  {
460  va_end(args);
461  return false;
462  }
463  var = (char *) var + offset;
464  ecpg_log("ECPGget_desc: RETURNED[%d] = %d\n", act_tuple, PQgetlength(ECPGresult, act_tuple, index));
465  }
466  break;
467 
468  default:
469  snprintf(type_str, sizeof(type_str), "%d", type);
471  va_end(args);
472  return false;
473  }
474 
475  type = va_arg(args, enum ECPGdtype);
476  }
477 
478  if (data_var.type != ECPGt_EORT)
479  {
480  struct statement stmt;
481 
482  memset(&stmt, 0, sizeof stmt);
483  stmt.lineno = lineno;
484 
485  /* Make sure we do NOT honor the locale for numeric input */
486  /* since the database gives the standard decimal point */
487  /* (see comments in execute.c) */
488 #ifdef HAVE_USELOCALE
489 
490  /*
491  * To get here, the above PQnfields() test must have found nonzero
492  * fields. One needs a connection to create such a descriptor. (EXEC
493  * SQL SET DESCRIPTOR can populate the descriptor's "items", but it
494  * can't change the descriptor's PQnfields().) Any successful
495  * connection initializes ecpg_clocale.
496  */
497  Assert(ecpg_clocale);
498  stmt.oldlocale = uselocale(ecpg_clocale);
499 #else
500 #ifdef HAVE__CONFIGTHREADLOCALE
501  stmt.oldthreadlocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
502 #endif
503  stmt.oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
504  setlocale(LC_NUMERIC, "C");
505 #endif
506 
507  /* desperate try to guess something sensible */
508  stmt.connection = ecpg_get_connection(NULL);
509  ecpg_store_result(ECPGresult, index, &stmt, &data_var);
510 
511 #ifdef HAVE_USELOCALE
512  if (stmt.oldlocale != (locale_t) 0)
513  uselocale(stmt.oldlocale);
514 #else
515  if (stmt.oldlocale)
516  {
517  setlocale(LC_NUMERIC, stmt.oldlocale);
518  ecpg_free(stmt.oldlocale);
519  }
520 #ifdef HAVE__CONFIGTHREADLOCALE
521  if (stmt.oldthreadlocale != -1)
522  (void) _configthreadlocale(stmt.oldthreadlocale);
523 #endif
524 #endif
525  }
526  else if (data_var.ind_type != ECPGt_NO_INDICATOR && data_var.ind_pointer != NULL)
527 
528  /*
529  * ind_type != NO_INDICATOR should always have ind_pointer != NULL but
530  * since this might be changed manually in the .c file let's play it
531  * safe
532  */
533  {
534  /*
535  * this is like ECPGstore_result but since we don't have a data
536  * variable at hand, we can't call it
537  */
538  if (data_var.ind_arrsize > 0 && ntuples > data_var.ind_arrsize)
539  {
540  ecpg_log("ECPGget_desc on line %d: incorrect number of matches (indicator); %d don't fit into array of %ld\n",
541  lineno, ntuples, data_var.ind_arrsize);
543  va_end(args);
544  return false;
545  }
546 
547  /* allocate storage if needed */
548  if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
549  {
550  void *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
551 
552  if (!mem)
553  {
554  va_end(args);
555  return false;
556  }
557  *(void **) data_var.ind_pointer = mem;
558  data_var.ind_value = mem;
559  }
560 
561  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
562  {
563  if (!get_int_item(lineno, data_var.ind_value, data_var.ind_type, -PQgetisnull(ECPGresult, act_tuple, index)))
564  {
565  va_end(args);
566  return false;
567  }
568  data_var.ind_value = (char *) data_var.ind_value + data_var.ind_offset;
569  ecpg_log("ECPGget_desc: INDICATOR[%d] = %d\n", act_tuple, -PQgetisnull(ECPGresult, act_tuple, index));
570  }
571  }
572  sqlca->sqlerrd[2] = ntuples;
573  va_end(args);
574  return true;
575 }
#define VARHDRSZ
Definition: c.h:676
#define ECPG_UNKNOWN_DESCRIPTOR_ITEM
Definition: ecpgerrno.h:44
#define ECPG_INVALID_DESCRIPTOR_INDEX
Definition: ecpgerrno.h:43
#define ECPG_TOO_MANY_MATCHES
Definition: ecpgerrno.h:21
static bool get_char_item(int lineno, void *var, enum ECPGttype vartype, char *value, int varcharsize)
Definition: descriptor.c:201
#define RETURN_IF_NO_DATA
Definition: descriptor.c:233
static PGresult * ecpg_result_by_descriptor(int line, const char *name)
Definition: descriptor.c:61
static bool get_int_item(int lineno, void *var, enum ECPGttype vartype, int value)
Definition: descriptor.c:115
static unsigned int ecpg_dynamic_type_DDT(Oid type)
Definition: descriptor.c:71
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:303
char * ecpg_auto_alloc(long size, int lineno)
Definition: memory.c:108
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION
#define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX
char * ecpg_strdup(const char *string, int lineno)
Definition: memory.c:47
int ecpg_dynamic_type(Oid type)
Definition: typename.c:73
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
ECPGdtype
Definition: ecpgtype.h:72
@ ECPGd_scale
Definition: ecpgtype.h:86
@ ECPGd_precision
Definition: ecpgtype.h:83
@ ECPGd_length
Definition: ecpgtype.h:79
@ ECPGd_nullable
Definition: ecpgtype.h:81
@ ECPGd_type
Definition: ecpgtype.h:87
@ ECPGd_cardinality
Definition: ecpgtype.h:89
@ ECPGd_indicator
Definition: ecpgtype.h:77
@ ECPGd_ret_length
Definition: ecpgtype.h:84
@ ECPGd_di_code
Definition: ecpgtype.h:75
@ ECPGd_name
Definition: ecpgtype.h:80
@ ECPGd_key_member
Definition: ecpgtype.h:78
@ ECPGd_EODT
Definition: ecpgtype.h:88
@ ECPGd_octet
Definition: ecpgtype.h:82
@ ECPGd_ret_octet
Definition: ecpgtype.h:85
@ ECPGd_data
Definition: ecpgtype.h:74
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3716
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3548
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3396
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3570
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3730
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3318
int PQfsize(const PGresult *res, int field_num)
Definition: fe-exec.c:3559
Assert(fmt[strlen(fmt) - 1] !='\n')
#define snprintf
Definition: port.h:238
Definition: type.h:95
#define locale_t
Definition: win32_port.h:419
#define setlocale(a, b)
Definition: win32_port.h:464

References generate_unaccent_rules::args, variable::arrsize, Assert(), statement::connection, ecpg_auto_alloc(), ecpg_dynamic_type(), ecpg_dynamic_type_DDT(), ecpg_free(), ecpg_get_connection(), ecpg_init_sqlca(), ECPG_INVALID_DESCRIPTOR_INDEX, ecpg_log(), ECPG_OUT_OF_MEMORY, ecpg_raise(), ecpg_result_by_descriptor(), ECPG_SQLSTATE_CARDINALITY_VIOLATION, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX, ecpg_store_result(), ecpg_strdup(), ECPG_TOO_MANY_MATCHES, ECPG_UNKNOWN_DESCRIPTOR_ITEM, ECPGd_cardinality, ECPGd_data, ECPGd_di_code, ECPGd_EODT, ECPGd_indicator, ECPGd_key_member, ECPGd_length, ECPGd_name, ECPGd_nullable, ECPGd_octet, ECPGd_precision, ECPGd_ret_length, ECPGd_ret_octet, ECPGd_scale, ECPGd_type, ECPGget_sqlca(), ECPGt_EORT, ECPGt_NO_INDICATOR, get_char_item(), get_int_item(), variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, statement::lineno, locale_t, variable::offset, statement::oldlocale, variable::pointer, PQfmod(), PQfname(), PQfsize(), PQftype(), PQgetisnull(), PQgetlength(), PQnfields(), PQntuples(), RETURN_IF_NO_DATA, setlocale, snprintf, sqlca, generate_unaccent_rules::type, variable::type, va_end(), va_start(), variable::value, variable::varcharsize, and VARHDRSZ.

Referenced by execute_test(), and main().

◆ ECPGget_desc_header()

bool ECPGget_desc_header ( int  lineno,
const char *  desc_name,
int *  count 
)

Definition at line 91 of file descriptor.c.

92 {
93  PGresult *ECPGresult;
94  struct sqlca_t *sqlca = ECPGget_sqlca();
95 
96  if (sqlca == NULL)
97  {
100  return false;
101  }
102 
104  ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
105  if (!ECPGresult)
106  return false;
107 
108  *count = PQnfields(ECPGresult);
109  sqlca->sqlerrd[2] = 1;
110  ecpg_log("ECPGget_desc_header: found %d attributes\n", *count);
111  return true;
112 }

References ecpg_init_sqlca(), ecpg_log(), ECPG_OUT_OF_MEMORY, ecpg_raise(), ecpg_result_by_descriptor(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPGget_sqlca(), PQnfields(), and sqlca.

Referenced by execute_test(), and main().

◆ ECPGset_desc()

bool ECPGset_desc ( int  lineno,
const char *  desc_name,
int  index,
  ... 
)

Definition at line 612 of file descriptor.c.

613 {
614  va_list args;
615  struct descriptor *desc;
616  struct descriptor_item *desc_item;
617  struct variable *var;
618 
619  desc = ecpg_find_desc(lineno, desc_name);
620  if (desc == NULL)
621  return false;
622 
623  for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
624  {
625  if (desc_item->num == index)
626  break;
627  }
628 
629  if (desc_item == NULL)
630  {
631  desc_item = (struct descriptor_item *) ecpg_alloc(sizeof(*desc_item), lineno);
632  if (!desc_item)
633  return false;
634  desc_item->num = index;
635  if (desc->count < index)
636  desc->count = index;
637  desc_item->next = desc->items;
638  desc->items = desc_item;
639  }
640 
641  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
642  return false;
643 
644  va_start(args, index);
645 
646  for (;;)
647  {
648  enum ECPGdtype itemtype;
649  char *tobeinserted = NULL;
650 
651  itemtype = va_arg(args, enum ECPGdtype);
652 
653  if (itemtype == ECPGd_EODT)
654  break;
655 
656  var->type = va_arg(args, enum ECPGttype);
657  var->pointer = va_arg(args, char *);
658 
659  var->varcharsize = va_arg(args, long);
660  var->arrsize = va_arg(args, long);
661  var->offset = va_arg(args, long);
662 
663  if (var->arrsize == 0 || var->varcharsize == 0)
664  var->value = *((char **) (var->pointer));
665  else
666  var->value = var->pointer;
667 
668  /*
669  * negative values are used to indicate an array without given bounds
670  */
671  /* reset to zero for us */
672  if (var->arrsize < 0)
673  var->arrsize = 0;
674  if (var->varcharsize < 0)
675  var->varcharsize = 0;
676 
677  var->next = NULL;
678 
679  switch (itemtype)
680  {
681  case ECPGd_data:
682  {
683  if (!ecpg_store_input(lineno, true, var, &tobeinserted, false))
684  {
685  ecpg_free(var);
686  va_end(args);
687  return false;
688  }
689 
690  set_desc_attr(desc_item, var, tobeinserted);
691  tobeinserted = NULL;
692  break;
693  }
694 
695  case ECPGd_indicator:
696  set_int_item(lineno, &desc_item->indicator, var->pointer, var->type);
697  break;
698 
699  case ECPGd_length:
700  set_int_item(lineno, &desc_item->length, var->pointer, var->type);
701  break;
702 
703  case ECPGd_precision:
704  set_int_item(lineno, &desc_item->precision, var->pointer, var->type);
705  break;
706 
707  case ECPGd_scale:
708  set_int_item(lineno, &desc_item->scale, var->pointer, var->type);
709  break;
710 
711  case ECPGd_type:
712  set_int_item(lineno, &desc_item->type, var->pointer, var->type);
713  break;
714 
715  default:
716  {
717  char type_str[20];
718 
719  snprintf(type_str, sizeof(type_str), "%d", itemtype);
721  ecpg_free(var);
722  va_end(args);
723  return false;
724  }
725  }
726  }
727  ecpg_free(var);
728  va_end(args);
729 
730  return true;
731 }
static bool set_int_item(int lineno, int *target, const void *var, enum ECPGttype vartype)
Definition: descriptor.c:158
static void set_desc_attr(struct descriptor_item *desc_item, struct variable *var, char *tobeinserted)
Definition: descriptor.c:591
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:506
void * value
long varcharsize
struct variable * next
enum ECPGttype type
void * pointer

References generate_unaccent_rules::args, variable::arrsize, descriptor::count, ecpg_alloc(), ecpg_find_desc(), ecpg_free(), ecpg_raise(), ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_store_input(), ECPG_UNKNOWN_DESCRIPTOR_ITEM, ECPGd_data, ECPGd_EODT, ECPGd_indicator, ECPGd_length, ECPGd_precision, ECPGd_scale, ECPGd_type, descriptor_item::indicator, descriptor::items, descriptor_item::length, descriptor_item::next, variable::next, descriptor_item::num, variable::offset, variable::pointer, descriptor_item::precision, descriptor_item::scale, set_desc_attr(), set_int_item(), snprintf, descriptor_item::type, variable::type, va_end(), va_start(), variable::value, and variable::varcharsize.

Referenced by main().

◆ ECPGset_desc_header()

bool ECPGset_desc_header ( int  lineno,
const char *  desc_name,
int  count 
)

Definition at line 580 of file descriptor.c.

581 {
582  struct descriptor *desc = ecpg_find_desc(lineno, desc_name);
583 
584  if (desc == NULL)
585  return false;
586  desc->count = count;
587  return true;
588 }

References descriptor::count, and ecpg_find_desc().

Referenced by main().

◆ get_char_item()

static bool get_char_item ( int  lineno,
void *  var,
enum ECPGttype  vartype,
char *  value,
int  varcharsize 
)
static

Definition at line 201 of file descriptor.c.

202 {
203  switch (vartype)
204  {
205  case ECPGt_char:
206  case ECPGt_unsigned_char:
207  case ECPGt_string:
208  strncpy((char *) var, value, varcharsize);
209  break;
210  case ECPGt_varchar:
211  {
212  struct ECPGgeneric_varchar *variable =
213  (struct ECPGgeneric_varchar *) var;
214 
215  if (varcharsize == 0)
216  memcpy(variable->arr, value, strlen(value));
217  else
218  strncpy(variable->arr, value, varcharsize);
219 
220  variable->len = strlen(value);
221  if (varcharsize > 0 && variable->len > varcharsize)
222  variable->len = varcharsize;
223  }
224  break;
225  default:
227  return false;
228  }
229 
230  return true;
231 }
#define ECPG_VAR_NOT_CHAR
Definition: ecpgerrno.h:46
#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_unsigned_char
Definition: ecpgtype.h:43
@ ECPGt_char
Definition: ecpgtype.h:43
@ ECPGt_string
Definition: ecpgtype.h:65
static struct @143 value

References ecpg_raise(), ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION, ECPG_VAR_NOT_CHAR, ECPGt_char, ECPGt_string, ECPGt_unsigned_char, ECPGt_varchar, and value.

Referenced by ECPGget_desc().

◆ get_int_item()

static bool get_int_item ( int  lineno,
void *  var,
enum ECPGttype  vartype,
int  value 
)
static

Definition at line 115 of file descriptor.c.

116 {
117  switch (vartype)
118  {
119  case ECPGt_short:
120  *(short *) var = (short) value;
121  break;
122  case ECPGt_int:
123  *(int *) var = (int) value;
124  break;
125  case ECPGt_long:
126  *(long *) var = (long) value;
127  break;
129  *(unsigned short *) var = (unsigned short) value;
130  break;
131  case ECPGt_unsigned_int:
132  *(unsigned int *) var = (unsigned int) value;
133  break;
134  case ECPGt_unsigned_long:
135  *(unsigned long *) var = (unsigned long) value;
136  break;
137  case ECPGt_long_long:
138  *(long long int *) var = (long long int) value;
139  break;
141  *(unsigned long long int *) var = (unsigned long long int) value;
142  break;
143  case ECPGt_float:
144  *(float *) var = (float) value;
145  break;
146  case ECPGt_double:
147  *(double *) var = (double) value;
148  break;
149  default:
151  return false;
152  }
153 
154  return true;
155 }
#define ECPG_VAR_NOT_NUMERIC
Definition: ecpgerrno.h:45
@ ECPGt_float
Definition: ecpgtype.h:47
@ ECPGt_long_long
Definition: ecpgtype.h:45
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_unsigned_short
Definition: ecpgtype.h:43
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_long
Definition: ecpgtype.h:44
@ ECPGt_double
Definition: ecpgtype.h:47
@ ECPGt_unsigned_long
Definition: ecpgtype.h:44
@ ECPGt_unsigned_long_long
Definition: ecpgtype.h:45
@ ECPGt_unsigned_int
Definition: ecpgtype.h:44

References ecpg_raise(), ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION, ECPG_VAR_NOT_NUMERIC, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_long, ECPGt_long_long, ECPGt_short, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, and value.

Referenced by ECPGget_desc().

◆ set_desc_attr()

static void set_desc_attr ( struct descriptor_item desc_item,
struct variable var,
char *  tobeinserted 
)
static

Definition at line 591 of file descriptor.c.

593 {
594  if (var->type != ECPGt_bytea)
595  desc_item->is_binary = false;
596 
597  else
598  {
599  struct ECPGgeneric_bytea *variable =
600  (struct ECPGgeneric_bytea *) (var->value);
601 
602  desc_item->is_binary = true;
603  desc_item->data_len = variable->len;
604  }
605 
606  ecpg_free(desc_item->data); /* free() takes care of a potential NULL value */
607  desc_item->data = (char *) tobeinserted;
608 }
@ ECPGt_bytea
Definition: ecpgtype.h:67

References descriptor_item::data, descriptor_item::data_len, ecpg_free(), ECPGt_bytea, descriptor_item::is_binary, variable::type, and variable::value.

Referenced by ECPGset_desc().

◆ set_int_item()

static bool set_int_item ( int  lineno,
int *  target,
const void *  var,
enum ECPGttype  vartype 
)
static

Definition at line 158 of file descriptor.c.

159 {
160  switch (vartype)
161  {
162  case ECPGt_short:
163  *target = *(const short *) var;
164  break;
165  case ECPGt_int:
166  *target = *(const int *) var;
167  break;
168  case ECPGt_long:
169  *target = *(const long *) var;
170  break;
172  *target = *(const unsigned short *) var;
173  break;
174  case ECPGt_unsigned_int:
175  *target = *(const unsigned int *) var;
176  break;
177  case ECPGt_unsigned_long:
178  *target = *(const unsigned long *) var;
179  break;
180  case ECPGt_long_long:
181  *target = *(const long long int *) var;
182  break;
184  *target = *(const unsigned long long int *) var;
185  break;
186  case ECPGt_float:
187  *target = *(const float *) var;
188  break;
189  case ECPGt_double:
190  *target = *(const double *) var;
191  break;
192  default:
194  return false;
195  }
196 
197  return true;
198 }

References ecpg_raise(), ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION, ECPG_VAR_NOT_NUMERIC, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_long, ECPGt_long_long, ECPGt_short, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, and ECPGt_unsigned_short.

Referenced by ECPGset_desc().

Variable Documentation

◆ all_descriptors

struct descriptor* all_descriptors = NULL
static

Definition at line 53 of file descriptor.c.