From be1e40dac057034b76d4db8daf4c2ea33e4315b2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 3 Feb 2022 00:05:31 +0800 Subject: [PATCH] cgen: fix error for sql statement inside fn call (fix #13330) (#13346) --- cmd/tools/vtest-self.v | 2 ++ vlib/v/gen/c/sql.v | 4 +++- .../tests/sql_statement_inside_fn_call_test.v | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/sql_statement_inside_fn_call_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 719217e2a0..82d6e0cb4a 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -45,6 +45,7 @@ const ( 'vlib/sqlite/sqlite_orm_test.v', 'vlib/v/tests/orm_sub_struct_test.v', 'vlib/v/tests/orm_sub_array_struct_test.v', + 'vlib/v/tests/sql_statement_inside_fn_call_test.v', 'vlib/vweb/tests/vweb_test.v', 'vlib/vweb/request_test.v', 'vlib/net/http/request_test.v', @@ -83,6 +84,7 @@ const ( 'vlib/orm/orm_test.v', 'vlib/v/tests/orm_sub_struct_test.v', 'vlib/v/tests/orm_sub_array_struct_test.v', + 'vlib/v/tests/sql_statement_inside_fn_call_test.v', 'vlib/clipboard/clipboard_test.v', 'vlib/vweb/tests/vweb_test.v', 'vlib/vweb/request_test.v', diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 07a8a3e2a8..cb67bb02ae 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -759,7 +759,9 @@ fn (mut g Gen) sql_select(node ast.SqlExpr, expr string, left string) { if node.is_array { g.write('_array') } - g.writeln(';') + if !g.inside_call { + g.writeln(';') + } } } diff --git a/vlib/v/tests/sql_statement_inside_fn_call_test.v b/vlib/v/tests/sql_statement_inside_fn_call_test.v new file mode 100644 index 0000000000..536585dc3e --- /dev/null +++ b/vlib/v/tests/sql_statement_inside_fn_call_test.v @@ -0,0 +1,24 @@ +import sqlite + +struct Movie { + id int [primary] + name string +} + +fn x(m Movie) int { + return m.id +} + +fn test_sql_statement_inside_fn_call() { + db := sqlite.connect(':memory:') or { panic('failed') } + sql db { + create table Movie + } + m := Movie{1, 'Maria'} + sql db { + insert m into Movie + } + dump(x(sql db { + select from Movie where id == 1 + })) +}