2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2015-10-22 01:39:43 +02:00
|
|
|
"fmt"
|
2015-10-22 01:14:02 +02:00
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2017-05-03 23:25:33 +02:00
|
|
|
"github.com/drone/drone/store/datastore/sql"
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetRepo(id int64) (*model.Repo, error) {
|
2015-10-22 01:14:02 +02:00
|
|
|
var repo = new(model.Repo)
|
|
|
|
var err = meddler.Load(db, repoTable, repo, id)
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetRepoName(name string) (*model.Repo, error) {
|
2015-10-22 01:14:02 +02:00
|
|
|
var repo = new(model.Repo)
|
|
|
|
var err = meddler.QueryRow(db, repo, rebind(repoNameQuery), name)
|
|
|
|
return repo, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) GetRepoListOf(listof []*model.RepoLite) ([]*model.Repo, error) {
|
2015-10-22 01:39:43 +02:00
|
|
|
var (
|
|
|
|
repos []*model.Repo
|
|
|
|
args []interface{}
|
|
|
|
stmt string
|
2016-07-06 15:02:36 +02:00
|
|
|
err error
|
2015-10-22 01:39:43 +02:00
|
|
|
)
|
|
|
|
switch meddler.Default {
|
|
|
|
case meddler.PostgreSQL:
|
2016-09-02 13:32:17 +02:00
|
|
|
stmt, args = toListPostgres(listof)
|
2015-10-22 01:39:43 +02:00
|
|
|
default:
|
|
|
|
stmt, args = toList(listof)
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
2016-07-06 15:02:36 +02:00
|
|
|
if len(args) > 0 {
|
|
|
|
err = meddler.QueryAll(db, &repos, fmt.Sprintf(repoListOfQuery, stmt), args...)
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:25:33 +02:00
|
|
|
func (db *datastore) GetRepoCount() (count int, err error) {
|
2017-05-04 02:02:08 +02:00
|
|
|
err = db.QueryRow(
|
|
|
|
sql.Lookup(db.driver, "count-repos"),
|
|
|
|
).Scan(&count)
|
2017-05-03 23:25:33 +02:00
|
|
|
return
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) CreateRepo(repo *model.Repo) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
return meddler.Insert(db, repoTable, repo)
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) UpdateRepo(repo *model.Repo) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
return meddler.Update(db, repoTable, repo)
|
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func (db *datastore) DeleteRepo(repo *model.Repo) error {
|
2015-10-22 01:14:02 +02:00
|
|
|
var _, err = db.Exec(rebind(repoDeleteStmt), repo.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const repoTable = "repos"
|
|
|
|
|
|
|
|
const repoNameQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM repos
|
|
|
|
WHERE repo_full_name = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
2015-10-22 01:39:43 +02:00
|
|
|
const repoListOfQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM repos
|
2015-10-22 02:17:29 +02:00
|
|
|
WHERE repo_full_name IN (%s)
|
2015-10-22 01:39:43 +02:00
|
|
|
ORDER BY repo_name
|
|
|
|
`
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
const repoCountQuery = `
|
|
|
|
SELECT COUNT(*) FROM repos
|
|
|
|
`
|
|
|
|
|
|
|
|
const repoDeleteStmt = `
|
|
|
|
DELETE FROM repos
|
|
|
|
WHERE repo_id = ?
|
|
|
|
`
|