PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 charquote_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, Oid oid, enum ARRAY_TYPE isarray, int lineno)
 
static enum ARRAY_TYPE ecpg_is_type_an_array (Oid 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 charconvert_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 489 of file execute.c.

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

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

Referenced by ecpg_build_params().

◆ ecpg_autostart_transaction()

bool ecpg_autostart_transaction ( struct statement stmt)

Definition at line 1580 of file execute.c.

1581{
1582 if (PQtransactionStatus(stmt->connection->connection) == PQTRANS_IDLE && !stmt->connection->autocommit)
1583 {
1584 stmt->results = PQexec(stmt->connection->connection, "begin transaction");
1585 if (!ecpg_check_PQresult(stmt->results, stmt->lineno, stmt->connection->connection, stmt->compat))
1586 {
1587 ecpg_free_params(stmt, false);
1588 return false;
1589 }
1590 PQclear(stmt->results);
1591 stmt->results = NULL;
1592 }
1593 return true;
1594}
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:1107
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
#define stmt
#define PQclear
@ PQTRANS_IDLE
Definition libpq-fe.h:153

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

Referenced by ecpg_do().

◆ ecpg_build_params()

bool ecpg_build_params ( struct statement stmt)

Definition at line 1212 of file execute.c.

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

References convert_bytea_to_string(), descriptor::count, 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, fb(), i, INFORMIX_MODE, insert_tobeinserted(), descriptor::items, len, variable::next, next_insert(), variable::pointer, PQparameterStatus(), snprintf, sprintf, stmt, store_input_from_desc(), 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 2258 of file execute.c.

2259{
2260 struct statement *stmt = NULL;
2261
2264 query, args, &stmt))
2265 goto fail;
2266
2267 if (!ecpg_build_params(stmt))
2268 goto fail;
2269
2271 goto fail;
2272
2273 if (!ecpg_execute(stmt))
2274 goto fail;
2275
2276 if (!ecpg_process_output(stmt, true))
2277 goto fail;
2278
2280 return true;
2281
2282fail:
2284 return false;
2285}
enum COMPAT_MODE compat
Definition ecpg.c:26
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:1212
bool ecpg_execute(struct statement *stmt)
Definition execute.c:1601
void ecpg_do_epilogue(struct statement *stmt)
Definition execute.c:2231
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:1943
bool ecpg_process_output(struct statement *stmt, bool clear_result)
Definition execute.c:1670
bool ecpg_autostart_transaction(struct statement *stmt)
Definition execute.c:1580

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

Referenced by ECPGdo().

◆ ecpg_do_epilogue()

void ecpg_do_epilogue ( struct statement stmt)

Definition at line 2231 of file execute.c.

2232{
2233 if (stmt == NULL)
2234 return;
2235
2236#ifdef HAVE_USELOCALE
2237 if (stmt->oldlocale != (locale_t) 0)
2238 uselocale(stmt->oldlocale);
2239#else
2240 if (stmt->oldlocale)
2241 {
2242 setlocale(LC_NUMERIC, stmt->oldlocale);
2243#ifdef WIN32
2244 _configthreadlocale(stmt->oldthreadlocale);
2245#endif
2246 }
2247#endif
2248
2250}
static void free_statement(struct statement *stmt)
Definition execute.c:96
#define locale_t
Definition win32_port.h:429
#define setlocale(a, b)
Definition win32_port.h:472

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

Referenced by ecpg_do(), and ecpg_do_prologue().

◆ ecpg_do_prologue()

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

Definition at line 1943 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_execute()

bool ecpg_execute ( struct statement stmt)

Definition at line 1601 of file execute.c.

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

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

Referenced by ecpg_do().

◆ ecpg_free_params()

void ecpg_free_params ( struct statement stmt,
bool  print 
)

Definition at line 1107 of file execute.c.

1108{
1109 int n;
1110
1111 for (n = 0; n < stmt->nparams; n++)
1112 {
1113 if (print)
1114 print_param_value(stmt->paramvalues[n], stmt->paramlengths[n],
1115 stmt->paramformats[n], stmt->lineno, n + 1);
1116 ecpg_free(stmt->paramvalues[n]);
1117 }
1118 ecpg_free(stmt->paramvalues);
1119 ecpg_free(stmt->paramlengths);
1120 ecpg_free(stmt->paramformats);
1121 stmt->paramvalues = NULL;
1122 stmt->paramlengths = NULL;
1123 stmt->paramformats = NULL;
1124 stmt->nparams = 0;
1125}
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:1077

References ecpg_free(), fb(), print(), print_param_value(), and stmt.

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 ( Oid  type,
const struct statement stmt,
const struct variable var 
)
static

Definition at line 164 of file execute.c.

