PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
execute.c File Reference
#include "postgres_fe.h"
#include <float.h>
#include <math.h>
#include "pg_type.h"
#include "ecpgtype.h"
#include "ecpglib.h"
#include "ecpgerrno.h"
#include "extern.h"
#include "sqlca.h"
#include "sqlda-native.h"
#include "sqlda-compat.h"
#include "sql3types.h"
#include "pgtypes_numeric.h"
#include "pgtypes_date.h"
#include "pgtypes_timestamp.h"
#include "pgtypes_interval.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)
 
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)
 
bool ecpg_store_input (const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
 
void ecpg_free_params (struct statement *stmt, bool print)
 
static bool insert_tobeinserted (int position, int ph_len, struct statement *stmt, 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

#define not_an_array_in_ecpg   ECPG_ARRAY_NONE

Referenced by ecpg_is_type_an_array().

#define POSTGRES_ECPG_INTERNAL

Definition at line 16 of file execute.c.

Function Documentation

bool ecpg_autostart_transaction ( struct statement stmt)

Definition at line 1405 of file execute.c.

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

Referenced by ecpg_do().

1406 {
1408  {
1409  stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1410  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1411  {
1412  ecpg_free_params(stmt, false);
1413  return false;
1414  }
1415  PQclear(stmt->results);
1416  stmt->results = NULL;
1417  }
1418  return true;
1419 }
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1052
PGresult * results
Definition: extern.h:66
bool autocommit
Definition: extern.h:83
struct connection * connection
Definition: extern.h:56
enum COMPAT_MODE compat
Definition: extern.h:57
int lineno
Definition: extern.h:53
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6052
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
bool ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
Definition: error.c:283
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
bool ecpg_build_params ( struct statement stmt)

Definition at line 1107 of file execute.c.

References variable::arrsize, statement::command, statement::compat, descriptor::count, descriptor_item::data, ecpg_alloc(), ecpg_find_desc(), 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, ECPGt_char, ECPGt_char_variable, ECPGt_descriptor, ECPGt_int, ECPGt_NO_INDICATOR, ECPGt_short, ECPGt_sqlda, ECPGt_varchar, statement::force_indicator, i, variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, descriptor_item::indicator, INFORMIX_MODE, statement::inlist, insert_tobeinserted(), descriptor::items, statement::lineno, descriptor_item::next, variable::next, next_insert(), statement::nparams, NULL, descriptor_item::num, variable::offset, statement::paramvalues, variable::pointer, statement::questionmarks, snprintf(), 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, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_do().

1108 {
1109  struct variable *var;
1110  int desc_counter = 0;
1111  int position = 0;
1112 
1113  /*
1114  * If the type is one of the fill in types then we take the argument and
1115  * enter it to our parameter array at the first position. Then if there
1116  * are any more fill in types we add more parameters.
1117  */
1118  var = stmt->inlist;
1119  while (var)
1120  {
1121  char *tobeinserted;
1122  int counter = 1;
1123 
1124  tobeinserted = NULL;
1125 
1126  /*
1127  * A descriptor is a special case since it contains many variables but
1128  * is listed only once.
1129  */
1130  if (var->type == ECPGt_descriptor)
1131  {
1132  /*
1133  * We create an additional variable list here, so the same logic
1134  * applies.
1135  */
1136  struct variable desc_inlist;
1137  struct descriptor *desc;
1138  struct descriptor_item *desc_item;
1139 
1140  desc = ecpg_find_desc(stmt->lineno, var->pointer);
1141  if (desc == NULL)
1142  return false;
1143 
1144  desc_counter++;
1145  for (desc_item = desc->items; desc_item; desc_item = desc_item->next)
1146  {
1147  if (desc_item->num == desc_counter)
1148  {
1149  desc_inlist.type = ECPGt_char;
1150  desc_inlist.value = desc_item->data;
1151  desc_inlist.pointer = &(desc_item->data);
1152  desc_inlist.varcharsize = strlen(desc_item->data);
1153  desc_inlist.arrsize = 1;
1154  desc_inlist.offset = 0;
1155  if (!desc_item->indicator)
1156  {
1157  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1158  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1159  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1160  }
1161  else
1162  {
1163  desc_inlist.ind_type = ECPGt_int;
1164  desc_inlist.ind_value = &(desc_item->indicator);
1165  desc_inlist.ind_pointer = &(desc_inlist.ind_value);
1166  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1167  desc_inlist.ind_offset = 0;
1168  }
1169  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1170  return false;
1171 
1172  break;
1173  }
1174  }
1175  if (desc->count == desc_counter)
1176  desc_counter = 0;
1177  }
1178  else if (var->type == ECPGt_sqlda)
1179  {
1180  if (INFORMIX_MODE(stmt->compat))
1181  {
1182  struct sqlda_compat *sqlda = *(struct sqlda_compat **) var->pointer;
1183  struct variable desc_inlist;
1184  int i;
1185 
1186  if (sqlda == NULL)
1187  return false;
1188 
1189  desc_counter++;
1190  for (i = 0; i < sqlda->sqld; i++)
1191  {
1192  if (i + 1 == desc_counter)
1193  {
1194  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1195  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1196  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1197  switch (desc_inlist.type)
1198  {
1199  case ECPGt_char:
1200  case ECPGt_varchar:
1201  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1202  break;
1203  default:
1204  desc_inlist.varcharsize = 0;
1205  break;
1206  }
1207  desc_inlist.arrsize = 1;
1208  desc_inlist.offset = 0;
1209  if (sqlda->sqlvar[i].sqlind)
1210  {
1211  desc_inlist.ind_type = ECPGt_short;
1212  /* ECPG expects indicator value < 0 */
1213  if (*(sqlda->sqlvar[i].sqlind))
1214  *(sqlda->sqlvar[i].sqlind) = -1;
1215  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1216  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1217  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1218  desc_inlist.ind_offset = 0;
1219  }
1220  else
1221  {
1222  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1223  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1224  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1225  }
1226  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1227  return false;
1228 
1229  break;
1230  }
1231  }
1232  if (sqlda->sqld == desc_counter)
1233  desc_counter = 0;
1234  }
1235  else
1236  {
1237  struct sqlda_struct *sqlda = *(struct sqlda_struct **) var->pointer;
1238  struct variable desc_inlist;
1239  int i;
1240 
1241  if (sqlda == NULL)
1242  return false;
1243 
1244  desc_counter++;
1245  for (i = 0; i < sqlda->sqln; i++)
1246  {
1247  if (i + 1 == desc_counter)
1248  {
1249  desc_inlist.type = sqlda->sqlvar[i].sqltype;
1250  desc_inlist.value = sqlda->sqlvar[i].sqldata;
1251  desc_inlist.pointer = &(sqlda->sqlvar[i].sqldata);
1252  switch (desc_inlist.type)
1253  {
1254  case ECPGt_char:
1255  case ECPGt_varchar:
1256  desc_inlist.varcharsize = strlen(sqlda->sqlvar[i].sqldata);
1257  break;
1258  default:
1259  desc_inlist.varcharsize = 0;
1260  break;
1261  }
1262  desc_inlist.arrsize = 1;
1263  desc_inlist.offset = 0;
1264  if (sqlda->sqlvar[i].sqlind)
1265  {
1266  desc_inlist.ind_type = ECPGt_short;
1267  /* ECPG expects indicator value < 0 */
1268  if (*(sqlda->sqlvar[i].sqlind))
1269  *(sqlda->sqlvar[i].sqlind) = -1;
1270  desc_inlist.ind_value = sqlda->sqlvar[i].sqlind;
1271  desc_inlist.ind_pointer = &(sqlda->sqlvar[i].sqlind);
1272  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
1273  desc_inlist.ind_offset = 0;
1274  }
1275  else
1276  {
1277  desc_inlist.ind_type = ECPGt_NO_INDICATOR;
1278  desc_inlist.ind_value = desc_inlist.ind_pointer = NULL;
1279  desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = desc_inlist.ind_offset = 0;
1280  }
1281  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, false))
1282  return false;
1283 
1284  break;
1285  }
1286  }
1287  if (sqlda->sqln == desc_counter)
1288  desc_counter = 0;
1289  }
1290 
1291  }
1292  else
1293  {
1294  if (!ecpg_store_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, false))
1295  return false;
1296  }
1297 
1298  /*
1299  * now tobeinserted points to an area that contains the next
1300  * parameter; now find the position in the string where it belongs
1301  */
1302  if ((position = next_insert(stmt->command, position, stmt->questionmarks) + 1) == 0)
1303  {
1304  /*
1305  * We have an argument but we dont have the matched up placeholder
1306  * in the string
1307  */
1310  NULL);
1311  ecpg_free_params(stmt, false);
1312  return false;
1313  }
1314 
1315  /*
1316  * if var->type=ECPGt_char_variable we have a dynamic cursor we have
1317  * to simulate a dynamic cursor because there is no backend
1318  * functionality for it
1319  */
1320  if (var->type == ECPGt_char_variable)
1321  {
1322  int ph_len = (stmt->command[position] == '?') ? strlen("?") : strlen("$1");
1323 
1324  if (!insert_tobeinserted(position, ph_len, stmt, tobeinserted))
1325  {
1326  ecpg_free_params(stmt, false);
1327  return false;
1328  }
1329  tobeinserted = NULL;
1330  }
1331 
1332  /*
1333  * if the placeholder is '$0' we have to replace it on the client side
1334  * this is for places we want to support variables at that are not
1335  * supported in the backend
1336  */
1337  else if (stmt->command[position] == '0')
1338  {
1339  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1340  {
1341  ecpg_free_params(stmt, false);
1342  return false;
1343  }
1344  tobeinserted = NULL;
1345  }
1346  else
1347  {
1348  char **paramvalues;
1349 
1350  if (!(paramvalues = (char **) ecpg_realloc(stmt->paramvalues, sizeof(char *) * (stmt->nparams + 1), stmt->lineno)))
1351  {
1352  ecpg_free_params(stmt, false);
1353  return false;
1354  }
1355 
1356  stmt->nparams++;
1357  stmt->paramvalues = paramvalues;
1358  stmt->paramvalues[stmt->nparams - 1] = tobeinserted;
1359 
1360  /* let's see if this was an old style placeholder */
1361  if (stmt->command[position] == '?')
1362  {
1363  /* yes, replace with new style */
1364  int buffersize = sizeof(int) * CHAR_BIT * 10 / 3; /* a rough guess of the
1365  * size we need */
1366 
1367  if (!(tobeinserted = (char *) ecpg_alloc(buffersize, stmt->lineno)))
1368  {
1369  ecpg_free_params(stmt, false);
1370  return false;
1371  }
1372 
1373  snprintf(tobeinserted, buffersize, "$%d", counter++);
1374 
1375  if (!insert_tobeinserted(position, 2, stmt, tobeinserted))
1376  {
1377  ecpg_free_params(stmt, false);
1378  return false;
1379  }
1380  tobeinserted = NULL;
1381  }
1382  }
1383 
1384  if (desc_counter == 0)
1385  var = var->next;
1386  }
1387 
1388  /* Check if there are unmatched things left. */
1389  if (next_insert(stmt->command, position, stmt->questionmarks) >= 0)
1390  {
1393  ecpg_free_params(stmt, false);
1394  return false;
1395  }
1396 
1397  return true;
1398 }
static bool insert_tobeinserted(int position, int ph_len, struct statement *stmt, char *tobeinserted)
Definition: execute.c:1068
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1052
#define INFORMIX_MODE(X)
Definition: extern.h:23
char * data
Definition: extern.h:102
void * pointer
Definition: extern.h:115
enum ECPGttype type
Definition: extern.h:113
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
#define ECPG_TOO_MANY_ARGUMENTS
Definition: ecpgerrno.h:19
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * sqldata
Definition: sqlda-native.h:28
struct variable * next
Definition: extern.h:125
char * sqldata
Definition: sqlda-compat.h:12
enum COMPAT_MODE compat
Definition: extern.h:57
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:799
int lineno
Definition: extern.h:53
char * ecpg_alloc(long, int)
Definition: memory.c:19
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
int count
Definition: extern.h:95
char * command
Definition: extern.h:54
struct variable * inlist
Definition: extern.h:61
#define NULL
Definition: c.h:229
#define ECPG_TOO_FEW_ARGUMENTS
Definition: ecpgerrno.h:20
bool force_indicator
Definition: extern.h:58
struct descriptor_item * items
Definition: extern.h:96
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_PARAMETERS
Definition: extern.h:201
char ** paramvalues
Definition: extern.h:65
short * sqlind
Definition: sqlda-native.h:29
struct descriptor_item * next
Definition: extern.h:108
bool questionmarks
Definition: extern.h:60
int nparams
Definition: extern.h:64
int i
bool ecpg_store_input(const int lineno, const bool force_indicator, const struct variable *var, char **tobeinserted_p, bool quote)
Definition: execute.c:494
short * sqlind
Definition: sqlda-compat.h:13
static int next_insert(char *text, int pos, bool questionmarks)
Definition: execute.c:111
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40
char * ecpg_realloc(void *, long, int)
Definition: memory.c:33
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 1996 of file execute.c.

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

