/*
 * mg_mtigroup_dbio.pgc - mg 모듈 DB 접근 함수 (mg_mtigroup_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 임계 이상 응답오류가 난 전문유형(MTI) 그룹 수 (GROUP BY HAVING 커서).
 */
#include <string.h>
#include <userlog.h>
#include "mg_mtigroup_dbio.h"

long mgdb_mti_err_groups(const char *bizdate, long minc)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_mti[8];
    long h_min, h_cnt, groups = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    h_min = minc;
    EXEC SQL DECLARE meg CURSOR FOR
        SELECT mti, count(*) FROM mg_msg_log
        WHERE biz_date = :h_bizdate AND rc <> '00'
        GROUP BY mti HAVING count(*) >= :h_min;
    EXEC SQL OPEN meg;
    if (sqlca.sqlcode < 0) { userlog("mgdb_mti_err_groups OPEN [%d]", sqlca.sqlcode); return -1; }
    for (;;) {
        EXEC SQL FETCH meg INTO :h_mti, :h_cnt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE meg; return -1; }
        groups++;
    }
    EXEC SQL CLOSE meg;
    return groups;
}
