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

299 lines
5.7 KiB
V
Raw Normal View History

import os
import db.sqlite
import db.mysql
import db.pg
2021-02-04 22:28:33 +03:00
// The goal of this example, is to show how you can connect to
// several different databases in the same program, and use both
// the ORM and the native connection wrapper, that each DB driver
// provides, if you need to execute more complex SQL queries.
//
// You can use environment variables to pass your local DB connection
// settings, without editing the code, like this:
//
// MUSER='myuser' MPASS='abc' MDATABASE='vtestdb' PGUSER='postgres' PGPASS='password' PGDATABASE='postgres' ./v -g run examples/database/orm.v
//
// WARNING: this example will drop and re-create any tables named:
// * modules
// * User
// * Parent
// * Child
// in the passed databases, so it is better to use empty DBs for it.
const (
mysql_host = os.getenv_opt('MHOST') or { 'localhost' }
mysql_port = os.getenv_opt('MPORT') or { '3306' }.u32()
mysql_user = os.getenv_opt('MUSER') or { 'myuser' }
mysql_pass = os.getenv_opt('MPASS') or { 'abc' }
mysql_db = os.getenv_opt('MDATABASE') or { 'test' }
)
const (
pg_host = os.getenv_opt('PGHOST') or { 'localhost' }
pg_user = os.getenv_opt('PGUSER') or { 'test' }
pg_pass = os.getenv_opt('PGPASS') or { 'abc' }
pg_db = os.getenv_opt('PGDATABASE') or { 'test' }
)
[table: 'modules']
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]
2021-09-26 11:17:56 +03:00
age u32 [unique: 'user']
name string [sql: 'username'; sql_type: 'VARCHAR(200)'; unique]
is_customer bool [sql: 'abc'; unique: 'user']
2021-02-04 22:28:33 +03:00
skipped_string string [skip]
}
2021-04-30 09:13:26 +03:00
struct Parent {
id int [primary; sql: serial]
2021-05-04 12:35:18 +03:00
name string
children []Child [fkey: 'parent_id']
2021-04-30 09:13:26 +03:00
}
struct Child {
2021-04-30 09:13:26 +03:00
id int [primary; sql: serial]
parent_id int
name string
}
fn sqlite3_array() ! {
eprintln('------------ ${@METHOD} -----------------')
mut db := sqlite.connect(':memory:')!
defer {
sql db {
drop table Parent
drop table Child
} or {}
db.close() or {}
}
2021-04-30 09:13:26 +03:00
sql db {
create table Parent
}!
sql db {
create table Child
}!
2021-04-30 09:13:26 +03:00
par := Parent{
name: 'test'
2021-05-04 12:35:18 +03:00
children: [
Child{
2021-04-30 09:13:26 +03:00
name: 'abc'
},
Child{
2021-04-30 09:13:26 +03:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}!
2021-04-30 09:13:26 +03:00
parent := sql db {
select from Parent where id == 1
}!
2021-04-30 09:13:26 +03:00
eprintln(parent)
}
fn msql_array() ! {
eprintln('------------ ${@METHOD} -----------------')
mut db := mysql.Connection{
host: mysql_host
port: mysql_port
username: mysql_user
password: mysql_pass
dbname: mysql_db
}
db.connect()!
defer {
sql db {
drop table Parent
} or {}
db.close()
}
db.query('drop table if exists Parent')!
db.query('drop table if exists Child')!
2021-04-30 09:13:26 +03:00
sql db {
create table Parent
create table Child
}!
2021-04-30 09:13:26 +03:00
par := Parent{
name: 'test'
2021-05-04 12:35:18 +03:00
children: [
Child{
2021-04-30 09:13:26 +03:00
name: 'abc'
},
Child{
2021-04-30 09:13:26 +03:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}!
2021-04-30 09:13:26 +03:00
parent := sql db {
select from Parent where id == 1
}!
2021-04-30 09:13:26 +03:00
eprintln(parent)
}
fn psql_array() ! {
eprintln('------------ ${@METHOD} -----------------')
mut db := pg.connect(host: pg_host, user: pg_user, password: pg_pass, dbname: pg_db)!
defer {
db.exec_one('drop table if exists "Parent", "Child"') or { eprintln(err) }
db.close()
2021-04-30 09:13:26 +03:00
}
db.exec_one('drop table if exists "Parent", "Child"') or { eprintln(err) }
2021-04-30 09:13:26 +03:00
sql db {
create table Parent
create table Child
}!
2021-04-30 09:13:26 +03:00
par := Parent{
name: 'test'
2021-05-04 12:35:18 +03:00
children: [
Child{
2021-04-30 09:13:26 +03:00
name: 'abc'
},
Child{
2021-04-30 09:13:26 +03:00
name: 'def'
},
]
}
sql db {
insert par into Parent
}!
2021-04-30 09:13:26 +03:00
parent := sql db {
select from Parent where id == 1
}!
2021-04-30 09:13:26 +03:00
eprintln(parent)
2021-07-24 20:49:40 +03:00
}
2021-04-30 09:13:26 +03:00
fn sqlite3() ! {
eprintln('------------ ${@METHOD} -----------------')
mut db := sqlite.connect(':memory:')!
defer {
sql db {
drop table Module
drop table User
} or {}
db.close() or {}
}
sql db {
create table Module
}!
sql db {
create table User
}!
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
}!
eprintln(modul)
}
fn msql() ! {
eprintln('------------ ${@METHOD} -----------------')
mut conn := mysql.Connection{
host: mysql_host
port: mysql_port
username: mysql_user
password: mysql_pass
dbname: mysql_db
}
conn.connect()!
defer {
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
conn.close()
}
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
sql conn {
create table Module
}!
sql conn {
create table User
}!
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
}
2021-04-25 18:57:55 +03:00
fn psql() ! {
eprintln('------------ ${@METHOD} -----------------')
mut db := pg.connect(host: pg_host, user: pg_user, password: pg_pass, dbname: pg_db)!
defer {
db.exec_one('drop table if exists "modules", "User"') or { eprintln(err) }
db.close()
}
db.exec_one('drop table if exists "modules", "User"') or { eprintln(err) }
sql db {
create table Module
create table User
}!
2021-04-25 18:57:55 +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-04-29 13:38:36 +03:00
modul := sql db {
select from Module where id == 1
}!
2021-04-29 13:38:36 +03:00
sql db {
drop table Module
}!
2021-04-29 13:38:36 +03:00
eprintln(modul)
}
fn main() {
eprintln('------------ ${@METHOD} -----------------')
sqlite3_array()!
msql_array()!
psql_array()!
sqlite3()!
msql()!
psql()!
2021-07-24 20:49:40 +03:00
}