2021-04-10 17:38:27 +03:00
|
|
|
# ORM
|
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
2021-04-15 10:53:43 +03:00
|
|
|
### Structs
|
|
|
|
|
2021-06-08 00:48:42 +03:00
|
|
|
- `[table: 'name']` sets a custom table name
|
2021-04-15 10:53:43 +03:00
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
### Fields
|
|
|
|
|
2021-04-12 00:57:25 +03:00
|
|
|
- `[primary]` sets the field as the primary key
|
|
|
|
- `[unique]` sets the field as unique
|
|
|
|
- `[unique: 'foo']` adds the field to a unique group
|
2021-04-10 17:38:27 +03:00
|
|
|
- `[skip]` field will be skipped
|
2022-07-09 14:04:35 +03:00
|
|
|
- `[sql: type]` where `type` is a V type such as `int` or `f64`, or special type `serial`
|
2021-04-15 10:53:43 +03:00
|
|
|
- `[sql: 'name']` sets a custom column name for the field
|
2021-04-10 17:38:27 +03:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
struct Foo {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
name string [nonull]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Create
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
create table Foo
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-04-12 00:57:25 +03:00
|
|
|
### Drop
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
drop table Foo
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
### Insert
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
var := Foo{
|
|
|
|
name: 'abc'
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert var into Foo
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Update
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
update Foo set name = 'cde' 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 limit 5
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```v ignore
|
|
|
|
result := sql db {
|
|
|
|
select from Foo where id > 1 order by id
|
|
|
|
}
|
2021-06-08 00:48:42 +03:00
|
|
|
```
|