-
Notifications
You must be signed in to change notification settings - Fork 6
/
tableNode.go
148 lines (127 loc) · 3.22 KB
/
tableNode.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package query
import (
"bytes"
"encoding/gob"
"github.com/goradd/goradd/pkg/log"
"strings"
)
// TableNodeI is the interface that all table nodes must implement. TableNodes are created by the code generation
// process, one for each table in the database.
type TableNodeI interface {
ReferenceNodeI
SelectNodes_() []*ColumnNode
PrimaryKeyNode() *ColumnNode
EmbeddedNode_() NodeI
Copy_() NodeI
}
// A TableNode is a representation of the top level of a chain of nodes that point to a particular field in a query, even after
// aliases and joins are taken into account. TableNodes are create by the code generation
// process, one for each table in the database.
type TableNode struct {
nodeAlias
nodeLink
// Which database in the global list of databases does the node belong to
dbKey string
// Name of table in the database we point to
dbTable string
// The name of the function used to access the property as a node or ORM item
goPropName string
}
// NewTableNode creates a table node, which is always the starting node in a node chain
func NewTableNode(dbKey string, dbName string, goName string) *TableNode {
return &TableNode{
dbKey: dbKey,
dbTable: dbName,
goPropName: goName,
}
}
func (n *TableNode) copy() NodeI {
return &TableNode{
dbKey: n.dbKey,
dbTable: n.dbTable,
goPropName: n.goPropName,
nodeAlias: nodeAlias{n.alias},
}
}
func (n *TableNode) tableName() string {
return n.dbTable
}
func (n *TableNode) databaseKey() string {
return n.dbKey
}
func (n *TableNode) goName() string {
return n.goPropName
}
func (n *TableNode) Equals(n2 NodeI) bool {
if tn, ok := n2.(TableNodeI); !ok {
return false
} else if cn, ok := tn.EmbeddedNode_().(*TableNode); !ok {
return false
} else {
return cn.dbTable == n.dbTable &&
cn.dbKey == n.dbKey &&
(cn.alias == "" || n.alias == "" || cn.alias == n.alias)
}
}
func (n *TableNode) Expand() {
panic("you cannot expand a TableNode")
}
func (n *TableNode) isExpanded() bool {
return false
}
func (n *TableNode) isExpander() bool {
return false
}
func (n *TableNode) getCondition() NodeI {
return nil
}
func (n *TableNode) setCondition(c NodeI) {
panic("you cannot set a condition on a TableNode")
}
func (n *TableNode) nodeType() NodeType {
return TableNodeType
}
func (n *TableNode) log(level int) {
tabs := strings.Repeat("\t", level)
log.FrameworkDebug(tabs + "Table: " + n.dbTable)
}
func (n *TableNode) GobEncode() (data []byte, err error) {
var buf bytes.Buffer
e := gob.NewEncoder(&buf)
if err = e.Encode(n.alias); err != nil {
panic(err)
}
if err = e.Encode(n.dbKey); err != nil {
panic(err)
}
if err = e.Encode(n.dbTable); err != nil {
panic(err)
}
if err = e.Encode(n.goPropName); err != nil {
panic(err)
}
// table nodes have no parent
//err = e.Encode(n.nodeLink.parentNode)
data = buf.Bytes()
return
}
func (n *TableNode) GobDecode(data []byte) (err error) {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
if err = dec.Decode(&n.alias); err != nil {
panic(err)
}
if err = dec.Decode(&n.dbKey); err != nil {
panic(err)
}
if err = dec.Decode(&n.dbTable); err != nil {
panic(err)
}
if err = dec.Decode(&n.goPropName); err != nil {
panic(err)
}
return
}
func init() {
gob.Register(&TableNode{})
}