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

84 lines
1.2 KiB
V
Raw Normal View History

2021-02-04 22:28:33 +03:00
import sqlite
import mysql
2021-02-04 22:28:33 +03:00
struct Module {
id int [primary; sql: serial]
2021-02-04 22:28:33 +03:00
name string
nr_downloads int [sql: u64]
2021-02-04 22:28:33 +03:00
creator User
}
struct User {
id int [primary; sql: serial]
age int [unique: 'user']
name string [unique]
is_customer bool [unique: 'user']
2021-02-04 22:28:33 +03:00
skipped_string string [skip]
}
fn main() {
db := sqlite.connect(':memory:') or { panic(err) }
sql db {
create table Module
}
2021-02-04 22:28:33 +03:00
mod := Module{
name: 'test'
nr_downloads: 10
creator: User{
age: 21
name: 'VUser'
is_customer: true
}
}
sql db {
insert mod into Module
}
2021-02-04 22:28:33 +03:00
modul := sql db {
select from Module where id == 1
}
sql db {
drop table Module
}
eprintln(modul)
mysql()
}
fn mysql() {
mut conn := mysql.Connection{
host: 'localhost'
port: 3306
username: 'root'
password: 'abc'
dbname: 'test'
}
conn.connect() or { panic(err) }
sql conn {
create table Module
}
mod := Module{
name: 'test'
nr_downloads: 10
creator: User{
age: 21
name: 'VUser'
is_customer: true
}
}
sql conn {
insert mod into Module
}
m := sql conn {
select from Module where id == 1
}
eprintln(m)
2021-02-04 22:28:33 +03:00
}