forked from huandu/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injection.go
51 lines (40 loc) · 1.19 KB
/
injection.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
// Copyright 2018 Huan Du. All rights reserved.
// Copyright 2022 OOO SuperJob. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sphinxql
import (
"strings"
)
// injection is a helper type to manage injected SQLs in all builders.
type injection struct {
markerSQLs map[injectionMarker][]string
}
type injectionMarker int
// newInjection creates a new injection.
func newInjection() *injection {
return &injection{
markerSQLs: map[injectionMarker][]string{},
}
}
// SQL adds sql to injection's sql list.
// All sqls inside injection is ordered by marker in ascending order.
func (injection *injection) SQL(marker injectionMarker, sql string) {
injection.markerSQLs[marker] = append(injection.markerSQLs[marker], sql)
}
// WriteTo joins all SQL strings at the same marker value with blank (" ")
// and writes the joined value to buf.
func (injection *injection) WriteTo(buf *strings.Builder, marker injectionMarker) {
sqls := injection.markerSQLs[marker]
notEmpty := buf.Len() > 0
if len(sqls) == 0 {
return
}
if notEmpty {
buf.WriteRune(' ')
}
s := strings.Join(sqls, " ")
buf.WriteString(s)
if !notEmpty {
buf.WriteRune(' ')
}
}