Commit ec400817dbc08c4fdd9aa8b4e0cc6afd3bbc2161
1 parent
1d0f615536
Exists in
master
and in
1 other branch
Ora v4 working
Showing
1 changed file
with
0 additions
and
4 deletions
Show diff stats
json_utility.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | ||
5 | "net/http" | 4 | "net/http" |
6 | "encoding/json" | 5 | "encoding/json" |
7 | "errors" | 6 | "errors" |
8 | "io" | 7 | "io" |
9 | //"io/ioutil" | 8 | //"io/ioutil" |
10 | "sync" | 9 | "sync" |
11 | 10 | ||
12 | //"gopkg.in/rana/ora.v3" | 11 | //"gopkg.in/rana/ora.v3" |
13 | "gopkg.in/rana/ora.v4" | 12 | "gopkg.in/rana/ora.v4" |
14 | ) | 13 | ) |
15 | 14 | ||
16 | var mu = &sync.Mutex{} | 15 | var mu = &sync.Mutex{} |
17 | var payloads []payloadBuff | 16 | var payloads []payloadBuff |
18 | 17 | ||
19 | type LangMap map[string]map[string]string | 18 | type LangMap map[string]map[string]string |
20 | 19 | ||
21 | type Field struct { | 20 | type Field struct { |
22 | Parameter string `json:"param"` | 21 | Parameter string `json:"param"` |
23 | Type string `json:"type"` | 22 | Type string `json:"type"` |
24 | Visible bool `json:"visible"` | 23 | Visible bool `json:"visible"` |
25 | Editable bool `json:"editable"` | 24 | Editable bool `json:"editable"` |
26 | } | 25 | } |
27 | 26 | ||
28 | type CorrelationField struct { | 27 | type CorrelationField struct { |
29 | Result string `json:"result"` | 28 | Result string `json:"result"` |
30 | Elements []string `json:"elements"` | 29 | Elements []string `json:"elements"` |
31 | Type string `json:"type"` | 30 | Type string `json:"type"` |
32 | } | 31 | } |
33 | 32 | ||
34 | type Translation struct { | 33 | type Translation struct { |
35 | Language string `json:"language"` | 34 | Language string `json:"language"` |
36 | FieldsLabels map[string]string `json:"fieldsLabels"` | 35 | FieldsLabels map[string]string `json:"fieldsLabels"` |
37 | } | 36 | } |
38 | 37 | ||
39 | type payloadBuff struct { | 38 | type payloadBuff struct { |
40 | Type string `json:"tableType"` | 39 | Type string `json:"tableType"` |
41 | Method string `json:"method"` | 40 | Method string `json:"method"` |
42 | Params map[string]string `json:"params"` | 41 | Params map[string]string `json:"params"` |
43 | Lang []Translation `json:"lang"` | 42 | Lang []Translation `json:"lang"` |
44 | Fields []Field `json:"fields"` | 43 | Fields []Field `json:"fields"` |
45 | Correlations []CorrelationField `json:"correlationFields"` | 44 | Correlations []CorrelationField `json:"correlationFields"` |
46 | IdField string `json:"idField"` | 45 | IdField string `json:"idField"` |
47 | 46 | ||
48 | // Data can only hold slices of any type. It can't be used for itteration | 47 | // Data can only hold slices of any type. It can't be used for itteration |
49 | Data interface{} `json:"data"` | 48 | Data interface{} `json:"data"` |
50 | } | 49 | } |
51 | 50 | ||
52 | type Payload struct { | 51 | type Payload struct { |
53 | Method string `json:"method"` | 52 | Method string `json:"method"` |
54 | Params map[string]string `json:"params"` | 53 | Params map[string]string `json:"params"` |
55 | Lang []Translation `json:"lang"` | 54 | Lang []Translation `json:"lang"` |
56 | Fields []Field `json:"fields"` | 55 | Fields []Field `json:"fields"` |
57 | Correlations []CorrelationField `json:"correlationFields"` | 56 | Correlations []CorrelationField `json:"correlationFields"` |
58 | IdField string `json:"idField"` | 57 | IdField string `json:"idField"` |
59 | 58 | ||
60 | // Data can only hold slices of any type. It can't be used for itteration | 59 | // Data can only hold slices of any type. It can't be used for itteration |
61 | Data interface{} `json:"data"` | 60 | Data interface{} `json:"data"` |
62 | } | 61 | } |
63 | 62 | ||
64 | // NewPayload returs a payload sceleton for provided table. | 63 | // NewPayload returs a payload sceleton for provided table. |
65 | func NewPayload(r *http.Request, table string) Payload { | 64 | func NewPayload(r *http.Request, table string) Payload { |
66 | var pload Payload | 65 | var pload Payload |
67 | 66 | ||
68 | pload.Method = r.Method + " " + r.URL.Path | 67 | pload.Method = r.Method + " " + r.URL.Path |
69 | if table != "" { | 68 | if table != "" { |
70 | pload.Params = make(map[string]string, 0) | 69 | pload.Params = make(map[string]string, 0) |
71 | pload.Lang = translations(table) | 70 | pload.Lang = translations(table) |
72 | pload.Fields = fields(table) | 71 | pload.Fields = fields(table) |
73 | pload.IdField = id(table) | 72 | pload.IdField = id(table) |
74 | pload.Correlations = correlations(table) | 73 | pload.Correlations = correlations(table) |
75 | } | 74 | } |
76 | return pload | 75 | return pload |
77 | } | 76 | } |
78 | 77 | ||
79 | // DeliverPayload encodes payload to w. | 78 | // DeliverPayload encodes payload to w. |
80 | func DeliverPayload(w http.ResponseWriter, payload Payload) { | 79 | func DeliverPayload(w http.ResponseWriter, payload Payload) { |
81 | json.NewEncoder(w).Encode(payload) | 80 | json.NewEncoder(w).Encode(payload) |
82 | payload.Data = nil | 81 | payload.Data = nil |
83 | } | 82 | } |
84 | 83 | ||
85 | // translations returns a slice of translations for a payload/table of ptype type. | 84 | // translations returns a slice of translations for a payload/table of ptype type. |
86 | func translations(ptype string) []Translation { | 85 | func translations(ptype string) []Translation { |
87 | var translations []Translation | 86 | var translations []Translation |
88 | 87 | ||
89 | for _, pload := range payloads { | 88 | for _, pload := range payloads { |
90 | if pload.Type == ptype { | 89 | if pload.Type == ptype { |
91 | for _, t := range pload.Lang { | 90 | for _, t := range pload.Lang { |
92 | translations = append(translations, Translation{ | 91 | translations = append(translations, Translation{ |
93 | Language: t.Language, | 92 | Language: t.Language, |
94 | FieldsLabels: t.FieldsLabels, | 93 | FieldsLabels: t.FieldsLabels, |
95 | }) | 94 | }) |
96 | } | 95 | } |
97 | } | 96 | } |
98 | } | 97 | } |
99 | 98 | ||
100 | return translations | 99 | return translations |
101 | } | 100 | } |
102 | 101 | ||
103 | // fields returns a slice of fields for a payload/table of ptype type. | 102 | // fields returns a slice of fields for a payload/table of ptype type. |
104 | func fields(ptype string) []Field { | 103 | func fields(ptype string) []Field { |
105 | var fields []Field | 104 | var fields []Field |
106 | 105 | ||
107 | for _, pload := range payloads { | 106 | for _, pload := range payloads { |
108 | if pload.Type == ptype { | 107 | if pload.Type == ptype { |
109 | for _, f := range pload.Fields { | 108 | for _, f := range pload.Fields { |
110 | fields = append(fields, f) | 109 | fields = append(fields, f) |
111 | } | 110 | } |
112 | } | 111 | } |
113 | } | 112 | } |
114 | 113 | ||
115 | return fields | 114 | return fields |
116 | } | 115 | } |
117 | 116 | ||
118 | // id returns the name of ID field of a payload/table of ptype type. | 117 | // id returns the name of ID field of a payload/table of ptype type. |
119 | func id(ptype string) string { | 118 | func id(ptype string) string { |
120 | for _, pload := range payloads { | 119 | for _, pload := range payloads { |
121 | if pload.Type == ptype { | 120 | if pload.Type == ptype { |
122 | return pload.IdField | 121 | return pload.IdField |
123 | } | 122 | } |
124 | } | 123 | } |
125 | return "" | 124 | return "" |
126 | } | 125 | } |
127 | 126 | ||
128 | // correlations returns a slice of correlation fields for a payload/table of ptype type. | 127 | // correlations returns a slice of correlation fields for a payload/table of ptype type. |
129 | func correlations(ptype string) []CorrelationField { | 128 | func correlations(ptype string) []CorrelationField { |
130 | var corr []CorrelationField | 129 | var corr []CorrelationField |
131 | 130 | ||
132 | for _, pload := range payloads { | 131 | for _, pload := range payloads { |
133 | if pload.Type == ptype { | 132 | if pload.Type == ptype { |
134 | for _, c := range pload.Correlations { | 133 | for _, c := range pload.Correlations { |
135 | corr = append(corr, c) | 134 | corr = append(corr, c) |
136 | } | 135 | } |
137 | } | 136 | } |
138 | } | 137 | } |
139 | 138 | ||
140 | return corr | 139 | return corr |
141 | } | 140 | } |
142 | 141 | ||
143 | // InitTables loads all payloads in the payloads variable. | 142 | // InitTables loads all payloads in the payloads variable. |
144 | // Returns an error if it fails. | 143 | // Returns an error if it fails. |
145 | func InitTables(db *ora.Ses, project string) error { | 144 | func InitTables(db *ora.Ses, project string) error { |
146 | jsonbuf, err := fetchJSON(db, project) | 145 | jsonbuf, err := fetchJSON(db, project) |
147 | if err != nil { | 146 | if err != nil { |
148 | return err | 147 | return err |
149 | } | 148 | } |
150 | 149 | ||
151 | mu.Lock() | 150 | mu.Lock() |
152 | defer mu.Unlock() | 151 | defer mu.Unlock() |
153 | json.Unmarshal([]byte(jsonbuf), &payloads) | 152 | json.Unmarshal([]byte(jsonbuf), &payloads) |
154 | if len(payloads) == 0 { | 153 | if len(payloads) == 0 { |
155 | return errors.New("tables config is corrupt") | 154 | return errors.New("tables config is corrupt") |
156 | } | 155 | } |
157 | return nil | 156 | return nil |
158 | } | 157 | } |
159 | 158 | ||
160 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. | 159 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. |
161 | // Returns an error if it fails. | 160 | // Returns an error if it fails. |
162 | func fetchJSON(db *ora.Ses, project string) (string, error) { | 161 | func fetchJSON(db *ora.Ses, project string) (string, error) { |
163 | stmt, err := db.Prep(`SELECT JSON_CLOB FROM TABLES_CONFIG WHERE PROJEKAT` + EqualQuotes(project), ora.S) | 162 | stmt, err := db.Prep(`SELECT JSON_CLOB FROM TABLES_CONFIG WHERE PROJEKAT` + EqualQuotes(project), ora.S) |
164 | defer stmt.Close() | 163 | defer stmt.Close() |
165 | if err != nil { | 164 | if err != nil { |
166 | return "", err | 165 | return "", err |
167 | } | 166 | } |
168 | fmt.Println("prep passes") | ||
169 | 167 | ||
170 | rset, err := stmt.Qry() | 168 | rset, err := stmt.Qry() |
171 | if err != nil { | 169 | if err != nil { |
172 | fmt.Println(err) | ||
173 | return "", err | 170 | return "", err |
174 | } | 171 | } |
175 | fmt.Println("query passes") | ||
176 | 172 | ||
177 | var data string | 173 | var data string |
178 | if rset.Next() { | 174 | if rset.Next() { |
179 | data = rset.Row[0].(string) | 175 | data = rset.Row[0].(string) |
180 | } | 176 | } |
181 | 177 | ||
182 | return data, nil | 178 | return data, nil |
183 | } | 179 | } |
184 | 180 | ||
185 | // DecodeJSON decodes JSON data from r to v. | 181 | // DecodeJSON decodes JSON data from r to v. |
186 | // Returns an error if it fails. | 182 | // Returns an error if it fails. |
187 | func DecodeJSON(r io.Reader, v interface{}) error { | 183 | func DecodeJSON(r io.Reader, v interface{}) error { |
188 | return json.NewDecoder(r).Decode(v) | 184 | return json.NewDecoder(r).Decode(v) |
189 | } | 185 | } |
190 | 186 |