Skip to content

Commit cad6a9f

Browse files
committed
add auto @fields resolver in finder
1 parent abc6a6a commit cad6a9f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

mysql/repo_find.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"strings"
78

89
"github.com/georgysavva/scany/v2/sqlscan"
910
)
@@ -21,6 +22,7 @@ type Finder[T any] interface {
2122
WithTransformer(func(*T) error) Finder[T]
2223

2324
// Rows executes the query and returns a pgx.Rows iterator for processing result rows.
25+
// Auto replace @fields with struct fields list
2426
Rows(ctx context.Context, args ...any) (*sql.Rows, error)
2527

2628
// Struct executes the query and retrieves a single result, or an error if the query fails.
@@ -67,7 +69,16 @@ func (f *finder[T]) Rows(ctx context.Context, args ...any) (*sql.Rows, error) {
6769
return nil, ErrEmptySQL
6870
}
6971

70-
cmd := compile(f.sql, f.replacements...)
72+
replacements := append([]string{}, f.replacements...)
73+
if columns := typeColumns[T]([]string{}, []string{}); len(columns) > 0 {
74+
replacements = append(
75+
replacements,
76+
"@fields",
77+
strings.Join(columns, ","),
78+
)
79+
}
80+
81+
cmd := compile(f.sql, replacements...)
7182
rows, err := f.db.QueryContext(ctx, cmd, args...)
7283

7384
if errors.Is(err, sql.ErrNoRows) {

mysql/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func structColumns(v any, only, exclude []string) []string {
5353
return columns
5454
}
5555

56+
// typeColumns extracts column names from the `db` struct tag from type
57+
func typeColumns[T any](only, exclude []string) []string {
58+
var v T
59+
return structColumns(v, only, exclude)
60+
}
61+
5662
// structValues extracts field values from a struct based on the `db` tag, excluding unexported fields.
5763
func structValues(v any, only, exclude []string) []any {
5864
val := reflect.Indirect(reflect.ValueOf(v))

postgres/repo_find.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgres
33
import (
44
"context"
55
"errors"
6+
"strings"
67

78
"github.com/jackc/pgx/v5"
89
)
@@ -20,6 +21,7 @@ type Finder[T any] interface {
2021
WithTransformer(func(*T) error) Finder[T]
2122

2223
// Rows executes the query and returns a pgx.Rows iterator for processing result rows.
24+
// Auto replace @fields with struct fields list
2325
Rows(ctx context.Context, args ...any) (pgx.Rows, error)
2426

2527
// Struct executes the query and retrieves a single result, or an error if the query fails.
@@ -66,7 +68,16 @@ func (f *finder[T]) Rows(ctx context.Context, args ...any) (pgx.Rows, error) {
6668
return nil, ErrEmptySQL
6769
}
6870

69-
sql := compile(f.sql, f.replacements...)
71+
replacements := append([]string{}, f.replacements...)
72+
if columns := typeColumns[T]([]string{}, []string{}); len(columns) > 0 {
73+
replacements = append(
74+
replacements,
75+
"@fields",
76+
strings.Join(columns, ","),
77+
)
78+
}
79+
80+
sql := compile(f.sql, replacements...)
7081
rows, err := f.db.Query(ctx, sql, args...)
7182

7283
if errors.Is(err, pgx.ErrNoRows) {

postgres/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func structColumns(v any, only, exclude []string) []string {
5353
return columns
5454
}
5555

56+
// typeColumns extracts column names from the `db` struct tag from type
57+
func typeColumns[T any](only, exclude []string) []string {
58+
var v T
59+
return structColumns(v, only, exclude)
60+
}
61+
5662
// structValues extracts field values from a struct based on the `db` tag, excluding unexported fields.
5763
func structValues(v any, only, exclude []string) []any {
5864
val := reflect.Indirect(reflect.ValueOf(v))

0 commit comments

Comments
 (0)