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 "ecpgtype.h"
#include "ecpglib.h"
#include "ecpgerrno.h"
#include "extern.h"
#include "sqlca.h"
#include "sqlda.h"
#include "sql3types.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)
 
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 56 of file descriptor.c.

Referenced by ecpg_find_desc(), ECPGallocate_desc(), and ECPGdeallocate_desc().

◆ 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_SQLSTATE_NO_DATA
Definition: extern.h:201
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10

Definition at line 238 of file descriptor.c.

Referenced by ECPGget_desc().

◆ set_descriptors

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

Definition at line 57 of file descriptor.c.

Referenced by ECPGallocate_desc(), and ECPGdeallocate_desc().

Function Documentation

◆ descriptor_free()

static void descriptor_free ( struct descriptor desc)
static

Definition at line 693 of file descriptor.c.

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

Referenced by ECPGdeallocate_desc().

694 {
695  struct descriptor_item *desc_item;
696 
697  for (desc_item = desc->items; desc_item;)
698  {
699  struct descriptor_item *di;
700 
701  ecpg_free(desc_item->data);
702  di = desc_item;
703  desc_item = desc_item->next;
704  ecpg_free(di);
705  }
706 
707  ecpg_free(desc->name);
708  PQclear(desc->result);
709  ecpg_free(desc);
710 }
void ecpg_free(void *)
Definition: memory.c:13
char * data
Definition: extern.h:103
PGresult * result
Definition: extern.h:94
void PQclear(PGresult *res)
Definition: fe-exec.c:671
struct descriptor_item * items
Definition: extern.h:97
struct descriptor_item * next
Definition: extern.h:109
char * name
Definition: extern.h:93

◆ ecpg_dynamic_type_DDT()

static unsigned int ecpg_dynamic_type_DDT ( Oid  type)
static

Definition at line 72 of file descriptor.c.

References SQL3_DDT_DATE, SQL3_DDT_ILLEGAL, SQL3_DDT_TIME, SQL3_DDT_TIME_WITH_TIME_ZONE, SQL3_DDT_TIMESTAMP, and SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE.

Referenced by ECPGget_desc().

73 {
74  switch (type)
75  {
76  case DATEOID:
77  return SQL3_DDT_DATE;
78  case TIMEOID:
79  return SQL3_DDT_TIME;
80  case TIMESTAMPOID:
81  return SQL3_DDT_TIMESTAMP;
82  case TIMESTAMPTZOID:
84  case TIMETZOID:
86  default:
87  return SQL3_DDT_ILLEGAL;
88  }
89 }

◆ ecpg_find_desc()

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

Definition at line 800 of file descriptor.c.

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

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

801 {
802  struct descriptor *desc;
803 
804  for (desc = get_descriptors(); desc; desc = desc->next)
805  {
806  if (strcmp(name, desc->name) == 0)
807  return desc;
808  }
809 
811  return NULL; /* not found */
812 }
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME
Definition: extern.h:214
struct descriptor * next
Definition: extern.h:95
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_UNKNOWN_DESCRIPTOR
Definition: ecpgerrno.h:42
const char * name
Definition: encode.c:521
char * name
Definition: extern.h:93
#define get_descriptors()
Definition: descriptor.c:56

◆ ecpg_result_by_descriptor()

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

Definition at line 62 of file descriptor.c.

References ecpg_find_desc(), and descriptor::result.

Referenced by ECPGget_desc(), and ECPGget_desc_header().

63 {
64  struct descriptor *desc = ecpg_find_desc(line, name);
65 
66  if (desc == NULL)
67  return NULL;
68  return desc->result;
69 }
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:800
PGresult * result
Definition: extern.h:94
const char * name
Definition: encode.c:521

◆ ECPGallocate_desc()

bool ECPGallocate_desc ( int  line,
const char *  name 
)

Definition at line 760 of file descriptor.c.

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

Referenced by fn(), and main().

