forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_parameter_value.go
76 lines (64 loc) · 1.7 KB
/
query_parameter_value.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
70
71
72
73
74
75
76
package provider
import (
"fmt"
"strings"
)
// Query parameter holds normalized parameter data ready to be inserted in the
// final query
type QueryParameterValue struct {
// Token to replace e.g., !TOKEN!
Token string
// SQL expression to be inserted. Contains "?" that will be replaced with an
// ordinal argument e.g., "$1"
SQL string
// Value that will be passed to the final query in arguments list
Value interface{}
// Raw parameter and value for debugging and monitoring
RawParam string
// RawValue will be "" if the param wasn't passed and defaults were used
RawValue string
}
type Params map[string]QueryParameterValue
// ReplaceParams substitutes configured query parameter tokens for their values
// within the provided SQL string
func (params Params) ReplaceParams(sql string, args *[]interface{}) string {
if params == nil {
return sql
}
var (
cache = make(map[string]string)
sb strings.Builder
)
for _, token := range ParameterTokenRegexp.FindAllString(sql, -1) {
resultSQL, ok := cache[token]
if ok {
// Already have it cached, replace the token and move on.
sql = strings.ReplaceAll(sql, token, resultSQL)
continue
}
param, ok := params[token]
if !ok {
// Unknown token, ignoring
continue
}
sb.Reset()
sb.Grow(len(param.SQL))
argFound := false
// Replace every `?` in the param's SQL with a positional argument
for _, c := range param.SQL {
if c != '?' {
sb.WriteRune(c)
continue
}
if !argFound {
*args = append(*args, param.Value)
argFound = true
}
sb.WriteString(fmt.Sprintf("$%d", len(*args)))
}
resultSQL = sb.String()
cache[token] = resultSQL
sql = strings.ReplaceAll(sql, token, resultSQL)
}
return sql
}