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)
 
void ecpg_pthreads_init (void)
 
struct connectionecpg_get_connection (const char *connection_name)
 
char * ecpg_alloc (long size, int lineno)
 
char * ecpg_auto_alloc (long size, int lineno)
 
char * ecpg_realloc (void *ptr, long size, int lineno)
 
void ecpg_free (void *ptr)
 
bool ecpg_init (const struct connection *con, const char *connection_name, const int lineno)
 
char * ecpg_strdup (const char *string, int lineno)
 
const char * ecpg_type_name (enum ECPGttype typ)
 
int ecpg_dynamic_type (Oid type)
 
int sqlda_dynamic_type (Oid type, enum COMPAT_MODE compat)
 
void ecpg_clear_auto_mem (void)
 
struct descriptorecpg_find_desc (int line, const char *name)
 
struct prepared_statementecpg_find_prepared_statement (const char *name, struct connection *con, struct prepared_statement **prev_)
 
bool ecpg_store_result (const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
 
bool ecpg_store_input (const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
 
void ecpg_free_params (struct statement *stmt, bool print)
 
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)
 
bool ecpg_build_params (struct statement *stmt)
 
bool ecpg_autostart_transaction (struct statement *stmt)
 
bool ecpg_execute (struct statement *stmt)
 
bool ecpg_process_output (struct statement *stmt, bool clear_result)
 
void ecpg_do_epilogue (struct statement *stmt)
 
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)
 
bool ecpg_check_PQresult (PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
 
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 *name, struct connection *con)
 
bool ecpg_deallocate_all_conn (int lineno, enum COMPAT_MODE c, struct connection *con)
 
