Skip to content

Commit

Permalink
add NewPrepareBuilder(...)/NewPreparexBuilder(...) help function
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 30, 2023
1 parent 4fc309d commit 8397bcd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 21 deletions.
40 changes: 20 additions & 20 deletions examples/auto/yesql.go

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

67 changes: 67 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package yesql

import (
"context"
"database/sql"
"os"
"path/filepath"
stdTmpl "text/template"

"github.com/alimy/yesql/naming"
"github.com/alimy/yesql/template"
"github.com/jmoiron/sqlx"
)

var (
Expand All @@ -22,10 +25,50 @@ type tmplCtx struct {
YesqlVer string
}

type simplePrepareBuilder struct {
p PrepareContext
hook func(string) string
}

type simplePreparexBuilder struct {
p PreparexContext
hook func(string) string
}

type sqlGenerator struct {
tmpl *stdTmpl.Template
}

func (s *simplePrepareBuilder) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return s.p.PrepareContext(ctx, query)
}

func (s *simplePrepareBuilder) QueryHook(query string) string {
if s.hook != nil {
return s.hook(query)
}
return query
}

func (s *simplePreparexBuilder) PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error) {
return s.p.PreparexContext(ctx, query)
}

func (s *simplePreparexBuilder) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) {
return s.p.PrepareNamedContext(ctx, query)
}

func (s *simplePreparexBuilder) Rebind(query string) string {
return s.p.Rebind(query)
}

func (s *simplePreparexBuilder) QueryHook(query string) string {
if s.hook != nil {
return s.hook(query)
}
return query
}

func (t *tmplCtx) DefaultQueryMapNotEmpty() bool {
return len(t.DefaultQueryMap) != 0
}
Expand Down Expand Up @@ -70,12 +113,36 @@ func (s *sqlGenerator) Generate(dstPath string, pkgName string, query SQLQuery,
return s.tmpl.Execute(file, data)
}

// NewPrepareBuilder create a simple prepare builder instance
func NewPrepareBuilder(p PrepareContext, hook ...func(string) string) PrepareBuilder {
obj := &simplePrepareBuilder{
p: p,
}
if len(hook) > 0 && hook[0] != nil {
obj.hook = hook[0]
}
return obj
}

// NewPreprarexBuilder create a simple preparex builder instance
func NewPreparexBuilder(p PreparexContext, hook ...func(string) string) PreparexBuilder {
obj := &simplePreparexBuilder{
p: p,
}
if len(hook) > 0 && hook[0] != nil {
obj.hook = hook[0]
}
return obj
}

// NewSqlGenerator create a sql generator use std sql
func NewSqlGenerator() Generator {
return &sqlGenerator{
tmpl: template.NewSqlTemplate(),
}
}

// NewSqlxGenerator create a sqlx generator use sqlx
func NewSqlxGenerator() Generator {
return &sqlGenerator{
tmpl: template.NewSqlxTemplate(),
Expand Down
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.1.0"
var Version = "v1.1.1"

0 comments on commit 8397bcd

Please sign in to comment.