1
0
Fork 0

Use named queries and helper methods

This commit is contained in:
Gregory Eremin 2015-03-06 18:29:52 +07:00
parent d03c5e41e0
commit 13f971a731
7 changed files with 23 additions and 17 deletions

View File

@ -17,3 +17,15 @@ func Connect(params string) (err error) {
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
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)
}
}

View File

@ -12,10 +12,10 @@ type Contrib struct {
const saveContribQuery = `
insert into contributions (week, author, owner, repo, commits, additions, deletions)
values (?, ?, ?, ?, ?, ?, ?)
values (:week, :author, :owner, :repo, :commits, :additions, :deletions)
on duplicate key update
commits=values(commits), additions=values(additions), deletions=values(deletions)`
func (c *Contrib) Save() {
conn.MustExec(saveContribQuery, c.Week, c.Author, c.Owner, c.Repo, c.Commits, c.Additions, c.Deletions)
mustExecN(saveContribQuery, c)
}

View File

@ -10,8 +10,6 @@ type Org struct {
const userOrgsQuery = `select o.* from members m join orgs o on o.login = m.org where user = ?`
func UserOrgs(login string) (orgs []*Org) {
if err := conn.Select(&orgs, userOrgsQuery, login); err != nil {
panic(err)
}
mustSelect(&orgs, userOrgsQuery, login)
return
}

View File

@ -16,17 +16,15 @@ type Repo struct {
const orgReposQuery = `select * from repos where owner = ?`
const saveRepoQuery = `
insert into repos (owner, name, updated_at)
values (?, ?, now())
values (:owner, :name, now())
on duplicate key update
updated_at=now()`
func (r *Repo) Save() {
conn.MustExec(saveRepoQuery, r.Owner, r.Name)
mustExecN(saveRepoQuery, r)
}
func OrgRepos(login string) (repos []*Repo) {
if err := conn.Select(&repos, orgReposQuery, login); err != nil {
panic(err)
}
mustSelect(&repos, orgReposQuery, login)
return
}

View File

@ -9,8 +9,6 @@ type Team struct {
const orgTeamsQuery = `select * from teams where owner = ?`
func OrgTeams(login string) (teams []*Team) {
if err := conn.Select(&teams, orgTeamsQuery, login); err != nil {
panic(err)
}
mustSelect(&teams, orgTeamsQuery, login)
return
}

View File

@ -16,10 +16,10 @@ type Token struct {
const saveTokenQuery = `
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
quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)`
func (t *Token) Save() {
conn.MustExec(saveTokenQuery, t.User, t.Token, t.Quota, t.Remaining, t.ResetAt)
mustExecN(saveTokenQuery, t)
}

View File

@ -9,10 +9,10 @@ type User struct {
const saveUserQuery = `
insert into users (login, name, id, avatar_url)
values (?, ?, ?, ?)
values (:login, :name, :id, :avatar_url)
on duplicate key update
login=values(login), name=values(name), avatar_url=values(avatar_url)`
func (u *User) Save() {
conn.MustExec(saveUserQuery, u.Login, u.Name, u.ID, u.AvatarURL)
mustExecN(saveUserQuery, u)
}