void ecpg_log (const char *format,...) pg_attribute_printf(1
 
void bool ecpg_auto_prepare (int lineno, const char *connection_name, const int compat, char **name, const char *query)
 
bool ecpg_register_prepared_stmt (struct statement *stmt)
 
void ecpg_init_sqlca (struct sqlca_t *sqlca)
 
struct sqlda_compatecpg_build_compat_sqlda (int line, PGresult *res, int row, enum COMPAT_MODE compat)
 
void ecpg_set_compat_sqlda (int lineno, struct sqlda_compat **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
 
struct sqlda_structecpg_build_native_sqlda (int line, PGresult *res, int row, enum COMPAT_MODE compat)
 
void ecpg_set_native_sqlda (int lineno, struct sqlda_struct **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
 
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 242 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 258 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_CARDINALITY_VIOLATION

#define ECPG_SQLSTATE_CARDINALITY_VIOLATION   "21000"

Definition at line 256 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST

#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST   "08003"

Definition at line 254 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_DATATYPE_MISMATCH

#define ECPG_SQLSTATE_DATATYPE_MISMATCH   "42804"

Definition at line 264 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_DUPLICATE_CURSOR

#define ECPG_SQLSTATE_DUPLICATE_CURSOR   "42P03"

Definition at line 265 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_ECPG_INTERNAL_ERROR

#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR   "YE000"

Definition at line 268 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY

#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY   "YE001"

Definition at line 269 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_CURSOR_NAME

#define ECPG_SQLSTATE_INVALID_CURSOR_NAME   "34000"

Definition at line 262 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX

#define ECPG_SQLSTATE_INVALID_DESCRIPTOR_INDEX   "07009"

Definition at line 252 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME

#define ECPG_SQLSTATE_INVALID_SQL_DESCRIPTOR_NAME   "33000"

Definition at line 261 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME

#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME   "26000"

Definition at line 260 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION

#define ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION   "25P01"

Definition at line 259 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NO_DATA

#define ECPG_SQLSTATE_NO_DATA   "02000"

Definition at line 248 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER

#define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER   "22002"

Definition at line 257 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION

#define ECPG_SQLSTATE_RESTRICTED_DATA_TYPE_ATTRIBUTE_VIOLATION   "07006"

Definition at line 251 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION

#define ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION   "08001"

Definition at line 253 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_SYNTAX_ERROR

#define ECPG_SQLSTATE_SYNTAX_ERROR   "42601"

Definition at line 263 of file ecpglib_extern.h.

◆ ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN

#define ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN   "08007"

Definition at line 255 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 249 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 250 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 117 of file memory.c.

118 {
119  struct auto_mem *am = (struct auto_mem *) ecpg_alloc(sizeof(struct auto_mem), lineno);
120 
121  if (!am)
122  return false;
123 
124  am->pointer = ptr;
125  am->next = get_auto_allocs();
126  set_auto_allocs(am);
127  return true;
128 }
static struct auto_mem * get_auto_allocs(void)
Definition: memory.c:88
char * ecpg_alloc(long size, int lineno)
Definition: memory.c:19
static void set_auto_allocs(struct auto_mem *am)
Definition: memory.c:95
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
static pg_noinline void Size size
Definition: slab.c:607

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

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 101 of file memory.c.

102 {
103  void *ptr = (void *) ecpg_alloc(size, lineno);
104 
105  if (!ptr)
106  return NULL;
107 
108  if (!ecpg_add_mem(ptr, lineno))
109  {
110  ecpg_free(ptr);
111  return NULL;
112  }
113  return ptr;
114 }
bool ecpg_add_mem(void *ptr, int lineno)
Definition: memory.c:117
void ecpg_free(void *ptr)
Definition: memory.c:13

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

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:71
enum COMPAT_MODE compat
Definition: ecpg.c:25
void ecpg_log(const char *format,...) pg_attribute_printf(1
char * ecpg_strdup(const char *string, int lineno)
Definition: memory.c:47
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:240
Definition: type.h:108
char stmtID[STMTID_SIZE]
Definition: prepare.c:31
long execs
Definition: prepare.c:33
const char * name

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 {
1583  if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
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 *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
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:6903
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
#define stmt
Definition: indent_codes.h:59
@ PQTRANS_IDLE
Definition: libpq-fe.h:121

References ecpg_check_PQresult(), ecpg_free_params(), PQclear(), PQexec(), PQTRANS_IDLE, PQtransactionStatus(), and stmt.

Referenced by ecpg_do().

◆ ecpg_build_compat_sqlda()

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

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 
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 size, int lineno)
Definition: memory.c:19
int sqlda_dynamic_type(Oid type, enum COMPAT_MODE compat)
Definition: typename.c:107
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3679
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3527
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3449
int PQfsize(const PGresult *res, int field_num)
Definition: fe-exec.c:3690
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, size, 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  compat 
)

Definition at line 412 of file sqlda.c.

413 {
414  struct sqlda_struct *sqlda;
415  long size;
416  int i;
417 
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, size, 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 ||
1448  stmt->statement_type == ECPGst_exec_with_exprlist)
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:832
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS
#define INFORMIX_MODE(X)
char * ecpg_realloc(void *ptr, long size, int lineno)
Definition: memory.c:33
void ecpg_free(void *ptr)
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:6913
static struct @154 value
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
const void size_t len
#define snprintf
Definition: port.h:238
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
void * value
struct variable * next
enum ECPGttype type
void * pointer

References 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, i, if(), INFORMIX_MODE, insert_tobeinserted(), descriptor_item::is_binary, descriptor::items, len, descriptor_item::next, variable::next, next_insert(), descriptor_item::num, variable::pointer, PQparameterStatus(), 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, stmt, 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  compat 
)

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:6948
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3387
int PQendcopy(PGconn *conn)
Definition: fe-exec.c:2911
@ PGRES_COPY_IN
Definition: libpq-fe.h:107
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:111
@ PGRES_COPY_OUT
Definition: libpq-fe.h:106
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:99
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:108
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:110
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103

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 151 of file memory.c.

152 {
153  struct auto_mem *am = get_auto_allocs();
154 
155  /* only free our own structure */
156  if (am)
157  {
158  do
159  {
160  struct auto_mem *act = am;
161 
162  am = am->next;
163  ecpg_free(act);
164  } while (am);
165  set_auto_allocs(NULL);
166  }
167 }

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 con 
)

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
struct prepared_statement * prep_stmts

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 2243 of file execute.c.

2244 {
2245  struct statement *stmt = NULL;
2246 
2247  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2249  query, args, &stmt))
2250  goto fail;
2251 
2252  if (!ecpg_build_params(stmt))
2253  goto fail;
2254 
2256  goto fail;
2257 
2258  if (!ecpg_execute(stmt))
2259  goto fail;
2260 
2261  if (!ecpg_process_output(stmt, true))
2262  goto fail;
2263 
2265  return true;
2266 
2267 fail:
2269  return false;
2270 }
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:2211
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, questionmarks, and stmt.

