Commit 79071a5d4fc3720a184381f583e32a94181fec2b

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

Using database/sql package to make the package driver agnostic

Showing 3 changed files with 282 additions and 429 deletions   Show diff stats
1 1 package webutility
2 2  
3 3 import (
  4 + "database/sql"
4 5 "encoding/json"
5   - "errors"
6 6 "fmt"
7 7 "io"
8 8 "net/http"
9   - "os"
10 9 "sync"
11 10 "time"
12 11  
13 12 "git.to-net.rs/marko.tikvic/gologger"
14   -
15   - "gopkg.in/rana/ora.v4"
16 13 )
17 14  
18 15 var (
... ... @@ -20,7 +17,7 @@ var (
20 17 metadata = make(map[string]Payload)
21 18 updateQue = make(map[string][]byte)
22 19  
23   - metadataDB *ora.Ses
  20 + metadataDB *sql.DB
24 21 activeProject string
25 22  
26 23 inited bool
... ... @@ -68,8 +65,21 @@ type Payload struct {
68 65 Data interface{} `json:"data"`
69 66 }
70 67  
  68 +// NewPayload returs a payload sceleton for entity described with etype.
  69 +func NewPayload(r *http.Request, etype string) Payload {
  70 + pload := metadata[etype]
  71 + pload.Method = r.Method + " " + r.RequestURI
  72 + return pload
  73 +}
  74 +
  75 +// DecodeJSON decodes JSON data from r to v.
  76 +// Returns an error if it fails.
  77 +func DecodeJSON(r io.Reader, v interface{}) error {
  78 + return json.NewDecoder(r).Decode(v)
  79 +}
  80 +
71 81 // LoadPayloadsdetaData loads all payloads' information into 'metadata' variable.
72   -func LoadPayloadsMetadata(db *ora.Ses, project string) error {
  82 +func LoadPayloadsMetadata(db *sql.DB, project string) error {
73 83 metadataDB = db
74 84 activeProject = project
75 85  
... ... @@ -103,6 +113,119 @@ func QueEntityModelUpdate(entityType string, v interface{}) {
103 113 updateQue[entityType], _ = json.Marshal(v)
104 114 }
105 115  
  116 +func initMetadata(project string) error {
  117 + rows, err := metadataDB.Query(`select
  118 + entity_type,
  119 + metadata
  120 + from entities
  121 + where projekat = ` + fmt.Sprintf("'%s'", project))
  122 + if err != nil {
  123 + return err
  124 + }
  125 + defer rows.Close()
  126 +
  127 + count := 0
  128 + success := 0
  129 + if len(metadata) > 0 {
  130 + metadata = nil
  131 + }
  132 + metadata = make(map[string]Payload)
  133 + for rows.Next() {
  134 + var name, load string
  135 + rows.Scan(&name, &load)
  136 +
  137 + p := Payload{}
  138 + err := json.Unmarshal([]byte(load), &p)
  139 + if err != nil {
  140 + logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), load)
  141 + } else {
  142 + success++
  143 + metadata[name] = p
  144 + }
  145 + count++
  146 + }
  147 + perc := float32(success/count) * 100.0
  148 + logger.Log("loaded %d/%d (%.1f%%) entities\n", success, count, perc)
  149 +
  150 + return nil
  151 +}
  152 +
  153 +func hotload(n int) {
  154 + entityScan := make(map[string]int64)
  155 + firstCheck := true
  156 + for {
  157 + time.Sleep(time.Duration(n) * time.Second)
  158 + rows, err := metadataDB.Query(`select
  159 + ora_rowscn,
  160 + entity_type
  161 + from entities where projekat = ` + fmt.Sprintf("'%s'", activeProject))
  162 + if err != nil {
  163 + logger.Log("hotload failed: %v\n", err)
  164 + time.Sleep(time.Duration(n) * time.Second)
  165 + continue
  166 + }
  167 +
  168 + var toRefresh []string
  169 + for rows.Next() {
  170 + var scanID int64
  171 + var entity string
  172 + rows.Scan(&scanID, &entity)
  173 + oldID, ok := entityScan[entity]
  174 + if !ok || oldID != scanID {
  175 + entityScan[entity] = scanID
  176 + toRefresh = append(toRefresh, entity)
  177 + }
  178 + }
  179 + rows.Close()
  180 +
  181 + if rows.Err() != nil {
  182 + logger.Log("hotload rset error: %v\n", rows.Err())
  183 + time.Sleep(time.Duration(n) * time.Second)
  184 + continue
  185 + }
  186 +
  187 + if len(toRefresh) > 0 && !firstCheck {
  188 + mu.Lock()
  189 + refreshMetadata(toRefresh)
  190 + mu.Unlock()
  191 + }
  192 + if firstCheck {
  193 + firstCheck = false
  194 + }
  195 + }
  196 +}
  197 +
  198 +func refreshMetadata(entities []string) {
  199 + for _, e := range entities {
  200 + fmt.Printf("refreshing %s\n", e)
  201 + rows, err := metadataDB.Query(`select
  202 + metadata
  203 + from entities
  204 + where projekat = ` + fmt.Sprintf("'%s'", activeProject) +
  205 + ` and entity_type = ` + fmt.Sprintf("'%s'", e))
  206 +
  207 + if err != nil {
  208 + logger.Log("webutility: refresh: prep: %v\n", err)
  209 + rows.Close()
  210 + continue
  211 + }
  212 +
  213 + for rows.Next() {
  214 + var load string
  215 + rows.Scan(&load)
  216 + p := Payload{}
  217 + err := json.Unmarshal([]byte(load), &p)
  218 + if err != nil {
  219 + logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load)
  220 + } else {
  221 + metadata[e] = p
  222 + }
  223 + }
  224 + rows.Close()
  225 + }
  226 +}
  227 +
  228 +/*
106 229 func UpdateEntityModels(command string) (total, upd, add int, err error) {
107 230 if command != "force" && command != "missing" {
108 231 return total, 0, 0, errors.New("uknown command: " + command)
... ... @@ -195,152 +318,4 @@ func DeleteEntityModel(entityType string) error {
195 318 }
196 319 return err
197 320 }
198   -
199   -// NewPayload returs a payload sceleton for entity described with etype.
200   -func NewPayload(r *http.Request, etype string) Payload {
201   - pload := metadata[etype]
202   - pload.Method = r.Method + " " + r.RequestURI
203   - return pload
204   -}
205   -
206   -// DecodeJSON decodes JSON data from r to v.
207   -// Returns an error if it fails.
208   -func DecodeJSON(r io.Reader, v interface{}) error {
209   - return json.NewDecoder(r).Decode(v)
210   -}
211   -
212   -func initMetadata(project string) error {
213   - metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S))
214   - stmt, err := metadataDB.Prep(`select
215   - entity_type,
216   - metadata
217   - from entities
218   - where projekat = `+fmt.Sprintf("'%s'", project),
219   - ora.S,
220   - ora.S)
221   -
222   - defer stmt.Close()
223   - if err != nil {
224   - return err
225   - }
226   -
227   - rset, err := stmt.Qry()
228   - if err != nil {
229   - return err
230   - }
231   -
232   - count := 0
233   - success := 0
234   - if len(metadata) > 0 {
235   - metadata = nil
236   - }
237   - metadata = make(map[string]Payload)
238   - for rset.Next() {
239   - name := rset.Row[0].(string)
240   - load := []byte(rset.Row[1].(string))
241   -
242   - p := Payload{}
243   - err := json.Unmarshal(load, &p)
244   - if err != nil {
245   - logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), rset.Row[1].(string))
246   - } else {
247   - success++
248   - metadata[name] = p
249   - }
250   - count++
251   - }
252   - perc := float32(success/count) * 100.0
253   - logger.Log("loaded %d/%d (%.1f%%) entities\n", success, count, perc)
254   -
255   - return nil
256   -}
257   -
258   -func hotload(n int) {
259   - entityScan := make(map[string]int64)
260   - firstCheck := true
261   - for {
262   - time.Sleep(time.Duration(n) * time.Second)
263   - stmt, err := metadataDB.Prep(`select
264   - ora_rowscn,
265   - entity_type
266   - from entities where projekat = `+fmt.Sprintf("'%s'", activeProject),
267   - ora.I64,
268   - ora.S)
269   - if err != nil {
270   - logger.Log("hotload failed: %v\n", err)
271   - time.Sleep(time.Duration(n) * time.Second)
272   - continue
273   - }
274   -
275   - rset, err := stmt.Qry()
276   - if err != nil {
277   - fmt.Fprintf(os.Stderr, "hotload failed: %v\n", err)
278   - time.Sleep(time.Duration(n) * time.Second)
279   - continue
280   - }
281   -
282   - var toRefresh []string
283   - for rset.Next() {
284   - scanID := rset.Row[0].(int64)
285   - entity := rset.Row[1].(string)
286   - oldID, ok := entityScan[entity]
287   - if !ok || oldID != scanID {
288   - entityScan[entity] = scanID
289   - toRefresh = append(toRefresh, entity)
290   - }
291   - }
292   - stmt.Close()
293   -
294   - if rset.Err() != nil {
295   - logger.Log("hotload rset error: %v\n", rset.Err())
296   - time.Sleep(time.Duration(n) * time.Second)
297   - continue
298   - }
299   -
300   - if len(toRefresh) > 0 && !firstCheck {
301   - mu.Lock()
302   - refreshMetadata(toRefresh)
303   - mu.Unlock()
304   - }
305   - if firstCheck {
306   - firstCheck = false
307   - }
308   - }
309   -}
310   -
311   -func refreshMetadata(entities []string) {
312   - for _, e := range entities {
313   - fmt.Printf("refreshing %s\n", e)
314   - stmt, err := metadataDB.Prep(`select
315   - metadata
316   - from entities
317   - where projekat = `+fmt.Sprintf("'%s'", activeProject)+
318   - ` and entity_type = `+fmt.Sprintf("'%s'", e),
319   - ora.S)
320   -
321   - if err != nil {
322   - logger.Log("webutility: refresh: prep: %v\n", err)
323   - stmt.Close()
324   - continue
325   - }
326   -
327   - rset, err := stmt.Qry()
328   - if err != nil {
329   - fmt.Printf("webutility: refresh: query: %v\n", err)
330   - stmt.Close()
331   - continue
332   - }
333   -
334   - for rset.Next() {
335   - load := []byte(rset.Row[0].(string))
336   - p := Payload{}
337   - err := json.Unmarshal(load, &p)
338   - if err != nil {
339   - logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), rset.Row[0].(string))
340   - } else {
341   - metadata[e] = p
342   - }
343   - }
344   - stmt.Close()
345   - }
346   -}
  321 +*/
... ...
1 1 package webutility
2 2  
3   -import "gopkg.in/rana/ora.v4"
  3 +import (
  4 + "database/sql"
  5 + "fmt"
  6 +)
4 7  
5 8 type ListOptions struct {
6 9 GlobalFilter bool `json:"globalFilter"`
7 10 LocalFilters bool `json:"localFilters"`
8 11 RemoteFilters bool `json:"remoteFilters"`
9 12 Pagination bool `json:"pagination"`
10   - PageSize uint64 `json:"pageSize"`
  13 + PageSize uint32 `json:"pageSize"`
11 14 Pivot bool `json:"pivot"`
12 15 Detail bool `json:"detail"`
13 16 Total bool `json:"total"`
... ... @@ -93,7 +96,7 @@ type ListConfig struct {
93 96  
94 97 // GetListConfig returns list configuration for the provided object type for the front-end application
95 98 // or an error if it fails.
96   -func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
  99 +func GetListConfig(db *sql.DB, objType string) (ListConfig, error) {
97 100 resp := newDefaultList(objType)
98 101 var err error
99 102  
... ... @@ -116,29 +119,23 @@ func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
116 119  
117 120 // GetListConfigObjectIDField takes in database connection and an object type and it returns the
118 121 // ID field name for the provided object type.
119   -func GetListConfigObjectIDField(db *ora.Ses, otype string) string {
  122 +func GetListConfigObjectIDField(db *sql.DB, otype string) string {
120 123 var resp string
121   - var err error
122   - var stmt *ora.Stmt
123 124  
124   - stmt, err = db.Prep(`SELECT
  125 + rows, err := db.Query(`SELECT
125 126 ID_FIELD
126 127 FROM LIST_CONFIG_ID_FIELD
127   - WHERE OBJECT_TYPE = '`+otype+`'`,
128   - ora.S)
129   -
130   - defer stmt.Close()
131   -
  128 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", otype))
132 129 if err != nil {
133 130 return ""
134 131 }
  132 + defer rows.Close()
135 133  
136   - rset, err := stmt.Qry()
137   - if rset.Next() {
138   - resp = rset.Row[0].(string)
  134 + if rows.Next() {
  135 + rows.Scan(&resp)
139 136 }
140 137  
141   - if rset.Err() != nil {
  138 + if rows.Err() != nil {
142 139 return ""
143 140 }
144 141  
... ... @@ -175,121 +172,93 @@ func newDefaultList(objType string) ListConfig {
175 172 }
176 173  
177 174 // setListParams sets the default parameters of the provided configuration list for the provided object type.
178   -func setListParams(db *ora.Ses, list *ListConfig, objType string) error {
179   - var err error
180   - var stmt *ora.Stmt
181   - query := `SELECT
  175 +func setListParams(db *sql.DB, list *ListConfig, objType string) error {
  176 + rows, err := db.Query(`SELECT
182 177 OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT
183 178 FROM LIST_CONFIG
184   - WHERE OBJECT_TYPE = '` + objType + `'`
185   -
186   - stmt, err = db.Prep(query, ora.S, ora.S, ora.U32, ora.U32)
  179 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
187 180 if err != nil {
188 181 return err
189 182 }
190   - defer stmt.Close()
  183 + defer rows.Close()
  184 + if rows.Next() {
  185 + otype, title := "", ""
  186 + lazyLoad, inlineEdit := 0, 0
  187 + rows.Scan(&otype, &title, &lazyLoad, &inlineEdit)
191 188  
192   - rset, err := stmt.Qry()
193   - if err != nil {
194   - return err
195   - }
196   - if rset.Next() {
197   - otype := rset.Row[0].(string)
198 189 if otype != "" {
199 190 list.ObjectType = otype
200 191 }
201   -
202   - title := rset.Row[1].(string)
203 192 if title != "" {
204 193 list.Title = title
205 194 }
206   - list.LazyLoad = rset.Row[2].(uint32) != 0
207   - list.InlineEdit = rset.Row[3].(uint32) != 0
  195 + list.LazyLoad = lazyLoad != 0
  196 + list.InlineEdit = inlineEdit != 0
208 197 }
209   - if rset.Err() != nil {
210   - return rset.Err()
  198 + if rows.Err() != nil {
  199 + return rows.Err()
211 200 }
212 201 return nil
213 202 }
214 203  
215 204 // getListNavigation returns list navigation node slice for the provided objectType.
216   -func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) {
  205 +func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) {
217 206 resp := make([]ListNavNode, 0)
218   - var err error
219   - var stmt *ora.Stmt
220   - query := `SELECT
221   - a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD, b.RB
  207 + rows, err := db.Query(`SELECT
  208 + a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD
222 209 FROM LIST_CONFIG_NAVIGATION b
223 210 JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID
224   - WHERE b.LIST_OBJECT_TYPE = '` + listObjType + `'
225   - ORDER BY b.RB ASC`
226   -
227   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S)
  211 + WHERE b.LIST_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", listObjType) +
  212 + ` ORDER BY b.RB ASC`)
228 213 if err != nil {
229 214 return resp, err
230 215 }
231   - defer stmt.Close()
  216 + defer rows.Close()
232 217  
233   - rset, err := stmt.Qry()
234   - if err != nil {
235   - return resp, err
236   - }
237   - for rset.Next() {
238   - resp = append(resp, ListNavNode{
239   - ObjectType: rset.Row[0].(string),
240   - ParentObjectType: rset.Row[1].(string),
241   - LabelField: rset.Row[2].(string),
242   - Icon: rset.Row[3].(string),
243   - ParentFilterField: rset.Row[4].(string),
244   - ParentIDField: rset.Row[5].(string),
245   - // RB is ignored
246   - })
  218 + var node ListNavNode
  219 + for rows.Next() {
  220 + rows.Scan(&node.ObjectType, &node.ParentObjectType, &node.LabelField, &node.Icon,
  221 + &node.ParentFilterField, &node.ParentIDField)
  222 + resp = append(resp, node)
247 223 }
248   - if rset.Err() != nil {
249   - return nil, rset.Err()
  224 + if rows.Err() != nil {
  225 + return nil, rows.Err()
250 226 }
251 227  
252 228 return resp, nil
253 229 }
254 230  
255 231 // getListActions returns list actions for the provided object type.
256   -func getListActions(db *ora.Ses, objType string) (ListActions, error) {
  232 +func getListActions(db *sql.DB, objType string) (ListActions, error) {
257 233 var resp ListActions
258   - var err error
259   - var stmt *ora.Stmt
260   - query := `SELECT
  234 + rows, err := db.Query(`SELECT
261 235 ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT,
262 236 ACTION_PRINT, ACTION_GRAPH
263 237 FROM LIST_CONFIG
264   - WHERE OBJECT_TYPE = '` + objType + `'`
265   -
266   - stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32,
267   - ora.U32, ora.U32)
  238 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
268 239 if err != nil {
269 240 return ListActions{}, err
270 241 }
271   - defer stmt.Close()
  242 + defer rows.Close()
272 243  
273   - rset, err := stmt.Qry()
274   - if err != nil {
275   - return ListActions{}, err
  244 + var create, update, delete, export, print, graph uint32
  245 + if rows.Next() {
  246 + rows.Scan(&create, &update, &delete, &export, &print, &graph)
  247 + resp.Create = create != 0
  248 + resp.Update = update != 0
  249 + resp.Delete = delete != 0
  250 + resp.Export = export != 0
  251 + resp.Print = print != 0
  252 + resp.Graph = graph != 0
276 253 }
277   - if rset.Next() {
278   - resp.Create = rset.Row[0].(uint32) != 0
279   - resp.Update = rset.Row[1].(uint32) != 0
280   - resp.Delete = rset.Row[2].(uint32) != 0
281   - resp.Export = rset.Row[3].(uint32) != 0
282   - resp.Print = rset.Row[4].(uint32) != 0
283   - resp.Graph = rset.Row[5].(uint32) != 0
284   - }
285   - if rset.Err() != nil {
286   - return ListActions{}, rset.Err()
  254 + if rows.Err() != nil {
  255 + return ListActions{}, rows.Err()
287 256 }
288 257 return resp, nil
289 258 }
290 259  
291 260 // getListFiters returns list filter slice for the provided object type.
292   -func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) {
  261 +func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) {
293 262 resp := make([]ListFilter, 0)
294 263 filtersFields, err := getFilterFieldsAndPosition(db, objType)
295 264 if err != nil {
... ... @@ -321,30 +290,25 @@ func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) {
321 290 }
322 291  
323 292 // getFilterFieldsAndPosition returns a map of filter fields and their respective position in the menu.
324   -func getFilterFieldsAndPosition(db *ora.Ses, objType string) (map[string]uint32, error) {
  293 +func getFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) {
325 294 filtersField := make(map[string]uint32, 0)
326   - var err error
327   - var stmt *ora.Stmt
328   - query := `SELECT
  295 + rows, err := db.Query(`SELECT
329 296 FILTERS_FIELD, RB
330 297 FROM LIST_CONFIG_FILTERS
331   - WHERE OBJECT_TYPE = '` + objType + `'`
332   -
333   - stmt, err = db.Prep(query, ora.S, ora.U32)
  298 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
334 299 if err != nil {
335 300 return nil, err
336 301 }
337   - defer stmt.Close()
  302 + defer rows.Close()
338 303  
339   - rset, err := stmt.Qry()
340   - if err != nil {
341   - return nil, err
342   - }
343   - for rset.Next() {
344   - filtersField[rset.Row[0].(string)] = rset.Row[1].(uint32)
  304 + for rows.Next() {
  305 + var field string
  306 + var rb uint32
  307 + rows.Scan(&field, &rb)
  308 + filtersField[field] = rb
345 309 }
346   - if rset.Err() != nil {
347   - return nil, rset.Err()
  310 + if rows.Err() != nil {
  311 + return nil, rows.Err()
348 312 }
349 313 return filtersField, nil
350 314 }
... ... @@ -356,66 +320,44 @@ type _filter struct {
356 320 }
357 321  
358 322 // getFiltersByFilterField returns filter slice for the provided filter field.
359   -func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error) {
  323 +func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) {
360 324 resp := make([]_filter, 0)
361   - var err error
362   - var stmt *ora.Stmt
363   - query := `SELECT
  325 + rows, err := db.Query(`SELECT
364 326 FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES
365 327 FROM LIST_FILTERS_FIELD
366   - WHERE FILTERS_FIELD = '` + filtersField + `'`
367   -
368   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S)
  328 + WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField))
369 329 if err != nil {
370 330 return resp, err
371 331 }
372   - defer stmt.Close()
  332 + defer rows.Close()
373 333  
374   - rset, err := stmt.Qry()
375   - if err != nil {
376   - return resp, err
  334 + var f _filter
  335 + for rows.Next() {
  336 + rows.Scan(&f.Type, &f.Label, &f.DefaultValues)
  337 + resp = append(resp, f)
377 338 }
378   - for rset.Next() {
379   - resp = append(resp, _filter{
380   - Type: rset.Row[0].(string),
381   - Label: rset.Row[1].(string),
382   - DefaultValues: rset.Row[2].(string),
383   - })
384   - }
385   - if rset.Err() != nil {
386   - return resp, rset.Err()
  339 + if rows.Err() != nil {
  340 + return resp, rows.Err()
387 341 }
388 342 return resp, nil
389 343 }
390 344  
391 345 // getFilterDropdownConfig returns dropdown menu for the provided filter field.
392   -func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) {
  346 +func getFilterDropdownConfig(db *sql.DB, filtersField string) (Dropdown, error) {
393 347 var resp Dropdown
394   - var err error
395   - var stmt *ora.Stmt
396   - query := `SELECT
  348 + rows, err := db.Query(`SELECT
397 349 FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD
398 350 FROM LIST_DROPDOWN_FILTER
399   - WHERE FILTERS_FIELD = '` + filtersField + `'`
400   -
401   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
  351 + WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField))
402 352 if err != nil {
403 353 return resp, err
404 354 }
405   - defer stmt.Close()
406   -
407   - rset, err := stmt.Qry()
408   - if err != nil {
409   - return resp, err
  355 + defer rows.Close()
  356 + if rows.Next() {
  357 + rows.Scan(&resp.FiltersField, &resp.ObjectType, &resp.IDField, &resp.LabelField)
410 358 }
411   - if rset.Next() {
412   - resp.FiltersField = rset.Row[0].(string)
413   - resp.ObjectType = rset.Row[1].(string)
414   - resp.IDField = rset.Row[2].(string)
415   - resp.LabelField = rset.Row[3].(string)
416   - }
417   - if rset.Err() != nil {
418   - return resp, rset.Err()
  359 + if rows.Err() != nil {
  360 + return resp, rows.Err()
419 361 }
420 362 return resp, nil
421 363 }
... ... @@ -438,175 +380,124 @@ func sortFilters(filters []ListFilter) {
438 380 }
439 381  
440 382 // getListGraph return list graph slice for the provided object type.
441   -func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
  383 +func getListGraph(db *sql.DB, objType string) ([]ListGraph, error) {
442 384 resp := make([]ListGraph, 0)
443   - var err error
444   - var stmt *ora.Stmt
445   - query := `SELECT
  385 + rows, err := db.Query(`SELECT
446 386 OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL
447 387 FROM LIST_GRAPHS
448   - WHERE OBJECT_TYPE = '` + objType + `'`
449   -
450   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
  388 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
451 389 if err != nil {
452 390 return resp, err
453 391 }
454   - defer stmt.Close()
  392 + defer rows.Close()
455 393  
456   - rset, err := stmt.Qry()
457   - if err != nil {
458   - return resp, err
459   - }
460   - for rset.Next() {
461   - resp = append(resp, ListGraph{
462   - ObjectType: rset.Row[0].(string),
463   - X: rset.Row[1].(string),
464   - Y: rset.Row[2].(string),
465   - GroupField: rset.Row[3].(string),
466   - Label: rset.Row[4].(string),
467   - })
  394 + var lg ListGraph
  395 + for rows.Next() {
  396 + rows.Scan(&lg.ObjectType, &lg.X, &lg.Y, &lg.GroupField, &lg.Label)
  397 + resp = append(resp, lg)
468 398 }
469   - if rset.Err() != nil {
470   - return resp, rset.Err()
  399 + if rows.Err() != nil {
  400 + return resp, rows.Err()
471 401 }
472 402 return resp, nil
473 403 }
474 404  
475 405 // getListOptions returns list options for the provided object type.
476   -func getListOptions(db *ora.Ses, objType string) (ListOptions, error) {
  406 +func getListOptions(db *sql.DB, objType string) (ListOptions, error) {
477 407 var resp ListOptions
478   - var err error
479   - var stmt *ora.Stmt
480   - query := `SELECT
  408 + rows, err := db.Query(`SELECT
481 409 GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION,
482 410 PAGE_SIZE, PIVOT, DETAIL, TOTAL
483 411 FROM LIST_CONFIG
484   - WHERE OBJECT_TYPE = '` + objType + `'`
485   -
486   - stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32,
487   - ora.U64, ora.U64, ora.U32, ora.U32)
488   - if err != nil {
489   - return ListOptions{}, err
490   - }
491   - defer stmt.Close()
492   -
493   - rset, err := stmt.Qry()
  412 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
494 413 if err != nil {
495 414 return ListOptions{}, err
496 415 }
497   - if rset.Next() {
498   - resp.GlobalFilter = rset.Row[0].(uint32) != 0
499   - resp.LocalFilters = rset.Row[1].(uint32) != 0
500   - resp.RemoteFilters = rset.Row[2].(uint32) != 0
501   - resp.Pagination = rset.Row[3].(uint32) != 0
502   - resp.PageSize = rset.Row[4].(uint64)
503   - resp.Pivot = rset.Row[5].(uint64) != 0
504   - resp.Detail = rset.Row[6].(uint32) != 0
505   - resp.Total = rset.Row[7].(uint32) != 0
506   - }
507   - if rset.Err() != nil {
508   - return ListOptions{}, rset.Err()
  416 + defer rows.Close()
  417 + if rows.Next() {
  418 + var gfilter, lfilters, rfilters, pagination, pageSize, pivot, detail, total uint32
  419 + rows.Scan(&gfilter, &lfilters, &rfilters, &pagination, &pageSize, &pivot, &detail, &total)
  420 + resp.GlobalFilter = gfilter != 0
  421 + resp.LocalFilters = lfilters != 0
  422 + resp.RemoteFilters = rfilters != 0
  423 + resp.Pagination = pagination != 0
  424 + resp.PageSize = pageSize
  425 + resp.Pivot = pivot != 0
  426 + resp.Detail = detail != 0
  427 + resp.Total = total != 0
  428 + }
  429 + if rows.Err() != nil {
  430 + return ListOptions{}, rows.Err()
509 431 }
510 432 return resp, nil
511 433 }
512 434  
513 435 // getListParent returns list parent node slice for the provided object type.
514   -func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) {
  436 +func getListParent(db *sql.DB, objType string) ([]ListParentNode, error) {
515 437 resp := make([]ListParentNode, 0)
516   - var err error
517   - var stmt *ora.Stmt
518   - query := `SELECT
  438 + rows, err := db.Query(`SELECT
519 439 PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD
520 440 FROM LIST_CONFIG_CHILD
521   - WHERE OBJECT_TYPE = '` + objType + `'`
522   -
523   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S)
  441 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
524 442 if err != nil {
525 443 return resp, err
526 444 }
527   - defer stmt.Close()
  445 + defer rows.Close()
528 446  
529   - rset, err := stmt.Qry()
530   - if err != nil {
531   - return resp, err
532   - }
533   - for rset.Next() {
534   - resp = append(resp, ListParentNode{
535   - ObjectType: rset.Row[0].(string),
536   - LabelField: rset.Row[1].(string),
537   - FilterField: rset.Row[2].(string),
538   - })
  447 + var pnode ListParentNode
  448 + for rows.Next() {
  449 + rows.Scan(&pnode.ObjectType, &pnode.LabelField, &pnode.FilterField)
  450 + resp = append(resp, pnode)
539 451 }
540   - if rset.Err() != nil {
541   - return nil, rset.Err()
  452 + if rows.Err() != nil {
  453 + return nil, rows.Err()
542 454 }
543 455  
544 456 return resp, nil
545 457 }
546 458  
547 459 // getListPivot list pivot slice for the provided object type.
548   -func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) {
  460 +func getListPivot(db *sql.DB, objType string) ([]ListPivot, error) {
549 461 resp := make([]ListPivot, 0)
550   - var err error
551   - var stmt *ora.Stmt
552   - query := `SELECT
  462 + rows, err := db.Query(`SELECT
553 463 OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD
554 464 FROM LIST_PIVOTS
555   - WHERE OBJECT_TYPE = '` + objType + `'`
556   -
557   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
  465 + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
558 466 if err != nil {
559 467 return resp, err
560 468 }
561   - defer stmt.Close()
  469 + defer rows.Close()
562 470  
563   - rset, err := stmt.Qry()
564   - if err != nil {
565   - return resp, err
  471 + var p ListPivot
  472 + for rows.Next() {
  473 + rows.Scan(&p.ObjectType, &p.GroupField, &p.DistinctField, &p.Value)
  474 + resp = append(resp, p)
566 475 }
567   - for rset.Next() {
568   - resp = append(resp, ListPivot{
569   - ObjectType: rset.Row[0].(string),
570   - GroupField: rset.Row[1].(string),
571   - DistinctField: rset.Row[2].(string),
572   - Value: rset.Row[3].(string),
573   - })
574   - }
575   - if rset.Err() != nil {
576   - return nil, rset.Err()
  476 + if rows.Err() != nil {
  477 + return nil, rows.Err()
577 478 }
578 479  
579 480 return resp, nil
580 481 }
581 482  
582 483 // getListDetails returns list details for the provided object type.
583   -func getListDetails(db *ora.Ses, objType string) (ListDetails, error) {
  484 +func getListDetails(db *sql.DB, objType string) (ListDetails, error) {
584 485 var resp ListDetails
585   - var err error
586   - var stmt *ora.Stmt
587   - query := `SELECT
  486 + rows, err := db.Query(`SELECT
588 487 OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL
589 488 FROM LIST_CONFIG_DETAIL
590   - WHERE PARENT_OBJECT_TYPE = '` + objType + `'`
591   -
592   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.U32)
593   - if err != nil {
594   - return resp, err
595   - }
596   - defer stmt.Close()
597   -
598   - rset, err := stmt.Qry()
  489 + WHERE PARENT_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType))
599 490 if err != nil {
600 491 return resp, err
601 492 }
602   - if rset.Next() {
603   - resp.ObjectType = rset.Row[0].(string)
604   - resp.ParentObjectType = rset.Row[1].(string)
605   - resp.ParentFilterField = rset.Row[2].(string)
606   - resp.SingleDetail = rset.Row[3].(uint32) != 0
  493 + defer rows.Close()
  494 + if rows.Next() {
  495 + var singleDetail uint32
  496 + rows.Scan(&resp.ObjectType, &resp.ParentObjectType, &resp.ParentFilterField, &singleDetail)
  497 + resp.SingleDetail = singleDetail != 0
607 498 }
608   - if rset.Err() != nil {
609   - return resp, rset.Err()
  499 + if rows.Err() != nil {
  500 + return resp, rows.Err()
610 501 }
611 502  
612 503 return resp, nil
... ...
1 1 package webutility
2 2  
3   -import "gopkg.in/rana/ora.v4"
  3 +import "database/sql"
4 4  
5 5 type SelectConfig struct {
6 6 ListObjType string `json:"listObjectType"`
... ... @@ -12,11 +12,9 @@ type SelectConfig struct {
12 12 }
13 13  
14 14 // GetSelectConfig returns select configuration slice for the given object type.
15   -func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) {
  15 +func GetSelectConfig(db *sql.DB, otype string) ([]SelectConfig, error) {
16 16 resp := make([]SelectConfig, 0)
17   - var err error
18   - var stmt *ora.Stmt
19   - query := `SELECT
  17 + rows, err := db.Query(`SELECT
20 18 a.LIST_OBJECT_TYPE,
21 19 a.OBJECT_TYPE,
22 20 a.ID_FIELD,
... ... @@ -26,30 +24,19 @@ func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) {
26 24 FROM LIST_SELECT_CONFIG a, LIST_VALUE_FIELD b
27 25 WHERE a.LIST_OBJECT_TYPE` + otype + `
28 26 AND b.LIST_TYPE = a.LIST_OBJECT_TYPE
29   - AND b.OBJECT_TYPE = a.OBJECT_TYPE`
30   -
31   - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S, ora.S)
32   - defer stmt.Close()
  27 + AND b.OBJECT_TYPE = a.OBJECT_TYPE`)
33 28 if err != nil {
34 29 return nil, err
35 30 }
  31 + defer rows.Close()
36 32  
37   - rset, err := stmt.Qry()
38   - if err != nil {
39   - return nil, err
40   - }
41   - for rset.Next() {
42   - resp = append(resp, SelectConfig{
43   - ListObjType: rset.Row[0].(string),
44   - ObjType: rset.Row[1].(string),
45   - IdField: rset.Row[2].(string),
46   - LabelField: rset.Row[3].(string),
47   - Type: rset.Row[4].(string),
48   - ValueField: rset.Row[5].(string),
49   - })
  33 + var sc SelectConfig
  34 + for rows.Next() {
  35 + rows.Scan(&sc.ListObjType, &sc.ObjType, &sc.IdField, &sc.LabelField, &sc.Type, &sc.ValueField)
  36 + resp = append(resp, sc)
50 37 }
51   - if rset.Err() != nil {
52   - return nil, rset.Err()
  38 + if rows.Err() != nil {
  39 + return nil, rows.Err()
53 40 }
54 41  
55 42 return resp, nil
... ...