aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_save_calendar_source
Parameters
Name
Type
Mode
p_data
jsonb
IN
Definition
DECLARE v_state text; v_message text; v_detail text; v_hint text; v_context text; v_rec record; BEGIN SELECT * INTO v_rec FROM jsonb_to_record(p_data) AS t( id int, calendar_id int, source_type text, name text, ics_url text, auto_sync boolean, enabled boolean); IF NOT EXISTS (SELECT 1 FROM public.calendar WHERE id = v_rec.calendar_id) THEN RETURN fn_result_error('50503', sys_msg('50503', 'Calendar not found (%s)'), coalesce(v_rec.calendar_id::text, '')); END IF; IF v_rec.id IS NULL OR v_rec.id < 1 THEN INSERT INTO public.calendar_source(calendar_id, source_type, name, ics_url, auto_sync, enabled) VALUES (v_rec.calendar_id, coalesce(v_rec.source_type, 'ics_url'), v_rec.name, nullif(v_rec.ics_url, ''), coalesce(v_rec.auto_sync, false), coalesce(v_rec.enabled, true)) RETURNING id INTO v_rec.id; ELSE UPDATE public.calendar_source SET calendar_id = v_rec.calendar_id, source_type = coalesce(v_rec.source_type, source_type), name = v_rec.name, ics_url = nullif(v_rec.ics_url, ''), auto_sync = coalesce(v_rec.auto_sync, auto_sync), enabled = coalesce(v_rec.enabled, enabled) WHERE id = v_rec.id; IF NOT FOUND THEN RETURN fn_result_error('50507', sys_msg('50507', 'Calendar source not found (%s)'), v_rec.id::text); END IF; END IF; RETURN fn_result_success((SELECT to_json(s) FROM public.calendar_source s WHERE s.id = v_rec.id)); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS v_state = RETURNED_SQLSTATE, v_message = MESSAGE_TEXT, v_detail = PG_EXCEPTION_DETAIL, v_hint = PG_EXCEPTION_HINT, v_context = PG_EXCEPTION_CONTEXT; RETURN fn_handle_error(v_state, v_message, v_detail, v_hint, v_context, 'public.sp_save_calendar_source', p_data); END