/
macros.go
69 lines (55 loc) · 1.53 KB
/
macros.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package sqlds
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
)
var (
// ErrorBadArgumentCount is returned from macros when the wrong number of arguments were provided
ErrorBadArgumentCount = errors.New("unexpected number of arguments")
)
// MacroFunc defines a signature for applying a query macro
// Query macro implementations are defined by users / consumers of this package
type MacroFunc func(*Query, []string) (string, error)
// Macros is a list of MacroFuncs.
// The "string" key is the name of the macro function. This name has to be regex friendly.
type Macros map[string]MacroFunc
func trimAll(s []string) []string {
r := make([]string, len(s))
for i, v := range s {
r[i] = strings.TrimSpace(v)
}
return r
}
func getMacroRegex(name string) string {
return fmt.Sprintf("\\$__%s(?:\\((.*?)\\))?", name)
}
func interpolate(driver Driver, query *Query) (string, error) {
macros := driver.Macros()
rawSQL := query.RawSQL
for key, macro := range macros {
rgx, err := regexp.Compile(getMacroRegex(key))
if err != nil {
return "", err
}
matches := rgx.FindAllStringSubmatch(rawSQL, -1)
for _, match := range matches {
if len(match) == 0 {
// There were no matches for this macro
continue
}
args := []string{}
if len(match) > 1 {
// This macro has arguments
args = trimAll(strings.Split(match[1], ","))
}
res, err := macro(query.WithSQL(rawSQL), args)
if err != nil {
return "", err
}
rawSQL = strings.Replace(rawSQL, match[0], res, -1)
}
}
return rawSQL, nil
}