Skip to content

Commit

Permalink
Simplify the routine provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlin Ralchev committed May 16, 2018
1 parent ada6b6e commit bea6df1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 300 deletions.
42 changes: 0 additions & 42 deletions fake/Querier.go
Expand Up @@ -28,14 +28,6 @@ type Querier struct {
queryRowReturns struct {
result1 *sql.Row
}
RebindStub func(query string) string
rebindMutex sync.RWMutex
rebindArgsForCall []struct {
query string
}
rebindReturns struct {
result1 string
}
CloseStub func() error
closeMutex sync.RWMutex
closeArgsForCall []struct{}
Expand Down Expand Up @@ -113,38 +105,6 @@ func (fake *Querier) QueryRowReturns(result1 *sql.Row) {
}{result1}
}

func (fake *Querier) Rebind(query string) string {
fake.rebindMutex.Lock()
fake.rebindArgsForCall = append(fake.rebindArgsForCall, struct {
query string
}{query})
fake.recordInvocation("Rebind", []interface{}{query})
fake.rebindMutex.Unlock()
if fake.RebindStub != nil {
return fake.RebindStub(query)
}
return fake.rebindReturns.result1
}

func (fake *Querier) RebindCallCount() int {
fake.rebindMutex.RLock()
defer fake.rebindMutex.RUnlock()
return len(fake.rebindArgsForCall)
}

func (fake *Querier) RebindArgsForCall(i int) string {
fake.rebindMutex.RLock()
defer fake.rebindMutex.RUnlock()
return fake.rebindArgsForCall[i].query
}

func (fake *Querier) RebindReturns(result1 string) {
fake.RebindStub = nil
fake.rebindReturns = struct {
result1 string
}{result1}
}

