/*
 * rc_summaryratio_dbio.pgc - rc 모듈 DB 접근 함수 세트 (rc_summaryratio_dbio), archived into librcdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 요약 대사율(%) - CASE 로 0 분모 방지.
 */
#include <string.h>
#include <userlog.h>
#include "rc_summaryratio_dbio.h"

long rcdb_summary_match_pct(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 CASE WHEN (matched_cnt + unmatched_cnt) > 0
        THEN matched_cnt * 100 / (matched_cnt + unmatched_cnt) ELSE 0 END
        INTO :h_v FROM rc_summary WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode == 100) return 0;
    if (sqlca.sqlcode < 0) { userlog("rcdb_summary_match_pct FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
