/*
 * mm_fx_query_dbio.pgc - 환율 조회 + 원화 환산 (libmmdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_fx_query_dbio.h"

long mmdb_fx_rate(const char *ccy)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_rate; char h_ccy[8];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_ccy, ccy, sizeof(h_ccy)-1); h_ccy[sizeof(h_ccy)-1] = 0;
    EXEC SQL SELECT rate_bps INTO :h_rate FROM mm_fx_rate WHERE ccy = :h_ccy;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { userlog("mmdb_fx_rate FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_rate;
}

long mmdb_fx_to_krw(const char *ccy, long amt)
{
    long rate = mmdb_fx_rate(ccy);
    if (rate < 0) return -1;
    return amt * rate / 100;
}
