/*
 * rc_diffinsert_dbio.pgc - 예외 대사로부터 차이 라인 생성 (rc_diffinsert_dbio), librcdbio.a.
 * ECPG; INSERT .. SELECT 로 EXCEPTION 상태 대사를 rc_diff 로 적재. 반환=적재 행수.
 */
#include <string.h>
#include <userlog.h>
#include "rc_diffinsert_dbio.h"

long rcdb_seed_diff_from_exception(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 rc_diff (diff_id, purchase_id, expected_amt, actual_amt, diff_amt, diff_kind, biz_date)
        SELECT nextval('rc_diff_seq'), purchase_id, amount, 0, amount, 'AMT', biz_date
        FROM rc_match_log WHERE biz_date = :h_bizdate AND status = 'EXCEPTION';
    if (sqlca.sqlcode < 0) { userlog("rcdb_seed_diff_from_exception FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return (long) sqlca.sqlerrd[2];
}
