forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
94 lines (82 loc) · 2.18 KB
/
Copy pathutils.go
File metadata and controls
94 lines (82 loc) · 2.18 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
package dolphin
import (
pcast "github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
func parseTableName(n *pcast.TableName) *ast.TableName {
return &ast.TableName{
Schema: identifier(n.Schema.String()),
Name: identifier(n.Name.String()),
}
}
func toList(node pcast.Node) *ast.List {
var items []ast.Node
switch n := node.(type) {
case *pcast.TableName:
if schema := n.Schema.String(); schema != "" {
items = append(items, NewIdentifier(schema))
}
items = append(items, NewIdentifier(n.Name.String()))
default:
return nil
}
return &ast.List{Items: items}
}
func isNotNull(n *pcast.ColumnDef) bool {
for i := range n.Options {
if n.Options[i].Tp == pcast.ColumnOptionNotNull {
return true
}
if n.Options[i].Tp == pcast.ColumnOptionPrimaryKey {
return true
}
}
return false
}
func convertToRangeVarList(list *ast.List, result *ast.List) {
if len(list.Items) == 0 {
return
}
switch rel := list.Items[0].(type) {
// Special case for joins in updates
case *ast.JoinExpr:
left, ok := rel.Larg.(*ast.RangeVar)
if !ok {
if list, check := rel.Larg.(*ast.List); check {
convertToRangeVarList(list, result)
} else if subselect, check := rel.Larg.(*ast.RangeSubselect); check {
// Handle subqueries in JOIN clauses
result.Items = append(result.Items, subselect)
} else {
panic("expected range var")
}
}
if left != nil {
result.Items = append(result.Items, left)
}
right, ok := rel.Rarg.(*ast.RangeVar)
if !ok {
if list, check := rel.Rarg.(*ast.List); check {
convertToRangeVarList(list, result)
} else if subselect, check := rel.Rarg.(*ast.RangeSubselect); check {
// Handle subqueries in JOIN clauses
result.Items = append(result.Items, subselect)
} else {
panic("expected range var")
}
}
if right != nil {
result.Items = append(result.Items, right)
}
case *ast.RangeVar:
result.Items = append(result.Items, rel)
case *ast.RangeSubselect:
result.Items = append(result.Items, rel)
default:
panic("expected range var")
}
}
func isUnsigned(n *pcast.ColumnDef) bool {
return mysql.HasUnsignedFlag(n.Tp.GetFlag())
}