/*
 * ac_unmatch_purge_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_unmatch_purge_dbio), archived into libacdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: DELETE 구간 삭제 - 기준일 이전 미매입 로그 정리, 삭제 건수 반환.
 */
#include <string.h>
#include <userlog.h>
#include "ac_unmatch_purge_dbio.h"

/* cutoff(YYYY-MM-DD) 이전의 ac_unmatch 로그를 삭제하고 삭제 건수를 반환. */
long acdb_unmatch_purge_before(const char *cutoff)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_cut[16];
    long deleted = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_cut, cutoff, sizeof(h_cut)-1); h_cut[sizeof(h_cut)-1] = 0;
    EXEC SQL DELETE FROM ac_unmatch WHERE biz_date < :h_cut;
    if (sqlca.sqlcode < 0) {
        userlog("acdb_unmatch_purge_before FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    deleted = sqlca.sqlerrd[2];
    return deleted;
}
