PostgreSQL Source Code  git master
ecpglib_extern.h File Reference
#include "ecpg_config.h"
#include "ecpgtype.h"
#include "libpq-fe.h"
#include "sqlca.h"
#include "sqlda-compat.h"
#include "sqlda-native.h"
#include <limits.h>
Include dependency graph for ecpglib_extern.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ECPGgeneric_varchar
 
struct  ECPGgeneric_bytea
 
struct  ECPGtype_information_cache
 
struct  statement
 
struct  prepared_statement
 
struct  connection
 
struct  descriptor
 
struct  descriptor_item
 
struct  variable
 
struct  var_list
 

Macros

#define INFORMIX_MODE(X)   ((X) == ECPG_COMPAT_INFORMIX || (X) == ECPG_COMPAT_INFORMIX_SE)
 
#define ORACLE_MODE(X)   ((X) == ECPG_COMPAT_ORACLE)
 
#define ECPG_IS_ARRAY(X)   ((X) == ECPG_ARRAY_ARRAY || (X) == ECPG_ARRAY_VECTOR)
 
#define ecpg_gettext(x)   (x)
 
#define ECPG_SQLSTATE_NO_DATA   "02000"
 
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS   "07001"
 
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS   "07002"
 
#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION   "07006"
 
#define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX   "07009"
 
#define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION   "08001"
 
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST   "08003"
 
#define ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN   "08007"
 
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION   "21000"
 
#define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER   "22002"
 
#define ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION   "25001"
 
#define ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION   "25P01"
 
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME   "26000"
 
#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME   "33000"
 
#define ECPG_SQLSTATE_INVALID_CURSOR_NAME   "34000"
 
#define ECPG_SQLSTATE_SYNTAX_ERROR   "42601"
 
#define ECPG_SQLSTATE_DATATYPE_MISMATCH   "42804"
 
#define ECPG_SQLSTATE_DUPLICATE_CURSOR   "42P03"
 
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR   "YE000"
 
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY   "YE001"
 

Enumerations

enum  COMPAT_MODE {
  ECPG_COMPAT_PGSQL = 0 , ECPG_COMPAT_INFORMIX , ECPG_COMPAT_INFORMIX_SE , ECPG_COMPAT_ORACLE ,
  ECPG_COMPAT_PGSQL = 0 , ECPG_COMPAT_INFORMIX , ECPG_COMPAT_INFORMIX_SE , ECPG_COMPAT_ORACLE
}
 
enum  ARRAY_TYPE {
  ECPG_ARRAY_ERROR , ECPG_ARRAY_NOT_SET , ECPG_ARRAY_ARRAY , ECPG_ARRAY_VECTOR ,
  ECPG_ARRAY_NONE
}
 

Functions

bool ecpg_add_mem (void *ptr, int lineno)
 
bool ecpg_get_data (const PGresult *, int, int, int, enum ECPGttype type, enum ECPGttype, char *, char *, long, long, long, enum ARRAY_TYPE, enum COMPAT_MODE, bool)
 
struct connectionecpg_get_connection (const char *)
 
char * ecpg_alloc (long, int)
 
char * ecpg_auto_alloc (long, int)
 
char * ecpg_realloc (void *, long, int)
 
void ecpg_free (void *)
 
bool ecpg_init (const struct connection *, const char *, const int)
 
char * ecpg_strdup (const char *, int)
 
const char * ecpg_type_name (enum ECPGttype)
 
int ecpg_dynamic_type (Oid)
 
int sqlda_dynamic_type (Oid, enum COMPAT_MODE)
 
void ecpg_clear_auto_mem (void)
 
struct descriptorecpg_find_desc (int line, const char *name)
 
struct prepared_statementecpg_find_prepared_statement (const char *, struct connection *, struct prepared_statement **)
 
