PostgreSQL Source Code  git master
execute.c File Reference
#include "postgres_fe.h"
#include <math.h>
#include "catalog/pg_type_d.h"
#include "ecpgerrno.h"
#include "ecpglib.h"
#include "ecpglib_extern.h"
#include "ecpgtype.h"
#include "pgtypes_date.h"
#include "pgtypes_interval.h"
#include "pgtypes_numeric.h"
#include "pgtypes_timestamp.h"
#include "sql3types.h"
#include "sqlca.h"
#include "sqlda-compat.h"
#include "sqlda-native.h"
Include dependency graph for execute.c:

Go to the source code of this file.

Macros

#define POSTGRES_ECPG_INTERNAL
 
#define not_an_array_in_ecpg   ECPG_ARRAY_NONE
 

Functions

static char * quote_postgres (char *arg, bool quote, int lineno)
 
static void free_variable (struct variable *var)
 
static void free_statement (struct statement *stmt)
 
static int next_insert (char *text, int pos, bool questionmarks, bool std_strings)
 
static bool ecpg_type_infocache_push (struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
 
static enum ARRAY_TYPE ecpg_is_type_an_array (int type, const struct statement *stmt, const struct variable *var)
 
bool ecpg_store_result (const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
 
static void sprintf_double_value (char *ptr, double value, const char *delim)
 
static void sprintf_float_value (char *ptr, float value, const char *delim)
 
static char * convert_bytea_to_string (char *from_data, int from_len, int lineno)
 
bool ecpg_store_input (const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
 
static void print_param_value (char *value, int len, int is_binary, int lineno, int nth)
 
void ecpg_free_params (struct statement *stmt, bool print)
 
static bool insert_tobeinserted (int position, int ph_len, struct statement *stmt, char *tobeinserted)
 
static bool store_input_from_desc (struct statement *stmt, struct descriptor_item *desc_item, char **tobeinserted)
 
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)
 
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)
 
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 ECPGdo (const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
 
bool ECPGdo_descriptor (int line, const char *connection, const char *descriptor, const char *query)
 

Macro Definition Documentation

◆ not_an_array_in_ecpg

#define not_an_array_in_ecpg   ECPG_ARRAY_NONE

Referenced by ecpg_is_type_an_array().

◆ POSTGRES_ECPG_INTERNAL

#define POSTGRES_ECPG_INTERNAL

Definition at line 16 of file execute.c.

Function Documentation

◆ convert_bytea_to_string()

static char* convert_bytea_to_string ( char *  from_data,
int  from_len,
int  lineno 
)
static

Definition at line 491 of file execute.c.

References ecpg_alloc(), ecpg_hex_enc_len(), and ecpg_hex_encode().

Referenced by ecpg_build_params().

492 {
493  char *to_data;
494  int to_len = ecpg_hex_enc_len(from_len) + 4 + 1; /* backslash + 'x' +
495  * quote + quote */
496 
497  to_data = ecpg_alloc(to_len, lineno);
498  if (!to_data)
499  return NULL;
500 
501  strcpy(to_data, "'\\x");
502  ecpg_hex_encode(from_data, from_len, to_data + 3);
503  strcpy(to_data + 3 + ecpg_hex_enc_len(from_len), "\'");
504 
505  return to_data;
506 }
unsigned ecpg_hex_enc_len(unsigned srclen)
Definition: data.c:128
char * ecpg_alloc(long, int)
Definition: memory.c:19
unsigned ecpg_hex_encode(const char *src, unsigned len, char *dst)
Definition: data.c:191

◆ ecpg_autostart_transaction()

bool ecpg_autostart_transaction ( struct statement stmt)

Definition at line 1574 of file execute.c.

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

Referenced by ecpg_do().

1575 {
1577  {
1578  stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1579  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1580  {
1581  ecpg_free_params(stmt, false);
1582  return false;
1583  }
1584  PQclear(stmt->results);
1585  stmt->results = NULL;
1586  }
1587  return true;
1588 }
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1109
PGresult * results
struct connection * connection
enum COMPAT_MODE compat
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6578
void PQclear(PGresult *res)
Definition: fe-exec.c:694
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:283
PGconn * connection
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ ecpg_build_params()

bool ecpg_build_params ( struct statement stmt)

Definition at line 1216 of file execute.c.

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

Referenced by ecpg_do().

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

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

References ecpg_autostart_transaction(), ecpg_build_params(), ecpg_do_epilogue(), ecpg_do_prologue(), ecpg_execute(), and ecpg_process_output().

Referenced by ECPGdo().

2243 {
2244  struct statement *stmt = NULL;
2245 
2246  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2248  query, args, &stmt))
2249  goto fail;
2250 
2251  if (!ecpg_build_params(stmt))
2252  goto fail;
2253 
2254  if (!ecpg_autostart_transaction(stmt))
2255  goto fail;
2256 
2257  if (!ecpg_execute(stmt))
2258  goto fail;
2259 
2260  if (!ecpg_process_output(stmt, true))
2261  goto fail;
2262 
2263  ecpg_do_epilogue(stmt);
2264  return true;
2265 
2266 fail:
2267  ecpg_do_epilogue(stmt);
2268  return false;
2269 }
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition: execute.c:1664
bool ecpg_autostart_transaction(struct statement *stmt)
Definition: execute.c:1574
ECPG_statement_type
Definition: ecpgtype.h:95
bool ecpg_build_params(struct statement *stmt)
Definition: execute.c:1216
bool ecpg_execute(struct statement *stmt)
Definition: execute.c:1595
bool questionmarks
Definition: ecpg.c:19
enum COMPAT_MODE compat
Definition: ecpg.c:25
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:1938
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:2210
bool force_indicator
Definition: ecpg.c:18

