Skip to content

Commit

Permalink
Merge pull request #3 from networkteam/jsonb_build_object
Browse files Browse the repository at this point in the history
added jsonb_build_object
  • Loading branch information
hlubek authored May 9, 2023
2 parents ea2a941 + 7bc3a27 commit 31b2147
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
20 changes: 14 additions & 6 deletions builder/json_build_object.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package builder

func JsonBuildObject() JsonBuildObjectBuilder {
func JsonBuildObject(isJsonB bool) JsonBuildObjectBuilder {
return JsonBuildObjectBuilder{
props: newImmutableSliceMap[string, Exp](),
isJsonB: isJsonB,
props: newImmutableSliceMap[string, Exp](),
}
}

type JsonBuildObjectBuilder struct {
props immutableSliceMap[string, Exp]
isJsonB bool
props immutableSliceMap[string, Exp]
}

var _ Exp = JsonBuildObjectBuilder{}
Expand All @@ -16,7 +18,11 @@ func (b JsonBuildObjectBuilder) IsExp() {}
func (b JsonBuildObjectBuilder) NoParensExp() {}

func (b JsonBuildObjectBuilder) WriteSQL(sb *SQLBuilder) {
sb.WriteString("json_build_object(")
if b.isJsonB {
sb.WriteString("jsonb_build_object(")
} else {
sb.WriteString("json_build_object(")
}

i := 0
for _, entry := range b.props {
Expand All @@ -36,7 +42,8 @@ func (b JsonBuildObjectBuilder) WriteSQL(sb *SQLBuilder) {
func (b JsonBuildObjectBuilder) Prop(key string, value Exp) JsonBuildObjectBuilder {
newProps := b.props.Set(key, value)
return JsonBuildObjectBuilder{
props: newProps,
isJsonB: b.isJsonB,
props: newProps,
}
}

Expand All @@ -50,7 +57,8 @@ func (b JsonBuildObjectBuilder) PropIf(condition bool, key string, value Exp) Js
func (b JsonBuildObjectBuilder) Unset(key string) JsonBuildObjectBuilder {
newProps := b.props.Delete(key)
return JsonBuildObjectBuilder{
props: newProps,
isJsonB: b.isJsonB,
props: newProps,
}
}

Expand Down
42 changes: 42 additions & 0 deletions builder/jsonb_build_object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package builder_test

import (
"testing"

"github.com/networkteam/qrb"
"github.com/networkteam/qrb/builder"
"github.com/networkteam/qrb/fn"
"github.com/networkteam/qrb/internal/testhelper"
)

func TestJsonbQuery(t *testing.T) {
t.Run("select json object", func(t *testing.T) {
b := qrb.SelectJson(
fn.JsonbBuildObject().
Prop("id", qrb.N("authors.author_id")).
Prop("name", qrb.N("authors.name")),
).
From(qrb.N("authors")).
Where(qrb.N("authors.author_id").Eq(qrb.Arg(123)))

testhelper.AssertSQLWriterEquals(
t,
"SELECT jsonb_build_object('id',authors.author_id,'name',authors.name) FROM authors WHERE authors.author_id = $1",
[]any{123},
b,
)

// We can now modify an existing JSON selection!
// Each SelectBuilder acts as a kind of query blueprint that can be used to modify later.
withPostCount := b.ApplySelectJson(func(obj builder.JsonBuildObjectBuilder) builder.JsonBuildObjectBuilder {
return obj.Prop("postCount", fn.Count(qrb.N("posts")))
})

testhelper.AssertSQLWriterEquals(
t,
"SELECT jsonb_build_object('id',authors.author_id,'name',authors.name,'postCount',count(posts)) FROM authors WHERE authors.author_id = $1",
[]any{123},
withPostCount,
)
})
}
6 changes: 5 additions & 1 deletion fn/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ package fn
import "github.com/networkteam/qrb/builder"

func JsonBuildObject() builder.JsonBuildObjectBuilder {
return builder.JsonBuildObject()
return builder.JsonBuildObject(false)
}

func JsonbBuildObject() builder.JsonBuildObjectBuilder {
return builder.JsonBuildObject(true)
}

0 comments on commit 31b2147

Please sign in to comment.