/*
 * st_dtlgroup_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_dtlgroup_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 수수료 종류별 GROUP BY 커서 - 종류 수 반환.
 */
#include <string.h>
#include <userlog.h>
#include "st_dtlgroup_dbio.h"

long stdb_dtl_fee_kinds(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_type[16];
    long h_sum, kinds = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE dgc CURSOR FOR
        SELECT fee_type, coalesce(sum(amount),0) FROM st_settle_dtl
        WHERE biz_date = :h_bizdate GROUP BY fee_type ORDER BY fee_type;
    EXEC SQL OPEN dgc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH dgc INTO :h_type, :h_sum;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE dgc; return -1; }
        kinds++;
    }
    EXEC SQL CLOSE dgc;
    return kinds;
}
