Blame view

list_config.go 14.3 KB
8bc396eb9   Marko Tikvić   reverted changes
1
  package webutility
79071a5d4   Marko Tikvić   Using database/sq...
2
3
4
5
  import (
  	"database/sql"
  	"fmt"
  )
8bc396eb9   Marko Tikvić   reverted changes
6
7
8
9
10
11
  
  type ListOptions struct {
  	GlobalFilter  bool   `json:"globalFilter"`
  	LocalFilters  bool   `json:"localFilters"`
  	RemoteFilters bool   `json:"remoteFilters"`
  	Pagination    bool   `json:"pagination"`
79071a5d4   Marko Tikvić   Using database/sq...
12
  	PageSize      uint32 `json:"pageSize"`
8bc396eb9   Marko Tikvić   reverted changes
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
  	Pivot         bool   `json:"pivot"`
  	Detail        bool   `json:"detail"`
  	Total         bool   `json:"total"`
  }
  
  type ListFilter struct {
  	Position       uint32   `json:"-"`
  	ObjectType     string   `json:"-"`
  	FiltersField   string   `json:"filtersField"`
  	DefaultValues  string   `json:"defaultValues"`
  	FiltersType    string   `json:"filtersType"`
  	FiltersLabel   string   `json:"filtersLabel"`
  	DropdownConfig Dropdown `json:"dropdownConfig"`
  }
  
  type Dropdown struct {
  	ObjectType   string `json:"objectType"`
  	FiltersField string `json:"filtersField"`
  	IDField      string `json:"idField"`
  	LabelField   string `json:"labelField"`
  }
  
  type ListGraph struct {
  	ObjectType string `json:"objectType"`
  	X          string `json:"xField"`
  	Y          string `json:"yField"`
  	GroupField string `json:"groupField"`
  	Label      string `json:"label"`
  }
  
  type ListActions struct {
087f8fb21   Marko Tikvić   expanded list con...
44
45
46
47
48
49
50
  	Create    bool `json:"create"`
  	Update    bool `json:"update"`
  	Delete    bool `json:"delete"`
  	Export    bool `json:"export"`
  	Print     bool `json:"print"`
  	Graph     bool `json:"graph"`
  	LiveGraph bool `json:"liveGraph"`
8bc396eb9   Marko Tikvić   reverted changes
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
  }
  
  type ListNavNode struct {
  	ObjectType        string `json:"objectType"`
  	LabelField        string `json:"label"`
  	Icon              string `json:"icon"`
  	ParentObjectType  string `json:"parentObjectType"`
  	ParentIDField     string `json:"parentIdField"`
  	ParentFilterField string `json:"parentFilterField"`
  }
  
  type ListParentNode struct {
  	ObjectType  string `json:"objectType"`
  	LabelField  string `json:"labelField"`
  	FilterField string `json:"filterField"`
  }
  
  type ListPivot struct {
  	ObjectType    string `json:"objectType"`
  	GroupField    string `json:"groupField"`
  	DistinctField string `json:"distinctField"`
  	Value         string `json:"valueField"`
  }
  
  type ListDetails struct {
  	ObjectType        string `json:"objectType"`
  	ParentObjectType  string `json:"parentObjectType"`
  	ParentFilterField string `json:"parentFilterField"`
  	SingleDetail      bool   `json:"singleDetail"`
  }
087f8fb21   Marko Tikvić   expanded list con...
81
82
83
84
85
  type ListLiveGraph struct {
  	ObjectType  string `json:"objectType"`
  	ValueFields string `json:"valueFields"`
  	LabelFields string `json:"labelFields"`
  }
