/*
 * MM_CODE_REG.pgc - mm 모듈 서비스 MM_CODE_REG (one service = one file).
 * 기능: 공통코드 등록.
 * 기존 인라인 mm_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * mm_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "MM_CODE_REG.h"

void MM_CODE_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char grp[32], code[32], name[64];
    EXEC SQL BEGIN DECLARE SECTION;
    char h_grp[32], h_code[32], h_name[64];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_STR1, grp, sizeof(grp));   if (grp[0] == 0) strcpy(grp, "GEN");
    gets_(b, T_KEY1, code, sizeof(code));
    if (code[0] == 0) { userlog("MM_CODE_REG: T_KEY1(code) 누락"); FAIL(b); }
    gets_(b, T_STR2, name, sizeof(name));
    strncpy(h_grp,  grp,  sizeof(h_grp)-1);  h_grp[sizeof(h_grp)-1]   = 0;
    strncpy(h_code, code, sizeof(h_code)-1); h_code[sizeof(h_code)-1] = 0;
    strncpy(h_name, name, sizeof(h_name)-1); h_name[sizeof(h_name)-1] = 0;
    EXEC SQL INSERT INTO mm_code (code_group, code, code_name)
        VALUES (:h_grp, :h_code, :h_name)
        ON CONFLICT (code_group, code) DO UPDATE SET code_name = EXCLUDED.code_name;
    if (sqlca.sqlcode < 0) { userlog("MM_CODE_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
