/*
 * cm_ccycase_dbio.pgc - cm 모듈 DB 접근 함수 (cm_ccycase_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 통화별 가중치를 적용한 환율 가중합 (CASE 식).
 */
#include <string.h>
#include <userlog.h>
#include "cm_ccycase_dbio.h"

long cmdb_ccy_case_weight(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(CASE ccy
                WHEN 'USD' THEN rate_bps * 3
                WHEN 'EUR' THEN rate_bps * 2
                ELSE rate_bps END), 0)
        INTO :h_v FROM cm_fx_rate WHERE base_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
