/*
 * cl_type_having_dbio.pgc - cl 모듈 DB 접근 함수 (cl_type_having_dbio), archived into libcldbio.a.
 * 마감유형별 건수 임계 초과 유형 수 (GROUP BY ... HAVING 커서).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_type_having_dbio.h"

/* close_type 별 마감 건수가 min_cnt 이상인 유형 개수. */
long clqdb_type_having(long min_cnt)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_min = min_cnt, h_cnt;
    char h_type[16];
    EXEC SQL END DECLARE SECTION;
    long types = 0;
    EXEC SQL DECLARE cth CURSOR FOR
        SELECT close_type, count(*) FROM cl_close_log
        WHERE status <> 'X' GROUP BY close_type HAVING count(*) >= :h_min
        ORDER BY close_type;
    EXEC SQL OPEN cth;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH cth INTO :h_type, :h_cnt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE cth; return -1; }
        types++;
    }
    EXEC SQL CLOSE cth;
    return types;
}
