/*
 * au_auth_query_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_auth_query_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: plain scalar SELECT (단건 스칼라 조회).
 */
#include <string.h>
#include <userlog.h>
#include "au_auth_query_dbio.h"

/* 단일 승인의 금액. */
long audb_auth_amount(long auth_id)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_v = 0;
    EXEC SQL END DECLARE SECTION;
    h_id = auth_id;
    EXEC SQL SELECT coalesce(amount,0) INTO :h_v FROM au_authorization WHERE auth_id = :h_id;
    if (sqlca.sqlcode < 0) { userlog("audb_auth_amount FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}

/* 카드의 전체 승인 건수. */
long audb_auth_card_count(const char *card)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_card[24];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_card, card, sizeof(h_card)-1); h_card[sizeof(h_card)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_v FROM au_authorization WHERE card_no = :h_card;
    if (sqlca.sqlcode < 0) { userlog("audb_auth_card_count FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
