/*
 * cl_lock_walk_dbio.pgc - cl 모듈 DB 접근 함수 (cl_lock_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_lock_walk_dbio.h"

/* period_lock 을 커서로 훑어 잠긴(locked) 기간 수 누적. */
long clqdb_lock_walk(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_period[16];
    int  h_locked;
    EXEC SQL END DECLARE SECTION;
    long locked = 0;
    EXEC SQL DECLARE clw CURSOR FOR
        SELECT period_key, CASE WHEN locked THEN 1 ELSE 0 END FROM cl_period_lock;
    EXEC SQL OPEN clw;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH clw INTO :h_period, :h_locked;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE clw; return -1; }
        if (h_locked) locked++;
    }
    EXEC SQL CLOSE clw;
    return locked;
}
