/*
 * st_heldcursor_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_heldcursor_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 보류(HELD) 지급액 커서 누적 합계.
 */
#include <string.h>
#include <userlog.h>
#include "st_heldcursor_dbio.h"

long stdb_held_payment_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 hpc CURSOR FOR
        SELECT net_amount FROM st_payment WHERE biz_date = :h_bizdate AND status = 'HELD';
    EXEC SQL OPEN hpc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH hpc INTO :h_amt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE hpc; return -1; }
        total += h_amt;
    }
    EXEC SQL CLOSE hpc;
    return total;
}
