migrated star to auto-gen sql

This commit is contained in:
Brad Rydzewski 2015-06-13 17:49:21 -07:00
parent 771f1cf606
commit 1f0c249c82
5 changed files with 212 additions and 40 deletions

View file

@ -110,9 +110,9 @@ CREATE INDEX repos_user_idx ON repos (repo_user_id);
var starTable = ` var starTable = `
CREATE TABLE IF NOT EXISTS stars ( CREATE TABLE IF NOT EXISTS stars (
star_id INTEGER PRIMARY KEY AUTOINCREMENT star_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_id INTEGER ,star_user_id INTEGER
,repo_id INTEGER ,star_repo_id INTEGER
,UNIQUE (repo_id, user_id) ,UNIQUE (star_repo_id, star_user_id)
); );
` `

View file

@ -80,6 +80,6 @@ SELECT
FROM FROM
repos r repos r
,stars s ,stars s
WHERE r.repo_id = s.repo_id WHERE r.repo_id = s.star_repo_id
AND s.user_id = ? AND s.star_user_id = ?
` `

View file

@ -1,60 +1,47 @@
package builtin package builtin
import ( import (
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" "database/sql"
common "github.com/drone/drone/pkg/types"
"github.com/drone/drone/pkg/types"
) )
type Starstore struct { type Starstore struct {
meddler.DB *sql.DB
} }
func NewStarstore(db meddler.DB) *Starstore { func NewStarstore(db *sql.DB) *Starstore {
return &Starstore{db} return &Starstore{db}
} }
// Starred returns true if the user starred // Starred returns true if the user starred
// the given repository. // the given repository.
func (db *Starstore) Starred(user *common.User, repo *common.Repo) (bool, error) { func (db *Starstore) Starred(user *types.User, repo *types.Repo) (bool, error) {
var star = new(star) _, err := getStar(db, rebind(stmtStarSelectStarUserRepo), user.ID, repo.ID)
err := meddler.QueryRow(db, star, rebind(starQuery), user.ID, repo.ID)
return (err == nil), err return (err == nil), err
} }
// AddStar inserts a starred repo / user in the datastore. // AddStar inserts a starred repo / user in the datastore.
func (db *Starstore) AddStar(user *common.User, repo *common.Repo) error { func (db *Starstore) AddStar(user *types.User, repo *types.Repo) error {
var star = &star{UserID: user.ID, RepoID: repo.ID} var star = &Star{UserID: user.ID, RepoID: repo.ID}
return meddler.Insert(db, starTable, star) return createStar(db, rebind(stmtStarInsert), star)
} }
// DelStar removes starred repo / user from the datastore. // DelStar removes starred repo / user from the datastore.
func (db *Starstore) DelStar(user *common.User, repo *common.Repo) error { func (db *Starstore) DelStar(user *types.User, repo *types.Repo) error {
var _, err = db.Exec(rebind(starDeleteStmt), user.ID, repo.ID) var _, err = db.Exec(rebind(stmtStartDeleteUserRepo), user.ID, repo.ID)
return err return err
} }
type star struct { type Star struct {
ID int64 `meddler:"star_id,pk"` ID int64
UserID int64 `meddler:"user_id"` UserID int64 `sql:"unique:ux_star_user_repo"`
RepoID int64 `meddler:"repo_id"` RepoID int64 `sql:"unique:ux_star_user_repo"`
} }
// Stars table name in database.
const starTable = "stars"
// SQL query to retrieve a user's stars to
// access a repository.
const starQuery = `
SELECT *
FROM stars
WHERE user_id=?
AND repo_id=?
LIMIT 1
`
// SQL statement to delete a star by ID. // SQL statement to delete a star by ID.
const starDeleteStmt = ` const stmtStartDeleteUserRepo = `
DELETE FROM stars DELETE FROM stars
WHERE user_id=? WHERE star_user_id=?
AND repo_id=? AND star_repo_id=?
` `

View file

@ -0,0 +1,185 @@
package builtin
// DO NOT EDIT
// code generated by go:generate
import (
"database/sql"
"encoding/json"
)
var _ = json.Marshal
// generic database interface, matching both *sql.Db and *sql.Tx
type starDB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
func getStar(db starDB, query string, args ...interface{}) (*Star, error) {
row := db.QueryRow(query, args...)
return scanStar(row)
}
func getStars(db starDB, query string, args ...interface{}) ([]*Star, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanStars(rows)
}
func createStar(db starDB, query string, v *Star) error {
var v0 int64
var v1 int64
v0 = v.UserID
v1 = v.RepoID
res, err := db.Exec(query,
&v0,
&v1,
)
if err != nil {
return err
}
v.ID, err = res.LastInsertId()
return err
}
func updateStar(db starDB, query string, v *Star) error {
var v0 int64
var v1 int64
var v2 int64
v0 = v.ID
v1 = v.UserID
v2 = v.RepoID
_, err := db.Exec(query,
&v1,
&v2,
&v0,
)
return err
}
func scanStar(row *sql.Row) (*Star, error) {
var v0 int64
var v1 int64
var v2 int64
err := row.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return nil, err
}
v := &Star{}
v.ID = v0
v.UserID = v1
v.RepoID = v2
return v, nil
}
func scanStars(rows *sql.Rows) ([]*Star, error) {
var err error
var vv []*Star
for rows.Next() {
var v0 int64
var v1 int64
var v2 int64
err = rows.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return vv, err
}
v := &Star{}
v.ID = v0
v.UserID = v1
v.RepoID = v2
vv = append(vv, v)
}
return vv, rows.Err()
}
const stmtStarSelectList = `
SELECT
star_id
,star_user_id
,star_repo_id
FROM stars
`
const stmtStarSelectRange = `
SELECT
star_id
,star_user_id
,star_repo_id
FROM stars
LIMIT ? OFFSET ?
`
const stmtStarSelect = `
SELECT
star_id
,star_user_id
,star_repo_id
FROM stars
WHERE star_id = ?
`
const stmtStarSelectStarUserRepo = `
SELECT
star_id
,star_user_id
,star_repo_id
FROM stars
WHERE star_user_id = ?
AND star_repo_id = ?
`
const stmtStarSelectCount = `
SELECT count(1)
FROM stars
`
const stmtStarInsert = `
INSERT INTO stars (
star_user_id
,star_repo_id
) VALUES (?,?);
`
const stmtStarUpdate = `
UPDATE stars SET
star_user_id = ?
,star_repo_id = ?
WHERE star_id = ?
`
const stmtStarDelete = `
DELETE FROM stars
WHERE star_id = ?
`
const stmtStarTable = `
CREATE TABLE IF NOT EXISTS stars (
star_id INTEGER PRIMARY KEY AUTOINCREMENT
,star_user_id INTEGER
,star_repo_id INTEGER
);
`
const stmtStarStarUserRepoIndex = `
CREATE UNIQUE INDEX IF NOT EXISTS ux_star_user_repo ON stars (star_user_id,star_repo_id);
`

View file

@ -83,8 +83,8 @@ FROM
,repos r ,repos r
,stars s ,stars s
WHERE c.repo_id = r.repo_id WHERE c.repo_id = r.repo_id
AND r.repo_id = s.repo_id AND r.repo_id = s.star_repo_id
AND s.user_id = ? AND s.star_user_id = ?
ORDER BY c.commit_seq DESC ORDER BY c.commit_seq DESC
LIMIT ? OFFSET ? LIMIT ? OFFSET ?
` `