761 {
762  struct descriptor *new;
763  struct sqlca_t *sqlca = ECPGget_sqlca();
764 
765  if (sqlca == NULL)
766  {
769  return false;
770  }
771 
772  ecpg_init_sqlca(sqlca);
773  new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
774  if (!new)
775  return false;
776  new->next = get_descriptors();
777  new->name = ecpg_alloc(strlen(name) + 1, line);
778  if (!new->name)
779  {
780  ecpg_free(new);
781  return false;
782  }
783  new->count = -1;
784  new->items = NULL;
785  new->result = PQmakeEmptyPGresult(NULL, 0);
786  if (!new->result)
787  {
788  ecpg_free(new->name);
789  ecpg_free(new);
791  return false;
792  }
793  strcpy(new->name, name);
794  set_descriptors(new);
795  return true;
796 }
void ecpg_free(void *)
Definition: memory.c:13
#define sqlca
Definition: sqlca.h:59
#define set_descriptors(value)
Definition: descriptor.c:57
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:222
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:142
Definition: sqlca.h:19
PGresult * PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
Definition: fe-exec.c:142
char * ecpg_alloc(long, int)
Definition: memory.c:19
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:99
const char * name
Definition: encode.c:521
#define get_descriptors()
Definition: descriptor.c:56

◆ ECPGdeallocate_desc()

bool ECPGdeallocate_desc ( int  line,
const char *  name 
)

Definition at line 713 of file descriptor.c.

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, sort-test::list, descriptor::next, next, set_descriptors, and sqlca.

Referenced by fn(), and main().

714 {
715  struct descriptor *desc;
716  struct descriptor *prev;
717  struct sqlca_t *sqlca = ECPGget_sqlca();
718 
719  if (sqlca == NULL)
720  {
723  return false;
724  }
725 
726  ecpg_init_sqlca(sqlca);
727  for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
728  {
729  if (strcmp(name, desc->name) == 0)
730  {
731  if (prev)
732  prev->next = desc->next;
733  else
734  set_descriptors(desc->next);
735  descriptor_free(desc);
736  return true;
737  }
738  }
740  return false;
741 }
#define sqlca
Definition: sqlca.h:59
#define set_descriptors(value)
Definition: descriptor.c:57
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:222
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME
Definition: extern.h:214
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct descriptor * next
Definition: extern.h:95
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:142
Definition: sqlca.h:19
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_UNKNOWN_DESCRIPTOR
Definition: ecpgerrno.h:42
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:99
static void descriptor_free(struct descriptor *desc)
Definition: descriptor.c:693
const char * name
Definition: encode.c:521
#define get_descriptors()
Definition: descriptor.c:56

◆ ECPGdescribe()

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

Definition at line 815 of file descriptor.c.

References generate_unaccent_rules::args, connection::connection, sqlda_struct::desc_next, sqlda_compat::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(), descriptor::result, and generate_unaccent_rules::type.

Referenced by main().

