Skip to content

Commit

Permalink
Use NamedQueryContext to count rows
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio committed Nov 13, 2019
1 parent 22f1e85 commit e73db1b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
51 changes: 30 additions & 21 deletions things/postgres/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type channelRepository struct {
db Database
}

type dbConnection struct {
Channel string `db:"channel"`
Thing string `db:"thing"`
Owner string `db:"owner"`
}

// NewChannelRepository instantiates a PostgreSQL implementation of channel
// repository.
func NewChannelRepository(db Database) things.ChannelRepository {
Expand Down Expand Up @@ -149,37 +155,29 @@ func (cr channelRepository) RetrieveAll(ctx context.Context, owner string, offse
items = append(items, ch)
}

total := uint64(0)
cq := `SELECT COUNT(*) FROM channels WHERE owner = $1`
cq := `SELECT COUNT(*) FROM channels WHERE owner = :owner`
switch name {
case "":
switch metadata {
case nil:
cq = fmt.Sprintf("%s;", cq)
if err := cr.db.GetContext(ctx, &total, cq, owner); err != nil {
return things.ChannelsPage{}, err
}
default:
cq = fmt.Sprintf("%s %s", cq, "AND metadata @> $2;")
if err := cr.db.GetContext(ctx, &total, cq, owner, m); err != nil {
return things.ChannelsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND metadata @> :metadata;")
}
default:
switch metadata {
case nil:
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE $2;")
if err := cr.db.GetContext(ctx, &total, cq, owner, name); err != nil {
return things.ChannelsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE :name;")
default:
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE $2 AND metadata @> $3;")
if err := cr.db.GetContext(ctx, &total, cq, owner, name, m); err != nil {
return things.ChannelsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE :name AND metadata @> :metadata;")
}
}

total, err := cr.total(ctx, cq, params)
if err != nil {
return things.ChannelsPage{}, err
}

page := things.ChannelsPage{
Channels: items,
PageMetadata: things.PageMetadata{
Expand Down Expand Up @@ -450,8 +448,19 @@ func getMetadataQuery(m things.Metadata) ([]byte, string, error) {
return mb, mq, nil
}

type dbConnection struct {
Channel string `db:"channel"`
Thing string `db:"thing"`
Owner string `db:"owner"`
func (cr channelRepository) total(ctx context.Context, query string, params map[string]interface{}) (uint64, error) {
rows, err := cr.db.NamedQueryContext(ctx, query, params)
if err != nil {
return 0, err
}

total := uint64(0)
if rows.Next() {
err := rows.Scan(&total)
if err != nil {
return 0, err
}
}

return total, nil
}
43 changes: 26 additions & 17 deletions things/postgres/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,37 +218,29 @@ func (tr thingRepository) RetrieveAll(ctx context.Context, owner string, offset,
items = append(items, th)
}

total := uint64(0)
cq := `SELECT COUNT(*) FROM things WHERE owner = $1`
cq := `SELECT COUNT(*) FROM things WHERE owner = :owner`
switch name {
case "":
switch metadata {
case nil:
cq = fmt.Sprintf("%s;", cq)
if err := tr.db.GetContext(ctx, &total, cq, owner); err != nil {
return things.ThingsPage{}, err
}
default:
cq = fmt.Sprintf("%s %s", cq, "AND metadata @> $2;")
if err := tr.db.GetContext(ctx, &total, cq, owner, m); err != nil {
return things.ThingsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND metadata @> :metadata;")
}
default:
switch metadata {
case nil:
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE $2;")
if err := tr.db.GetContext(ctx, &total, cq, owner, name); err != nil {
return things.ThingsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE :name;")
default:
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE $2 AND metadata @> $3;")
if err := tr.db.GetContext(ctx, &total, cq, owner, name, m); err != nil {
return things.ThingsPage{}, err
}
cq = fmt.Sprintf("%s %s", cq, "AND name LIKE :name AND metadata @> :metadata;")
}
}

total, err := tr.total(ctx, cq, params)
if err != nil {
return things.ThingsPage{}, err
}

page := things.ThingsPage{
Things: items,
PageMetadata: things.PageMetadata{
Expand Down Expand Up @@ -376,3 +368,20 @@ func toThing(dbth dbThing) (things.Thing, error) {
Metadata: metadata,
}, nil
}

func (tr thingRepository) total(ctx context.Context, query string, params map[string]interface{}) (uint64, error) {
rows, err := tr.db.NamedQueryContext(ctx, query, params)
if err != nil {
return 0, err
}

total := uint64(0)
if rows.Next() {
err := rows.Scan(&total)
if err != nil {
return 0, err
}
}

return total, nil
}

0 comments on commit e73db1b

Please sign in to comment.