/* 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_0008.pgc"
/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0008.pgc - 세션 공통 검증 서비스 (CM_OL_0008)  [tier=medium]
 *
 * 요청의 세션ID(SESSID)로 공통 세션 테이블(cm_session)을 조회하여
 * 세션 소유자/상태/만료시각을 확인한다. 만료시각과 현재시각 비교는
 * DB 에서 수행하여 유효/만료 여부를 판정해 응답한다.
 * (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 18 "app/online/cm_ol_0008.pgc"


TX_SERVICE(CM_OL_0008, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
    
#line 23 "app/online/cm_ol_0008.pgc"
 char h_sessid [ 40 ] ;
 
#line 24 "app/online/cm_ol_0008.pgc"
 char h_userid [ 16 ] ;
 
#line 25 "app/online/cm_ol_0008.pgc"
 char h_status [ 2 ] ;
 
#line 26 "app/online/cm_ol_0008.pgc"
 char h_expire [ 16 ] ;
 
#line 27 "app/online/cm_ol_0008.pgc"
 char h_expired [ 2 ] ;
/* exec sql end declare section */
#line 28 "app/online/cm_ol_0008.pgc"


    char sessid[40];
    int  valid;

    tx_log(TX_LOG_INFO, "[CM_OL_0008] 세션 공통 검증 서비스 진입");

    memset(sessid, 0, sizeof(sessid));

    if (tx_buf_get(ctx->in, "SESSID", sessid, sizeof(sessid)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0008] 세션ID(SESSID) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_sessid, 0, sizeof(h_sessid));
    memset(h_userid, 0, sizeof(h_userid));
    memset(h_status, 0, sizeof(h_status));
    memset(h_expire, 0, sizeof(h_expire));
    memset(h_expired, 0, sizeof(h_expired));
    strncpy(h_sessid, sessid, sizeof(h_sessid) - 1);

    /* 만료판정은 DB 현재시각과 비교하여 산출 */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select user_id , sess_status , TO_CHAR ( expire_ts , 'YYYYMMDDHH24MISS' ) , case when expire_ts < current_timestamp then 'Y' else 'N' end from cm_session where sess_id = $1 ", 
	ECPGt_char,(h_sessid),(long)40,(long)1,(40)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_userid),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_status),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_expire),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_expired),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 57 "app/online/cm_ol_0008.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0008] 세션 없음 sessid=%s", sessid);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0008 SELECT cm_session");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 유효 = 상태가 활성('A')이고 만료되지 않음 */
    valid = (h_status[0] == 'A' && h_expired[0] == 'N');

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "SESSID", sessid);
    tx_buf_sets(ctx->out, "USERID", h_userid);
    tx_buf_sets(ctx->out, "STATUS", h_status);
    tx_buf_sets(ctx->out, "EXPIRETS", h_expire);
    tx_buf_sets(ctx->out, "VALID", valid ? "Y" : "N");

    tx_log(TX_LOG_INFO, "[CM_OL_0008] 검증완료 sessid=%s user=%s valid=%s",
           sessid, h_userid, valid ? "Y" : "N");
    tx_return(ctx, valid ? TX_OK : TX_FAIL, ctx->out);
}
