2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-23 19:39:36 +00:00
|
|
|
"time"
|
2015-04-29 22:26:22 +00:00
|
|
|
|
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 Buildstore struct {
|
|
|
|
meddler.DB
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func NewBuildstore(db meddler.DB) *Buildstore {
|
|
|
|
return &Buildstore{db}
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Build returns a build by ID.
|
|
|
|
func (db *Buildstore) Build(id int64) (*common.Build, error) {
|
|
|
|
var build = new(common.Build)
|
|
|
|
var err = meddler.Load(db, buildTable, build, id)
|
2015-04-11 05:22:55 +00:00
|
|
|
return build, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// BuildSeq returns a build by sequence number.
|
|
|
|
func (db *Buildstore) BuildSeq(commit *common.Commit, seq int) (*common.Build, error) {
|
|
|
|
var build = new(common.Build)
|
|
|
|
var err = meddler.QueryRow(db, build, rebind(buildNumberQuery), commit.ID, seq)
|
|
|
|
return build, err
|
2015-04-16 06:45:24 +00:00
|
|
|
}
|
2015-04-25 00:19:48 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// BuildList returns a list of all commit builds
|
|
|
|
func (db *Buildstore) BuildList(commit *common.Commit) ([]*common.Build, error) {
|
|
|
|
var builds []*common.Build
|
|
|
|
var err = meddler.QueryAll(db, &builds, rebind(buildListQuery), commit.ID)
|
|
|
|
return builds, err
|
2015-04-29 22:26:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// SetBuild updates an existing build.
|
|
|
|
func (db *Buildstore) SetBuild(build *common.Build) error {
|
|
|
|
build.Updated = time.Now().UTC().Unix()
|
|
|
|
return meddler.Update(db, buildTable, build)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build table name in database.
|
|
|
|
const buildTable = "builds"
|
|
|
|
|
|
|
|
// SQL query to retrieve a token by label.
|
|
|
|
const buildListQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE commit_id = ?
|
|
|
|
ORDER BY build_seq ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
const buildNumberQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM builds
|
|
|
|
WHERE commit_id = ?
|
|
|
|
AND build_seq = ?
|
|
|
|
LIMIT 1;
|
|
|
|
`
|