Skip to content

Commit

Permalink
change PreparexBuilder interface define
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Aug 19, 2023
1 parent 29c5fae commit 86c49a5
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 48 deletions.
12 changes: 6 additions & 6 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ type PrepareContext interface {
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

// PreparexContext[T] enhances the Conn interface with context.
type PreparexContext[T any] interface {
// PreparexContext[T, S] enhances the Conn interface with context.
type PreparexContext[T, S any] interface {
// PrepareContext prepares a statement.
// The provided context is used for the preparation of the statement, not for
// the execution of the statement.
PreparexContext(ctx context.Context, query string) (T, error)

// PrepareNamedContext returns an sqlx.NamedStmt
PrepareNamedContext(ctx context.Context, query string) (T, error)
PrepareNamedContext(ctx context.Context, query string) (S, error)

// Rebind rebind query to adapte SQL Driver
Rebind(query string) string
Expand All @@ -168,9 +168,9 @@ type PrepareBuilder interface {
QueryHook(query string) string
}

// PreparexBuilder preparex builder interface for sqlx
type PreparexBuilder[T any] interface {
PreparexContext[T]
// PreparexBuilder[T, S] preparex builder interface for sqlx
type PreparexBuilder[T, S any] interface {
PreparexContext[T, S]
QueryHook(query string) string
}

Expand Down
32 changes: 26 additions & 6 deletions examples/auto/yesql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
query.Query = strings.TrimRight(query.Query, ";")
return query, nil
})
if err := yesql.Generate("yesql.sql", "auto", "yesql"); err != nil {
if err := yesql.Generate("yesql.sql", "auto", "yesql", yesql.SqlxPkgName("github.com/bitbus/sqlx")); err != nil {
log.Fatalf("generate code occurs error: %s", err)
}
log.Println("[Yesql] generate code finish")
Expand Down
6 changes: 4 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/alimy/yesql/examples
go 1.18

require (
github.com/alimy/yesql v1.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/alimy/yesql v1.8.0
github.com/bitbus/sqlx v1.6.0
)

replace github.com/alimy/yesql => ../
15 changes: 5 additions & 10 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
github.com/alimy/yesql v1.5.0 h1:Xmx+yBQCWA4f5xsraypbMnY7id8bckjUr8zxynteDBE=
github.com/alimy/yesql v1.5.0/go.mod h1:Y0FdRIwIbJyTv56wSX+MpaIHiAW1PyKTDYO6K/er4JY=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/bitbus/sqlx v1.6.0 h1:ewrBydRkyHZqfOqvHVYpiBlqQtLn93B/loa2EwjpZ74=
github.com/bitbus/sqlx v1.6.0/go.mod h1:MemKLfQ600g6PxUVsIDe48PlY3wOquyW2ApeiXoynFo=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
18 changes: 9 additions & 9 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type simplePrepareBuilder struct {
hooks []func(string) string
}

type simplePreparexBuilder[T any] struct {
p PreparexContext[T]
type simplePreparexBuilder[T, S any] struct {
p PreparexContext[T, S]
hooks []func(string) string
}

Expand All @@ -54,19 +54,19 @@ func (s *simplePrepareBuilder) QueryHook(query string) string {
return query
}

func (s *simplePreparexBuilder[T]) PreparexContext(ctx context.Context, query string) (T, error) {
func (s *simplePreparexBuilder[T, S]) PreparexContext(ctx context.Context, query string) (T, error) {
return s.p.PreparexContext(ctx, query)
}

func (s *simplePreparexBuilder[T]) PrepareNamedContext(ctx context.Context, query string) (T, error) {
func (s *simplePreparexBuilder[T, S]) PrepareNamedContext(ctx context.Context, query string) (S, error) {
return s.p.PrepareNamedContext(ctx, query)
}

func (s *simplePreparexBuilder[T]) Rebind(query string) string {
func (s *simplePreparexBuilder[T, S]) Rebind(query string) string {
return s.p.Rebind(query)
}

func (s *simplePreparexBuilder[T]) QueryHook(query string) string {
func (s *simplePreparexBuilder[T, S]) QueryHook(query string) string {
for _, h := range s.hooks {
query = h(query)
}
Expand Down Expand Up @@ -132,9 +132,9 @@ func NewPrepareBuilder(p PrepareContext, hooks ...func(string) string) PrepareBu
return obj
}

// NewPreprarexBuilder[T] create a simple preparex builder instance
func NewPreparexBuilder[T any](p PreparexContext[T], hooks ...func(string) string) PreparexBuilder[T] {
obj := &simplePreparexBuilder[T]{
// NewPreprarexBuilder[T, S] create a simple preparex builder instance
func NewPreparexBuilder[T, S any](p PreparexContext[T, S], hooks ...func(string) string) PreparexBuilder[T, S] {
obj := &simplePreparexBuilder[T, S]{
p: p,
}
for _, h := range hooks {
Expand Down
14 changes: 7 additions & 7 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

var (
_ PrepareHook = (*stdPrepareHook)(nil)
_ PrepareHook = (*sqlxPrepareHook[any])(nil)
_ PrepareHook = (*sqlxPrepareHook[any, any])(nil)
)

type stdPrepareHook struct {
prepare PrepareContext
}

type sqlxPrepareHook[T any] struct {
prepare PreparexContext[T]
type sqlxPrepareHook[T, S any] struct {
prepare PreparexContext[T, S]
}

func (s *stdPrepareHook) Prepare(field reflect.Type, query string) (any, error) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *stdPrepareHook) PrepareContext(ctx context.Context, field reflect.Type,
}
}

func (s *sqlxPrepareHook[T]) Prepare(field reflect.Type, query string) (any, error) {
func (s *sqlxPrepareHook[T, S]) Prepare(field reflect.Type, query string) (any, error) {
switch field.String() {
case "string":
// Unprepared SQL query.
Expand All @@ -77,7 +77,7 @@ func (s *sqlxPrepareHook[T]) Prepare(field reflect.Type, query string) (any, err
}
}

func (s *sqlxPrepareHook[T]) PrepareContext(ctx context.Context, field reflect.Type, query string) (any, error) {
func (s *sqlxPrepareHook[T, S]) PrepareContext(ctx context.Context, field reflect.Type, query string) (any, error) {
switch field.String() {
case "string":
// Unprepared SQL query.
Expand Down Expand Up @@ -109,8 +109,8 @@ func NewPrepareHook(p PrepareContext) PrepareHook {
}

// NewSqlxPrepareHook[T] create a prepare hook prepare that implement PreparexContext
func NewSqlxPrepareHook[T any](p PreparexContext[T]) PrepareHook {
return &sqlxPrepareHook[T]{
func NewSqlxPrepareHook[T, S any](p PreparexContext[T, S]) PrepareHook {
return &sqlxPrepareHook[T, S]{
prepare: p,
}
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package yesql

var Version = "v1.7.0"
var Version = "v1.8.0"
11 changes: 6 additions & 5 deletions yesql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package yesql
import (
"bytes"
"context"
"database/sql"
"fmt"
"io"
"os"
Expand All @@ -26,8 +27,8 @@ func Use(p PrepareContext) {
_defaultPrepareScanner = NewPrepareScanner(prepareHook)
}

// UseSqlx[T] use default prepare scanner withprepare that implement PreparexContext
func UseSqlx[T any](p PreparexContext[T]) {
// UseSqlx[T, S] use default prepare scanner withprepare that implement PreparexContext
func UseSqlx[T, S any](p PreparexContext[T, S]) {
prepareHook := NewSqlxPrepareHook[T](p)
_defaultPrepareScanner = NewPrepareScanner(prepareHook)
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func Generate(sqlFilePath string, dstPath string, pkgName string, opts ...option
}

// MustBuild build a struct object than type of T
func MustBuild[T any](p PrepareContext, fn func(PrepareBuilder, ...context.Context) (T, error), hook ...func(query string) string) T {
func MustBuild(p PrepareContext, fn func(PrepareBuilder, ...context.Context) (*sql.Stmt, error), hook ...func(query string) string) *sql.Stmt {
b := NewPrepareBuilder(p, hook...)
obj, err := fn(b)
if err != nil {
Expand All @@ -149,8 +150,8 @@ func MustBuild[T any](p PrepareContext, fn func(PrepareBuilder, ...context.Conte
return obj
}

// MustBuildx[T] build a struct object than type of T
func MustBuildx[T any](p PreparexContext[T], fn func(PreparexBuilder[T], ...context.Context) (T, error), hook ...func(query string) string) T {
// MustBuildx[T, S] build a struct object than type of T
func MustBuildx[T, S any](p PreparexContext[T, S], fn func(PreparexBuilder[T, S], ...context.Context) (T, error), hook ...func(query string) string) T {
b := NewPreparexBuilder(p, hook...)
obj, err := fn(b)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion yesql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestScan(t *testing.T) {
t.Error("[q2] expected to fail at non-existent query 'does-not-exist' but didn't")
}

SetDefaultPrepareHook(NewSqlxPrepareHook[*sqlx.Stmt](nil))
SetDefaultPrepareHook(NewSqlxPrepareHook[*sqlx.Stmt, *sqlx.NamedStmt](nil))
if err = Scan(&q3, queries); err != nil {
t.Errorf("[q3] failed to scan raw query to struct: %s", err)
}
Expand Down

0 comments on commit 86c49a5

Please sign in to comment.