PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
descriptor.c File Reference
#include "postgres_fe.h"
#include "pg_type.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

#define get_descriptors ( )    (all_descriptors)

Definition at line 55 of file descriptor.c.

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

#define POSTGRES_ECPG_INTERNAL

Definition at line 6 of file descriptor.c.

#define RETURN_IF_NO_DATA
Value:
if (ntuples < 1) \
{ \
va_end(args); \
return (false); \
}
#define ECPG_SQLSTATE_NO_DATA
Definition: extern.h:200
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
#define NULL
Definition: c.h:229

Definition at line 237 of file descriptor.c.

Referenced by ECPGget_desc().

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

Definition at line 56 of file descriptor.c.

Referenced by ECPGallocate_desc(), and ECPGdeallocate_desc().

Function Documentation

static void descriptor_free ( struct descriptor desc)
static

Definition at line 692 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().

693 {
694  struct descriptor_item *desc_item;
695 
696  for (desc_item = desc->items; desc_item;)
697  {
698  struct descriptor_item *di;
699 
700  ecpg_free(desc_item->data);
701  di = desc_item;
702  desc_item = desc_item->next;
703  ecpg_free(di);
704  }
705 
706  ecpg_free(desc->name);
707  PQclear(desc->result);
708  ecpg_free(desc);
709 }
void ecpg_free(void *)
Definition: memory.c:13
char * data
Definition: extern.h:102
PGresult * result
Definition: extern.h:93
void PQclear(PGresult *res)
Definition: fe-exec.c:650
struct descriptor_item * items
Definition: extern.h:96
struct descriptor_item * next
Definition: extern.h:108
char * name
Definition: extern.h:92
static unsigned int ecpg_dynamic_type_DDT ( Oid  type)
static

Definition at line 71 of file descriptor.c.

References DATEOID, SQL3_DDT_DATE, SQL3_DDT_ILLEGAL, SQL3_DDT_TIME, SQL3_DDT_TIME_WITH_TIME_ZONE, SQL3_DDT_TIMESTAMP, SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, and TIMETZOID.

Referenced by ECPGget_desc().

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 }
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define TIMEOID
Definition: pg_type.h:514
#define DATEOID
Definition: pg_type.h:511
#define TIMESTAMPOID
Definition: pg_type.h:519
#define TIMETZOID
Definition: pg_type.h:536
struct descriptor* ecpg_find_desc ( int  line,
const char *  name 
)

Definition at line 799 of file descriptor.c.

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

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

800 {
801  struct descriptor *desc;
802 
803  for (desc = get_descriptors(); desc; desc = desc->next)
804  {
805  if (strcmp(name, desc->name) == 0)
806  return desc;
807  }
808 
810  return NULL; /* not found */
811 }
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME
Definition: extern.h:213
struct descriptor * next
Definition: extern.h:94
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
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
char * name
Definition: extern.h:92
#define get_descriptors()
Definition: descriptor.c:55
static PGresult* ecpg_result_by_descriptor ( int  line,
const char *  name 
)
static

Definition at line 61 of file descriptor.c.

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

Referenced by ECPGget_desc(), and ECPGget_desc_header().

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:799
PGresult * result
Definition: extern.h:93
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
bool ECPGallocate_desc ( int  line,
const char *  name 
)

Definition at line 759 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, NULL, PQmakeEmptyPGresult(), set_descriptors, and sqlca.

Referenced by fn(), and main().

760 {
761  struct descriptor *new;
762  struct sqlca_t *sqlca = ECPGget_sqlca();
763 
764  if (sqlca == NULL)
765  {
768  return false;
769  }
770 
771  ecpg_init_sqlca(sqlca);
772  new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
773  if (!new)
774  return false;
775  new->next = get_descriptors();
776  new->name = ecpg_alloc(strlen(name) + 1, line);
777  if (!new->name)
778  {
779  ecpg_free(new);
780  return false;
781  }
782  new->count = -1;
783  new->items = NULL;
784  new->result = PQmakeEmptyPGresult(NULL, 0);
785  if (!new->result)
786  {
787  ecpg_free(new->name);
788  ecpg_free(new);
790  return false;
791  }
792  strcpy(new->name, name);
793  set_descriptors(new);
794  return true;
795 }
void ecpg_free(void *)
Definition: memory.c:13
#define sqlca
Definition: sqlca.h:59
#define set_descriptors(value)
Definition: descriptor.c:56
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:221
#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:140
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
#define NULL
Definition: c.h:229
const char * name
Definition: encode.c:521
#define get_descriptors()
Definition: descriptor.c:55
bool ECPGdeallocate_desc ( int  line,
const char *  name 
)

Definition at line 712 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, descriptor::next, NULL, set_descriptors, and sqlca.

Referenced by fn(), and main().

