/*
 * cl_max_close_dbio.pgc - cl 모듈 DB 접근 함수 (cl_max_close_dbio), archived into libcldbio.a.
 * 당일 최대 마감 ID.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_max_close_dbio.h"

/* 당일 채번된 최대 마감 ID (없으면 0). */
long clqdb_max_close(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_max = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(max(close_id), 0) INTO :h_max
        FROM cl_close_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("clqdb_max_close FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_max;
}
