migrated more files to generated sql

This commit is contained in:
Brad Rydzewski 2015-06-13 17:37:36 -07:00
parent dddc1dd84c
commit 771f1cf606
6 changed files with 412 additions and 52 deletions

View file

@ -3,8 +3,7 @@ package builtin
import ( import (
"database/sql" "database/sql"
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" "github.com/drone/drone/pkg/types"
common "github.com/drone/drone/pkg/types"
) )
type Agentstore struct { type Agentstore struct {
@ -16,38 +15,22 @@ func NewAgentstore(db *sql.DB) *Agentstore {
} }
// Agent returns an agent by ID. // Agent returns an agent by ID.
func (db *Agentstore) Agent(commit *common.Commit) (string, error) { func (db *Agentstore) Agent(commit *types.Commit) (string, error) {
var agent = new(agent) agent, err := getAgent(db, rebind(stmtAgentSelectAgentCommit), commit.ID)
var err = meddler.QueryRow(db, agent, rebind(agentQuery), commit.ID) if err != nil {
return agent.Addr, err return "", err
}
return agent.Addr, nil
} }
// SetAgent updates an agent in the datastore. // SetAgent updates an agent in the datastore.
func (db *Agentstore) SetAgent(commit *common.Commit, addr string) error { func (db *Agentstore) SetAgent(commit *types.Commit, addr string) error {
agent := &agent{} agent := Agent{Addr: addr, CommitID: commit.ID}
agent.Addr = addr return createAgent(db, rebind(stmtAgentInsert), &agent)
agent.CommitID = commit.ID
db.Exec(rebind(deleteAgentQuery), commit.ID)
return meddler.Insert(db, agentTable, agent)
} }
type agent struct { type Agent struct {
ID int64 `meddler:"agent_id,pk"` ID int64
Addr string `meddler:"agent_addr"` Addr string
CommitID int64 `meddler:"commit_id"` CommitID int64 `sql:"unique:ux_agent_commit"`
} }
// 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 = ?;
`

View file

@ -0,0 +1,184 @@
package builtin
// DO NOT EDIT
// code generated by go:generate
import (
"database/sql"
"encoding/json"
)
var _ = json.Marshal
// generic database interface, matching both *sql.Db and *sql.Tx
type agentDB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
func getAgent(db agentDB, query string, args ...interface{}) (*Agent, error) {
row := db.QueryRow(query, args...)
return scanAgent(row)
}
func getAgents(db agentDB, query string, args ...interface{}) ([]*Agent, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanAgents(rows)
}
func createAgent(db agentDB, query string, v *Agent) error {
var v0 string
var v1 int64
v0 = v.Addr
v1 = v.CommitID
res, err := db.Exec(query,
&v0,
&v1,
)
if err != nil {
return err
}
v.ID, err = res.LastInsertId()
return err
}
func updateAgent(db agentDB, query string, v *Agent) error {
var v0 int64
var v1 string
var v2 int64
v0 = v.ID
v1 = v.Addr
v2 = v.CommitID
_, err := db.Exec(query,
&v1,
&v2,
&v0,
)
return err
}
func scanAgent(row *sql.Row) (*Agent, error) {
var v0 int64
var v1 string
var v2 int64
err := row.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return nil, err
}
v := &Agent{}
v.ID = v0
v.Addr = v1
v.CommitID = v2
return v, nil
}
func scanAgents(rows *sql.Rows) ([]*Agent, error) {
var err error
var vv []*Agent
for rows.Next() {
var v0 int64
var v1 string
var v2 int64
err = rows.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return vv, err
}
v := &Agent{}
v.ID = v0
v.Addr = v1
v.CommitID = v2
vv = append(vv, v)
}
return vv, rows.Err()
}
const stmtAgentSelectList = `
SELECT
agent_id
,agent_addr
,agent_commit_id
FROM agents
`
const stmtAgentSelectRange = `
SELECT
agent_id
,agent_addr
,agent_commit_id
FROM agents
LIMIT ? OFFSET ?
`
const stmtAgentSelect = `
SELECT
agent_id
,agent_addr
,agent_commit_id
FROM agents
WHERE agent_id = ?
`
const stmtAgentSelectAgentCommit = `
SELECT
agent_id
,agent_addr
,agent_commit_id
FROM agents
WHERE agent_commit_id = ?
`
const stmtAgentSelectCount = `
SELECT count(1)
FROM agents
`
const stmtAgentInsert = `
INSERT INTO agents (
agent_addr
,agent_commit_id
) VALUES (?,?);
`
const stmtAgentUpdate = `
UPDATE agents SET
agent_addr = ?
,agent_commit_id = ?
WHERE agent_id = ?
`
const stmtAgentDelete = `
DELETE FROM agents
WHERE agent_id = ?
`
const stmtAgentTable = `
CREATE TABLE IF NOT EXISTS agents (
agent_id INTEGER PRIMARY KEY AUTOINCREMENT
,agent_addr VARCHAR
,agent_commit_idINTEGER
);
`
const stmtAgentAgentCommitIndex = `
CREATE UNIQUE INDEX IF NOT EXISTS ux_agent_commit ON agents (agent_commit_id);
`

View file

@ -2,33 +2,38 @@ package builtin
import ( import (
"bytes" "bytes"
"database/sql"
"io" "io"
"io/ioutil" "io/ioutil"
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler"
) )
type blob struct { type Blob struct {
ID int64 `meddler:"blob_id,pk"` ID int64
Path string `meddler:"blob_path"` Path string `sql:"unique:ux_blob_path"`
Data string `meddler:"blob_data,gobgzip"` Data []byte
} }
type Blobstore struct { type Blobstore struct {
meddler.DB *sql.DB
} }
// Del removes an object from the blobstore. // Del removes an object from the blobstore.
func (db *Blobstore) DelBlob(path string) error { func (db *Blobstore) DelBlob(path string) error {
var _, err = db.Exec(rebind(blobDeleteStmt), path) blob, _ := getBlob(db, rebind(stmtBlobSelectBlobPath), path)
if blob == nil {
return nil
}
_, err := db.Exec(rebind(stmtBlobDelete), blob.ID)
return err return err
} }
// Get retrieves an object from the blobstore. // Get retrieves an object from the blobstore.
func (db *Blobstore) GetBlob(path string) ([]byte, error) { func (db *Blobstore) GetBlob(path string) ([]byte, error) {
var blob = blob{} blob, err := getBlob(db, rebind(stmtBlobSelectBlobPath), path)
var err = meddler.QueryRow(db, &blob, rebind(blobQuery), path) if err != nil {
return []byte(blob.Data), err return nil, nil
}
return blob.Data, nil
} }
// GetBlobReader retrieves an object from the blobstore. // GetBlobReader retrieves an object from the blobstore.
@ -42,11 +47,16 @@ func (db *Blobstore) GetBlobReader(path string) (io.ReadCloser, error) {
// SetBlob inserts an object into the blobstore. // SetBlob inserts an object into the blobstore.
func (db *Blobstore) SetBlob(path string, data []byte) error { func (db *Blobstore) SetBlob(path string, data []byte) error {
var blob = blob{} blob, _ := getBlob(db, rebind(stmtBlobSelectBlobPath), path)
meddler.QueryRow(db, &blob, rebind(blobQuery), path) if blob == nil {
blob = &Blob{}
}
blob.Path = path blob.Path = path
blob.Data = string(data) blob.Data = data
return meddler.Save(db, blobTable, &blob) if blob.ID == 0 {
return createBlob(db, rebind(stmtBlobInsert), blob)
}
return updateBlob(db, rebind(stmtBlobUpdate), blob)
} }
// SetBlobReader inserts an object into the blobstore by // SetBlobReader inserts an object into the blobstore by
@ -56,7 +66,7 @@ func (db *Blobstore) SetBlobReader(path string, r io.Reader) error {
return db.SetBlob(path, data) return db.SetBlob(path, data)
} }
func NewBlobstore(db meddler.DB) *Blobstore { func NewBlobstore(db *sql.DB) *Blobstore {
return &Blobstore{db} return &Blobstore{db}
} }

View file

@ -0,0 +1,184 @@
package builtin
// DO NOT EDIT
// code generated by go:generate
import (
"database/sql"
"encoding/json"
)
var _ = json.Marshal
// generic database interface, matching both *sql.Db and *sql.Tx
type blobDB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
func getBlob(db blobDB, query string, args ...interface{}) (*Blob, error) {
row := db.QueryRow(query, args...)
return scanBlob(row)
}
func getBlobs(db blobDB, query string, args ...interface{}) ([]*Blob, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanBlobs(rows)
}
func createBlob(db blobDB, query string, v *Blob) error {
var v0 string
var v1 []byte
v0 = v.Path
v1 = v.Data
res, err := db.Exec(query,
&v0,
&v1,
)
if err != nil {
return err
}
v.ID, err = res.LastInsertId()
return err
}
func updateBlob(db blobDB, query string, v *Blob) error {
var v0 int64
var v1 string
var v2 []byte
v0 = v.ID
v1 = v.Path
v2 = v.Data
_, err := db.Exec(query,
&v1,
&v2,
&v0,
)
return err
}
func scanBlob(row *sql.Row) (*Blob, error) {
var v0 int64
var v1 string
var v2 []byte
err := row.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return nil, err
}
v := &Blob{}
v.ID = v0
v.Path = v1
v.Data = v2
return v, nil
}
func scanBlobs(rows *sql.Rows) ([]*Blob, error) {
var err error
var vv []*Blob
for rows.Next() {
var v0 int64
var v1 string
var v2 []byte
err = rows.Scan(
&v0,
&v1,
&v2,
)
if err != nil {
return vv, err
}
v := &Blob{}
v.ID = v0
v.Path = v1
v.Data = v2
vv = append(vv, v)
}
return vv, rows.Err()
}
const stmtBlobSelectList = `
SELECT
blob_id
,blob_path
,blob_data
FROM blobs
`
const stmtBlobSelectRange = `
SELECT
blob_id
,blob_path
,blob_data
FROM blobs
LIMIT ? OFFSET ?
`
const stmtBlobSelect = `
SELECT
blob_id
,blob_path
,blob_data
FROM blobs
WHERE blob_id = ?
`
const stmtBlobSelectBlobPath = `
SELECT
blob_id
,blob_path
,blob_data
FROM blobs
WHERE blob_path = ?
`
const stmtBlobSelectCount = `
SELECT count(1)
FROM blobs
`
const stmtBlobInsert = `
INSERT INTO blobs (
blob_path
,blob_data
) VALUES (?,?);
`
const stmtBlobUpdate = `
UPDATE blobs SET
blob_path = ?
,blob_data = ?
WHERE blob_id = ?
`
const stmtBlobDelete = `
DELETE FROM blobs
WHERE blob_id = ?
`
const stmtBlobTable = `
CREATE TABLE IF NOT EXISTS blobs (
blob_id INTEGER PRIMARY KEY AUTOINCREMENT
,blob_path VARCHAR
,blob_data BLOB
);
`
const stmtBlobBlobPathIndex = `
CREATE UNIQUE INDEX IF NOT EXISTS ux_blob_path ON blobs (blob_path);
`

View file

@ -3,7 +3,6 @@ package builtin
import ( import (
"testing" "testing"
"github.com/bradrydzewski/drone/common"
"github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin" "github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin"
"github.com/drone/drone/pkg/types" "github.com/drone/drone/pkg/types"
) )
@ -109,7 +108,7 @@ func TestBuildstore(t *testing.T) {
//Add Commit. //Add Commit.
commit1 := types.Commit{ commit1 := types.Commit{
RepoID: 1, RepoID: 1,
State: common.StateSuccess, State: types.StateSuccess,
Ref: "refs/heads/master", Ref: "refs/heads/master",
Sha: "14710626f22791619d3b7e9ccf58b10374e5b76d", Sha: "14710626f22791619d3b7e9ccf58b10374e5b76d",
Builds: buildList, Builds: buildList,
@ -121,7 +120,7 @@ func TestBuildstore(t *testing.T) {
g.Assert(err2 == nil).IsTrue() g.Assert(err2 == nil).IsTrue()
g.Assert(len(bldList)).Equal(3) g.Assert(len(bldList)).Equal(3)
g.Assert(bldList[0].Sequence).Equal(1) g.Assert(bldList[0].Sequence).Equal(1)
g.Assert(bldList[0].State).Equal(common.StateSuccess) g.Assert(bldList[0].State).Equal(types.StateSuccess)
}) })
}) })
} }

View file

@ -211,8 +211,8 @@ CREATE TABLE IF NOT EXISTS blobs (
var agentTable = ` var agentTable = `
CREATE TABLE IF NOT EXISTS agents ( CREATE TABLE IF NOT EXISTS agents (
agent_id INTEGER PRIMARY KEY AUTOINCREMENT agent_id INTEGER PRIMARY KEY AUTOINCREMENT
,commit_id INTEGER ,agent_commit_id INTEGER
,agent_addr VARCHAR(2000) ,agent_addr VARCHAR(2000)
,UNIQUE(commit_id) ,UNIQUE(agent_commit_id)
); );
` `