/*
 * cm_counter_add_dbio.pgc - cm 모듈 DB 접근 함수 (cm_counter_add_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 카운터 채번 후 적재.
 */
#include <string.h>
#include <userlog.h>
#include "cm_counter_add_dbio.h"

long cmdb_counter_add(const char *bizdate, long amount)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = 0, h_amt = amount;
    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 SELECT nextval('cm_counter_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL INSERT INTO cm_counter (cnt_id, cnt_val, biz_date)
        VALUES (:h_id, :h_amt, :h_bizdate);
    if (sqlca.sqlcode < 0) { userlog("cmdb_counter_add FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_id;
}
