/*
 * cl_cancel_purge_dbio.pgc - cl 모듈 DB 접근 함수 (cl_cancel_purge_dbio), archived into libcldbio.a.
 * 오래된 취소(X) 마감 및 스냅샷 삭제.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_cancel_purge_dbio.h"

/* before 이전 취소(X) 마감과 스냅샷 삭제, 삭제 마감 수 반환. */
long clqdb_cancel_purge(const char *before)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_before[16];
    long h_cnt;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_before, before, sizeof(h_before)-1); h_before[sizeof(h_before)-1] = 0;
    EXEC SQL DELETE FROM cl_snapshot WHERE close_id IN
        (SELECT close_id FROM cl_close_log WHERE status = 'X' AND biz_date < :h_before);
    if (sqlca.sqlcode < 0) { userlog("clqdb_cancel_purge snap FAIL [%d]", sqlca.sqlcode); return -1; }
    EXEC SQL DELETE FROM cl_close_log WHERE status = 'X' AND biz_date < :h_before;
    if (sqlca.sqlcode < 0) { userlog("clqdb_cancel_purge log FAIL [%d]", sqlca.sqlcode); return -1; }
    h_cnt = sqlca.sqlerrd[2];
    return h_cnt;
}
