forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorp.go
110 lines (96 loc) · 2.43 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
package controllers
import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
r "github.com/revel/revel"
"github.com/revel/revel/modules/db/app"
"github.com/revel/revel/samples/booking/app/models"
)
var (
Dbm *gorp.DbMap
)
func InitDB() {
db.Init()
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]", r.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 {
*r.Controller
Txn *gorp.Transaction
}
func (c *GorpController) Begin() r.Result {
txn, err := Dbm.Begin()
if err != nil {
panic(err)
}
c.Txn = txn
return nil
}
func (c *GorpController) Commit() r.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() r.Result {
if c.Txn == nil {
return nil
}
if err := c.Txn.Rollback(); err != nil && err != sql.ErrTxDone {
panic(err)
}
c.Txn = nil
return nil
}