/*
 * vl_cross_dbio.pgc - 교차검증 존재비트 (매입/가맹점/승인) (libvldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "vl_cross_dbio.h"

long vldb_cross_bits(long pid)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid, h_p, h_m, h_a;
    EXEC SQL END DECLARE SECTION;
    long rc = 0;
    EXEC SQL SELECT count(*) INTO :h_p FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL SELECT count(*) INTO :h_m FROM purchase p
        JOIN merchant m ON m.merchant_id = p.merchant_id WHERE p.purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL SELECT count(*) INTO :h_a FROM approval WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) return -1;
    if (h_p == 0) rc |= 1;
    if (h_m == 0) rc |= 2;
    if (h_a == 0) rc |= 4;
    return rc;
}