◆ ecpg_do_epilogue()

void ecpg_do_epilogue ( struct statement stmt)

Definition at line 2210 of file execute.c.

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

Referenced by ecpg_do(), and ecpg_do_prologue().

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

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

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

Referenced by ecpg_do().

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

◆ ecpg_execute()

bool ecpg_execute ( struct statement stmt)

Definition at line 1595 of file execute.c.

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

Referenced by ecpg_do().

1596 {
1597  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);
1598  if (stmt->statement_type == ECPGst_execute)
1599  {
1601  stmt->name,
1602  stmt->nparams,
1603  (const char *const *) stmt->paramvalues,
1604  (const int *) stmt->paramlengths,
1605  (const int *) stmt->paramformats,
1606  0);
1607  ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1608  }
1609  else
1610  {
1611  if (stmt->nparams == 0)
1612  {
1613  stmt->results = PQexec(stmt->connection->connection, stmt->command);
1614  ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1615  }
1616  else
1617  {
1618  stmt->results = PQexecParams(stmt->connection->connection,
1619  stmt->command, stmt->nparams, NULL,
1620  (const char *const *) stmt->paramvalues,
1621  (const int *) stmt->paramlengths,
1622  (const int *) stmt->paramformats,
1623  0);
1624 
1625  ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1626  }
1627 
1628  if (stmt->statement_type == ECPGst_prepare)
1629  {
1630  if (!ecpg_register_prepared_stmt(stmt))
1631  {
1632  ecpg_free_params(stmt, true);
1633  return false;
1634  }
1635  }
1636  }
1637 
1638  ecpg_free_params(stmt, true);
1639 
1640  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1641  return false;
1642 
1643  return true;
1644 }
bool ecpg_register_prepared_stmt(struct statement *)
Definition: prepare.c:59
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:2000
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1109
enum ECPG_statement_type statement_type
PGresult * results
int * paramformats
struct connection * connection
enum COMPAT_MODE compat
void ecpg_log(const char *format,...) pg_attribute_printf(1
char * command
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:283
PGconn * connection
char * name
char ** paramvalues
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:1953
int * paramlengths
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ ecpg_free_params()

void ecpg_free_params ( struct statement stmt,
bool  print 
)

Definition at line 1109 of file execute.c.

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

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

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

◆ ecpg_is_type_an_array()

static enum ARRAY_TYPE ecpg_is_type_an_array ( int  type,
const struct statement stmt,
const struct variable var 
)
static

Definition at line 167 of file execute.c.

References connection::cache_head, statement::compat, statement::connection, connection::connection, ecpg_alloc(), ECPG_ARRAY_ARRAY, ECPG_ARRAY_ERROR, ECPG_ARRAY_NONE, ECPG_ARRAY_NOT_SET, ECPG_ARRAY_VECTOR, ecpg_check_PQresult(), ecpg_dynamic_type(), ecpg_free(), ECPG_IS_ARRAY, ecpg_log(), ecpg_type_infocache_push(), ECPGtype_information_cache::isarray, statement::lineno, ECPGtype_information_cache::next, not_an_array_in_ecpg, ECPGtype_information_cache::oid, PGRES_TUPLES_OK, PQclear(), PQexec(), PQgetvalue(), PQntuples(), PQresultStatus(), sprintf, SQL3_CHARACTER, SQL3_CHARACTER_VARYING, and variable::type.

Referenced by ecpg_store_result().

168 {
169  char *array_query;
170  enum ARRAY_TYPE isarray = ECPG_ARRAY_NOT_SET;
171  PGresult *query;
172  struct ECPGtype_information_cache *cache_entry;
173 
174  if ((stmt->connection->cache_head) == NULL)
175  {
176  /*
177  * Text like types are not an array for ecpg, but postgres counts them
178  * as an array. This define reminds you to not 'correct' these values.
179  */
180 #define not_an_array_in_ecpg ECPG_ARRAY_NONE
181 
182  /* populate cache with well known types to speed things up */
183  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOOLOID, ECPG_ARRAY_NONE, stmt->lineno))
184  return ECPG_ARRAY_ERROR;
185  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BYTEAOID, ECPG_ARRAY_NONE, stmt->lineno))
186  return ECPG_ARRAY_ERROR;
187  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CHAROID, ECPG_ARRAY_NONE, stmt->lineno))
188  return ECPG_ARRAY_ERROR;
190  return ECPG_ARRAY_ERROR;
191  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT8OID, ECPG_ARRAY_NONE, stmt->lineno))
192  return ECPG_ARRAY_ERROR;
193  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2OID, ECPG_ARRAY_NONE, stmt->lineno))
194  return ECPG_ARRAY_ERROR;
195  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT2VECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
196  return ECPG_ARRAY_ERROR;
197  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INT4OID, ECPG_ARRAY_NONE, stmt->lineno))
198  return ECPG_ARRAY_ERROR;
199  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), REGPROCOID, ECPG_ARRAY_NONE, stmt->lineno))
200  return ECPG_ARRAY_ERROR;
201  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TEXTOID, ECPG_ARRAY_NONE, stmt->lineno))
202  return ECPG_ARRAY_ERROR;
203  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDOID, ECPG_ARRAY_NONE, stmt->lineno))
204  return ECPG_ARRAY_ERROR;
205  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIDOID, ECPG_ARRAY_NONE, stmt->lineno))
206  return ECPG_ARRAY_ERROR;
207  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), XIDOID, ECPG_ARRAY_NONE, stmt->lineno))
208  return ECPG_ARRAY_ERROR;
209  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDOID, ECPG_ARRAY_NONE, stmt->lineno))
210  return ECPG_ARRAY_ERROR;
211  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), OIDVECTOROID, ECPG_ARRAY_VECTOR, stmt->lineno))
212  return ECPG_ARRAY_ERROR;
213  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POINTOID, ECPG_ARRAY_VECTOR, stmt->lineno))
214  return ECPG_ARRAY_ERROR;
215  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LSEGOID, ECPG_ARRAY_VECTOR, stmt->lineno))
216  return ECPG_ARRAY_ERROR;
217  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), PATHOID, ECPG_ARRAY_NONE, stmt->lineno))
218  return ECPG_ARRAY_ERROR;
220  return ECPG_ARRAY_ERROR;
221  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), POLYGONOID, ECPG_ARRAY_NONE, stmt->lineno))
222  return ECPG_ARRAY_ERROR;
223  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), LINEOID, ECPG_ARRAY_VECTOR, stmt->lineno))
224  return ECPG_ARRAY_ERROR;
225  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT4OID, ECPG_ARRAY_NONE, stmt->lineno))
226  return ECPG_ARRAY_ERROR;
227  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), FLOAT8OID, ECPG_ARRAY_NONE, stmt->lineno))
228  return ECPG_ARRAY_ERROR;
229  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), UNKNOWNOID, ECPG_ARRAY_NONE, stmt->lineno))
230  return ECPG_ARRAY_ERROR;
231  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIRCLEOID, ECPG_ARRAY_NONE, stmt->lineno))
232  return ECPG_ARRAY_ERROR;
233  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CASHOID, ECPG_ARRAY_NONE, stmt->lineno))
234  return ECPG_ARRAY_ERROR;
235  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INETOID, ECPG_ARRAY_NONE, stmt->lineno))
236  return ECPG_ARRAY_ERROR;
237  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), CIDROID, ECPG_ARRAY_NONE, stmt->lineno))
238  return ECPG_ARRAY_ERROR;
239  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BPCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
240  return ECPG_ARRAY_ERROR;
241  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARCHAROID, ECPG_ARRAY_NONE, stmt->lineno))
242  return ECPG_ARRAY_ERROR;
243  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), DATEOID, ECPG_ARRAY_NONE, stmt->lineno))
244  return ECPG_ARRAY_ERROR;
245  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMEOID, ECPG_ARRAY_NONE, stmt->lineno))
246  return ECPG_ARRAY_ERROR;
247  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPOID, ECPG_ARRAY_NONE, stmt->lineno))
248  return ECPG_ARRAY_ERROR;
249  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMESTAMPTZOID, ECPG_ARRAY_NONE, stmt->lineno))
250  return ECPG_ARRAY_ERROR;
251  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), INTERVALOID, ECPG_ARRAY_NONE, stmt->lineno))
252  return ECPG_ARRAY_ERROR;
253  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), TIMETZOID, ECPG_ARRAY_NONE, stmt->lineno))
254  return ECPG_ARRAY_ERROR;
255  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BITOID, ECPG_ARRAY_NONE, stmt->lineno))
256  return ECPG_ARRAY_ERROR;
257  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), VARBITOID, ECPG_ARRAY_NONE, stmt->lineno))
258  return ECPG_ARRAY_ERROR;
259  if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NUMERICOID, ECPG_ARRAY_NONE, stmt->lineno))
260  return ECPG_ARRAY_ERROR;
261  }
262 
263  for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
264  {
265  if (cache_entry->oid == type)
266  return cache_entry->isarray;
267  }
268 
269  array_query = (char *) ecpg_alloc(strlen("select typlen from pg_type where oid= and typelem<>0") + 11, stmt->lineno);
270  if (array_query == NULL)
271  return ECPG_ARRAY_ERROR;
272 
273  sprintf(array_query, "select typlen from pg_type where oid=%d and typelem<>0", type);
274  query = PQexec(stmt->connection->connection, array_query);
275  ecpg_free(array_query);
276  if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
277  return ECPG_ARRAY_ERROR;
278  else if (PQresultStatus(query) == PGRES_TUPLES_OK)
279  {
280  if (PQntuples(query) == 0)
281  isarray = ECPG_ARRAY_NONE;
282  else
283  {
284  isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
287  {
288  /*
289  * arrays of character strings are not yet implemented
290  */
291  isarray = ECPG_ARRAY_NONE;
292  }
293  }
294  PQclear(query);
295  }
296  else
297  return ECPG_ARRAY_ERROR;
298 
299  ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
300  ecpg_log("ecpg_is_type_an_array on line %d: type (%d); C (%d); array (%s)\n", stmt->lineno, type, var->type, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
301  return isarray;
302 }
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
Definition: execute.c:151
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int ecpg_dynamic_type(Oid)
Definition: typename.c:73
ARRAY_TYPE
void ecpg_free(void *)
Definition: memory.c:13
enum ECPGttype type
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define sprintf
Definition: port.h:194
struct ECPGtype_information_cache * next
struct connection * connection
struct ECPGtype_information_cache * cache_head
enum COMPAT_MODE compat
#define ECPG_IS_ARRAY(X)
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define not_an_array_in_ecpg
char * ecpg_alloc(long, int)
Definition: memory.c:19
void PQclear(PGresult *res)
Definition: fe-exec.c:694
bool ecpg_check_PQresult(PGresult *, int, PGconn *, enum COMPAT_MODE)
Definition: error.c:283
PGconn * connection
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ ecpg_process_output()

