/* 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/dbio/ac_dbio_main.pgc"
/* @tier medium @module ac @transform acquire-dbio */
/*
 * ac_dbio_main.pgc - 매입 표준 DBIO (ECPG 임베디드 SQL)
 *
 * ac_core.h 의 표준 API 를 ac_ledger 테이블에 대해 구현한다.
 * 온라인/배치 프로그램은 이 함수들만 호출한다 (self-contained).
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "ac_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 15 "app/dbio/ac_dbio_main.pgc"


int ac_dbio_connect(const char *target)
{
    /* exec sql begin declare section */
      
    
#line 20 "app/dbio/ac_dbio_main.pgc"
 const char * h_target ;
/* exec sql end declare section */
#line 21 "app/dbio/ac_dbio_main.pgc"


    h_target = (target && target[0]) ? target : "acquire@localhost";

    { ECPGconnect(__LINE__, 0, h_target , NULL, NULL , NULL, 0); }
#line 25 "app/dbio/ac_dbio_main.pgc"

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac CONNECT");
        return TX_EDB;
    }
    tx_log(TX_LOG_INFO, "[ac] DB 접속 완료 target=%s", h_target);
    return TX_OK;
}

int ac_dbio_disconnect(void)
{
    { ECPGdisconnect(__LINE__, "CURRENT");}
#line 36 "app/dbio/ac_dbio_main.pgc"

    return TX_OK;
}

int ac_rec_insert(const ac_rec_t *rec)
{
    /* exec sql begin declare section */
     
     
     
     
     
     
     
    
#line 43 "app/dbio/ac_dbio_main.pgc"
 char h_key [ 16 ] ;
 
#line 44 "app/dbio/ac_dbio_main.pgc"
 char h_merch_id [ 16 ] ;
 
#line 45 "app/dbio/ac_dbio_main.pgc"
 char h_card_no [ 20 ] ;
 
#line 46 "app/dbio/ac_dbio_main.pgc"
 long h_amount ;
 
#line 47 "app/dbio/ac_dbio_main.pgc"
 char h_biz_date [ 9 ] ;
 
#line 48 "app/dbio/ac_dbio_main.pgc"
 char h_status [ 2 ] ;
 
#line 49 "app/dbio/ac_dbio_main.pgc"
 long h_fee ;
/* exec sql end declare section */
#line 50 "app/dbio/ac_dbio_main.pgc"


    if (!rec)
        return TX_EINVAL;

    strncpy(h_key, rec->key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_merch_id, rec->merch_id, sizeof(h_merch_id) - 1);
    h_merch_id[sizeof(h_merch_id) - 1] = '\0';
    strncpy(h_card_no, rec->card_no, sizeof(h_card_no) - 1);
    h_card_no[sizeof(h_card_no) - 1] = '\0';
    strncpy(h_biz_date, rec->biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';
    strncpy(h_status, rec->status, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';
    h_amount = rec->amount;
    h_fee    = rec->fee;

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into ac_ledger ( key , merch_id , card_no , amount , biz_date , status , fee ) values ( $1  , $2  , $3  , $4  , $5  , $6  , $7  )", 
	ECPGt_char,(h_key),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_merch_id),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_card_no),(long)20,(long)1,(20)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_amount),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*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_long,&(h_fee),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 71 "app/dbio/ac_dbio_main.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac_rec_insert");
        return TX_EDB;
    }
    return TX_OK;
}

