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

cgen: allow ORM to work with DB aliases (#16939)

This commit is contained in:
Swastik Baranwal 2023-01-11 15:28:55 +05:30 committed by GitHub
parent e854051c1f
commit b872487d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 3 deletions

View File

@ -1,5 +1,34 @@
import sqlite
type Connection = sqlite.DB
struct User {
pub:
id int [primary; sql: serial]
name string
}
type Content = []u8 | string
struct Host {
pub:
db Connection
}
fn (back Host) get_users() []User {
return []
}
fn create_host(db Connection) Host {
sql db {
create table User
}
return Host{
db: db
}
}
fn test_sqlite() {
$if !linux {
return
@ -56,3 +85,8 @@ fn test_can_access_sqlite_result_consts() {
assert sqlite.sqlite_row == 100
assert sqlite.sqlite_done == 101
}
fn test_alias_db() {
create_host(sqlite.connect(':memory:')!)
assert true
}

View File

@ -735,6 +735,12 @@ pub fn (t &Table) get_type_name(typ Type) string {
return sym.name
}
[inline]
pub fn (t &Table) get_final_type_name(typ Type) string {
sym := t.final_sym(typ)
return sym.name
}
[inline]
pub fn (t &Table) unalias_num_type(typ Type) Type {
sym := t.sym(typ)

View File

@ -239,6 +239,8 @@ fn (mut g Gen) gen_str_for_result(typ ast.Type, styp string, str_fn_name string)
fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string) {
parent_str_fn_name := g.get_str_fn(info.parent_type)
_, str_method_expects_ptr, _ := g.table.sym(info.parent_type).str_method_info()
$if trace_autostr ? {
eprintln('> gen_str_for_alias: ${parent_str_fn_name} | ${styp} | ${str_fn_name}')
}
@ -248,7 +250,11 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
g.definitions.writeln('static string indent_${str_fn_name}(${styp} it, int indent_count); // auto')
g.auto_str_funcs.writeln('static string indent_${str_fn_name}(${styp} it, int indent_count) {')
g.auto_str_funcs.writeln('\tstring indents = string_repeat(_SLIT(" "), indent_count);')
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
if str_method_expects_ptr {
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(&it);')
} else {
g.auto_str_funcs.writeln('\tstring tmp_ds = ${parent_str_fn_name}(it);')
}
g.auto_str_funcs.writeln('\tstring res = str_intp(3, _MOV((StrIntpData[]){
{_SLIT0, ${c.si_s_code}, {.d_s = indents }},
{_SLIT("${clean_type_v_type_name}("), ${c.si_s_code}, {.d_s = tmp_ds }},

View File

@ -835,11 +835,11 @@ fn (mut g Gen) parse_db_type(expr ast.Expr) SqlType {
match expr {
ast.Ident {
if expr.info is ast.IdentVar {
return g.parse_db_from_type_string(g.table.get_type_name(expr.info.typ))
return g.parse_db_from_type_string(g.table.get_final_type_name(expr.info.typ))
}
}
ast.SelectorExpr {
return g.parse_db_from_type_string(g.table.get_type_name(expr.typ))
return g.parse_db_from_type_string(g.table.get_final_type_name(expr.typ))
}
else {
return .unknown