/*
 * vl_bin_query_dbio.pgc - BIN 등록/활성 여부 검증 (libvldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "vl_bin_query_dbio.h"

int vldb_bin_active(const char *bin)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt, h_active; char h_bin[16];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bin, bin, sizeof(h_bin)-1); h_bin[sizeof(h_bin)-1] = 0;
    EXEC SQL SELECT count(*), coalesce(sum(CASE WHEN status='ACTIVE' THEN 1 ELSE 0 END),0)
        INTO :h_cnt, :h_active FROM mm_bin WHERE bin = :h_bin;
    if (sqlca.sqlcode < 0) { userlog("vldb_bin_active FAIL [%d]", sqlca.sqlcode); return -1; }
    if (h_cnt == 0) return 1;
    if (h_active == 0) return 2;
    return 0;
}
