/*
 * rc_sumupsert_dbio.pgc - 요약 증분 upsert (rc_sumupsert_dbio), librcdbio.a.
 * ECPG; ON CONFLICT (biz_date) DO UPDATE 로 matched_cnt / gross_amount 를 누적한다.
 */
#include <string.h>
#include <userlog.h>
#include "rc_sumupsert_dbio.h"

int rcdb_bump_summary(const char *bizdate, long matched, long gross)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_m = matched, h_g = gross;
    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_summary (biz_date, matched_cnt, unmatched_cnt, gross_amount)
        VALUES (:h_bizdate, :h_m, 0, :h_g)
        ON CONFLICT (biz_date) DO UPDATE
        SET matched_cnt = rc_summary.matched_cnt + EXCLUDED.matched_cnt,
            gross_amount = rc_summary.gross_amount + EXCLUDED.gross_amount,
            updated_at = now();
    if (sqlca.sqlcode < 0) { userlog("rcdb_bump_summary FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return 0;
}
