github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/repos.go (about) 1 package database 2 3 import ( 4 "time" 5 6 . "github.com/drone/drone/pkg/model" 7 "github.com/russross/meddler" 8 ) 9 10 // Name of the Repos table in the database 11 const repoTable = "repos" 12 13 // SQL Queries to retrieve a list of all repos belonging to a User. 14 const repoStmt = ` 15 SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password, 16 public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id 17 FROM repos 18 WHERE user_id = ? AND team_id = 0 19 ORDER BY slug ASC 20 ` 21 22 // SQL Queries to retrieve a list of all repos belonging to a Team. 23 const repoTeamStmt = ` 24 SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password, 25 public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id 26 FROM repos 27 WHERE team_id = ? 28 ORDER BY slug ASC 29 ` 30 31 // SQL Queries to retrieve a repo by id. 32 const repoFindStmt = ` 33 SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password, 34 public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id 35 FROM repos 36 WHERE id = ? 37 ` 38 39 // SQL Queries to retrieve a repo by name. 40 const repoFindSlugStmt = ` 41 SELECT id, slug, host, owner, name, private, disabled, disabled_pr, scm, url, username, password, 42 public_key, private_key, params, timeout, privileged, created, updated, user_id, team_id 43 FROM repos 44 WHERE slug = ? 45 ` 46 47 // Returns the Repo with the given ID. 48 func GetRepo(id int64) (*Repo, error) { 49 repo := Repo{} 50 err := meddler.QueryRow(db, &repo, repoFindStmt, id) 51 return &repo, err 52 } 53 54 // Returns the Repo with the given slug. 55 func GetRepoSlug(slug string) (*Repo, error) { 56 repo := Repo{} 57 err := meddler.QueryRow(db, &repo, repoFindSlugStmt, slug) 58 return &repo, err 59 } 60 61 // Creates a new Repository. 62 func SaveRepo(repo *Repo) error { 63 if repo.ID == 0 { 64 repo.Created = time.Now().UTC() 65 } 66 repo.Updated = time.Now().UTC() 67 return meddler.Save(db, repoTable, repo) 68 } 69 70 // Deletes an existing Repository. 71 // TODO need to delete builds too. 72 func DeleteRepo(id int64) error { 73 _, err := db.Exec("DELETE FROM repos WHERE id = ?", id) 74 db.Exec("DELETE FROM commits WHERE repo_id = ?", id) 75 return err 76 } 77 78 // Returns a list of all Repos associated 79 // with the specified User ID. 80 func ListRepos(id int64) ([]*Repo, error) { 81 var repos []*Repo 82 err := meddler.QueryAll(db, &repos, repoStmt, id) 83 return repos, err 84 } 85 86 // Returns a list of all Repos associated 87 // with the specified Team ID. 88 func ListReposTeam(id int64) ([]*Repo, error) { 89 var repos []*Repo 90 err := meddler.QueryAll(db, &repos, repoTeamStmt, id) 91 return repos, err 92 }