/*
 * mm_fx_upsert_dbio.pgc - mm 모듈 DB 접근 함수 (mm_fx_upsert_dbio), archived into libmmdbio.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 "mm_fx_upsert_dbio.h"

/* mm_fx_rate 에 통화별 환율을 upsert. */
int mmdb_fx_upsert(const char *ccy, long rate_bps, const char *base_date)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_ccy[8], h_date[16];
    long h_rate = rate_bps;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_ccy, ccy, sizeof(h_ccy)-1); h_ccy[sizeof(h_ccy)-1] = 0;
    strncpy(h_date, base_date, sizeof(h_date)-1); h_date[sizeof(h_date)-1] = 0;
    EXEC SQL INSERT INTO mm_fx_rate (ccy, rate_bps, base_date)
        VALUES (:h_ccy, :h_rate, :h_date)
        ON CONFLICT (ccy) DO UPDATE
        SET rate_bps = EXCLUDED.rate_bps, base_date = EXCLUDED.base_date, updated_at = now();
    if (sqlca.sqlcode < 0) { userlog("mmdb_fx_upsert FAIL [%d]", sqlca.sqlcode); return -1; }
    return 0;
}
