/*
 * cm_sortrange_dbio.pgc - cm 모듈 DB 접근 함수 (cm_sortrange_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 코드그룹 내 정렬순서 최대-최소 범위 (min/max).
 */
#include <string.h>
#include <userlog.h>
#include "cm_sortrange_dbio.h"

long cmdb_sort_spread(const char *grp)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_grp[32];
    long h_lo = 0, h_hi = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_grp, grp, sizeof(h_grp)-1); h_grp[sizeof(h_grp)-1] = 0;
    EXEC SQL SELECT coalesce(min(sort_ord),0), coalesce(max(sort_ord),0)
        INTO :h_lo, :h_hi FROM cm_code WHERE code_grp = :h_grp AND use_yn = 'Y';
    if (sqlca.sqlcode < 0) return -1;
    return h_hi - h_lo;
}