713 {
714  struct descriptor *desc;
715  struct descriptor *prev;
716  struct sqlca_t *sqlca = ECPGget_sqlca();
717 
718  if (sqlca == NULL)
719  {
722  return false;
723  }
724 
725  ecpg_init_sqlca(sqlca);
726  for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
727  {
728  if (strcmp(name, desc->name) == 0)
729  {
730  if (prev)
731  prev->next = desc->next;
732  else
733  set_descriptors(desc->next);
734  descriptor_free(desc);
735  return true;
736  }
737  }
739  return false;
740 }
#define sqlca
Definition: sqlca.h:59
#define set_descriptors(value)
Definition: descriptor.c:56
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:221
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME
Definition: extern.h:213
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct descriptor * next
Definition: extern.h:94
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
#define NULL
Definition: c.h:229
static void descriptor_free(struct descriptor *desc)
Definition: descriptor.c:692
const char * name
Definition: encode.c:521
#define get_descriptors()
Definition: descriptor.c:55
bool ECPGdescribe ( int  line,
int  compat,
bool  input,
const char *  connection_name,
const char *  stmt_name,
  ... 
)

Definition at line 814 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, NULL, PQclear(), PQdescribePrepared(), and descriptor::result.

Referenced by main().

815 {
816  bool ret = false;
817  struct connection *con;
818  struct prepared_statement *prep;
819  PGresult *res;
820  va_list args;
821 
822  /* DESCRIBE INPUT is not yet supported */
823  if (input)
824  {
826  return ret;
827  }
828 
829  con = ecpg_get_connection(connection_name);
830  if (!con)
831  {
833  connection_name ? connection_name : ecpg_gettext("NULL"));
834  return ret;
835  }
836  prep = ecpg_find_prepared_statement(stmt_name, con, NULL);
837  if (!prep)
838  {
840  return ret;
841  }
842 
843  va_start(args, stmt_name);
844 
845  for (;;)
846  {
847  enum ECPGttype type;
848  void *ptr;
849 
850  /* variable type */
851  type = va_arg(args, enum ECPGttype);
852 
853  if (type == ECPGt_EORT)
854  break;
855 
856  /* rest of variable parameters */
857  ptr = va_arg(args, void *);
858  (void) va_arg(args, long); /* skip args */
859  (void) va_arg(args, long);
860  (void) va_arg(args, long);
861 
862  /* variable indicator */
863  (void) va_arg(args, enum ECPGttype);
864  (void) va_arg(args, void *); /* skip args */
865  (void) va_arg(args, long);
866  (void) va_arg(args, long);
867  (void) va_arg(args, long);
868 
869  switch (type)
870  {
871  case ECPGt_descriptor:
872  {
873  char *name = ptr;
874  struct descriptor *desc = ecpg_find_desc(line, name);
875 
876  if (desc == NULL)
877  break;
878 
879  res = PQdescribePrepared(con->connection, stmt_name);
880  if (!ecpg_check_PQresult(res, line, con->connection, compat))
881  break;
882 
883  if (desc->result != NULL)
884  PQclear(desc->result);
885 
886  desc->result = res;
887  ret = true;
888  break;
889  }
890  case ECPGt_sqlda:
891  {
892  if (INFORMIX_MODE(compat))
893  {
894  struct sqlda_compat **_sqlda = ptr;
895  struct sqlda_compat *sqlda;
896 
897  res = PQdescribePrepared(con->connection, stmt_name);
898  if (!ecpg_check_PQresult(res, line, con->connection, compat))
899  break;
900 
901  sqlda = ecpg_build_compat_sqlda(line, res, -1, compat);
902  if (sqlda)
903  {
904  struct sqlda_compat *sqlda_old = *_sqlda;
905  struct sqlda_compat *sqlda_old1;
906 
907  while (sqlda_old)
908  {
909  sqlda_old1 = sqlda_old->desc_next;
910  free(sqlda_old);
911  sqlda_old = sqlda_old1;
912  }
913 
914  *_sqlda = sqlda;
915  ret = true;
916  }
917 
918  PQclear(res);
919  }
920  else
921  {
922  struct sqlda_struct **_sqlda = ptr;
923  struct sqlda_struct *sqlda;
924 
925  res = PQdescribePrepared(con->connection, stmt_name);
926  if (!ecpg_check_PQresult(res, line, con->connection, compat))
927  break;
928 
929  sqlda = ecpg_build_native_sqlda(line, res, -1, compat);
930  if (sqlda)
931  {
932  struct sqlda_struct *sqlda_old = *_sqlda;
933  struct sqlda_struct *sqlda_old1;
934 
935  while (sqlda_old)
936  {
937  sqlda_old1 = sqlda_old->desc_next;
938  free(sqlda_old);
939  sqlda_old = sqlda_old1;
940  }
941 
942  *_sqlda = sqlda;
943  ret = true;
944  }
945 
946  PQclear(res);
947  }
948  break;
949  }
950  default:
951  /* nothing else may come */
952  ;
953  }
954  }
955 
956  va_end(args);
957 
958  return ret;
959 }
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2063
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
Definition: extern.h:220
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:103
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
Definition: extern.h:206
struct sqlda_compat * desc_next
Definition: sqlda-compat.h:43
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:799
PGresult * result
Definition: extern.h:93
#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:202
#define ecpg_gettext(x)
Definition: ecpglib.h:18
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:409
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
Definition: extern.h:212
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
bool ECPGget_desc ( int  lineno,
const char *  desc_name,
int  index,
  ... 
)

