/*
 * rc_excupdate_dbio.pgc - 당일 미해결 예외 일괄 해결 (rc_excupdate_dbio), librcdbio.a.
 * ECPG; FOR UPDATE 커서를 순회하며 WHERE CURRENT OF 로 각 행을 resolved=true 로 갱신.
 */
#include <string.h>
#include <userlog.h>
#include "rc_excupdate_dbio.h"

long rcdb_resolve_stale_exc(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_id, fixed = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE euc CURSOR FOR
        SELECT exc_id FROM rc_exception
        WHERE biz_date = :h_bizdate AND resolved = false FOR UPDATE;
    EXEC SQL OPEN euc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH euc INTO :h_id;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE euc; return -1; }
        EXEC SQL UPDATE rc_exception SET resolved = true WHERE CURRENT OF euc;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE euc; return -1; }
        fixed++;
    }
    EXEC SQL CLOSE euc;
    return fixed;
}
