/*
 * rc_excpct_dbio.pgc - rc 모듈 DB 접근 함수 세트 (rc_excpct_dbio), archived into librcdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 예외율(%) - 두 서브쿼리 비율 계산.
 */
#include <string.h>
#include <userlog.h>
#include "rc_excpct_dbio.h"

long rcdb_exception_pct(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_exc = 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
        (SELECT count(*) FROM rc_match_log WHERE biz_date = :h_bizdate AND status = 'EXCEPTION'),
        (SELECT count(*) FROM rc_match_log WHERE biz_date = :h_bizdate)
        INTO :h_exc, :h_all;
    if (sqlca.sqlcode < 0) { userlog("rcdb_exception_pct FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_all > 0 ? (h_exc * 100 / h_all) : 0;
}