Referenced by ECPGdo().

◆ ecpg_do_epilogue()

void ecpg_do_epilogue ( struct statement stmt)

Definition at line 2211 of file execute.c.

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

References free_statement(), locale_t, setlocale, and stmt.

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

References generate_unaccent_rules::args, variable::arrsize, Assert(), statement::command, compat, 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_pthreads_init(), 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, force_indicator, variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, statement::lineno, sort-test::list, locale_t, connection::name, variable::next, variable::offset, variable::pointer, questionmarks, setlocale, statement::statement_type, stmt, 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 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  {
1607  stmt->results = PQexecPrepared(stmt->connection->connection,
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  {
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 *stmt)
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:2238
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:2285

References ecpg_check_PQresult(), ecpg_free_params(), ecpg_log(), ecpg_register_prepared_stmt(), ECPGst_execute, ECPGst_prepare, PQexec(), PQexecParams(), PQexecPrepared(), and stmt.

Referenced by ecpg_do().

◆ ecpg_find_desc()

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

Definition at line 832 of file descriptor.c.

833 {
834  struct descriptor *desc;
835 
836  for (desc = get_descriptors(); desc; desc = desc->next)
837  {
838  if (strcmp(name, desc->name) == 0)
839  return desc;
840  }
841 
843  return NULL; /* not found */
844 }
#define ECPG_UNKNOWN_DESCRIPTOR
Definition: ecpgerrno.h:42
static struct descriptor * get_descriptors(void)
Definition: descriptor.c:40
#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 * 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(), print(), print_param_value(), and stmt.

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 71 of file connect.c.

72 {
73  struct connection *ret = NULL;
74 
75  if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
76  {
77  ecpg_pthreads_init(); /* ensure actual_connection_key is valid */
78 
80 
81  /*
82  * if no connection in TSD for this thread, get the global default
83  * connection and hope the user knows what they're doing (i.e. using
84  * their own mutex to protect that connection from concurrent accesses
85  */
86  if (ret == NULL)
87  /* no TSD connection here either, using global */
88  ret = actual_connection;
89  }
90  else
91  {
93 
94  ret = ecpg_get_connection_nr(connection_name);
95 
97  }
98 
99  return ret;
100 }
static pthread_mutex_t connections_mutex
Definition: connect.c:17
static struct connection * ecpg_get_connection_nr(const char *connection_name)
Definition: connect.c:36
static pthread_key_t actual_connection_key
Definition: connect.c:18
static struct connection * actual_connection
Definition: connect.c:20
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:60
void * pthread_getspecific(pthread_key_t key)
Definition: pthread-win32.c:29
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:42

References actual_connection, actual_connection_key, connections_mutex, ecpg_get_connection_nr(), ecpg_pthreads_init(), 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  case ECPGt_long_long:
442  *((long long int *) (var + offset * act_tuple)) = strtoll(pval, &scan_length, 10);
443  if (garbage_left(isarray, &scan_length, compat))
444  {
446  return false;
447  }
448  pval = scan_length;
449 
450  break;
451 
453  *((unsigned long long int *) (var + offset * act_tuple)) = strtoull(pval, &scan_length, 10);
454  if (garbage_left(isarray, &scan_length, compat))
455  {
457  return false;
458  }
459  pval = scan_length;
460 
461  break;
462 
463  case ECPGt_float:
464  case ECPGt_double:
465  if (isarray && *pval == '"')
466  pval++;
467 
468  if (!check_special_value(pval, &dres, &scan_length))
469  dres = strtod(pval, &scan_length);
470 
471  if (isarray && *scan_length == '"')
472  scan_length++;
473 
474  /* no special INFORMIX treatment for floats */
475  if (garbage_left(isarray, &scan_length, ECPG_COMPAT_PGSQL))
476  {
479  return false;
480  }
481  pval = scan_length;
482 
483  switch (type)
484  {
485  case ECPGt_float:
486  *((float *) (var + offset * act_tuple)) = dres;
487  break;
488  case ECPGt_double:
489  *((double *) (var + offset * act_tuple)) = dres;
490  break;
491  default:
492  /* Cannot happen */
493  break;
494  }
495  break;
496 
497  case ECPGt_bool:
498  if (pval[0] == 'f' && pval[1] == '\0')
499  {
500  *((bool *) (var + offset * act_tuple)) = false;
501  pval++;
502  break;
503  }
504  else if (pval[0] == 't' && pval[1] == '\0')
505  {
506  *((bool *) (var + offset * act_tuple)) = true;
507  pval++;
508  break;
509  }
510  else if (pval[0] == '\0' && PQgetisnull(results, act_tuple, act_field))
511  {
512  /* NULL is valid */
513  break;
514  }
515 
518  return false;
519  break;
520 
521  case ECPGt_bytea:
522  {
523  struct ECPGgeneric_bytea *variable =
524  (struct ECPGgeneric_bytea *) (var + offset * act_tuple);
525  long dst_size,
526  src_size,
527  dec_size;
528 
529  dst_size = ecpg_hex_enc_len(varcharsize);
530  src_size = size - 2; /* exclude backslash + 'x' */
531  dec_size = src_size < dst_size ? src_size : dst_size;
532  variable->len = hex_decode(pval + 2, dec_size, variable->arr);
533 
534  if (dst_size < src_size)
535  {
536  long rcv_size = ecpg_hex_dec_len(size - 2);
537 
538  /* truncation */
539  switch (ind_type)
540  {
541  case ECPGt_short:
543  *((short *) (ind + ind_offset * act_tuple)) = rcv_size;
544  break;
545  case ECPGt_int:
546  case ECPGt_unsigned_int:
547  *((int *) (ind + ind_offset * act_tuple)) = rcv_size;
548  break;
549  case ECPGt_long:
550  case ECPGt_unsigned_long:
551  *((long *) (ind + ind_offset * act_tuple)) = rcv_size;
552  break;
553  case ECPGt_long_long:
555  *((long long int *) (ind + ind_offset * act_tuple)) = rcv_size;
556  break;
557  default:
558  break;
559  }
560  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
561  }
562 
563  pval += size;
564  }
565  break;
566 
567  case ECPGt_char:
568  case ECPGt_unsigned_char:
569  case ECPGt_string:
570  {
571  char *str = (char *) (var + offset * act_tuple);
572 
573  /*
574  * If varcharsize is unknown and the offset is that of
575  * char *, then this variable represents the array of
576  * character pointers. So, use extra indirection.
577  */
578  if (varcharsize == 0 && offset == sizeof(char *))
579  str = *(char **) str;
580 
581  if (varcharsize > size)
582  {
583  /*
584  * compatibility mode, blank pad and null
585  * terminate char array
586  */
588  {
589  memset(str, ' ', varcharsize);
590  memcpy(str, pval, size);
591  str[varcharsize - 1] = '\0';
592 
593  /*
594  * compatibility mode empty string gets -1
595  * indicator but no warning
596  */
597  if (size == 0)
598  {
599  /* truncation */
600  switch (ind_type)
601  {
602  case ECPGt_short:
604  *((short *) (ind + ind_offset * act_tuple)) = -1;
605  break;
606  case ECPGt_int:
607  case ECPGt_unsigned_int:
608  *((int *) (ind + ind_offset * act_tuple)) = -1;
609  break;
610  case ECPGt_long:
611  case ECPGt_unsigned_long:
612  *((long *) (ind + ind_offset * act_tuple)) = -1;
613  break;
614  case ECPGt_long_long:
616  *((long long int *) (ind + ind_offset * act_tuple)) = -1;
617  break;
618  default:
619  break;
620  }
621  }
622  }
623  else
624  {
625  strncpy(str, pval, size + 1);
626  }
627  /* do the rtrim() */
628  if (type == ECPGt_string)
629  {
630  char *last = str + size;
631 
632  while (last > str && (*last == ' ' || *last == '\0'))
633  {
634  *last = '\0';
635  last--;
636  }
637  }
638  }
639  else
640  {
641  int charsize = varcharsize;
642 
643  /*
644  * assume that the caller provided storage exactly
645  * fit when varcharsize is zero.
646  */
647  if (varcharsize == 0)
648  charsize = size + 1;
649 
650  strncpy(str, pval, charsize);
651 
652  /* compatibility mode, null terminate char array */
653  if (ORACLE_MODE(compat) && (charsize - 1) < size)
654  {
656  str[charsize - 1] = '\0';
657  }
658 
659  if (charsize < size || (ORACLE_MODE(compat) && (charsize - 1) < size))
660  {
661  /* truncation */
662  switch (ind_type)
663  {
664  case ECPGt_short:
666  *((short *) (ind + ind_offset * act_tuple)) = size;
667  break;
668  case ECPGt_int:
669  case ECPGt_unsigned_int:
670  *((int *) (ind + ind_offset * act_tuple)) = size;
671  break;
672  case ECPGt_long:
673  case ECPGt_unsigned_long:
674  *((long *) (ind + ind_offset * act_tuple)) = size;
675  break;
676  case ECPGt_long_long:
678  *((long long int *) (ind + ind_offset * act_tuple)) = size;
679  break;
680  default:
681  break;
682  }
683  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
684  }
685  }
686  pval += size;
687  }
688  break;
689 
690  case ECPGt_varchar:
691  {
692  struct ECPGgeneric_varchar *variable =
693  (struct ECPGgeneric_varchar *) (var + offset * act_tuple);
694 
695  variable->len = size;
696  if (varcharsize == 0)
697  strncpy(variable->arr, pval, variable->len);
698  else
699  {
700  strncpy(variable->arr, pval, varcharsize);
701 
702  if (variable->len > varcharsize)
703  {
704  /* truncation */
705  switch (ind_type)
706  {
707  case ECPGt_short:
709  *((short *) (ind + ind_offset * act_tuple)) = variable->len;
710  break;
711  case ECPGt_int:
712  case ECPGt_unsigned_int:
713  *((int *) (ind + ind_offset * act_tuple)) = variable->len;
714  break;
715  case ECPGt_long:
716  case ECPGt_unsigned_long:
717  *((long *) (ind + ind_offset * act_tuple)) = variable->len;
718  break;
719  case ECPGt_long_long:
721  *((long long int *) (ind + ind_offset * act_tuple)) = variable->len;
722  break;
723  default:
724  break;
725  }
726  sqlca->sqlwarn[0] = sqlca->sqlwarn[1] = 'W';
727 
728  variable->len = varcharsize;
729  }
730  }
731  pval += size;
732  }
733  break;
734 
735  case ECPGt_decimal:
736  case ECPGt_numeric:
737  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '}'; endptr++);
738  endchar = *endptr;
739  *endptr = '\0';
740  nres = PGTYPESnumeric_from_asc(pval, &scan_length);
741  *endptr = endchar;
742 
743  /* did we get an error? */
744  if (nres == NULL)
745  {
746  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
747  lineno, pval, errno);
748 
749  if (INFORMIX_MODE(compat))
750  {
751  /*
752  * Informix wants its own NULL value here instead
753  * of an error
754  */
755  nres = PGTYPESnumeric_new();
756  if (nres)
758  else
759  {
762  return false;
763  }
764  }
765  else
766  {
769  return false;
770  }
771  }
772  else
773  {
774  if (!isarray && garbage_left(isarray, &scan_length, compat))
775  {
776  free(nres);
779  return false;
780  }
781  }
782  pval = scan_length;
783 
784  if (type == ECPGt_numeric)
785  PGTYPESnumeric_copy(nres, (numeric *) (var + offset * act_tuple));
786  else
787  PGTYPESnumeric_to_decimal(nres, (decimal *) (var + offset * act_tuple));
788 
789  PGTYPESnumeric_free(nres);
790  break;
791 
792  case ECPGt_interval:
793  if (*pval == '"')
794  pval++;
795 
796  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
797  endchar = *endptr;
798  *endptr = '\0';
799  ires = PGTYPESinterval_from_asc(pval, &scan_length);
800  *endptr = endchar;
801 
802  /* did we get an error? */
803  if (ires == NULL)
804  {
805  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
806  lineno, pval, errno);
807 
808  if (INFORMIX_MODE(compat))
809  {
810  /*
811  * Informix wants its own NULL value here instead
812  * of an error
813  */
814  ires = (interval *) ecpg_alloc(sizeof(interval), lineno);
815  if (!ires)
816  return false;
817 
819  }
820  else
821  {
824  return false;
825  }
826  }
827  else
828  {
829  if (*scan_length == '"')
830  scan_length++;
831 
832  if (!isarray && garbage_left(isarray, &scan_length, compat))
833  {
834  free(ires);
837  return false;
838  }
839  }
840  pval = scan_length;
841 
842  PGTYPESinterval_copy(ires, (interval *) (var + offset * act_tuple));
843  free(ires);
844  break;
845 
846  case ECPGt_date:
847  if (*pval == '"')
848  pval++;
849 
850  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
851  endchar = *endptr;
852  *endptr = '\0';
853  ddres = PGTYPESdate_from_asc(pval, &scan_length);
854  *endptr = endchar;
855 
856  /* did we get an error? */
857  if (errno != 0)
858  {
859  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
860  lineno, pval, errno);
861 
862  if (INFORMIX_MODE(compat))
863  {
864  /*
865  * Informix wants its own NULL value here instead
866  * of an error
867  */
869  }
870  else
871  {
874  return false;
875  }
876  }
877  else
878  {
879  if (*scan_length == '"')
880  scan_length++;
881 
882  if (!isarray && garbage_left(isarray, &scan_length, compat))
883  {
886  return false;
887  }
888  }
889 
890  *((date *) (var + offset * act_tuple)) = ddres;
891  pval = scan_length;
892  break;
893 
894  case ECPGt_timestamp:
895  if (*pval == '"')
896  pval++;
897 
898  for (endptr = pval; *endptr && *endptr != ',' && *endptr != '"' && *endptr != '}'; endptr++);
899  endchar = *endptr;
900  *endptr = '\0';
901  tres = PGTYPEStimestamp_from_asc(pval, &scan_length);
902  *endptr = endchar;
903 
904  /* did we get an error? */
905  if (errno != 0)
906  {
907  ecpg_log("ecpg_get_data on line %d: RESULT %s; errno %d\n",
908  lineno, pval, errno);
909 
910  if (INFORMIX_MODE(compat))
911  {
912  /*
913  * Informix wants its own NULL value here instead
914  * of an error
915  */
917  }
918  else
919  {
922  return false;
923  }
924  }
925  else
926  {
927  if (*scan_length == '"')
928  scan_length++;
929 
930  if (!isarray && garbage_left(isarray, &scan_length, compat))
931  {
934  return false;
935  }
936  }
937 
938  *((timestamp *) (var + offset * act_tuple)) = tres;
939  pval = scan_length;
940  break;
941 
942  default:
946  return false;
947  break;
948  }
949  if (ECPG_IS_ARRAY(isarray))
950  {
951  bool string = false;
952 
953  /* set array to next entry */
954  ++act_tuple;
955 
956  /* set pval to the next entry */
957 
958  /*
959  * *pval != '\0' should not be needed, but is used as a safety
960  * guard
961  */
962  for (; *pval != '\0' && (string || (!array_delimiter(isarray, *pval) && !array_boundary(isarray, *pval))); ++pval)
963  if (*pval == '"')
964  string = string ? false : true;
965 
966  if (array_delimiter(isarray, *pval))
967  ++pval;
968  }
969  }
970  } while (*pval != '\0' && !array_boundary(isarray, *pval));
971 
972  return true;
973 }
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
const char * ecpg_type_name(enum ECPGttype typ)
Definition: typename.c:17
#define ECPG_SQLSTATE_NULL_VALUE_NO_INDICATOR_PARAMETER
#define ORACLE_MODE(X)
#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:3847
int PQfformat(const PGresult *res, int field_num)
Definition: fe-exec.c:3668
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3836
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3861
void ECPGset_noind_null(enum ECPGttype type, void *ptr)
Definition: misc.c:273
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:108
return false
Definition: isn.c:131
long date
Definition: pgtypes_date.h:9
date PGTYPESdate_from_asc(char *str, char **endptr)
Definition: datetime.c:47
int PGTYPESinterval_copy(interval *intvlsrc, interval *intvldest)
Definition: interval.c:1082
interval * PGTYPESinterval_from_asc(char *str, char **endptr)
Definition: interval.c:1003
int PGTYPESnumeric_copy(numeric *src, numeric *dst)
Definition: numeric.c:1388
int PGTYPESnumeric_to_decimal(numeric *src, decimal *dst)
Definition: numeric.c:1547
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:42
numeric * PGTYPESnumeric_from_asc(char *str, char **endptr)
Definition: numeric.c:321
void PGTYPESnumeric_free(numeric *var)
Definition: numeric.c:385
timestamp PGTYPEStimestamp_from_asc(char *str, char **endptr)
Definition: timestamp.c:206
int64 timestamp
#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, size, sqlca, generate_unaccent_rules::str, and 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 73 of file misc.c.

