From e5692a250e4c56f0b946f914fc601bd64310fdb2 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 18 Jan 2020 05:36:36 +0000 Subject: [PATCH] some experimental into mod-mysql.c --- hawk/lib/fmt.c | 2 +- hawk/lib/run.c | 2 +- hawk/mod/mod-mysql.c | 85 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/hawk/lib/fmt.c b/hawk/lib/fmt.c index 314ddfaf..a3b3747e 100644 --- a/hawk/lib/fmt.c +++ b/hawk/lib/fmt.c @@ -1013,7 +1013,7 @@ static int fmt_outv (hawk_fmtout_t* fmtout, va_list ap) if (!fmtout->putobj) goto invalid_format; if (fmtout->putobj(fmtout, va_arg(ap, hawk_val_t*)) <= -1) goto oops; break; - + case 'e': case 'E': case 'f': diff --git a/hawk/lib/run.c b/hawk/lib/run.c index 1e42ca8b..0185bb36 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -7794,7 +7794,7 @@ wp_mod_main: } else { - v = eval_expression (rtx, args); + v = eval_expression(rtx, args); if (v == HAWK_NULL) return HAWK_NULL; } } diff --git a/hawk/mod/mod-mysql.c b/hawk/mod/mod-mysql.c index d630b28d..0886c9b8 100644 --- a/hawk/mod/mod-mysql.c +++ b/hawk/mod/mod-mysql.c @@ -1181,8 +1181,87 @@ oops: static int fnc_stmt_bind_param (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { - /* TODO: */ - return -1; + sql_list_t* sql_list; + sql_node_t* sql_node; + stmt_list_t* stmt_list; + stmt_node_t* stmt_node; + int ret = -1; + + sql_list = rtx_to_sql_list(rtx, fi); + stmt_list = rtx_to_stmt_list(rtx, fi); + sql_node = get_sql_list_node_with_arg(rtx, sql_list, hawk_rtx_getarg(rtx, 0)); + stmt_node = get_stmt_list_node_with_arg(rtx, sql_list, stmt_list, hawk_rtx_getarg(rtx, 0)); + if (stmt_node) + { + hawk_oow_t nargs, i; + MYSQL_BIND* binds; + + ENSURE_CONNECT_EVER_ATTEMPTED(rtx, sql_list, sql_node); + + nargs = hawk_rtx_getnargs(rtx); + if (nargs - 1 != mysql_stmt_param_count(stmt_node->stmt)) + { + set_error_on_sql_list (rtx, sql_list, HAWK_T("invalid number of pramaters")); + goto done; + } + binds = hawk_rtx_callocmem(rtx, HAWK_SIZEOF(MYSQL_BIND) * (nargs - 1)); + if (!binds) goto done; + +int int_data[100]; + for (i = 1; i < nargs; i++) + { + hawk_val_t* a; + hawk_oow_t j; + int x, y; + + a = hawk_rtx_getarg(rtx, i); + j = i - 1; + switch (HAWK_RTX_GETVALTYPE(rtx, a)) + { + case HAWK_VAL_STR: + break; + case HAWK_VAL_MBS: + break; + + case HAWK_VAL_INT: + binds[j].buffer_type = MYSQL_TYPE_LONG; + binds[j].buffer = &int_data[j]; + binds[j].is_null = 0; + binds[j].is_unsigned = 0; + binds[j].length = HAWK_NULL; + + int_data[j] = HAWK_RTX_GETINTFROMVAL(rtx, a); + break; + + case HAWK_VAL_FLT: + break; + + default: + set_error_on_sql_list (rtx, sql_list, HAWK_T("invalid parameter")); + goto done; + } + } + + if (mysql_stmt_bind_param(stmt_node->stmt, binds) != 0) + { + set_error_on_sql_list (rtx, sql_list, HAWK_T("%hs"), mysql_stmt_error(stmt_node->stmt)); + hawk_rtx_freemem (rtx, binds); + goto done; + } + + if (mysql_stmt_execute(stmt_node->stmt) != 0) + { + set_error_on_sql_list (rtx, sql_list, HAWK_T("%hs"), mysql_stmt_error(stmt_node->stmt)); + goto done; + } + + hawk_rtx_freemem (rtx, binds); + ret = 0; + } + +done: + hawk_rtx_setretval (rtx, hawk_rtx_makeintval(rtx, ret)); + return 0; } static int fnc_stmt_execute (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) @@ -1251,7 +1330,7 @@ static fnctab_t fnctab[] = { HAWK_T("select_db"), { { 2, 2, HAWK_NULL }, fnc_select_db, 0 } }, { HAWK_T("set_option"), { { 3, 3, HAWK_NULL }, fnc_set_option, 0 } }, - { HAWK_T("stmt_bind_param"), { { 1, 1, HAWK_NULL }, fnc_stmt_bind_param, 0 } }, + { HAWK_T("stmt_bind_param"), { { 1, A_MAX, HAWK_NULL }, fnc_stmt_bind_param, 0 } }, { HAWK_T("stmt_close"), { { 1, 1, HAWK_NULL }, fnc_stmt_close, 0 } }, { HAWK_T("stmt_execute"), { { 1, 1, HAWK_NULL }, fnc_stmt_execute, 0 } }, { HAWK_T("stmt_init"), { { 1, 1, HAWK_NULL }, fnc_stmt_init, 0 } },