/*
 * mg_reqfail_dbio.pgc - mg 모듈 DB 접근 함수 (mg_reqfail_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 응답오류(rc<>00) 전문을 재전송 큐로 적재 (INSERT..SELECT).
 */
#include <string.h>
#include <userlog.h>
#include "mg_reqfail_dbio.h"

long mgdb_enqueue_failed(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL INSERT INTO mg_queue
        (queue_id, stan, channel, mti, payload, status, biz_date)
        SELECT nextval('mg_queue_seq'), m.stan, m.channel, m.mti, m.raw_msg, 'Q', m.biz_date
        FROM mg_msg_log m
        WHERE m.biz_date = :h_bizdate AND m.rc <> '00' AND m.direction = 'IN';
    if (sqlca.sqlcode < 0) { userlog("mgdb_enqueue_failed FAIL [%d]", sqlca.sqlcode); return -1; }
    return (long) sqlca.sqlerrd[2];
}