bool ecpg_store_result (const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
 
bool ecpg_store_input (const int, const bool, const struct variable *, char **, bool)
 
void ecpg_free_params (struct statement *stmt, bool print)
 
bool ecpg_do_prologue (int, const int, const int, const char *, const bool, enum ECPG_statement_type, const char *, va_list, struct statement **)
 
bool ecpg_build_params (struct statement *)
 
bool ecpg_autostart_transaction (struct statement *stmt)
 
bool ecpg_execute (struct statement *stmt)
 
bool ecpg_process_output (struct statement *, bool)
 
void ecpg_do_epilogue (struct statement *)
 
bool ecpg_do (const int, const int, const int, const char *, const bool, const int, const char *, va_list)
 
bool ecpg_check_PQresult (PGresult *, int, PGconn *, enum COMPAT_MODE)
 
void ecpg_raise (int line, int code, const char *sqlstate, const char *str)
 
void ecpg_raise_backend (int line, PGresult *result, PGconn *conn, int compat)
 
char * ecpg_prepared (const char *, struct connection *)
 
bool ecpg_deallocate_all_conn (int lineno, enum COMPAT_MODE c, struct connection *conn)
 
void ecpg_log (const char *format,...) pg_attribute_printf(1
 
void bool ecpg_auto_prepare (int, const char *, const int, char **, const char *)
 
bool ecpg_register_prepared_stmt (struct statement *)
 
void ecpg_init_sqlca (struct sqlca_t *sqlca)
 
struct sqlda_compatecpg_build_compat_sqlda (int, PGresult *, int, enum COMPAT_MODE)
 
void ecpg_set_compat_sqlda (int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE)
 
struct sqlda_structecpg_build_native_sqlda (int, PGresult *, int, enum COMPAT_MODE)
 
void ecpg_set_native_sqlda (int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE)
 
unsigned ecpg_hex_dec_len (unsigned srclen)
 
unsigned ecpg_hex_enc_len (unsigned srclen)
 
unsigned ecpg_hex_encode (const char *src, unsigned len, char *dst)
 

Variables

bool ecpg_internal_regression_mode
 
struct var_listivlist
 

Macro Definition Documentation

◆ ecpg_gettext

#define ecpg_gettext (   x)    (x)

Definition at line 228 of file ecpglib_extern.h.

◆ ECPG_IS_ARRAY

#define ECPG_IS_ARRAY (   X)    ((X) == ECPG_ARRAY_ARRAY || (X) == ECPG_ARRAY_VECTOR)

Definition at line 35 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION

#define ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION   "25001"

Definition at line 244 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_CARDINALITY_VIOLATION

#define ECPG_SQLSTATE_CARDINALITY_VIOLATION   "21000"

Definition at line 242 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST

#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST   "08003"

Definition at line 240 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_DATATYPE_MISMATCH

#define ECPG_SQLSTATE_DATATYPE_MISMATCH   "42804"

Definition at line 250 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_DUPLICATE_CURSOR

#define ECPG_SQLSTATE_DUPLICATE_CURSOR   "42P03"

Definition at line 251 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_ECPG_INTERNAL_ERROR

#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR   "YE000"

Definition at line 254 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY

#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY   "YE001"

Definition at line 255 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_CURSOR_NAME

#define ECPG_SQLSTATE_INVALID_CURSOR_NAME   "34000"

Definition at line 248 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX

#define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX   "07009"

Definition at line 238 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME

#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME   "33000"

Definition at line 247 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME

#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME   "26000"

Definition at line 246 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION

#define ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION   "25P01"

Definition at line 245 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NO_DATA

#define ECPG_SQLSTATE_NO_DATA   "02000"

Definition at line 234 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER

#define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER   "22002"

Definition at line 243 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION

#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION   "07006"

Definition at line 237 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION

#define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION   "08001"

Definition at line 239 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_SYNTAX_ERROR

#define ECPG_SQLSTATE_SYNTAX_ERROR   "42601"

Definition at line 249 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN

#define ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN   "08007"

Definition at line 241 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS

#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS   "07001"

Definition at line 235 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS

#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS   "07002"

Definition at line 236 of file ecpglib_extern.h.

◆ INFORMIX_MODE

#define INFORMIX_MODE (   X)    ((X) == ECPG_COMPAT_INFORMIX || (X) == ECPG_COMPAT_INFORMIX_SE)

Definition at line 27 of file ecpglib_extern.h.

◆ ORACLE_MODE

#define ORACLE_MODE (   X)    ((X) == ECPG_COMPAT_ORACLE)

Definition at line 28 of file ecpglib_extern.h.

Enumeration Type Documentation

◆ ARRAY_TYPE

enum ARRAY_TYPE
Enumerator
ECPG_ARRAY_ERROR 
ECPG_ARRAY_NOT_SET 
ECPG_ARRAY_ARRAY 
ECPG_ARRAY_VECTOR 
ECPG_ARRAY_NONE 

Definition at line 30 of file ecpglib_extern.h.

31 {
33 };
@ ECPG_ARRAY_NOT_SET
@ ECPG_ARRAY_ARRAY
@ ECPG_ARRAY_VECTOR
@ ECPG_ARRAY_NONE
@ ECPG_ARRAY_ERROR

◆ COMPAT_MODE

Enumerator
ECPG_COMPAT_PGSQL 
ECPG_COMPAT_INFORMIX 
ECPG_COMPAT_INFORMIX_SE 
ECPG_COMPAT_ORACLE 
ECPG_COMPAT_PGSQL 
ECPG_COMPAT_INFORMIX 
ECPG_COMPAT_INFORMIX_SE 
ECPG_COMPAT_ORACLE 

Definition at line 20 of file ecpglib_extern.h.

21 {
23 };
@ ECPG_COMPAT_PGSQL
@ ECPG_COMPAT_ORACLE
@ ECPG_COMPAT_INFORMIX
@ ECPG_COMPAT_INFORMIX_SE

Function Documentation

◆ ecpg_add_mem()

bool ecpg_add_mem ( void *  ptr,
int  lineno 
)

Definition at line 124 of file memory.c.

125 {
126  struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
127 
128  if (!am)
129  return false;
130 
131  am->pointer = ptr;
132  am->next = get_auto_allocs();
133  set_auto_allocs(am);
134  return true;
135 }
#define set_auto_allocs(am)
Definition: memory.c:104
#define get_auto_allocs()
Definition: memory.c:103
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
void * pointer
Definition: memory.c:67
struct auto_mem * next
Definition: memory.c:68

References ecpg_alloc(), get_auto_allocs, auto_mem::next, auto_mem::pointer, and set_auto_allocs.

Referenced by ecpg_auto_alloc().

◆ ecpg_alloc()

char* ecpg_alloc ( long  size,
int  lineno 
)

Definition at line 19 of file memory.c.

20 {
21  char *new = (char *) calloc(1L, size);
22 
23  if (!new)
24  {
26  return NULL;
27  }
28 
29  return new;
30 }
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define calloc(a, b)
Definition: header.h:55

References calloc, ECPG_OUT_OF_MEMORY, ecpg_raise(), and ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY.

Referenced by AddStmtToCache(), convert_bytea_to_string(), deallocate_one(), ecpg_add_mem(), ecpg_auto_alloc(), ecpg_build_compat_sqlda(), ecpg_build_native_sqlda(), ecpg_build_params(), ecpg_do_prologue(), ecpg_get_data(), ecpg_is_type_an_array(), ecpg_register_prepared_stmt(), ecpg_store_input(), ECPGallocate_desc(), ECPGconnect(), ECPGset_desc(), insert_tobeinserted(), prepare_common(), print_param_value(), quote_postgres(), replace_variables(), and store_input_from_desc().

◆ ecpg_auto_alloc()

char* ecpg_auto_alloc ( long  size,
int  lineno 
)

Definition at line 108 of file memory.c.

109 {
110  void *ptr = (void *) ecpg_alloc(size, lineno);
111 
112  if (!ptr)
113  return NULL;
114 
115  if (!ecpg_add_mem(ptr, lineno))
116  {
117  ecpg_free(ptr);
118  return NULL;
119  }
120  return ptr;
121 }
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:124
void ecpg_free(void *ptr)
Definition: memory.c:13

References ecpg_add_mem(), ecpg_alloc(), and ecpg_free().

Referenced by ecpg_store_result(), and ECPGget_desc().

◆ ecpg_auto_prepare()

void bool ecpg_auto_prepare ( int  lineno,
const char *  connection_name,
const int  compat,
char **  name,
const char *  query 
)

Definition at line 553 of file prepare.c.

554 {
555  int entNo;
556 
557  /* search the statement cache for this statement */
558  entNo = SearchStmtCache(query);
559 
560  /* if not found - add the statement to the cache */
561  if (entNo)
562  {
563  char *stmtID;
564  struct connection *con;
565  struct prepared_statement *prep;
566 
567  ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
568 
569  stmtID = stmtCacheEntries[entNo].stmtID;
570 
571  con = ecpg_get_connection(connection_name);
572  prep = ecpg_find_prepared_statement(stmtID, con, NULL);
573  /* This prepared name doesn't exist on this connection. */
574  if (!prep && !prepare_common(lineno, con, stmtID, query))
575  return false;
576 
577  *name = ecpg_strdup(stmtID, lineno);
578  }
579  else
580  {
581  char stmtID[STMTID_SIZE];
582 
583  ecpg_log("ecpg_auto_prepare on line %d: statement not in cache; inserting\n", lineno);
584 
585  /* generate a statement ID */
586  sprintf(stmtID, "ecpg%d", nextStmtID++);
587 
588  if (!ECPGprepare(lineno, connection_name, 0, stmtID, query))
589  return false;
590 
591  entNo = AddStmtToCache(lineno, stmtID, connection_name, compat, query);
592  if (entNo < 0)
593  return false;
594 
595  *name = ecpg_strdup(stmtID, lineno);
596  }
597 
598  /* increase usage counter */
599  stmtCacheEntries[entNo].execs++;
600 
601  return true;
602 }
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:79
enum COMPAT_MODE compat
Definition: ecpg.c:25
void ecpg_log(const char *format,...) pg_attribute_printf(1
char * ecpg_strdup(const char *, int)
Definition: memory.c:47
const char * name
Definition: encode.c:561
static bool prepare_common(int lineno, struct connection *con, const char *name, const char *variable)
Definition: prepare.c:159
static int nextStmtID
Definition: prepare.c:37
static int AddStmtToCache(int lineno, const char *stmtID, const char *connection, int compat, const char *ecpgQuery)
Definition: prepare.c:491
struct prepared_statement * ecpg_find_prepared_statement(const char *name, struct connection *con, struct prepared_statement **prev_)
Definition: prepare.c:239
static int SearchStmtCache(const char *ecpgQuery)
Definition: prepare.c:415
bool ECPGprepare(int lineno, const char *connection_name, const bool questionmarks, const char *name, const char *variable)
Definition: prepare.c:217
#define STMTID_SIZE
Definition: prepare.c:14
static stmtCacheEntry * stmtCacheEntries
Definition: prepare.c:38
#define sprintf
Definition: port.h:227
Definition: type.h:103
char stmtID[STMTID_SIZE]
Definition: prepare.c:31
long execs
Definition: prepare.c:33

References AddStmtToCache(), compat, ecpg_find_prepared_statement(), ecpg_get_connection(), ecpg_log(), ecpg_strdup(), ECPGprepare(), stmtCacheEntry::execs, name, nextStmtID, prepare_common(), SearchStmtCache(), sprintf, stmtCacheEntries, stmtCacheEntry::stmtID, and STMTID_SIZE.

Referenced by ecpg_do_prologue().

◆ ecpg_autostart_transaction()

bool ecpg_autostart_transaction ( struct statement stmt)

Definition at line 1581 of file execute.c.

1582 {
1584  {
1585  stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1586  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1587  {
1588  ecpg_free_params(stmt, false);
1589  return false;
1590  }
1591  PQclear(stmt->results);
1592  stmt->results = NULL;
1593  }
1594  return true;
1595 }
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:281
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1106
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6790
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2269
@ PQTRANS_IDLE
Definition: libpq-fe.h:118
PGconn * connection
struct connection * connection
enum COMPAT_MODE compat
PGresult * results

References connection::autocommit, statement::compat, statement::connection, connection::connection, ecpg_check_PQresult(), ecpg_free_params(), statement::lineno, PQclear(), PQexec(), PQTRANS_IDLE, PQtransactionStatus(), and statement::results.

Referenced by ecpg_do().

◆ ecpg_build_compat_sqlda()

struct sqlda_compat* ecpg_build_compat_sqlda ( int  line,
PGresult res,
int  row,
enum  COMPAT_MODE 
)

Definition at line 205 of file sqlda.c.

206 {
207  struct sqlda_compat *sqlda;
208  struct sqlvar_compat *sqlvar;
209  char *fname;
210  long size;
211  int sqld;
212  int i;
213 
214  size = sqlda_compat_total_size(res, row, compat);
215  sqlda = (struct sqlda_compat *) ecpg_alloc(size, line);
216  if (!sqlda)
217  return NULL;
218 
219  memset(sqlda, 0, size);
220  sqlvar = (struct sqlvar_compat *) (sqlda + 1);
221  sqld = PQnfields(res);
222  fname = (char *) (sqlvar + sqld);
223 
224  sqlda->sqld = sqld;
225  ecpg_log("ecpg_build_compat_sqlda on line %d sqld = %d\n", line, sqld);
226  sqlda->desc_occ = size; /* cheat here, keep the full allocated size */
227  sqlda->sqlvar = sqlvar;
228 
229  for (i = 0; i < sqlda->sqld; i++)
230  {
232  strcpy(fname, PQfname(res, i));
233  sqlda->sqlvar[i].sqlname = fname;
234  fname += strlen(sqlda->sqlvar[i].sqlname) + 1;
235 
236  /*
237  * this is reserved for future use, so we leave it empty for the time
238  * being
239  */
240  /* sqlda->sqlvar[i].sqlformat = (char *) (long) PQfformat(res, i); */
241  sqlda->sqlvar[i].sqlxid = PQftype(res, i);
242  sqlda->sqlvar[i].sqltypelen = PQfsize(res, i);
243  }
244 
245  return sqlda;
246 }
char * ecpg_alloc(long, int)
Definition: memory.c:19
int sqlda_dynamic_type(Oid, enum COMPAT_MODE)
Definition: typename.c:107
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3573
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3421
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3343
int PQfsize(const PGresult *res, int field_num)
Definition: fe-exec.c:3584
int i
Definition: isn.c:73
static long sqlda_compat_total_size(const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:157
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40
short desc_occ
Definition: sqlda-compat.h:42
short sqltypelen
Definition: sqlda-compat.h:21
char * sqlname
Definition: sqlda-compat.h:14

References compat, sqlda_compat::desc_occ, ecpg_alloc(), ecpg_log(), i, PQfname(), PQfsize(), PQftype(), PQnfields(), res, sqlda_compat::sqld, sqlda_compat_total_size(), sqlda_dynamic_type(), sqlvar_compat::sqlname, sqlvar_compat::sqltype, sqlvar_compat::sqltypelen, sqlda_compat::sqlvar, and sqlvar_compat::sqlxid.

Referenced by ecpg_process_output(), and ECPGdescribe().

◆ ecpg_build_native_sqlda()

struct sqlda_struct* ecpg_build_native_sqlda ( int  line,
PGresult res,
int  row,
enum  COMPAT_MODE 
)

Definition at line 412 of file sqlda.c.

413 {
414  struct sqlda_struct *sqlda;
415  long size;
416  int i;
417 
418  size = sqlda_native_total_size(res, row, compat);
419  sqlda = (struct sqlda_struct *) ecpg_alloc(size, line);
420  if (!sqlda)
421  return NULL;
422 
423  memset(sqlda, 0, size);
424 
425  sprintf(sqlda->sqldaid, "SQLDA ");
426  sqlda->sqld = sqlda->sqln = PQnfields(res);
427  ecpg_log("ecpg_build_native_sqlda on line %d sqld = %d\n", line, sqlda->sqld);
428  sqlda->sqldabc = sizeof(struct sqlda_struct) + (sqlda->sqld - 1) * sizeof(struct sqlvar_struct);
429 
430  for (i = 0; i < sqlda->sqld; i++)
431  {
432  char *fname;
433 
435  fname = PQfname(res, i);
436  sqlda->sqlvar[i].sqlname.length = strlen(fname);
437  strcpy(sqlda->sqlvar[i].sqlname.data, fname);
438  }
439 
440  return sqlda;
441 }
static long sqlda_native_total_size(const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:186
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
char sqldaid[8]
Definition: sqlda-native.h:35
char data[NAMEDATALEN]
Definition: sqlda-native.h:21
short length
Definition: sqlda-native.h:20
struct sqlname sqlname
Definition: sqlda-native.h:30

References compat, sqlname::data, ecpg_alloc(), ecpg_log(), i, sqlname::length, PQfname(), PQftype(), PQnfields(), res, sprintf, sqlda_struct::sqld, sqlda_dynamic_type(), sqlda_native_total_size(), sqlda_struct::sqldabc, sqlda_struct::sqldaid, sqlda_struct::sqln, sqlvar_struct::sqlname, sqlvar_struct::sqltype, and sqlda_struct::sqlvar.

Referenced by ecpg_process_output(), and ECPGdescribe().

◆ ecpg_build_params()

bool ecpg_build_params ( struct statement stmt)

Definition at line 1213 of file execute.c.

1214 {
1215  struct variable *var;
1216  int desc_counter = 0;
1217  int position = 0;
1218  const char *value;
1219  bool std_strings = false;
1220 
1221  /* Get standard_conforming_strings setting. */
1222  value = PQparameterStatus(stmt->connection->connection, "standard_conforming_strings");
1223  if (value && strcmp(value, "on") == 0)
1224  std_strings = true;
1225 
1226  /*
1227  * If the type is one of the fill in types then we take the argument and
1228  * enter it to our parameter array at the first position. Then if there
1229  * are any more fill in types we add more parameters.
1230  */
1231  var = stmt->inlist;
1232  while (var)
1233  {
1234  char *tobeinserted;
1235  int counter = 1;
1236  bool binary_format;
1237  int binary_length;
1238 
1239 
1240  tobeinserted = NULL;
1241  binary_length = 0;
1242  binary_format = false;
1243 
1244  /*
1245  * A descriptor is a special case since it contains many variables but
1246  * is listed only once.
1247  */
1248  if (var->type == ECPGt_descriptor)
1249  {
1250  /*
1251  * We create an additional variable list here, so the same logic
1252  * applies.
1253  */
1254  struct descriptor *desc;
1255  struct descriptor_item *desc_item;
1256 
1257  desc = ecpg_find_desc(stmt->lineno, var->pointer);
1258  if (desc == NULL)
1259  return false;
1260 
1261  desc_counter++;
1262  for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1263  {
1264  if (desc_item->num != desc_counter)
1265  continue;
1266 
1267  if (!store_input_from_desc(stmt, desc_item, &tobeinserted))
1268  return false;
1269 
1270  if (desc_item->is_binary)
1271  {
1272  binary_length = desc_item->data_len;
1273  binary_format = true;
1274  }
1275  break;
1276  }
1277  if (desc->count == desc_counter)
1278  desc_counter = 0;
1279  }
1280  else if (var->type == ECPGt_sqlda)
1281  {
1282  if (INFORMIX_MODE(stmt->compat))
1283  {
1284  struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1285  struct variable desc_inlist;
1286  int i;
1287 
1288  if (sqlda == NULL)
1289  return false;
1290 
1291  desc_counter++;
1292  for (i = 0; i < sqlda->sqld; i++)
1293  {
1294  if (i + 1 == desc_counter)
1295  {
1296  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1297  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1298  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1299  switch (desc_inlist.type)
1300  {
1301  case ECPGt_char:
1302  case ECPGt_varchar:
1303  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1304  break;
1305  default:
1306  desc_inlist.varcharsize = 0;
1307  break;
1308  }
1309  desc_inlist.arrsize = 1;
1310  desc_inlist.offset = 0;
1311  if (sqlda->sqlvar[i].sqlind)
1312  {
1313  desc_inlist.ind_type = ECPGt_short;
1314  /* ECPG expects indicator value < 0 */
1315  if (*(sqlda->sqlvar[i].sqlind))
1316  *(sqlda->sqlvar[i].sqlind) = -1;
1317  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1318  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1319  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1320  desc_inlist.ind_offset = 0;
1321  }
1322  else
1323  {
1324  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1325  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1326  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1327  }
1328  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1329  return false;
1330 
1331  break;
1332  }
1333  }
1334  if (sqlda->sqld == desc_counter)
1335  desc_counter = 0;
1336  }
1337  else
1338  {
1339  struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1340  struct variable desc_inlist;
1341  int i;
1342 
1343  if (sqlda == NULL)
1344  return false;
1345 
1346  desc_counter++;
1347  for (i = 0; i < sqlda->sqln; i++)
1348  {
1349  if (i + 1 == desc_counter)
1350  {
1351  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1352  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1353  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1354  switch (desc_inlist.type)
1355  {
1356  case ECPGt_char:
1357  case ECPGt_varchar:
1358  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1359  break;
1360  default:
1361  desc_inlist.varcharsize = 0;
1362  break;
1363  }
1364  desc_inlist.arrsize = 1;
1365  desc_inlist.offset = 0;
1366  if (sqlda->sqlvar[i].sqlind)
1367  {
1368  desc_inlist.ind_type = ECPGt_short;
1369  /* ECPG expects indicator value < 0 */
1370  if (*(sqlda->sqlvar[i].sqlind))
1371  *(sqlda->sqlvar[i].sqlind) = -1;
1372  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1373  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1374  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1375  desc_inlist.ind_offset = 0;
1376  }
1377  else
1378  {
1379  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1380  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1381  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1382  }
1383  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1384  return false;
1385 
1386  break;
1387  }
1388  }
1389  if (sqlda->sqln == desc_counter)
1390  desc_counter = 0;
1391  }
1392  }
1393  else
1394  {
1395  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1396  return false;
1397 
1398  if (var->type == ECPGt_bytea)
1399  {
1400  binary_length = ((struct ECPGgeneric_bytea *) (var->value))->len;
1401  binary_format = true;
1402  }
1403  }
1404 
1405  /*
1406  * now tobeinserted points to an area that contains the next
1407  * parameter; now find the position in the string where it belongs
1408  */
1409  if ((position = next_insert(stmt->command, position, stmt->questionmarks, std_strings) + 1) == 0)
1410  {
1411  /*
1412  * We have an argument but we don't have the matched up
1413  * placeholder in the string
1414  */
1417  NULL);
1418  ecpg_free_params(stmt, false);
1419  ecpg_free(tobeinserted);
1420  return false;
1421  }
1422 
1423  /*
1424  * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1425  * to simulate a dynamic cursor because there is no backend
1426  * functionality for it
1427  */
1428  if (var->type == ECPGt_char_variable)
1429  {
1430  int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1431 
1432  if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1433  {
1434  ecpg_free_params(stmt, false);
1435  return false;
1436  }
1437  tobeinserted = NULL;
1438  }
1439 
1440  /*
1441  * if the placeholder is '$0' we have to replace it on the client side
1442  * this is for places we want to support variables at that are not
1443  * supported in the backend
1444  */
1445  else if (stmt->command[position] == '0')
1446  {
1447  if (stmt->statement_type == ECPGst_prepare ||
1449  {
1450  /* Need to double-quote the inserted statement name. */
1451  char *str = ecpg_alloc(strlen(tobeinserted) + 2 + 1,
1452  stmt->lineno);
1453 
1454  if (!str)
1455  {
1456  ecpg_free(tobeinserted);
1457  ecpg_free_params(stmt, false);
1458  return false;
1459  }
1460  sprintf(str, "\"%s\"", tobeinserted);
1461  ecpg_free(tobeinserted);
1462  tobeinserted = str;
1463  }
1464 
1465  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1466  {
1467  ecpg_free_params(stmt, false);
1468  return false;
1469  }
1470  tobeinserted = NULL;
1471  }
1472  else if (stmt->statement_type == ECPGst_exec_with_exprlist)
1473  {
1474  if (binary_format)
1475  {
1476  char *p = convert_bytea_to_string(tobeinserted,
1477  binary_length,
1478  stmt->lineno);
1479 
1480  ecpg_free(tobeinserted);
1481  if (!p)
1482  {
1483  ecpg_free_params(stmt, false);
1484  return false;
1485  }
1486  tobeinserted = p;
1487  }
1488 
1489  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1490  {
1491  ecpg_free_params(stmt, false);
1492  return false;
1493  }
1494  tobeinserted = NULL;
1495  }
1496  else
1497  {
1498  bool realloc_failed = false;
1499  char **newparamvalues;
1500  int *newparamlengths;
1501  int *newparamformats;
1502 
1503  /* enlarge all the param arrays */
1504  if ((newparamvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1505  stmt->paramvalues = newparamvalues;
1506  else
1507  realloc_failed = true;
1508 
1509  if ((newparamlengths = (int *) ecpg_realloc(stmt->paramlengths, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1510  stmt->paramlengths = newparamlengths;
1511  else
1512  realloc_failed = true;
1513 
1514  if ((newparamformats = (int *) ecpg_realloc(stmt->paramformats, sizeof(int) * (stmt->nparams + 1), stmt->lineno)))
1515  stmt->paramformats = newparamformats;
1516  else
1517  realloc_failed = true;
1518 
1519  if (realloc_failed)
1520  {
1521  ecpg_free_params(stmt, false);
1522  ecpg_free(tobeinserted);
1523  return false;
1524  }
1525 
1526  /* only now can we assign ownership of "tobeinserted" to stmt */
1527  stmt->paramvalues[stmt->nparams] = tobeinserted;
1528  stmt->paramlengths[stmt->nparams] = binary_length;
1529  stmt->paramformats[stmt->nparams] = (binary_format ? 1 : 0);
1530  stmt->nparams++;
1531 
1532  /* let's see if this was an old style placeholder */
1533  if (stmt->command[position] == '?')
1534  {
1535  /* yes, replace with new style */
1536  int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1537  * size we need */
1538 
1539  if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1540  {
1541  ecpg_free_params(stmt, false);
1542  return false;
1543  }
1544 
1545  snprintf(tobeinserted, buffersize, "$%d", counter++);
1546 
1547  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1548  {
1549  ecpg_free_params(stmt, false);
1550  return false;
1551  }
1552  tobeinserted = NULL;
1553  }
1554  }
1555 
1556  if (desc_counter == 0)
1557  var = var->next;
1558  }
1559 
1560  /*
1561  * Check if there are unmatched things left. PREPARE AS has no parameter.
1562  * Check other statement.
1563  */
1564  if (stmt->statement_type != ECPGst_prepare &&
1565  next_insert(stmt->command, position, stmt->questionmarks, std_strings) >= 0)
1566  {
1569  ecpg_free_params(stmt, false);
1570  return false;
1571  }
1572 
1573  return true;
1574 }
#define ECPG_TOO_MANY_ARGUMENTS
Definition: ecpgerrno.h:19
#define ECPG_TOO_FEW_ARGUMENTS
Definition: ecpgerrno.h:20
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:842
char * ecpg_realloc(void *, long, int)
Definition: memory.c:33
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS
#define INFORMIX_MODE(X)
void ecpg_free(void *)
Definition: memory.c:13
@ ECPGst_exec_with_exprlist
Definition: ecpgtype.h:102
@ ECPGst_prepare
Definition: ecpgtype.h:101
@ ECPGt_sqlda
Definition: ecpgtype.h:66
@ ECPGt_short
Definition: ecpgtype.h:43
@ ECPGt_char_variable
Definition: ecpgtype.h:60
@ ECPGt_bytea
Definition: ecpgtype.h:67
@ ECPGt_varchar
Definition: ecpgtype.h:48
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_descriptor
Definition: ecpgtype.h:59
@ ECPGt_char
Definition: ecpgtype.h:43
static bool insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
Definition: execute.c:1127
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:506
static bool store_input_from_desc(struct statement *stmt, struct descriptor_item *desc_item, char **tobeinserted)
Definition: execute.c:1159
static char * convert_bytea_to_string(char *from_data, int from_len, int lineno)
Definition: execute.c:488
static int next_insert(char *text, int pos, bool questionmarks, bool std_strings)
Definition: execute.c:111
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6800
static struct @151 value
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
const void size_t len
#define snprintf
Definition: port.h:225
struct descriptor_item * next
struct descriptor_item * items
short * sqlind
Definition: sqlda-compat.h:13
char * sqldata
Definition: sqlda-compat.h:12
char * sqldata
Definition: sqlda-native.h:28
short * sqlind
Definition: sqlda-native.h:29
struct variable * inlist
bool questionmarks
char * command
char ** paramvalues
bool force_indicator
int * paramformats
enum ECPG_statement_type statement_type
int * paramlengths
void * value
struct variable * next
enum ECPGttype type
void * pointer

References statement::command, statement::compat, statement::connection, connection::connection, convert_bytea_to_string(), descriptor::count, descriptor_item::data_len, ecpg_alloc(), ecpg_find_desc(), ecpg_free(), ecpg_free_params(), ecpg_raise(), ecpg_realloc(), ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS, ecpg_store_input(), ECPG_TOO_FEW_ARGUMENTS, ECPG_TOO_MANY_ARGUMENTS, ECPGst_exec_with_exprlist, ECPGst_prepare, ECPGt_bytea, ECPGt_char, ECPGt_char_variable, ECPGt_descriptor, ECPGt_NO_INDICATOR, ECPGt_short, ECPGt_sqlda, ECPGt_varchar, statement::force_indicator, i, if(), INFORMIX_MODE, statement::inlist, insert_tobeinserted(), descriptor_item::is_binary, descriptor::items, len, statement::lineno, descriptor_item::next, variable::next, next_insert(), statement::nparams, descriptor_item::num, statement::paramformats, statement::paramlengths, statement::paramvalues, variable::pointer, PQparameterStatus(), statement::questionmarks, snprintf, sprintf, sqlda_compat::sqld, sqlvar_compat::sqldata, sqlvar_struct::sqldata, sqlvar_compat::sqlind, sqlvar_struct::sqlind, sqlda_struct::sqln, sqlvar_compat::sqltype, sqlvar_struct::sqltype, sqlda_compat::sqlvar, sqlda_struct::sqlvar, statement::statement_type, store_input_from_desc(), generate_unaccent_rules::str, variable::type, value, and variable::value.

Referenced by ecpg_do().

◆ ecpg_check_PQresult()

bool ecpg_check_PQresult ( PGresult results,
int  lineno,
PGconn connection,
enum  COMPAT_MODE 
)

Definition at line 281 of file error.c.

282 {
283  if (results == NULL)
284  {
285  ecpg_log("ecpg_check_PQresult on line %d: no result - %s", lineno, PQerrorMessage(connection));
286  ecpg_raise_backend(lineno, NULL, connection, compat);
287  return false;
288  }
289 
290  switch (PQresultStatus(results))
291  {
292 
293  case PGRES_TUPLES_OK:
294  return true;
295  break;
296  case PGRES_EMPTY_QUERY:
297  /* do nothing */
299  PQclear(results);
300  return false;
301  break;
302  case PGRES_COMMAND_OK:
303  return true;
304  break;
306  case PGRES_FATAL_ERROR:
307  case PGRES_BAD_RESPONSE:
308  ecpg_log("ecpg_check_PQresult on line %d: bad response - %s", lineno, PQresultErrorMessage(results));
309  ecpg_raise_backend(lineno, results, connection, compat);
310  PQclear(results);
311  return false;
312  break;
313  case PGRES_COPY_OUT:
314  return true;
315  break;
316  case PGRES_COPY_IN:
317  ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno);
319  PQclear(results);
320  return false;
321  break;
322  default:
323  ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
324  lineno);
325  ecpg_raise_backend(lineno, results, connection, compat);
326  PQclear(results);
327  return false;
328  break;
329  }
330 }
#define ECPG_EMPTY
Definition: ecpgerrno.h:30
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
void ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
Definition: error.c:219
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6835
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3265
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3281
int PQendcopy(PGconn *conn)
Definition: fe-exec.c:2880
@ PGRES_COPY_IN
Definition: libpq-fe.h:104
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:108
@ PGRES_COPY_OUT
Definition: libpq-fe.h:103
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:96
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:105
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:107
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100

References compat, ECPG_EMPTY, ecpg_log(), ecpg_raise(), ecpg_raise_backend(), ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_TUPLES_OK, PQclear(), PQendcopy(), PQerrorMessage(), PQresultErrorMessage(), and PQresultStatus().

Referenced by deallocate_one(), ecpg_autostart_transaction(), ecpg_execute(), ECPGdescribe(), ECPGsetcommit(), ECPGtrans(), and prepare_common().

◆ ecpg_clear_auto_mem()

void ecpg_clear_auto_mem ( void  )

Definition at line 158 of file memory.c.

159 {
160  struct auto_mem *am = get_auto_allocs();
161 
162  /* only free our own structure */
163  if (am)
164  {
165  do
166  {
167  struct auto_mem *act = am;
168 
169  am = am->next;
170  ecpg_free(act);
171  } while (am);
172  set_auto_allocs(NULL);
173  }
174 }

References ecpg_free(), get_auto_allocs, auto_mem::next, and set_auto_allocs.

Referenced by ecpg_do_prologue(), and ECPGconnect().

◆ ecpg_deallocate_all_conn()

bool ecpg_deallocate_all_conn ( int  lineno,
enum COMPAT_MODE  c,
struct connection conn 
)

Definition at line 337 of file prepare.c.

338 {
339  /* deallocate all prepared statements */
340  while (con->prep_stmts)
341  {
342  if (!deallocate_one(lineno, c, con, NULL, con->prep_stmts))
343  return false;
344  }
345 
346  return true;
347 }
static bool deallocate_one(int lineno, enum COMPAT_MODE c, struct connection *con, struct prepared_statement *prev, struct prepared_statement *this)
Definition: prepare.c:260
char * c

References deallocate_one(), and connection::prep_stmts.

Referenced by ecpg_finish(), and ECPGdeallocate_all().

◆ ecpg_do()

bool ecpg_do ( const int  lineno,
const int  compat,
const int  force_indicator,
const char *  connection_name,
const bool  questionmarks,
const int  st,
const char *  query,
va_list  args 
)

Definition at line 2245 of file execute.c.

2246 {
2247  struct statement *stmt = NULL;
2248 
2249  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2251  query, args, &stmt))
2252  goto fail;
2253 
2254  if (!ecpg_build_params(stmt))
2255  goto fail;
2256 
2257  if (!ecpg_autostart_transaction(stmt))
2258  goto fail;
2259 
2260  if (!ecpg_execute(stmt))
2261  goto fail;
2262 
2263  if (!ecpg_process_output(stmt, true))
2264  goto fail;
2265 
2266  ecpg_do_epilogue(stmt);
2267  return true;
2268 
2269 fail:
2270  ecpg_do_epilogue(stmt);
2271  return false;
2272 }
bool force_indicator
Definition: ecpg.c:18
bool questionmarks
Definition: ecpg.c:19
ECPG_statement_type
Definition: ecpgtype.h:96
bool ecpg_build_params(struct statement *stmt)
Definition: execute.c:1213
bool ecpg_execute(struct statement *stmt)
Definition: execute.c:1602
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:2213
bool ecpg_do_prologue(int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, enum ECPG_statement_type statement_type, const char *query, va_list args, struct statement **stmt_out)
Definition: execute.c:1944
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition: execute.c:1671
bool ecpg_autostart_transaction(struct statement *stmt)
Definition: execute.c:1581

References generate_unaccent_rules::args, compat, ecpg_autostart_transaction(), ecpg_build_params(), ecpg_do_epilogue(), ecpg_do_prologue(), ecpg_execute(), ecpg_process_output(), force_indicator, statement::lineno, and questionmarks.

Referenced by ECPGdo().

◆ ecpg_do_epilogue()

void ecpg_do_epilogue ( struct statement stmt)

Definition at line 2213 of file execute.c.

2214 {
2215  if (stmt == NULL)
2216  return;
2217 
2218 #ifdef HAVE_USELOCALE
2219  if (stmt->oldlocale != (locale_t) 0)
2220  uselocale(stmt->oldlocale);
2221 #else
2222  if (stmt->oldlocale)
2223  setlocale(LC_NUMERIC, stmt->oldlocale);
2224 #ifdef HAVE__CONFIGTHREADLOCALE
2225 
2226  /*
2227  * This is a bit trickier than it looks: if we failed partway through
2228  * statement initialization, oldthreadlocale could still be 0. But that's
2229  * okay because a call with 0 is defined to be a no-op.
2230  */
2231  if (stmt->oldthreadlocale != -1)
2232  (void) _configthreadlocale(stmt->oldthreadlocale);
2233 #endif
2234 #endif
2235 
2236  free_statement(stmt);
2237 }
static void free_statement(struct statement *stmt)
Definition: execute.c:96
char * oldlocale
#define locale_t
Definition: win32_port.h:401
#define setlocale(a, b)
Definition: win32_port.h:446

References free_statement(), locale_t, statement::oldlocale, and setlocale.

Referenced by ecpg_do(), and ecpg_do_prologue().

◆ ecpg_do_prologue()

bool ecpg_do_prologue ( int  lineno,
const int  compat,
const int  force_indicator,
const char *  connection_name,
const bool  questionmarks,
enum  ECPG_statement_type,
const char *  query,
va_list  args,
struct statement **  stmt_out 
)

Definition at line 1944 of file execute.c.

1948 {
1949  struct statement *stmt = NULL;
1950  struct connection *con;
1951  enum ECPGttype type;
1952  struct variable **list;
1953  char *prepname;
1954  bool is_prepared_name_set;
1955 
1956  *stmt_out = NULL;
1957 
1958  if (!query)
1959  {
1961  return false;
1962  }
1963 
1964 #ifdef ENABLE_THREAD_SAFETY
1965  ecpg_pthreads_init();
1966 #endif
1967 
1968  con = ecpg_get_connection(connection_name);
1969 
1970  if (!ecpg_init(con, connection_name, lineno))
1971  return false;
1972 
1973  stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1974 
1975  if (stmt == NULL)
1976  return false;
1977 
1978  /*
1979  * Make sure we do NOT honor the locale for numeric input/output since the
1980  * database wants the standard decimal point. If available, use
1981  * uselocale() for this because it's thread-safe. Windows doesn't have
1982  * that, but it usually does have _configthreadlocale(). In some versions
1983  * of MinGW, _configthreadlocale() exists but always returns -1 --- so
1984  * treat that situation as if the function doesn't exist.
1985  */
1986 #ifdef HAVE_USELOCALE
1987 
1988  /*
1989  * Since ecpg_init() succeeded, we have a connection. Any successful
1990  * connection initializes ecpg_clocale.
1991  */
1992  Assert(ecpg_clocale);
1993  stmt->oldlocale = uselocale(ecpg_clocale);
1994  if (stmt->oldlocale == (locale_t) 0)
1995  {
1996  ecpg_do_epilogue(stmt);
1997  return false;
1998  }
1999 #else
2000 #ifdef HAVE__CONFIGTHREADLOCALE
2001  stmt->oldthreadlocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
2002 #endif
2003  stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
2004  if (stmt->oldlocale == NULL)
2005  {
2006  ecpg_do_epilogue(stmt);
2007  return false;
2008  }
2009  setlocale(LC_NUMERIC, "C");
2010 #endif
2011 
2012  /*
2013  * If statement type is ECPGst_prepnormal we are supposed to prepare the
2014  * statement before executing them
2015  */
2017  {
2018  if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
2019  {
2020  ecpg_do_epilogue(stmt);
2021  return false;
2022  }
2023 
2024  /*
2025  * statement is now prepared, so instead of the query we have to
2026  * execute the name
2027  */
2028  stmt->command = prepname;
2030  }
2031  else
2032  stmt->command = ecpg_strdup(query, lineno);
2033 
2034  stmt->name = NULL;
2035 
2037  {
2038  /* if we have an EXECUTE command, only the name is send */
2039  char *command = ecpg_prepared(stmt->command, con);
2040 
2041  if (command)
2042  {
2043  stmt->name = stmt->command;
2044  stmt->command = ecpg_strdup(command, lineno);
2045  }
2046  else
2047  {
2049  ecpg_do_epilogue(stmt);
2050  return false;
2051  }
2052  }
2053  /* name of PREPARE AS will be set in loop of inlist */
2054 
2055  stmt->connection = con;
2056  stmt->lineno = lineno;
2057  stmt->compat = compat;
2059  stmt->questionmarks = questionmarks;
2061 
2062  /*------
2063  * create a list of variables
2064  *
2065  * The variables are listed with input variables preceding output
2066  * variables. The end of each group is marked by an end marker.
2067  * Per variable we list:
2068  *
2069  * type - as defined in ecpgtype.h
2070  * value - where to store the data
2071  * varcharsize - length of string in case we have a stringvariable, else 0
2072  * arraysize - 0 for pointer (we don't know the size of the array), 1 for
2073  * simple variable, size for arrays
2074  * offset - offset between ith and (i+1)th entry in an array, normally
2075  * that means sizeof(type)
2076  * ind_type - type of indicator variable
2077  * ind_pointer - pointer to indicator variable
2078  * ind_varcharsize - empty
2079  * ind_arrsize - arraysize of indicator array
2080  * ind_offset - indicator offset
2081  *------
2082  */
2083 
2084  is_prepared_name_set = false;
2085 
2086  list = &(stmt->inlist);
2087 
2088  type = va_arg(args, enum ECPGttype);
2089 
2090  while (type != ECPGt_EORT)
2091  {
2092  if (type == ECPGt_EOIT)
2093  list = &(stmt->outlist);
2094  else
2095  {
2096  struct variable *var,
2097  *ptr;
2098 
2099  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
2100  {
2101  ecpg_do_epilogue(stmt);
2102  return false;
2103  }
2104 
2105  var->type = type;
2106  var->pointer = va_arg(args, char *);
2107 
2108  var->varcharsize = va_arg(args, long);
2109  var->arrsize = va_arg(args, long);
2110  var->offset = va_arg(args, long);
2111 
2112  /*
2113  * Unknown array size means pointer to an array. Unknown
2114  * varcharsize usually also means pointer. But if the type is
2115  * character and the array size is known, it is an array of
2116  * pointers to char, so use var->pointer as it is.
2117  */
2118  if (var->arrsize == 0 ||
2119  (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
2120  var->value = *((char **) (var->pointer));
2121  else
2122  var->value = var->pointer;
2123 
2124  /*
2125  * negative values are used to indicate an array without given
2126  * bounds
2127  */
2128  /* reset to zero for us */
2129  if (var->arrsize < 0)
2130  var->arrsize = 0;
2131  if (var->varcharsize < 0)
2132  var->varcharsize = 0;
2133 
2134  var->next = NULL;
2135 
2136  var->ind_type = va_arg(args, enum ECPGttype);
2137  var->ind_pointer = va_arg(args, char *);
2138  var->ind_varcharsize = va_arg(args, long);
2139  var->ind_arrsize = va_arg(args, long);
2140  var->ind_offset = va_arg(args, long);
2141 
2142  if (var->ind_type != ECPGt_NO_INDICATOR
2143  && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
2144  var->ind_value = *((char **) (var->ind_pointer));
2145  else
2146  var->ind_value = var->ind_pointer;
2147 
2148  /*
2149  * negative values are used to indicate an array without given
2150  * bounds
2151  */
2152  /* reset to zero for us */
2153  if (var->ind_arrsize < 0)
2154  var->ind_arrsize = 0;
2155  if (var->ind_varcharsize < 0)
2156  var->ind_varcharsize = 0;
2157 
2158  /* if variable is NULL, the statement hasn't been prepared */
2159  if (var->pointer == NULL)
2160  {
2162  ecpg_free(var);
2163  ecpg_do_epilogue(stmt);
2164  return false;
2165  }
2166 
2167  for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
2168  ;
2169 
2170  if (ptr == NULL)
2171  *list = var;
2172  else
2173  ptr->next = var;
2174 
2175  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2176  {
2177  stmt->name = ecpg_strdup(var->value, lineno);
2178  is_prepared_name_set = true;
2179  }
2180  }
2181 
2182  type = va_arg(args, enum ECPGttype);
2183  }
2184 
2185  /* are we connected? */
2186  if (con == NULL || con->connection == NULL)
2187  {
2188  ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
2189  ecpg_do_epilogue(stmt);
2190  return false;
2191  }
2192 
2193  if (!is_prepared_name_set && stmt->statement_type == ECPGst_prepare)
2194  {
2196  ecpg_do_epilogue(stmt);
2197  return false;
2198  }
2199 
2200  /* initialize auto_mem struct */
2202 
2203  *stmt_out = stmt;
2204 
2205  return true;
2206 }
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
#define ECPG_NOT_CONN
Definition: ecpgerrno.h:37
#define ecpg_gettext(x)
char * ecpg_prepared(const char *, struct connection *)
Definition: prepare.c:357
void ecpg_clear_auto_mem(void)
Definition: memory.c:158
bool ecpg_init(const struct connection *, const char *, const int)
Definition: misc.c:104
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
void bool ecpg_auto_prepare(int, const char *, const int, char **, const char *)
Definition: prepare.c:553
@ ECPGst_execute
Definition: ecpgtype.h:98
@ ECPGst_prepnormal
Definition: ecpgtype.h:100
ECPGttype
Definition: ecpgtype.h:42
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_unsigned_char
Definition: ecpgtype.h:43
@ ECPGt_EORT
Definition: ecpgtype.h:63
Assert(fmt[strlen(fmt) - 1] !='\n')
struct variable * outlist
char * name
long ind_arrsize
long ind_offset
void * ind_value
long ind_varcharsize
long varcharsize
enum ECPGttype ind_type
void * ind_pointer

References generate_unaccent_rules::args, variable::arrsize, Assert(), statement::command, statement::compat, compat, statement::connection, connection::connection, ecpg_alloc(), ecpg_auto_prepare(), ecpg_clear_auto_mem(), ecpg_do_epilogue(), ECPG_EMPTY, ecpg_free(), ecpg_get_connection(), ecpg_gettext, ecpg_init(), ECPG_INVALID_STMT, ECPG_NOT_CONN, ecpg_prepared(), ecpg_raise(), ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, ecpg_strdup(), ECPG_TOO_FEW_ARGUMENTS, ECPGst_execute, ECPGst_prepare, ECPGst_prepnormal, ECPGt_char, ECPGt_EOIT, ECPGt_EORT, ECPGt_NO_INDICATOR, ECPGt_unsigned_char, statement::force_indicator, force_indicator, variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, statement::inlist, statement::lineno, sort-test::list, locale_t, statement::name, connection::name, variable::next, variable::offset, statement::oldlocale, statement::outlist, variable::pointer, statement::questionmarks, questionmarks, setlocale, statement::statement_type, generate_unaccent_rules::type, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_do().

◆ ecpg_dynamic_type()

int ecpg_dynamic_type ( Oid  type)

Definition at line 73 of file typename.c.

74 {
75  switch (type)
76  {
77  case BOOLOID:
78  return SQL3_BOOLEAN; /* bool */
79  case INT2OID:
80  return SQL3_SMALLINT; /* int2 */
81  case INT4OID:
82  return SQL3_INTEGER; /* int4 */
83  case TEXTOID:
84  return SQL3_CHARACTER; /* text */
85  case FLOAT4OID:
86  return SQL3_REAL; /* float4 */
87  case FLOAT8OID:
88  return SQL3_DOUBLE_PRECISION; /* float8 */
89  case BPCHAROID:
90  return SQL3_CHARACTER; /* bpchar */
91  case VARCHAROID:
92  return SQL3_CHARACTER_VARYING; /* varchar */
93  case DATEOID:
94  return SQL3_DATE_TIME_TIMESTAMP; /* date */
95  case TIMEOID:
96  return SQL3_DATE_TIME_TIMESTAMP; /* time */
97  case TIMESTAMPOID:
98  return SQL3_DATE_TIME_TIMESTAMP; /* datetime */
99  case NUMERICOID:
100  return SQL3_NUMERIC; /* numeric */
101  default:
102  return 0;
103  }
104 }
@ SQL3_DATE_TIME_TIMESTAMP
Definition: sql3types.h:18
@ SQL3_INTEGER
Definition: sql3types.h:13
@ SQL3_BOOLEAN
Definition: sql3types.h:24
@ SQL3_CHARACTER_VARYING
Definition: sql3types.h:20
@ SQL3_SMALLINT
Definition: sql3types.h:14
@ SQL3_DOUBLE_PRECISION
Definition: sql3types.h:17
@ SQL3_NUMERIC
Definition: sql3types.h:11
@ SQL3_CHARACTER
Definition: sql3types.h:10
@ SQL3_REAL
Definition: sql3types.h:16

References SQL3_BOOLEAN, SQL3_CHARACTER, SQL3_CHARACTER_VARYING, SQL3_DATE_TIME_TIMESTAMP, SQL3_DOUBLE_PRECISION, SQL3_INTEGER, SQL3_NUMERIC, SQL3_REAL, SQL3_SMALLINT, and generate_unaccent_rules::type.

Referenced by ECPGget_desc().

◆ ecpg_execute()

bool ecpg_execute ( struct statement stmt)

Definition at line 1602 of file execute.c.

1603 {
1604  ecpg_log("ecpg_execute on line %d: query: %s; with %d parameter(s) on connection %s\n", stmt->lineno, stmt->command, stmt->nparams, stmt->connection->name);
1605  if (stmt->statement_type == ECPGst_execute)
1606  {
1608  stmt->name,
1609  stmt->nparams,
1610  (const char *const *) stmt->paramvalues,
1611  (const int *) stmt->paramlengths,
1612  (const int *) stmt->paramformats,
1613  0);
1614  ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1615  }
1616  else
1617  {
1618  if (stmt->nparams == 0)
1619  {
1620  stmt->results = PQexec(stmt->connection->connection, stmt->command);
1621  ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1622  }
1623  else
1624  {
1625  stmt->results = PQexecParams(stmt->connection->connection,
1626  stmt->command, stmt->nparams, NULL,
1627  (const char *const *) stmt->paramvalues,
1628  (const int *) stmt->paramlengths,
1629  (const int *) stmt->paramformats,
1630  0);
1631 
1632  ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1633  }
1634 
1635  if (stmt->statement_type == ECPGst_prepare)
1636  {
1637  if (!ecpg_register_prepared_stmt(stmt))
1638  {
1639  ecpg_free_params(stmt, true);
1640  return false;
1641  }
1642  }
1643  }
1644 
1645  ecpg_free_params(stmt, true);
1646 
1647  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1648  return false;
1649 
1650  return true;
1651 }
bool ecpg_register_prepared_stmt(struct statement *)
Definition: prepare.c:59
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:2283
PGresult * PQexecPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:2330

References statement::command, statement::compat, statement::connection, connection::connection, ecpg_check_PQresult(), ecpg_free_params(), ecpg_log(), ecpg_register_prepared_stmt(), ECPGst_execute, ECPGst_prepare, statement::lineno, statement::name, connection::name, statement::nparams, statement::paramformats, statement::paramlengths, statement::paramvalues, PQexec(), PQexecParams(), PQexecPrepared(), statement::results, and statement::statement_type.

Referenced by ecpg_do().

◆ 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
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_find_prepared_statement()

struct prepared_statement* ecpg_find_prepared_statement ( const char *  name,
struct connection con,
struct prepared_statement **  prev_ 
)

Definition at line 239 of file prepare.c.

241 {
242  struct prepared_statement *this,
243  *prev;
244 
245  for (this = con->prep_stmts, prev = NULL;
246  this != NULL;
247  prev = this, this = this->next)
248  {
249  if (strcmp(this->name, name) == 0)
250  {
251  if (prev_)
252  *prev_ = prev;
253  return this;
254  }
255  }
256  return NULL;
257 }
struct prepared_statement * prep_stmts
struct prepared_statement * next

References name, prepared_statement::next, and connection::prep_stmts.

Referenced by ecpg_auto_prepare(), ecpg_freeStmtCacheEntry(), ecpg_prepared(), ecpg_register_prepared_stmt(), ECPGdeallocate(), ECPGdescribe(), and ECPGprepare().

◆ ecpg_free()

◆ ecpg_free_params()

void ecpg_free_params ( struct statement stmt,
bool  print 
)

Definition at line 1106 of file execute.c.

1107 {
1108  int n;
1109 
1110  for (n = 0; n < stmt->nparams; n++)
1111  {
1112  if (print)
1113  print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1114  stmt->paramformats[n], stmt->lineno, n + 1);
1115  ecpg_free(stmt->paramvalues[n]);
1116  }
1117  ecpg_free(stmt->paramvalues);
1118  ecpg_free(stmt->paramlengths);
1119  ecpg_free(stmt->paramformats);
1120  stmt->paramvalues = NULL;
1121  stmt->paramlengths = NULL;
1122  stmt->paramformats = NULL;
1123  stmt->nparams = 0;
1124 }
void print(const void *obj)
Definition: print.c:36
static void print_param_value(char *value, int len, int is_binary, int lineno, int nth)
Definition: execute.c:1076

References ecpg_free(), statement::lineno, statement::nparams, statement::paramformats, statement::paramlengths, statement::paramvalues, print(), and print_param_value().

Referenced by ecpg_autostart_transaction(), ecpg_build_params(), and ecpg_execute().

◆ ecpg_get_connection()

struct connection* ecpg_get_connection ( const char *  connection_name)

Definition at line 79 of file connect.c.

80 {
81  struct connection *ret = NULL;
82 
83  if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
84  {
85 #ifdef ENABLE_THREAD_SAFETY
86  ecpg_pthreads_init(); /* ensure actual_connection_key is valid */
87 
88  ret = pthread_getspecific(actual_connection_key);
89 
90  /*
91  * if no connection in TSD for this thread, get the global default
92  * connection and hope the user knows what they're doing (i.e. using
93  * their own mutex to protect that connection from concurrent accesses
94  */
95  if (ret == NULL)
96  /* no TSD connection here either, using global */
97  ret = actual_connection;
98 #else
99  ret = actual_connection;
100 #endif
101  }
102  else
103  {
104 #ifdef ENABLE_THREAD_SAFETY
105  pthread_mutex_lock(&connections_mutex);
106 #endif
107 
108  ret = ecpg_get_connection_nr(connection_name);
109 
110 #ifdef ENABLE_THREAD_SAFETY
111  pthread_mutex_unlock(&connections_mutex);
112 #endif
113  }
114 
115  return ret;
116 }
static struct connection * ecpg_get_connection_nr(const char *connection_name)
Definition: connect.c:40
static struct connection * actual_connection
Definition: connect.c:22
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:54
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:45

References actual_connection, ecpg_get_connection_nr(), pthread_getspecific(), pthread_mutex_lock(), and pthread_mutex_unlock().

Referenced by ecpg_auto_prepare(), ecpg_do_prologue(), ecpg_freeStmtCacheEntry(), ECPGconnect(), ECPGdeallocate(), ECPGdeallocate_all(), ECPGdescribe(), ECPGget_desc(), ECPGget_PGconn(), ECPGprepare(), ECPGprepared_statement(), ECPGsetcommit(), ECPGsetconn(), ECPGstatus(), ECPGtrans(), and ECPGtransactionStatus().

◆ ecpg_get_data()

bool ecpg_get_data ( const PGresult results,
int  act_tuple,
int  act_field,
int  lineno,
enum ECPGttype  type,
enum  ECPGttype,
char *  var,
char *  ind,
long  varcharsize,
long  offset,
long  ind_offset,
enum  ARRAY_TYPE,
enum  COMPAT_MODE,
bool  force_indicator 
)

Definition at line 206 of file data.c.

210 {
211  struct sqlca_t *sqlca = ECPGget_sqlca();
212  char *pval = (char *) PQgetvalue(results, act_tuple, act_field);
213  int binary = PQfformat(results, act_field);
214  int size = PQgetlength(results, act_tuple, act_field);
215  int value_for_indicator = 0;
216  long log_offset;
217 
218  if (sqlca == NULL)
219  {
222  return false;
223  }
224 
225  /*
226  * If we are running in a regression test, do not log the offset variable,
227  * it depends on the machine's alignment.
228  */
230  log_offset = -1;
231  else
232  log_offset = offset;
233 
234  ecpg_log("ecpg_get_data on line %d: RESULT: %s offset: %ld; array: %s\n", lineno, pval ? (binary ? "BINARY" : pval) : "EMPTY", log_offset, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
235 
236  /* pval is a pointer to the value */
237  if (!pval)
238  {
239  /*
240  * This should never happen because we already checked that we found
241  * at least one tuple, but let's play it safe.
242  */
244  return false;
245  }
246 
247  /* We will have to decode the value */
248 
249  /*
250  * check for null value and set indicator accordingly, i.e. -1 if NULL and
251  * 0 if not
252  */
253  if (PQgetisnull(results, act_tuple, act_field))
254  value_for_indicator = -1;
255 
256  switch (ind_type)
257  {
258  case ECPGt_short:
260  *((short *) (ind + ind_offset * act_tuple)) = value_for_indicator;
261  break;
262  case ECPGt_int:
263  case ECPGt_unsigned_int:
264  *((int *) (ind + ind_offset * act_tuple)) = value_for_indicator;
265  break;
266  case ECPGt_long:
267  case ECPGt_unsigned_long:
268  *((long *) (ind + ind_offset * act_tuple)) = value_for_indicator;
269  break;
270  case ECPGt_long_long:
272  *((long long int *) (ind + ind_offset * act_tuple)) = value_for_indicator;
273  break;
274  case ECPGt_NO_INDICATOR:
275  if (value_for_indicator == -1)
276  {
277  if (force_indicator == false)
278  {
279  /*
280  * Informix has an additional way to specify NULLs note
281  * that this uses special values to denote NULL
282  */
283  ECPGset_noind_null(type, var + offset * act_tuple);
284  }
285  else
286  {
289  NULL);
290  return false;
291  }
292  }
293  break;
294  default:
297  ecpg_type_name(ind_type));
298  return false;
299  break;
300  }
301 
302  if (value_for_indicator == -1)
303  return true;
304 
305  /* let's check if it really is an array if it should be one */
306  if (isarray == ECPG_ARRAY_ARRAY)
307  {
308  if (*pval != '{')
309  {
312  return false;
313  }
314 
315  switch (type)
316  {
317  case ECPGt_char:
318  case ECPGt_unsigned_char:
319  case ECPGt_varchar:
320  case ECPGt_string:
321  break;
322 
323  default:
324  pval++;
325  break;
326  }
327  }
328 
329  do
330  {
331  if (binary)
332  {
333  if (varcharsize == 0 || varcharsize * offset >= size)
334  memcpy(var + offset * act_tuple, pval, size);
335  else
336  {
337  memcpy(var + offset * act_tuple, pval, varcharsize * offset);
338 
339  if (varcharsize * offset < size)
340  {
341  /* truncation */
342  switch (ind_type)
343  {
344  case ECPGt_short:
346  *((short *) (ind + ind_offset * act_tuple)) = size;
347  break;
348  case ECPGt_int:
349  case ECPGt_unsigned_int:
350  *((int *) (ind + ind_offset * act_tuple)) = size;
351  break;
352  case ECPGt_long:
353  case ECPGt_unsigned_long:
354  *((long *) (ind + ind_offset * act_tuple)) = size;
355  break;
356  case ECPGt_long_long:
358  *((long long int *) (ind + ind_offset * act_tuple)) = size;
359  break;
360  default:
361  break;
362  }
363  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
364  }
365  }
366  pval += size;
367  }
368  else
369  {
370  switch (type)
371  {
372  long res;
373  unsigned long ures;
374  double dres;
375  char *scan_length;
376  numeric *nres;
377  date ddres;
378  timestamp tres;
379  interval *ires;
380  char *endptr,
381  endchar;
382 
383  case ECPGt_short:
384  case ECPGt_int:
385  case ECPGt_long:
386  res = strtol(pval, &scan_length, 10);
387  if (garbage_left(isarray, &scan_length, compat))
388  {
389  ecpg_raise(lineno, ECPG_INT_FORMAT,
391  return false;
392  }
393  pval = scan_length;
394 
395  switch (type)
396  {
397  case ECPGt_short:
398  *((short *) (var + offset * act_tuple)) = (short) res;
399  break;
400  case ECPGt_int:
401  *((int *) (var + offset * act_tuple)) = (int) res;
402  break;
403  case ECPGt_long:
404  *((long *) (var + offset * act_tuple)) = (long) res;
405  break;
406  default:
407  /* Cannot happen */
408  break;
409  }
410  break;
411 
413  case ECPGt_unsigned_int:
414  case ECPGt_unsigned_long:
415  ures = strtoul(pval, &scan_length, 10);
416  if (garbage_left(isarray, &scan_length, compat))
417  {
420  return false;
421  }
422  pval = scan_length;
423 
424  switch (type)
425  {
427  *((unsigned short *) (var + offset * act_tuple)) = (unsigned short) ures;
428  break;
429  case ECPGt_unsigned_int:
430  *((unsigned int *) (var + offset * act_tuple)) = (unsigned int) ures;
431  break;
432  case ECPGt_unsigned_long:
433  *((unsigned long *) (var + offset * act_tuple)) = (unsigned long) ures;
434  break;
435  default:
436  /* Cannot happen */
437  break;
438  }
439  break;
440 
441 #ifdef HAVE_STRTOLL
442  case ECPGt_long_long:
443  *((long long int *) (var + offset * act_tuple)) = strtoll(pval, &scan_length, 10);
444  if (garbage_left(isarray, &scan_length, compat))
445  {
447  return false;
448  }
449  pval = scan_length;
450 
451  break;
452 #endif /* HAVE_STRTOLL */
453 #ifdef HAVE_STRTOULL
455  *((unsigned long long int *) (var + offset * act_tuple)) = strtoull(pval, &scan_length, 10);
456  if (garbage_left(isarray, &scan_length, compat))
457  {
459  return false;
460  }
461  pval = scan_length;
462 
463  break;
464 #endif /* HAVE_STRTOULL */
465 
466  case ECPGt_float:
467  case ECPGt_double:
468  if (isarray && *pval == '"')
469  pval++;
470 
471  if (!check_special_value(pval, &dres, &scan_length))
472  dres = strtod(pval, &scan_length);
473 
474  if (isarray && *scan_length == '"')
475  scan_length++;
476 
477  /* no special INFORMIX treatment for floats */
478  if (garbage_left(isarray, &scan_length, ECPG_COMPAT_PGSQL))
479  {
482  return false;
483  }
484  pval = scan_length;
485 
486  switch (type)
487  {
488  case ECPGt_float:
489  *((float *) (var + offset * act_tuple)) = dres;
490  break;
491  case ECPGt_double:
492  *((double *) (var + offset * act_tuple)) = dres;
493  break;
494  default:
495  /* Cannot happen */
496  break;
497  }
498  break;
499 
500  case ECPGt_bool:
501  if (pval[0] == 'f' && pval[1] == '\0')
502  {
503  *((bool *) (var + offset * act_tuple)) = false;
504  pval++;
505  break;
506  }
507  else if (pval[0] == 't' && pval[1] == '\0')
508  {
509  *((bool *) (var + offset * act_tuple)) = true;
510  pval++;
511  break;
512  }
513  else if (pval[0] == '\0' && PQgetisnull(results, act_tuple, act_field))
514  {
515  /* NULL is valid */
516  break;
517  }
518 
521  return false;
522  break;
523 
524  case ECPGt_bytea:
525  {
526  struct ECPGgeneric_bytea *variable =
527  (struct ECPGgeneric_bytea *) (var + offset * act_tuple);
528  long dst_size,
529  src_size,
530  dec_size;
531 
532  dst_size = ecpg_hex_enc_len(varcharsize);
533  src_size = size - 2; /* exclude backslash + 'x' */
534  dec_size = src_size < dst_size ? src_size : dst_size;
535  variable->len = hex_decode(pval + 2, dec_size, variable->arr);
536 
537  if (dst_size < src_size)
538  {
539  long rcv_size = ecpg_hex_dec_len(size - 2);
540 
541  /* truncation */
542  switch (ind_type)
543  {
544  case ECPGt_short:
546  *((short *) (ind + ind_offset * act_tuple)) = rcv_size;
547  break;
548  case ECPGt_int:
549  case ECPGt_unsigned_int:
550  *((int *) (ind + ind_offset * act_tuple)) = rcv_size;
551  break;
552  case ECPGt_long:
553  case ECPGt_unsigned_long:
554  *((long *) (ind + ind_offset * act_tuple)) = rcv_size;
555  break;
556  case ECPGt_long_long:
558  *((long long int *) (ind + ind_offset * act_tuple)) = rcv_size;
559  break;
560  default:
561  break;
562  }
563  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
564  }
565 
566  pval += size;
567  }
568  break;
569 
570  case ECPGt_char:
571  case ECPGt_unsigned_char:
572  case ECPGt_string:
573  {
574  char *str = (char *) (var + offset * act_tuple);
575 
576  /*
577  * If varcharsize is unknown and the offset is that of
578  * char *, then this variable represents the array of
579  * character pointers. So, use extra indirection.
580  */
581  if (varcharsize == 0 && offset == sizeof(char *))
582  str = *(char **) str;
583 
584  if (varcharsize == 0 || varcharsize > size)
585  {
586  /*
587  * compatibility mode, blank pad and null
588  * terminate char array
589  */
591  {
592  memset(str, ' ', varcharsize);
593  memcpy(str, pval, size);
594  str[varcharsize - 1] = '\0';
595 
596  /*
597  * compatibility mode empty string gets -1
598  * indicator but no warning
599  */
600  if (size == 0)
601  {
602  /* truncation */
603  switch (ind_type)
604  {
605  case ECPGt_short:
607  *((short *) (ind + ind_offset * act_tuple)) = -1;
608  break;
609  case ECPGt_int:
610  case ECPGt_unsigned_int:
611  *((int *) (ind + ind_offset * act_tuple)) = -1;
612  break;
613  case ECPGt_long:
614  case ECPGt_unsigned_long:
615  *((long *) (ind + ind_offset * act_tuple)) = -1;
616  break;
617  case ECPGt_long_long:
619  *((long long int *) (ind + ind_offset * act_tuple)) = -1;
620  break;
621  default:
622  break;
623  }
624  }
625  }
626  else
627  {
628  strncpy(str, pval, size + 1);
629  }
630  /* do the rtrim() */
631  if (type == ECPGt_string)
632  {
633  char *last = str + size;
634 
635  while (last > str && (*last == ' ' || *last == '\0'))
636  {
637  *last = '\0';
638  last--;
639  }
640  }
641  }
642  else
643  {
644  strncpy(str, pval, varcharsize);
645 
646  /* compatibility mode, null terminate char array */
647  if (ORACLE_MODE(compat) && (varcharsize - 1) < size)
648  {
650  str[varcharsize - 1] = '\0';
651  }
652 
653  if (varcharsize < size || (ORACLE_MODE(compat) && (varcharsize - 1) < size))
654  {
655  /* truncation */
656  switch (ind_type)
657  {
658  case ECPGt_short:
660  *((short *) (ind + ind_offset * act_tuple)) = size;
661  break;
662  case ECPGt_int:
663  case ECPGt_unsigned_int:
664  *((int *) (ind + ind_offset * act_tuple)) = size;
665  break;
666  case ECPGt_long:
667  case ECPGt_unsigned_long:
668  *((long *) (ind + ind_offset * act_tuple)) = size;
669  break;
670  case ECPGt_long_long:
672  *((long long int *) (ind + ind_offset * act_tuple)) = size;
673  break;
674  default:
675  break;
676  }
677  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
678  }
679  }
680  pval += size;
681  }
682  break;
683 
684  case ECPGt_varchar:
685  {
686  struct ECPGgeneric_varchar *variable =
687  (struct ECPGgeneric_varchar *) (var + offset * act_tuple);
688 
689  variable->len = size;
690  if (varcharsize == 0)
691  strncpy(variable->arr, pval, variable->len);
692  else
693  {
694  strncpy(variable->arr, pval, varcharsize);
695 
696  if (variable->len > varcharsize)
697  {
698  /* truncation */
699  switch (ind_type)
700  {
701  case ECPGt_short:
703  *((short *) (ind + ind_offset * act_tuple)) = variable->len;
704  break;
705  case ECPGt_int:
706  case ECPGt_unsigned_int:
707  *((int *) (ind + ind_offset * act_tuple)) = variable->len;
708  break;
709  case ECPGt_long:
710  case ECPGt_unsigned_long:
711  *((long *) (ind + ind_offset * act_tuple)) = variable->len;
712  break;
713  case ECPGt_long_long:
715  *((long long int *) (ind + ind_offset * act_tuple)) = variable->len;
716  break;
717  default:
718  break;
719  }
720  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
721 
722  variable->len = varcharsize;
723  }
724  }
725  pval += size;
726  }
727  break;
728 
729  case ECPGt_decimal:
730  case ECPGt_numeric:
731  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '}'; endptr++);
732  endchar = *endptr;
733  *endptr = '\0';
734  nres = PGTYPESnumeric_from_asc(pval, &scan_length);
735  *endptr = endchar;
736 
737  /* did we get an error? */
738  if (nres == NULL)
739  {
740  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
741  lineno, pval, errno);
742 
743  if (INFORMIX_MODE(compat))
744  {
745  /*
746  * Informix wants its own NULL value here instead
747  * of an error
748  */
749  nres = PGTYPESnumeric_new();
750  if (nres)
752  else
753  {
756  return false;
757  }
758  }
759  else
760  {
763  return false;
764  }
765  }
766  else
767  {
768  if (!isarray && garbage_left(isarray, &scan_length, compat))
769  {
770  free(nres);
773  return false;
774  }
775  }
776  pval = scan_length;
777 
778  if (type == ECPGt_numeric)
779  PGTYPESnumeric_copy(nres, (numeric *) (var + offset * act_tuple));
780  else
781  PGTYPESnumeric_to_decimal(nres, (decimal *) (var + offset * act_tuple));
782 
783  PGTYPESnumeric_free(nres);
784  break;
785 
786  case ECPGt_interval:
787  if (*pval == '"')
788  pval++;
789 
790  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
791  endchar = *endptr;
792  *endptr = '\0';
793  ires = PGTYPESinterval_from_asc(pval, &scan_length);
794  *endptr = endchar;
795 
796  /* did we get an error? */
797  if (ires == NULL)
798  {
799  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
800  lineno, pval, errno);
801 
802  if (INFORMIX_MODE(compat))
803  {
804  /*
805  * Informix wants its own NULL value here instead
806  * of an error
807  */
808  ires = (interval *) ecpg_alloc(sizeof(interval), lineno);
809  if (!ires)
810  return false;
811 
813  }
814  else
815  {
818  return false;
819  }
820  }
821  else
822  {
823  if (*scan_length == '"')
824  scan_length++;
825 
826  if (!isarray && garbage_left(isarray, &scan_length, compat))
827  {
828  free(ires);
831  return false;
832  }
833  }
834  pval = scan_length;
835 
836  PGTYPESinterval_copy(ires, (interval *) (var + offset * act_tuple));
837  free(ires);
838  break;
839 
840  case ECPGt_date:
841  if (*pval == '"')
842  pval++;
843 
844  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
845  endchar = *endptr;
846  *endptr = '\0';
847  ddres = PGTYPESdate_from_asc(pval, &scan_length);
848  *endptr = endchar;
849 
850  /* did we get an error? */
851  if (errno != 0)
852  {
853  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
854  lineno, pval, errno);
855 
856  if (INFORMIX_MODE(compat))
857  {
858  /*
859  * Informix wants its own NULL value here instead
860  * of an error
861  */
863  }
864  else
865  {
868  return false;
869  }
870  }
871  else
872  {
873  if (*scan_length == '"')
874  scan_length++;
875 
876  if (!isarray && garbage_left(isarray, &scan_length, compat))
877  {
880  return false;
881  }
882  }
883 
884  *((date *) (var + offset * act_tuple)) = ddres;
885  pval = scan_length;
886  break;
887 
888  case ECPGt_timestamp:
889  if (*pval == '"')
890  pval++;
891 
892  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
893  endchar = *endptr;
894  *endptr = '\0';
895  tres = PGTYPEStimestamp_from_asc(pval, &scan_length);
896  *endptr = endchar;
897 
898  /* did we get an error? */
899  if (errno != 0)
900  {
901  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
902  lineno, pval, errno);
903 
904  if (INFORMIX_MODE(compat))
905  {
906  /*
907  * Informix wants its own NULL value here instead
908  * of an error
909  */
911  }
912  else
913  {
916  return false;
917  }
918  }
919  else
920  {
921  if (*scan_length == '"')
922  scan_length++;
923 
924  if (!isarray && garbage_left(isarray, &scan_length, compat))
925  {
928  return false;
929  }
930  }
931 
932  *((timestamp *) (var + offset * act_tuple)) = tres;
933  pval = scan_length;
934  break;
935 
936  default:
940  return false;
941  break;
942  }
943  if (ECPG_IS_ARRAY(isarray))
944  {
945  bool string = false;
946 
947  /* set array to next entry */
948  ++act_tuple;
949 
950  /* set pval to the next entry */
951 
952  /*
953  * *pval != '\0' should not be needed, but is used as a safety
954  * guard
955  */
956  for (; *pval != '\0' && (string || (!array_delimiter(isarray, *pval) && !array_boundary(isarray, *pval))); ++pval)
957  if (*pval == '"')
958  string = string ? false : true;
959 
960  if (array_delimiter(isarray, *pval))
961  ++pval;
962  }
963  }
964  } while (*pval != '\0' && !array_boundary(isarray, *pval));
965 
966  return true;
967 }
unsigned ecpg_hex_enc_len(unsigned srclen)
Definition: data.c:128
static bool garbage_left(enum ARRAY_TYPE isarray, char **scan_length, enum COMPAT_MODE compat)
Definition: data.c:46
unsigned ecpg_hex_dec_len(unsigned srclen)
Definition: data.c:134
static unsigned hex_decode(const char *src, unsigned len, char *dst)
Definition: data.c:161
static bool array_delimiter(enum ARRAY_TYPE isarray, char c)
Definition: data.c:20
static bool array_boundary(enum ARRAY_TYPE isarray, char c)
Definition: data.c:33
static bool check_special_value(char *ptr, double *retval, char **endptr)
Definition: data.c:101
#define ECPG_CONVERT_BOOL
Definition: ecpgerrno.h:29
#define ECPG_UNSUPPORTED
Definition: ecpgerrno.h:18
#define ECPG_FLOAT_FORMAT
Definition: ecpgerrno.h:24
#define ECPG_INT_FORMAT
Definition: ecpgerrno.h:22
#define ECPG_MISSING_INDICATOR
Definition: ecpgerrno.h:31
#define ECPG_INTERVAL_FORMAT
Definition: ecpgerrno.h:26
#define ECPG_UINT_FORMAT
Definition: ecpgerrno.h:23
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
#define ECPG_DATA_NOT_ARRAY
Definition: ecpgerrno.h:33
#define ECPG_NUMERIC_FORMAT
Definition: ecpgerrno.h:25
#define ECPG_DATE_FORMAT
Definition: ecpgerrno.h:27
#define ECPG_TIMESTAMP_FORMAT
Definition: ecpgerrno.h:28
#define ECPG_SQLSTATE_NO_DATA
#define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER
#define ORACLE_MODE(X)
const char * ecpg_type_name(enum ECPGttype)
Definition: typename.c:17
#define ECPG_SQLSTATE_DATATYPE_MISMATCH
#define ECPG_IS_ARRAY(X)
bool ecpg_internal_regression_mode
Definition: misc.c:29
@ ECPGt_float
Definition: ecpgtype.h:47
@ ECPGt_long_long
Definition: ecpgtype.h:45
@ ECPGt_decimal
Definition: ecpgtype.h:51
@ ECPGt_numeric
Definition: ecpgtype.h:49
@ ECPGt_timestamp
Definition: ecpgtype.h:54
@ ECPGt_unsigned_short
Definition: ecpgtype.h:43
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_long
Definition: ecpgtype.h:44
@ ECPGt_double
Definition: ecpgtype.h:47
@ ECPGt_date
Definition: ecpgtype.h:53
@ ECPGt_interval
Definition: ecpgtype.h:55
@ ECPGt_unsigned_long
Definition: ecpgtype.h:44
@ ECPGt_bool
Definition: ecpgtype.h:46
@ ECPGt_unsigned_long_long
Definition: ecpgtype.h:45
@ ECPGt_unsigned_int
Definition: ecpgtype.h:44
@ ECPGt_string
Definition: ecpgtype.h:65
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3741
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:3562
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3730
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3755
void ECPGset_noind_null(enum ECPGttype type, void *ptr)
Definition: misc.c:318
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:141
return false
Definition: isn.c:131
date PGTYPESdate_from_asc(char *, char **)
Definition: datetime.c:47
long date
Definition: pgtypes_date.h:9
interval * PGTYPESinterval_from_asc(char *, char **)
Definition: interval.c:1003
int PGTYPESinterval_copy(interval *, interval *)
Definition: interval.c:1082
int PGTYPESnumeric_copy(numeric *, numeric *)
Definition: numeric.c:1389
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:42
int PGTYPESnumeric_to_decimal(numeric *, decimal *)
Definition: numeric.c:1548
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:385
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:321
int64 timestamp
timestamp PGTYPEStimestamp_from_asc(char *, char **)
Definition: timestamp.c:206
#define sqlca
Definition: sqlca.h:59
Definition: sqlca.h:20

References array_boundary(), array_delimiter(), check_special_value(), compat, ecpg_alloc(), ECPG_ARRAY_ARRAY, ECPG_COMPAT_PGSQL, ECPG_CONVERT_BOOL, ECPG_DATA_NOT_ARRAY, ECPG_DATE_FORMAT, ECPG_FLOAT_FORMAT, ecpg_hex_dec_len(), ecpg_hex_enc_len(), ECPG_INT_FORMAT, ecpg_internal_regression_mode, ECPG_INTERVAL_FORMAT, ECPG_IS_ARRAY, ecpg_log(), ECPG_MISSING_INDICATOR, ECPG_NOT_FOUND, ECPG_NUMERIC_FORMAT, ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_DATATYPE_MISMATCH, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_NO_DATA, ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER, ECPG_TIMESTAMP_FORMAT, ecpg_type_name(), ECPG_UINT_FORMAT, ECPG_UNSUPPORTED, ECPGget_sqlca(), ECPGset_noind_null(), ECPGt_bool, ECPGt_bytea, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, ECPGt_varchar, false, force_indicator, free, garbage_left(), hex_decode(), INFORMIX_MODE, ORACLE_MODE, PGTYPESdate_from_asc(), PGTYPESinterval_copy(), PGTYPESinterval_from_asc(), PGTYPESnumeric_copy(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PGTYPESnumeric_new(), PGTYPESnumeric_to_decimal(), PGTYPEStimestamp_from_asc(), PQfformat(), PQgetisnull(), PQgetlength(), PQgetvalue(), res, sqlca, generate_unaccent_rules::str, and generate_unaccent_rules::type.

Referenced by ecpg_set_compat_sqlda(), ecpg_set_native_sqlda(), and ecpg_store_result().

◆ ecpg_hex_dec_len()

unsigned ecpg_hex_dec_len ( unsigned  srclen)

Definition at line 134 of file data.c.

135 {
136  return srclen >> 1;
137 }

Referenced by ecpg_get_data().

◆ ecpg_hex_enc_len()

unsigned ecpg_hex_enc_len ( unsigned  srclen)

Definition at line 128 of file data.c.

129 {
130  return srclen << 1;
131 }

Referenced by convert_bytea_to_string(), ecpg_get_data(), and print_param_value().

◆ ecpg_hex_encode()

unsigned ecpg_hex_encode ( const char *  src,
unsigned  len,
char *  dst 
)

Definition at line 191 of file data.c.

192 {
193  static const char hextbl[] = "0123456789abcdef";
194  const char *end = src + len;
195 
196  while (src < end)
197  {
198  *dst++ = hextbl[(*src >> 4) & 0xF];
199  *dst++ = hextbl[*src & 0xF];
200  src++;
201  }
202  return len * 2;
203 }
static const char hextbl[]
Definition: pgp-info.c:87

References hextbl, and len.

Referenced by convert_bytea_to_string(), and print_param_value().

◆ ecpg_init()

bool ecpg_init ( const struct connection con,
const char *  connection_name,
const int  lineno 
)

Definition at line 104 of file misc.c.

105 {
106  struct sqlca_t *sqlca = ECPGget_sqlca();
107 
108  if (sqlca == NULL)
109  {
111  NULL);
112  return false;
113  }
114 
116  if (con == NULL)
117  {
119  connection_name ? connection_name : ecpg_gettext("NULL"));
120  return false;
121  }
122 
123  return true;
124 }
#define ECPG_NO_CONN
Definition: ecpgerrno.h:36
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
static struct sqlca_t sqlca
Definition: misc.c:62
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:98

References ecpg_gettext, ecpg_init_sqlca(), ECPG_NO_CONN, ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPGget_sqlca(), and sqlca.

Referenced by ecpg_do_prologue(), ECPGdeallocate(), ECPGdisconnect(), ECPGprepare(), ECPGsetcommit(), ECPGsetconn(), ECPGstatus(), and ECPGtrans().

◆ ecpg_init_sqlca()

void ecpg_init_sqlca ( struct sqlca_t sqlca)

Definition at line 98 of file misc.c.

99 {
100  memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
101 }
static struct sqlca_t sqlca_init
Definition: misc.c:31

References sqlca, and sqlca_init.

Referenced by ecpg_init(), ECPGallocate_desc(), ECPGconnect(), ECPGdeallocate_desc(), ECPGdisconnect(), ECPGget_desc(), ECPGget_desc_header(), ECPGget_sqlca(), and ECPGset_var().

◆ ecpg_log()

◆ ecpg_prepared()

char* ecpg_prepared ( const char *  name,
struct connection con 
)

Definition at line 357 of file prepare.c.

358 {
359  struct prepared_statement *this;
360 
361  this = ecpg_find_prepared_statement(name, con, NULL);
362  return this ? this->stmt->command : NULL;
363 }
struct statement * stmt

References statement::command, ecpg_find_prepared_statement(), name, and prepared_statement::stmt.

Referenced by ecpg_do_prologue(), and ECPGprepared_statement().

◆ ecpg_process_output()

bool ecpg_process_output ( struct statement stmt,
bool  clear_result 
)

Definition at line 1671 of file execute.c.

1672 {
1673  struct variable *var;
1674  bool status = false;
1675  char *cmdstat;
1676  PGnotify *notify;
1677  struct sqlca_t *sqlca = ECPGget_sqlca();
1678  int nfields,
1679  ntuples,
1680  act_field;
1681 
1682  if (sqlca == NULL)
1683  {
1686  return false;
1687  }
1688 
1689  var = stmt->outlist;
1690  switch (PQresultStatus(stmt->results))
1691  {
1692  case PGRES_TUPLES_OK:
1693  nfields = PQnfields(stmt->results);
1694  sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1695 
1696  ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1697  status = true;
1698 
1699  if (ntuples < 1)
1700  {
1701  if (ntuples)
1702  ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1703  stmt->lineno, ntuples);
1705  status = false;
1706  break;
1707  }
1708 
1709  if (var != NULL && var->type == ECPGt_descriptor)
1710  {
1711  struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1712 
1713  if (desc == NULL)
1714  status = false;
1715  else
1716  {
1717  PQclear(desc->result);
1718  desc->result = stmt->results;
1719  clear_result = false;
1720  ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1721  stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1722  }
1723  var = var->next;
1724  }
1725  else if (var != NULL && var->type == ECPGt_sqlda)
1726  {
1727  if (INFORMIX_MODE(stmt->compat))
1728  {
1729  struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1730  struct sqlda_compat *sqlda = *_sqlda;
1731  struct sqlda_compat *sqlda_new;
1732  int i;
1733 
1734  /*
1735  * If we are passed in a previously existing sqlda (chain)
1736  * then free it.
1737  */
1738  while (sqlda)
1739  {
1740  sqlda_new = sqlda->desc_next;
1741  free(sqlda);
1742  sqlda = sqlda_new;
1743  }
1744  *_sqlda = sqlda = sqlda_new = NULL;
1745  for (i = ntuples - 1; i >= 0; i--)
1746  {
1747  /*
1748  * Build a new sqlda structure. Note that only
1749  * fetching 1 record is supported
1750  */
1751  sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1752 
1753  if (!sqlda_new)
1754  {
1755  /* cleanup all SQLDAs we created up */
1756  while (sqlda)
1757  {
1758  sqlda_new = sqlda->desc_next;
1759  free(sqlda);
1760  sqlda = sqlda_new;
1761  }
1762  *_sqlda = NULL;
1763 
1764  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1765  status = false;
1766  break;
1767  }
1768  else
1769  {
1770  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1771 
1772  *_sqlda = sqlda_new;
1773 
1774  ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1775  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1776  stmt->lineno, PQnfields(stmt->results));
1777 
1778  sqlda_new->desc_next = sqlda;
1779  sqlda = sqlda_new;
1780  }
1781  }
1782  }
1783  else
1784  {
1785  struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1786  struct sqlda_struct *sqlda = *_sqlda;
1787  struct sqlda_struct *sqlda_new;
1788  int i;
1789 
1790  /*
1791  * If we are passed in a previously existing sqlda (chain)
1792  * then free it.
1793  */
1794  while (sqlda)
1795  {
1796  sqlda_new = sqlda->desc_next;
1797  free(sqlda);
1798  sqlda = sqlda_new;
1799  }
1800  *_sqlda = sqlda = sqlda_new = NULL;
1801  for (i = ntuples - 1; i >= 0; i--)
1802  {
1803  /*
1804  * Build a new sqlda structure. Note that only
1805  * fetching 1 record is supported
1806  */
1807  sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1808 
1809  if (!sqlda_new)
1810  {
1811  /* cleanup all SQLDAs we created up */
1812  while (sqlda)
1813  {
1814  sqlda_new = sqlda->desc_next;
1815  free(sqlda);
1816  sqlda = sqlda_new;
1817  }
1818  *_sqlda = NULL;
1819 
1820  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1821  status = false;
1822  break;
1823  }
1824  else
1825  {
1826  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1827 
1828  *_sqlda = sqlda_new;
1829 
1830  ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1831  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1832  stmt->lineno, PQnfields(stmt->results));
1833 
1834  sqlda_new->desc_next = sqlda;
1835  sqlda = sqlda_new;
1836  }
1837  }
1838  }
1839 
1840  var = var->next;
1841  }
1842  else
1843  for (act_field = 0; act_field < nfields && status; act_field++)
1844  {
1845  if (var != NULL)
1846  {
1847  status = ecpg_store_result(stmt->results, act_field, stmt, var);
1848  var = var->next;
1849  }
1850  else if (!INFORMIX_MODE(stmt->compat))
1851  {
1853  return false;
1854  }
1855  }
1856 
1857  if (status && var != NULL)
1858  {
1860  status = false;
1861  }
1862 
1863  break;
1864  case PGRES_COMMAND_OK:
1865  status = true;
1866  cmdstat = PQcmdStatus(stmt->results);
1867  sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1868  sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1869  ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1870  if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1871  !sqlca->sqlerrd[2] &&
1872  (strncmp(cmdstat, "UPDATE", 6) == 0
1873  || strncmp(cmdstat, "INSERT", 6) == 0
1874  || strncmp(cmdstat, "DELETE", 6) == 0))
1876  break;
1877  case PGRES_COPY_OUT:
1878  {
1879  char *buffer;
1880  int res;
1881 
1882  ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1883  while ((res = PQgetCopyData(stmt->connection->connection,
1884  &buffer, 0)) > 0)
1885  {
1886  printf("%s", buffer);
1887  PQfreemem(buffer);
1888  }
1889  if (res == -1)
1890  {
1891  /* COPY done */
1892  PQclear(stmt->results);
1893  stmt->results = PQgetResult(stmt->connection->connection);
1894  if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1895  ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1896  else
1897  ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1898  }
1899  break;
1900  }
1901  default:
1902 
1903  /*
1904  * execution should never reach this code because it is already
1905  * handled in ecpg_check_PQresult()
1906  */
1907  ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1908  stmt->lineno);
1909  ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1910  status = false;
1911  break;
1912  }
1913 
1914  if (clear_result)
1915  {
1916  PQclear(stmt->results);
1917  stmt->results = NULL;
1918  }
1919 
1920  /* check for asynchronous returns */
1922  while ((notify = PQnotifies(stmt->connection->connection)) != NULL)
1923  {
1924  ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1925  stmt->lineno, notify->relname, notify->be_pid);
1926  PQfreemem(notify);
1928  }
1929 
1930  return status;
1931 }
void ecpg_set_native_sqlda(int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:444
struct sqlda_compat * ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:205
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:412
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS
void ecpg_set_compat_sqlda(int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:255
void ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
Definition: error.c:219
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:303
void PQfreemem(void *ptr)
Definition: fe-exec.c:3886
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2594
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3676
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3335
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:2000
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3606
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3647
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2078
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2746
while(p+4<=pend)
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
#define printf(...)
Definition: port.h:231
PGresult * result
int be_pid
Definition: libpq-fe.h:190
char * relname
Definition: libpq-fe.h:189

References pgNotify::be_pid, statement::compat, statement::connection, connection::connection, ecpg_build_compat_sqlda(), ecpg_build_native_sqlda(), ECPG_COMPAT_INFORMIX_SE, ecpg_find_desc(), ecpg_log(), ECPG_NOT_FOUND, ECPG_OUT_OF_MEMORY, ecpg_raise(), ecpg_raise_backend(), ecpg_set_compat_sqlda(), ecpg_set_native_sqlda(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_NO_DATA, ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS, ecpg_store_result(), ECPG_TOO_FEW_ARGUMENTS, ECPG_TOO_MANY_ARGUMENTS, ECPGget_sqlca(), ECPGt_descriptor, ECPGt_sqlda, free, i, INFORMIX_MODE, statement::lineno, variable::next, statement::outlist, PGRES_COMMAND_OK, PGRES_COPY_OUT, PGRES_TUPLES_OK, variable::pointer, PQclear(), PQcmdStatus(), PQcmdTuples(), PQconsumeInput(), PQfreemem(), PQgetCopyData(), PQgetResult(), PQnfields(), PQnotifies(), PQntuples(), PQoidValue(), PQresultErrorMessage(), PQresultStatus(), printf, pgNotify::relname, res, descriptor::result, statement::results, sqlca, status(), variable::type, and while().

Referenced by ecpg_do().

◆ ecpg_raise()

void ecpg_raise ( int  line,
int  code,
const char *  sqlstate,
const char *  str 
)

Definition at line 13 of file error.c.

14 {
15  struct sqlca_t *sqlca = ECPGget_sqlca();
16 
17  if (sqlca == NULL)
18  {
19  ecpg_log("out of memory");
21  return;
22  }
23 
24  sqlca->sqlcode = code;
25  strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
26 
27  switch (code)
28  {
29  case ECPG_NOT_FOUND:
30  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
31  /*------
32  translator: this string will be truncated at 149 characters expanded. */
33  ecpg_gettext("no data found on line %d"), line);
34  break;
35 
36  case ECPG_OUT_OF_MEMORY:
37  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
38  /*------
39  translator: this string will be truncated at 149 characters expanded. */
40  ecpg_gettext("out of memory on line %d"), line);
41  break;
42 
43  case ECPG_UNSUPPORTED:
44  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
45  /*------
46  translator: this string will be truncated at 149 characters expanded. */
47  ecpg_gettext("unsupported type \"%s\" on line %d"), str, line);
48  break;
49 
51  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
52  /*------
53  translator: this string will be truncated at 149 characters expanded. */
54  ecpg_gettext("too many arguments on line %d"), line);
55  break;
56 
58  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
59  /*------
60  translator: this string will be truncated at 149 characters expanded. */
61  ecpg_gettext("too few arguments on line %d"), line);
62  break;
63 
64  case ECPG_INT_FORMAT:
65  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
66  /*------
67  translator: this string will be truncated at 149 characters expanded. */
68  ecpg_gettext("invalid input syntax for type int: \"%s\", on line %d"), str, line);
69  break;
70 
71  case ECPG_UINT_FORMAT:
72  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
73  /*------
74  translator: this string will be truncated at 149 characters expanded. */
75  ecpg_gettext("invalid input syntax for type unsigned int: \"%s\", on line %d"), str, line);
76  break;
77 
78  case ECPG_FLOAT_FORMAT:
79  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
80  /*------
81  translator: this string will be truncated at 149 characters expanded. */
82  ecpg_gettext("invalid input syntax for floating-point type: \"%s\", on line %d"), str, line);
83  break;
84 
85  case ECPG_CONVERT_BOOL:
86  if (str)
87  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
88  /*------
89  translator: this string will be truncated at 149 characters expanded. */
90  ecpg_gettext("invalid syntax for type boolean: \"%s\", on line %d"), str, line);
91  else
92  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
93  /*------
94  translator: this string will be truncated at 149 characters expanded. */
95  ecpg_gettext("could not convert boolean value: size mismatch, on line %d"), line);
96  break;
97 
98  case ECPG_EMPTY:
99  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
100  /*------
101  translator: this string will be truncated at 149 characters expanded. */
102  ecpg_gettext("empty query on line %d"), line);
103  break;
104 
106  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
107  /*------
108  translator: this string will be truncated at 149 characters expanded. */
109  ecpg_gettext("null value without indicator on line %d"), line);
110  break;
111 
112  case ECPG_NO_ARRAY:
113  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
114  /*------
115  translator: this string will be truncated at 149 characters expanded. */
116  ecpg_gettext("variable does not have an array type on line %d"), line);
117  break;
118 
119  case ECPG_DATA_NOT_ARRAY:
120  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
121  /*------
122  translator: this string will be truncated at 149 characters expanded. */
123  ecpg_gettext("data read from server is not an array on line %d"), line);
124  break;
125 
126  case ECPG_ARRAY_INSERT:
127  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
128  /*------
129  translator: this string will be truncated at 149 characters expanded. */
130  ecpg_gettext("inserting an array of variables is not supported on line %d"), line);
131  break;
132 
133  case ECPG_NO_CONN:
134  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
135  /*------
136  translator: this string will be truncated at 149 characters expanded. */
137  ecpg_gettext("connection \"%s\" does not exist on line %d"), str, line);
138  break;
139 
140  case ECPG_NOT_CONN:
141  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
142  /*------
143  translator: this string will be truncated at 149 characters expanded. */
144  ecpg_gettext("not connected to connection \"%s\" on line %d"), str, line);
145  break;
146 
147  case ECPG_INVALID_STMT:
148  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
149  /*------
150  translator: this string will be truncated at 149 characters expanded. */
151  ecpg_gettext("invalid statement name \"%s\" on line %d"), str, line);
152  break;
153 
155  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
156  /*------
157  translator: this string will be truncated at 149 characters expanded. */
158  ecpg_gettext("descriptor \"%s\" not found on line %d"), str, line);
159  break;
160 
162  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
163  /*------
164  translator: this string will be truncated at 149 characters expanded. */
165  ecpg_gettext("descriptor index out of range on line %d"), line);
166  break;
167 
169  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
170  /*------
171  translator: this string will be truncated at 149 characters expanded. */
172  ecpg_gettext("unrecognized descriptor item \"%s\" on line %d"), str, line);
173  break;
174 
176  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
177  /*------
178  translator: this string will be truncated at 149 characters expanded. */
179  ecpg_gettext("variable does not have a numeric type on line %d"), line);
180  break;
181 
182  case ECPG_VAR_NOT_CHAR:
183  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
184  /*------
185  translator: this string will be truncated at 149 characters expanded. */
186  ecpg_gettext("variable does not have a character type on line %d"), line);
187  break;
188 
189  case ECPG_TRANS:
190  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
191  /*------
192  translator: this string will be truncated at 149 characters expanded. */
193  ecpg_gettext("error in transaction processing on line %d"), line);
194  break;
195 
196  case ECPG_CONNECT:
197  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
198  /*------
199  translator: this string will be truncated at 149 characters expanded. */
200  ecpg_gettext("could not connect to database \"%s\" on line %d"), str, line);
201  break;
202 
203  default:
204  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
205  /*------
206  translator: this string will be truncated at 149 characters expanded. */
207  ecpg_gettext("SQL error %d on line %d"), code, line);
208  break;
209  }
210 
211  sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
212  ecpg_log("raising sqlcode %d on line %d: %s\n", code, line, sqlca->sqlerrm.sqlerrmc);
213 
214  /* free all memory we have allocated for the user */
216 }
#define ECPG_ARRAY_INSERT
Definition: ecpgerrno.h:34
#define ECPG_UNKNOWN_DESCRIPTOR_ITEM
Definition: ecpgerrno.h:44
#define ECPG_INVALID_DESCRIPTOR_INDEX
Definition: ecpgerrno.h:43
#define ECPG_VAR_NOT_NUMERIC
Definition: ecpgerrno.h:45
#define ECPG_VAR_NOT_CHAR
Definition: ecpgerrno.h:46
#define ECPG_TRANS
Definition: ecpgerrno.h:50
#define ECPG_NO_ARRAY
Definition: ecpgerrno.h:32
#define ECPG_CONNECT
Definition: ecpgerrno.h:51
void ECPGfree_auto_mem(void)
Definition: memory.c:138
char sqlstate[5]
Definition: sqlca.h:53

