woodpecker/server/database/repo.go

79 lines
2.1 KiB
Go
Raw Normal View History

package database
2014-06-04 21:25:38 +00:00
import (
"database/sql"
"time"
"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.
Find(id int64) (*model.Repo, error)
2014-06-04 21:25:38 +00:00
// FindName retrieves the Repo by the remote, owner and name.
FindName(remote, owner, name string) (*model.Repo, error)
2014-06-04 21:25:38 +00:00
// Insert persists a new Repo to the datastore.
Insert(repo *model.Repo) error
2014-06-04 21:25:38 +00:00
// Insert persists a modified Repo to the datastore.
Update(repo *model.Repo) error
2014-06-04 21:25:38 +00:00
// Delete removes a Repo from the datastore.
Delete(repo *model.Repo) error
2014-06-04 21:25:38 +00:00
// List retrieves all repositories from the datastore.
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)
}
func NewRepoManager(db *sql.DB) RepoManager {
2014-06-04 21:25:38 +00:00
return &repoManager{db}
}
type repoManager struct {
*sql.DB
}
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 = ?"
var repo = model.Repo{}
2014-06-04 21:25:38 +00:00
var err = meddler.QueryRow(db, &repo, query, id)
return &repo, err
}
func (db *repoManager) FindName(remote, owner, name string) (*model.Repo, error) {
const query = "select * from repos where repo_host = ? and repo_owner = ? and repo_name = ?"
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
}
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 = ?)"
var repos []*model.Repo
2014-06-04 21:25:38 +00:00
err := meddler.QueryAll(db, &repos, query, user)
return repos, err
}
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)
}
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)
}
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
}