func (fake *Querier) Close() error {
fake.closeMutex.Lock()
fake.closeArgsForCall = append(fake.closeArgsForCall, struct{}{})
Expand Down Expand Up @@ -176,8 +136,6 @@ func (fake *Querier) Invocations() map[string][][]interface{} {
defer fake.queryMutex.RUnlock()
fake.queryRowMutex.RLock()
defer fake.queryRowMutex.RUnlock()
fake.rebindMutex.RLock()
defer fake.rebindMutex.RUnlock()
fake.closeMutex.RLock()
defer fake.closeMutex.RUnlock()
return fake.invocations
Expand Down
6 changes: 0 additions & 6 deletions sqlexec/model.go
Expand Up @@ -24,9 +24,3 @@ type P = map[string]Param

// FileSystem provides with primitives to work with the underlying file system
type FileSystem = parcello.FileSystem

// Query represents an SQL Query
type Query interface {
// NamedQuery prepares query for execution
NamedQuery() (string, map[string]interface{})
}
27 changes: 5 additions & 22 deletions sqlexec/provider.go
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"strings"
"sync"

"github.com/jmoiron/sqlx"
)

const every = "sql"
Expand Down Expand Up @@ -85,34 +87,15 @@ func (p *Provider) ReadFrom(r io.Reader) (int64, error) {

// Query returns a query statement for given name and parameters. The operation can
// err if the command cannot be found.
func (p *Provider) Query(name string, params ...Param) (Query, error) {
p.mu.RLock()
defer p.mu.RUnlock()

if query, ok := p.repository[name]; ok {
return &Stmt{
query: query,
params: params,
}, nil
}

return nil, nonExistQueryErr(name)
}

// NamedQuery returns a query statement for given name and parameters. The operation can
// err if the command cannot be found.
func (p *Provider) NamedQuery(name string, param Param) (Query, error) {
func (p *Provider) Query(name string) (string, error) {
p.mu.RLock()
defer p.mu.RUnlock()

if query, ok := p.repository[name]; ok {
return &NamedStmt{
query: query,
param: param,
}, nil
return sqlx.Rebind(sqlx.BindType(p.DriverName), query), nil
}

return nil, nonExistQueryErr(name)
return "", nonExistQueryErr(name)
}

// Filter returns true if the file can be processed for the current driver
Expand Down
68 changes: 8 additions & 60 deletions sqlexec/provider_test.go
Expand Up @@ -35,10 +35,8 @@ var _ = Describe("Provider", func() {
Expect(n).To(Equal(int64(1)))
Expect(err).To(Succeed())

cmd, err := provider.Query("up")
query, err := provider.Query("up")
Expect(err).To(BeNil())

query, _ := cmd.NamedQuery()
Expect(query).To(Equal("SELECT * FROM users;"))
})

Expand Down Expand Up @@ -128,7 +126,7 @@ var _ = Describe("Provider", func() {
It("does not load the driver", func() {
Expect(provider.ReadDir(fileSystem)).To(Succeed())
cmd, err := provider.Query("up")
Expect(cmd).To(BeNil())
Expect(cmd).To(BeEmpty())
Expect(err).To(MatchError("query 'up' not found"))
})
})
Expand All @@ -150,7 +148,7 @@ var _ = Describe("Provider", func() {
Expect(provider.ReadDir(fileSystem)).To(Succeed())

cmd, err := provider.Query("up")
Expect(cmd).To(BeNil())
Expect(cmd).To(BeEmpty())
Expect(err).To(MatchError("query 'up' not found"))
})

Expand Down Expand Up @@ -204,22 +202,8 @@ var _ = Describe("Provider", func() {
})

It("returns a command", func() {
stmt, err := provider.Query("up")
Expect(err).To(BeNil())
Expect(stmt).NotTo(BeNil())

query, params := stmt.NamedQuery()
Expect(params).To(BeEmpty())
Expect(query).To(Equal("SELECT * FROM users"))
})

It("returns a named command", func() {
stmt, err := provider.NamedQuery("up", sqlexec.P{"id": 1})
query, err := provider.Query("up")
Expect(err).To(BeNil())
Expect(stmt).NotTo(BeNil())

query, params := stmt.NamedQuery()
Expect(params).To(HaveKeyWithValue("id", 1))
Expect(query).To(Equal("SELECT * FROM users"))
})

Expand All @@ -232,42 +216,14 @@ var _ = Describe("Provider", func() {
n, err := provider.ReadFrom(buffer)
Expect(n).To(Equal(int64(1)))
Expect(err).To(Succeed())
})

It("returns a command with params", func() {
stmt, err := provider.Query("show-users", 1)
Expect(err).To(BeNil())
Expect(stmt).NotTo(BeNil())

query, params := stmt.NamedQuery()
Expect(query).To(Equal("SELECT * FROM users WHERE id = :arg0"))
Expect(params).To(HaveKeyWithValue("arg0", 1))
})
})

Context("when the named command has arguments", func() {
BeforeEach(func() {
buffer := bytes.NewBufferString("-- name: show-users")
fmt.Fprintln(buffer)
fmt.Fprintln(buffer, "SELECT * FROM users WHERE id_pk = :id_pk")

n, err := provider.ReadFrom(buffer)
Expect(n).To(Equal(int64(1)))
Expect(err).To(Succeed())
provider.DriverName = "ora"
})

It("returns a command with params", func() {
type param struct {
ID int `db:"id_pk"`
}

stmt, err := provider.NamedQuery("show-users", param{ID: 1})
query, err := provider.Query("show-users")
Expect(err).To(BeNil())
Expect(stmt).NotTo(BeNil())

query, params := stmt.NamedQuery()
Expect(query).To(Equal("SELECT * FROM users WHERE id_pk = :id_pk"))
Expect(params).To(HaveKeyWithValue("id_pk", 1))
Expect(query).To(Equal("SELECT * FROM users WHERE id = :arg1"))
})
})

Expand All @@ -276,15 +232,7 @@ var _ = Describe("Provider", func() {
It("returns a error", func() {
cmd, err := provider.Query("down")
Expect(err).To(MatchError("query 'down' not found"))
Expect(cmd).To(BeNil())
})
})

Describe("NamedCmd", func() {
It("returns a error", func() {
cmd, err := provider.NamedQuery("down", sqlexec.P{"id": 1})
Expect(err).To(MatchError("query 'down' not found"))
Expect(cmd).To(BeNil())
Expect(cmd).To(BeEmpty())
})
})
})
Expand Down
10 changes: 3 additions & 7 deletions sqlexec/runner.go
Expand Up @@ -18,14 +18,12 @@ func (r *Runner) Run(name string, args ...Param) (*Rows, error) {
return nil, err
}

cmd, err := provider.Query(name, args...)
query, err := provider.Query(name)
if err != nil {
return nil, err
}

query, params := cmd.NamedQuery()

stmt, err := r.DB.PrepareNamed(query)
stmt, err := r.DB.Preparex(query)
if err != nil {
return nil, err
}
Expand All @@ -36,7 +34,5 @@ func (r *Runner) Run(name string, args ...Param) (*Rows, error) {
}
}()

var rows *Rows
rows, err = stmt.Queryx(params)
return rows, err
return stmt.Queryx(args...)
}
105 changes: 0 additions & 105 deletions sqlexec/stmt.go

This file was deleted.

0 comments on commit bea6df1

Please sign in to comment.