/*
 * py_schedule_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_schedule_dbio), archived into libpydbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_schedule_dbio.h"

/* 지정 실행일의 대기(S) 예약 건수. */
long pydb_schedule_pending_count(const char *rundate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_rundate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_rundate, rundate, sizeof(h_rundate)-1); h_rundate[sizeof(h_rundate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_v FROM py_schedule WHERE run_date = :h_rundate AND status = 'S';
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}

/* 예약 시퀀스 다음 값. */
long pydb_schedule_next_id(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT nextval('py_schedule_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) { userlog("pydb_schedule_next_id FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_id;
}