8bc396eb9   Marko Tikvić   reverted changes
86
87
88
89
90
91
92
93
94
95
96
97
98
  type ListConfig struct {
  	ObjectType string           `json:"objectType"`
  	Title      string           `json:"title"`
  	LazyLoad   bool             `json:"lazyLoad"`
  	InlineEdit bool             `json:"inlineEdit"`
  	Options    ListOptions      `json:"options"`
  	Filters    []ListFilter     `json:"defaultFilters"`
  	Graphs     []ListGraph      `json:"graphs"`
  	Actions    ListActions      `json:"actions"`
  	Parent     []ListParentNode `json:"parent"`
  	Navigation []ListNavNode    `json:"navigation"`
  	Pivots     []ListPivot      `json:"pivots"`
  	Details    ListDetails      `json:"details"`
087f8fb21   Marko Tikvić   expanded list con...
99
  	LiveGraph  ListLiveGraph    `json:"liveGraphs"`
8bc396eb9   Marko Tikvić   reverted changes
100
101
102
103
  }
  
  // GetListConfig returns list configuration for the provided object type for the front-end application
  // or an error if it fails.
79071a5d4   Marko Tikvić   Using database/sq...
104
  func GetListConfig(db *sql.DB, objType string) (ListConfig, error) {
8bc396eb9   Marko Tikvić   reverted changes
105
106
107
108
109
110
111
112
113
114
115
116
  	resp := newDefaultList(objType)
  	var err error
  
  	err = setListParams(db, &resp, objType)
  	resp.Navigation, err = getListNavigation(db, objType)
  	resp.Actions, err = getListActions(db, objType)
  	resp.Filters, err = getListFilters(db, objType)
  	resp.Options, err = getListOptions(db, objType)
  	resp.Parent, err = getListParent(db, objType)
  	resp.Graphs, err = getListGraph(db, objType)
  	resp.Pivots, err = getListPivot(db, objType)
  	resp.Details, err = getListDetails(db, objType)
087f8fb21   Marko Tikvić   expanded list con...
117
  	resp.LiveGraph, err = getListLiveGraph(db, objType)
8bc396eb9   Marko Tikvić   reverted changes
118
119
120
121
122
123
124
125
126
127
  
  	if err != nil {
  		return ListConfig{}, err
  	}
  
  	return resp, nil
  }
  
  // GetListConfigObjectIDField takes in database connection and an object type and it returns the
  // ID field name for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
128
  func GetListConfigObjectIDField(db *sql.DB, otype string) string {
8bc396eb9   Marko Tikvić   reverted changes
129
  	var resp string
8bc396eb9   Marko Tikvić   reverted changes
130

79071a5d4   Marko Tikvić   Using database/sq...
131
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
132
133
  		ID_FIELD
  		FROM LIST_CONFIG_ID_FIELD
79071a5d4   Marko Tikvić   Using database/sq...
134
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", otype))
8bc396eb9   Marko Tikvić   reverted changes
135
136
137
  	if err != nil {
  		return ""
  	}
79071a5d4   Marko Tikvić   Using database/sq...
138
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
139

79071a5d4   Marko Tikvić   Using database/sq...
140
141
  	if rows.Next() {
  		rows.Scan(&resp)
8bc396eb9   Marko Tikvić   reverted changes
142
  	}
79071a5d4   Marko Tikvić   Using database/sq...
143
  	if rows.Err() != nil {
8bc396eb9   Marko Tikvić   reverted changes
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  		return ""
  	}
  
  	return resp
  }
  
  // newDefaultList returns default configuration for the provided object type.
  func newDefaultList(objType string) ListConfig {
  	list := ListConfig{
  		ObjectType: objType,
  		Title:      objType,
  		LazyLoad:   false,
  		Options: ListOptions{
  			GlobalFilter:  true,
  			LocalFilters:  true,
  			RemoteFilters: false,
  			Pagination:    true,
  			PageSize:      20,
  		},
  		Filters: nil,
  		Actions: ListActions{
087f8fb21   Marko Tikvić   expanded list con...
165
166
167
168
169
170
171
  			Create:    false,
  			Update:    false,
  			Delete:    false,
  			Export:    false,
  			Print:     false,
  			Graph:     false,
  			LiveGraph: false,
8bc396eb9   Marko Tikvić   reverted changes
172
173
174
175
176
177
178
179
180
  		},
  		Parent:     nil,
  		Navigation: nil,
  	}
  
  	return list
  }
  
  // setListParams sets the default parameters of the provided configuration list for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