References ECPG_ARRAY_INSERT, ECPG_CONNECT, ECPG_CONVERT_BOOL, ECPG_DATA_NOT_ARRAY, ECPG_EMPTY, ECPG_FLOAT_FORMAT, ecpg_gettext, ECPG_INT_FORMAT, ECPG_INVALID_DESCRIPTOR_INDEX, ECPG_INVALID_STMT, ecpg_log(), ECPG_MISSING_INDICATOR, ECPG_NO_ARRAY, ECPG_NO_CONN, ECPG_NOT_CONN, ECPG_NOT_FOUND, ECPG_OUT_OF_MEMORY, ECPG_TOO_FEW_ARGUMENTS, ECPG_TOO_MANY_ARGUMENTS, ECPG_TRANS, ECPG_UINT_FORMAT, ECPG_UNKNOWN_DESCRIPTOR, ECPG_UNKNOWN_DESCRIPTOR_ITEM, ECPG_UNSUPPORTED, ECPG_VAR_NOT_CHAR, ECPG_VAR_NOT_NUMERIC, ECPGfree_auto_mem(), ECPGget_sqlca(), snprintf, sqlca, sqlca_t::sqlstate, and generate_unaccent_rules::str.

Referenced by deallocate_one(), ecpg_alloc(), ecpg_build_params(), ecpg_check_PQresult(), ecpg_do_prologue(), ecpg_find_desc(), ecpg_get_data(), ecpg_init(), ecpg_process_output(), ecpg_realloc(), ecpg_store_input(), ecpg_store_result(), ecpg_strdup(), ECPGallocate_desc(), ECPGconnect(), ECPGdeallocate(), ECPGdeallocate_desc(), ECPGdescribe(), ECPGdisconnect(), ECPGget_desc(), ECPGget_desc_header(), ECPGset_desc(), ECPGset_var(), ECPGstatus(), get_char_item(), get_int_item(), and set_int_item().

