mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs, orm: update examples (#18106)
This commit is contained in:
parent
2c123dffbb
commit
40a97aed1a
@ -12,7 +12,7 @@
|
|||||||
- `[unique]` sets the field as unique
|
- `[unique]` sets the field as unique
|
||||||
- `[unique: 'foo']` adds the field to a unique group
|
- `[unique: 'foo']` adds the field to a unique group
|
||||||
- `[nonull]` set the field as not null
|
- `[nonull]` set the field as not null
|
||||||
- `[skip]` field will be skipped
|
- `[skip]` or `[sql: '-']` field will be skipped
|
||||||
- `[sql: type]` where `type` is a V type such as `int` or `f64`, or special type `serial`
|
- `[sql: type]` where `type` is a V type such as `int` or `f64`, or special type `serial`
|
||||||
- `[sql: 'name']` sets a custom column name for the field
|
- `[sql: 'name']` sets a custom column name for the field
|
||||||
- `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql
|
- `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql
|
||||||
@ -22,6 +22,8 @@
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
|
import time
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
name string [nonull]
|
name string [nonull]
|
||||||
@ -36,7 +38,6 @@ struct Child {
|
|||||||
parent_id int
|
parent_id int
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create
|
### Create
|
||||||
@ -44,7 +45,7 @@ struct Child {
|
|||||||
```v ignore
|
```v ignore
|
||||||
sql db {
|
sql db {
|
||||||
create table Foo
|
create table Foo
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Drop
|
### Drop
|
||||||
@ -52,16 +53,16 @@ sql db {
|
|||||||
```v ignore
|
```v ignore
|
||||||
sql db {
|
sql db {
|
||||||
drop table Foo
|
drop table Foo
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Insert
|
### Insert
|
||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
var := Foo{
|
foo := Foo{
|
||||||
name: 'abc'
|
name: 'abc'
|
||||||
created_at: time.now()
|
created_at: time.now()
|
||||||
updated_at: time.now().str()
|
updated_at: time.now()
|
||||||
deleted_at: time.now()
|
deleted_at: time.now()
|
||||||
children: [
|
children: [
|
||||||
Child{
|
Child{
|
||||||
@ -74,8 +75,8 @@ var := Foo{
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
insert var into Foo
|
insert foo into Foo
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Update
|
### Update
|
||||||
@ -83,31 +84,31 @@ sql db {
|
|||||||
```v ignore
|
```v ignore
|
||||||
sql db {
|
sql db {
|
||||||
update Foo set name = 'cde', updated_at = time.now() where name == 'abc'
|
update Foo set name = 'cde', updated_at = time.now() where name == 'abc'
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Delete
|
### Delete
|
||||||
```v ignore
|
```v ignore
|
||||||
sql db {
|
sql db {
|
||||||
delete from Foo where id > 10
|
delete from Foo where id > 10
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Select
|
### Select
|
||||||
```v ignore
|
```v ignore
|
||||||
result := sql db {
|
result := sql db {
|
||||||
select from Foo where id == 1
|
select from Foo where id == 1
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
```v ignore
|
```v ignore
|
||||||
result := sql db {
|
result := sql db {
|
||||||
select from Foo where id > 1 && name != 'lasanha' limit 5
|
select from Foo where id > 1 && name != 'lasanha' limit 5
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
```v ignore
|
```v ignore
|
||||||
result := sql db {
|
result := sql db {
|
||||||
select from Foo where id > 1 order by id
|
select from Foo where id > 1 order by id
|
||||||
}
|
}!
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
@ -127,10 +128,7 @@ fn main() {
|
|||||||
user: 'user'
|
user: 'user'
|
||||||
password: 'password'
|
password: 'password'
|
||||||
dbname: 'dbname'
|
dbname: 'dbname'
|
||||||
}) or {
|
})!
|
||||||
println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
db.close()
|
db.close()
|
||||||
@ -138,7 +136,7 @@ fn main() {
|
|||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
create table Member
|
create table Member
|
||||||
}
|
}!
|
||||||
|
|
||||||
new_member := Member{
|
new_member := Member{
|
||||||
name: 'John Doe'
|
name: 'John Doe'
|
||||||
@ -146,16 +144,15 @@ fn main() {
|
|||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
insert new_member into Member
|
insert new_member into Member
|
||||||
}
|
}!
|
||||||
|
|
||||||
selected_member := sql db {
|
selected_members := sql db {
|
||||||
select from Member where name == 'John Doe' limit 1
|
select from Member where name == 'John Doe' limit 1
|
||||||
}
|
}!
|
||||||
|
john_doe := selected_members.first()
|
||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
update Member set name = 'Hitalo' where id == selected_member.id
|
update Member set name = 'Hitalo' where id == john_doe.id
|
||||||
}
|
}!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user