/*
 * lg_merch_balance_dbio.pgc - lg 모듈 DB 접근 함수 (lg_merch_balance_dbio), archived into liblgdbio.a.
 * 가맹점 잔액 조회 (merch_bal).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_merch_balance_dbio.h"

/* 가맹점 x 일자 잔액. */
long lgqdb_merch_balance(const char *merch, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_bizdate[16];
    long h_bal = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(balance, 0) INTO :h_bal FROM lg_merch_bal
        WHERE merchant_id = :h_merch AND biz_date = :h_bizdate;
    if (sqlca.sqlcode == 100) return 0;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_merch_balance FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_bal;
}
