/*
 * mm_summary_batch.pgc - 마스터 종합 요약 배치 (다중 집계) (XA).
 *
 * ATMI client: tpinit/tpopen open the ECPG XA RM, tpbegin starts ONE global
 * transaction, the EXEC SQL work runs on that branch, then tpcommit drives XA 2PC.
 * Usage: mm_summary_batch
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>

int main(int argc, char **argv)
{
    long rows = 0; (void)rows; (void)argc; (void)argv;

    EXEC SQL BEGIN DECLARE SECTION;
    long h_merch, h_bin, h_code, h_issuer;
    EXEC SQL END DECLARE SECTION;

    if (tpinit(NULL) < 0) { fprintf(stderr, "tpinit FAIL: %s\n", tpstrerror(tperrno)); return 1; }
    if (tpopen() < 0)     { fprintf(stderr, "tpopen FAIL: %s\n", tpstrerror(tperrno)); return 1; }
    if (tpbegin(60, 0) < 0) { fprintf(stderr, "tpbegin FAIL: %s\n", tpstrerror(tperrno)); return 1; }

    EXEC SQL SELECT
        (SELECT count(*) FROM mm_merchant_ext),
        (SELECT count(*) FROM mm_bin),
        (SELECT count(*) FROM mm_code),
        (SELECT count(*) FROM mm_issuer)
        INTO :h_merch, :h_bin, :h_code, :h_issuer;
    if (sqlca.sqlcode < 0) { fprintf(stderr, "mm_summary_batch FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); tpabort(0); return 1; }

    if (tpcommit(0) < 0) { fprintf(stderr, "tpcommit FAIL: %s\n", tpstrerror(tperrno)); tpabort(0); return 1; }
    printf(">>> mm_summary_batch COMMIT: merch=%ld bin=%ld code=%ld issuer=%ld\n", h_merch, h_bin, h_code, h_issuer);

    tpclose(); tpterm();
    return 0;
}
