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

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

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

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

Referenced by ecpg_build_params().

◆ ecpg_autostart_transaction()

bool ecpg_autostart_transaction ( struct statement stmt)

Definition at line 1581 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_build_params()

bool ecpg_build_params ( struct statement stmt)

Definition at line 1213 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_do()

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

Definition at line 2245 of file execute.c.

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

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

Referenced by ECPGdo().

◆ ecpg_do_epilogue()

void ecpg_do_epilogue ( struct statement stmt)

Definition at line 2213 of file execute.c.

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

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

Referenced by ecpg_do(), and ecpg_do_prologue().

◆ ecpg_do_prologue()

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

Definition at line 1944 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_execute()

bool ecpg_execute ( struct statement stmt)

Definition at line 1602 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_free_params()

void ecpg_free_params ( struct statement stmt,
bool  print 
)

Definition at line 1106 of file execute.c.

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

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

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

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

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

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

Referenced by ecpg_store_result().

◆ ecpg_process_output()

bool ecpg_process_output ( struct statement stmt,
bool  clear_result 
)

Definition at line 1671 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_store_input()

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

Definition at line 506 of file execute.c.

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

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

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

◆ ecpg_store_result()

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

Definition at line 303 of file execute.c.

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

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

Referenced by ecpg_process_output(), and ECPGget_desc().

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

149 {
150  struct ECPGtype_information_cache *new_entry
151  = (struct ECPGtype_information_cache *) ecpg_alloc(sizeof(struct ECPGtype_information_cache), lineno);
152 
153  if (new_entry == NULL)
154  return false;
155 
156  new_entry->oid = oid;
157  new_entry->isarray = isarray;
158  new_entry->next = *cache;
159  *cache = new_entry;
160  return true;
161 }

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

2280 {
2281  va_list args;
2282  bool ret;
2283 
2284  va_start(args, query);
2285  ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2286  questionmarks, st, query, args);
2287  va_end(args);
2288 
2289  return ret;
2290 }
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:2245
va_end(args)
va_start(args, fmt)

References generate_unaccent_rules::args, compat, ecpg_do(), force_indicator, statement::lineno, questionmarks, va_end(), and va_start().

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

◆ ECPGdo_descriptor()

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

Definition at line 2294 of file execute.c.

2296 {
2297  return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2298  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2299  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
2300 }
@ ECPG_COMPAT_PGSQL
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:2279

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

◆ free_statement()

static void free_statement ( struct statement stmt)
static

Definition at line 96 of file execute.c.

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 #ifndef HAVE_USELOCALE
105  ecpg_free(stmt->oldlocale);
106 #endif
107  ecpg_free(stmt);
108 }
static void free_variable(struct variable *var)
Definition: execute.c:83

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

Referenced by ecpg_do_epilogue().

◆ free_variable()

static void free_variable ( struct variable var)
static

Definition at line 83 of file execute.c.

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 }

References ecpg_free(), and variable::next.

Referenced by free_statement().

◆ insert_tobeinserted()

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

Definition at line 1127 of file execute.c.

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

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

Referenced by ecpg_build_params().

◆ next_insert()

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

Definition at line 111 of file execute.c.

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

References false, i, and questionmarks.

Referenced by ecpg_build_params().

◆ print_param_value()

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

Definition at line 1076 of file execute.c.

1077 {
1078  char *value_s;
1079  bool malloced = false;
1080 
1081  if (value == NULL)
1082  value_s = "null";
1083  else if (!is_binary)
1084  value_s = value;
1085  else
1086  {
1087  value_s = ecpg_alloc(ecpg_hex_enc_len(len) + 1, lineno);
1088  if (value_s != NULL)
1089  {
1090  ecpg_hex_encode(value, len, value_s);
1091  value_s[ecpg_hex_enc_len(len)] = '\0';
1092  malloced = true;
1093  }
1094  else
1095  value_s = "no memory for logging of parameter";
1096  }
1097 
1098  ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n",
1099  lineno, nth, value_s);
1100 
1101  if (malloced)
1102  ecpg_free(value_s);
1103 }

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

Referenced by ecpg_free_params().

◆ quote_postgres()

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

Definition at line 40 of file execute.c.

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);
74  res[1] = res[escaped_len + 2] = '\'';
75  res[escaped_len + 3] = '\0';
76  }
77  ecpg_free(arg);
78  return res;
79  }
80 }
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1105
size_t PQescapeString(char *to, const char *from, size_t length)
Definition: fe-exec.c:4000
void * arg

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

Referenced by ecpg_store_input().

◆ sprintf_double_value()

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

Definition at line 456 of file execute.c.

457 {
458  if (isnan(value))
459  sprintf(ptr, "%s%s", "NaN", delim);
460  else if (isinf(value))
461  {
462  if (value < 0)
463  sprintf(ptr, "%s%s", "-Infinity", delim);
464  else
465  sprintf(ptr, "%s%s", "Infinity", delim);
466  }
467  else
468  sprintf(ptr, "%.15g%s", value, delim);
469 }

References sprintf, and value.

Referenced by ecpg_store_input().

◆ sprintf_float_value()

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

Definition at line 472 of file execute.c.

473 {
474  if (isnan(value))
475  sprintf(ptr, "%s%s", "NaN", delim);
476  else if (isinf(value))
477  {
478  if (value < 0)
479  sprintf(ptr, "%s%s", "-Infinity", delim);
480  else
481  sprintf(ptr, "%s%s", "Infinity", delim);
482  }
483  else
484  sprintf(ptr, "%.15g%s", value, delim);
485 }

References sprintf, and value.

Referenced by ecpg_store_input().

◆ store_input_from_desc()

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

Definition at line 1159 of file execute.c.

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

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