/*
 * MG_BITMAP.pgc - mg 모듈 서비스 MG_BITMAP (one service = one file).
 * Split out of the former inline mg_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by mg_svr at tpsvrinit.
 */
#include "MG_BITMAP.h"

/* ----- service-local pure helper(s) ----- */
/* 16-hex primary bitmap from a 64-bit field-present mask. */
static void mg_bitmap_hex(unsigned long mask, char *out /* >= 17 */)
{
    sprintf(out, "%016lX", mask);
}


/* 4. MG_BITMAP - 비트맵 계산 후 해당 전문(msg_id)에 반영. */
void MG_BITMAP(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char hex[24];
    unsigned long mask = (unsigned long)getl(b, T_ARG1);   /* 필드 존재 마스크 */
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    char h_bitmap[40];
    EXEC SQL END DECLARE SECTION;

    mg_bitmap_hex(mask, hex);
    sets_(b, T_STR6, hex);

    h_id = getl(b, T_ID1);
    if (h_id > 0) {
        strncpy(h_bitmap, hex, sizeof(h_bitmap)-1); h_bitmap[sizeof(h_bitmap)-1] = 0;
        EXEC SQL UPDATE mg_msg_log SET bitmap = :h_bitmap WHERE msg_id = :h_id;
        if (sqlca.sqlcode < 0) { userlog("MG_BITMAP UPDATE FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    }
    OK(b);
}
