forked from richtr/baseapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorp.go
187 lines (163 loc) · 4.56 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
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
package controllers
import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"github.com/coopernurse/gorp"
r "github.com/revel/revel"
"github.com/revel/revel/modules/db/app"
"github.com/richtr/baseapp/app/models"
"time"
)
var (
Dbm *gorp.DbMap
)
func InitDB() {
db.Init()
dbDriver := r.Config.StringDefault("db.driver", "sqlite3")
if dbDriver == "mysql" {
Dbm = &gorp.DbMap{Db: db.Db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
} else if dbDriver == "postgres" {
Dbm = &gorp.DbMap{Db: db.Db, Dialect: gorp.PostgresDialect{}}
} else {
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
t.ColMap("Created").Transient = true
t.ColMap("Profile").Transient = true
setColumnSizes(t, map[string]int{
"Email": 200,
})
t = Dbm.AddTable(models.Token{}).SetKeys(true, "TokenId")
setColumnSizes(t, map[string]int{
"Email": 200,
"Type": 20,
"Hash": 16,
})
t = Dbm.AddTable(models.Profile{}).SetKeys(true, "ProfileId")
t.ColMap("User").Transient = true
setColumnSizes(t, map[string]int{
"UserName": 64,
"Name": 100,
"Summary": 140,
"Description": 400,
"PhotoUrl": 200,
})
t = Dbm.AddTable(models.Post{}).SetKeys(true, "PostId")
t.ColMap("DateObj").Transient = true
t.ColMap("ContentStr").Transient = true
setColumnSizes(t, map[string]int{
"Title": 400,
"Content": 16777212, // mediumblob storage capacity
})
// Social components
Dbm.AddTable(models.Like{}).SetKeys(true, "LikeId")
Dbm.AddTable(models.Follower{}).SetKeys(true, "FollowerId")
Dbm.TraceOn("[gorp]", r.INFO)
// Create tables in datastore if they don't already exist
Dbm.CreateTablesIfNotExists()
// Set up database test data in 'test' run mode
// e.g. `$> revel run baseapp test`
if r.RunMode == "test" {
bcryptPassword, _ := bcrypt.GenerateFromPassword(
[]byte("demouser"), bcrypt.DefaultCost)
demoUser := &models.User{
Email: "demo@demo.com",
HashedPassword: bcryptPassword,
Confirmed: false,
}
otherUser := &models.User{
Email: "demo1@demo1.com",
HashedPassword: bcryptPassword,
Confirmed: false,
}
if err := Dbm.Insert(demoUser, otherUser); err != nil {
panic(err)
}
demoProfile := &models.Profile{
UserId: demoUser.UserId,
UserName: "demouser",
Name: "Demo User",
Summary: "Just a regular guy",
Description: "...",
PhotoUrl: "http://www.gravatar.com/avatar/53444f91e698c0c7caa2dbc3bdbf93fc?s=128&d=identicon",
User: demoUser,
AggregateFollowing: 1,
}
otherProfile := &models.Profile{
UserId: otherUser.UserId,
UserName: "otheruser",
Name: "Other User",
Summary: "Just another regular guy",
Description: "...",
PhotoUrl: "http://www.gravatar.com/avatar/740fcda1a2304bd073b46f405b3622ce?s=128&d=identicon",
User: otherUser,
AggregateFollowers: 1,
}
if err := Dbm.Insert(demoProfile, otherProfile); err != nil {
panic(err)
}
demoPost := &models.Post{
ProfileId: demoProfile.ProfileId,
Title: "Hello World",
ContentStr: "Full markdown support with things like [links](http://example.org)!",
Status: "public",
DateObj: time.Now(),
}
otherPost := &models.Post{
ProfileId: otherProfile.ProfileId,
Title: "Hello World 1",
ContentStr: "This post does not belong to demo@demo.com account",
Status: "public",
DateObj: time.Now(),
AggregateLikes: 8,
}
if err := Dbm.Insert(demoPost, otherPost); err != nil {
panic(err)
}
demoFollow := &models.Follower{
UserId: demoUser.UserId,
FollowUserId: otherUser.UserId,
}
if err := Dbm.Insert(demoFollow); 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
}