/*
 * au_limit_agg_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_limit_agg_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: table-wide aggregate + comparison-predicate count (인자 없음).
 */
#include <string.h>
#include <userlog.h>
#include "au_limit_agg_dbio.h"

/* 전체 카드의 사용금액 총합. */
long audb_limit_total_used(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT coalesce(sum(used_amount),0) INTO :h_v FROM card_limit;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}

/* 한도를 초과 사용한(used_amount > daily_limit) 카드 수. */
long audb_limit_over_cards(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_v FROM card_limit WHERE used_amount > daily_limit;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
