0
CREATE OR REPLACE FUNCTION public.defaultmapviewport(hasura_session json)
 RETURNS viewport_info
 STABLE
AS $function$
    case hasura_session ->> 'x-hasura-default-country-code'
        when 'FR' then select lat, lng from places where slug_en = 'city-paris' 
        when 'ES' then select lat, lng from places where slug_en = 'municipality-madrid'
    end;
END $function$ language SQL;

I'm getting a syntax error near case, even though it seems right. What could be the problem?

Viewport info:

create table viewport_info(
    lat float,
    lng float
);
1
  • 2
    CASE cannot exist on its own, it needs to be part of larger query e.eg. SELECT CASE .... In other words you inverted the statement. See CASE. Commented Dec 29, 2021 at 19:59

1 Answer 1

2

As @AdrianKlaver commented, a CASE expression can't stand on its own, it must be part of a SELECT query. You'll want to use

CREATE OR REPLACE FUNCTION public.defaultmapviewport(hasura_session json)
RETURNS viewport_info
STABLE
AS $function$
  SELECT lat, lng
  FROM places
  WHERE slug_en = (CASE hasura_session ->> 'x-hasura-default-country-code'
    WHEN 'FR' THEN 'city-paris' 
    WHEN 'ES' THEN 'municipality-madrid'
  END);
$function$ language SQL;

(online demo)

Sign up to request clarification or add additional context in comments.

2 Comments

i'm getting a return type mismatch in function declared to return viewport_info, I'll update question with the viewport_info create statement
@PlayMa256 Ah, there was a stray END at the end that I copied from the question. Fixed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.