Re: Best way to insert a row with two columns having the value based on the same argument generated by another postgres function.

Поиск
Список
Период
Сортировка
От Sándor Daku
Тема Re: Best way to insert a row with two columns having the value based on the same argument generated by another postgres function.
Дата
Msg-id CAKyoTgan4rBM86qjteE8=cc_kUrz9SaGOasMXVtPhwB8Zs9hTA@mail.gmail.com
обсуждение исходный текст
Ответ на Best way to insert a row with two columns having the value based on the same argument generated by another postgres function.  (Silk Parrot <silkparrot@gmail.com>)
Список pgsql-general
On 7 July 2016 at 08:56, Silk Parrot <silkparrot@gmail.com> wrote:
Hi,
 
   I am trying to build a user database. The steps for creating a new user are:

1. Use gen_salt to create a salt.
2. Compute the hash based on the salt and password and store both the hash and the salt into a new row.


The pl/pgsql would look like this:

CREATE OR REPLACE FUNCTION system.create_enduser(IN emailArg TEXT, IN passwordArg TEXT, IN nicknameArg TEXT, IN roundsArg int) RETURNS TEXT AS
$$
#print_strict_params on
DECLARE
salt TEXT;
inserted_uuid TEXT;
BEGIN
salt := public.gen_salt('bf', roundsArg);
INSERT INTO system.enduser (email, password_hash, password_salt, nickname, user_state) VALUES (emailArg, public.crypt(passwordArg, salt), salt, nicknameArg, 'REGISTERED') RETURNING uuid into inserted_uuid;
RETURN inserted_uuid;
END
$$
LANGUAGE 'plpgsql' VOLATILE
;

Is there a way to do this in a single SQL statement without using a function? One way I can think of is using trigger, but that still requires another setup.

Appreciate any help.

-- 
Regards
Ryan

A CTE would do that.


Regards,
Sándor

В списке pgsql-general по дате отправления:

Предыдущее
От: Silk Parrot
Дата:
Сообщение: Best way to insert a row with two columns having the value based on the same argument generated by another postgres function.
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: Best way to insert a row with two columns having the value based on the same argument generated by another postgres function.