/*
 * mm_hist_query_dbio.pgc - 가맹점 변경 이력 건수/최근액션 (libmmdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_hist_query_dbio.h"

long mmdb_hist_count(const char *merch)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt; char h_merch[64];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM mm_merch_hist WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0) { userlog("mmdb_hist_count FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_cnt;
}

int mmdb_last_action(const char *merch, char *act_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_act[16];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    EXEC SQL SELECT action INTO :h_act FROM mm_merch_hist
        WHERE merchant_id = :h_merch ORDER BY hist_id DESC LIMIT 1;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { userlog("mmdb_last_action FAIL [%d]", sqlca.sqlcode); return -1; }
    strncpy(act_out, h_act, 15); act_out[15] = 0;
    return 0;
}
