2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler"
|
2015-05-18 22:47:13 +00:00
|
|
|
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.
|
2015-05-19 03:44:44 +00:00
|
|
|
func (db *Agentstore) Agent(commit *common.Commit) (string, error) {
|
2015-05-19 04:53:34 +00:00
|
|
|
var agent = new(agent)
|
2015-05-19 03:44:44 +00:00
|
|
|
var err = meddler.QueryRow(db, agent, rebind(agentQuery), commit.ID)
|
|
|
|
return agent.Addr, err
|
2015-05-18 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
// 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)
|
2015-05-18 22:47:13 +00:00
|
|
|
return meddler.Insert(db, agentTable, agent)
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
type agent struct {
|
|
|
|
ID int64 `meddler:"agent_id,pk"`
|
|
|
|
Addr string `meddler:"agent_addr"`
|
|
|
|
CommitID int64 `meddler:"commit_id"`
|
2015-05-18 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
// Build table name in database.
|
2015-05-18 22:47:13 +00:00
|
|
|
const agentTable = "agents"
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
const agentQuery = `
|
2015-05-18 22:47:13 +00:00
|
|
|
SELECT *
|
|
|
|
FROM agents
|
2015-05-19 03:44:44 +00:00
|
|
|
WHERE commit_id = ?
|
2015-05-18 22:47:13 +00:00
|
|
|
LIMIT 1;
|
|
|
|
`
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
const deleteAgentQuery = `
|
|
|
|
DELETE FROM agents
|
|
|
|
WHERE commit_id = ?;
|
2015-05-18 22:47:13 +00:00
|
|
|
`
|