/*
 * rc_matchcorr_dbio.pgc - 대사별 예외 동반 여부 (rc_matchcorr_dbio), librcdbio.a.
 * ECPG; SELECT 목록의 상관 서브쿼리로 각 대사의 동일 매입 예외수를 구해 >0 인 건수를 센다.
 */
#include <string.h>
#include <userlog.h>
#include "rc_matchcorr_dbio.h"

long rcdb_matches_with_exc(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_pid, h_ecnt, withexc = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE mcc CURSOR FOR
        SELECT m.purchase_id,
               (SELECT count(*) FROM rc_exception e WHERE e.purchase_id = m.purchase_id)
        FROM rc_match_log m WHERE m.biz_date = :h_bizdate;
    EXEC SQL OPEN mcc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH mcc INTO :h_pid, :h_ecnt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE mcc; return -1; }
        if (h_ecnt > 0) withexc++;
    }
    EXEC SQL CLOSE mcc;
    return withexc;
}
