examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
import os
|
2023-01-13 18:02:32 +03:00
|
|
|
import db.sqlite
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
import db.mysql
|
2023-01-13 18:02:32 +03:00
|
|
|
import db.pg
|
2021-02-04 22:28:33 +03:00
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +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' }
|
|
|
|
)
|
|
|
|
|
2021-04-15 10:53:43 +03:00
|
|
|
[table: 'modules']
|
2021-02-04 22:28:33 +03:00
|
|
|
struct Module {
|
2021-04-10 17:38:27 +03:00
|
|
|
id int [primary; sql: serial]
|
2021-02-04 22:28:33 +03:00
|
|
|
name string
|
2021-04-10 17:38:27 +03:00
|
|
|
nr_downloads int [sql: u64]
|
2021-02-04 22:28:33 +03:00
|
|
|
creator User
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
2021-04-10 17:38:27 +03:00
|
|
|
id int [primary; sql: serial]
|
2021-09-26 11:17:56 +03:00
|
|
|
age u32 [unique: 'user']
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
name string [sql: 'username'; sql_type: 'VARCHAR(200)'; unique]
|
2021-04-15 10:53:43 +03:00
|
|
|
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 {
|
2021-06-11 11:52:06 +03:00
|
|
|
id int [primary; sql: serial]
|
2021-05-04 12:35:18 +03:00
|
|
|
name string
|
2021-06-11 11:52:06 +03:00
|
|
|
children []Child [fkey: 'parent_id']
|
2021-04-30 09:13:26 +03:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:06 +03:00
|
|
|
struct Child {
|
2021-04-30 09:13:26 +03:00
|
|
|
id int [primary; sql: serial]
|
|
|
|
parent_id int
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
fn sqlite3_array() ! {
|
|
|
|
eprintln('------------ ${@METHOD} -----------------')
|
|
|
|
mut db := sqlite.connect(':memory:')!
|
|
|
|
defer {
|
|
|
|
sql db {
|
|
|
|
drop table Parent
|
|
|
|
drop table Child
|
|
|
|
}
|
|
|
|
db.close() or {}
|
|
|
|
}
|
|
|
|
//
|
2021-04-30 09:13:26 +03:00
|
|
|
sql db {
|
|
|
|
create table Parent
|
|
|
|
}
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
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: [
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'abc'
|
|
|
|
},
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'def'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
insert par into Parent
|
|
|
|
}
|
|
|
|
parent := sql db {
|
|
|
|
select from Parent where id == 1
|
|
|
|
}
|
|
|
|
eprintln(parent)
|
|
|
|
}
|
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
create table Child
|
2021-04-30 09:13:26 +03:00
|
|
|
}
|
|
|
|
par := Parent{
|
|
|
|
name: 'test'
|
2021-05-04 12:35:18 +03:00
|
|
|
children: [
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'abc'
|
|
|
|
},
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'def'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
insert par into Parent
|
|
|
|
}
|
|
|
|
parent := sql db {
|
|
|
|
select from Parent where id == 1
|
|
|
|
}
|
|
|
|
eprintln(parent)
|
|
|
|
}
|
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
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
|
|
|
}
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +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
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
create table Child
|
2021-04-30 09:13:26 +03:00
|
|
|
}
|
|
|
|
par := Parent{
|
|
|
|
name: 'test'
|
2021-05-04 12:35:18 +03:00
|
|
|
children: [
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'abc'
|
|
|
|
},
|
2021-06-11 11:52:06 +03:00
|
|
|
Child{
|
2021-04-30 09:13:26 +03:00
|
|
|
name: 'def'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
insert par into Parent
|
|
|
|
}
|
|
|
|
parent := sql db {
|
|
|
|
select from Parent where id == 1
|
|
|
|
}
|
|
|
|
eprintln(parent)
|
2021-07-24 20:49:40 +03:00
|
|
|
}
|
2021-04-30 09:13:26 +03:00
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
fn sqlite3() ! {
|
|
|
|
eprintln('------------ ${@METHOD} -----------------')
|
|
|
|
mut db := sqlite.connect(':memory:')!
|
|
|
|
defer {
|
|
|
|
sql db {
|
|
|
|
drop table Module
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
drop table User
|
|
|
|
}
|
|
|
|
db.close() or {}
|
|
|
|
}
|
|
|
|
//
|
2021-04-10 17:38:27 +03:00
|
|
|
sql db {
|
|
|
|
create table Module
|
|
|
|
}
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
modul := sql db {
|
|
|
|
select from Module where id == 1
|
|
|
|
}
|
2021-04-10 17:38:27 +03:00
|
|
|
eprintln(modul)
|
|
|
|
}
|
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
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) }
|
|
|
|
//
|
2021-04-10 17:38:27 +03:00
|
|
|
sql conn {
|
|
|
|
create table Module
|
|
|
|
}
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
sql conn {
|
|
|
|
create table User
|
|
|
|
}
|
2021-04-10 17:38:27 +03:00
|
|
|
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
|
|
|
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +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
|
|
|
|
}
|
|
|
|
sql db {
|
|
|
|
drop table Module
|
|
|
|
}
|
|
|
|
eprintln(modul)
|
examples, orm: fix orm example; add `-d trace_orm` option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`
* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors
The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 22:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
eprintln('------------ ${@METHOD} -----------------')
|
|
|
|
sqlite3_array()!
|
|
|
|
msql_array()!
|
|
|
|
psql_array()!
|
|
|
|
|
|
|
|
sqlite3()!
|
|
|
|
msql()!
|
|
|
|
psql()!
|
2021-07-24 20:49:40 +03:00
|
|
|
}
|