woodpecker/pkg/store/builtin/build.go

63 lines
1.4 KiB
Go
Raw Normal View History

package builtin
import (
"time"
2015-04-29 22:26:22 +00:00
2015-05-17 20:51:42 +00:00
common "github.com/drone/drone/pkg/types"
"github.com/russross/meddler"
)
type Buildstore struct {
meddler.DB
}
func NewBuildstore(db meddler.DB) *Buildstore {
return &Buildstore{db}
}
// 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
}
// 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-25 00:19:48 +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
}
// 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;
`