some sample code to mod-mysql.c

added a test script file t/h-001.hawk. there is still a reference handling bug regarding hawk::call()
This commit is contained in:
hyung-hwan 2020-04-14 07:40:30 +00:00
parent 11fe4e17ad
commit c7961f84d2
4 changed files with 106 additions and 4 deletions

View File

@ -308,6 +308,7 @@ static stmt_node_t* get_stmt_list_node_with_arg (hawk_rtx_t* rtx, sql_list_t* sq
/* ------------------------------------------------------------------------ */
/*
BEGIN {
mysql = mysql::open();
if (mysql::connect(mysql, "localhost", "username", "password", "database") <= -1)
@ -315,8 +316,8 @@ static stmt_node_t* get_stmt_list_node_with_arg (hawk_rtx_t* rtx, sql_list_t* sq
print "connect error -", mysql::errmsg();
}
if (mysql::query(mysql, "select * from mytable") <= -1)
mysql::escape_string(mysql, "hawk", name);
if (mysql::query(mysql, sprintf("select * from mytable where name like '%%%s%%'", name)) <= -1)
{
print "query error -", mysql::errmsg();
}
@ -337,6 +338,42 @@ static stmt_node_t* get_stmt_list_node_with_arg (hawk_rtx_t* rtx, sql_list_t* sq
mysql::free_result(result);
mysql::close(mysql);
}
BEGIN {
mysql = mysql::open();
if (mysql::connect(mysql, "localhost", "username", "password", "database") <= -1)
{
print "connect error -", mysql::errmsg();
}
stmt = mysql::stmt_init(mysql);
if (stmt <= -1)
{
print "stmt initialization error - ", mysql::errmsg();
}
if (mysql::stmt_prepare(stmt, "select name,id,location from mytable where name like ?") <= -1)
{
print "stmt preparation error - ", mysql::errmsg();
}
result = mysql::stmt_execute(stmt, "%hawk%");
if (result <= -1)
{
print "statement execution error - ", mysql::errmsg();
}
while (mysql::stmt_fetch(result, name, id, loc) > 0)
{
print "name=", name, "id=", id, "location=", loc;
}
mysql::stmt_close (stmt);
mysql::close(mysql);
}
*/
static int fnc_open (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
@ -1303,7 +1340,7 @@ static int fnc_stmt_execute (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
param_count = mysql_stmt_param_count(stmt_node->stmt);
if (nparams != param_count)
{
set_error_on_sql_list (rtx, sql_list, HAWK_T("invalid number of pramaters"));
set_error_on_sql_list (rtx, sql_list, HAWK_T("invalid number of paramaters"));
goto done;
}

View File

@ -12,6 +12,8 @@ AM_CPPFLAGS = \
AM_LDFLAGS = -L$(abs_builddir)/../lib -L$(libdir)
LDADD = $(PTHREAD_LIBS)
noinst_SCRIPTS = h-001.hawk
##bin_PROGRAMS = t-001 t-002 t-003 t-004
bin_PROGRAMS = t-001 t-002 t-005

View File

@ -14,6 +14,7 @@
@SET_MAKE@
VPATH = @srcdir@
am__is_gnu_make = { \
if test -z '$(MAKELEVEL)'; then \
@ -122,6 +123,7 @@ t_002_OBJECTS = $(am_t_002_OBJECTS)
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
am_t_005_OBJECTS = t-005.$(OBJEXT)
t_005_OBJECTS = $(am_t_005_OBJECTS)
SCRIPTS = $(noinst_SCRIPTS)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
am__v_P_0 = false
@ -355,6 +357,7 @@ AM_CPPFLAGS = \
AM_LDFLAGS = -L$(abs_builddir)/../lib -L$(libdir)
LDADD = $(PTHREAD_LIBS)
noinst_SCRIPTS = h-001.hawk
t_001_SOURCES = t-001.c
t_002_SOURCES = t-002.c
t_002_LDADD = -lhawk $(LDADD)
@ -598,7 +601,7 @@ distdir-am: $(DISTFILES)
done
check-am: all-am
check: check-am
all-am: Makefile $(PROGRAMS)
all-am: Makefile $(PROGRAMS) $(SCRIPTS)
installdirs:
for dir in "$(DESTDIR)$(bindir)"; do \
test -z "$$dir" || $(MKDIR_P) "$$dir"; \

60
hawk/t/h-001.hawk Normal file
View File

@ -0,0 +1,60 @@
@pragma entry main
function ensure (a, b, desc)
{
if (a != b)
{
print "FAILURE", desc;
exit (-1);
}
}
function call_by_ref_1(&a, b, &c)
{
c = "hello, world";
return b * a;
}
function call_by_ref_2(a, &b)
{
b[1] = b[1] * a;
return a;
}
function main()
{
x = 20;
y = 90;
r = call_by_ref_1(x, y, z);
ensure (r, 1800);
ensure (x, 20);
ensure (y, 90);
ensure (z, "hello, world");
## TODO: add a new special word, @FILENAME, @FILELINE, @LINE <--- which are understood by the parser and swapped to the actual value
{
@local b;
call_by_ref_2(99, b);
ensure (b[1], 0);
}
{
@local b;
b[1] = 1;
r = call_by_ref_2(99, b);
ensure (r, 99);
ensure (b[1], 99);
}
{
@local b;
b[1] = 1;
r = hawk::call("call_by_ref_2", 99, b);
ensure (r, 99);
ensure (b[1], 99);
}
print "SUCCESS"
}