bool ecpg_process_output ( struct statement stmt,
bool  clear_result 
)

Definition at line 1664 of file execute.c.

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

Referenced by ecpg_do().

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

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

References ECPGgeneric_varchar::arr, 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(), variable::ind_type, variable::ind_value, ECPGgeneric_varchar::len, 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().

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

◆ ecpg_store_result()

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

Definition at line 306 of file execute.c.

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

Referenced by ecpg_process_output(), and ECPGget_desc().

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

◆ ecpg_type_infocache_push()

static bool ecpg_type_infocache_push ( struct ECPGtype_information_cache **  cache,
int  oid,
enum ARRAY_TYPE  isarray,
int  lineno 
)
static

Definition at line 151 of file execute.c.

References ecpg_alloc(), ECPGtype_information_cache::isarray, ECPGtype_information_cache::next, and ECPGtype_information_cache::oid.

Referenced by ecpg_is_type_an_array().

152 {
153  struct ECPGtype_information_cache *new_entry
154  = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
155 
156  if (new_entry == NULL)
157  return false;
158 
159  new_entry->oid = oid;
160  new_entry->isarray = isarray;
161  new_entry->next = *cache;
162  *cache = new_entry;
163  return true;
164 }
struct ECPGtype_information_cache * next
char * ecpg_alloc(long, int)
Definition: memory.c:19

