Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
How to write a function that return modified row type in PostgreSQL

I want to decide whether to hide some of the fields based on the value of the field:

CREATE FUNCTION hide(messages) RETURNS messages AS
$$
SELECT CASE
           WHEN $1.is_public THEN $1 -- modify only if "is_public" is false
           ELSE ROW (
               $1.id,
               $1.name, -- some other
               E'\\x00000000', -- hide this
               '', -- hide this
               '[]', -- hide this
               $1.created,
               $1.modified,
               )::messages END AS result;
$$ LANGUAGE SQL;

It works, but the maintainability is poor. Every time I change the messages table I have to change this function at the same time.

Is there a better way?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I assume your problem is having to keep adding public fields into the function if they are added to messages. You might have a little more flexibility at the cost of some speed with

CREATE FUNCTION hide(messages) RETURNS messages AS
$$
DECLARE
    retval messages;
BEGIN
    IF $1.is_public THEN
      retval = $1; -- copy
      SELECT 0, '', '[]' INTO retval.private_field1,
          retval.private_field2, retval.private_field3;
      RETURN retval;
    ELSE RETURN $1;
    END IF;
END
$$ LANGUAGE PLPGSQL;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda