/*
 * cl_status_filter_dbio.pgc - cl 모듈 DB 접근 함수 (cl_status_filter_dbio), archived into libcldbio.a.
 * 마감 상태별 건수 한 행 분리집계 (FILTER).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_status_filter_dbio.h"

/* 개시(O)/완료(C)/잠금(L) 건수를 FILTER 로 한 번에 반환. */
int clqdb_status_filter(const char *prefix, long *opn, long *closed, long *locked)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_prefix[16];
    long h_o = 0, h_c = 0, h_l = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_prefix, prefix, sizeof(h_prefix)-1); h_prefix[sizeof(h_prefix)-1] = 0;
    EXEC SQL SELECT
        count(*) FILTER (WHERE status = 'O'),
        count(*) FILTER (WHERE status = 'C'),
        count(*) FILTER (WHERE status = 'L')
        INTO :h_o, :h_c, :h_l FROM cl_close_log
        WHERE period_key LIKE :h_prefix || '%';
    if (sqlca.sqlcode < 0) return -1;
    *opn = h_o; *closed = h_c; *locked = h_l;
    return 0;
}
