2023-03-01 01:49:30 +03:00
|
|
|
import db.sqlite
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2023-04-04 08:23:06 +03:00
|
|
|
fn get_users(mut db sqlite.DB) ![]User {
|
2023-03-01 01:49:30 +03:00
|
|
|
return sql db {
|
|
|
|
select from User
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-03-01 01:49:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_orm_mut_db() {
|
|
|
|
mut db := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table User
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-03-01 01:49:30 +03:00
|
|
|
|
|
|
|
first_user := User{
|
|
|
|
name: 'first'
|
|
|
|
}
|
|
|
|
second_user := User{
|
|
|
|
name: 'second'
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert first_user into User
|
|
|
|
insert second_user into User
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-03-01 01:49:30 +03:00
|
|
|
|
2023-04-04 08:23:06 +03:00
|
|
|
users := get_users(mut db)!
|
2023-03-01 01:49:30 +03:00
|
|
|
|
|
|
|
assert users.len == 2
|
|
|
|
}
|