165{
166 char *array_query;
167 enum ARRAY_TYPE isarray = ECPG_ARRAY_NOT_SET;
168 PGresult *query;
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;
186 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), NAMEOID, not_an_array_in_ecpg, stmt->lineno))
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;
216 if (!ecpg_type_infocache_push(&(stmt->connection->cache_head), BOXOID, ECPG_ARRAY_VECTOR, stmt->lineno))
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 = 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=%u and typelem<>0", type);
271 query = PQexec(stmt->connection->connection, 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 = (atoi(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
296 ecpg_type_infocache_push(&(stmt->connection->cache_head), type, isarray, stmt->lineno);
297 ecpg_log("ecpg_is_type_an_array on line %d: type (%u); C (%d); array (%s)\n",
298 stmt->lineno, type, var->type, ECPG_IS_ARRAY(isarray) ? "yes" : "no");
299 return isarray;
300}
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
#define not_an_array_in_ecpg
static bool ecpg_type_infocache_push(struct ECPGtype_information_cache **cache, Oid oid, enum ARRAY_TYPE isarray, int lineno)
Definition execute.c:148
#define PQgetvalue
#define PQresultStatus
#define PQntuples
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
@ SQL3_CHARACTER_VARYING
Definition sql3types.h:20
@ SQL3_CHARACTER
Definition sql3types.h:10

References 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(), fb(), ECPGtype_information_cache::isarray, not_an_array_in_ecpg, PGRES_TUPLES_OK, PQclear, PQexec(), PQgetvalue, PQntuples, PQresultStatus, sprintf, SQL3_CHARACTER, SQL3_CHARACTER_VARYING, stmt, type, and variable::type.

Referenced by ecpg_store_result().

◆ ecpg_process_output()

bool ecpg_process_output ( struct statement stmt,
bool  clear_result 
)

Definition at line 1670 of file execute.c.

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

References sqlda_compat::desc_next, sqlda_struct::desc_next, 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, fb(), free, i, INFORMIX_MODE, variable::next, PGRES_COMMAND_OK, PGRES_COPY_OUT, PGRES_TUPLES_OK, variable::pointer, PQclear, PQcmdStatus, PQcmdTuples, PQconsumeInput(), PQfreemem(), PQgetCopyData(), PQgetResult, PQnfields, PQnotifies(), PQntuples, PQoidValue(), PQresultErrorMessage, PQresultStatus, printf, descriptor::result, sqlca, stmt, and variable::type.

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

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

References variable::arrsize, 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(), fb(), 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(), 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 304 of file execute.c.

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

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

Referenced by ecpg_process_output(), and ECPGget_desc().

◆ ecpg_type_infocache_push()

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

Definition at line 148 of file execute.c.

149{
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}

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

Referenced by ecpg_is_type_an_array().

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

2293{
2294 va_list args;
2295 bool ret;
2296
2297 va_start(args, query);
2299 questionmarks, st, query, args);
2300 va_end(args);
2301
2302 return ret;
2303}
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:2258

References compat, ecpg_do(), fb(), force_indicator, statement::lineno, and questionmarks.

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

◆ ECPGdo_descriptor()

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

Definition at line 2307 of file execute.c.

2309{
2310 return ECPGdo(line, ECPG_COMPAT_PGSQL, true, connection, '\0', 0, query, ECPGt_EOIT,
2313}
@ 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:2292

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

◆ 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
108}
static void free_variable(struct variable *var)
Definition execute.c:83

References ecpg_free(), fb(), free_variable(), and stmt.

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(), fb(), 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 1128 of file execute.c.

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

References ecpg_alloc(), ecpg_free(), fb(), and stmt.

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}
#define false
Definition c.h:778

References false, fb(), 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 1077 of file execute.c.

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

References ecpg_alloc(), ecpg_free(), ecpg_hex_enc_len(), ecpg_hex_encode(), ecpg_log(), fb(), 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 = ecpg_alloc(buffer_len + 3, lineno);
58 if (!res)
59 return res;
61 if (length == escaped_len)
62 {
63 res[0] = res[escaped_len + 1] = '\'';
64 res[escaped_len + 2] = '\0';
65 }
66 else
67 {
68 /*
69 * We don't know if the target database is using
70 * standard_conforming_strings, so we always use E'' strings.
71 */
72 memmove(res + 2, res + 1, escaped_len);
73 res[0] = ESCAPE_STRING_SYNTAX;
74 res[1] = res[escaped_len + 2] = '\'';
75 res[escaped_len + 3] = '\0';
76 }
78 return res;
79 }
80}
#define ESCAPE_STRING_SYNTAX
Definition c.h:1257
Datum arg
Definition elog.c:1322
size_t PQescapeString(char *to, const char *from, size_t length)
Definition fe-exec.c:4216

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

Referenced by ecpg_store_input().

◆ sprintf_double_value()

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

Definition at line 457 of file execute.c.

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

References fb(), 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 473 of file execute.c.

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

References fb(), 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 1158 of file execute.c.

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

References variable::arrsize, ecpg_alloc(), ecpg_store_input(), ECPGt_char, ECPGt_int, ECPGt_NO_INDICATOR, fb(), variable::ind_arrsize, variable::ind_offset, variable::ind_pointer, variable::ind_type, variable::ind_value, variable::ind_varcharsize, variable::offset, variable::pointer, stmt, variable::type, variable::value, and variable::varcharsize.

Referenced by ecpg_build_params().