/*
 * vl_refinteg_dbio.pgc - 참조무결성 위반 건수 (가맹점 orphan) (libvldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "vl_refinteg_dbio.h"

long vldb_orphan_count(long pid)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid, h_cnt;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM purchase p
        WHERE p.purchase_id = :h_pid
          AND NOT EXISTS (SELECT 1 FROM merchant m WHERE m.merchant_id = p.merchant_id);
    if (sqlca.sqlcode < 0) { userlog("vldb_orphan_count FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_cnt;
}
