/*
 * mg_err_ratio_dbio.pgc - mg 모듈 DB 접근 함수 (mg_err_ratio_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 오류 전문 비율(bps).
 */
#include <string.h>
#include <userlog.h>
#include "mg_err_ratio_dbio.h"

long mgdb_msg_err_ratio(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_a = 0, h_b = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_a FROM mg_msg_log WHERE biz_date = :h_bizdate AND status = 'N';
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL SELECT count(*) INTO :h_b FROM mg_msg_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_b <= 0) return 0;
    return h_a * 10000 / h_b;
}
