Repository: zeromicro/go-zero
Pull request #4237 (2024-07-07 13:25:27)

feat/sqlc_partial

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/4237?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.10%. Comparing base [(`8690859`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/8690859c7d1c9461bdc422c3c65608bde30904db?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) to head [(`0174ed5`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/0174ed5825d0a22469cc58c11dc9c9106432f17d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro).
> Report is 79 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/4237?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [core/stores/sqlc/cachedsql.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/4237?src=pr&el=tree&filepath=core%2Fstores%2Fsqlc%2Fcachedsql.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-Y29yZS9zdG9yZXMvc3FsYy9jYWNoZWRzcWwuZ28=) | `100.00% <100.00%> (ø)` | |

... and [254 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/4237/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
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)


Test patch:
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
 }