-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
65 lines (53 loc) · 1.41 KB
/
main.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
package main
import (
"log"
"strings"
"github.com/gchaincl/sqlhooks"
"github.com/go-gorp/gorp"
_ "github.com/mattn/go-sqlite3"
)
type Post struct {
// db tag lets you specify the column name if it differs from the struct field
Id int64 `db:"post_id"`
Title string `db:",size:50"` // Column size set to 50
Body string `db:"article_body,size:1024"` // Set both column name and size
}
type Hooks struct{}
func (h Hooks) BeforeQuery(ctx *sqlhooks.Context) error {
log.Println(ctx.Query, ctx.Args)
return nil
}
func (h Hooks) AfterQuery(ctx *sqlhooks.Context) error {
return ctx.Error
}
// Update Post's title field Before Inserting
func (h Hooks) BeforeExec(ctx *sqlhooks.Context) error {
if strings.HasPrefix(ctx.Query, `insert into "posts"`) {
ctx.Args[0] = "[updated] " + ctx.Args[0].(string)
}
return nil
}
func (h Hooks) AfterExec(ctx *sqlhooks.Context) error {
return ctx.Error
}
func main() {
db, err := sqlhooks.Open("sqlite3", ":memory:", &Hooks{})
if err != nil {
panic(err)
}
dbmap := gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
dbmap.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
if err := dbmap.CreateTablesIfNotExists(); err != nil {
panic(err)
}
dbmap.Insert(
&Post{Title: "Foo", Body: "Some Content"},
&Post{Title: "Bar", Body: "More Content"},
)
post := Post{}
p, err := dbmap.Get(&post, 1)
if err != nil {
panic(err)
}
log.Printf("%#v", p)
}