diff --git a/core/stores/sqlc/cachedsql.go b/core/stores/sqlc/cachedsql.go index 8b81e925..addf3b40 100644 --- a/core/stores/sqlc/cachedsql.go +++ b/core/stores/sqlc/cachedsql.go @@ -190,6 +190,17 @@ func (cc CachedConn) QueryRowNoCacheCtx(ctx context.Context, v any, q string, return cc.db.QueryRowCtx(ctx, v, q, args...) } +// QueryRowPartialNoCache unmarshals into v with given statement. +func (cc CachedConn) QueryRowPartialNoCache(v any, q string, args ...any) error { + return cc.QueryRowPartialNoCacheCtx(context.Background(), v, q, args...) +} + +// QueryRowPartialNoCacheCtx unmarshals into v with given statement. +func (cc CachedConn) QueryRowPartialNoCacheCtx(ctx context.Context, v any, q string, + args ...any) error { + return cc.db.QueryRowPartialCtx(ctx, v, q, args...) +} + // QueryRowsNoCache unmarshals into v with given statement. // It doesn't use cache, because it might cause consistency problem. func (cc CachedConn) QueryRowsNoCache(v any, q string, args ...any) error { @@ -203,6 +214,19 @@ func (cc CachedConn) QueryRowsNoCacheCtx(ctx context.Context, v any, q string, return cc.db.QueryRowsCtx(ctx, v, q, args...) } +// QueryRowsPartialNoCache unmarshals into v with given statement. +// It doesn't use cache, because it might cause consistency problem. +func (cc CachedConn) QueryRowsPartialNoCache(v any, q string, args ...any) error { + return cc.QueryRowsPartialNoCacheCtx(context.Background(), v, q, args...) +} + +// QueryRowsPartialNoCacheCtx unmarshals into v with given statement. +// It doesn't use cache, because it might cause consistency problem. +func (cc CachedConn) QueryRowsPartialNoCacheCtx(ctx context.Context, v any, q string, + args ...any) error { + return cc.db.QueryRowsPartialCtx(ctx, v, q, args...) +} + // SetCache sets v into cache with given key. func (cc CachedConn) SetCache(key string, val any) error { return cc.SetCacheCtx(context.Background(), key, val) diff --git a/core/stores/sqlc/cachedsql_test.go b/core/stores/sqlc/cachedsql_test.go index 84b04402..5ebb56d4 100644 --- a/core/stores/sqlc/cachedsql_test.go +++ b/core/stores/sqlc/cachedsql_test.go @@ -579,6 +579,48 @@ func TestQueryRowNoCache(t *testing.T) { assert.True(t, ran) } +func TestQueryRowPartialNoCache(t *testing.T) { + r := redistest.CreateRedis(t) + + const ( + key = "user" + value = "any" + ) + var user string + var ran bool + conn := dummySqlConn{queryRow: func(v any, q string, args ...any) error { + user = value + ran = true + return nil + }} + c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*30)) + err := c.QueryRowPartialNoCache(&user, key) + assert.Nil(t, err) + assert.Equal(t, value, user) + assert.True(t, ran) +} + +func TestQueryRowsPartialNoCache(t *testing.T) { + r := redistest.CreateRedis(t) + + var ( + key = "user" + values = []string{"any", "any"} + ) + var users []string + var ran bool + conn := dummySqlConn{queryRows: func(v any, q string, args ...any) error { + users = values + ran = true + return nil + }} + c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*30)) + err := c.QueryRowsPartialNoCache(&users, key) + assert.Nil(t, err) + assert.Equal(t, values, users) + assert.True(t, ran) +} + func TestNewConnWithCache(t *testing.T) { r := redistest.CreateRedis(t) @@ -716,7 +758,8 @@ func resetStats() { } type dummySqlConn struct { - queryRow func(any, string, ...any) error + queryRow func(any, string, ...any) error + queryRows func(any, string, ...any) error } func (d dummySqlConn) ExecCtx(_ context.Context, _ string, _ ...any) (sql.Result, error) { @@ -727,7 +770,11 @@ func (d dummySqlConn) PrepareCtx(_ context.Context, _ string) (sqlx.StmtSession, return nil, nil } -func (d dummySqlConn) QueryRowPartialCtx(_ context.Context, _ any, _ string, _ ...any) error { +func (d dummySqlConn) QueryRowPartialCtx(_ context.Context, v any, query string, args ...any) error { + if d.queryRow != nil { + return d.queryRow(v, query, args...) + } + return nil } @@ -735,7 +782,11 @@ func (d dummySqlConn) QueryRowsCtx(_ context.Context, _ any, _ string, _ ...any) return nil } -func (d dummySqlConn) QueryRowsPartialCtx(_ context.Context, _ any, _ string, _ ...any) error { +func (d dummySqlConn) QueryRowsPartialCtx(_ context.Context, v any, query string, args ...any) error { + if d.queryRows != nil { + return d.queryRows(v, query, args...) + } + return nil }