Referenced by ECPGdo().

1997 {
1998  struct statement *stmt = NULL;
1999 
2000  if (!ecpg_do_prologue(lineno, compat, force_indicator, connection_name,
2002  query, args, &stmt))
2003  goto fail;
2004 
2005  if (!ecpg_build_params(stmt))
2006  goto fail;
2007 
2008  if (!ecpg_autostart_transaction(stmt))
2009  goto fail;
2010 
2011  if (!ecpg_execute(stmt))
2012  goto fail;
2013 
2014  if (!ecpg_process_output(stmt, true))
2015  goto fail;
2016 
2017  ecpg_do_epilogue(stmt);
2018  return true;
2019 
2020 fail:
2021  ecpg_do_epilogue(stmt);
2022  return false;
2023 }
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition: execute.c:1474
bool ecpg_autostart_transaction(struct statement *stmt)
Definition: execute.c:1405
ECPG_statement_type
Definition: ecpgtype.h:94
bool ecpg_build_params(struct statement *stmt)
Definition: execute.c:1107
bool ecpg_execute(struct statement *stmt)
Definition: execute.c:1426
int lineno
Definition: extern.h:53
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:1747
#define NULL
Definition: c.h:229
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:1979
bool force_indicator
Definition: ecpg.c:18
void ecpg_do_epilogue ( struct statement stmt)

