2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-11 07:45:31 +00:00
|
|
|
"database/sql"
|
2015-04-07 08:20:55 +00:00
|
|
|
"time"
|
|
|
|
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-05-11 07:45:31 +00:00
|
|
|
"github.com/russross/meddler"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
type Repostore struct {
|
|
|
|
*sql.DB
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func NewRepostore(db *sql.DB) *Repostore {
|
|
|
|
return &Repostore{db}
|
2015-04-15 05:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Repo retrieves a specific repo from the
|
|
|
|
// datastore for the given ID.
|
|
|
|
func (db *Repostore) Repo(id int64) (*common.Repo, error) {
|
|
|
|
var repo = new(common.Repo)
|
|
|
|
var err = meddler.Load(db, repoTable, repo, id)
|
|
|
|
return repo, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// RepoName retrieves a repo from the datastore
|
|
|
|
// for the specified name.
|
|
|
|
func (db *Repostore) RepoName(owner, name string) (*common.Repo, error) {
|
|
|
|
var repo = new(common.Repo)
|
|
|
|
var err = meddler.QueryRow(db, repo, rebind(repoNameQuery), owner, name)
|
|
|
|
return repo, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// RepoList retrieves a list of all repos from
|
|
|
|
// the datastore accessible by the given user ID.
|
|
|
|
func (db *Repostore) RepoList(user *common.User) ([]*common.Repo, error) {
|
|
|
|
var repos []*common.Repo
|
|
|
|
var err = meddler.QueryAll(db, &repos, rebind(repoListQuery), user.ID)
|
|
|
|
return repos, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// // RepoKeys retrieves a set of repository keys from
|
|
|
|
// // the datastore for the specified name.
|
|
|
|
// func (db *Repostore) RepoKeypair(repo *common.Repo) (*common.Keypair, error) {
|
|
|
|
// var keypair = new(common.Keypair)
|
|
|
|
// var err = meddler.QueryRow(db, keypair, rebind(repoKeysQuery), repo.ID)
|
|
|
|
// return keypair, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // RepoParams retrieves a set of repository params from
|
|
|
|
// // the datastore for the specified name.
|
|
|
|
// func (db *Repostore) RepoParams(repo *common.Repo) (*common.Params, error) {
|
|
|
|
// var params = new(common.Params)
|
|
|
|
// var err = meddler.QueryRow(db, params, rebind(repoParamsQuery), repo.ID)
|
|
|
|
// return params, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// AddRepo inserts a repo in the datastore.
|
|
|
|
func (db *Repostore) AddRepo(repo *common.Repo) error {
|
2015-04-07 08:20:55 +00:00
|
|
|
repo.Created = time.Now().UTC().Unix()
|
|
|
|
repo.Updated = time.Now().UTC().Unix()
|
2015-05-11 07:45:31 +00:00
|
|
|
return meddler.Insert(db, repoTable, repo)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SetRepo updates a repo in the datastore.
|
|
|
|
func (db *Repostore) SetRepo(repo *common.Repo) error {
|
|
|
|
repo.Updated = time.Now().UTC().Unix()
|
|
|
|
return meddler.Update(db, repoTable, repo)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// // SetRepoKeypair upserts a keypair in the datastore.
|
|
|
|
// func (db *Repostore) SetRepoKeypair(keys *common.Keypair) error {
|
|
|
|
// return meddler.Save(db, repoKeyTable, keys)
|
|
|
|
// }
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// // SetRepoKeypair upserts a param set in the datastore.
|
|
|
|
// func (db *Repostore) SetRepoParams(params *common.Params) error {
|
|
|
|
// return meddler.Save(db, repoParamTable, params)
|
|
|
|
// }
|
2015-04-20 22:48:44 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// DelRepo removes the repo from the datastore.
|
|
|
|
func (db *Repostore) DelRepo(repo *common.Repo) error {
|
|
|
|
var _, err = db.Exec(rebind(repoDeleteStmt), repo.ID)
|
|
|
|
return err
|
2015-04-14 04:39:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Repo table names in database.
|
|
|
|
const (
|
|
|
|
repoTable = "repos"
|
|
|
|
repoKeyTable = "repo_keys"
|
|
|
|
repoParamTable = "repo_params"
|
|
|
|
)
|
2015-04-14 04:39:05 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SQL statement to retrieve a Repo by name.
|
|
|
|
const repoNameQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM repos
|
|
|
|
WHERE repo_owner = ?
|
|
|
|
AND repo_name = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to retrieve a list of Repos
|
|
|
|
// with permissions for the given User ID.
|
|
|
|
const repoListQuery = `
|
|
|
|
SELECT r.*
|
|
|
|
FROM
|
|
|
|
repos r
|
|
|
|
,stars s
|
|
|
|
WHERE r.repo_id = s.repo_id
|
|
|
|
AND s.user_id = ?
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to retrieve a keypair for
|
|
|
|
// a Repository.
|
|
|
|
const repoKeysQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM repo_keys
|
|
|
|
WHERE repo_id = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to retrieve a keypair for
|
|
|
|
// a Repository.
|
|
|
|
const repoParamsQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM repo_params
|
|
|
|
WHERE repo_id = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to delete a User by ID.
|
|
|
|
const (
|
|
|
|
repoDeleteStmt = `DELETE FROM repos WHERE repo_id = ?`
|
|
|
|
repoKeypairDeleteStmt = `DELETE FROM repo_params WHERE repo_id = ?`
|
|
|
|
repoParamsDeleteStmt = `DELETE FROM repo_keys WHERE repo_id = ?`
|
|
|
|
)
|