v/examples/database/orm.v

261 lines
3.3 KiB
V
Raw Normal View History

2021-02-04 20:28:33 +01:00
import sqlite
import mysql
2021-04-25 17:57:55 +02:00
import pg
2021-02-04 20:28:33 +01:00
[table: 'modules']
2021-02-04 20:28:33 +01:00
struct Module {
id int [primary; sql: serial]
2021-02-04 20:28:33 +01:00
name string
nr_downloads int [sql: u64]
2021-02-04 20:28:33 +01:00
creator User
}
struct User {
id int [primary; sql: serial]
age int [unique: 'user']
name string [sql: 'username'; unique]
is_customer bool [sql: 'abc'; unique: 'user']
2021-02-04 20:28:33 +01:00
skipped_string string [skip]
}
2021-04-30 08:13:26 +02:00
struct Parent {
id int [primary; sql: serial]
2021-05-04 11:35:18 +02:00
name string
children []Child [fkey: 'parent_id']
2021-04-30 08:13:26 +02:00
}
struct Child {
2021-04-30 08:13:26 +02:00
id int [primary; sql: serial]
parent_id int
name string
}
2021-02-04 20:28:33 +01:00
fn main() {
2021-04-30 08:13:26 +02:00
sqlite3_array()
mysql_array()
psql_array()
2021-04-29 20:00:17 +02:00
sqlite3()
mysql()
psql()
}
2021-04-30 08:13:26 +02:00
fn sqlite3_array() {
mut db := sqlite.connect(':memory:') or { panic(err) }
sql db {
create table Parent
}
par := Parent{
name: 'test'
2021-05-04 11:35:18 +02:00
children: [
Child{
2021-04-30 08:13:26 +02:00
name: 'abc'
},
Child{
2021-04-30 08:13:26 +02:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}
parent := sql db {
select from Parent where id == 1
}
sql db {
drop table Child
2021-04-30 08:13:26 +02:00
drop table Parent
}
eprintln(parent)
}
fn mysql_array() {
mut db := mysql.Connection{
host: 'localhost'
port: 3306
username: 'root'
password: 'abc'
dbname: 'test'
}
db.connect() or { panic(err) }
sql db {
create table Parent
}
par := Parent{
name: 'test'
2021-05-04 11:35:18 +02:00
children: [
Child{
2021-04-30 08:13:26 +02:00
name: 'abc'
},
Child{
2021-04-30 08:13:26 +02:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}
parent := sql db {
select from Parent where id == 1
}
eprintln(parent)
sql db {
drop table Child
2021-04-30 08:13:26 +02:00
drop table Parent
}
db.close()
}
fn psql_array() {
mut db := pg.connect(host: 'localhost', user: 'test', password: 'abc', dbname: 'test') or {
panic(err)
}
sql db {
create table Parent
}
par := Parent{
name: 'test'
2021-05-04 11:35:18 +02:00
children: [
Child{
2021-04-30 08:13:26 +02:00
name: 'abc'
},
Child{
2021-04-30 08:13:26 +02:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}
parent := sql db {
select from Parent where id == 1
}
eprintln(parent)
sql db {
drop table Child
2021-04-30 08:13:26 +02:00
drop table Parent
}
db.close()
}
2021-04-29 20:00:17 +02:00
fn sqlite3() {
mut db := sqlite.connect(':memory:') or { panic(err) }
sql db {
create table Module
}
2021-02-04 20:28:33 +01: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 20:28:33 +01:00
modul := sql db {
select from Module where id == 1
}
sql db {
drop table Module
}
eprintln(modul)
2021-04-29 20:00:17 +02:00
db.close() or { panic(err) }
}
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
}
2021-04-29 20:00:17 +02:00
eprintln(m)
2021-04-29 20:00:17 +02:00
conn.close()
2021-02-04 20:28:33 +01:00
}
2021-04-25 17:57:55 +02:00
fn psql() {
mut db := pg.connect(host: 'localhost', user: 'test', password: 'abc', dbname: 'test') or {
panic(err)
}
mod := Module{
name: 'test'
nr_downloads: 10
creator: User{
age: 21
name: 'VUser'
is_customer: true
}
}
sql db {
create table Module
}
sql db {
insert mod into Module
}
2021-04-29 12:38:36 +02:00
modul := sql db {
select from Module where id == 1
}
sql db {
drop table Module
}
eprintln(modul)
2021-04-29 20:00:17 +02:00
db.close()
2021-04-25 17:57:55 +02:00
}