◆ ecpg_raise_backend()

void ecpg_raise_backend ( int  line,
PGresult result,
PGconn conn,
int  compat 
)

Definition at line 219 of file error.c.

220 {
221  struct sqlca_t *sqlca = ECPGget_sqlca();
222  char *sqlstate;
223  char *message;
224 
225  if (sqlca == NULL)
226  {
227  ecpg_log("out of memory");
229  return;
230  }
231 
232  /*
233  * PQresultErrorField will return NULL if "result" is NULL, or if there is
234  * no such field, which will happen for libpq-generated errors. Fall back
235  * to PQerrorMessage in such cases.
236  */
238  if (sqlstate == NULL)
240  message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
241  if (message == NULL)
242  message = PQerrorMessage(conn);
243 
245  {
246  /*
247  * we might get here if the connection breaks down, so let's check for
248  * this instead of giving just the generic internal error
249  */
250  if (PQstatus(conn) == CONNECTION_BAD)
251  {
252  sqlstate = "57P02";
253  message = ecpg_gettext("the connection to the server was lost");
254  }
255  }
256 
257  /* copy error message */
258  snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "%s on line %d", message, line);
259  sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
260 
261  /* copy SQLSTATE */
262  strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
263 
264  /* assign SQLCODE for backward compatibility */
265  if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0)
267  else if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0)
269  else
270  sqlca->sqlcode = ECPG_PGSQL;
271 
272  ecpg_log("raising sqlstate %.*s (sqlcode %ld): %s\n",
273  (int) sizeof(sqlca->sqlstate), sqlca->sqlstate, sqlca->sqlcode, sqlca->sqlerrm.sqlerrmc);
274 
275  /* free all memory we have allocated for the user */
277 }
#define ECPG_PGSQL
Definition: ecpgerrno.h:49
#define ECPG_SUBSELECT_NOT_ONE
Definition: ecpgerrno.h:53
#define ECPG_INFORMIX_SUBSELECT_NOT_ONE
Definition: ecpgerrno.h:58
#define ECPG_DUPLICATE_KEY
Definition: ecpgerrno.h:52
#define ECPG_INFORMIX_DUPLICATE_KEY
Definition: ecpgerrno.h:57
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6782
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3320
@ CONNECTION_BAD
Definition: libpq-fe.h:61
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:58
PGconn * conn
Definition: streamutil.c:54

