2023-01-13 18:02:32 +03:00
|
|
|
import db.sqlite
|
2023-02-08 21:37:04 +03:00
|
|
|
import db.mysql as sql
|
2023-01-13 18:02:32 +03:00
|
|
|
import db.pg
|
2021-02-04 22:28:33 +03:00
|
|
|
|
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']
|
2021-04-15 10:53:43 +03:00
|
|
|
name string [sql: 'username'; 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 {
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-04 22:28:33 +03:00
|
|
|
fn main() {
|
2021-04-30 09:13:26 +03:00
|
|
|
sqlite3_array()
|
|
|
|
mysql_array()
|
2021-07-24 20:49:40 +03:00
|
|
|
psql_array()
|
2021-04-30 09:13:26 +03:00
|
|
|
|
2021-04-29 21:00:17 +03:00
|
|
|
sqlite3()
|
|
|
|
mysql()
|
2021-07-24 20:49:40 +03:00
|
|
|
psql()
|
2021-04-29 21:00:17 +03:00
|
|
|
}
|
|
|
|
|
2021-04-30 09:13:26 +03:00
|
|
|
fn sqlite3_array() {
|
|
|
|
mut db := sqlite.connect(':memory:') or { panic(err) }
|
|
|
|
sql db {
|
|
|
|
create table Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
drop table Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mysql_array() {
|
2023-02-08 21:37:04 +03:00
|
|
|
mut db := sql.Connection{
|
2021-04-30 09:13:26 +03:00
|
|
|
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 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)
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
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 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)
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
drop table Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
db.close()
|
2021-07-24 20:49:40 +03:00
|
|
|
}
|
2021-04-30 09:13:26 +03:00
|
|
|
|
2021-04-29 21:00:17 +03:00
|
|
|
fn sqlite3() {
|
|
|
|
mut db := sqlite.connect(':memory:') or { panic(err) }
|
2021-04-10 17:38:27 +03:00
|
|
|
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-03-01 02:18:14 +03:00
|
|
|
|
2021-02-04 22:28:33 +03:00
|
|
|
modul := sql db {
|
|
|
|
select from Module where id == 1
|
|
|
|
}
|
|
|
|
|
2021-04-12 00:57:25 +03:00
|
|
|
sql db {
|
|
|
|
drop table Module
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
eprintln(modul)
|
2021-04-29 21:00:17 +03:00
|
|
|
db.close() or { panic(err) }
|
2021-04-10 17:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mysql() {
|
2023-02-08 21:37:04 +03:00
|
|
|
mut conn := sql.Connection{
|
2021-04-10 17:38:27 +03:00
|
|
|
host: 'localhost'
|
|
|
|
port: 3306
|
|
|
|
username: 'root'
|
|
|
|
password: 'abc'
|
|
|
|
dbname: 'test'
|
|
|
|
}
|
|
|
|
conn.connect() or { panic(err) }
|
|
|
|
|
|
|
|
sql conn {
|
|
|
|
create table Module
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
|
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
|
|
|
|
}
|
2021-04-29 21:00:17 +03:00
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
eprintln(m)
|
2021-04-29 21:00:17 +03:00
|
|
|
conn.close()
|
2021-02-04 22:28:33 +03:00
|
|
|
}
|
2021-04-25 18:57:55 +03: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 13:38:36 +03:00
|
|
|
|
|
|
|
modul := sql db {
|
|
|
|
select from Module where id == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
drop table Module
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln(modul)
|
2021-04-29 21:00:17 +03:00
|
|
|
db.close()
|
2021-07-24 20:49:40 +03:00
|
|
|
}
|