2014-06-12 23:41:04 +00:00
|
|
|
package database
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RepoManager interface {
|
|
|
|
// Find retrieves the Repo by ID.
|
2014-06-12 23:41:04 +00:00
|
|
|
Find(id int64) (*model.Repo, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// FindName retrieves the Repo by the remote, owner and name.
|
2014-06-12 23:41:04 +00:00
|
|
|
FindName(remote, owner, name string) (*model.Repo, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Insert persists a new Repo to the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Insert(repo *model.Repo) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Insert persists a modified Repo to the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Update(repo *model.Repo) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Delete removes a Repo from the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
Delete(repo *model.Repo) error
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// List retrieves all repositories from the datastore.
|
2014-06-12 23:41:04 +00:00
|
|
|
List(user int64) ([]*model.Repo, error)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// List retrieves all public repositories from the datastore.
|
|
|
|
//ListPublic(user int64) ([]*Repo, error)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func NewRepoManager(db *sql.DB) RepoManager {
|
2014-06-04 21:25:38 +00:00
|
|
|
return &repoManager{db}
|
|
|
|
}
|
|
|
|
|
|
|
|
type repoManager struct {
|
|
|
|
*sql.DB
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) Find(id int64) (*model.Repo, error) {
|
2014-06-04 21:25:38 +00:00
|
|
|
const query = "select * from repos where repo_id = ?"
|
2014-06-12 23:41:04 +00:00
|
|
|
var repo = model.Repo{}
|
2014-06-04 21:25:38 +00:00
|
|
|
var err = meddler.QueryRow(db, &repo, query, id)
|
|
|
|
return &repo, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) FindName(remote, owner, name string) (*model.Repo, error) {
|
2014-06-07 20:33:34 +00:00
|
|
|
const query = "select * from repos where repo_host = ? and repo_owner = ? and repo_name = ?"
|
2014-06-12 23:41:04 +00:00
|
|
|
var repo = model.Repo{}
|
2014-06-04 21:25:38 +00:00
|
|
|
var err = meddler.QueryRow(db, &repo, query, remote, owner, name)
|
|
|
|
return &repo, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) List(user int64) ([]*model.Repo, error) {
|
2014-06-04 21:25:38 +00:00
|
|
|
const query = "select * from repos where repo_id IN (select repo_id from perms where user_id = ?)"
|
2014-06-12 23:41:04 +00:00
|
|
|
var repos []*model.Repo
|
2014-06-04 21:25:38 +00:00
|
|
|
err := meddler.QueryAll(db, &repos, query, user)
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) Insert(repo *model.Repo) error {
|
2014-06-04 21:25:38 +00:00
|
|
|
repo.Created = time.Now().Unix()
|
|
|
|
repo.Updated = time.Now().Unix()
|
|
|
|
return meddler.Insert(db, "repos", repo)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) Update(repo *model.Repo) error {
|
2014-06-04 21:25:38 +00:00
|
|
|
repo.Updated = time.Now().Unix()
|
|
|
|
return meddler.Update(db, "repos", repo)
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
func (db *repoManager) Delete(repo *model.Repo) error {
|
2014-06-04 21:25:38 +00:00
|
|
|
const stmt = "delete from repos where repo_id = ?"
|
|
|
|
_, err := db.Exec(stmt, repo.ID)
|
|
|
|
return err
|
|
|
|
}
|