816 {
817  bool ret = false;
818  struct connection *con;
819  struct prepared_statement *prep;
820  PGresult *res;
821  va_list args;
822 
823  /* DESCRIBE INPUT is not yet supported */
824  if (input)
825  {
827  return ret;
828  }
829 
830  con = ecpg_get_connection(connection_name);
831  if (!con)
832  {
834  connection_name ? connection_name : ecpg_gettext("NULL"));
835  return ret;
836  }
837  prep = ecpg_find_prepared_statement(stmt_name, con, NULL);
838  if (!prep)
839  {
841  return ret;
842  }
843 
844  va_start(args, stmt_name);
845 
846  for (;;)
847  {
848  enum ECPGttype type;
849  void *ptr;
850 
851  /* variable type */
852  type = va_arg(args, enum ECPGttype);
853 
854  if (type == ECPGt_EORT)
855  break;
856 
857  /* rest of variable parameters */
858  ptr = va_arg(args, void *);
859  (void) va_arg(args, long); /* skip args */
860  (void) va_arg(args, long);
861  (void) va_arg(args, long);
862 
863  /* variable indicator */
864  (void) va_arg(args, enum ECPGttype);
865  (void) va_arg(args, void *); /* skip args */
866  (void) va_arg(args, long);
867  (void) va_arg(args, long);
868  (void) va_arg(args, long);
869 
870  switch (type)
871  {
872  case ECPGt_descriptor:
873  {
874  char *name = ptr;
875  struct descriptor *desc = ecpg_find_desc(line, name);
876 
877  if (desc == NULL)
878  break;
879 
880  res = PQdescribePrepared(con->connection, stmt_name);
881  if (!ecpg_check_PQresult(res, line, con->connection, compat))
882  break;
883 
884  if (desc->result != NULL)
885  PQclear(desc->result);
886 
887  desc->result = res;
888  ret = true;
889  break;
890  }
891  case ECPGt_sqlda:
892  {
893  if (INFORMIX_MODE(compat))
894  {
895  struct sqlda_compat **_sqlda = ptr;
896  struct sqlda_compat *sqlda;
897 
898  res = PQdescribePrepared(con->connection, stmt_name);
899  if (!ecpg_check_PQresult(res, line, con->connection, compat))
900  break;
901 
902  sqlda = ecpg_build_compat_sqlda(line, res, -1, compat);
903  if (sqlda)
904  {
905  struct sqlda_compat *sqlda_old = *_sqlda;
906  struct sqlda_compat *sqlda_old1;
907 
908  while (sqlda_old)
909  {
910  sqlda_old1 = sqlda_old->desc_next;
911  free(sqlda_old);
912  sqlda_old = sqlda_old1;
913  }
914 
915  *_sqlda = sqlda;
916  ret = true;
917  }
918 
919  PQclear(res);
920  }
921  else
922  {
923  struct sqlda_struct **_sqlda = ptr;
924  struct sqlda_struct *sqlda;
925 
926  res = PQdescribePrepared(con->connection, stmt_name);
927  if (!ecpg_check_PQresult(res, line, con->connection, compat))
928  break;
929 
930  sqlda = ecpg_build_native_sqlda(line, res, -1, compat);
931  if (sqlda)
932  {
933  struct sqlda_struct *sqlda_old = *_sqlda;
934  struct sqlda_struct *sqlda_old1;
935 
936  while (sqlda_old)
937  {
938  sqlda_old1 = sqlda_old->desc_next;
939  free(sqlda_old);
940  sqlda_old = sqlda_old1;
941  }
942 
943  *_sqlda = sqlda;
944  ret = true;
945  }
946 
947  PQclear(res);
948  }
949  break;
950  }
951  default:
952  /* nothing else may come */
953  ;
954  }
955  }
956 
957  va_end(args);
958 
959  return ret;
960 }
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2114
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
Definition: extern.h:221
struct sqlda_struct * desc_next
Definition: sqlda-native.h:39
#define INFORMIX_MODE(X)
Definition: extern.h:23
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
Definition: type.h:102
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
Definition: extern.h:207
struct sqlda_compat * desc_next
Definition: sqlda-compat.h:43
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:800
PGresult * result
Definition: extern.h:94
#define ECPG_UNSUPPORTED
Definition: ecpgerrno.h:18
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
enum COMPAT_MODE compat
Definition: ecpg.c:25
struct prepared_statement * ecpg_find_prepared_statement(const char *, struct connection *, struct prepared_statement **)
Definition: prepare.c:182
struct sqlda_compat * ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:203
#define ecpg_gettext(x)
Definition: ecpglib.h:18
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:410
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
PGconn * connection
Definition: extern.h:83
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
Definition: extern.h:213
bool ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
Definition: error.c:283
const char * name
Definition: encode.c:521
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:74
#define ECPG_NO_CONN
Definition: ecpgerrno.h:36
ECPGttype
Definition: ecpgtype.h:41

◆ ECPGget_desc()

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

Definition at line 246 of file descriptor.c.

References generate_unaccent_rules::args, variable::arrsize, 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, variable::offset, statement::oldlocale, variable::pointer, PQfmod(), PQfname(), PQfsize(), PQftype(), PQgetisnull(), PQgetlength(), PQnfields(), PQntuples(), RETURN_IF_NO_DATA, setlocale, snprintf(), sqlca, sqlca_t::sqlerrd, variable::type, generate_unaccent_rules::type, variable::value, variable::varcharsize, and VARHDRSZ.

Referenced by main().

