Skip to content

Commit b872487

Browse files
authored
cgen: allow ORM to work with DB aliases (#16939)
1 parent e854051 commit b872487

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

vlib/sqlite/sqlite_test.v

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import sqlite
22

3+
type Connection = sqlite.DB
4+
5+
struct User {
6+
pub:
7+
id int [primary; sql: serial]
8+
name string
9+
}
10+
11+
type Content = []u8 | string
12+
13+
struct Host {
14+
pub:
15+
db Connection
16+
}
17+
18+
fn (back Host) get_users() []User {
19+
return []
20+
}
21+
22+
fn create_host(db Connection) Host {
23+
sql db {
24+
create table User
25+
}
26+
27+
return Host{
28+
db: db
29+
}
30+
}
31+
332
fn test_sqlite() {
433
$if !linux {
534
return
@@ -56,3 +85,8 @@ fn test_can_access_sqlite_result_consts() {
5685
assert sqlite.sqlite_row == 100
5786
assert sqlite.sqlite_done == 101
5887
}
88+
89+
fn test_alias_db() {
90+
create_host(sqlite.connect(':memory:')!)
91+
assert true
92+
}

vlib/v/ast/table.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@ pub fn (t &Table) get_type_name(typ Type) string {
735735
return sym.name
736736
}
737737

738+
[inline]
739+
pub fn (t &Table) get_final_type_name(typ Type) string {
740+
sym := t.final_sym(typ)
741+
return sym.name
742+
}
743+
738744
[inline]
739745
pub fn (t &Table) unalias_num_type(typ Type) Type {
740746
sym := t.sym(typ)

vlib/v/gen/c/auto_str_methods.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ fn (mut g Gen) gen_str_for_result(typ ast.Type, styp string, str_fn_name string)
239239

240240
fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string) {
241241
parent_str_fn_name := g.get_str_fn(info.parent_type)
242+
_, str_method_expects_ptr, _ := g.table.sym(info.parent_type).str_method_info()
243+
242244
$if trace_autostr ? {
243245
eprintln('> gen_str_for_alias: ${parent_str_fn_name} | ${styp} | ${str_fn_name}')
244246
}
@@ -248,7 +250,11 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
248250
g.definitions.writeln('static string indent_${str_fn_name}(${styp} it, int indent_count); // auto')
249251
g.auto_str_funcs.writeln('static string indent_${str_fn_name}(${styp} it, int indent_count) {')
250252
g.auto_str_funcs.writeln('\tstring indents = string_repeat(_SLIT(" "), indent_count);')
251-
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
253+
if str_method_expects_ptr {
254+
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(&it);')
255+
} else {
256+
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
257+
}
252258
g.auto_str_funcs.writeln('\tstring res = str_intp(3, _MOV((StrIntpData[]){
253259
{_SLIT0, ${c.si_s_code}, {.d_s = indents }},
254260
{_SLIT("${clean_type_v_type_name}("), ${c.si_s_code}, {.d_s = tmp_ds }},

vlib/v/gen/c/sql.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,11 @@ fn (mut g Gen) parse_db_type(expr ast.Expr) SqlType {
835835
match expr {
836836
ast.Ident {
837837
if expr.info is ast.IdentVar {
838-
return g.parse_db_from_type_string(g.table.get_type_name(expr.info.typ))
838+
return g.parse_db_from_type_string(g.table.get_final_type_name(expr.info.typ))
839839
}
840840
}
841841
ast.SelectorExpr {
842-
return g.parse_db_from_type_string(g.table.get_type_name(expr.typ))
842+
return g.parse_db_from_type_string(g.table.get_final_type_name(expr.typ))
843843
}
844844
else {
845845
return .unknown

0 commit comments

Comments
 (0)