Skip to content

Commit 615eb31

Browse files
committed
Updated databasevm_step0, databasevm_step and databasevm_clear_bindings. Fixed warnings in test (due to uint64_t usage in a signed BIGINT)
1 parent ac5fc52 commit 615eb31

1 file changed

Lines changed: 15 additions & 25 deletions

File tree

src/postgresql/database_postgresql.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ uint64_t database_schema_hash (cloudsync_context *data) {
13741374

13751375
bool database_check_schema_hash (cloudsync_context *data, uint64_t hash) {
13761376
char sql[1024];
1377-
snprintf(sql, sizeof(sql), "SELECT 1 FROM cloudsync_schema_versions WHERE hash = %" PRIu64, hash);
1377+
snprintf(sql, sizeof(sql), "SELECT 1 FROM cloudsync_schema_versions WHERE hash = %" PRId64, (int64_t)hash);
13781378

13791379
int64_t value = 0;
13801380
database_select_int(data, sql, &value);
@@ -1399,10 +1399,10 @@ int database_update_schema_hash (cloudsync_context *data, uint64_t *hash) {
13991399
char sql[1024];
14001400
snprintf(sql, sizeof(sql),
14011401
"INSERT INTO cloudsync_schema_versions (hash, seq) "
1402-
"VALUES (%" PRIu64 ", COALESCE((SELECT MAX(seq) FROM cloudsync_schema_versions), 0) + 1) "
1402+
"VALUES (%" PRId64 ", COALESCE((SELECT MAX(seq) FROM cloudsync_schema_versions), 0) + 1) "
14031403
"ON CONFLICT(hash) DO UPDATE SET "
14041404
"seq = (SELECT COALESCE(MAX(seq), 0) + 1 FROM cloudsync_schema_versions);",
1405-
h);
1405+
(int64_t)h);
14061406
rc = database_exec(data, sql);
14071407
if (rc == DBRES_OK && hash) {
14081408
if (hash) *hash = h;
@@ -1541,9 +1541,9 @@ int databasevm_step0 (pg_stmt_t *stmt) {
15411541

15421542
PG_TRY();
15431543
{
1544-
if (!stmt || !stmt->sql) {
1544+
if (!stmt->sql) {
15451545
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
1546-
errmsg("databasevm_step0 invalid stmt or sql pointer")));
1546+
errmsg("databasevm_step0 invalid sql pointer")));
15471547
}
15481548

15491549
stmt->plan = SPI_prepare(stmt->sql, stmt->nparams, stmt->types);
@@ -1682,8 +1682,13 @@ int databasevm_step (dbvm_t *vm) {
16821682
}
16831683

16841684
// Execute once (non-row-returning or cursor open failed).
1685-
if (stmt->nparams == 0) SPI_execute_plan(stmt->plan, NULL, NULL, false, 0);
1686-
else SPI_execute_plan(stmt->plan, stmt->values, stmt->nulls, false, 0);
1685+
int spi_rc;
1686+
if (stmt->nparams == 0) spi_rc = SPI_execute_plan(stmt->plan, NULL, NULL, false, 0);
1687+
else spi_rc = SPI_execute_plan(stmt->plan, stmt->values, stmt->nulls, false, 0);
1688+
if (spi_rc < 0) {
1689+
rc = cloudsync_set_error(data, "SPI_execute_plan failed", DBRES_ERROR);
1690+
break;
1691+
}
16871692
if (SPI_tuptable) {
16881693
SPI_freetuptable(SPI_tuptable);
16891694
SPI_tuptable = NULL;
@@ -1776,24 +1781,9 @@ void databasevm_clear_bindings (dbvm_t *vm) {
17761781
if (!vm) return;
17771782
pg_stmt_t *stmt = (pg_stmt_t*)vm;
17781783

1779-
clear_fetch_batch(stmt);
1780-
close_portal(stmt);
1781-
if (SPI_tuptable) {
1782-
SPI_freetuptable(SPI_tuptable);
1783-
SPI_tuptable = NULL;
1784-
}
1785-
1786-
if (stmt->plan_is_prepared && stmt->plan) {
1787-
SPI_freeplan(stmt->plan);
1788-
stmt->plan = NULL;
1789-
stmt->plan_is_prepared = false;
1790-
}
1791-
1792-
// DO NOT call clear_fetch_batch() - not related to bindings
1793-
// DO NOT call close_portal() - not related to bindings
1794-
// DO NOT free the plan - clearing bindings != destroying prepared statement
1795-
1796-
// Only clear the bound parameter values
1784+
// Only clear the bound parameter values.
1785+
// Do NOT close portals, free fetch batches, or free the plan —
1786+
// those are execution state, not bindings.
17971787
if (stmt->bind_mcxt) MemoryContextReset(stmt->bind_mcxt);
17981788
stmt->nparams = 0;
17991789

0 commit comments

Comments
 (0)