aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_check_register_record_lock
Parameters
Name
Type
Mode
i_register_id
integer
IN
i_user_name
text
IN
i_session_id
text
IN
Definition
declare v_msg_text text; v_sqlstate text; v_detail text; v_hint text; v_context text; v_lock_id bigint; v_owner_lock record; v_timeout timestamptz; -- v_timeout_minute_config integer; v_result json; v_lock_success boolean; v_final_result json; v_get_record_lock json; v_search json; begin -- อ่าน timeout จาก config -- v_timeout_minute_config := fn_prop_config('record_lock_time_out_minute')::integer; -- หา record lock ปัจจุบัน v_get_record_lock := fn_get_record_lock( user_name := i_user_name, session_id := i_session_id, booking_id := null, booking_item := null, block_id := null, register_id := i_register_id ); if v_get_record_lock is null then -- === ไม่มีใคร lock อยู่ -- === สามารถทำการ lock ได้เลย select sp_lock_registration( i_user_name, i_session_id, i_register_id ) into v_lock_id; v_lock_success:=true; else v_lock_id := (v_get_record_lock ->> 'id')::bigint; v_lock_success := false; end if; -- select * into v_owner_lock -- from record_lock lck -- where (lck.register_id is not distinct from i_register_id) -- and (lck.booking_item is null) -- and (lck.block_id is null) -- and (lck.booking_id is null) -- order by lck.lock_time desc -- limit 1; -- -- if found then -- -- v_timeout := v_owner_lock.lock_time + (v_timeout_minute_config || ' minutes')::interval; -- v_timeout := v_owner_lock.lock_time + fn_lock_timeout(); -- if i_user_name = v_owner_lock.lock_user then -- -- user เดิมขอ lock ซ้ำ -- -- if now() > v_timeout then -- -- timeout แล้ว → lock ใหม่ -- select sp_lock_registration( -- i_user_name, -- i_session_id, -- i_register_id -- ) into v_lock_id; -- -- v_lock_success:=true; -- ---- return true; ---- else ---- -- ยังไม่หมด timeout (user เดิม) ---- return true; -- -- end if; -- else -- -- user อื่นขอ lock -- if now() > v_timeout then -- -- timeout หมดแล้ว → lock ใหม่ -- select sp_lock_registration( -- i_user_name, -- i_session_id, -- i_register_id -- ) into v_lock_id; -- -- return true; -- v_lock_success:=true; -- else -- -- ยังไม่หมด timeout → lock ไม่ได้ -- -- return false; -- -- v_lock_success:=false; -- end if; -- end if; -- else -- -- ไม่มี lock record → lock ได้เลย -- select sp_lock_registration( -- i_user_name, -- i_session_id, -- i_register_id -- ) into v_lock_id; -- -- return true; -- v_lock_success:=true; -- end if; -- if v_lock_id is not null then -- select json_agg(to_jsonb(t)) into v_result from record_lock t where t.id=v_lock_id; select to_jsonb(t) into v_result from record_lock t where t.id=v_lock_id; --select to_jsonb(t) into v_result from record_lock t where t.register_id=i_register_id limit 1; else -- select '[]' :: json into v_result; -- select to_jsonb(t) into v_result from record_lock t where t.booking_id=i_register_id limit 1; select to_jsonb(t) into v_result from record_lock t where t.register_id=i_register_id limit 1; end if; select json_build_object( 'lock_complete', v_lock_success, 'data', v_result ) into v_final_result; return fn_result_success(v_final_result); exception when others then get stacked diagnostics v_msg_text = message_text, v_sqlstate = returned_sqlstate, v_detail = pg_exception_detail, v_hint = pg_exception_hint, v_context = pg_exception_context; return fn_handle_error( v_sqlstate, v_msg_text, v_detail, v_hint, v_context, 'sp_check_register_record_lock', null ); end