/*
 * cl_lock_upsert_dbio.pgc - cl 모듈 DB 접근 함수 (cl_lock_upsert_dbio), archived into libcldbio.a.
 * 기간잠금 upsert (ON CONFLICT).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_lock_upsert_dbio.h"

/* cl_period_lock 에 잠금 여부를 upsert. */
int cldb_lock_upsert(const char *period, int locked)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_period[16];
    int h_locked = locked;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_period, period, sizeof(h_period)-1); h_period[sizeof(h_period)-1] = 0;
    EXEC SQL INSERT INTO cl_period_lock (period_key, locked)
        VALUES (:h_period, :h_locked)
        ON CONFLICT (period_key) DO UPDATE
        SET locked = EXCLUDED.locked, locked_at = now();
    if (sqlca.sqlcode < 0) { userlog("cldb_lock_upsert FAIL [%d]", sqlca.sqlcode); return -1; }
    return 0;
}
