/*
 * vl_promote_dbio.pgc - vl 모듈 DB 접근 함수 (vl_promote_dbio), archived into libvldbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 실패 검증로그(result_code>0)를 의심거래로 승격 (INSERT..SELECT).
 */
#include <string.h>
#include <userlog.h>
#include "vl_promote_dbio.h"

long vldb_promote_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 vl_suspect
        (suspect_id, purchase_id, rule_code, severity, detail, biz_date)
        SELECT nextval('vl_suspect_seq'), l.target_id, 'CHK_FAIL', 'WARN', l.detail, l.biz_date
        FROM vl_check_log l
        WHERE l.biz_date = :h_bizdate AND l.result_code > 0;
    if (sqlca.sqlcode < 0) { userlog("vldb_promote_failed FAIL [%d]", sqlca.sqlcode); return -1; }
    return (long) sqlca.sqlerrd[2];
}
