Skip to content

Commit 971538b

Browse files
committed
fix(database_postgresql): fix database_select1_value to make it work with text result of type name (for example the result of SELECT current_schema();)
1 parent 2ccdcfe commit 971538b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/postgresql/database_postgresql.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ int database_select1_value (cloudsync_context *data, const char *sql, char **ptr
300300

301301
// init values and sanity check expected_type
302302
if (ptr_value) *ptr_value = NULL;
303-
*int_value = 0;
303+
if (int_value) *int_value = 0;
304304
if (expected_type != DBTYPE_INTEGER && expected_type != DBTYPE_TEXT && expected_type != DBTYPE_BLOB) {
305305
return cloudsync_set_error(data, "Invalid expected_type", DBRES_MISUSE);
306306
}
@@ -356,18 +356,20 @@ int database_select1_value (cloudsync_context *data, const char *sql, char **ptr
356356
goto cleanup;
357357
}
358358
} else if (expected_type == DBTYPE_TEXT) {
359-
text *txt = DatumGetTextP(datum);
360-
int len = VARSIZE(txt) - VARHDRSZ;
361-
if (len > 0) {
359+
char *val = SPI_getvalue(tuple, SPI_tuptable->tupdesc, 1);
360+
if (val) {
361+
size_t len = strlen(val);
362362
char *ptr = cloudsync_memory_alloc(len + 1);
363363
if (!ptr) {
364+
pfree(val);
364365
rc = cloudsync_set_error(data, "Memory allocation failed", DBRES_NOMEM);
365366
goto cleanup;
366367
}
367-
memcpy(ptr, VARDATA(txt), len);
368+
memcpy(ptr, val, len);
368369
ptr[len] = '\0';
369-
*ptr_value = ptr;
370-
*int_value = len;
370+
if (ptr_value) *ptr_value = ptr;
371+
if (int_value) *int_value = (int64_t)len;
372+
pfree(val);
371373
}
372374
} else if (expected_type == DBTYPE_BLOB) {
373375
bytea *ba = DatumGetByteaP(datum);

0 commit comments

Comments
 (0)