Commit 5316eaa975d589f616fb0b5fabc5703356ca5e6f

Authored by Marko Tikvić
1 parent ec400817db
Exists in master and in 1 other branch v2

ora v4 bug with utf-8 clob

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