Definition at line 1979 of file execute.c.

References free_statement(), NULL, and statement::oldlocale.

Referenced by ecpg_do(), and ecpg_do_prologue().

1980 {
1981  if (stmt == NULL)
1982  return;
1983 
1984  if (stmt->oldlocale)
1985  setlocale(LC_NUMERIC, stmt->oldlocale);
1986 
1987  free_statement(stmt);
1988 }
static void free_statement(struct statement *stmt)
Definition: execute.c:98
char * oldlocale
Definition: extern.h:63
#define NULL
Definition: c.h:229
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 1747 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(), ECPGst_execute, 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, statement::name, connection::name, variable::next, NULL, variable::offset, statement::oldlocale, statement::outlist, variable::pointer, questionmarks, statement::questionmarks, statement::statement_type, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_do().

1751 {
1752  struct statement *stmt;
1753  struct connection *con;
1754  enum ECPGttype type;
1755  struct variable **list;
1756  char *prepname;
1757 
1758  *stmt_out = NULL;
1759 
1760  if (!query)
1761  {
1763  return false;
1764  }
1765 
1766  stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno);
1767 
1768  if (stmt == NULL)
1769  return false;
1770 
1771  /*
1772  * Make sure we do NOT honor the locale for numeric input/output since the
1773  * database wants the standard decimal point
1774  */
1775  stmt->oldlocale = ecpg_strdup(setlocale(LC_NUMERIC, NULL), lineno);
1776  if (stmt->oldlocale == NULL)
1777  {
1778  ecpg_do_epilogue(stmt);
1779  return false;
1780  }
1781  setlocale(LC_NUMERIC, "C");
1782 
1783 #ifdef ENABLE_THREAD_SAFETY
1784  ecpg_pthreads_init();
1785 #endif
1786 
1787  con = ecpg_get_connection(connection_name);
1788 
1789  if (!ecpg_init(con, connection_name, lineno))
1790  {
1791  ecpg_do_epilogue(stmt);
1792  return false;
1793  }
1794 
1795  /*
1796  * If statement type is ECPGst_prepnormal we are supposed to prepare the
1797  * statement before executing them
1798  */
1800  {
1801  if (!ecpg_auto_prepare(lineno, connection_name, compat, &prepname, query))
1802  {
1803  ecpg_do_epilogue(stmt);
1804  return false;
1805  }
1806 
1807  /*
1808  * statement is now prepared, so instead of the query we have to
1809  * execute the name
1810  */
1811  stmt->command = prepname;
1813  }
1814  else
1815  stmt->command = ecpg_strdup(query, lineno);
1816 
1817  stmt->name = NULL;
1818 
1820  {
1821  /* if we have an EXECUTE command, only the name is send */
1822  char *command = ecpg_prepared(stmt->command, con);
1823 
1824  if (command)
1825  {
1826  stmt->name = stmt->command;
1827  stmt->command = ecpg_strdup(command, lineno);
1828  }
1829  else
1830  {
1832  ecpg_do_epilogue(stmt);
1833  return (false);
1834  }
1835  }
1836 
1837  stmt->connection = con;
1838  stmt->lineno = lineno;
1839  stmt->compat = compat;
1841  stmt->questionmarks = questionmarks;
1843 
1844  /*------
1845  * create a list of variables
1846  *
1847  * The variables are listed with input variables preceding outputvariables
1848  * The end of each group is marked by an end marker. per variable we list:
1849  *
1850  * type - as defined in ecpgtype.h
1851  * value - where to store the data
1852  * varcharsize - length of string in case we have a stringvariable, else 0
1853  * arraysize - 0 for pointer (we don't know the size of the array), 1 for
1854  * simple variable, size for arrays
1855  * offset - offset between ith and (i+1)th entry in an array, normally
1856  * that means sizeof(type)
1857  * ind_type - type of indicator variable
1858  * ind_value - pointer to indicator variable
1859  * ind_varcharsize - empty
1860  * ind_arraysize - arraysize of indicator array
1861  * ind_offset - indicator offset
1862  *------
1863  */
1864 
1865  list = &(stmt->inlist);
1866 
1867  type = va_arg(args, enum ECPGttype);
1868 
1869  while (type != ECPGt_EORT)
1870  {
1871  if (type == ECPGt_EOIT)
1872  list = &(stmt->outlist);
1873  else
1874  {
1875  struct variable *var,
1876  *ptr;
1877 
1878  if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno)))
1879  {
1880  ecpg_do_epilogue(stmt);
1881  return false;
1882  }
1883 
1884  var->type = type;
1885  var->pointer = va_arg(args, char *);
1886 
1887  var->varcharsize = va_arg(args, long);
1888  var->arrsize = va_arg(args, long);
1889  var->offset = va_arg(args, long);
1890 
1891  /*
1892  * Unknown array size means pointer to an array. Unknown
1893  * varcharsize usually also means pointer. But if the type is
1894  * character and the array size is known, it is an array of
1895  * pointers to char, so use var->pointer as it is.
1896  */
1897  if (var->arrsize == 0 ||
1898  (var->varcharsize == 0 && ((var->type != ECPGt_char && var->type != ECPGt_unsigned_char) || (var->arrsize <= 1))))
1899  var->value = *((char **) (var->pointer));
1900  else
1901  var->value = var->pointer;
1902 
1903  /*
1904  * negative values are used to indicate an array without given
1905  * bounds
1906  */
1907  /* reset to zero for us */
1908  if (var->arrsize < 0)
1909  var->arrsize = 0;
1910  if (var->varcharsize < 0)
1911  var->varcharsize = 0;
1912 
1913  var->next = NULL;
1914 
1915  var->ind_type = va_arg(args, enum ECPGttype);
1916  var->ind_pointer = va_arg(args, char *);
1917  var->ind_varcharsize = va_arg(args, long);
1918  var->ind_arrsize = va_arg(args, long);
1919  var->ind_offset = va_arg(args, long);
1920 
1921  if (var->ind_type != ECPGt_NO_INDICATOR
1922  && (var->ind_arrsize == 0 || var->ind_varcharsize == 0))
1923  var->ind_value = *((char **) (var->ind_pointer));
1924  else
1925  var->ind_value = var->ind_pointer;
1926 
1927  /*
1928  * negative values are used to indicate an array without given
1929  * bounds
1930  */
1931  /* reset to zero for us */
1932  if (var->ind_arrsize < 0)
1933  var->ind_arrsize = 0;
1934  if (var->ind_varcharsize < 0)
1935  var->ind_varcharsize = 0;
1936 
1937  /* if variable is NULL, the statement hasn't been prepared */
1938  if (var->pointer == NULL)
1939  {
1941  ecpg_free(var);
1942  ecpg_do_epilogue(stmt);
1943  return false;
1944  }
1945 
1946  for (ptr = *list; ptr && ptr->next; ptr = ptr->next)
1947  ;
1948 
1949  if (ptr == NULL)
1950  *list = var;
1951  else
1952  ptr->next = var;
1953  }
1954 
1955  type = va_arg(args, enum ECPGttype);
1956  }
1957 
1958  /* are we connected? */
1959  if (con == NULL || con->connection == NULL)
1960  {
1961  ecpg_raise(lineno, ECPG_NOT_CONN, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, (con) ? con->name : ecpg_gettext("<empty>"));
1962  ecpg_do_epilogue(stmt);
1963  return false;
1964  }
1965 
1966  /* initialize auto_mem struct */
1968 
1969  *stmt_out = stmt;
1970 
1971  return true;
1972 }
long ind_arrsize
Definition: extern.h:123
bool ecpg_init(const struct connection *, const char *, const int)
Definition: misc.c:105
#define ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
Definition: extern.h:220
#define ECPG_NOT_CONN
Definition: ecpgerrno.h:37
void ecpg_free(void *)
Definition: memory.c:13
#define ECPG_INVALID_STMT
Definition: ecpgerrno.h:39
char * name
Definition: extern.h:81
enum ECPG_statement_type statement_type
Definition: extern.h:59
void * pointer
Definition: extern.h:115
enum ECPGttype type
Definition: extern.h:113
char * ecpg_strdup(const char *, int)
Definition: memory.c:47
struct variable * next
Definition: extern.h:125
#define ECPG_EMPTY
Definition: ecpgerrno.h:30
char * oldlocale
Definition: extern.h:63
struct connection * connection
Definition: extern.h:56
enum COMPAT_MODE compat
Definition: extern.h:57
int lineno
Definition: extern.h:53
void * value
Definition: extern.h:114
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:468
void ecpg_clear_auto_mem(void)
Definition: memory.c:158
bool questionmarks
Definition: ecpg.c:19
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
enum COMPAT_MODE compat
Definition: ecpg.c:25
char * ecpg_prepared(const char *, struct connection *)
Definition: prepare.c:295
#define ecpg_gettext(x)
Definition: ecpglib.h:18
void * ind_pointer
Definition: extern.h:121
long offset
Definition: extern.h:118
char * command
Definition: extern.h:54
struct variable * inlist
Definition: extern.h:61
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
long ind_varcharsize
Definition: extern.h:122
bool force_indicator
Definition: extern.h:58
enum ECPGttype ind_type
Definition: extern.h:119
#define ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME
Definition: extern.h:212
char * name
Definition: extern.h:55
void ecpg_do_epilogue(struct statement *stmt)
Definition: execute.c:1979
long ind_offset
Definition: extern.h:124
tuple list
Definition: sort-test.py:11
bool questionmarks
Definition: extern.h:60
long arrsize
Definition: extern.h:117
struct connection * ecpg_get_connection(const char *connection_name)
Definition: connect.c:74
bool force_indicator
Definition: ecpg.c:18
void * ind_value
Definition: extern.h:120
long varcharsize
Definition: extern.h:116
struct variable * outlist
Definition: extern.h:62
ECPGttype
Definition: ecpgtype.h:41
bool ecpg_execute ( struct statement stmt)

