/*
 * rc_excnotexists_dbio.pgc - 예외가 없는 대사 건수 (rc_excnotexists_dbio), librcdbio.a.
 * ECPG; 상관 NOT EXISTS 로 rc_exception 에 매칭 purchase_id 가 없는 rc_match_log 를 센다.
 */
#include <string.h>
#include <userlog.h>
#include "rc_excnotexists_dbio.h"

long rcdb_clean_matches(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 rc_match_log m
        WHERE m.biz_date = :h_bizdate
          AND NOT EXISTS (SELECT 1 FROM rc_exception e WHERE e.purchase_id = m.purchase_id);
    if (sqlca.sqlcode < 0) { userlog("rcdb_clean_matches FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
