/*
 * mg_unrouted_dbio.pgc - mg 모듈 DB 접근 함수 (mg_unrouted_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 라우팅 룰이 없는 전문 건수 (NOT EXISTS mg_route).
 */
#include <string.h>
#include <userlog.h>
#include "mg_unrouted_dbio.h"

long mgdb_unrouted_msgs(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 count(*) INTO :h_v FROM mg_msg_log m
        WHERE m.biz_date = :h_bizdate
          AND NOT EXISTS (SELECT 1 FROM mg_route r
                          WHERE r.channel = m.channel AND r.mti = m.mti AND r.active);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
