/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/cm_ol_0024.pgc"
/* @tier easy @module cm @transform common-online */
/*
 * cm_ol_0024.pgc - 다음 순번 미리보기 서비스 (CM_OL_0024)  [tier=easy]
 *
 * 채번키(SEQKEY)로 채번 마스터(cm_seq)의 현재값/증분을 조회(갱신 없음)하여
 * 다음에 발급될 순번을 미리 계산해 응답한다. 실제 채번 확정은 별도 서비스가
 * 수행하며 본 서비스는 read-only 미리보기다. (ATMI void SVC(TPSVCINFO*) 대응)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "cm_core.h"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 17 "app/online/cm_ol_0024.pgc"


TX_SERVICE(CM_OL_0024, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
    
#line 22 "app/online/cm_ol_0024.pgc"
 char h_seqkey [ 24 ] ;
 
#line 23 "app/online/cm_ol_0024.pgc"
 long h_curval ;
 
#line 24 "app/online/cm_ol_0024.pgc"
 long h_incby ;
 
#line 25 "app/online/cm_ol_0024.pgc"
 long h_maxval ;
/* exec sql end declare section */
#line 26 "app/online/cm_ol_0024.pgc"


    char seqkey[24];
    long nextval;

    tx_log(TX_LOG_INFO, "[CM_OL_0024] 다음 순번 미리보기 서비스 진입");

    if (tx_buf_get(ctx->in, "SEQKEY", seqkey, sizeof(seqkey)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0024] SEQKEY 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(&h_seqkey, 0, sizeof(h_seqkey));
    strncpy(h_seqkey, seqkey, sizeof(h_seqkey) - 1);
    h_curval = 0;
    h_incby  = 1;
    h_maxval = 0;

    /* 현재값만 읽고 갱신하지 않는다 (미리보기) */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select cur_val , inc_by , max_val from cm_seq where seq_key = $1 ", 
	ECPGt_char,(h_seqkey),(long)24,(long)1,(24)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_curval),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_incby),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_maxval),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 50 "app/online/cm_ol_0024.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0024] 채번키 없음 seqkey=%s", seqkey);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0024 SELECT cm_seq");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    if (h_incby <= 0)
        h_incby = 1;

    nextval = h_curval + h_incby;

    /* 상한 초과 시 순환(wrap-around) 예측 */
    if (h_maxval > 0 && nextval > h_maxval) {
        nextval = 1;
        tx_log(TX_LOG_WARN, "[CM_OL_0024] 순번 상한 초과 → 순환 예측 seqkey=%s", seqkey);
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "SEQKEY", seqkey);
    tx_buf_setlong(ctx->out, "CURVAL", h_curval);
    tx_buf_setlong(ctx->out, "NEXTVAL", nextval);
    tx_log(TX_LOG_INFO, "[CM_OL_0024] 미리보기 완료 seqkey=%s cur=%ld next=%ld",
           seqkey, h_curval, nextval);
    tx_return(ctx, TX_OK, ctx->out);
}
