/*
 * rc_exccursor_dbio.pgc - rc 모듈 DB 접근 함수 세트 (rc_exccursor_dbio), archived into librcdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * EXCEPTION 대사 금액 커서 누적 - 부호 무시 절대합.
 */
#include <string.h>
#include <userlog.h>
#include "rc_exccursor_dbio.h"

long rcdb_exception_amount_accum(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_amt, 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 eac CURSOR FOR
        SELECT amount FROM rc_match_log WHERE biz_date = :h_bizdate AND status = 'EXCEPTION';
    EXEC SQL OPEN eac;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH eac INTO :h_amt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE eac; return -1; }
        total += (h_amt < 0) ? -h_amt : h_amt;
    }
    EXEC SQL CLOSE eac;
    return total;
}
