-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstates.go
67 lines (54 loc) · 1.29 KB
/
states.go
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
package main
import (
"./dm";
"reflect";
"fmt";
)
type State struct {
Id int;
Name string;
Abbv string;
Null bool;
*dm.Model;
}
// declare models globally
var States dm.Model
func setup() {
dm.Init("states.db");
States = dm.AddModel("State", "states", reflect.Typeof(State{}));
}
func main() {
setup();
state := States.New().(State);
state.Name = "Jimmy Dean";
state.Abbv = "JD";
States.Save(state);
state = States.Find(12).(State);
fmt.Printf("FIND BY ID: %s\n", state.Name);
state = States.First().(State);
fmt.Printf("FIRST: %s\n", state.Name);
// null
state = States.First(dm.Opts{"conditions": "name='Russia'"}).(State);
if state.Null {
println("Can't find russia: NULL")
} else {
fmt.Printf("RUSSIA: %s\n", state.Name)
}
state = States.Last().(State);
fmt.Printf("LAST: %s\n", state.Name);
count := States.Count();
fmt.Printf("COUNT: %d\n", count);
println("First 5 states");
states := States.All(dm.Opts{"limit": 5});
for s := range states.Results.Iter() {
state = s.(State);
fmt.Printf("id: %d name: %s abbv: %s\n", state.Id, state.Name, state.Abbv);
}
println();
println("States");
states = States.All();
for s := range states.Results.Iter() {
state = s.(State);
fmt.Printf("id: %d name: %s abbv: %s\n", state.Id, state.Name, state.Abbv);
}
}