References compat, conn, CONNECTION_BAD, ECPG_DUPLICATE_KEY, ecpg_gettext, ECPG_INFORMIX_DUPLICATE_KEY, ECPG_INFORMIX_SUBSELECT_NOT_ONE, ecpg_log(), ECPG_PGSQL, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ECPG_SUBSELECT_NOT_ONE, ECPGfree_auto_mem(), ECPGget_sqlca(), INFORMIX_MODE, PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SQLSTATE, PQerrorMessage(), PQresultErrorField(), PQstatus(), snprintf, sqlca, and sqlca_t::sqlstate.

Referenced by ecpg_check_PQresult(), and ecpg_process_output().

◆ ecpg_realloc()

char* ecpg_realloc ( void *  ptr,
long  size,
int  lineno 
)

Definition at line 33 of file memory.c.

34 {
35  char *new = (char *) realloc(ptr, size);
36 
37  if (!new)
38  {
40  return NULL;
41  }
42 
43  return new;
44 }
#define realloc(a, b)
Definition: header.h:60

References ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, and realloc.

Referenced by ecpg_build_params(), and ecpg_store_input().

◆ ecpg_register_prepared_stmt()

bool ecpg_register_prepared_stmt ( struct statement stmt)

Definition at line 59 of file prepare.c.