◆ ECPGdo()

bool ECPGdo ( const int  lineno,
const int  compat,
const int  force_indicator,
const char *  connection_name,
const bool  questionmarks,
const int  st,
const char *  query,
  ... 
)

Definition at line 2276 of file execute.c.

References generate_unaccent_rules::args, and ecpg_do().

Referenced by check_result_of_insert(), close_cur1(), ECPGdo_descriptor(), get_record1(), main(), open_cur1(), openit(), and test().

2277 {
2278  va_list args;
2279  bool ret;
2280 
2281  va_start(args, query);
2282  ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2283  questionmarks, st, query, args);
2284  va_end(args);
2285 
2286  return ret;
2287 }
bool questionmarks
Definition: ecpg.c:19
enum COMPAT_MODE compat
Definition: ecpg.c:25
bool force_indicator
Definition: ecpg.c:18
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: execute.c:2242

◆ ECPGdo_descriptor()

bool ECPGdo_descriptor ( int  line,
const char *  connection,
const char *  descriptor,
const char *  query 
)

Definition at line 2291 of file execute.c.

References ECPG_COMPAT_PGSQL, ECPGdo(), ECPGt_descriptor, ECPGt_EOIT, ECPGt_EORT, and ECPGt_NO_INDICATOR.

2293 {
2294  return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2295  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2296  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
2297 }
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2276

