/*
 * cl_status_walk_dbio.pgc - cl 모듈 DB 접근 함수 (cl_status_walk_dbio), archived into libcldbio.a.
 * 마감 커서 순회 - 특정 상태 마감 수 누적.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_status_walk_dbio.h"

/* 커서로 유형별 마감을 훑어 want 상태 건수를 누적. */
long clqdb_status_walk(const char *ctype, const char *want)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_type[16], h_st[8];
    EXEC SQL END DECLARE SECTION;
    long hit = 0;
    char want0 = want[0];
    strncpy(h_type, ctype, sizeof(h_type)-1); h_type[sizeof(h_type)-1] = 0;
    EXEC SQL DECLARE csw CURSOR FOR
        SELECT status FROM cl_close_log WHERE close_type = :h_type ORDER BY close_id;
    EXEC SQL OPEN csw;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH csw INTO :h_st;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE csw; return -1; }
        if (h_st[0] == want0) hit++;
    }
    EXEC SQL CLOSE csw;
    return hit;
}
