/*
 * mg_queue_weight_dbio.pgc - mg 모듈 DB 접근 함수 (mg_queue_weight_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * STAN x 재시도수 가중 합.
 */
#include <string.h>
#include <userlog.h>
#include "mg_queue_weight_dbio.h"

long mgdb_queue_weighted(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_a = 0, h_b = 0, acc = 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 stan, retry_cnt FROM mg_queue WHERE biz_date = :h_bizdate;
    EXEC SQL OPEN qc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH qc INTO :h_a, :h_b;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE qc; return -1; }
        acc += h_a * h_b;
    }
    EXEC SQL CLOSE qc;
    return acc;
}
