/*
 * mg_pos_count_dbio.pgc - mg 모듈 DB 접근 함수 (mg_pos_count_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 금액>0 전문 건수(커서).
 */
#include <string.h>
#include <userlog.h>
#include "mg_pos_count_dbio.h"

long mgdb_msg_positive_count(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_x = 0, k = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE qc CURSOR FOR
        SELECT amount FROM mg_msg_log WHERE biz_date = :h_bizdate;
    EXEC SQL OPEN qc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH qc INTO :h_x;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE qc; return -1; }
        if (h_x > 0) k++;
    }
    EXEC SQL CLOSE qc;
    return k;
}
