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: 'foo']` adds the field to a unique group
|
||||
- `[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: 'name']` sets a custom column name for the field
|
||||
- `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql
|
||||
@ -22,6 +22,8 @@
|
||||
## Usage
|
||||
|
||||
```v ignore
|
||||
import time
|
||||
|
||||
struct Foo {
|
||||
id int [primary; sql: serial]
|
||||
name string [nonull]
|
||||
@ -36,7 +38,6 @@ struct Child {
|
||||
parent_id int
|
||||
name string
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Create
|
||||
@ -44,7 +45,7 @@ struct Child {
|
||||
```v ignore
|
||||
sql db {
|
||||
create table Foo
|
||||
}
|
||||
}!
|
||||
```
|
||||
|
||||
### Drop
|
||||
@ -52,16 +53,16 @@ sql db {
|
||||
```v ignore
|
||||
sql db {
|
||||
drop table Foo
|
||||
}
|
||||
}!
|
||||
```
|
||||
|
||||
### Insert
|
||||
|
||||
```v ignore
|
||||
var := Foo{
|
||||
foo := Foo{
|
||||
name: 'abc'
|
||||
created_at: time.now()
|
||||
updated_at: time.now().str()
|
||||
updated_at: time.now()
|
||||
deleted_at: time.now()
|
||||
children: [
|
||||
Child{
|
||||
@ -74,8 +75,8 @@ var := Foo{
|
||||
}
|
||||
|
||||
sql db {
|
||||
insert var into Foo
|
||||
}
|
||||
insert foo into Foo
|
||||
}!
|
||||
```
|
||||
|
||||
### Update
|
||||
@ -83,31 +84,31 @@ sql db {
|
||||
```v ignore
|
||||
sql db {
|
||||
update Foo set name = 'cde', updated_at = time.now() where name == 'abc'
|
||||
}
|
||||
}!
|
||||
```
|
||||
|
||||
### Delete
|
||||
```v ignore
|
||||
sql db {
|
||||
delete from Foo where id > 10
|
||||
}
|
||||
}!
|
||||
```
|
||||
|
||||
### Select
|
||||
```v ignore
|
||||
result := sql db {
|
||||
select from Foo where id == 1
|
||||
}
|
||||
}!
|
||||
```
|
||||
```v ignore
|
||||
result := sql db {
|
||||
select from Foo where id > 1 && name != 'lasanha' limit 5
|
||||
}
|
||||
}!
|
||||
```
|
||||
```v ignore
|
||||
result := sql db {
|
||||
select from Foo where id > 1 order by id
|
||||
}
|
||||
}!
|
||||
```
|
||||
|
||||
### Example
|
||||
@ -127,10 +128,7 @@ fn main() {
|
||||
user: 'user'
|
||||
password: 'password'
|
||||
dbname: 'dbname'
|
||||
}) or {
|
||||
println(err)
|
||||
return
|
||||
}
|
||||
})!
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
@ -138,7 +136,7 @@ fn main() {
|
||||
|
||||
sql db {
|
||||
create table Member
|
||||
}
|
||||
}!
|
||||
|
||||
new_member := Member{
|
||||
name: 'John Doe'
|
||||
@ -146,16 +144,15 @@ fn main() {
|
||||
|
||||
sql db {
|
||||
insert new_member into Member
|
||||
}
|
||||
}!
|
||||
|
||||
selected_member := sql db {
|
||||
selected_members := sql db {
|
||||
select from Member where name == 'John Doe' limit 1
|
||||
}
|
||||
}!
|
||||
john_doe := selected_members.first()
|
||||
|
||||
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