◆ free_statement()

static void free_statement ( struct statement stmt)
static

Definition at line 96 of file execute.c.

References statement::command, ecpg_free(), free_variable(), statement::inlist, statement::name, statement::oldlocale, and statement::outlist.

Referenced by ecpg_do_epilogue().

97 {
98  if (stmt == NULL)
99  return;
100  free_variable(stmt->inlist);
101  free_variable(stmt->outlist);
102  ecpg_free(stmt->command);
103  ecpg_free(stmt->name);
104 #ifdef HAVE_USELOCALE
105  if (stmt->clocale)
106  freelocale(stmt->clocale);
107 #else
108  ecpg_free(stmt->oldlocale);
109 #endif
110  ecpg_free(stmt);
111 }
void ecpg_free(void *)
Definition: memory.c:13
static void free_variable(struct variable *var)
Definition: execute.c:83
char * oldlocale
char * command
struct variable * inlist
char * name
struct variable * outlist

◆ free_variable()

static void free_variable ( struct variable var)
static

Definition at line 83 of file execute.c.

References ecpg_free(), and variable::next.

Referenced by free_statement().

84 {
85  struct variable *var_next;
86 
87  while (var)
88  {
89  var_next = var->next;
90  ecpg_free(var);
91  var = var_next;
92  }
93 }
void ecpg_free(void *)
Definition: memory.c:13
struct variable * next

◆ insert_tobeinserted()

static bool insert_tobeinserted ( int  position,
int  ph_len,
struct statement stmt,
char *  tobeinserted 
)
static

Definition at line 1130 of file execute.c.

References statement::command, ecpg_alloc(), ecpg_free(), and statement::lineno.

Referenced by ecpg_build_params().