int ac_rec_select(const char *key, ac_rec_t *out)
{
    /* exec sql begin declare section */
     
     
     
     
     
     
     
    
#line 83 "app/dbio/ac_dbio_main.pgc"
 char h_key [ 16 ] ;
 
#line 84 "app/dbio/ac_dbio_main.pgc"
 char h_merch_id [ 16 ] ;
 
#line 85 "app/dbio/ac_dbio_main.pgc"
 char h_card_no [ 20 ] ;
 
#line 86 "app/dbio/ac_dbio_main.pgc"
 long h_amount ;
 
#line 87 "app/dbio/ac_dbio_main.pgc"
 char h_biz_date [ 9 ] ;
 
#line 88 "app/dbio/ac_dbio_main.pgc"
 char h_status [ 2 ] ;
 
#line 89 "app/dbio/ac_dbio_main.pgc"
 long h_fee ;
/* exec sql end declare section */
#line 90 "app/dbio/ac_dbio_main.pgc"


    if (!key || !out)
        return TX_EINVAL;

    strncpy(h_key, key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select merch_id , card_no , amount , biz_date , status , fee from ac_ledger where key = $1 ", 
	ECPGt_char,(h_key),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_merch_id),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_card_no),(long)20,(long)1,(20)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_amount),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*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_long,&(h_fee),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 101 "app/dbio/ac_dbio_main.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND)
        return TX_ENOENT;
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac_rec_select");
        return TX_EDB;
    }

    memset(out, 0, sizeof(*out));
    strncpy(out->key, h_key, sizeof(out->key) - 1);
    strncpy(out->merch_id, h_merch_id, sizeof(out->merch_id) - 1);
    strncpy(out->card_no, h_card_no, sizeof(out->card_no) - 1);
    strncpy(out->biz_date, h_biz_date, sizeof(out->biz_date) - 1);
    strncpy(out->status, h_status, sizeof(out->status) - 1);
    out->amount = h_amount;
    out->fee    = h_fee;
    return TX_OK;
}

int ac_rec_update_status(const char *key, const char *status)
{
    /* exec sql begin declare section */
     
     
    
#line 124 "app/dbio/ac_dbio_main.pgc"
 char h_key [ 16 ] ;
 
#line 125 "app/dbio/ac_dbio_main.pgc"
 char h_status [ 2 ] ;
/* exec sql end declare section */
#line 126 "app/dbio/ac_dbio_main.pgc"


    if (!key || !status)
        return TX_EINVAL;

    strncpy(h_key, key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_status, status, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "update ac_ledger set status = $1  , upd_ts = now ( ) where key = $2 ", 
	ECPGt_char,(h_status),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_key),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 138 "app/dbio/ac_dbio_main.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac_rec_update_status");
        return TX_EDB;
    }
    if (sqlca.sqlerrd[2] == 0)
        return TX_ENOENT;
    return TX_OK;
}

long ac_rec_count(const char *biz_date, const char *status)
{
    /* exec sql begin declare section */
     
     
      
    
#line 152 "app/dbio/ac_dbio_main.pgc"
 char h_biz_date [ 9 ] ;
 
#line 153 "app/dbio/ac_dbio_main.pgc"
 char h_status [ 2 ] ;
 
#line 154 "app/dbio/ac_dbio_main.pgc"
 int h_cnt ;
/* exec sql end declare section */
#line 155 "app/dbio/ac_dbio_main.pgc"


    if (!biz_date || !status)
        return -1;

    strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';
    strncpy(h_status, status, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from ac_ledger where biz_date = $1  and status = $2 ", 
	ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*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_EOIT, 
	ECPGt_int,&(h_cnt),(long)1,(long)1,sizeof(int), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 169 "app/dbio/ac_dbio_main.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac_rec_count");
        return -1;
    }
    return (long)h_cnt;
}

int ac_rec_sum(const char *biz_date, long *out_sum)
{
    /* exec sql begin declare section */
     
     
    
#line 181 "app/dbio/ac_dbio_main.pgc"
 char h_biz_date [ 9 ] ;
 
#line 182 "app/dbio/ac_dbio_main.pgc"
 long h_sum ;
/* exec sql end declare section */
#line 183 "app/dbio/ac_dbio_main.pgc"


    if (!biz_date || !out_sum)
        return TX_EINVAL;

    strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select coalesce ( sum ( amount ) , 0 ) from ac_ledger where biz_date = $1 ", 
	ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_sum),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 194 "app/dbio/ac_dbio_main.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("ac_rec_sum");
        return TX_EDB;
    }
    *out_sum = h_sum;
    return TX_OK;
}
