From 771f1cf6062b6df194b40c82e6703bddad9aff66 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 13 Jun 2015 17:37:36 -0700 Subject: [PATCH] migrated more files to generated sql --- pkg/store/builtin/agent.go | 45 ++----- pkg/store/builtin/agent_sql.go | 184 +++++++++++++++++++++++++++ pkg/store/builtin/blob.go | 42 +++--- pkg/store/builtin/blob_sql.go | 184 +++++++++++++++++++++++++++ pkg/store/builtin/build_test.go | 5 +- pkg/store/builtin/migrate/migrate.go | 4 +- 6 files changed, 412 insertions(+), 52 deletions(-) create mode 100644 pkg/store/builtin/agent_sql.go create mode 100644 pkg/store/builtin/blob_sql.go diff --git a/pkg/store/builtin/agent.go b/pkg/store/builtin/agent.go index 71232d52..615e3eb2 100644 --- a/pkg/store/builtin/agent.go +++ b/pkg/store/builtin/agent.go @@ -3,8 +3,7 @@ package builtin import ( "database/sql" - "github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" - common "github.com/drone/drone/pkg/types" + "github.com/drone/drone/pkg/types" ) type Agentstore struct { @@ -16,38 +15,22 @@ func NewAgentstore(db *sql.DB) *Agentstore { } // 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 +func (db *Agentstore) Agent(commit *types.Commit) (string, error) { + agent, err := getAgent(db, rebind(stmtAgentSelectAgentCommit), commit.ID) + if err != nil { + return "", err + } + return agent.Addr, nil } // 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) +func (db *Agentstore) SetAgent(commit *types.Commit, addr string) error { + agent := Agent{Addr: addr, CommitID: commit.ID} + return createAgent(db, rebind(stmtAgentInsert), &agent) } -type agent struct { - ID int64 `meddler:"agent_id,pk"` - Addr string `meddler:"agent_addr"` - CommitID int64 `meddler:"commit_id"` +type Agent struct { + ID int64 + Addr string + 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 = ?; -` diff --git a/pkg/store/builtin/agent_sql.go b/pkg/store/builtin/agent_sql.go new file mode 100644 index 00000000..c1977d1b --- /dev/null +++ b/pkg/store/builtin/agent_sql.go @@ -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); +` diff --git a/pkg/store/builtin/blob.go b/pkg/store/builtin/blob.go index 64b7121a..102a267a 100644 --- a/pkg/store/builtin/blob.go +++ b/pkg/store/builtin/blob.go @@ -2,33 +2,38 @@ package builtin import ( "bytes" + "database/sql" "io" "io/ioutil" - - "github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler" ) -type blob struct { - ID int64 `meddler:"blob_id,pk"` - Path string `meddler:"blob_path"` - Data string `meddler:"blob_data,gobgzip"` +type Blob struct { + ID int64 + Path string `sql:"unique:ux_blob_path"` + Data []byte } type Blobstore struct { - meddler.DB + *sql.DB } // Del removes an object from the blobstore. 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 } // Get retrieves an object from the blobstore. func (db *Blobstore) GetBlob(path string) ([]byte, error) { - var blob = blob{} - var err = meddler.QueryRow(db, &blob, rebind(blobQuery), path) - return []byte(blob.Data), err + blob, err := getBlob(db, rebind(stmtBlobSelectBlobPath), path) + if err != nil { + return nil, nil + } + return blob.Data, nil } // 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. func (db *Blobstore) SetBlob(path string, data []byte) error { - var blob = blob{} - meddler.QueryRow(db, &blob, rebind(blobQuery), path) + blob, _ := getBlob(db, rebind(stmtBlobSelectBlobPath), path) + if blob == nil { + blob = &Blob{} + } blob.Path = path - blob.Data = string(data) - return meddler.Save(db, blobTable, &blob) + blob.Data = data + if blob.ID == 0 { + return createBlob(db, rebind(stmtBlobInsert), blob) + } + return updateBlob(db, rebind(stmtBlobUpdate), blob) } // 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) } -func NewBlobstore(db meddler.DB) *Blobstore { +func NewBlobstore(db *sql.DB) *Blobstore { return &Blobstore{db} } diff --git a/pkg/store/builtin/blob_sql.go b/pkg/store/builtin/blob_sql.go new file mode 100644 index 00000000..8de131f6 --- /dev/null +++ b/pkg/store/builtin/blob_sql.go @@ -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); +` diff --git a/pkg/store/builtin/build_test.go b/pkg/store/builtin/build_test.go index 6cd6d865..8db23f4d 100644 --- a/pkg/store/builtin/build_test.go +++ b/pkg/store/builtin/build_test.go @@ -3,7 +3,6 @@ package builtin import ( "testing" - "github.com/bradrydzewski/drone/common" "github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin" "github.com/drone/drone/pkg/types" ) @@ -109,7 +108,7 @@ func TestBuildstore(t *testing.T) { //Add Commit. commit1 := types.Commit{ RepoID: 1, - State: common.StateSuccess, + State: types.StateSuccess, Ref: "refs/heads/master", Sha: "14710626f22791619d3b7e9ccf58b10374e5b76d", Builds: buildList, @@ -121,7 +120,7 @@ func TestBuildstore(t *testing.T) { g.Assert(err2 == nil).IsTrue() g.Assert(len(bldList)).Equal(3) g.Assert(bldList[0].Sequence).Equal(1) - g.Assert(bldList[0].State).Equal(common.StateSuccess) + g.Assert(bldList[0].State).Equal(types.StateSuccess) }) }) } diff --git a/pkg/store/builtin/migrate/migrate.go b/pkg/store/builtin/migrate/migrate.go index 7ab7b6bb..8d0f7b0e 100644 --- a/pkg/store/builtin/migrate/migrate.go +++ b/pkg/store/builtin/migrate/migrate.go @@ -211,8 +211,8 @@ CREATE TABLE IF NOT EXISTS blobs ( var agentTable = ` CREATE TABLE IF NOT EXISTS agents ( agent_id INTEGER PRIMARY KEY AUTOINCREMENT - ,commit_id INTEGER + ,agent_commit_id INTEGER ,agent_addr VARCHAR(2000) - ,UNIQUE(commit_id) + ,UNIQUE(agent_commit_id) ); `