/*
 * lg_stale_purge_dbio.pgc - lg 모듈 DB 접근 함수 (lg_stale_purge_dbio), archived into liblgdbio.a.
 * 오래된 취소(X) 전표 정리 (delete count).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_stale_purge_dbio.h"

/* before 이전 취소(X) 전표와 그 라인 삭제, 삭제 라인 수 반환. */
long lgqdb_stale_purge(const char *before)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_before[16];
    long h_lines;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_before, before, sizeof(h_before)-1); h_before[sizeof(h_before)-1] = 0;
    EXEC SQL DELETE FROM lg_ledger_line WHERE voucher_id IN
        (SELECT voucher_id FROM lg_voucher WHERE status = 'X' AND biz_date < :h_before);
    if (sqlca.sqlcode < 0) { userlog("lgqdb_stale_purge line FAIL [%d]", sqlca.sqlcode); return -1; }
    h_lines = sqlca.sqlerrd[2];
    EXEC SQL DELETE FROM lg_voucher WHERE status = 'X' AND biz_date < :h_before;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_stale_purge hdr FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_lines;
}
