From 5b55e6726af2225617f2c0ff782750a340bbe6be Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 21 Apr 2026 15:34:27 +0300 Subject: [PATCH] cgen: fix db.sqlite.sqlerror on windows 11 from release download (fixes #20744) --- vlib/db/sqlite/sqlite_orm_test.v | 51 ++++++++++++++++++++++++++++++++ vlib/v/gen/c/orm.v | 15 ++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/vlib/db/sqlite/sqlite_orm_test.v b/vlib/db/sqlite/sqlite_orm_test.v index a064aae55..a86a1dd66 100644 --- a/vlib/db/sqlite/sqlite_orm_test.v +++ b/vlib/db/sqlite/sqlite_orm_test.v @@ -32,6 +32,23 @@ struct EntityToTest { smth string @[notnull; sql_type: 'TEXT'] } +struct TutorialBlogArticle { + id int @[primary; sql: serial] + title string + text string +} + +struct TutorialBlogApp { +mut: + db sqlite.DB +} + +fn (app &TutorialBlogApp) find_all_tutorial_blog_articles() []TutorialBlogArticle { + return sql app.db { + select from TutorialBlogArticle + } or { panic(err) } +} + fn test_sqlite_orm() { mut db := sqlite.connect(':memory:') or { panic(err) } defer { @@ -264,3 +281,37 @@ fn test_sqlite_orm_supports_time_duration_alias_fields() { assert rows.len == 1 assert rows[0].duration == 3 * time.second } + +fn test_sqlite_orm_tutorial_style_select_method() { + mut app := TutorialBlogApp{ + db: sqlite.connect(':memory:') or { panic(err) } + } + defer { + app.db.close() or { panic(err) } + } + + sql app.db { + create table TutorialBlogArticle + }! + + first_article := TutorialBlogArticle{ + title: 'Hello, world!' + text: 'V is great.' + } + second_article := TutorialBlogArticle{ + title: 'Second post.' + text: 'Hm... what should I write about?' + } + + sql app.db { + insert first_article into TutorialBlogArticle + insert second_article into TutorialBlogArticle + }! + + articles := app.find_all_tutorial_blog_articles() + assert articles.len == 2 + + mut titles := articles.map(it.title) + titles.sort() + assert titles == ['Hello, world!', 'Second post.'] +} diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index 79e3f671a..152469038 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -53,6 +53,17 @@ fn (g &Gen) orm_primitive_field_name(typ ast.Type) string { return vint2int(sym.cname) } +fn (g &Gen) orm_non_array_fields(fields []ast.StructField) []ast.StructField { + mut non_array_fields := []ast.StructField{cap: fields.len} + for field in fields { + final_typ := g.table.final_type(field.typ.clear_flag(.option)) + if g.table.sym(final_typ).kind != .array { + non_array_fields << field + } + } + return non_array_fields +} + fn (g &Gen) orm_primitive_variant_field_name(typ ast.Type) string { final_typ := g.table.final_type(typ.clear_flag(.option)) sym := g.table.sym(final_typ) @@ -864,7 +875,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v } } - fields := node.fields.filter(g.table.sym(it.typ).kind != .array) + fields := g.orm_non_array_fields(node.fields) auto_fields := get_auto_field_idxs(fields) primary_field := g.get_orm_struct_primary_field(fields) or { ast.StructField{} } @@ -1517,7 +1528,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re g.writeln('.primary = _S("${primary_field.name}"),') } - mut select_fields := fields.filter(g.table.sym(it.typ).kind != .array) + mut select_fields := g.orm_non_array_fields(fields) if node.aggregate_kind != .none { select_fields = fields.clone() } -- 2.39.5