74 {
75  struct sqlca_t *sqlca = ECPGget_sqlca();
76 
77  if (sqlca == NULL)
78  {
80  NULL);
81  return false;
82  }
83 
85  if (con == NULL)
86  {
88  connection_name ? connection_name : ecpg_gettext("NULL"));
89  return false;
90  }
91 
92  return true;
93 }
#define ECPG_NO_CONN
Definition: ecpgerrno.h:36
#define ECPG_SQLSTATE_CONNECTION_DOES_NOT_EXIST
void ecpg_init_sqlca(struct sqlca_t *sqlca)
Definition: misc.c:67

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 67 of file misc.c.

68 {
69  memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
70 }
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 }

References ecpg_find_prepared_statement(), name, and 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 */
1921  PQconsumeInput(stmt->connection->connection);
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);
1927  PQconsumeInput(stmt->connection->connection);
1928  }
1929 
1930  return status;
1931 }
struct sqlda_struct * ecpg_build_native_sqlda(int line, PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:412
void ecpg_set_compat_sqlda(int lineno, struct sqlda_compat **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:255
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS
void ecpg_set_native_sqlda(int lineno, struct sqlda_struct **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:444
struct sqlda_compat * ecpg_build_compat_sqlda(int line, PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:205
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:3992
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2629
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3782
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3441
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1960
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3712
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3753
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2038
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2778
while(p+4<=pend)
#define printf(...)
Definition: port.h:244
PGresult * result
int be_pid
Definition: libpq-fe.h:198
char * relname
Definition: libpq-fe.h:197

References pgNotify::be_pid, 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, variable::next, 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, sqlca, stmt, variable::type, and while().

Referenced by ecpg_do().

◆ ecpg_pthreads_init()

void ecpg_pthreads_init ( void  )

Definition at line 30 of file connect.c.

31 {
33 }
static pthread_once_t actual_connection_key_once
Definition: connect.c:19
static void ecpg_actual_connection_init(void)
Definition: connect.c:24

◆ 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:131
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:6895
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3426
@ CONNECTION_BAD
Definition: libpq-fe.h:61
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:57
PGconn * conn
Definition: streamutil.c:55

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, realloc, and size.

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:221
struct variable * inlist
struct variable * outlist
struct connection * connection
char * name

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 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  compat 
)

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:101
Datum numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:1237
unsigned char bool
Definition: c.h:443
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:670
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  compat 
)

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:342
char * PGTYPESdate_to_asc(date dDate)
Definition: datetime.c:101
char * PGTYPESinterval_to_asc(interval *span)
Definition: interval.c:1062
int PGTYPESnumeric_from_decimal(decimal *src, numeric *dst)
Definition: numeric.c:1570
char * PGTYPESnumeric_to_asc(numeric *num, int dscale)
Definition: numeric.c:343
char * PGTYPEStimestamp_to_asc(timestamp tstamp)
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 slen = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
371 
372  if (slen > var->varcharsize)
373  var->varcharsize = slen;
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 size, int lineno)
Definition: memory.c:101
#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, 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, 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, variable::offset, variable::pointer, PQfformat(), PQftype(), PQgetlength(), PQgetvalue(), PQntuples(), stmt, 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  typ)

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:
55  return "decimal";
56  case ECPGt_numeric:
57  return "numeric";
58  case ECPGt_date:
59  return "date";
60  case ECPGt_timestamp:
61  return "timestamp";
62  case ECPGt_interval:
63  return "interval";
64  case ECPGt_const:
65  return "Const";
66  default:
67  abort();
68  }
69  return ""; /* keep MSC compiler happy */
70 }

