-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
191 lines (160 loc) · 4.38 KB
/
demo.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"log"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/tomyl/xl"
"github.com/tomyl/xl/logger"
)
const schema = `
create table department (
id integer primary key,
created_at timestamp not null,
name text not null,
city text not null
);
insert into department (id, created_at, name, city) values (1, current_timestamp, 'HR', 'Stockholm');
insert into department (id, created_at, name, city) values (2, current_timestamp, 'R&D', 'Hong Kong');
create table employee (
id integer primary key,
created_at timestamp not null,
department_id integer references department (id) not null,
name text not null,
salary integer not null
);
insert into employee (id, created_at, department_id, name, salary) values (1, current_timestamp, 1, 'Alice Örn', 12000);
insert into employee (id, created_at, department_id, name, salary) values (2, current_timestamp, 2, 'Bob Älv', 9000);
`
type Department struct {
ID int64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
Name string `db:"name"`
City string `db:"city"`
}
type Employee struct {
ID int64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
DepartmentID int64 `db:"department_id"`
Name string `db:"name"`
Salary int64 `db:"salary"`
}
func main() {
db, err := xl.Open("sqlite3", ":memory:")
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
xl.SetLogger(logger.Color)
if err := xl.MultiExec(db, schema); err != nil {
log.Fatalf("Failed to create schema: %v", err)
}
// Insert an employee
var empId int64
{
q := xl.Insert("employee")
q.SetRaw("created_at", "current_timestamp")
q.Set("department_id", 1)
q.Set("name", "Cecil Ål")
q.Set("salary", 12345)
id, err := q.ExecId(db)
if err != nil {
log.Fatalf("Failed to insert: %v", err)
}
empId = id
log.Printf("Inserted employee %d", empId)
}
// Update employee
{
q := xl.Update("employee")
q.Where("id=?", empId)
q.Set("salary", 14000)
if err := q.ExecOne(db); err != nil {
log.Fatalf("Failed to update: %v", err)
}
log.Printf("Updated employee")
}
// Select all employees
{
var entries []Employee
q := xl.Select("*").From("employee")
if err := q.All(db, &entries); err != nil {
log.Fatalf("Failed to select: %v", err)
}
log.Printf("Employees: %v", entries)
}
// Select employee with highest salary
{
var entry Employee
q := xl.Select("*").From("employee")
q.OrderBy("salary DESC")
q.LimitOffset(1, 0)
if err := q.First(db, &entry); err != nil {
log.Fatalf("Failed to select: %v", err)
}
log.Printf("Employee: %v", entry)
// Re-run query with COUNT(*) and without LIMIT/OFFSET. Useful for pagination.
count, err := q.Total(db)
if err != nil {
log.Fatalf("Failed to count: %v", err)
}
log.Printf("%d employees", count)
}
// Select employee names from Stockholm department
{
var entries []string
q := xl.Select("e.name")
q.FromAs("employee", "e")
q.FromAs("department", "d")
q.Where("e.department_id=d.id")
q.Where("d.city=?", "Stockholm")
if err := q.All(db, &entries); err != nil {
log.Fatalf("Failed to select: %v", err)
}
log.Printf("Employees: %v", entries)
}
// Select employees with inner join
{
var entries []struct {
Department `db:"department"`
Employee `db:"employee"`
}
iq := xl.Select(`d.name "department.name"`)
iq.FromAs("department", "d")
iq.Where("d.city=?", "Stockholm")
q := xl.Select(`e.name "employee.name"`)
q.FromAs("employee", "e")
q.InnerJoin(iq, "d.id=e.department_id")
q.OrderBy("d.name, e.name")
if err := q.All(db, &entries); err != nil {
log.Fatalf("Failed to select: %v", err)
}
log.Printf("Employees: %v", entries)
}
// Same as above, just using SelectAlias instead of Select
{
var entries []struct {
Department `db:"d"`
Employee `db:"e"`
}
iq := xl.SelectAlias("name")
iq.FromAs("department", "d")
iq.Where("d.city=?", "Stockholm")
q := xl.SelectAlias("name")
q.FromAs("employee", "e")
q.InnerJoin(iq, "d.id=e.department_id")
q.OrderBy("d.name, e.name")
if err := q.All(db, &entries); err != nil {
log.Fatalf("Failed to select: %v", err)
}
log.Printf("Employees: %v", entries)
}
// Delete employee
{
q := xl.Delete("employee")
q.Where("id=?", empId)
count, err := q.ExecCount(db)
if err != nil {
log.Fatalf("Failed to update: %v", err)
}
log.Printf("Deleted %d employees", count)
}
}