/*
 * cm_log_rollup_dbio.pgc - cm 모듈 DB 접근 함수 (cm_log_rollup_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_log_rollup_dbio.h"

long cmdb_log_svc_rollup(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_key[32];
    long h_amt = 0, total = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE qc CURSOR FOR
        SELECT svc_name, coalesce(sum(log_id),0) FROM cm_log
        WHERE biz_date = :h_bizdate GROUP BY svc_name;
    EXEC SQL OPEN qc;
    if (sqlca.sqlcode < 0) { userlog("cmdb_log_svc_rollup OPEN [%d]", sqlca.sqlcode); return -1; }
    for (;;) {
        EXEC SQL FETCH qc INTO :h_key, :h_amt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE qc; return -1; }
        total += h_amt;
    }
    EXEC SQL CLOSE qc;
    return total;
}
