Use named queries and helper methods
This commit is contained in:
parent
d03c5e41e0
commit
13f971a731
12
db/common.go
12
db/common.go
|
@ -17,3 +17,15 @@ func Connect(params string) (err error) {
|
||||||
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustExecN(query string, arg interface{}) {
|
||||||
|
if _, err := conn.NamedExec(query, arg); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustSelect(dest interface{}, query string, args ...interface{}) {
|
||||||
|
if err := conn.Select(dest, query, args...); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -12,10 +12,10 @@ type Contrib struct {
|
||||||
|
|
||||||
const saveContribQuery = `
|
const saveContribQuery = `
|
||||||
insert into contributions (week, author, owner, repo, commits, additions, deletions)
|
insert into contributions (week, author, owner, repo, commits, additions, deletions)
|
||||||
values (?, ?, ?, ?, ?, ?, ?)
|
values (:week, :author, :owner, :repo, :commits, :additions, :deletions)
|
||||||
on duplicate key update
|
on duplicate key update
|
||||||
commits=values(commits), additions=values(additions), deletions=values(deletions)`
|
commits=values(commits), additions=values(additions), deletions=values(deletions)`
|
||||||
|
|
||||||
func (c *Contrib) Save() {
|
func (c *Contrib) Save() {
|
||||||
conn.MustExec(saveContribQuery, c.Week, c.Author, c.Owner, c.Repo, c.Commits, c.Additions, c.Deletions)
|
mustExecN(saveContribQuery, c)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,6 @@ type Org struct {
|
||||||
const userOrgsQuery = `select o.* from members m join orgs o on o.login = m.org where user = ?`
|
const userOrgsQuery = `select o.* from members m join orgs o on o.login = m.org where user = ?`
|
||||||
|
|
||||||
func UserOrgs(login string) (orgs []*Org) {
|
func UserOrgs(login string) (orgs []*Org) {
|
||||||
if err := conn.Select(&orgs, userOrgsQuery, login); err != nil {
|
mustSelect(&orgs, userOrgsQuery, login)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,17 +16,15 @@ type Repo struct {
|
||||||
const orgReposQuery = `select * from repos where owner = ?`
|
const orgReposQuery = `select * from repos where owner = ?`
|
||||||
const saveRepoQuery = `
|
const saveRepoQuery = `
|
||||||
insert into repos (owner, name, updated_at)
|
insert into repos (owner, name, updated_at)
|
||||||
values (?, ?, now())
|
values (:owner, :name, now())
|
||||||
on duplicate key update
|
on duplicate key update
|
||||||
updated_at=now()`
|
updated_at=now()`
|
||||||
|
|
||||||
func (r *Repo) Save() {
|
func (r *Repo) Save() {
|
||||||
conn.MustExec(saveRepoQuery, r.Owner, r.Name)
|
mustExecN(saveRepoQuery, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OrgRepos(login string) (repos []*Repo) {
|
func OrgRepos(login string) (repos []*Repo) {
|
||||||
if err := conn.Select(&repos, orgReposQuery, login); err != nil {
|
mustSelect(&repos, orgReposQuery, login)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,6 @@ type Team struct {
|
||||||
const orgTeamsQuery = `select * from teams where owner = ?`
|
const orgTeamsQuery = `select * from teams where owner = ?`
|
||||||
|
|
||||||
func OrgTeams(login string) (teams []*Team) {
|
func OrgTeams(login string) (teams []*Team) {
|
||||||
if err := conn.Select(&teams, orgTeamsQuery, login); err != nil {
|
mustSelect(&teams, orgTeamsQuery, login)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,10 @@ type Token struct {
|
||||||
|
|
||||||
const saveTokenQuery = `
|
const saveTokenQuery = `
|
||||||
insert into tokens (user, token, quota, remaining, reset_at, created_at)
|
insert into tokens (user, token, quota, remaining, reset_at, created_at)
|
||||||
values (?, ?, ?, ?, ?, now())
|
values (:user, :token, :quota, :remaining, :reset_at, now())
|
||||||
on duplicate key update
|
on duplicate key update
|
||||||
quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)`
|
quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)`
|
||||||
|
|
||||||
func (t *Token) Save() {
|
func (t *Token) Save() {
|
||||||
conn.MustExec(saveTokenQuery, t.User, t.Token, t.Quota, t.Remaining, t.ResetAt)
|
mustExecN(saveTokenQuery, t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,10 @@ type User struct {
|
||||||
|
|
||||||
const saveUserQuery = `
|
const saveUserQuery = `
|
||||||
insert into users (login, name, id, avatar_url)
|
insert into users (login, name, id, avatar_url)
|
||||||
values (?, ?, ?, ?)
|
values (:login, :name, :id, :avatar_url)
|
||||||
on duplicate key update
|
on duplicate key update
|
||||||
login=values(login), name=values(name), avatar_url=values(avatar_url)`
|
login=values(login), name=values(name), avatar_url=values(avatar_url)`
|
||||||
|
|
||||||
func (u *User) Save() {
|
func (u *User) Save() {
|
||||||
conn.MustExec(saveUserQuery, u.Login, u.Name, u.ID, u.AvatarURL)
|
mustExecN(saveUserQuery, u)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue