/*
 * mm_noterm_dbio.pgc - mm 모듈 DB 접근 함수 (mm_noterm_dbio), archived into libmmdbio.a.
 * 단말 미보유 활성 가맹점 수 (NOT EXISTS).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_noterm_dbio.h"

/* mm_terminal 에 단말이 하나도 없는 활성 가맹점 수. */
long mmdb_noterm_count(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_v FROM mm_merchant_ext m
        WHERE m.status = 'ACTIVE'
          AND NOT EXISTS (SELECT 1 FROM mm_terminal t
                          WHERE t.merchant_id = m.merchant_id AND t.status = 'ACTIVE');
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
