Skip to content

Commit 3589f15

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 bce53be commit 3589f15

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
@@ -303,7 +303,7 @@ int database_select1_value (cloudsync_context *data, const char *sql, char **ptr
303303

304304
// init values and sanity check expected_type
305305
if (ptr_value) *ptr_value = NULL;
306-
*int_value = 0;
306+
if (int_value) *int_value = 0;
307307
if (expected_type != DBTYPE_INTEGER && expected_type != DBTYPE_TEXT && expected_type != DBTYPE_BLOB) {
308308
return cloudsync_set_error(data, "Invalid expected_type", DBRES_MISUSE);
309309
}
@@ -359,18 +359,20 @@ int database_select1_value (cloudsync_context *data, const char *sql, char **ptr
359359
goto cleanup;
360360
}
361361
} else if (expected_type == DBTYPE_TEXT) {
362-
text *txt = DatumGetTextP(datum);
363-
int len = VARSIZE(txt) - VARHDRSZ;
364-
if (len > 0) {
362+
char *val = SPI_getvalue(tuple, SPI_tuptable->tupdesc, 1);
363+
if (val) {
364+
size_t len = strlen(val);
365365
char *ptr = cloudsync_memory_alloc(len + 1);
366366
if (!ptr) {
367+
pfree(val);
367368
rc = cloudsync_set_error(data, "Memory allocation failed", DBRES_NOMEM);
368369
goto cleanup;
369370
}
370-
memcpy(ptr, VARDATA(txt), len);
371+
memcpy(ptr, val, len);
371372
ptr[len] = '\0';
372-
*ptr_value = ptr;
373-
*int_value = len;
373+
if (ptr_value) *ptr_value = ptr;
374+
if (int_value) *int_value = (int64_t)len;
375+
pfree(val);
374376
}
375377
} else if (expected_type == DBTYPE_BLOB) {
376378
bytea *ba = DatumGetByteaP(datum);

0 commit comments

Comments
 (0)