Definition at line 245 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, NULL, variable::offset, statement::oldlocale, variable::pointer, PQfmod(), PQfname(), PQfsize(), PQftype(), PQgetisnull(), PQgetlength(), PQnfields(), PQntuples(), RETURN_IF_NO_DATA, snprintf(), sqlca, sqlca_t::sqlerrd, variable::type, variable::value, variable::varcharsize, and VARHDRSZ.

Referenced by main().

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

Definition at line 91 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(), NULL, PQnfields(), sqlca, and sqlca_t::sqlerrd.

Referenced by main().

92 {
93  PGresult *ECPGresult;
94  struct sqlca_t *sqlca = ECPGget_sqlca();
95 
96  if (sqlca == NULL)
97  {
100  return false;
101  }
102 
103  ecpg_init_sqlca(sqlca);
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 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
#define sqlca
Definition: sqlca.h:59
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:221
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:61
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
#define NULL
Definition: c.h:229
bool ECPGset_desc ( int  lineno,
const char *  desc_name,
int  index,
  ... 
)

Definition at line 567 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, NULL, 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().

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

Definition at line 556 of file descriptor.c.

References descriptor::count, ecpg_find_desc(), and NULL.

Referenced by main().

557 {
558  struct descriptor *desc = ecpg_find_desc(lineno, desc_name);
559 
560  if (desc == NULL)
561  return false;
562  desc->count = count;
563  return true;
564 }
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:799
int count
Definition: extern.h:95
#define NULL
Definition: c.h:229
static bool get_char_item ( int  lineno,
void *  var,
enum ECPGttype  vartype,
char *  value,
int  varcharsize 
)
static

Definition at line 205 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, ECPGgeneric_varchar::len, and NULL.

Referenced by ECPGget_desc().

206 {
207  switch (vartype)
208  {
209  case ECPGt_char:
210  case ECPGt_unsigned_char:
211  case ECPGt_string:
212  strncpy((char *) var, value, varcharsize);
213  break;
214  case ECPGt_varchar:
215  {
216  struct ECPGgeneric_varchar *variable =
217  (struct ECPGgeneric_varchar *) var;
218 
219  if (varcharsize == 0)
220  strncpy(variable->arr, value, strlen(value));
221  else
222  strncpy(variable->arr, value, varcharsize);
223 
224  variable->len = strlen(value);
225  if (varcharsize > 0 && variable->len > varcharsize)
226  variable->len = varcharsize;
227  }
228  break;
229  default:
231  return (false);
232  }
233 
234  return (true);
235 }
char arr[FLEXIBLE_ARRAY_MEMBER]
Definition: extern.h:36
#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:203
#define NULL
Definition: c.h:229
static struct @121 value
static bool get_int_item ( int  lineno,
void *  var,
enum ECPGttype  vartype,
int  value 
)
static

Definition at line 115 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, NULL, and value.

Referenced by ECPGget_desc().

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 #ifdef HAVE_LONG_LONG_INT
138  case ECPGt_long_long:
139  *(long long int *) var = (long long int) value;
140  break;
142  *(unsigned long long int *) var = (unsigned long long int) value;
143  break;
144 #endif /* HAVE_LONG_LONG_INT */
145  case ECPGt_float:
146  *(float *) var = (float) value;
147  break;
148  case ECPGt_double:
149  *(double *) var = (double) value;
150  break;
151  default:
153  return (false);
154  }
155 
156  return (true);
157 }
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:203
#define NULL
Definition: c.h:229
static struct @121 value
static bool set_int_item ( int  lineno,
int *  target,
const void *  var,
enum ECPGttype  vartype 
)
static

Definition at line 160 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 NULL.

Referenced by ECPGset_desc().

161 {
162  switch (vartype)
163  {
164  case ECPGt_short:
165  *target = *(const short *) var;
166  break;
167  case ECPGt_int:
168  *target = *(const int *) var;
169  break;
170  case ECPGt_long:
171  *target = *(const long *) var;
172  break;
174  *target = *(const unsigned short *) var;
175  break;
176  case ECPGt_unsigned_int:
177  *target = *(const unsigned int *) var;
178  break;
179  case ECPGt_unsigned_long:
180  *target = *(const unsigned long *) var;
181  break;
182 #ifdef HAVE_LONG_LONG_INT
183  case ECPGt_long_long:
184  *target = *(const long long int *) var;
185  break;
187  *target = *(const unsigned long long int *) var;
188  break;
189 #endif /* HAVE_LONG_LONG_INT */
190  case ECPGt_float:
191  *target = *(const float *) var;
192  break;
193  case ECPGt_double:
194  *target = *(const double *) var;
195  break;
196  default:
198  return (false);
199  }
200 
201  return true;
202 }
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:203
#define NULL
Definition: c.h:229

Variable Documentation

struct descriptor* all_descriptors = NULL
static

Definition at line 53 of file descriptor.c.