References ECPGt_bool, ECPGt_bytea, ECPGt_char, ECPGt_char_variable, ECPGt_const, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, and ECPGt_varchar.

Referenced by ecpg_get_data(), ecpg_store_input(), and ECPGdump_a_simple().

◆ sqlda_dynamic_type()

int sqlda_dynamic_type ( Oid  type,
enum COMPAT_MODE  compat 
)

Definition at line 107 of file typename.c.

108 {
109  switch (type)
110  {
111  case CHAROID:
112  case VARCHAROID:
113  case BPCHAROID:
114  case TEXTOID:
115  return ECPGt_char;
116  case INT2OID:
117  return ECPGt_short;
118  case INT4OID:
119  return ECPGt_int;
120  case FLOAT8OID:
121  return ECPGt_double;
122  case FLOAT4OID:
123  return ECPGt_float;
124  case NUMERICOID:
126  case DATEOID:
127  return ECPGt_date;
128  case TIMESTAMPOID:
129  case TIMESTAMPTZOID:
130  return ECPGt_timestamp;
131  case INTERVALOID:
132  return ECPGt_interval;
133  case INT8OID:
134 #ifdef HAVE_LONG_LONG_INT_64
135  return ECPGt_long_long;
136 #endif
137 #ifdef HAVE_LONG_INT_64
138  return ECPGt_long;
139 #endif
140  /* Unhandled types always return a string */
141  default:
142  return ECPGt_char;
143  }
144 }

References compat, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_numeric, ECPGt_short, ECPGt_timestamp, INFORMIX_MODE, and type.

Referenced by ecpg_build_compat_sqlda(), ecpg_build_native_sqlda(), and sqlda_common_total_size().

Variable Documentation

◆ ecpg_internal_regression_mode

bool ecpg_internal_regression_mode
extern

Definition at line 29 of file misc.c.

Referenced by ecpg_get_data(), ecpg_log(), ECPGconnect(), and ECPGdebug().

◆ ivlist

struct var_list* ivlist
extern

Definition at line 516 of file misc.c.

Referenced by ecpg_finish(), ECPGget_var(), and ECPGset_var().