Definition at line 1426 of file execute.c.

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

Referenced by ecpg_do().

1427 {
1428  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);
1429  if (stmt->statement_type == ECPGst_execute)
1430  {
1431  stmt->results = PQexecPrepared(stmt->connection->connection, stmt->name, stmt->nparams, (const char *const *) stmt->paramvalues, NULL, NULL, 0);
1432  ecpg_log("ecpg_execute on line %d: using PQexecPrepared for \"%s\"\n", stmt->lineno, stmt->command);
1433  }
1434  else
1435  {
1436  if (stmt->nparams == 0)
1437  {
1438  stmt->results = PQexec(stmt->connection->connection, stmt->command);
1439  ecpg_log("ecpg_execute on line %d: using PQexec\n", stmt->lineno);
1440  }
1441  else
1442  {
1443  stmt->results = PQexecParams(stmt->connection->connection, stmt->command, stmt->nparams, NULL, (const char *const *) stmt->paramvalues, NULL, NULL, 0);
1444  ecpg_log("ecpg_execute on line %d: using PQexecParams\n", stmt->lineno);
1445  }
1446  }
1447 
1448  ecpg_free_params(stmt, true);
1449 
1450  if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1451  return false;
1452 
1453  return true;
1454 }
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:1907
void ecpg_free_params(struct statement *stmt, bool print)
Definition: execute.c:1052
char * name
Definition: extern.h:81
enum ECPG_statement_type statement_type
Definition: extern.h:59
PGresult * results
Definition: extern.h:66
void ecpg_log(const char *format,...) pg_attribute_printf(1
struct connection * connection
Definition: extern.h:56
enum COMPAT_MODE compat
Definition: extern.h:57
int lineno
Definition: extern.h:53
char * command
Definition: extern.h:54
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
bool ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
Definition: error.c:283
char * name
Definition: extern.h:55
char ** paramvalues
Definition: extern.h:65
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:1860
int nparams
Definition: extern.h:64
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void ecpg_free_params ( struct statement stmt,
bool  print 
)

Definition at line 1052 of file execute.c.

References ecpg_free(), ecpg_log(), statement::lineno, statement::nparams, NULL, and statement::paramvalues.

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

1053 {
1054  int n;
1055 
1056  for (n = 0; n < stmt->nparams; n++)
1057  {
1058  if (print)
1059  ecpg_log("ecpg_free_params on line %d: parameter %d = %s\n", stmt->lineno, n + 1, stmt->paramvalues[n] ? stmt->paramvalues[n] : "null");
1060  ecpg_free(stmt->paramvalues[n]);
1061  }
1062  ecpg_free(stmt->paramvalues);
1063  stmt->paramvalues = NULL;
1064  stmt->nparams = 0;
1065 }
void print(const void *obj)
Definition: print.c:35
void ecpg_free(void *)
Definition: memory.c:13
void ecpg_log(const char *format,...) pg_attribute_printf(1
int lineno
Definition: extern.h:53
#define NULL
Definition: c.h:229
char ** paramvalues
Definition: extern.h:65
int nparams
Definition: extern.h:64
static enum ARRAY_TYPE ecpg_is_type_an_array ( int  type,
const struct statement stmt,
const struct variable var 
)
static

Definition at line 164 of file execute.c.

References ABSTIMEOID, BOOLOID, BOXOID, BPCHAROID, BYTEAOID, connection::cache_head, CASHOID, CHAROID, CIDOID, CIDROID, CIRCLEOID, statement::compat, statement::connection, connection::connection, DATEOID, 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(), FLOAT4OID, FLOAT8OID, INETOID, INT2OID, INT2VECTOROID, INT4OID, INT8OID, INTERVALOID, ECPGtype_information_cache::isarray, statement::lineno, LINEOID, LSEGOID, NAMEOID, ECPGtype_information_cache::next, not_an_array_in_ecpg, NULL, NUMERICOID, ECPGtype_information_cache::oid, OIDOID, OIDVECTOROID, PATHOID, PGRES_TUPLES_OK, POINTOID, POLYGONOID, PQclear(), PQexec(), PQgetvalue(), PQntuples(), PQresultStatus(), REGPROCOID, RELTIMEOID, SQL3_CHARACTER, SQL3_CHARACTER_VARYING, TEXTOID, TIDOID, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TINTERVALOID, variable::type, UNKNOWNOID, VARBITOID, VARCHAROID, XIDOID, and ZPBITOID.

Referenced by ecpg_store_result().

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 */
181  return (ECPG_ARRAY_ERROR);
183  return (ECPG_ARRAY_ERROR);
185  return (ECPG_ARRAY_ERROR);
187  return (ECPG_ARRAY_ERROR);
189  return (ECPG_ARRAY_ERROR);
191  return (ECPG_ARRAY_ERROR);
193  return (ECPG_ARRAY_ERROR);
195  return (ECPG_ARRAY_ERROR);
197  return (ECPG_ARRAY_ERROR);
199  return (ECPG_ARRAY_ERROR);
201  return (ECPG_ARRAY_ERROR);
203  return (ECPG_ARRAY_ERROR);
205  return (ECPG_ARRAY_ERROR);
207  return (ECPG_ARRAY_ERROR);
209  return (ECPG_ARRAY_ERROR);
211  return (ECPG_ARRAY_ERROR);
213  return (ECPG_ARRAY_ERROR);
215  return (ECPG_ARRAY_ERROR);
217  return (ECPG_ARRAY_ERROR);
219  return (ECPG_ARRAY_ERROR);
221  return (ECPG_ARRAY_ERROR);
223  return (ECPG_ARRAY_ERROR);
225  return (ECPG_ARRAY_ERROR);
227  return (ECPG_ARRAY_ERROR);
229  return (ECPG_ARRAY_ERROR);
231  return (ECPG_ARRAY_ERROR);
233  return (ECPG_ARRAY_ERROR);
235  return (ECPG_ARRAY_ERROR);
237  return (ECPG_ARRAY_ERROR);
239  return (ECPG_ARRAY_ERROR);
241  return (ECPG_ARRAY_ERROR);
243  return (ECPG_ARRAY_ERROR);
245  return (ECPG_ARRAY_ERROR);
247  return (ECPG_ARRAY_ERROR);
249  return (ECPG_ARRAY_ERROR);
251  return (ECPG_ARRAY_ERROR);
253  return (ECPG_ARRAY_ERROR);
255  return (ECPG_ARRAY_ERROR);
257  return (ECPG_ARRAY_ERROR);
259  return (ECPG_ARRAY_ERROR);
261  return (ECPG_ARRAY_ERROR);
263  return (ECPG_ARRAY_ERROR);
264  }
265 
266  for (cache_entry = (stmt->connection->cache_head); cache_entry != NULL; cache_entry = cache_entry->next)
267  {
268  if (cache_entry->oid == type)
269  return cache_entry->isarray;
270  }
271 
272  array_query = (char *) ecpg_alloc(strlen("select typlen from pg_type where oid= and typelem<>0") + 11, stmt->lineno);
273  if (array_query == NULL)
274  return (ECPG_ARRAY_ERROR);
275 
276  sprintf(array_query, "select typlen from pg_type where oid=%d and typelem<>0", type);
277  query = PQexec(stmt->connection->connection, array_query);
278  ecpg_free(array_query);
279  if (!ecpg_check_PQresult(query, stmt->lineno, stmt->connection->connection, stmt->compat))
280  return (ECPG_ARRAY_ERROR);
281  else if (PQresultStatus(query) == PGRES_TUPLES_OK)
282  {
283  if (PQntuples(query) == 0)
284  isarray = ECPG_ARRAY_NONE;
285  else
286  {
287  isarray = (atol((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
288  if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
290  {
291  /*
292  * arrays of character strings are not yet implemented
293  */
294  isarray = ECPG_ARRAY_NONE;
295  }
296  }
297  PQclear(query);
298  }
299  else
300  return (ECPG_ARRAY_ERROR);
301 
302  ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
303  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");
304  return isarray;
305 }
#define CIDROID
Definition: pg_type.h:451
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define TIMEOID
Definition: pg_type.h:514
#define BPCHAROID
Definition: pg_type.h:504
#define DATEOID
Definition: pg_type.h:511
#define INT2VECTOROID
Definition: pg_type.h:312
int ecpg_dynamic_type(Oid)
Definition: typename.c:71
#define NAMEOID
Definition: pg_type.h:300
ARRAY_TYPE
Definition: extern.h:25
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, int oid, enum ARRAY_TYPE isarray, int lineno)
Definition: execute.c:148
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void ecpg_free(void *)
Definition: memory.c:13
#define OIDOID
Definition: pg_type.h:328
#define TEXTOID
Definition: pg_type.h:324
#define INETOID
Definition: pg_type.h:448
#define NUMERICOID
Definition: pg_type.h:554
#define INT4OID
Definition: pg_type.h:316
enum ECPGttype type
Definition: extern.h:113
#define VARBITOID
Definition: pg_type.h:546
#define TINTERVALOID
Definition: pg_type.h:428
#define XIDOID
Definition: pg_type.h:336
#define CIRCLEOID
Definition: pg_type.h:435
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
struct ECPGtype_information_cache * next
Definition: extern.h:45
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define OIDVECTOROID
Definition: pg_type.h:344
#define TIDOID
Definition: pg_type.h:332
#define TIMESTAMPOID
Definition: pg_type.h:519
#define BOXOID
Definition: pg_type.h:402
#define LSEGOID
Definition: pg_type.h:396
struct connection * connection
Definition: extern.h:56
struct ECPGtype_information_cache * cache_head
Definition: extern.h:84
enum COMPAT_MODE compat
Definition: extern.h:57
#define INT2OID
Definition: pg_type.h:308
#define PATHOID
Definition: pg_type.h:399
int lineno
Definition: extern.h:53
#define INTERVALOID
Definition: pg_type.h:529
char * ecpg_alloc(long, int)
Definition: memory.c:19
#define POINTOID
Definition: pg_type.h:393
#define ZPBITOID
Definition: pg_type.h:57
#define not_an_array_in_ecpg
#define VARCHAROID
Definition: pg_type.h:507
#define FLOAT4OID
Definition: pg_type.h:416
#define CIDOID
Definition: pg_type.h:340
#define POLYGONOID
Definition: pg_type.h:405
#define ECPG_IS_ARRAY(X)
Definition: extern.h:30
#define CHAROID
Definition: pg_type.h:296
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define INT8OID
Definition: pg_type.h:304
#define LINEOID
Definition: pg_type.h:409
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
#define TIMETZOID
Definition: pg_type.h:536
#define FLOAT8OID
Definition: pg_type.h:419
bool ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
Definition: error.c:283
#define BOOLOID
Definition: pg_type.h:288
#define UNKNOWNOID
Definition: pg_type.h:431
#define BYTEAOID
Definition: pg_type.h:292
#define CASHOID
Definition: pg_type.h:439
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
#define ABSTIMEOID
Definition: pg_type.h:422
enum ARRAY_TYPE isarray
Definition: extern.h:47
#define REGPROCOID
Definition: pg_type.h:320
#define RELTIMEOID
Definition: pg_type.h:425
bool ecpg_process_output ( struct statement stmt,
bool  clear_result 
)

Definition at line 1474 of file execute.c.

References pgNotify::be_pid, buffer, 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, NULL, statement::outlist, PGRES_COMMAND_OK, PGRES_COPY_OUT, PGRES_TUPLES_OK, variable::pointer, PQclear(), PQcmdStatus(), PQcmdTuples(), PQfreemem(), PQgetCopyData(), PQgetResult(), PQnfields(), PQnotifies(), PQntuples(), PQoidValue(), PQresultErrorMessage(), PQresultStatus(), pgNotify::relname, descriptor::result, statement::results, sqlca, sqlca_t::sqlerrd, status(), and variable::type.

Referenced by ecpg_do().

1475 {
1476  struct variable *var;
1477  bool status = false;
1478  char *cmdstat;
1479  PGnotify *notify;
1480  struct sqlca_t *sqlca = ECPGget_sqlca();
1481  int nfields,
1482  ntuples,
1483  act_field;
1484 
1485  if (sqlca == NULL)
1486  {
1489  return (false);
1490  }
1491 
1492  var = stmt->outlist;
1493  switch (PQresultStatus(stmt->results))
1494  {
1495  case PGRES_TUPLES_OK:
1496  nfields = PQnfields(stmt->results);
1497  sqlca->sqlerrd[2] = ntuples = PQntuples(stmt->results);
1498 
1499  ecpg_log("ecpg_process_output on line %d: correctly got %d tuples with %d fields\n", stmt->lineno, ntuples, nfields);
1500  status = true;
1501 
1502  if (ntuples < 1)
1503  {
1504  if (ntuples)
1505  ecpg_log("ecpg_process_output on line %d: incorrect number of matches (%d)\n",
1506  stmt->lineno, ntuples);
1508  status = false;
1509  break;
1510  }
1511 
1512  if (var != NULL && var->type == ECPGt_descriptor)
1513  {
1514  struct descriptor *desc = ecpg_find_desc(stmt->lineno, var->pointer);
1515 
1516  if (desc == NULL)
1517  status = false;
1518  else
1519  {
1520  if (desc->result)
1521  PQclear(desc->result);
1522  desc->result = stmt->results;
1523  clear_result = false;
1524  ecpg_log("ecpg_process_output on line %d: putting result (%d tuples) into descriptor %s\n",
1525  stmt->lineno, PQntuples(stmt->results), (const char *) var->pointer);
1526  }
1527  var = var->next;
1528  }
1529  else if (var != NULL && var->type == ECPGt_sqlda)
1530  {
1531  if (INFORMIX_MODE(stmt->compat))
1532  {
1533  struct sqlda_compat **_sqlda = (struct sqlda_compat **) var->pointer;
1534  struct sqlda_compat *sqlda = *_sqlda;
1535  struct sqlda_compat *sqlda_new;
1536  int i;
1537 
1538  /*
1539  * If we are passed in a previously existing sqlda (chain)
1540  * then free it.
1541  */
1542  while (sqlda)
1543  {
1544  sqlda_new = sqlda->desc_next;
1545  free(sqlda);
1546  sqlda = sqlda_new;
1547  }
1548  *_sqlda = sqlda = sqlda_new = NULL;
1549  for (i = ntuples - 1; i >= 0; i--)
1550  {
1551  /*
1552  * Build a new sqlda structure. Note that only
1553  * fetching 1 record is supported
1554  */
1555  sqlda_new = ecpg_build_compat_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1556 
1557  if (!sqlda_new)
1558  {
1559  /* cleanup all SQLDAs we created up */
1560  while (sqlda)
1561  {
1562  sqlda_new = sqlda->desc_next;
1563  free(sqlda);
1564  sqlda = sqlda_new;
1565  }
1566  *_sqlda = NULL;
1567 
1568  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1569  status = false;
1570  break;
1571  }
1572  else
1573  {
1574  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1575 
1576  *_sqlda = sqlda_new;
1577 
1578  ecpg_set_compat_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1579  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1580  stmt->lineno, PQnfields(stmt->results));
1581 
1582  sqlda_new->desc_next = sqlda;
1583  sqlda = sqlda_new;
1584  }
1585  }
1586  }
1587  else
1588  {
1589  struct sqlda_struct **_sqlda = (struct sqlda_struct **) var->pointer;
1590  struct sqlda_struct *sqlda = *_sqlda;
1591  struct sqlda_struct *sqlda_new;
1592  int i;
1593 
1594  /*
1595  * If we are passed in a previously existing sqlda (chain)
1596  * then free it.
1597  */
1598  while (sqlda)
1599  {
1600  sqlda_new = sqlda->desc_next;
1601  free(sqlda);
1602  sqlda = sqlda_new;
1603  }
1604  *_sqlda = sqlda = sqlda_new = NULL;
1605  for (i = ntuples - 1; i >= 0; i--)
1606  {
1607  /*
1608  * Build a new sqlda structure. Note that only
1609  * fetching 1 record is supported
1610  */
1611  sqlda_new = ecpg_build_native_sqlda(stmt->lineno, stmt->results, i, stmt->compat);
1612 
1613  if (!sqlda_new)
1614  {
1615  /* cleanup all SQLDAs we created up */
1616  while (sqlda)
1617  {
1618  sqlda_new = sqlda->desc_next;
1619  free(sqlda);
1620  sqlda = sqlda_new;
1621  }
1622  *_sqlda = NULL;
1623 
1624  ecpg_log("ecpg_process_output on line %d: out of memory allocating a new sqlda\n", stmt->lineno);
1625  status = false;
1626  break;
1627  }
1628  else
1629  {
1630  ecpg_log("ecpg_process_output on line %d: new sqlda was built\n", stmt->lineno);
1631 
1632  *_sqlda = sqlda_new;
1633 
1634  ecpg_set_native_sqlda(stmt->lineno, _sqlda, stmt->results, i, stmt->compat);
1635  ecpg_log("ecpg_process_output on line %d: putting result (1 tuple %d fields) into sqlda descriptor\n",
1636  stmt->lineno, PQnfields(stmt->results));
1637 
1638  sqlda_new->desc_next = sqlda;
1639  sqlda = sqlda_new;
1640  }
1641  }
1642  }
1643 
1644  var = var->next;
1645  }
1646  else
1647  for (act_field = 0; act_field < nfields && status; act_field++)
1648  {
1649  if (var != NULL)
1650  {
1651  status = ecpg_store_result(stmt->results, act_field, stmt, var);
1652  var = var->next;
1653  }
1654  else if (!INFORMIX_MODE(stmt->compat))
1655  {
1657  return (false);
1658  }
1659  }
1660 
1661  if (status && var != NULL)
1662  {
1664  status = false;
1665  }
1666 
1667  break;
1668  case PGRES_COMMAND_OK:
1669  status = true;
1670  cmdstat = PQcmdStatus(stmt->results);
1671  sqlca->sqlerrd[1] = PQoidValue(stmt->results);
1672  sqlca->sqlerrd[2] = atol(PQcmdTuples(stmt->results));
1673  ecpg_log("ecpg_process_output on line %d: OK: %s\n", stmt->lineno, cmdstat);
1674  if (stmt->compat != ECPG_COMPAT_INFORMIX_SE &&
1675  !sqlca->sqlerrd[2] &&
1676  (strncmp(cmdstat, "UPDATE", 6) == 0
1677  || strncmp(cmdstat, "INSERT", 6) == 0
1678  || strncmp(cmdstat, "DELETE", 6) == 0))
1680  break;
1681  case PGRES_COPY_OUT:
1682  {
1683  char *buffer;
1684  int res;
1685 
1686  ecpg_log("ecpg_process_output on line %d: COPY OUT data transfer in progress\n", stmt->lineno);
1687  while ((res = PQgetCopyData(stmt->connection->connection,
1688  &buffer, 0)) > 0)
1689  {
1690  printf("%s", buffer);
1691  PQfreemem(buffer);
1692  }
1693  if (res == -1)
1694  {
1695  /* COPY done */
1696  PQclear(stmt->results);
1697  stmt->results = PQgetResult(stmt->connection->connection);
1698  if (PQresultStatus(stmt->results) == PGRES_COMMAND_OK)
1699  ecpg_log("ecpg_process_output on line %d: got PGRES_COMMAND_OK after PGRES_COPY_OUT\n", stmt->lineno);
1700  else
1701  ecpg_log("ecpg_process_output on line %d: got error after PGRES_COPY_OUT: %s", stmt->lineno, PQresultErrorMessage(stmt->results));
1702  }
1703  break;
1704  }
1705  default:
1706 
1707  /*
1708  * execution should never reach this code because it is already
1709  * handled in ECPGcheck_PQresult()
1710  */
1711  ecpg_log("ecpg_process_output on line %d: unknown execution status type\n",
1712  stmt->lineno);
1713  ecpg_raise_backend(stmt->lineno, stmt->results, stmt->connection->connection, stmt->compat);
1714  status = false;
1715  break;
1716  }
1717 
1718  if (clear_result)
1719  {
1720  PQclear(stmt->results);
1721  stmt->results = NULL;
1722  }
1723 
1724  /* check for asynchronous returns */
1725  notify = PQnotifies(stmt->connection->connection);
1726  if (notify)
1727  {
1728  ecpg_log("ecpg_process_output on line %d: asynchronous notification of \"%s\" from backend PID %d received\n",
1729  stmt->lineno, notify->relname, notify->be_pid);
1730  PQfreemem(notify);
1731  }
1732 
1733  return status;
1734 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
bool ecpg_store_result(const PGresult *results, int act_field, const struct statement *stmt, struct variable *var)
Definition: execute.c:309
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2193
#define INFORMIX_MODE(X)
Definition: extern.h:23
#define sqlca
Definition: sqlca.h:59
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3014
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:2985
void * pointer
Definition: extern.h:115
enum ECPGttype type
Definition: extern.h:113
PGresult * results
Definition: extern.h:66
#define ECPG_TOO_MANY_ARGUMENTS
Definition: ecpgerrno.h:19
#define ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY
Definition: extern.h:221
struct variable * next
Definition: extern.h:125
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
int PQgetCopyData(PGconn *conn, char **buffer, int async)
Definition: fe-exec.c:2377
#define ECPG_SQLSTATE_NO_DATA
Definition: extern.h:200
void ecpg_log(const char *format,...) pg_attribute_printf(1
#define ECPG_SQLSTATE_USING_CLAUSE_DOES_NOT_MATCH_TARGETS
Definition: extern.h:202
#define ECPG_OUT_OF_MEMORY
Definition: ecpgerrno.h:15
struct sqlca_t * ECPGget_sqlca(void)
Definition: misc.c:142
void ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
Definition: error.c:219
struct connection * connection
Definition: extern.h:56
void ecpg_set_compat_sqlda(int, struct sqlda_compat **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:252
int be_pid
Definition: libpq-fe.h:164
enum COMPAT_MODE compat
Definition: extern.h:57
Definition: sqlca.h:19
struct descriptor * ecpg_find_desc(int line, const char *name)
Definition: descriptor.c:799
int lineno
Definition: extern.h:53
PGresult * result
Definition: extern.h:93
char * relname
Definition: libpq-fe.h:163
long sqlerrd[6]
Definition: sqlca.h:30
void ecpg_raise(int line, int code, const char *sqlstate, const char *str)
Definition: error.c:13
#define ECPG_NOT_FOUND
Definition: ecpgerrno.h:10
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:2944
struct sqlda_compat * ecpg_build_compat_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:202
struct sqlda_struct * ecpg_build_native_sqlda(int, PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:409
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
PGconn * connection
Definition: extern.h:82
#define NULL
Definition: c.h:229
#define ECPG_TOO_FEW_ARGUMENTS
Definition: ecpgerrno.h:20
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:2612
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
void ecpg_set_native_sqlda(int, struct sqlda_struct **, const PGresult *, int, enum COMPAT_MODE)
Definition: sqlda.c:441
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
struct variable * outlist
Definition: extern.h:62
bool ecpg_store_input ( const int  lineno,
const bool  force_indicator,
const struct variable var,
char **  tobeinserted_p,
bool  quote 
)

Definition at line 494 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_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, NULL, 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(), result, sprintf_double_value(), sprintf_float_value(), variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_build_params(), and ECPGset_desc().

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

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

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

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

Referenced by ecpg_is_type_an_array().

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 }
struct ECPGtype_information_cache * next
Definition: extern.h:45
char * ecpg_alloc(long, int)
Definition: memory.c:19
#define NULL
Definition: c.h:229
enum ARRAY_TYPE isarray
Definition: extern.h:47
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 2030 of file execute.c.

References generate_unaccent_rules::args, and ecpg_do().

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

2031 {
2032  va_list args;
2033  bool ret;
2034 
2035  va_start(args, query);
2036  ret = ecpg_do(lineno, compat, force_indicator, connection_name,
2037  questionmarks, st, query, args);
2038  va_end(args);
2039 
2040  return ret;
2041 }
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:1996
bool ECPGdo_descriptor ( int  line,
const char *  connection,
const char *  descriptor,
const char *  query 
)

Definition at line 2045 of file execute.c.

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

2047 {
2048  return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2049  ECPGt_descriptor, descriptor, 0L, 0L, 0L,
2050  ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
2051 }
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:2030
#define NULL
Definition: c.h:229
static void free_statement ( struct statement stmt)
static

Definition at line 98 of file execute.c.

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

Referenced by ecpg_do_epilogue().

99 {
100  if (stmt == NULL)
101  return;
102  free_variable(stmt->inlist);
103  free_variable(stmt->outlist);
104  ecpg_free(stmt->command);
105  ecpg_free(stmt->name);
106  ecpg_free(stmt->oldlocale);
107  ecpg_free(stmt);
108 }
void ecpg_free(void *)
Definition: memory.c:13
static void free_variable(struct variable *var)
Definition: execute.c:85
char * oldlocale
Definition: extern.h:63
char * command
Definition: extern.h:54
struct variable * inlist
Definition: extern.h:61
#define NULL
Definition: c.h:229
char * name
Definition: extern.h:55
struct variable * outlist
Definition: extern.h:62
static void free_variable ( struct variable var)
static

Definition at line 85 of file execute.c.

References ecpg_free(), and variable::next.

Referenced by free_statement().

86 {
87  struct variable *var_next;
88 
89  while (var)
90  {
91  var_next = var->next;
92  ecpg_free(var);
93  var = var_next;
94  }
95 }
void ecpg_free(void *)
Definition: memory.c:13
struct variable * next
Definition: extern.h:125
static bool insert_tobeinserted ( int  position,
int  ph_len,
struct statement stmt,
char *  tobeinserted 
)
static

Definition at line 1068 of file execute.c.

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

Referenced by ecpg_build_params().

1069 {
1070  char *newcopy;
1071 
1072  if (!(newcopy = (char *) ecpg_alloc(strlen(stmt->command)
1073  + strlen(tobeinserted)
1074  + 1, stmt->lineno)))
1075  {
1076  ecpg_free(tobeinserted);
1077  return false;
1078  }
1079 
1080  strcpy(newcopy, stmt->command);
1081  strcpy(newcopy + position - 1, tobeinserted);
1082 
1083  /*
1084  * The strange thing in the second argument is the rest of the string from
1085  * the old string
1086  */
1087  strcat(newcopy,
1088  stmt->command
1089  + position
1090  + ph_len - 1);
1091 
1092  ecpg_free(stmt->command);
1093  stmt->command = newcopy;
1094 
1095  ecpg_free((char *) tobeinserted);
1096  return true;
1097 }
void ecpg_free(void *)
Definition: memory.c:13
int lineno
Definition: extern.h:53
char * ecpg_alloc(long, int)
Definition: memory.c:19
char * command
Definition: extern.h:54
static int next_insert ( char *  text,
int  pos,
bool  questionmarks 
)
static

Definition at line 111 of file execute.c.

References i.

Referenced by ecpg_build_params().

112 {
113  bool string = false;
114  int p = pos;
115 
116  for (; text[p] != '\0'; p++)
117  {
118  if (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 }
bool questionmarks
Definition: ecpg.c:19
int i
Definition: c.h:439
static char* quote_postgres ( char *  arg,
bool  quote,
int  lineno 
)
static

Definition at line 42 of file execute.c.

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

Referenced by ecpg_store_input().

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

Definition at line 462 of file execute.c.

References isinf().

Referenced by ecpg_store_input().

463 {
464  if (isnan(value))
465  sprintf(ptr, "%s%s", "NaN", delim);
466  else if (isinf(value))
467  {
468  if (value < 0)
469  sprintf(ptr, "%s%s", "-Infinity", delim);
470  else
471  sprintf(ptr, "%s%s", "Infinity", delim);
472  }
473  else
474  sprintf(ptr, "%.15g%s", value, delim);
475 }
static struct @121 value
int isinf(double x)
static void sprintf_float_value ( char *  ptr,
float  value,
const char *  delim 
)
static

Definition at line 478 of file execute.c.

References isinf().

Referenced by ecpg_store_input().

479 {
480  if (isnan(value))
481  sprintf(ptr, "%s%s", "NaN", delim);
482  else if (isinf(value))
483  {
484  if (value < 0)
485  sprintf(ptr, "%s%s", "-Infinity", delim);
486  else
487  sprintf(ptr, "%s%s", "Infinity", delim);
488  }
489  else
490  sprintf(ptr, "%.15g%s", value, delim);
491 }
static struct @121 value
int isinf(double x)