-
Notifications
You must be signed in to change notification settings - Fork 264
/
metadata_tables.go
225 lines (186 loc) · 7.32 KB
/
metadata_tables.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
225
package db
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/turbot/go-kit/helpers"
typeHelpers "github.com/turbot/go-kit/types"
"github.com/turbot/steampipe/constants"
"github.com/turbot/steampipe/steampipeconfig/modconfig"
"github.com/turbot/steampipe/utils"
)
// TagColumn :: tag used to specify the column name and type in the reflection tables
const TagColumn = "column"
func UpdateMetadataTables(workspaceResources *modconfig.WorkspaceResourceMaps, client *Client) error {
utils.LogTime("db.UpdateMetadataTables start")
defer utils.LogTime("db.UpdateMetadataTables end")
// get the create sql for each table type
clearSql := getClearTablesSql()
// now get sql to populate the tables
insertSql := getTableInsertSql(workspaceResources)
sql := []string{clearSql, insertSql}
// execute the query, passing 'true' to disable the spinner
_, err := client.ExecuteSync(context.Background(), strings.Join(sql, "\n"), true)
if err != nil {
return fmt.Errorf("failed to update reflection tables: %v", err)
}
return nil
}
func CreateMetadataTables(workspaceResources *modconfig.WorkspaceResourceMaps, client *Client) error {
utils.LogTime("db.CreateMetadataTables start")
defer utils.LogTime("db.CreateMetadataTables end")
// get the sql for columns which every table has
commonColumnSql := getColumnDefinitions(modconfig.ResourceMetadata{})
// get the create sql for each table type
createSql := getCreateTablesSql(commonColumnSql)
// now get sql to populate the tables
insertSql := getTableInsertSql(workspaceResources)
sql := []string{createSql, insertSql}
// execute the query, passing 'true' to disable the spinner
_, err := client.ExecuteSync(context.Background(), strings.Join(sql, "\n"), true)
if err != nil {
return fmt.Errorf("failed to create reflection tables: %v", err)
}
client.loadSchema()
return nil
}
func getCreateTablesSql(commonColumnSql []string) string {
var createSql []string
createSql = append(createSql, getTableCreateSqlForResource(modconfig.Control{}, constants.ReflectionTableControl, commonColumnSql))
createSql = append(createSql, getTableCreateSqlForResource(modconfig.Query{}, constants.ReflectionTableQuery, commonColumnSql))
createSql = append(createSql, getTableCreateSqlForResource(modconfig.Benchmark{}, constants.ReflectionTableBenchmark, commonColumnSql))
createSql = append(createSql, getTableCreateSqlForResource(modconfig.Mod{}, constants.ReflectionTableMod, commonColumnSql))
return strings.Join(createSql, "\n")
}
func getClearTablesSql() string {
var clearSql []string
for _, t := range constants.ReflectionTableNames() {
clearSql = append(clearSql, fmt.Sprintf("delete from %s;", t))
}
return strings.Join(clearSql, "\n")
}
func getTableInsertSql(workspaceResources *modconfig.WorkspaceResourceMaps) string {
var insertSql []string
// the maps will have the same resource keyed by long and short name - avoid dupes
resourcesAdded := make(map[string]bool)
for _, control := range workspaceResources.ControlMap {
if _, added := resourcesAdded[control.Name()]; !added {
resourcesAdded[control.Name()] = true
insertSql = append(insertSql, getTableInsertSqlForResource(control, constants.ReflectionTableControl))
}
}
for _, query := range workspaceResources.QueryMap {
if _, added := resourcesAdded[query.Name()]; !added {
resourcesAdded[query.Name()] = true
insertSql = append(insertSql, getTableInsertSqlForResource(query, constants.ReflectionTableQuery))
}
}
for _, benchmark := range workspaceResources.BenchmarkMap {
if _, added := resourcesAdded[benchmark.Name()]; !added {
resourcesAdded[benchmark.Name()] = true
insertSql = append(insertSql, getTableInsertSqlForResource(benchmark, constants.ReflectionTableBenchmark))
}
}
for _, mod := range workspaceResources.ModMap {
if _, added := resourcesAdded[mod.Name()]; !added {
resourcesAdded[mod.Name()] = true
insertSql = append(insertSql, getTableInsertSqlForResource(mod, constants.ReflectionTableMod))
}
}
return strings.Join(insertSql, "\n")
}
func getTableCreateSqlForResource(s interface{}, tableName string, commonColumnSql []string) string {
columnDefinitions := append(commonColumnSql, getColumnDefinitions(s)...)
tableSql := fmt.Sprintf(`create temp table %s (
%s
);`, tableName, strings.Join(columnDefinitions, ",\n"))
return tableSql
}
// get the sql column definitions for tagged properties of the item
func getColumnDefinitions(item interface{}) []string {
t := reflect.TypeOf(item)
var columnDef []string
val := reflect.ValueOf(item)
for i := 0; i < val.NumField(); i++ {
fieldName := val.Type().Field(i).Name
field, _ := t.FieldByName(fieldName)
column, columnType, ok := getColumnTagValues(field)
if !ok {
continue
}
columnDef = append(columnDef, fmt.Sprintf(" %s %s", column, columnType))
}
return columnDef
}
func getColumnTagValues(field reflect.StructField) (string, string, bool) {
columnTag, ok := field.Tag.Lookup(TagColumn)
if !ok {
return "", "", false
}
split := strings.Split(columnTag, ",")
if len(split) != 2 {
return "", "", false
}
column := split[0]
columnType := split[1]
return column, columnType, true
}
func getTableInsertSqlForResource(item modconfig.ResourceWithMetadata, tableName string) string {
// for each item there is core reflection data (i.e. reflection resource all items have)
// and item specific reflection data
// get the core reflection data values
valuesCore, columnsCore := getColumnValues(item.GetMetadata())
// get item specific reflection data values from the item
valuesItem, columnsItem := getColumnValues(item)
columns := append(columnsCore, columnsItem...)
values := append(valuesCore, valuesItem...)
insertSql := fmt.Sprintf(`insert into %s (%s) values(%s);`, tableName, strings.Join(columns, ","), strings.Join(values, ","))
return insertSql
}
// use reflection to evaluate the column names and values from item - return as 2 separate arrays
func getColumnValues(item interface{}) ([]string, []string) {
if item == nil {
return nil, nil
}
var columns, values []string
// dereference item in vcase it is a pointer
item = helpers.DereferencePointer(item)
val := reflect.ValueOf(helpers.DereferencePointer(item))
t := reflect.TypeOf(item)
for i := 0; i < val.NumField(); i++ {
fieldName := val.Type().Field(i).Name
field, _ := t.FieldByName(fieldName)
column, columnType, ok := getColumnTagValues(field)
if !ok {
continue
}
value, ok := helpers.GetFieldValueFromInterface(item, fieldName)
// all fields will be pointers
value = helpers.DereferencePointer(value)
if !ok || value == nil {
continue
}
// pgValue escapes values, and for json columns, converts them into escaped JSON
// ignore JSON conversion errors - trust that array values read from hcl will be convertable
formattedValue, _ := pgValue(value, columnType)
values = append(values, formattedValue)
columns = append(columns, column)
}
return values, columns
}
// convert the value into a postgres format value which can used in an insert statement
func pgValue(item interface{}, columnsType string) (string, error) {
switch columnsType {
case "jsonb":
jsonBytes, err := json.Marshal(reflect.ValueOf(item).Interface())
if err != nil {
return "", err
}
res := PgEscapeString(fmt.Sprintf(`%s`, string(jsonBytes)))
return res, nil
default:
return PgEscapeString(typeHelpers.ToString(item)), nil
}
}