harness-drone/pkg/store/builtin/agent.go
2015-05-22 11:37:40 -07:00

53 lines
1.1 KiB
Go

package builtin
import (
"database/sql"
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler"
common "github.com/drone/drone/pkg/types"
)
type Agentstore struct {
*sql.DB
}
func NewAgentstore(db *sql.DB) *Agentstore {
return &Agentstore{db}
}
// Agent returns an agent by ID.
func (db *Agentstore) Agent(commit *common.Commit) (string, error) {
var agent = new(agent)
var err = meddler.QueryRow(db, agent, rebind(agentQuery), commit.ID)
return agent.Addr, err
}
// SetAgent updates an agent in the datastore.
func (db *Agentstore) SetAgent(commit *common.Commit, addr string) error {
agent := &agent{}
agent.Addr = addr
agent.CommitID = commit.ID
db.Exec(rebind(deleteAgentQuery), commit.ID)
return meddler.Insert(db, agentTable, agent)
}
type agent struct {
ID int64 `meddler:"agent_id,pk"`
Addr string `meddler:"agent_addr"`
CommitID int64 `meddler:"commit_id"`
}
// Build table name in database.
const agentTable = "agents"
const agentQuery = `
SELECT *
FROM agents
WHERE commit_id = ?
LIMIT 1;
`
const deleteAgentQuery = `
DELETE FROM agents
WHERE commit_id = ?;
`