/*
 * py_schedule_scan_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_schedule_scan_dbio), archived into libpydbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_schedule_scan_dbio.h"

/* 커서로 실행일 예약을 순회하며 대기(S) 상태 건만 집계. */
long pydb_schedule_due_count(const char *rundate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_rundate[16], h_st[8];
    long h_pay, due = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_rundate, rundate, sizeof(h_rundate)-1); h_rundate[sizeof(h_rundate)-1] = 0;
    EXEC SQL DECLARE pydsc CURSOR FOR
        SELECT payout_id, status FROM py_schedule WHERE run_date = :h_rundate;
    EXEC SQL OPEN pydsc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH pydsc INTO :h_pay, :h_st;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE pydsc; return -1; }
        if (h_st[0] == 'S') due++;
    }
    EXEC SQL CLOSE pydsc;
    return due;
}
