forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (57 loc) · 1.19 KB
/
utils.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
//go:build !windows
// +build !windows
package postgresql
import (
nodes "github.com/pganalyze/pg_query_go/v2"
)
func isArray(n *nodes.TypeName) bool {
if n == nil {
return false
}
return len(n.ArrayBounds) > 0
}
func isNotNull(n *nodes.ColumnDef) bool {
if n.IsNotNull {
return true
}
for _, c := range n.Constraints {
switch inner := c.Node.(type) {
case *nodes.Node_Constraint:
if inner.Constraint.Contype == nodes.ConstrType_CONSTR_NOTNULL {
return true
}
if inner.Constraint.Contype == nodes.ConstrType_CONSTR_PRIMARY {
return true
}
}
}
return false
}
func IsNamedParamFunc(node *nodes.Node) bool {
fun, ok := node.Node.(*nodes.Node_FuncCall)
return ok && joinNodes(fun.FuncCall.Funcname, ".") == "sqlc.arg"
}
func IsNamedParamSign(node *nodes.Node) bool {
expr, ok := node.Node.(*nodes.Node_AExpr)
return ok && joinNodes(expr.AExpr.Name, ".") == "@"
}
func makeByte(s string) byte {
var b byte
if s == "" {
return b
}
return []byte(s)[0]
}
func makeUint32Slice(in []uint64) []uint32 {
out := make([]uint32, len(in))
for i, v := range in {
out[i] = uint32(v)
}
return out
}
func makeString(s string) *string {
if s == "" {
return nil
}
return &s
}