Repository navigation
Expand file tree
/
Copy pathast_format_postgres.go
More file actions
132 lines (120 loc) · 3.62 KB
/
Copy pathast_format_postgres.go
File metadata and controls
132 lines (120 loc) · 3.62 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package astformat
import (
"fmt"
"strings"
"github.com/stackql/stackql-parser/go/vt/sqlparser"
"github.com/stackql/stackql/internal/stackql/astfuncrewrite"
"github.com/stackql/stackql/internal/stackql/constants"
)
//nolint:revive // Explicit type declaration removes any ambiguity
var (
_ sqlparser.NodeFormatter = PostgresSelectExprsFormatter
//nolint:gochecknoglobals // Convenient to have this as a global variable
sqliteKeywords map[string]struct{} = map[string]struct{}{
"commit": {},
"key": {},
"select": {},
}
)
func PostgresSelectExprsFormatter(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
switch node := node.(type) {
case *sqlparser.ColName:
formatColName(node, buf)
case sqlparser.ColIdent:
formatColIdent(node, buf)
return
case *sqlparser.FuncExpr:
node = postgresJSONExtract(node)
if strings.ToLower(node.Name.GetRawVal()) == constants.SQLFuncJSONExtractPostgres && len(node.Exprs) > 1 {
sb := buf
sb.AstPrintf(
node,
"%s(%v%s, %v",
constants.SQLFuncJSONExtractPostgres,
node.Exprs[0],
constants.PostgresJSONCastSuffix,
node.Exprs[1])
for _, val := range node.Exprs[2:] {
sb.AstPrintf(node, ", %v", val)
}
sb.AstPrintf(node, ")")
return
}
if strings.ToLower(node.Name.GetRawVal()) == sqlparser.JsonArrayElementsTextStr && len(node.Exprs) >= 1 {
sb := buf
sb.AstPrintf(
node,
"%s(%v%s",
sqlparser.JsonArrayElementsTextStr,
node.Exprs[0],
constants.PostgresJSONCastSuffix,
)
if len(node.Exprs) > 1 {
for _, val := range node.Exprs[1:] {
sb.AstPrintf(node, ", %v", val)
}
}
sb.AstPrintf(node, ")")
return
}
node.Format(buf)
return
case *sqlparser.GroupConcatExpr:
sb := buf
sb.AstPrintf(node, "%s(%v, %s)", constants.SQLFuncGroupConcatPostgres, node.Exprs[0], `','`)
return
default:
node.Format(buf)
return
}
}
func formatColIdent(node sqlparser.ColIdent, buf *sqlparser.TrackedBuffer) {
if node.AtCount() > 0 {
sqlparser.FormatID(buf, node.String(), node.Lowered(), node.AtCount())
return
}
buf.WriteString(fmt.Sprintf(`"%s"`, strings.ReplaceAll(node.String(), `"`, `""`)))
}
func isSqliteKeyword(term string) bool {
_, ok := sqliteKeywords[strings.ToLower(term)]
return ok
}
func formatColIdentCaseInsensitive(node sqlparser.ColIdent, buf *sqlparser.TrackedBuffer) {
if node.AtCount() > 0 {
sqlparser.FormatID(buf, node.String(), node.Lowered(), node.AtCount())
return
}
if isSqliteKeyword(node.String()) || strings.IndexFunc(node.String(), func(r rune) bool {
return !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_')
}) >= 0 {
buf.WriteString(fmt.Sprintf(`"%s"`, strings.ReplaceAll(node.String(), `"`, `""`)))
return
}
buf.WriteString(node.String())
}
func formatColName(node *sqlparser.ColName, buf *sqlparser.TrackedBuffer) {
tableNameStr := node.Qualifier.GetRawVal()
if tableNameStr != "" {
buf.WriteString(fmt.Sprintf(`"%s".`, strings.ReplaceAll(tableNameStr, `"`, `""`)))
}
formatColIdent(node.Name, buf)
}
func String(node sqlparser.SQLNode, formatter sqlparser.NodeFormatter) string {
return formattedString(node, formatter)
}
func formattedString(node sqlparser.SQLNode, formatter sqlparser.NodeFormatter) string {
buf := sqlparser.NewTrackedBuffer(formatter)
buf.Myprintf("%v", node)
return buf.String()
}
func postgresJSONExtract(node *sqlparser.FuncExpr) *sqlparser.FuncExpr {
if !strings.EqualFold(node.Name.GetRawVal(), constants.SQLFuncJSONExtractConformed) {
return node
}
cloned := *node
rewritten, err := astfuncrewrite.GetPostgresASTFuncRewriter().RewriteFunc(&cloned)
if err != nil {
return node
}
return rewritten
}