-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsqlinclause.go
More file actions
84 lines (65 loc) · 2.53 KB
/
sqlinclause.go
File metadata and controls
84 lines (65 loc) · 2.53 KB
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
77
78
79
80
81
82
83
84
package sqlutil
import (
"fmt"
"strconv"
"strings"
)
const (
sqlConditionIn = "IN"
sqlConditionNotIn = "NOT IN"
)
// BuildInClauseString prepares a SQL IN clause with the given list of string values.
func (c *SQLUtil) BuildInClauseString(field string, values []string) string {
return c.composeInClause(sqlConditionIn, field, c.formatStrings(values))
}
// BuildNotInClauseString prepares a SQL NOT IN clause with the given list of string values.
func (c *SQLUtil) BuildNotInClauseString(field string, values []string) string {
return c.composeInClause(sqlConditionNotIn, field, c.formatStrings(values))
}
// BuildInClauseInt prepares a SQL IN clause with the given list of integer values.
func (c *SQLUtil) BuildInClauseInt(field string, values []int) string {
return c.composeInClause(sqlConditionIn, field, formatInts(values))
}
// BuildNotInClauseInt prepares a SQL NOT IN clause with the given list of integer values.
func (c *SQLUtil) BuildNotInClauseInt(field string, values []int) string {
return c.composeInClause(sqlConditionNotIn, field, formatInts(values))
}
// BuildInClauseUint prepares a SQL IN clause with the given list of integer values.
func (c *SQLUtil) BuildInClauseUint(field string, values []uint64) string {
return c.composeInClause(sqlConditionIn, field, formatUints(values))
}
// BuildNotInClauseUint prepares a SQL NOT IN clause with the given list of integer values.
func (c *SQLUtil) BuildNotInClauseUint(field string, values []uint64) string {
return c.composeInClause(sqlConditionNotIn, field, formatUints(values))
}
// composeInClause constructs the final IN or NOT IN clause string.
func (c *SQLUtil) composeInClause(condition string, field string, values []string) string {
if len(values) == 0 {
return ""
}
return fmt.Sprintf("%s %s (%s)", c.QuoteID(field), condition, strings.Join(values, ","))
}
// formatStrings quotes each string value for safe SQL inclusion.
func (c *SQLUtil) formatStrings(values []string) []string {
items := make([]string, len(values))
for k, v := range values {
items[k] = c.QuoteValue(v)
}
return items
}
// formatInts converts each integer value to its string representation.
func formatInts(values []int) []string {
items := make([]string, len(values))
for k, v := range values {
items[k] = strconv.Itoa(v)
}
return items
}
// formatUints converts each unsigned integer value to its string representation.
func formatUints(values []uint64) []string {
items := make([]string, len(values))
for k, v := range values {
items[k] = strconv.FormatUint(v, 10)
}
return items
}