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

/* 최대 잔액 계좌의 잔액 (무인자). */
long pydb_account_top_balance(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT coalesce(max(balance),0) INTO :h_v FROM py_account;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}

/* 임계 미만 잔액 계좌 수 (threshold 인자). */
long pydb_account_low_count(long threshold)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_thr = threshold, h_v = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_v FROM py_account WHERE balance < :h_thr;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
