cgen: escape table names (fix #20313) (#20322)

This commit is contained in:
Anton 2024-01-01 15:23:25 +02:00 committed by GitHub
parent d8fa13481c
commit 18954afa73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View file

@ -159,6 +159,7 @@ const skip_with_fsanitize_memory = [
'vlib/v/tests/orm_joined_tables_select_test.v', 'vlib/v/tests/orm_joined_tables_select_test.v',
'vlib/v/tests/sql_statement_inside_fn_call_test.v', 'vlib/v/tests/sql_statement_inside_fn_call_test.v',
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v', 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
'vlib/v/tests/orm_table_name_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v', 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/vweb/tests/vweb_test.v', 'vlib/vweb/tests/vweb_test.v',
'vlib/vweb/csrf/csrf_test.v', 'vlib/vweb/csrf/csrf_test.v',
@ -244,6 +245,7 @@ const skip_on_ubuntu_musl = [
'vlib/v/tests/orm_sub_array_struct_test.v', 'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v', 'vlib/v/tests/orm_joined_tables_select_test.v',
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v', 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
'vlib/v/tests/orm_table_name_test.v',
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v', 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
'vlib/v/tests/sql_statement_inside_fn_call_test.v', 'vlib/v/tests/sql_statement_inside_fn_call_test.v',
'vlib/clipboard/clipboard_test.v', 'vlib/clipboard/clipboard_test.v',

View file

@ -809,6 +809,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
select_result_var_name := g.new_tmp_var() select_result_var_name := g.new_tmp_var()
table_name := g.get_table_name_by_struct_type(node.table_expr.typ) table_name := g.get_table_name_by_struct_type(node.table_expr.typ)
escaped_table_name := cescape_nonascii(util.smart_quote(table_name, false))
g.sql_table_name = g.table.sym(node.table_expr.typ).name g.sql_table_name = g.table.sym(node.table_expr.typ).name
g.writeln('// sql { select from `${table_name}` }') g.writeln('// sql { select from `${table_name}` }')
@ -817,7 +818,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
g.writeln('${connection_var_name}._object, // Connection object') g.writeln('${connection_var_name}._object, // Connection object')
g.writeln('(orm__SelectConfig){') g.writeln('(orm__SelectConfig){')
g.indent++ g.indent++
g.writeln('.table = _SLIT("${table_name}"),') g.writeln('.table = _SLIT("${escaped_table_name}"),')
g.writeln('.is_count = ${node.is_count},') g.writeln('.is_count = ${node.is_count},')
g.writeln('.has_where = ${node.has_where},') g.writeln('.has_where = ${node.has_where},')
g.writeln('.has_order = ${node.has_order},') g.writeln('.has_order = ${node.has_order},')

View file

@ -0,0 +1,17 @@
import db.sqlite
@[table: '"specific name"']
struct ORMTableSpecificName {
dummy int
}
fn test_orm_table_name() {
db := sqlite.connect(':memory:') or { panic(err) }
r := sql db {
select from ORMTableSpecificName
} or {
assert true
return
}
assert false
}