60 {
61  struct statement *prep_stmt;
62  struct prepared_statement *this;
63  struct connection *con = stmt->connection;
64  struct prepared_statement *prev = NULL;
65  int lineno = stmt->lineno;
66 
67  /* check if we already have prepared this statement */
68  this = ecpg_find_prepared_statement(stmt->name, con, &prev);
69  if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
70  return false;
71 
72  /* allocate new statement */
73  this = (struct prepared_statement *) ecpg_alloc(sizeof(struct prepared_statement), lineno);
74  if (!this)
75  return false;
76 
77  prep_stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
78  if (!prep_stmt)
79  {
80  ecpg_free(this);
81  return false;
82  }
83  memset(prep_stmt, 0, sizeof(struct statement));
84 
85  /* create statement */
86  prep_stmt->lineno = lineno;
87  prep_stmt->connection = con;
88  prep_stmt->command = ecpg_strdup(stmt->command, lineno);
89  prep_stmt->inlist = prep_stmt->outlist = NULL;
90  this->name = ecpg_strdup(stmt->name, lineno);
91  this->stmt = prep_stmt;
92  this->prepared = true;
93 
94  if (con->prep_stmts == NULL)
95  this->next = NULL;
96  else
97  this->next = con->prep_stmts;
98 
99  con->prep_stmts = this;
100  return true;
101 }
static int32 next
Definition: blutils.c:219

References statement::command, statement::connection, deallocate_one(), ecpg_alloc(), ECPG_COMPAT_PGSQL, ecpg_find_prepared_statement(), ecpg_free(), ecpg_strdup(), statement::inlist, statement::lineno, name, statement::name, next, statement::outlist, connection::prep_stmts, and prepared_statement::stmt.

Referenced by ecpg_execute().

◆ ecpg_set_compat_sqlda()

void ecpg_set_compat_sqlda ( int  lineno,
struct sqlda_compat **  _sqlda,
const PGresult res,
int  row,
enum  COMPAT_MODE 
)

Definition at line 255 of file sqlda.c.

