/*
 * cm_fx_highpct_dbio.pgc - cm 모듈 DB 접근 함수 (cm_fx_highpct_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 고환율 비율(%).
 */
#include <string.h>
#include <userlog.h>
#include "cm_fx_highpct_dbio.h"

long cmdb_fx_high_pct(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_hi = 0, h_all = 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 WHEN rate_bps > 1000000 THEN 1 ELSE 0 END),0), count(*)
        INTO :h_hi, :h_all FROM cm_fx_rate WHERE base_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_all <= 0) return 0;
    return h_hi * 100 / h_all;
}
