forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorp.go
114 lines (99 loc) · 2.52 KB
/
gorp.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
package controllers
import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"github.com/robfig/revel"
"github.com/robfig/revel/modules/db/app"
"github.com/robfig/revel/samples/booking/app/models"
)
var (
dbm *gorp.DbMap
)
type GorpPlugin struct {
rev.EmptyPlugin
}
func (p GorpPlugin) OnAppStart() {
db.DbPlugin{}.OnAppStart()
dbm = &gorp.DbMap{Db: db.Db, Dialect: gorp.SqliteDialect{}}
setColumnSizes := func(t *gorp.TableMap, colSizes map[string]int) {
for col, size := range colSizes {
t.ColMap(col).MaxSize = size
}
}
t := dbm.AddTable(models.User{}).SetKeys(true, "UserId")
t.ColMap("Password").Transient = true
setColumnSizes(t, map[string]int{
"Username": 20,
"Name": 100,
})
t = dbm.AddTable(models.Hotel{}).SetKeys(true, "HotelId")
setColumnSizes(t, map[string]int{
"Name": 50,
"Address": 100,
"City": 40,
"State": 6,
"Zip": 6,
"Country": 40,
})
t = dbm.AddTable(models.Booking{}).SetKeys(true, "BookingId")
t.ColMap("User").Transient = true
t.ColMap("Hotel").Transient = true
t.ColMap("CheckInDate").Transient = true
t.ColMap("CheckOutDate").Transient = true
setColumnSizes(t, map[string]int{
"CardNumber": 16,
"NameOnCard": 50,
})
dbm.TraceOn("[gorp]", rev.INFO)
dbm.CreateTables()
bcryptPassword, _ := bcrypt.GenerateFromPassword(
[]byte("demo"), bcrypt.DefaultCost)
demoUser := &models.User{0, "Demo User", "demo", "demo", bcryptPassword}
if err := dbm.Insert(demoUser); err != nil {
panic(err)
}
hotels := []*models.Hotel{
&models.Hotel{0, "Marriott Courtyard", "Tower Pl, Buckhead", "Atlanta", "GA", "30305", "USA", 120},
&models.Hotel{0, "W Hotel", "Union Square, Manhattan", "New York", "NY", "10011", "USA", 450},
&models.Hotel{0, "Hotel Rouge", "1315 16th St NW", "Washington", "DC", "20036", "USA", 250},
}
for _, hotel := range hotels {
if err := dbm.Insert(hotel); err != nil {
panic(err)
}
}
}
type GorpController struct {
*rev.Controller
Txn *gorp.Transaction
}
func (c *GorpController) Begin() rev.Result {
txn, err := dbm.Begin()
if err != nil {
panic(err)
}
c.Txn = txn
return nil
}
func (c *GorpController) Commit() rev.Result {
if c.Txn == nil {
return nil
}
if err := c.Txn.Commit(); err != nil && err != sql.ErrTxDone {
panic(err)
}
c.Txn = nil
return nil
}
func (c *GorpController) Rollback() rev.Result {
if c.Txn == nil {
return nil
}
if err := c.Txn.Rollback(); err != nil && err != sql.ErrTxDone {
panic(err)
}
c.Txn = nil
return nil
}