247 {
248  va_list args;
249  PGresult *ECPGresult;
250  enum ECPGdtype type;
251  int ntuples,
252  act_tuple;
253  struct variable data_var;
254  struct sqlca_t *sqlca = ECPGget_sqlca();
255 
256  if (sqlca == NULL)
257  {
260  return false;
261  }
262 
263  va_start(args, index);
264  ecpg_init_sqlca(sqlca);
265  ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
266  if (!ECPGresult)
267  {
268  va_end(args);
269  return false;
270  }
271 
272  ntuples = PQntuples(ECPGresult);
273 
274  if (index < 1 || index > PQnfields(ECPGresult))
275  {
277  va_end(args);
278  return false;
279  }
280 
281  ecpg_log("ECPGget_desc: reading items for tuple %d\n", index);
282  --index;
283 
284  type = va_arg(args, enum ECPGdtype);
285 
286  memset(&data_var, 0, sizeof data_var);
287  data_var.type = ECPGt_EORT;
288  data_var.ind_type = ECPGt_NO_INDICATOR;
289 
290  while (type != ECPGd_EODT)
291  {
292  char type_str[20];
293  long varcharsize;
294  long offset;
295  long arrsize;
296  enum ECPGttype vartype;
297  void *var;
298 
299  vartype = va_arg(args, enum ECPGttype);
300  var = va_arg(args, void *);
301  varcharsize = va_arg(args, long);
302  arrsize = va_arg(args, long);
303  offset = va_arg(args, long);
304 
305  switch (type)
306  {
307  case (ECPGd_indicator):
309  data_var.ind_type = vartype;
310  data_var.ind_pointer = var;
311  data_var.ind_varcharsize = varcharsize;
312  data_var.ind_arrsize = arrsize;
313  data_var.ind_offset = offset;
314  if (data_var.ind_arrsize == 0 || data_var.ind_varcharsize == 0)
315  data_var.ind_value = *((void **) (data_var.ind_pointer));
316  else
317  data_var.ind_value = data_var.ind_pointer;
318  break;
319 
320  case ECPGd_data:
322  data_var.type = vartype;
323  data_var.pointer = var;
324  data_var.varcharsize = varcharsize;
325  data_var.arrsize = arrsize;
326  data_var.offset = offset;
327  if (data_var.arrsize == 0 || data_var.varcharsize == 0)
328  data_var.value = *((void **) (data_var.pointer));
329  else
330  data_var.value = data_var.pointer;
331  break;
332 
333  case ECPGd_name:
334  if (!get_char_item(lineno, var, vartype, PQfname(ECPGresult, index), varcharsize))
335  {
336  va_end(args);
337  return false;
338  }
339 
340  ecpg_log("ECPGget_desc: NAME = %s\n", PQfname(ECPGresult, index));
341  break;
342 
343  case ECPGd_nullable:
344  if (!get_int_item(lineno, var, vartype, 1))
345  {
346  va_end(args);
347  return false;
348  }
349 
350  break;
351 
352  case ECPGd_key_member:
353  if (!get_int_item(lineno, var, vartype, 0))
354  {
355  va_end(args);
356  return false;
357  }
358 
359  break;
360 
361  case ECPGd_scale:
362  if (!get_int_item(lineno, var, vartype, (PQfmod(ECPGresult, index) - VARHDRSZ) & 0xffff))
363  {
364  va_end(args);
365  return false;
366  }
367 
368  ecpg_log("ECPGget_desc: SCALE = %d\n", (PQfmod(ECPGresult, index) - VARHDRSZ) & 0xffff);
369  break;
370 
371  case ECPGd_precision:
372  if (!get_int_item(lineno, var, vartype, PQfmod(ECPGresult, index) >> 16))
373  {
374  va_end(args);
375  return false;
376  }
377 
378  ecpg_log("ECPGget_desc: PRECISION = %d\n", PQfmod(ECPGresult, index) >> 16);
379  break;
380 
381  case ECPGd_octet:
382  if (!get_int_item(lineno, var, vartype, PQfsize(ECPGresult, index)))
383  {
384  va_end(args);
385  return false;
386  }
387 
388  ecpg_log("ECPGget_desc: OCTET_LENGTH = %d\n", PQfsize(ECPGresult, index));
389  break;
390 
391  case ECPGd_length:
392  if (!get_int_item(lineno, var, vartype, PQfmod(ECPGresult, index) - VARHDRSZ))
393  {
394  va_end(args);
395  return false;
396  }
397 
398  ecpg_log("ECPGget_desc: LENGTH = %d\n", PQfmod(ECPGresult, index) - VARHDRSZ);
399  break;
400 
401  case ECPGd_type:
402  if (!get_int_item(lineno, var, vartype, ecpg_dynamic_type(PQftype(ECPGresult, index))))
403  {
404  va_end(args);
405  return false;
406  }
407 
408  ecpg_log("ECPGget_desc: TYPE = %d\n", ecpg_dynamic_type(PQftype(ECPGresult, index)));
409  break;
410 
411  case ECPGd_di_code:
412  if (!get_int_item(lineno, var, vartype, ecpg_dynamic_type_DDT(PQftype(ECPGresult, index))))
413  {
414  va_end(args);
415  return false;
416  }
417 
418  ecpg_log("ECPGget_desc: TYPE = %d\n", ecpg_dynamic_type_DDT(PQftype(ECPGresult, index)));
419  break;
420 
421  case ECPGd_cardinality:
422  if (!get_int_item(lineno, var, vartype, PQntuples(ECPGresult)))
423  {
424  va_end(args);
425  return false;
426  }
427 
428  ecpg_log("ECPGget_desc: CARDINALITY = %d\n", PQntuples(ECPGresult));
429  break;
430 
431  case ECPGd_ret_length:
432  case ECPGd_ret_octet:
433 
435 
436  /*
437  * this is like ECPGstore_result
438  */
439  if (arrsize > 0 && ntuples > arrsize)
440  {
441  ecpg_log("ECPGget_desc on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
442  lineno, ntuples, arrsize);
444  va_end(args);
445  return false;
446  }
447  /* allocate storage if needed */
448  if (arrsize == 0 && *(void **) var == NULL)
449  {
450  void *mem = (void *) ecpg_auto_alloc(offset * ntuples, lineno);
451 
452  if (!mem)
453  {
454  va_end(args);
455  return false;
456  }
457  *(void **) var = mem;
458  var = mem;
459  }
460 
461  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
462  {
463  if (!get_int_item(lineno, var, vartype, PQgetlength(ECPGresult, act_tuple, index)))
464  {
465  va_end(args);
466  return false;
467  }
468  var = (char *) var + offset;
469  ecpg_log("ECPGget_desc: RETURNED[%d] = %d\n", act_tuple, PQgetlength(ECPGresult, act_tuple, index));
470  }
471  break;
472 
473  default:
474  snprintf(type_str, sizeof(type_str), "%d", type);
476  va_end(args);
477  return false;
478  }
479 
480  type = va_arg(args, enum ECPGdtype);
481  }
482 
483  if (data_var.type != ECPGt_EORT)
484  {
485  struct statement stmt;
486  char *oldlocale;
487 
488  /* Make sure we do NOT honor the locale for numeric input */
489  /* since the database gives the standard decimal point */
490  oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
491  setlocale(LC_NUMERIC, "C");
492 
493  memset(&stmt, 0, sizeof stmt);
494  stmt.lineno = lineno;
495 
496  /* desperate try to guess something sensible */
497  stmt.connection = ecpg_get_connection(NULL);
498  ecpg_store_result(ECPGresult, index, &stmt, &data_var);
499 
500  setlocale(LC_NUMERIC, oldlocale);
501  ecpg_free(oldlocale);
502  }
503  else if (data_var.ind_type != ECPGt_NO_INDICATOR && data_var.ind_pointer != NULL)
504 
505  /*
506  * ind_type != NO_INDICATOR should always have ind_pointer != NULL but
507  * since this might be changed manually in the .c file let's play it
508  * safe
509  */
510  {
511  /*
512  * this is like ECPGstore_result but since we don't have a data
513  * variable at hand, we can't call it
514  */
515  if (data_var.ind_arrsize > 0 && ntuples > data_var.ind_arrsize)
516  {
517  ecpg_log("ECPGget_desc on line %d: incorrect number of matches (indicator); %d don't fit into array of %ld\n",
518  lineno, ntuples, data_var.ind_arrsize);
520  va_end(args);
521  return false;
522  }
523 
524  /* allocate storage if needed */
525  if (data_var.ind_arrsize == 0 && data_var.ind_value == NULL)
526  {
527  void *mem = (void *) ecpg_auto_alloc(data_var.ind_offset * ntuples, lineno);
528 
529  if (!mem)
530  {
531  va_end(args);
532  return false;
533  }
534  *(void **) data_var.ind_pointer = mem;
535  data_var.ind_value = mem;
536  }
537 
538  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
539  {
540  if (!get_int_item(lineno, data_var.ind_value, data_var.ind_type, -PQgetisnull(ECPGresult, act_tuple, index)))
541  {
542  va_end(args);
543  return false;
544  }
545  data_var.ind_value = (char *) data_var.ind_value + data_var.ind_offset;
546  ecpg_log("ECPGget_desc: INDICATOR[%d] = %d\n", act_tuple, -PQgetisnull(ECPGresult, act_tuple, index));
547  }
548  }
549  sqlca->sqlerrd[2] = ntuples;
550  va_end(args);
551  return true;
552 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3129
static bool get_char_item(int lineno, void *var, enum ECPGttype vartype, char *value, int varcharsize)
Definition: descriptor.c:206
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:309
int ecpg_dynamic_type(Oid)
Definition: typename.c:72
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
Definition: extern.h:221
void ecpg_free(void *)
Definition: memory.c:13
#define setlocale(a, b)
Definition: win32_port.h:419
int PQfsize(const PGresult *res, int field_num)
Definition: fe-exec.c:2973
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2810
static bool get_int_item(int lineno, void *var, enum ECPGttype vartype, int value)
Definition: descriptor.c:116
#define sqlca
Definition: sqlca.h:59
#define VARHDRSZ
Definition: c.h:522
#define ECPG_INVALID_DESCRIPTOR_INDEX
Definition: ecpgerrno.h:43
#define ECPG_UNKNOWN_DESCRIPTOR_ITEM
Definition: ecpgerrno.h:44
char * ecpg_strdup(const char *, int)
Definition: memory.c:47
char * ecpg_auto_alloc(long, int)
Definition: memory.c:108
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:222
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION
Definition: extern.h:209
char * oldlocale
Definition: extern.h:64
#define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX
Definition: extern.h:205
Definition: type.h:89
#define ECPG_TOO_MANY_MATCHES
Definition: ecpgerrno.h:21
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:142
static PGresult * ecpg_result_by_descriptor(int line, const char *name)
Definition: descriptor.c:62
Definition: sqlca.h:19
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:2962
int lineno
Definition: extern.h:54
long sqlerrd[6]
Definition: sqlca.h:30
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
ECPGdtype
Definition: ecpgtype.h:70
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:99
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:2984
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:74
static unsigned int ecpg_dynamic_type_DDT(Oid type)
Definition: descriptor.c:72
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
#define RETURN_IF_NO_DATA
Definition: descriptor.c:238
ECPGttype
Definition: ecpgtype.h:41

◆ ECPGget_desc_header()

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

Definition at line 92 of file descriptor.c.

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(), sqlca, and sqlca_t::sqlerrd.

Referenced by main().

93 {
94  PGresult *ECPGresult;
95  struct sqlca_t *sqlca = ECPGget_sqlca();
96 
97  if (sqlca == NULL)
98  {
101  return false;
102  }
103 
104  ecpg_init_sqlca(sqlca);
105  ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
106  if (!ECPGresult)
107  return false;
108 
109  *count = PQnfields(ECPGresult);
110  sqlca->sqlerrd[2] = 1;
111  ecpg_log("ECPGget_desc_header: found %d attributes\n", *count);
112  return true;
113 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
#define sqlca
Definition: sqlca.h:59
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:222
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:142
static PGresult * ecpg_result_by_descriptor(int line, const char *name)
Definition: descriptor.c:62
Definition: sqlca.h:19
long sqlerrd[6]
Definition: sqlca.h:30
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:99

◆ ECPGset_desc()

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

Definition at line 568 of file descriptor.c.

References generate_unaccent_rules::args, variable::arrsize, descriptor::count, descriptor_item::data, 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_int_item(), snprintf(), descriptor_item::type, variable::type, variable::value, and variable::varcharsize.

Referenced by main().

569 {
570  va_list args;
571  struct descriptor *desc;
572  struct descriptor_item *desc_item;
573  struct variable *var;
574 
575  desc = ecpg_find_desc(lineno, desc_name);
576  if (desc == NULL)
577  return false;
578 
579  for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
580  {
581  if (desc_item->num == index)
582  break;
583  }
584 
585  if (desc_item == NULL)
586  {
587  desc_item = (struct descriptor_item *) ecpg_alloc(sizeof(*desc_item), lineno);
588  if (!desc_item)
589  return false;
590  desc_item->num = index;
591  if (desc->count < index)
592  desc->count = index;
593  desc_item->next = desc->items;
594  desc->items = desc_item;
595  }
596 
597  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
598  return false;
599 
600  va_start(args, index);
601 
602  for (;;)
603  {
604  enum ECPGdtype itemtype;
605  char *tobeinserted = NULL;
606 
607  itemtype = va_arg(args, enum ECPGdtype);
608 
609  if (itemtype == ECPGd_EODT)
610  break;
611 
612  var->type = va_arg(args, enum ECPGttype);
613  var->pointer = va_arg(args, char *);
614 
615  var->varcharsize = va_arg(args, long);
616  var->arrsize = va_arg(args, long);
617  var->offset = va_arg(args, long);
618 
619  if (var->arrsize == 0 || var->varcharsize == 0)
620  var->value = *((char **) (var->pointer));
621  else
622  var->value = var->pointer;
623 
624  /*
625  * negative values are used to indicate an array without given bounds
626  */
627  /* reset to zero for us */
628  if (var->arrsize < 0)
629  var->arrsize = 0;
630  if (var->varcharsize < 0)
631  var->varcharsize = 0;
632 
633  var->next = NULL;
634 
635  switch (itemtype)
636  {
637  case ECPGd_data:
638  {
639  if (!ecpg_store_input(lineno, true, var, &tobeinserted, false))
640  {
641  ecpg_free(var);
642  va_end(args);
643  return false;
644  }
645 
646  ecpg_free(desc_item->data); /* free() takes care of a
647  * potential NULL value */
648  desc_item->data = (char *) tobeinserted;
649  tobeinserted = NULL;
650  break;
651  }
652 
653  case ECPGd_indicator:
654  set_int_item(lineno, &desc_item->indicator, var->pointer, var->type);
655  break;
656 
657  case ECPGd_length:
658  set_int_item(lineno, &desc_item->length, var->pointer, var->type);
659  break;
660 
661  case ECPGd_precision:
662  set_int_item(lineno, &desc_item->precision, var->pointer, var->type);
663  break;
664 
665  case ECPGd_scale:
666  set_int_item(lineno, &desc_item->scale, var->pointer, var->type);
667  break;
668 
669  case ECPGd_type:
670  set_int_item(lineno, &desc_item->type, var->pointer, var->type);
671  break;
672 
673  default:
674  {
675  char type_str[20];
676 
677  snprintf(type_str, sizeof(type_str), "%d", itemtype);
679  ecpg_free(var);
680  va_end(args);
681  return false;
682  }
683  }
684  }
685  ecpg_free(var);
686  va_end(args);
687 
688  return true;
689 }
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
Definition: extern.h:221
static bool set_int_item(int lineno, int *target, const void *var, enum ECPGttype vartype)
Definition: descriptor.c:161
void ecpg_free(void *)
Definition: memory.c:13
char * data
Definition: extern.h:103
void * pointer
Definition: extern.h:116
#define ECPG_UNKNOWN_DESCRIPTOR_ITEM
Definition: ecpgerrno.h:44
enum ECPGttype type
Definition: extern.h:114
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
struct variable * next
Definition: extern.h:126
Definition: type.h:89
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:800
void * value
Definition: extern.h:115
char * ecpg_alloc(long, int)
Definition: memory.c:19
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
int count
Definition: extern.h:96
long offset
Definition: extern.h:119
ECPGdtype
Definition: ecpgtype.h:70
struct descriptor_item * items
Definition: extern.h:97
struct descriptor_item * next
Definition: extern.h:109
long arrsize
Definition: extern.h:118
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:494
long varcharsize
Definition: extern.h:117
ECPGttype
Definition: ecpgtype.h:41

◆ ECPGset_desc_header()

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

Definition at line 557 of file descriptor.c.

References descriptor::count, and ecpg_find_desc().

Referenced by main().

558 {
559  struct descriptor *desc = ecpg_find_desc(lineno, desc_name);
560 
561  if (desc == NULL)
562  return false;
563  desc->count = count;
564  return true;
565 }
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:800
int count
Definition: extern.h:96

◆ get_char_item()

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

Definition at line 206 of file descriptor.c.

References ECPGgeneric_varchar::arr, ecpg_raise(), ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION, ECPG_VAR_NOT_CHAR, ECPGt_char, ECPGt_string, ECPGt_unsigned_char, ECPGt_varchar, and ECPGgeneric_varchar::len.

Referenced by ECPGget_desc().

207 {
208  switch (vartype)
209  {
210  case ECPGt_char:
211  case ECPGt_unsigned_char:
212  case ECPGt_string:
213  strncpy((char *) var, value, varcharsize);
214  break;
215  case ECPGt_varchar:
216  {
217  struct ECPGgeneric_varchar *variable =
218  (struct ECPGgeneric_varchar *) var;
219 
220  if (varcharsize == 0)
221  strncpy(variable->arr, value, strlen(value));
222  else
223  strncpy(variable->arr, value, varcharsize);
224 
225  variable->len = strlen(value);
226  if (varcharsize > 0 && variable->len > varcharsize)
227  variable->len = varcharsize;
228  }
229  break;
230  default:
232  return false;
233  }
234 
235  return true;
236 }
char arr[FLEXIBLE_ARRAY_MEMBER]
Definition: extern.h:37
#define ECPG_VAR_NOT_CHAR
Definition: ecpgerrno.h:46
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION
Definition: extern.h:204
static struct @131 value

◆ get_int_item()

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

Definition at line 116 of file descriptor.c.

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().

117 {
118  switch (vartype)
119  {
120  case ECPGt_short:
121  *(short *) var = (short) value;
122  break;
123  case ECPGt_int:
124  *(int *) var = (int) value;
125  break;
126  case ECPGt_long:
127  *(long *) var = (long) value;
128  break;
130  *(unsigned short *) var = (unsigned short) value;
131  break;
132  case ECPGt_unsigned_int:
133  *(unsigned int *) var = (unsigned int) value;
134  break;
135  case ECPGt_unsigned_long:
136  *(unsigned long *) var = (unsigned long) value;
137  break;
138 #ifdef HAVE_LONG_LONG_INT
139  case ECPGt_long_long:
140  *(long long int *) var = (long long int) value;
141  break;
143  *(unsigned long long int *) var = (unsigned long long int) value;
144  break;
145 #endif /* HAVE_LONG_LONG_INT */
146  case ECPGt_float:
147  *(float *) var = (float) value;
148  break;
149  case ECPGt_double:
150  *(double *) var = (double) value;
151  break;
152  default:
154  return false;
155  }
156 
157  return true;
158 }
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_VAR_NOT_NUMERIC
Definition: ecpgerrno.h:45
#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION
Definition: extern.h:204
static struct @131 value

◆ set_int_item()

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

Definition at line 161 of file descriptor.c.

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().

162 {
163  switch (vartype)
164  {
165  case ECPGt_short:
166  *target = *(const short *) var;
167  break;
168  case ECPGt_int:
169  *target = *(const int *) var;
170  break;
171  case ECPGt_long:
172  *target = *(const long *) var;
173  break;
175  *target = *(const unsigned short *) var;
176  break;
177  case ECPGt_unsigned_int:
178  *target = *(const unsigned int *) var;
179  break;
180  case ECPGt_unsigned_long:
181  *target = *(const unsigned long *) var;
182  break;
183 #ifdef HAVE_LONG_LONG_INT
184  case ECPGt_long_long:
185  *target = *(const long long int *) var;
186  break;
188  *target = *(const unsigned long long int *) var;
189  break;
190 #endif /* HAVE_LONG_LONG_INT */
191  case ECPGt_float:
192  *target = *(const float *) var;
193  break;
194  case ECPGt_double:
195  *target = *(const double *) var;
196  break;
197  default:
199  return false;
200  }
201 
202  return true;
203 }
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_VAR_NOT_NUMERIC
Definition: ecpgerrno.h:45
#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION
Definition: extern.h:204

Variable Documentation

◆ all_descriptors

struct descriptor* all_descriptors = NULL
static

Definition at line 54 of file descriptor.c.