Blame view

tables_utility.go 2.67 KB
64041a2ea   Marko Tikvić   first commit
1
2
3
4
  package restutility
  
  import (
  	"encoding/json"
64041a2ea   Marko Tikvić   first commit
5
  	"errors"
39765a430   Marko Tikvić   added list and se...
6
7
8
9
  	"gopkg.in/rana/ora.v3"
  	"io"
  	"io/ioutil"
  	"fmt"
64041a2ea   Marko Tikvić   first commit
10
11
12
13
14
15
16
17
18
19
  )
  
  type TableConfig struct {
  	Tables []Table
  }
  
  type Table struct {
  	TableType    string             `json:"tableType"`
  	Translations []TableTranslation `json:"translations"`
  	TableFields  []Field            `json:"tableFields"`
4994f13a4   Marko Tikvić   correlationFields...
20
  	Correlations []CorrelationField `json:"correlationFields"`
64041a2ea   Marko Tikvić   first commit
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
  	IdField      string             `json:"idField"`
  }
  
  type CorrelationField struct {
  	Result   string   `json:"result"`
  	Elements []string `json:"elements"`
  	Type     string   `json:"type"`
  }
  
  type TableTranslation struct {
  	Language     string            `json:"language"`
  	FieldsLabels map[string]string `json:"fieldsLabels"`
  }
  
  func (tl TableConfig) LoadTranslations(tableType string) LangMap {
  	translations := make(LangMap, 0)
  
  	for _, table := range tl.Tables {
  		if tableType == table.TableType {
  			for _, t := range table.Translations {
  				translations[t.Language] = t.FieldsLabels
  			}
  		}
  	}
  
  	return translations
  }
  
  func (tl TableConfig) LoadFields(tableType string) []Field {
  	fields := make([]Field, 0)
  
  	for _, table := range tl.Tables {
  		if tableType == table.TableType {
  			for _, f := range table.TableFields {
  				fields = append(fields, f)
  			}
  		}
  	}
  
  	return fields
  }
  
  func (tl TableConfig) LoadIdField(tableType string) string {
  	for _, table := range tl.Tables {
  		if tableType == table.TableType {
  			return table.IdField
  		}
  	}
  	return ""
  }
  
  func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
  	resp := make([]CorrelationField, 0)
  
  	for _, table := range tl.Tables {
  		if tableType == table.TableType {
  			for _, f := range table.Correlations {
  				resp = append(resp, f)
  			}
  		}
  	}
  
  	return resp
  }
  
  var _tables TableConfig
  var _prevProject string
39765a430   Marko Tikvić   added list and se...
88
89
  func InitTables(db *ora.Ses, project string) error {
  	jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project))
90fd36e9b   Marko Tikvić   resolved some dep...
90
  	json.Unmarshal(jsonbuf, &_tables.Tables)
90fd36e9b   Marko Tikvić   resolved some dep...
91
  	if len(_tables.Tables) == 0 {
64041a2ea   Marko Tikvić   first commit
92
93
  		return errors.New("tables config is corrupt")
  	}
64041a2ea   Marko Tikvić   first commit
94
95
  	return nil
  }
39765a430   Marko Tikvić   added list and se...
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
  func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) {
  	stmt, err := db.Prep(`SELECT
  		JSON_CLOB
  		FROM TABLES_CONFIG
  		WHERE PROJEKAT` + project, ora.S)
  	defer stmt.Close()
  
  	if err != nil {
  		return nil, err
  	}
  
  	rset, err := stmt.Qry()
  	if err != nil {
  		return nil, err
  	}
  
  	bytes := make([]byte, 0)
  	if rset.Next() {
  		lob := rset.Row[0].(io.Reader)
  		bytes, err = ioutil.ReadAll(lob)
  		if err != nil {
  			fmt.Printf("mega error: %v
  ", err)
  		}
  	}
  
  	return bytes, nil
  }
8202fb0db   Marko Tikvić   json payload simp...
124
  func loadTable(table string) JSONParams {
64041a2ea   Marko Tikvić   first commit
125
  	return NewJSONParams(_tables.LoadTranslations(table),
25e001550   Marko Tikvić   exported everything
126
127
128
  		_tables.LoadFields(table),
  		_tables.LoadIdField(table),
  		_tables.LoadCorrelations(table))
64041a2ea   Marko Tikvić   first commit
129
  }