/*
 * st_laststatus_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_laststatus_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 최근 정산 상태 1건 - ORDER BY LIMIT single fetch (문자열 out).
 */
#include <string.h>
#include <userlog.h>
#include "st_laststatus_dbio.h"

int stdb_last_status(long pid, char *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid;
    char h_status[16];
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT status INTO :h_status FROM settlement
        WHERE purchase_id = :h_pid ORDER BY settlement_id DESC LIMIT 1;
    if (sqlca.sqlcode == 100) { strcpy(out, "NONE"); return 0; }
    if (sqlca.sqlcode < 0) { userlog("stdb_last_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    strncpy(out, h_status, 15); out[15] = 0;
    return 0;
}