181
182
  func setListParams(db *sql.DB, list *ListConfig, objType string) error {
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
183
184
  		OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT
  		FROM LIST_CONFIG
79071a5d4   Marko Tikvić   Using database/sq...
185
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
186
187
188
  	if err != nil {
  		return err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
189
190
191
192
193
  	defer rows.Close()
  	if rows.Next() {
  		otype, title := "", ""
  		lazyLoad, inlineEdit := 0, 0
  		rows.Scan(&otype, &title, &lazyLoad, &inlineEdit)
8bc396eb9   Marko Tikvić   reverted changes
194

8bc396eb9   Marko Tikvić   reverted changes
195
196
197
  		if otype != "" {
  			list.ObjectType = otype
  		}
8bc396eb9   Marko Tikvić   reverted changes
198
199
200
  		if title != "" {
  			list.Title = title
  		}
79071a5d4   Marko Tikvić   Using database/sq...
201
202
  		list.LazyLoad = lazyLoad != 0
  		list.InlineEdit = inlineEdit != 0
8bc396eb9   Marko Tikvić   reverted changes
203
  	}
79071a5d4   Marko Tikvić   Using database/sq...
204
205
  	if rows.Err() != nil {
  		return rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
206
207
208
209
210
  	}
  	return nil
  }
  
  // getListNavigation returns list navigation node slice for the provided objectType.
79071a5d4   Marko Tikvić   Using database/sq...
211
  func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) {
8bc396eb9   Marko Tikvić   reverted changes
212
  	resp := make([]ListNavNode, 0)
79071a5d4   Marko Tikvić   Using database/sq...
213
214
  	rows, err := db.Query(`SELECT
  		a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD
8bc396eb9   Marko Tikvić   reverted changes
215
216
  		FROM LIST_CONFIG_NAVIGATION b
  		JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID
79071a5d4   Marko Tikvić   Using database/sq...
217
218
  		WHERE b.LIST_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", listObjType) +
  		` ORDER BY b.RB ASC`)
8bc396eb9   Marko Tikvić   reverted changes
219
220
221
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
222
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
223

79071a5d4   Marko Tikvić   Using database/sq...
224
225
226
227
228
  	var node ListNavNode
  	for rows.Next() {
  		rows.Scan(&node.ObjectType, &node.ParentObjectType, &node.LabelField, &node.Icon,
  			&node.ParentFilterField, &node.ParentIDField)
  		resp = append(resp, node)
8bc396eb9   Marko Tikvić   reverted changes
229
  	}
79071a5d4   Marko Tikvić   Using database/sq...
230
231
  	if rows.Err() != nil {
  		return nil, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
232
233
234
235
236
237
  	}
  
  	return resp, nil
  }
  
  // getListActions returns list actions for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
238
  func getListActions(db *sql.DB, objType string) (ListActions, error) {
8bc396eb9   Marko Tikvić   reverted changes
239
  	var resp ListActions
79071a5d4   Marko Tikvić   Using database/sq...
240
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
241
  		ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT,
087f8fb21   Marko Tikvić   expanded list con...
242
  		ACTION_PRINT, ACTION_GRAPH, ACTION_LIVE_GRAPH
8bc396eb9   Marko Tikvić   reverted changes
243
  		FROM LIST_CONFIG
79071a5d4   Marko Tikvić   Using database/sq...
244
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
245
246
247
  	if err != nil {
  		return ListActions{}, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
248
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
249

087f8fb21   Marko Tikvić   expanded list con...
250
  	var create, update, delete, export, print, graph, liveGraph uint32
79071a5d4   Marko Tikvić   Using database/sq...
251
  	if rows.Next() {
087f8fb21   Marko Tikvić   expanded list con...
252
  		rows.Scan(&create, &update, &delete, &export, &print, &graph, &liveGraph)
79071a5d4   Marko Tikvić   Using database/sq...
253
254
255
256
257
258
  		resp.Create = create != 0
  		resp.Update = update != 0
  		resp.Delete = delete != 0
  		resp.Export = export != 0
  		resp.Print = print != 0
  		resp.Graph = graph != 0
087f8fb21   Marko Tikvić   expanded list con...
259
  		resp.LiveGraph = liveGraph != 0
8bc396eb9   Marko Tikvić   reverted changes
260
  	}
79071a5d4   Marko Tikvić   Using database/sq...
261
262
  	if rows.Err() != nil {
  		return ListActions{}, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
263
264
265
266
267
  	}
  	return resp, nil
  }
  
  // getListFiters returns list filter slice for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
268
  func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) {
8bc396eb9   Marko Tikvić   reverted changes
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  	resp := make([]ListFilter, 0)
  	filtersFields, err := getFilterFieldsAndPosition(db, objType)
  	if err != nil {
  		return nil, err
  	}
  	for field, pos := range filtersFields {
  		filters, _ := getFiltersByFilterField(db, field)
  		for _, filter := range filters {
  			var f ListFilter
  			f.Position = pos
  			f.ObjectType = objType
  			f.FiltersField = field
  			f.DefaultValues = filter.DefaultValues
  			f.FiltersLabel = filter.Label
  			f.FiltersType = filter.Type
  			if filter.Type == "dropdown" {
  				f.DropdownConfig, err = getFilterDropdownConfig(db, field)
  				if err != nil {
  					return nil, err
  				}
  			}
  			resp = append(resp, f)
  		}
  	}
  
  	sortFilters(resp)
  
  	return resp, nil
  }
  
  // getFilterFieldsAndPosition returns a map of filter fields and their respective position in the menu.
79071a5d4   Marko Tikvić   Using database/sq...
300
  func getFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) {
8bc396eb9   Marko Tikvić   reverted changes
301
  	filtersField := make(map[string]uint32, 0)
79071a5d4   Marko Tikvić   Using database/sq...
302
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
303
304
  		FILTERS_FIELD, RB
  		FROM LIST_CONFIG_FILTERS
79071a5d4   Marko Tikvić   Using database/sq...
305
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
306
307
308
  	if err != nil {
  		return nil, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
309
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
310

79071a5d4   Marko Tikvić   Using database/sq...
311
312
313
314
315
  	for rows.Next() {
  		var field string
  		var rb uint32
  		rows.Scan(&field, &rb)
  		filtersField[field] = rb
8bc396eb9   Marko Tikvić   reverted changes
316
  	}
79071a5d4   Marko Tikvić   Using database/sq...
317
318
  	if rows.Err() != nil {
  		return nil, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
319
320
321
322
323
324
325
326
327
328
329
  	}
  	return filtersField, nil
  }
  
  type _filter struct {
  	DefaultValues string
  	Label         string
  	Type          string
  }
  
  // getFiltersByFilterField returns filter slice for the provided filter field.
79071a5d4   Marko Tikvić   Using database/sq...
330
  func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) {
8bc396eb9   Marko Tikvić   reverted changes
331
  	resp := make([]_filter, 0)
79071a5d4   Marko Tikvić   Using database/sq...
332
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
333
334
  		FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES
  		FROM LIST_FILTERS_FIELD
79071a5d4   Marko Tikvić   Using database/sq...
335
  		WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField))
8bc396eb9   Marko Tikvić   reverted changes
336
337
338
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
339
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
340

79071a5d4   Marko Tikvić   Using database/sq...
341
342
343
344
  	var f _filter
  	for rows.Next() {
  		rows.Scan(&f.Type, &f.Label, &f.DefaultValues)
  		resp = append(resp, f)
8bc396eb9   Marko Tikvić   reverted changes
345
  	}
79071a5d4   Marko Tikvić   Using database/sq...
346
347
  	if rows.Err() != nil {
  		return resp, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
348
349
350
351
352
  	}
  	return resp, nil
  }
  
  // getFilterDropdownConfig returns dropdown menu for the provided filter field.
79071a5d4   Marko Tikvić   Using database/sq...
353
  func getFilterDropdownConfig(db *sql.DB, filtersField string) (Dropdown, error) {
8bc396eb9   Marko Tikvić   reverted changes
354
  	var resp Dropdown
79071a5d4   Marko Tikvić   Using database/sq...
355
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
356
357
  		FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD
  		FROM LIST_DROPDOWN_FILTER
79071a5d4   Marko Tikvić   Using database/sq...
358
  		WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField))
8bc396eb9   Marko Tikvić   reverted changes
359
360
361
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
362
363
364
  	defer rows.Close()
  	if rows.Next() {
  		rows.Scan(&resp.FiltersField, &resp.ObjectType, &resp.IDField, &resp.LabelField)
8bc396eb9   Marko Tikvić   reverted changes
365
  	}
79071a5d4   Marko Tikvić   Using database/sq...
366
367
  	if rows.Err() != nil {
  		return resp, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
  	}
  	return resp, nil
  }
  
  // sortFilters bubble sorts provided filters slice by position field.
  func sortFilters(filters []ListFilter) {
  	done := false
  	var temp ListFilter
  	for !done {
  		done = true
  		for i := 0; i < len(filters)-1; i++ {
  			if filters[i].Position > filters[i+1].Position {
  				done = false
  				temp = filters[i]
  				filters[i] = filters[i+1]
  				filters[i+1] = temp
  			}
  		}
  	}
  }
  
  // getListGraph return list graph slice for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
390
  func getListGraph(db *sql.DB, objType string) ([]ListGraph, error) {
8bc396eb9   Marko Tikvić   reverted changes
391
  	resp := make([]ListGraph, 0)
79071a5d4   Marko Tikvić   Using database/sq...
392
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
393
394
  		OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL
  		FROM LIST_GRAPHS
79071a5d4   Marko Tikvić   Using database/sq...
395
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
396
397
398
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
399
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
400

79071a5d4   Marko Tikvić   Using database/sq...
401
402
403
404
  	var lg ListGraph
  	for rows.Next() {
  		rows.Scan(&lg.ObjectType, &lg.X, &lg.Y, &lg.GroupField, &lg.Label)
  		resp = append(resp, lg)
8bc396eb9   Marko Tikvić   reverted changes
405
  	}
79071a5d4   Marko Tikvić   Using database/sq...
406
407
  	if rows.Err() != nil {
  		return resp, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
408
409
410
411
412
  	}
  	return resp, nil
  }
  
  // getListOptions returns list options for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
413
  func getListOptions(db *sql.DB, objType string) (ListOptions, error) {
8bc396eb9   Marko Tikvić   reverted changes
414
  	var resp ListOptions
79071a5d4   Marko Tikvić   Using database/sq...
415
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
416
417
418
  		GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION,
  		PAGE_SIZE, PIVOT, DETAIL, TOTAL
  		FROM LIST_CONFIG
79071a5d4   Marko Tikvić   Using database/sq...
419
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
420
421
422
  	if err != nil {
  		return ListOptions{}, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  	defer rows.Close()
  	if rows.Next() {
  		var gfilter, lfilters, rfilters, pagination, pageSize, pivot, detail, total uint32
  		rows.Scan(&gfilter, &lfilters, &rfilters, &pagination, &pageSize, &pivot, &detail, &total)
  		resp.GlobalFilter = gfilter != 0
  		resp.LocalFilters = lfilters != 0
  		resp.RemoteFilters = rfilters != 0
  		resp.Pagination = pagination != 0
  		resp.PageSize = pageSize
  		resp.Pivot = pivot != 0
  		resp.Detail = detail != 0
  		resp.Total = total != 0
  	}
  	if rows.Err() != nil {
  		return ListOptions{}, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
438
439
440
441
442
  	}
  	return resp, nil
  }
  
  // getListParent returns list parent node slice for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
443
  func getListParent(db *sql.DB, objType string) ([]ListParentNode, error) {
8bc396eb9   Marko Tikvić   reverted changes
444
  	resp := make([]ListParentNode, 0)
79071a5d4   Marko Tikvić   Using database/sq...
445
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
446
447
  		PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD
  		FROM LIST_CONFIG_CHILD
79071a5d4   Marko Tikvić   Using database/sq...
448
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
449
450
451
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
452
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
453

79071a5d4   Marko Tikvić   Using database/sq...
454
455
456
457
  	var pnode ListParentNode
  	for rows.Next() {
  		rows.Scan(&pnode.ObjectType, &pnode.LabelField, &pnode.FilterField)
  		resp = append(resp, pnode)
8bc396eb9   Marko Tikvić   reverted changes
458
  	}
79071a5d4   Marko Tikvić   Using database/sq...
459
460
  	if rows.Err() != nil {
  		return nil, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
461
462
463
464
465
466
  	}
  
  	return resp, nil
  }
  
  // getListPivot list pivot slice for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
467
  func getListPivot(db *sql.DB, objType string) ([]ListPivot, error) {
8bc396eb9   Marko Tikvić   reverted changes
468
  	resp := make([]ListPivot, 0)
79071a5d4   Marko Tikvić   Using database/sq...
469
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
470
471
  		OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD
  		FROM LIST_PIVOTS
79071a5d4   Marko Tikvić   Using database/sq...
472
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
473
474
475
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
476
  	defer rows.Close()
8bc396eb9   Marko Tikvić   reverted changes
477

79071a5d4   Marko Tikvić   Using database/sq...
478
479
480
481
  	var p ListPivot
  	for rows.Next() {
  		rows.Scan(&p.ObjectType, &p.GroupField, &p.DistinctField, &p.Value)
  		resp = append(resp, p)
8bc396eb9   Marko Tikvić   reverted changes
482
  	}
79071a5d4   Marko Tikvić   Using database/sq...
483
484
  	if rows.Err() != nil {
  		return nil, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
485
486
487
488
489
490
  	}
  
  	return resp, nil
  }
  
  // getListDetails returns list details for the provided object type.
79071a5d4   Marko Tikvić   Using database/sq...
491
  func getListDetails(db *sql.DB, objType string) (ListDetails, error) {
8bc396eb9   Marko Tikvić   reverted changes
492
  	var resp ListDetails
79071a5d4   Marko Tikvić   Using database/sq...
493
  	rows, err := db.Query(`SELECT
8bc396eb9   Marko Tikvić   reverted changes
494
495
  		OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL
  		FROM LIST_CONFIG_DETAIL
79071a5d4   Marko Tikvić   Using database/sq...
496
  		WHERE PARENT_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
8bc396eb9   Marko Tikvić   reverted changes
497
498
499
  	if err != nil {
  		return resp, err
  	}
79071a5d4   Marko Tikvić   Using database/sq...
500
501
502
503
504
  	defer rows.Close()
  	if rows.Next() {
  		var singleDetail uint32
  		rows.Scan(&resp.ObjectType, &resp.ParentObjectType, &resp.ParentFilterField, &singleDetail)
  		resp.SingleDetail = singleDetail != 0
8bc396eb9   Marko Tikvić   reverted changes
505
  	}
79071a5d4   Marko Tikvić   Using database/sq...
506
507
  	if rows.Err() != nil {
  		return resp, rows.Err()
8bc396eb9   Marko Tikvić   reverted changes
508
509
510
511
  	}
  
  	return resp, nil
  }
087f8fb21   Marko Tikvić   expanded list con...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  
  // getListLiveGraph returns live graph for the provided object type.
  func getListLiveGraph(db *sql.DB, objType string) (ListLiveGraph, error) {
  	var resp ListLiveGraph
  	rows, err := db.Query(`SELECT
  		OBJECT_TYPE, VALUE_FIELDS, LABEL_FIELDS
  		FROM LIST_LIVE_GRAPH
  		WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
  	if err != nil {
  		return resp, err
  	}
  	defer rows.Close()
  	if rows.Next() {
  		rows.Scan(&resp.ObjectType, &resp.ValueFields, &resp.LabelFields)
  	}
  	if rows.Err() != nil {
  		return resp, rows.Err()
  	}
  
  	return resp, nil
  }