Skip to content

Commit 6675006

Browse files
authored
orm: handle inserting enum fields (#26680)
* Handle inserting enum fields in ORM * Implement enum retrieval from db
1 parent a1690ef commit 6675006

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎vlib/orm/orm_func.v‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ fn (qb &QueryBuilder[T]) map_row(row []Primitive) !T {
524524
f64 { int(value) }
525525
else { 0 }
526526
}
527-
} $else $if field.typ is i64 || field.typ is ?i64 {
527+
} $else $if field.typ is i64 || field.typ is ?i64 || field.is_enum {
528528
instance.$(field.name) = match value {
529529
i8 { i64(value) }
530530
i16 { i64(value) }
@@ -847,6 +847,8 @@ fn fill_data_with_struct[T](value T, meta []TableField) QueryData {
847847
} else {
848848
qb.data << option_time_to_primitive(value.$(field.name))
849849
}
850+
} $else $if field.is_enum {
851+
qb.data << i64_to_primitive(i64(value.$(field.name)))
850852
}
851853
}
852854
}

‎vlib/orm/orm_func_test.v‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct User {
1111
age int
1212
role string @[index]
1313
status int @[index]
14+
level UserLevel
1415
salary int
1516
title string
1617
score int
@@ -42,6 +43,12 @@ struct User {
4243
option_string ?string
4344
}
4445

46+
enum UserLevel {
47+
beginner
48+
intermediate
49+
advanced
50+
}
51+
4552
// UserPart is part of User, so we can access only part of the `sys_users` table
4653
// note: for test, we modify `created_at` field from option to require
4754
// a `null` value in database, will map to default value of the require field in struct
@@ -258,6 +265,7 @@ fn test_orm_func_stmts() {
258265
age: 27
259266
role: 'employer'
260267
status: 5
268+
level: .intermediate
261269
salary: 2500
262270
title: 'doctor'
263271
score: 81
@@ -524,6 +532,8 @@ fn test_orm_func_stmts() {
524532
selected_users := qb.where('created_at IS NULL && ((salary > ? && age < ?) || (role LIKE ?))',
525533
2000, 30, '%employee%')!.query()!
526534
assert selected_users[0].name == 'Silly'
535+
// Check enum
536+
assert selected_users[0].level == .intermediate
527537
assert selected_users.len == 1
528538

529539
// complex select with lowercase `is null` and `like`

0 commit comments

Comments
 (0)