Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilLuno committed Nov 13, 2024
1 parent 7f690d3 commit 7d70cba
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 288 deletions.
55 changes: 20 additions & 35 deletions rsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -46,33 +45,19 @@ func (t eventType) ReflexType() int {

// makeDefaultInserter returns the default sql inserter configured via WithEventsXField options.
func makeDefaultInserter(schema eTableSchema) inserter {
return func(ctx context.Context, tx *sql.Tx,
foreignID string, typ reflex.EventType, metadata []byte,
ins := makeDefaultManyInserter(schema)
return func(
ctx context.Context,
tx *sql.Tx,
foreignID string,
typ reflex.EventType,
metadata []byte,
) error {
q := "insert into " + schema.name +
" set " + schema.foreignIDField + "=?, " + schema.timeField + "=now(6), " + schema.typeField + "=?"
args := []interface{}{foreignID, typ.ReflexType()}

if schema.metadataField != "" {
q += ", " + schema.metadataField + "=?"
args = append(args, metadata)
} else if metadata != nil {
return errors.New("metadata not enabled")
}

spanCtx, hasTrace := tracing.Extract(ctx)
if schema.traceField != "" && hasTrace {
traceData, err := tracing.Marshal(spanCtx)
if err != nil {
return err
}

q += ", " + schema.traceField + "=?"
args = append(args, traceData)
}

_, err := tx.ExecContext(ctx, q, args...)
return errors.Wrap(err, "insert error")
return ins(ctx, tx, EventToInsert{
ForeignID: foreignID,
Type: typ,
Metadata: metadata,
})
}
}

Expand Down Expand Up @@ -106,33 +91,33 @@ func makeInsertManyQuery(
traceData = d
}

cols := []string{schema.foreignIDField, schema.typeField, schema.timeField}
cols := schema.foreignIDField + ", " + schema.typeField + ", " + schema.timeField
if schema.metadataField != "" {
cols = append(cols, schema.metadataField)
cols += ", " + schema.metadataField
}
if traceData != nil {
cols = append(cols, schema.traceField)
cols += ", " + schema.traceField
}

q := "insert into " + schema.name + " (" + strings.Join(cols, ", ") + ") values"
q := "insert into " + schema.name + " (" + cols + ") values"

for i, e := range events {
vals := []string{"?", "?", "now(6)"}
vals := "?, ?, now(6)"
args = append(args, e.ForeignID, e.Type.ReflexType())
if schema.metadataField != "" {
vals = append(vals, "?")
vals += ", ?"
args = append(args, e.Metadata)
} else if e.Metadata != nil {
return "", nil, errors.New("metadata not enabled")
}
if traceData != nil {
vals = append(vals, "?")
vals += ", ?"
args = append(args, traceData)
}
if i > 0 {
q += ","
}
q += " (" + strings.Join(vals, ", ") + ")"
q += " (" + vals + ")"
}

return q, args, nil
Expand Down
32 changes: 20 additions & 12 deletions rsql/db_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package rsql

import (
"bytes"
"context"
"fmt"
"testing"

"github.com/luno/jettison/jtest"
Expand All @@ -17,11 +19,6 @@ import (
func Test_makeInsertManyQuery(t *testing.T) {
ctx := context.Background()

type res struct {
Q string
Args []any
}

defaultSchema := eTableSchema{
name: "events",
idField: "id",
Expand All @@ -30,18 +27,29 @@ func Test_makeInsertManyQuery(t *testing.T) {
foreignIDField: "foreign_id",
}

assert := func(t *testing.T, q string, args []any) {
buf := new(bytes.Buffer)
buf.WriteString(q)
buf.WriteString("\n")
for _, arg := range args {
buf.WriteString("\n")
buf.WriteString(fmt.Sprint(arg))
}
goldie.New(t).Assert(t, t.Name(), buf.Bytes())
}

t.Run("empty", func(t *testing.T) {
q, args, err := makeInsertManyQuery(ctx, defaultSchema, nil)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})

t.Run("one", func(t *testing.T) {
q, args, err := makeInsertManyQuery(ctx, defaultSchema, []EventToInsert{
{"fid1", testEventType(1), nil},
})
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})

t.Run("two", func(t *testing.T) {
Expand All @@ -50,17 +58,17 @@ func Test_makeInsertManyQuery(t *testing.T) {
{"fid2", testEventType(2), nil},
})
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})

t.Run("more", func(t *testing.T) {
var events []EventToInsert
for i := range 100 {
for i := range 5 {
events = append(events, EventToInsert{"fid", testEventType(i), nil})
}
q, args, err := makeInsertManyQuery(ctx, defaultSchema, events)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})

t.Run("metadata_error", func(t *testing.T) {
Expand All @@ -77,7 +85,7 @@ func Test_makeInsertManyQuery(t *testing.T) {
{"fid1", testEventType(1), []byte("metadata")},
})
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})

t.Run("with_trace", func(t *testing.T) {
Expand All @@ -97,7 +105,7 @@ func Test_makeInsertManyQuery(t *testing.T) {
{"fid1", testEventType(1), nil},
})
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), res{q, args})
assert(t, q, args)
})
}

Expand Down
5 changes: 1 addition & 4 deletions rsql/testdata/Test_makeInsertManyQuery/empty.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
{
"Q": "insert into events (foreign_id, type, timestamp) values",
"Args": null
}
insert into events (foreign_id, type, timestamp) values
Loading

0 comments on commit 7d70cba

Please sign in to comment.