Less models
This commit is contained in:
parent
255b6879c7
commit
3e0ac1ea2d
27
db/common.go
27
db/common.go
|
@ -1,7 +1,6 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
|
@ -10,33 +9,11 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
conn *sqlx.DB
|
||||
stmts map[string]*sqlx.Stmt
|
||||
conn *sqlx.DB
|
||||
)
|
||||
|
||||
func Connect(params string) (err error) {
|
||||
conn, err = sqlx.Connect("mysql", params)
|
||||
|
||||
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
||||
stmts = map[string]*sqlx.Stmt{}
|
||||
conn.Mappper = reflectx.NewMapperFunc("json", strings.ToLower)
|
||||
return
|
||||
}
|
||||
|
||||
func stmt(query string) *sqlx.Stmt {
|
||||
if stmt, ok := stmts[query]; ok {
|
||||
return stmt
|
||||
} else {
|
||||
stmt := prepareStatement(query)
|
||||
stmts[query] = stmt
|
||||
return stmt
|
||||
}
|
||||
}
|
||||
|
||||
func prepareStatement(query string) *sqlx.Stmt {
|
||||
if stmt, err := conn.Preparex(query); err == nil {
|
||||
return stmt
|
||||
} else {
|
||||
fmt.Println(query)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,29 +1,21 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
type Contrib struct {
|
||||
Week int64 `json:"week"`
|
||||
Author string `json:"author"`
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Commits int64 `json:"commits"`
|
||||
Additions int64 `json:"additions"`
|
||||
Deletions int64 `json:"deletions"`
|
||||
}
|
||||
|
||||
type (
|
||||
Contrib struct {
|
||||
Week int64
|
||||
Author string
|
||||
Owner string
|
||||
Repo string
|
||||
Commits int
|
||||
Additions int
|
||||
Deletions int
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
saveContribQuery = "" +
|
||||
"replace into contributions (week, author, owner, repo, commits, additions, deletions) " +
|
||||
"values (?, ?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
const saveContribQuery = `
|
||||
insert into contributions (week, author, owner, repo, commits, additions, deletions)
|
||||
values (?, ?, ?, ?, ?, ?, ?)
|
||||
on duplicate key update
|
||||
commits=values(commits), additions=values(additions), deletions=values(deletions)`
|
||||
|
||||
func (c *Contrib) Save() {
|
||||
if _, err := stmt(saveContribQuery).Exec(structs.Values(c)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
conn.MustExec(saveContribQuery, c.Week, c.Author, c.Owner, c.Repo, c.Commits, c.Additions, c.Deletions)
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
Hook struct {
|
||||
ID int
|
||||
Owner string
|
||||
}
|
||||
)
|
|
@ -1,8 +0,0 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
Member struct {
|
||||
TeamID int
|
||||
Login string
|
||||
}
|
||||
)
|
20
db/org.go
20
db/org.go
|
@ -1,20 +1,16 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
Org struct {
|
||||
Login string `json:"login"`
|
||||
Descr string `json:"descr"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
)
|
||||
type Org struct {
|
||||
Login string `json:"login"`
|
||||
Descr string `json:"descr"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
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) {
|
||||
if err := stmt(userOrgsQuery).Select(&orgs, login); err != nil {
|
||||
if err := conn.Select(&orgs, userOrgsQuery, login); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
Owner struct {
|
||||
Login string
|
||||
Name string
|
||||
}
|
||||
)
|
22
db/repo.go
22
db/repo.go
|
@ -1,22 +1,12 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
type Repo struct {
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type (
|
||||
Repo struct {
|
||||
Owner string
|
||||
Name string
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
saveRepoQuery = "replace into repos (owner, name, updated_at) values (?, ?, now())"
|
||||
)
|
||||
const saveRepoQuery = `replace into repos (owner, name, updated_at) values (?, ?, now())`
|
||||
|
||||
func (r *Repo) Save() {
|
||||
if _, err := stmt(saveRepoQuery).Exec(structs.Values(r)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
conn.MustExec(saveRepoQuery, r.Owner, r.Name)
|
||||
}
|
||||
|
|
39
db/task.go
39
db/task.go
|
@ -1,39 +0,0 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Task struct {
|
||||
Token string
|
||||
Owner string
|
||||
Job string
|
||||
Worker string
|
||||
Duration int64
|
||||
Error string
|
||||
CreatedAt time.Time
|
||||
StartedAt time.Time
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
saveTaskQuery = "" +
|
||||
"insert into tasks (token, owner, job, worker, duration, error, created_at, started_at) " +
|
||||
"values (?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
|
||||
func (t *Task) Save() {
|
||||
if _, err := stmt(saveTaskQuery).Exec(
|
||||
t.Token, t.Owner,
|
||||
t.Job, t.Worker,
|
||||
t.Duration, t.Error,
|
||||
t.CreatedAt, t.StartedAt,
|
||||
); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) T() *Task {
|
||||
return t
|
||||
}
|
19
db/team.go
19
db/team.go
|
@ -1,9 +1,16 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
Team struct {
|
||||
ID int
|
||||
Owner string
|
||||
Name string
|
||||
type Team struct {
|
||||
ID int64 `json:"id"`
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
const orgTeamsQuery = `select * from teams where owner = ?`
|
||||
|
||||
func OrgTeams(login string) (teams []*Team) {
|
||||
if err := conn.Select(&teams, orgTeamsQuery, login); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
36
db/token.go
36
db/token.go
|
@ -4,28 +4,22 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
Token struct {
|
||||
ID int
|
||||
User string
|
||||
Token string
|
||||
Limit int
|
||||
Remaining int
|
||||
ResetAt time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
)
|
||||
type Token struct {
|
||||
ID int64 `json:"id"`
|
||||
User string `json:"user"`
|
||||
Token string `json:"token"`
|
||||
Quota int64 `json:"quota"`
|
||||
Remaining int64 `json:"remaining"`
|
||||
ResetAt time.Time `json:"reset_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
const (
|
||||
saveTokenQuery = "" +
|
||||
"insert into tokens (`user`, token, `limit`, remaining, reset_at, created_at) " +
|
||||
"values (?, ?, ?, ?, ?, now()) " +
|
||||
"on duplicate key update " +
|
||||
"`limit` = values(`limit`), remaining = values(remaining), reset_at = values(reset_at)"
|
||||
)
|
||||
const saveTokenQuery = `
|
||||
insert into tokens (user, token, quota, remaining, reset_at, created_at)
|
||||
values (?, ?, ?, ?, ?, now())
|
||||
on duplicate key update
|
||||
quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)`
|
||||
|
||||
func (t *Token) Save() {
|
||||
if _, err := stmt(saveTokenQuery).Exec(t.User, t.Token, t.Limit, t.Remaining, t.ResetAt); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
conn.MustExec(saveTokenQuery, t.User, t.Token, t.Quota, t.Remaining, t.ResetAt)
|
||||
}
|
||||
|
|
30
db/user.go
30
db/user.go
|
@ -1,24 +1,18 @@
|
|||
package db
|
||||
|
||||
type (
|
||||
User struct {
|
||||
Login string `json:"login"`
|
||||
Name string `json:"name"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
)
|
||||
type User struct {
|
||||
Login string `json:"login"`
|
||||
Name string `json:"name"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
const (
|
||||
saveUserQuery = "" +
|
||||
"insert into users (login, name, id, avatar_url) " +
|
||||
"values (?, ?, ?, ?) " +
|
||||
"on duplicate key update " +
|
||||
"login=values(login), name=values(name), avatar_url=values(avatar_url)"
|
||||
)
|
||||
const saveUserQuery = `
|
||||
insert into users (login, name, id, avatar_url)
|
||||
values (?, ?, ?, ?)
|
||||
on duplicate key update
|
||||
login=values(login), name=values(name), avatar_url=values(avatar_url)`
|
||||
|
||||
func (u *User) Save() {
|
||||
if _, err := stmt(saveUserQuery).Exec(u.Login, u.Name, u.ID, u.AvatarURL); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
conn.MustExec(saveUserQuery, u.Login, u.Name, u.ID, u.AvatarURL)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue