Skip to content

Commit

Permalink
Reverse works for complex objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Florents-Tselai committed Sep 5, 2024
1 parent 4027c45 commit 97983f8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/jsonb_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,33 @@ apply_func_jsonb_value(void *_state, char *elem_value, int elem_len) {
JsonbApplyState *state = (JsonbApplyState *) _state;
Oid funcoid = state->funcoid;
Oid collation = state->top_fcinfo->fncollation;
Datum arg1, result;
Datum func_result;
text *result;

// elog(INFO, "collid=%u, funcoid=%u", state->top_fcinfo->fncollation, state->funcoid);

if (funcoid == 870) {
result = CStringGetTextDatum(str_tolower(elem_value, elem_len, collation));
result = cstring_to_text(str_tolower(elem_value, elem_len, collation));
} else if (funcoid == 871) {
result = CStringGetTextDatum(str_toupper(elem_value, elem_len, collation));
result = cstring_to_text(str_toupper(elem_value, elem_len, collation));

} else {
arg1 = PointerGetDatum(cstring_to_text(elem_value));
result = OidFunctionCall1(state->funcoid, arg1);

/* Can't figure out why some funcs need a copy. So making them all use copy. */
char *elemcopy = (char *) malloc(elem_len);
if (elemcopy != NULL) {
memcpy(elemcopy, elem_value, elem_len);
}

func_result = OidFunctionCall1(funcoid, CStringGetTextDatum(elemcopy));
result = DatumGetTextPP(func_result);

free(elemcopy);


}

return DatumGetTextPP(result);
return result;
}


Expand Down Expand Up @@ -111,7 +123,6 @@ jsonb_apply(PG_FUNCTION_ARGS) {
state->procStruct = (Form_pg_proc) GETSTRUCT(tuple);



printf("proname=%s\tpronargs=%d\tprorettype=%d\n",
NameStr(state->procStruct->proname),
state->procStruct->pronargs,
Expand Down
23 changes: 18 additions & 5 deletions test/expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ERROR: only functions with pronargs=1 are supported, requested function has pro
-- where proargtypes = '25'::oidvector
-- and prorettype = 25
-- order by proname;
select jsonb_apply('"hELLo"', 'btrim(text)');
select jsonb_apply('" hELLo "', 'btrim(text)');
jsonb_apply
-------------
"hELLo"
Expand All @@ -83,10 +83,10 @@ select jsonb_apply('"hELLo"', 'lower(text)');
"hello"
(1 row)

select jsonb_apply('"hELLo"', 'ltrim(text)');
select jsonb_apply('" hELLo "', 'ltrim(text)');
jsonb_apply
-------------
"hELLo"
"hELLo "
(1 row)

select jsonb_apply('"hELLo"', 'max(text)');
Expand Down Expand Up @@ -132,10 +132,10 @@ select jsonb_apply('"hELLo"', 'reverse(text)');
"oLLEh"
(1 row)

select jsonb_apply('"hELLo"', 'rtrim(text)');
select jsonb_apply('" hELLo "', 'rtrim(text)');
jsonb_apply
-------------
"hELLo"
" hELLo"
(1 row)

select jsonb_apply('"hELLo"', 'similar_to_escape(text)');
Expand All @@ -158,3 +158,16 @@ select jsonb_apply('"hELLo"', 'upper(text)');
"HELLO"
(1 row)

-- reverse was a bit tricky
select jsonb_apply('["Hello", {"k": "value"}]', 'reverse(text)');
jsonb_apply
---------------------------
["olleH", {"k": "eulav"}]
(1 row)

select jsonb_apply('{"f": "John", "l": "Doe", "message": "Who are you?", "arr": ["Hello", {"k": "value"}]}', 'reverse(text)');
jsonb_apply
----------------------------------------------------------------------------------------
{"f": "nhoJ", "l": "eoD", "arr": ["olleH", {"k": "eulav"}], "message": "?uoy era ohW"}
(1 row)

10 changes: 7 additions & 3 deletions test/sql/basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ select jsonb_apply('"hello"', 'replace(text, text, text)');
-- order by proname;


select jsonb_apply('"hELLo"', 'btrim(text)');
select jsonb_apply('" hELLo "', 'btrim(text)');


select jsonb_apply('"hELLo"', 'current_setting(text)');
Expand All @@ -44,7 +44,7 @@ select jsonb_apply('"hELLo"', 'lower(text)');



select jsonb_apply('"hELLo"', 'ltrim(text)');
select jsonb_apply('" hELLo "', 'ltrim(text)');



Expand Down Expand Up @@ -81,7 +81,7 @@ select jsonb_apply('"hELLo"', 'quote_nullable(text)');
select jsonb_apply('"hELLo"', 'reverse(text)');


select jsonb_apply('"hELLo"', 'rtrim(text)');
select jsonb_apply('" hELLo "', 'rtrim(text)');


select jsonb_apply('"hELLo"', 'similar_to_escape(text)');
Expand All @@ -94,3 +94,7 @@ select jsonb_apply('"hELLo"', 'unistr(text)');


select jsonb_apply('"hELLo"', 'upper(text)');

-- reverse was a bit tricky
select jsonb_apply('["Hello", {"k": "value"}]', 'reverse(text)');
select jsonb_apply('{"f": "John", "l": "Doe", "message": "Who are you?", "arr": ["Hello", {"k": "value"}]}', 'reverse(text)');

0 comments on commit 97983f8

Please sign in to comment.