/*
 * vl_rulejoin_dbio.pgc - vl 모듈 DB 접근 함수 (vl_rulejoin_dbio), archived into libvldbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 미결 의심거래에 걸린 규칙 임계값 합 (vl_suspect JOIN vl_rule).
 */
#include <string.h>
#include <userlog.h>
#include "vl_rulejoin_dbio.h"

long vldb_open_threshold_sum(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(r.threshold),0) INTO :h_v
        FROM vl_suspect s JOIN vl_rule r ON r.rule_code = s.rule_code
        WHERE s.biz_date = :h_bizdate AND s.status = 'OPEN';
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
