#3: create account in SQL not in Python.

This commit is contained in:
Dominik Pantůček 2025-04-20 17:42:12 +02:00
parent 111a8c9b63
commit c5d1fc3402
3 changed files with 57 additions and 1 deletions

View file

@ -40,7 +40,8 @@ class Account:
@classmethod
def create(cls, db, name, currency, acctype):
""" Constructor for new account """
id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype])
# id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype])
id = db.execute_and_fetch("SELECT public.create_account(%s, %s, %s)", [name, currency.id, acctype])
id = id[0]
return cls(db, name = name, id = id, currency = currency, acctype = acctype)

View file

@ -23,6 +23,9 @@
-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--
-- Require fully-qualified names
SELECT pg_catalog.set_config('search_path', '', false);
DO $upgrade_block$
BEGIN

View file

@ -0,0 +1,52 @@
--
-- 0003-new-account.sql
--
-- #3 - stored procedure for creating new account
--
-- ISC License
--
-- Copyright 2023-2025 Brmlab, z.s.
-- Dominik Pantůček <dominik.pantucek@trustica.cz>
--
-- Permission to use, copy, modify, and/or distribute this software
-- for any purpose with or without fee is hereby granted, provided
-- that the above copyright notice and this permission notice appear
-- in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--
-- Require fully-qualified names
SELECT pg_catalog.set_config('search_path', '', false);
DO $upgrade_block$
BEGIN
IF brmbar_privileged.has_exact_schema_version(2) THEN
CREATE OR REPLACE FUNCTION public.create_account(
IN i_name public.accounts.name%TYPE,
IN i_currency public.accounts.currency%TYPE,
IN i_acctype public.accounts.acctype%TYPE
) RETURNS INTEGER LANGUAGE plpgsql AS $$
DECLARE
r_id INTEGER;
BEGIN
INSERT INTO public.accounts (name, currency, acctype)
VALUES (i_name, i_currency, i_acctype) RETURNING id INTO r_id;
RETURN r_id;
END
$$;
PERFORM brmbar_privileged.upgrade_schema_version_to(3);
END IF;
END;
$upgrade_block$;