Skip to content

Commit

Permalink
pre-parse all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrein committed Oct 11, 2023
1 parent 038420b commit 4820f2c
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 60 deletions.
25 changes: 16 additions & 9 deletions dotsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type ExecerContext interface {

// DotSql represents a dotSQL queries holder.
type DotSql struct {
queries map[string]string
queries map[string]*template.Template
options map[string]interface{}
}

Expand All @@ -75,18 +75,16 @@ func (d DotSql) WithOptions(additionalOptions map[string]interface{}) DotSql {
}

func (d DotSql) lookupQuery(name string, options map[string]interface{}) (query string, err error) {
query, ok := d.queries[name]
template, ok := d.queries[name]
if !ok {
err = fmt.Errorf("dotsql: '%s' could not be found", name)
}
if query != "" {
var tmpl *template.Template
if template != nil {
buffer := bytes.NewBufferString("")
tmpl, err = template.New(name).Parse(query)
if err != nil {
return
}
err = tmpl.Execute(buffer, options)
err = template.Execute(buffer, options)
if err != nil {
return
}
Expand Down Expand Up @@ -182,7 +180,7 @@ func (d DotSql) Raw(name string) (string, error) {
}

// QueryMap returns a map[string]string of loaded queries
func (d DotSql) QueryMap() map[string]string {
func (d DotSql) QueryMap() map[string]*template.Template {
return d.queries
}

Expand All @@ -191,8 +189,17 @@ func Load(r io.Reader) (*DotSql, error) {
scanner := &Scanner{}
queries := scanner.Run(bufio.NewScanner(r))

templates := make(map[string]*template.Template, len(queries))
var err error
for k, v := range queries {
templates[k], err = template.New(k).Parse(v)
if err != nil {
return nil, err
}
}

dotsql := &DotSql{
queries: queries,
queries: templates,
}

return dotsql, nil
Expand All @@ -219,7 +226,7 @@ func LoadFromString(sql string) (*DotSql, error) {
// It's in-order, so the last source will override queries with the same name
// in the previous arguments if any.
func Merge(dots ...*DotSql) *DotSql {
queries := make(map[string]string)
queries := make(map[string]*template.Template)

for _, dot := range dots {
for k, v := range dot.QueryMap() {
Expand Down
Loading

0 comments on commit 4820f2c

Please sign in to comment.