From 1a838c115140bdf0f35054351153fb21f2a228da Mon Sep 17 00:00:00 2001
From: Masaya Tojo <masaya@tojo.tokyo>
Date: Sun, 12 Mar 2023 16:23:41 +0900
Subject: Add function to create new transaction.

---
 deploy/insert_transaction.sql | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 deploy/insert_transaction.sql

(limited to 'deploy/insert_transaction.sql')

diff --git a/deploy/insert_transaction.sql b/deploy/insert_transaction.sql
new file mode 100644
index 0000000..eb5a38e
--- /dev/null
+++ b/deploy/insert_transaction.sql
@@ -0,0 +1,24 @@
+-- Deploy kakeibo:insert_transaction to pg
+-- requires: schema
+-- requires: transactions
+
+BEGIN;
+
+CREATE OR REPLACE FUNCTION kakeibo.insert_transaction(
+  p_type_code INTEGER,
+  p_date DATE,
+  p_note TEXT
+) RETURNS INTEGER AS $$
+  DECLARE
+    inserted_id INTEGER;
+  BEGIN
+    INSERT INTO kakeibo.transactions (
+      type_code, date, note
+    ) VALUES (
+      p_type_code, p_date, p_note
+    ) RETURNING id INTO inserted_id;
+    RETURN inserted_id;
+  END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+COMMIT;
-- 
cgit v1.2.3

