/*
 * py_account_dbio.pgc - 지급계좌(py_account) DB 접근 (libpydbio.a).
 */
#include <string.h>
#include <userlog.h>
#include "py_dbio.h"

int pydb_account_check(const char *acct, char *status_out, long *balance_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_bal;
    char h_acct[32], h_status[8];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_acct, acct, sizeof(h_acct) - 1); h_acct[sizeof(h_acct)-1] = 0;

    EXEC SQL SELECT status, balance INTO :h_status, :h_bal
        FROM py_account WHERE account_no = :h_acct;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("pydb_account_check FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    strncpy(status_out, h_status, 7); status_out[7] = 0;
    *balance_out = h_bal;
    return 0;
}

int pydb_account_add(const char *acct, long delta)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_delta = delta;
    char h_acct[32];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_acct, acct, sizeof(h_acct) - 1); h_acct[sizeof(h_acct)-1] = 0;

    EXEC SQL UPDATE py_account SET balance = balance + :h_delta, updated_at = now()
        WHERE account_no = :h_acct;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("pydb_account_add FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
