1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: support string interpolation in ORM queries (#17141)

This commit is contained in:
walking devel 2023-01-28 23:27:34 +00:00 committed by GitHub
parent d3e4058aec
commit 9a86456365
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View File

@ -126,6 +126,7 @@ const (
'vlib/orm/orm_create_and_drop_test.v',
'vlib/orm/orm_insert_test.v',
'vlib/orm/orm_fn_calls_test.v',
'vlib/orm/orm_string_interpolation_in_where_test.v',
'vlib/db/sqlite/sqlite_test.v',
'vlib/db/sqlite/sqlite_orm_test.v',
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
@ -194,6 +195,7 @@ const (
'vlib/orm/orm_create_and_drop_test.v',
'vlib/orm/orm_insert_test.v',
'vlib/orm/orm_fn_calls_test.v',
'vlib/orm/orm_string_interpolation_in_where_test.v',
'vlib/v/tests/orm_sub_struct_test.v',
'vlib/v/tests/orm_sub_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v',

View File

@ -0,0 +1,36 @@
import db.sqlite
struct User {
id int [primary; sql: serial]
name string
}
fn test_string_interpolation() {
mut db := sqlite.connect(':memory:') or { panic(err) }
sql db {
create table User
}
user_suffix := '_user'
first_user := User{
name: 'first${user_suffix}'
}
second_user := User{
name: 'second${user_suffix}'
}
sql db {
insert first_user into User
insert second_user into User
}
users := sql db {
select from User where name == 'first${user_suffix}'
}
assert users.len == 1
assert users.first().name == 'first${user_suffix}'
}

View File

@ -286,6 +286,9 @@ fn (mut g Gen) sql_expr_to_orm_primitive(expr ast.Expr) {
ast.StringLiteral {
g.sql_write_orm_primitive(ast.string_type, expr)
}
ast.StringInterLiteral {
g.sql_write_orm_primitive(ast.string_type, expr)
}
ast.IntegerLiteral {
g.sql_write_orm_primitive(ast.int_type, expr)
}
@ -419,6 +422,9 @@ fn (mut g Gen) sql_where_data(expr ast.Expr, mut fields []string, mut parenthese
ast.StringLiteral {
data << expr
}
ast.StringInterLiteral {
data << expr
}
ast.IntegerLiteral {
data << expr
}