1131 {
1132  char *newcopy;
1133 
1134  if (!(newcopy = (char *) ecpg_alloc(strlen(stmt->command)
1135  + strlen(tobeinserted)
1136  + 1, stmt->lineno)))
1137  {
1138  ecpg_free(tobeinserted);
1139  return false;
1140  }
1141 
1142  strcpy(newcopy, stmt->command);
1143  strcpy(newcopy + position - 1, tobeinserted);
1144 
1145  /*
1146  * The strange thing in the second argument is the rest of the string from
1147  * the old string
1148  */
1149  strcat(newcopy,
1150  stmt->command
1151  + position
1152  + ph_len - 1);
1153 
1154  ecpg_free(stmt->command);
1155  stmt->command = newcopy;
1156 
1157  ecpg_free(tobeinserted);
1158  return true;
1159 }
void ecpg_free(void *)
Definition: memory.c:13
char * ecpg_alloc(long, int)
Definition: memory.c:19
char * command

◆ next_insert()

static int next_insert ( char *  text,
int  pos,
bool  questionmarks,
bool  std_strings 
)
static

Definition at line 114 of file execute.c.

References false, and i.

Referenced by ecpg_build_params().

115 {
116  bool string = false;
117  int p = pos;
118 
119  for (; text[p] != '\0'; p++)
120  {
121  if (string && !std_strings && text[p] == '\\') /* escape character */
122  p++;
123  else if (text[p] == '\'')
124  string = string ? false : true;
125  else if (!string)
126  {
127  if (text[p] == '$' && isdigit((unsigned char) text[p + 1]))
128  {
129  /* this can be either a dollar quote or a variable */
130  int i;
131 
132  for (i = p + 1; isdigit((unsigned char) text[i]); i++)
133  /* empty loop body */ ;
134  if (!isalpha((unsigned char) text[i]) &&
135  isascii((unsigned char) text[i]) &&text[i] != '_')
136  /* not dollar delimited quote */
137  return p;
138  }
139  else if (questionmarks && text[p] == '?')
140  {
141  /* also allow old style placeholders */
142  return p;
143  }
144  }
145  }
146 
147  return -1;
148 }
#define false
Definition: c.h:317
bool questionmarks
Definition: ecpg.c:19
int i
Definition: c.h:556

◆ print_param_value()

static void print_param_value ( char *  value,
int  len,
int  is_binary,
int  lineno,
int  nth 
)
static

Definition at line 1079 of file execute.c.

References ecpg_alloc(), ecpg_free(), ecpg_hex_enc_len(), ecpg_hex_encode(), ecpg_log(), and value.

Referenced by ecpg_free_params().

