/*
 * py_payout_retryhist_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_payout_retryhist_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_payout_retryhist_dbio.h"

/* GROUP BY retry_count HAVING: 건수 >= min_share 인 재시도 버킷 수. */
long pydb_payout_retry_buckets(const char *bizdate, long min_share)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_min = min_share, h_cnt = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM (
        SELECT retry_count FROM py_payout WHERE biz_date = :h_bizdate
        GROUP BY retry_count HAVING count(*) >= :h_min) b;
    if (sqlca.sqlcode < 0) { userlog("pydb_payout_retry_buckets FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_cnt;
}