256 {
257  struct sqlda_compat *sqlda = (*_sqlda);
258  int i;
259  long offset,
260  next_offset;
261 
262  if (row < 0)
263  return;
264 
265  /* Offset for the first field value */
266  offset = sqlda_compat_empty_size(res);
267 
268  /*
269  * Set sqlvar[i]->sqldata pointers and convert values to correct format
270  */
271  for (i = 0; i < sqlda->sqld; i++)
272  {
273  int isnull;
274  int datalen;
275  bool set_data = true;
276 
277  switch (sqlda->sqlvar[i].sqltype)
278  {
279  case ECPGt_short:
281  ecpg_sqlda_align_add_size(offset, sizeof(short), sizeof(short), &offset, &next_offset);
282  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
283  sqlda->sqlvar[i].sqllen = sizeof(short);
284  break;
285  case ECPGt_int:
286  case ECPGt_unsigned_int:
287  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(int), &offset, &next_offset);
288  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
289  sqlda->sqlvar[i].sqllen = sizeof(int);
290  break;
291  case ECPGt_long:
292  case ECPGt_unsigned_long:
293  ecpg_sqlda_align_add_size(offset, sizeof(long), sizeof(long), &offset, &next_offset);
294  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
295  sqlda->sqlvar[i].sqllen = sizeof(long);
296  break;
297  case ECPGt_long_long:
299  ecpg_sqlda_align_add_size(offset, sizeof(long long), sizeof(long long), &offset, &next_offset);
300  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
301  sqlda->sqlvar[i].sqllen = sizeof(long long);
302  break;
303  case ECPGt_bool:
304  ecpg_sqlda_align_add_size(offset, sizeof(bool), sizeof(bool), &offset, &next_offset);
305  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
306  sqlda->sqlvar[i].sqllen = sizeof(bool);
307  break;
308  case ECPGt_float:
309  ecpg_sqlda_align_add_size(offset, sizeof(float), sizeof(float), &offset, &next_offset);
310  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
311  sqlda->sqlvar[i].sqllen = sizeof(float);
312  break;
313  case ECPGt_double:
314  ecpg_sqlda_align_add_size(offset, sizeof(double), sizeof(double), &offset, &next_offset);
315  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
316  sqlda->sqlvar[i].sqllen = sizeof(double);
317  break;
318  case ECPGt_decimal:
319  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(decimal), &offset, &next_offset);
320  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
321  sqlda->sqlvar[i].sqllen = sizeof(decimal);
322  break;
323  case ECPGt_numeric:
324  {
325  numeric *num;
326  char *val;
327 
328  set_data = false;
329 
330  ecpg_sqlda_align_add_size(offset, sizeof(NumericDigit *), sizeof(numeric), &offset, &next_offset);
331  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
332  sqlda->sqlvar[i].sqllen = sizeof(numeric);
333 
334  if (PQgetisnull(res, row, i))
335  {
337  break;
338  }
339 
340  val = PQgetvalue(res, row, i);
341  num = PGTYPESnumeric_from_asc(val, NULL);
342  if (!num)
343  {
345  break;
346  }
347 
348  memcpy(sqlda->sqlvar[i].sqldata, num, sizeof(numeric));
349 
350  if (num->buf)
351  {
352  ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->digits - num->buf + num->ndigits, &offset, &next_offset);
353  memcpy((char *) sqlda + offset, num->buf, num->digits - num->buf + num->ndigits);
354 
355  ((numeric *) sqlda->sqlvar[i].sqldata)->buf = (NumericDigit *) sqlda + offset;
356  ((numeric *) sqlda->sqlvar[i].sqldata)->digits = (NumericDigit *) sqlda + offset + (num->digits - num->buf);
357  }
358 
359  PGTYPESnumeric_free(num);
360 
361  break;
362  }
363  case ECPGt_date:
364  ecpg_sqlda_align_add_size(offset, sizeof(date), sizeof(date), &offset, &next_offset);
365  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
366  sqlda->sqlvar[i].sqllen = sizeof(date);
367  break;
368  case ECPGt_timestamp:
369  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(timestamp), &offset, &next_offset);
370  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
371  sqlda->sqlvar[i].sqllen = sizeof(timestamp);
372  break;
373  case ECPGt_interval:
374  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(interval), &offset, &next_offset);
375  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
376  sqlda->sqlvar[i].sqllen = sizeof(interval);
377  break;
378  case ECPGt_char:
379  case ECPGt_unsigned_char:
380  case ECPGt_string:
381  default:
382  datalen = strlen(PQgetvalue(res, row, i)) + 1;
383  ecpg_sqlda_align_add_size(offset, sizeof(int), datalen, &offset, &next_offset);
384  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
385  sqlda->sqlvar[i].sqllen = datalen;
386  if (datalen > 32768)
387  sqlda->sqlvar[i].sqlilongdata = sqlda->sqlvar[i].sqldata;
388  break;
389  }
390 
391  isnull = PQgetisnull(res, row, i);
392  ecpg_log("ecpg_set_compat_sqlda on line %d row %d col %d %s\n", lineno, row, i, isnull ? "IS NULL" : "IS NOT NULL");
393  sqlda->sqlvar[i].sqlind = isnull ? &value_is_null : &value_is_not_null;
394  sqlda->sqlvar[i].sqlitype = ECPGt_short;
395  sqlda->sqlvar[i].sqlilen = sizeof(short);
396  if (!isnull)
397  {
398  if (set_data)
399  ecpg_get_data(res, row, i, lineno,
401  sqlda->sqlvar[i].sqldata, NULL, 0, 0, 0,
402  ECPG_ARRAY_NONE, compat, false);
403  }
404  else
405  ECPGset_noind_null(sqlda->sqlvar[i].sqltype, sqlda->sqlvar[i].sqldata);
406 
407  offset = next_offset;
408  }
409 }
int16 NumericDigit
Definition: numeric.c:102
Datum numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1165
unsigned char bool
Definition: c.h:402
bool ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno, enum ECPGttype type, enum ECPGttype ind_type, char *var, char *ind, long varcharsize, long offset, long ind_offset, enum ARRAY_TYPE isarray, enum COMPAT_MODE compat, bool force_indicator)
Definition: data.c:206
long val
Definition: informix.c:664
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33
static long sqlda_compat_empty_size(const PGresult *res)
Definition: sqlda.c:45
static int16 value_is_null
Definition: sqlda.c:251
static int16 value_is_not_null
Definition: sqlda.c:252
NumericDigit * digits
NumericDigit * buf
char * sqlilongdata
Definition: sqlda-compat.h:32

References numeric::buf, compat, numeric::digits, ECPG_ARRAY_NONE, ecpg_get_data(), ecpg_log(), ecpg_sqlda_align_add_size(), ECPGset_noind_null(), ECPGt_bool, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, i, numeric::ndigits, numeric(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PQgetisnull(), PQgetvalue(), res, sqlda_compat::sqld, sqlda_compat_empty_size(), sqlvar_compat::sqldata, sqlvar_compat::sqlilen, sqlvar_compat::sqlilongdata, sqlvar_compat::sqlind, sqlvar_compat::sqlitype, sqlvar_compat::sqllen, sqlvar_compat::sqltype, sqlda_compat::sqlvar, val, value_is_not_null, and value_is_null.

Referenced by ecpg_process_output().

◆ ecpg_set_native_sqlda()

void ecpg_set_native_sqlda ( int  lineno,
struct sqlda_struct **  _sqlda,
const PGresult res,
int  row,
enum  COMPAT_MODE 
)

Definition at line 444 of file sqlda.c.

445 {
446  struct sqlda_struct *sqlda = (*_sqlda);
447  int i;
448  long offset,
449  next_offset;
450 
451  if (row < 0)
452  return;
453 
454  /* Offset for the first field value */
455  offset = sqlda_native_empty_size(res);
456 
457  /*
458  * Set sqlvar[i]->sqldata pointers and convert values to correct format
459  */
460  for (i = 0; i < sqlda->sqld; i++)
461  {
462  int isnull;
463  int datalen;
464  bool set_data = true;
465 
466  switch (sqlda->sqlvar[i].sqltype)
467  {
468  case ECPGt_short:
470  ecpg_sqlda_align_add_size(offset, sizeof(short), sizeof(short), &offset, &next_offset);
471  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
472  sqlda->sqlvar[i].sqllen = sizeof(short);
473  break;
474  case ECPGt_int:
475  case ECPGt_unsigned_int:
476  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(int), &offset, &next_offset);
477  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
478  sqlda->sqlvar[i].sqllen = sizeof(int);
479  break;
480  case ECPGt_long:
481  case ECPGt_unsigned_long:
482  ecpg_sqlda_align_add_size(offset, sizeof(long), sizeof(long), &offset, &next_offset);
483  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
484  sqlda->sqlvar[i].sqllen = sizeof(long);
485  break;
486  case ECPGt_long_long:
488  ecpg_sqlda_align_add_size(offset, sizeof(long long), sizeof(long long), &offset, &next_offset);
489  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
490  sqlda->sqlvar[i].sqllen = sizeof(long long);
491  break;
492  case ECPGt_bool:
493  ecpg_sqlda_align_add_size(offset, sizeof(bool), sizeof(bool), &offset, &next_offset);
494  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
495  sqlda->sqlvar[i].sqllen = sizeof(bool);
496  break;
497  case ECPGt_float:
498  ecpg_sqlda_align_add_size(offset, sizeof(float), sizeof(float), &offset, &next_offset);
499  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
500  sqlda->sqlvar[i].sqllen = sizeof(float);
501  break;
502  case ECPGt_double:
503  ecpg_sqlda_align_add_size(offset, sizeof(double), sizeof(double), &offset, &next_offset);
504  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
505  sqlda->sqlvar[i].sqllen = sizeof(double);
506  break;
507  case ECPGt_decimal:
508  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(decimal), &offset, &next_offset);
509  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
510  sqlda->sqlvar[i].sqllen = sizeof(decimal);
511  break;
512  case ECPGt_numeric:
513  {
514  numeric *num;
515  char *val;
516 
517  set_data = false;
518 
519  ecpg_sqlda_align_add_size(offset, sizeof(NumericDigit *), sizeof(numeric), &offset, &next_offset);
520  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
521  sqlda->sqlvar[i].sqllen = sizeof(numeric);
522 
523  if (PQgetisnull(res, row, i))
524  {
526  break;
527  }
528 
529  val = PQgetvalue(res, row, i);
530  num = PGTYPESnumeric_from_asc(val, NULL);
531  if (!num)
532  {
534  break;
535  }
536 
537  memcpy(sqlda->sqlvar[i].sqldata, num, sizeof(numeric));
538 
539  if (num->buf)
540  {
541  ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->digits - num->buf + num->ndigits, &offset, &next_offset);
542  memcpy((char *) sqlda + offset, num->buf, num->digits - num->buf + num->ndigits);
543 
544  ((numeric *) sqlda->sqlvar[i].sqldata)->buf = (NumericDigit *) sqlda + offset;
545  ((numeric *) sqlda->sqlvar[i].sqldata)->digits = (NumericDigit *) sqlda + offset + (num->digits - num->buf);
546  }
547 
548  PGTYPESnumeric_free(num);
549 
550  break;
551  }
552  case ECPGt_date:
553  ecpg_sqlda_align_add_size(offset, sizeof(date), sizeof(date), &offset, &next_offset);
554  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
555  sqlda->sqlvar[i].sqllen = sizeof(date);
556  break;
557  case ECPGt_timestamp:
558  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(timestamp), &offset, &next_offset);
559  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
560  sqlda->sqlvar[i].sqllen = sizeof(timestamp);
561  break;
562  case ECPGt_interval:
563  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(interval), &offset, &next_offset);
564  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
565  sqlda->sqlvar[i].sqllen = sizeof(interval);
566  break;
567  case ECPGt_char:
568  case ECPGt_unsigned_char:
569  case ECPGt_string:
570  default:
571  datalen = strlen(PQgetvalue(res, row, i)) + 1;
572  ecpg_sqlda_align_add_size(offset, sizeof(int), datalen, &offset, &next_offset);
573  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
574  sqlda->sqlvar[i].sqllen = datalen;
575  break;
576  }
577 
578  isnull = PQgetisnull(res, row, i);
579  ecpg_log("ecpg_set_native_sqlda on line %d row %d col %d %s\n", lineno, row, i, isnull ? "IS NULL" : "IS NOT NULL");
580  sqlda->sqlvar[i].sqlind = isnull ? &value_is_null : &value_is_not_null;
581  if (!isnull)
582  {
583  if (set_data)
584  ecpg_get_data(res, row, i, lineno,
586  sqlda->sqlvar[i].sqldata, NULL, 0, 0, 0,
587  ECPG_ARRAY_NONE, compat, false);
588  }
589 
590  offset = next_offset;
591  }
592 }
static long sqlda_native_empty_size(const PGresult *res)
Definition: sqlda.c:171

