/
summary.go
224 lines (187 loc) · 4.67 KB
/
summary.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"strings"
"fmt"
"log"
"github.com/jmoiron/sqlx"
)
type InOutType struct {
Name string
Type string
}
type summary struct {
Enums []Enum
Tables []Table
Views []Table
Functions []Function
}
func NewSummaryFromDB(db *sqlx.DB) (*summary, error) {
s := &summary{}
// Populate the enums
es, err := GetEnums(db)
if err != nil {
return nil, err
}
s.Enums = es
// Populate the tables
ts, err := GetTables(db)
if err != nil {
return nil, err
}
s.Tables = make([]Table, 0)
s.Views = make([]Table, 0)
// Get the indexes for each table
for _, t := range ts {
switch t.Type {
case "table":
inds, err := GetIndices(db, t.OID)
if err != nil {
return nil, err
}
t.Indices = inds
s.Tables = append(s.Tables, t)
case "view":
s.Views = append(s.Views, t)
}
}
// Create functions
// -- The indices from functions
s.Functions = make([]Function, 0)
for _, table := range s.Tables {
for _, index := range table.Indices {
var name string
if index.IsUnique {
name = "Get"
} else {
name = "List"
}
name += capitalize(table.Name)
if len(index.Columns) > 0 {
name += "By"
if len(index.Columns) == 1 {
name += capitalize(index.Columns[0].Name)
} else {
name += capitalize(index.Name)
}
}
fn := Function{
Name: name,
Inputs: make([]InOutType, len(index.Columns)),
Outputs: []InOutType{{Name: "output", Type: table.Name}},
Type: "SELECT",
IsOutArray: !index.IsUnique,
}
// Starting point
fn.Query = "SELECT "
// The projected fields, in index case, all fields
var fs = []string{}
for _, field := range table.Fields {
fs = append(fs, strings.ToLower(field.Name))
}
fn.Query += strings.Join(fs, ", ") + " FROM " + strings.ToLower(table.Name) + " WHERE "
whereCasues := []string{}
for i, column := range index.Columns {
// Add to function struct
fn.Inputs[i] = InOutType{Name: column.Name, Type: column.Typename}
whereCasues = append(whereCasues, fmt.Sprintf("`%s` = $%d", column.Name, i+1))
}
fn.Query += strings.Join(whereCasues, " AND ")
s.Functions = append(s.Functions, fn)
}
}
// -- The functions from table lists only
for _, table := range s.Tables {
fn := Function{
Name: "List" + capitalize(table.Name),
Outputs: []InOutType{{Name: "output", Type: table.Name}},
Type: "SELECT",
IsOutArray: true,
}
fn.Query = "SELECT "
// The projected fields, in index case, all fields
var fs = []string{}
for _, field := range table.Fields {
fs = append(fs, strings.ToLower(field.Name))
}
fn.Query += strings.Join(fs, ", ") + " FROM " + strings.ToLower(table.Name)
s.Functions = append(s.Functions, fn)
}
// -- The user-defined functions
fs, err := GetUserFunctions(db)
if err != nil {
log.Fatal("haydaaa", err)
}
s.Functions = append(s.Functions, fs...)
return s, nil
}
func capitalize(s string) string {
return strings.ToUpper(s[0:1]) + s[1:]
}
func (s summary) generateProtoSummary() protoSummary {
ps := newProtoSummary()
// The placeholder VoidArg and VoidRes because gRPC does not support no arguments
voidArg := newProtoMessage("VoidArg")
ps.addMessage(voidArg)
voidRes := newProtoMessage("VoidRes")
ps.addMessage(voidRes)
// Enums are independent, they are first
for _, enum := range s.Enums {
ps.addEnum(protoEnum(enum))
}
// TODO: Other composite types
// Table types
for _, table := range s.Tables {
msg := newProtoMessage(table.Name)
for _, field := range table.Fields {
msg.addField(field.Name, field.Type, field.Num)
}
ps.addMessage(msg)
}
// Views
for _, view := range s.Views {
msg := newProtoMessage(view.Name)
for _, field := range view.Fields {
msg.addField(field.Name, field.Type, field.Num)
}
ps.addMessage(msg)
}
for _, fn := range s.Functions {
var inputName, outputName string
// Process inputs
if fn.Inputs == nil {
inputName = "VoidArg"
} else {
inputName = fn.Name + "Arg"
msg := newProtoMessage(inputName)
for idx, inp := range fn.Inputs {
if inp.Name == "" {
inp.Name = fmt.Sprintf("var%d", idx+1)
}
msg.addField(inp.Name, inp.Type, idx+1)
}
ps.addMessage(msg)
}
// Process outputs
if len(fn.Outputs) > 0 {
outputName = fn.Name + "Out"
msg := newProtoMessage(outputName)
for idx, outp := range fn.Outputs {
if outp.Name == "" {
outp.Name = fmt.Sprintf("var%d", idx+1)
}
msg.addField(outp.Name, outp.Type, idx+1)
}
ps.addMessage(msg)
} else {
outputName = "VoidRes"
}
rpc := protoRpc{
Function: fn,
Name: fn.Name,
InputName: inputName,
OutputName: outputName,
}
ps.addFunc(rpc)
}
return *ps
}