From c7961f84d21ccb1bda004f5dc81f5194aca615a0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 14 Apr 2020 07:40:30 +0000 Subject: [PATCH] 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() --- hawk/mod/mod-mysql.c | 43 ++++++++++++++++++++++++++++--- hawk/t/Makefile.am | 2 ++ hawk/t/Makefile.in | 5 +++- hawk/t/h-001.hawk | 60 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 hawk/t/h-001.hawk diff --git a/hawk/mod/mod-mysql.c b/hawk/mod/mod-mysql.c index 49f6cd27..33d8c9b8 100644 --- a/hawk/mod/mod-mysql.c +++ b/hawk/mod/mod-mysql.c @@ -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; } diff --git a/hawk/t/Makefile.am b/hawk/t/Makefile.am index 4b267899..16caebc0 100644 --- a/hawk/t/Makefile.am +++ b/hawk/t/Makefile.am @@ -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 diff --git a/hawk/t/Makefile.in b/hawk/t/Makefile.in index 3e128dd7..86b6df48 100644 --- a/hawk/t/Makefile.in +++ b/hawk/t/Makefile.in @@ -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"; \ diff --git a/hawk/t/h-001.hawk b/hawk/t/h-001.hawk new file mode 100644 index 00000000..1c90a491 --- /dev/null +++ b/hawk/t/h-001.hawk @@ -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" +}