/*
 * cl_seq_last_dbio.pgc - cl 모듈 DB 접근 함수 (cl_seq_last_dbio), archived into libcldbio.a.
 * 마감유형 마지막 순번 조회.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_seq_last_dbio.h"

/* close_type 의 마지막 발번 순번(없으면 0). */
long clqdb_seq_last(const char *ctype)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_type[16];
    long h_seq = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_type, ctype, sizeof(h_type)-1); h_type[sizeof(h_type)-1] = 0;
    EXEC SQL SELECT coalesce(last_seq,0) INTO :h_seq FROM cl_seq_reg WHERE close_type = :h_type;
    if (sqlca.sqlcode == 100) return 0;
    if (sqlca.sqlcode < 0) { userlog("clqdb_seq_last FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_seq;
}