1080 {
1081  char *value_s;
1082  bool malloced = false;
1083 
1084  if (value == NULL)
1085  value_s = "null";
1086  else if (!is_binary)
1087  value_s = value;
1088  else
1089  {
1090  value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1091  if (value_s != NULL)
1092  {
1093  ecpg_hex_encode(value, len, value_s);
1094  value_s[ecpg_hex_enc_len(len)] = '\0';
1095  malloced = true;
1096  }
1097  else
1098  value_s = "no memory for logging of parameter";
1099  }
1100 
1101  ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1102  lineno, nth, value_s);
1103 
1104  if (malloced)
1105  ecpg_free(value_s);
1106 }
void ecpg_free(void *)
Definition: memory.c:13
unsigned ecpg_hex_enc_len(unsigned srclen)
Definition: data.c:128
static struct @145 value
void ecpg_log(const char *format,...) pg_attribute_printf(1
char * ecpg_alloc(long, int)
Definition: memory.c:19
unsigned ecpg_hex_encode(const char *src, unsigned len, char *dst)
Definition: data.c:191

◆ quote_postgres()

static char* quote_postgres ( char *  arg,
bool  quote,
int  lineno 
)
static

Definition at line 40 of file execute.c.

References arg, ecpg_alloc(), ecpg_free(), ESCAPE_STRING_SYNTAX, memmove, and PQescapeString().

Referenced by ecpg_store_input().

41 {
42  char *res;
43  size_t length;
44  size_t escaped_len;
45  size_t buffer_len;
46 
47  /*
48  * if quote is false we just need to store things in a descriptor they
49  * will be quoted once they are inserted in a statement
50  */
51  if (!quote)
52  return arg;
53  else
54  {
55  length = strlen(arg);
56  buffer_len = 2 * length + 1;
57  res = (char *) ecpg_alloc(buffer_len + 3, lineno);
58  if (!res)
59  return res;
60  escaped_len = PQescapeString(res + 1, arg, buffer_len);
61  if (length == escaped_len)
62  {
63  res[0] = res[escaped_len + 1] = '\'';
64  res[escaped_len + 2] = '\0';
65  }
66  else
67  {
68  /*
69  * We don't know if the target database is using
70  * standard_conforming_strings, so we always use E'' strings.
71  */
72  memmove(res + 2, res + 1, escaped_len);
73  res[0] = ESCAPE_STRING_SYNTAX;
74  res[1] = res[escaped_len + 2] = '\'';
75  res[escaped_len + 3] = '\0';
76  }
77  ecpg_free(arg);
78  return res;
79  }
80 }
size_t PQescapeString(char *to, const char *from, size_t length)
Definition: fe-exec.c:3428
void ecpg_free(void *)
Definition: memory.c:13
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1117
#define memmove(d, s, c)
Definition: c.h:1267
char * ecpg_alloc(long, int)
Definition: memory.c:19
void * arg

◆ sprintf_double_value()

static void sprintf_double_value ( char *  ptr,
double  value,
const char *  delim 
)
static

Definition at line 459 of file execute.c.

References isinf(), and sprintf.

Referenced by ecpg_store_input().

460 {
461  if (isnan(value))
462  sprintf(ptr, "%s%s", "NaN", delim);
463  else if (isinf(value))
464  {
465  if (value < 0)
466  sprintf(ptr, "%s%s", "-Infinity", delim);
467  else
468  sprintf(ptr, "%s%s", "Infinity", delim);
469  }
470  else
471  sprintf(ptr, "%.15g%s", value, delim);
472 }
static struct @145 value
#define sprintf
Definition: port.h:194
int isinf(double x)

◆ sprintf_float_value()

static void sprintf_float_value ( char *  ptr,
float  value,
const char *  delim 
)
static

Definition at line 475 of file execute.c.

References isinf(), and sprintf.

Referenced by ecpg_store_input().

476 {
477  if (isnan(value))
478  sprintf(ptr, "%s%s", "NaN", delim);
479  else if (isinf(value))
480  {
481  if (value < 0)
482  sprintf(ptr, "%s%s", "-Infinity", delim);
483  else
484  sprintf(ptr, "%s%s", "Infinity", delim);
485  }
486  else
487  sprintf(ptr, "%.15g%s", value, delim);
488 }
static struct @145 value
#define sprintf
Definition: port.h:194
int isinf(double x)

◆ store_input_from_desc()

static bool store_input_from_desc ( struct statement stmt,
struct descriptor_item desc_item,
char **  tobeinserted 
)
static

Definition at line 1162 of file execute.c.

References variable::arrsize, descriptor_item::data, descriptor_item::data_len, ecpg_alloc(), ecpg_store_input(), ECPGt_char, ECPGt_int, ECPGt_NO_INDICATOR, statement::force_indicator, variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, descriptor_item::indicator, descriptor_item::is_binary, statement::lineno, variable::offset, variable::pointer, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_build_params().

1164 {
1165  struct variable var;
1166 
1167  /*
1168  * In case of binary data, only allocate memory and memcpy because binary
1169  * data have been already stored into desc_item->data with
1170  * ecpg_store_input() at ECPGset_desc().
1171  */
1172  if (desc_item->is_binary)
1173  {
1174  if (!(*tobeinserted = ecpg_alloc(desc_item->data_len, stmt->lineno)))
1175  return false;
1176  memcpy(*tobeinserted, desc_item->data, desc_item->data_len);
1177  return true;
1178  }
1179 
1180  var.type = ECPGt_char;
1181  var.varcharsize = strlen(desc_item->data);
1182  var.value = desc_item->data;
1183  var.pointer = &(desc_item->data);
1184  var.arrsize = 1;
1185  var.offset = 0;
1186 
1187  if (!desc_item->indicator)
1188  {
1189  var.ind_type = ECPGt_NO_INDICATOR;
1190  var.ind_value = var.ind_pointer = NULL;
1191  var.ind_varcharsize = var.ind_arrsize = var.ind_offset = 0;
1192  }
1193  else
1194  {
1195  var.ind_type = ECPGt_int;
1196  var.ind_value = &(desc_item->indicator);
1197  var.ind_pointer = &(var.ind_value);
1198  var.ind_varcharsize = var.ind_arrsize = 1;
1199  var.ind_offset = 0;
1200  }
1201 
1202  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &var, tobeinserted, false))
1203  return false;
1204 
1205  return true;
1206 }
char * ecpg_alloc(long, int)
Definition: memory.c:19
bool force_indicator
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:509