aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/orm/README.md
blob: 5cfa1fd076eaf815a36324ef3320ca3af2c921f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# ORM

## Attributes

### Structs

- `[table: 'name']` sets a custom table name

### Fields

- `[primary]` sets the field as the primary key
- `[unique]` sets the field as unique
- `[unique: 'foo']` adds the field to a unique group
- `[skip]` field will be skipped
- `[sql: type]` sets the type which is used in sql (special type `serial`)
- `[sql: 'name']` sets a custom column name for the field

## Usage

```v ignore
struct Foo {
    id   int    [primary; sql: serial]
    name string [nonull]
}
```

### Create

```v ignore
sql db {
    create table Foo
}
```

### Drop

```v ignore
sql db {
    drop table Foo
}
```

### 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
}
```