References numeric::buf, compat, numeric::digits, ECPG_ARRAY_NONE, ecpg_get_data(), ecpg_log(), ecpg_sqlda_align_add_size(), ECPGset_noind_null(), ECPGt_bool, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, i, numeric::ndigits, numeric(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PQgetisnull(), PQgetvalue(), res, sqlda_struct::sqld, sqlda_native_empty_size(), sqlvar_struct::sqldata, sqlvar_struct::sqlind, sqlvar_struct::sqllen, sqlvar_struct::sqltype, sqlda_struct::sqlvar, val, value_is_not_null, and value_is_null.

Referenced by ecpg_process_output().

◆ ecpg_store_input()

bool ecpg_store_input ( const int  lineno,
const bool  force_indicator,
const struct variable var,
char **  tobeinserted_p,
bool  quote 
)

Definition at line 506 of file execute.c.

508 {
509  char *mallocedval = NULL;
510  char *newcopy = NULL;
511 
512  /*
513  * arrays are not possible unless the column is an array, too FIXME: we do
514  * not know if the column is an array here array input to singleton column
515  * will result in a runtime error
516  */
517 
518  /*
519  * Some special treatment is needed for records since we want their
520  * contents to arrive in a comma-separated list on insert (I think).
521  */
522 
523  *tobeinserted_p = "";
524 
525  /* check for null value and set input buffer accordingly */
526  switch (var->ind_type)
527  {
528  case ECPGt_short:
530  if (*(short *) var->ind_value < 0)
531  *tobeinserted_p = NULL;
532  break;
533  case ECPGt_int:
534  case ECPGt_unsigned_int:
535  if (*(int *) var->ind_value < 0)
536  *tobeinserted_p = NULL;
537  break;
538  case ECPGt_long:
539  case ECPGt_unsigned_long:
540  if (*(long *) var->ind_value < 0L)
541  *tobeinserted_p = NULL;
542  break;
543  case ECPGt_long_long:
545  if (*(long long int *) var->ind_value < (long long) 0)
546  *tobeinserted_p = NULL;
547  break;
548  case ECPGt_NO_INDICATOR:
549  if (force_indicator == false)
550  {
551  if (ECPGis_noind_null(var->type, var->value))
552  *tobeinserted_p = NULL;
553  }
554  break;
555  default:
556  break;
557  }
558  if (*tobeinserted_p != NULL)
559  {
560  int asize = var->arrsize ? var->arrsize : 1;
561 
562  switch (var->type)
563  {
564  int element;
565 
566  case ECPGt_short:
567  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
568  return false;
569 
570  if (asize > 1)
571  {
572  strcpy(mallocedval, "{");
573 
574  for (element = 0; element < asize; element++)
575  sprintf(mallocedval + strlen(mallocedval), "%hd,", ((short *) var->value)[element]);
576 
577  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
578  }
579  else
580  sprintf(mallocedval, "%hd", *((short *) var->value));
581 
582  *tobeinserted_p = mallocedval;
583  break;
584 
585  case ECPGt_int:
586  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
587  return false;
588 
589  if (asize > 1)
590  {
591  strcpy(mallocedval, "{");
592 
593  for (element = 0; element < asize; element++)
594  sprintf(mallocedval + strlen(mallocedval), "%d,", ((int *) var->value)[element]);
595 
596  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
597  }
598  else
599  sprintf(mallocedval, "%d", *((int *) var->value));
600 
601  *tobeinserted_p = mallocedval;
602  break;
603 
605  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
606  return false;
607 
608  if (asize > 1)
609  {
610  strcpy(mallocedval, "{");
611 
612  for (element = 0; element < asize; element++)
613  sprintf(mallocedval + strlen(mallocedval), "%hu,", ((unsigned short *) var->value)[element]);
614 
615  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
616  }
617  else
618  sprintf(mallocedval, "%hu", *((unsigned short *) var->value));
619 
620  *tobeinserted_p = mallocedval;
621  break;
622 
623  case ECPGt_unsigned_int:
624  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
625  return false;
626 
627  if (asize > 1)
628  {
629  strcpy(mallocedval, "{");
630 
631  for (element = 0; element < asize; element++)
632  sprintf(mallocedval + strlen(mallocedval), "%u,", ((unsigned int *) var->value)[element]);
633 
634  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
635  }
636  else
637  sprintf(mallocedval, "%u", *((unsigned int *) var->value));
638 
639  *tobeinserted_p = mallocedval;
640  break;
641 
642  case ECPGt_long:
643  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
644  return false;
645 
646  if (asize > 1)
647  {
648  strcpy(mallocedval, "{");
649 
650  for (element = 0; element < asize; element++)
651  sprintf(mallocedval + strlen(mallocedval), "%ld,", ((long *) var->value)[element]);
652 
653  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
654  }
655  else
656  sprintf(mallocedval, "%ld", *((long *) var->value));
657 
658  *tobeinserted_p = mallocedval;
659  break;
660 
661  case ECPGt_unsigned_long:
662  if (!(mallocedval = ecpg_alloc(asize * 20, lineno)))
663  return false;
664 
665  if (asize > 1)
666  {
667  strcpy(mallocedval, "{");
668 
669  for (element = 0; element < asize; element++)
670  sprintf(mallocedval + strlen(mallocedval), "%lu,", ((unsigned long *) var->value)[element]);
671 
672  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
673  }
674  else
675  sprintf(mallocedval, "%lu", *((unsigned long *) var->value));
676 
677  *tobeinserted_p = mallocedval;
678  break;
679 
680  case ECPGt_long_long:
681  if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
682  return false;
683 
684  if (asize > 1)
685  {
686  strcpy(mallocedval, "{");
687 
688  for (element = 0; element < asize; element++)
689  sprintf(mallocedval + strlen(mallocedval), "%lld,", ((long long int *) var->value)[element]);
690 
691  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
692  }
693  else
694  sprintf(mallocedval, "%lld", *((long long int *) var->value));
695 
696  *tobeinserted_p = mallocedval;
697  break;
698 
700  if (!(mallocedval = ecpg_alloc(asize * 30, lineno)))
701  return false;
702 
703  if (asize > 1)
704  {
705  strcpy(mallocedval, "{");
706 
707  for (element = 0; element < asize; element++)
708  sprintf(mallocedval + strlen(mallocedval), "%llu,", ((unsigned long long int *) var->value)[element]);
709 
710  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
711  }
712  else
713  sprintf(mallocedval, "%llu", *((unsigned long long int *) var->value));
714 
715  *tobeinserted_p = mallocedval;
716  break;
717 
718  case ECPGt_float:
719  if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
720  return false;
721 
722  if (asize > 1)
723  {
724  strcpy(mallocedval, "{");
725 
726  for (element = 0; element < asize; element++)
727  sprintf_float_value(mallocedval + strlen(mallocedval), ((float *) var->value)[element], ",");
728 
729  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
730  }
731  else
732  sprintf_float_value(mallocedval, *((float *) var->value), "");
733 
734  *tobeinserted_p = mallocedval;
735  break;
736 
737  case ECPGt_double:
738  if (!(mallocedval = ecpg_alloc(asize * 25, lineno)))
739  return false;
740 
741  if (asize > 1)
742  {
743  strcpy(mallocedval, "{");
744 
745  for (element = 0; element < asize; element++)
746  sprintf_double_value(mallocedval + strlen(mallocedval), ((double *) var->value)[element], ",");
747 
748  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
749  }
750  else
751  sprintf_double_value(mallocedval, *((double *) var->value), "");
752 
753  *tobeinserted_p = mallocedval;
754  break;
755 
756  case ECPGt_bool:
757  if (!(mallocedval = ecpg_alloc(var->arrsize + sizeof("{}"), lineno)))
758  return false;
759 
760  if (var->arrsize > 1)
761  {
762  strcpy(mallocedval, "{");
763 
764  for (element = 0; element < asize; element++)
765  sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
766 
767  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
768  }
769  else
770  {
771  if (var->offset == sizeof(char))
772  sprintf(mallocedval, "%c", (*((char *) var->value)) ? 't' : 'f');
773  else if (var->offset == sizeof(int))
774  sprintf(mallocedval, "%c", (*((int *) var->value)) ? 't' : 'f');
775  else
777  }
778 
779  *tobeinserted_p = mallocedval;
780  break;
781 
782  case ECPGt_char:
783  case ECPGt_unsigned_char:
784  case ECPGt_string:
785  {
786  /* set slen to string length if type is char * */
787  int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : (unsigned int) var->varcharsize;
788 
789  if (!(newcopy = ecpg_alloc(slen + 1, lineno)))
790  return false;
791 
792  strncpy(newcopy, (char *) var->value, slen);
793  newcopy[slen] = '\0';
794 
795  mallocedval = quote_postgres(newcopy, quote, lineno);
796  if (!mallocedval)
797  {
798  ecpg_free(newcopy);
799  return false;
800  }
801 
802  *tobeinserted_p = mallocedval;
803  }
804  break;
805  case ECPGt_const:
806  case ECPGt_char_variable:
807  {
808  int slen = strlen((char *) var->value);
809 
810  if (!(mallocedval = ecpg_alloc(slen + 1, lineno)))
811  return false;
812 
813  strncpy(mallocedval, (char *) var->value, slen);
814  mallocedval[slen] = '\0';
815 
816  *tobeinserted_p = mallocedval;
817  }
818  break;
819 
820  case ECPGt_bytea:
821  {
822  struct ECPGgeneric_bytea *variable =
823  (struct ECPGgeneric_bytea *) (var->value);
824 
825  if (!(mallocedval = (char *) ecpg_alloc(variable->len, lineno)))
826  return false;
827 
828  memcpy(mallocedval, variable->arr, variable->len);
829  *tobeinserted_p = mallocedval;
830  }
831  break;
832 
833  case ECPGt_varchar:
834  {
835  struct ECPGgeneric_varchar *variable =
836  (struct ECPGgeneric_varchar *) (var->value);
837 
838  if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, lineno)))
839  return false;
840 
841  strncpy(newcopy, variable->arr, variable->len);
842  newcopy[variable->len] = '\0';
843 
844  mallocedval = quote_postgres(newcopy, quote, lineno);
845  if (!mallocedval)
846  {
847  ecpg_free(newcopy);
848  return false;
849  }
850 
851  *tobeinserted_p = mallocedval;
852  }
853  break;
854 
855  case ECPGt_decimal:
856  case ECPGt_numeric:
857  {
858  char *str = NULL;
859  int slen;
860  numeric *nval;
861 
862  if (var->arrsize > 1)
863  mallocedval = ecpg_strdup("{", lineno);
864  else
865  mallocedval = ecpg_strdup("", lineno);
866 
867  if (!mallocedval)
868  return false;
869 
870  for (element = 0; element < asize; element++)
871  {
872  int result;
873 
874  nval = PGTYPESnumeric_new();
875  if (!nval)
876  {
877  ecpg_free(mallocedval);
878  return false;
879  }
880 
881  if (var->type == ECPGt_numeric)
882  result = PGTYPESnumeric_copy(&(((numeric *) (var->value))[element]), nval);
883  else
884  result = PGTYPESnumeric_from_decimal(&(((decimal *) (var->value))[element]), nval);
885 
886  if (result != 0)
887  {
888  PGTYPESnumeric_free(nval);
889  ecpg_free(mallocedval);
890  return false;
891  }
892 
893  str = PGTYPESnumeric_to_asc(nval, nval->dscale);
894  slen = strlen(str);
895  PGTYPESnumeric_free(nval);
896 
897  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
898  {
899  ecpg_free(mallocedval);
900  ecpg_free(str);
901  return false;
902  }
903  mallocedval = newcopy;
904 
905  /* also copy trailing '\0' */
906  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
907  if (var->arrsize > 1)
908  strcpy(mallocedval + strlen(mallocedval), ",");
909 
910  ecpg_free(str);
911  }
912 
913  if (var->arrsize > 1)
914  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
915 
916  *tobeinserted_p = mallocedval;
917  }
918  break;
919 
920  case ECPGt_interval:
921  {
922  char *str = NULL;
923  int slen;
924 
925  if (var->arrsize > 1)
926  mallocedval = ecpg_strdup("{", lineno);
927  else
928  mallocedval = ecpg_strdup("", lineno);
929 
930  if (!mallocedval)
931  return false;
932 
933  for (element = 0; element < asize; element++)
934  {
935  str = quote_postgres(PGTYPESinterval_to_asc(&(((interval *) (var->value))[element])), quote, lineno);
936  if (!str)
937  {
938  ecpg_free(mallocedval);
939  return false;
940  }
941 
942  slen = strlen(str);
943 
944  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
945  {
946  ecpg_free(mallocedval);
947  ecpg_free(str);
948  return false;
949  }
950  mallocedval = newcopy;
951 
952  /* also copy trailing '\0' */
953  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
954  if (var->arrsize > 1)
955  strcpy(mallocedval + strlen(mallocedval), ",");
956 
957  ecpg_free(str);
958  }
959 
960  if (var->arrsize > 1)
961  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
962 
963  *tobeinserted_p = mallocedval;
964  }
965  break;
966 
967  case ECPGt_date:
968  {
969  char *str = NULL;
970  int slen;
971 
972  if (var->arrsize > 1)
973  mallocedval = ecpg_strdup("{", lineno);
974  else
975  mallocedval = ecpg_strdup("", lineno);
976 
977  if (!mallocedval)
978  return false;
979 
980  for (element = 0; element < asize; element++)
981  {
982  str = quote_postgres(PGTYPESdate_to_asc(((date *) (var->value))[element]), quote, lineno);
983  if (!str)
984  {
985  ecpg_free(mallocedval);
986  return false;
987  }
988 
989  slen = strlen(str);
990 
991  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
992  {
993  ecpg_free(mallocedval);
994  ecpg_free(str);
995  return false;
996  }
997  mallocedval = newcopy;
998 
999  /* also copy trailing '\0' */
1000  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1001  if (var->arrsize > 1)
1002  strcpy(mallocedval + strlen(mallocedval), ",");
1003 
1004  ecpg_free(str);
1005  }
1006 
1007  if (var->arrsize > 1)
1008  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1009 
1010  *tobeinserted_p = mallocedval;
1011  }
1012  break;
1013 
1014  case ECPGt_timestamp:
1015  {
1016  char *str = NULL;
1017  int slen;
1018 
1019  if (var->arrsize > 1)
1020  mallocedval = ecpg_strdup("{", lineno);
1021  else
1022  mallocedval = ecpg_strdup("", lineno);
1023 
1024  if (!mallocedval)
1025  return false;
1026 
1027  for (element = 0; element < asize; element++)
1028  {
1029  str = quote_postgres(PGTYPEStimestamp_to_asc(((timestamp *) (var->value))[element]), quote, lineno);
1030  if (!str)
1031  {
1032  ecpg_free(mallocedval);
1033  return false;
1034  }
1035 
1036  slen = strlen(str);
1037 
1038  if (!(newcopy = ecpg_realloc(mallocedval, strlen(mallocedval) + slen + 2, lineno)))
1039  {
1040  ecpg_free(mallocedval);
1041  ecpg_free(str);
1042  return false;
1043  }
1044  mallocedval = newcopy;
1045 
1046  /* also copy trailing '\0' */
1047  memcpy(mallocedval + strlen(mallocedval), str, slen + 1);
1048  if (var->arrsize > 1)
1049  strcpy(mallocedval + strlen(mallocedval), ",");
1050 
1051  ecpg_free(str);
1052  }
1053 
1054  if (var->arrsize > 1)
1055  strcpy(mallocedval + strlen(mallocedval) - 1, "}");
1056 
1057  *tobeinserted_p = mallocedval;
1058  }
1059  break;
1060 
1061  case ECPGt_descriptor:
1062  case ECPGt_sqlda:
1063  break;
1064 
1065  default:
1066  /* Not implemented yet */
1068  return false;
1069  break;
1070  }
1071  }
1072  return true;
1073 }
@ ECPGt_const
Definition: ecpgtype.h:61
static void sprintf_double_value(char *ptr, double value, const char *delim)
Definition: execute.c:456
static char * quote_postgres(char *arg, bool quote, int lineno)
Definition: execute.c:40
static void sprintf_float_value(char *ptr, float value, const char *delim)
Definition: execute.c:472
bool ECPGis_noind_null(enum ECPGttype type, const void *ptr)
Definition: misc.c:387
char * PGTYPESdate_to_asc(date)
Definition: datetime.c:101
char * PGTYPESinterval_to_asc(interval *)
Definition: interval.c:1062
int PGTYPESnumeric_from_decimal(decimal *, numeric *)
Definition: numeric.c:1571
char * PGTYPESnumeric_to_asc(numeric *, int)
Definition: numeric.c:343
char * PGTYPEStimestamp_to_asc(timestamp)
Definition: timestamp.c:272
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376

References variable::arrsize, numeric::dscale, ecpg_alloc(), ECPG_CONVERT_BOOL, ecpg_free(), ecpg_raise(), ecpg_realloc(), ECPG_SQLSTATE_DATATYPE_MISMATCH, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, ecpg_strdup(), ecpg_type_name(), ECPG_UNSUPPORTED, ECPGis_noind_null(), ECPGt_bool, ECPGt_bytea, ECPGt_char, ECPGt_char_variable, ECPGt_const, ECPGt_date, ECPGt_decimal, ECPGt_descriptor, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_sqlda, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, ECPGt_varchar, element(), force_indicator, variable::ind_type, variable::ind_value, variable::offset, PGTYPESdate_to_asc(), PGTYPESinterval_to_asc(), PGTYPESnumeric_copy(), PGTYPESnumeric_free(), PGTYPESnumeric_from_decimal(), PGTYPESnumeric_new(), PGTYPESnumeric_to_asc(), PGTYPEStimestamp_to_asc(), quote_postgres(), sprintf, sprintf_double_value(), sprintf_float_value(), generate_unaccent_rules::str, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_build_params(), ECPGset_desc(), and store_input_from_desc().

◆ ecpg_store_result()

bool ecpg_store_result ( const PGresult results,
int  act_field,
const struct statement stmt,
struct variable var 
)

Definition at line 303 of file execute.c.

305 {
306  enum ARRAY_TYPE isarray;
307  int act_tuple,
308  ntuples = PQntuples(results);
309  bool status = true;
310 
311  if ((isarray = ecpg_is_type_an_array(PQftype(results, act_field), stmt, var)) == ECPG_ARRAY_ERROR)
312  {
314  return false;
315  }
316 
317  if (isarray == ECPG_ARRAY_NONE)
318  {
319  /*
320  * if we don't have enough space, we cannot read all tuples
321  */
322  if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize))
323  {
324  ecpg_log("ecpg_store_result on line %d: incorrect number of matches; %d don't fit into array of %ld\n",
325  stmt->lineno, ntuples, var->arrsize);
327  return false;
328  }
329  }
330  else
331  {
332  /*
333  * since we read an array, the variable has to be an array too
334  */
335  if (var->arrsize == 0)
336  {
338  return false;
339  }
340  }
341 
342  /*
343  * allocate memory for NULL pointers
344  */
345  if ((var->arrsize == 0 || var->varcharsize == 0) && var->value == NULL)
346  {
347  int len = 0;
348 
349  if (!PQfformat(results, act_field))
350  {
351  switch (var->type)
352  {
353  case ECPGt_char:
354  case ECPGt_unsigned_char:
355  case ECPGt_string:
356  if (!var->varcharsize && !var->arrsize)
357  {
358  /* special mode for handling char**foo=0 */
359  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
360  len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
361  len *= var->offset; /* should be 1, but YMNK */
362  len += (ntuples + 1) * sizeof(char *);
363  }
364  else
365  {
366  var->varcharsize = 0;
367  /* check strlen for each tuple */
368  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
369  {
370  int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
371 
372  if (len > var->varcharsize)
373  var->varcharsize = len;
374  }
375  var->offset *= var->varcharsize;
376  len = var->offset * ntuples;
377  }
378  break;
379  case ECPGt_varchar:
380  len = ntuples * (var->varcharsize + sizeof(int));
381  break;
382  default:
383  len = var->offset * ntuples;
384  break;
385  }
386  }
387  else
388  {
389  for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
390  len += PQgetlength(results, act_tuple, act_field);
391  }
392 
393  ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
394  var->value = (char *) ecpg_auto_alloc(len, stmt->lineno);
395  if (!var->value)
396  return false;
397  *((char **) var->pointer) = var->value;
398  }
399 
400  /* allocate indicator variable if needed */
401  if ((var->ind_arrsize == 0 || var->ind_varcharsize == 0) && var->ind_value == NULL && var->ind_pointer != NULL)
402  {
403  int len = var->ind_offset * ntuples;
404 
405  var->ind_value = (char *) ecpg_auto_alloc(len, stmt->lineno);
406  if (!var->ind_value)
407  return false;
408  *((char **) var->ind_pointer) = var->ind_value;
409  }
410 
411  /* fill the variable with the tuple(s) */
412  if (!var->varcharsize && !var->arrsize &&
413  (var->type == ECPGt_char || var->type == ECPGt_unsigned_char || var->type == ECPGt_string))
414  {
415  /* special mode for handling char**foo=0 */
416 
417  /* filling the array of (char*)s */
418  char **current_string = (char **) var->value;
419 
420  /* storing the data (after the last array element) */
421  char *current_data_location = (char *) &current_string[ntuples + 1];
422 
423  for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
424  {
425  int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
426 
427  if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
428  var->type, var->ind_type, current_data_location,
429  var->ind_value, len, 0, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
430  status = false;
431  else
432  {
433  *current_string = current_data_location;
434  current_data_location += len;
435  current_string++;
436  }
437  }
438 
439  /* terminate the list */
440  *current_string = NULL;
441  }
442  else
443  {
444  for (act_tuple = 0; act_tuple < ntuples && status; act_tuple++)
445  {
446  if (!ecpg_get_data(results, act_tuple, act_field, stmt->lineno,
447  var->type, var->ind_type, var->value,
448  var->ind_value, var->varcharsize, var->offset, var->ind_offset, isarray, stmt->compat, stmt->force_indicator))
449  status = false;
450  }
451  }
452  return status;
453 }
#define ECPG_TOO_MANY_MATCHES
Definition: ecpgerrno.h:21
char * ecpg_auto_alloc(long, int)
Definition: memory.c:108
#define ECPG_SQLSTATE_CARDINALITY_VIOLATION
ARRAY_TYPE
static enum ARRAY_TYPE ecpg_is_type_an_array(int type, const struct statement *stmt, const struct variable *var)
Definition: execute.c:164

References variable::arrsize, statement::compat, ECPG_ARRAY_ERROR, ECPG_ARRAY_NONE, ecpg_auto_alloc(), ecpg_get_data(), ECPG_INFORMIX_SUBSELECT_NOT_ONE, ecpg_is_type_an_array(), ecpg_log(), ECPG_NO_ARRAY, ECPG_OUT_OF_MEMORY, ecpg_raise(), ECPG_SQLSTATE_CARDINALITY_VIOLATION, ECPG_SQLSTATE_DATATYPE_MISMATCH, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, ECPG_TOO_MANY_MATCHES, ECPGt_char, ECPGt_string, ECPGt_unsigned_char, ECPGt_varchar, statement::force_indicator, variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, INFORMIX_MODE, ECPGtype_information_cache::isarray, len, statement::lineno, variable::offset, variable::pointer, PQfformat(), PQftype(), PQgetlength(), PQgetvalue(), PQntuples(), status(), variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_process_output(), and ECPGget_desc().

◆ ecpg_strdup()

char* ecpg_strdup ( const char *  string,
int  lineno 
)

Definition at line 47 of file memory.c.

48 {
49  char *new;
50 
51  if (string == NULL)
52  return NULL;
53 
54  new = strdup(string);
55  if (!new)
56  {
58  return NULL;
59  }
60 
61  return new;
62 }

References ECPG_OUT_OF_MEMORY, ecpg_raise(), and ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY.

Referenced by AddStmtToCache(), ecpg_auto_prepare(), ecpg_do_prologue(), ecpg_register_prepared_stmt(), ecpg_store_input(), ECPGconnect(), ECPGget_desc(), and prepare_common().

◆ ecpg_type_name()

const char* ecpg_type_name ( enum  ECPGttype)

Definition at line 17 of file typename.c.

18 {
19  switch (typ)
20  {
21  case ECPGt_char:
22  case ECPGt_string:
23  return "char";
25  return "unsigned char";
26  case ECPGt_short:
27  return "short";
29  return "unsigned short";
30  case ECPGt_int:
31  return "int";
32  case ECPGt_unsigned_int:
33  return "unsigned int";
34  case ECPGt_long:
35  return "long";
37  return "unsigned long";
38  case ECPGt_long_long:
39  return "long long";
41  return "unsigned long long";
42  case ECPGt_float:
43  return "float";
44  case ECPGt_double:
45  return "double";
46  case ECPGt_bool:
47  return "bool";
48  case ECPGt_varchar:
49  return "varchar";
50  case ECPGt_bytea:
51  return "bytea";
53  return "char";
54  case ECPGt_decimal: