From 79071a5d4fc3720a184381f583e32a94181fec2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Tue, 27 Mar 2018 12:27:28 +0200 Subject: [PATCH] Using database/sql package to make the package driver agnostic --- json_utility.go | 285 ++++++++++++++++++---------------------- list_config.go | 391 ++++++++++++++++++++----------------------------------- select_config.go | 35 ++--- 3 files changed, 282 insertions(+), 429 deletions(-) diff --git a/json_utility.go b/json_utility.go index 3474fc7..ea283f6 100644 --- a/json_utility.go +++ b/json_utility.go @@ -1,18 +1,15 @@ package webutility import ( + "database/sql" "encoding/json" - "errors" "fmt" "io" "net/http" - "os" "sync" "time" "git.to-net.rs/marko.tikvic/gologger" - - "gopkg.in/rana/ora.v4" ) var ( @@ -20,7 +17,7 @@ var ( metadata = make(map[string]Payload) updateQue = make(map[string][]byte) - metadataDB *ora.Ses + metadataDB *sql.DB activeProject string inited bool @@ -68,8 +65,21 @@ type Payload struct { Data interface{} `json:"data"` } +// NewPayload returs a payload sceleton for entity described with etype. +func NewPayload(r *http.Request, etype string) Payload { + pload := metadata[etype] + pload.Method = r.Method + " " + r.RequestURI + return pload +} + +// DecodeJSON decodes JSON data from r to v. +// Returns an error if it fails. +func DecodeJSON(r io.Reader, v interface{}) error { + return json.NewDecoder(r).Decode(v) +} + // LoadPayloadsdetaData loads all payloads' information into 'metadata' variable. -func LoadPayloadsMetadata(db *ora.Ses, project string) error { +func LoadPayloadsMetadata(db *sql.DB, project string) error { metadataDB = db activeProject = project @@ -103,6 +113,119 @@ func QueEntityModelUpdate(entityType string, v interface{}) { updateQue[entityType], _ = json.Marshal(v) } +func initMetadata(project string) error { + rows, err := metadataDB.Query(`select + entity_type, + metadata + from entities + where projekat = ` + fmt.Sprintf("'%s'", project)) + if err != nil { + return err + } + defer rows.Close() + + count := 0 + success := 0 + if len(metadata) > 0 { + metadata = nil + } + metadata = make(map[string]Payload) + for rows.Next() { + var name, load string + rows.Scan(&name, &load) + + p := Payload{} + err := json.Unmarshal([]byte(load), &p) + if err != nil { + logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), load) + } else { + success++ + metadata[name] = p + } + count++ + } + perc := float32(success/count) * 100.0 + logger.Log("loaded %d/%d (%.1f%%) entities\n", success, count, perc) + + return nil +} + +func hotload(n int) { + entityScan := make(map[string]int64) + firstCheck := true + for { + time.Sleep(time.Duration(n) * time.Second) + rows, err := metadataDB.Query(`select + ora_rowscn, + entity_type + from entities where projekat = ` + fmt.Sprintf("'%s'", activeProject)) + if err != nil { + logger.Log("hotload failed: %v\n", err) + time.Sleep(time.Duration(n) * time.Second) + continue + } + + var toRefresh []string + for rows.Next() { + var scanID int64 + var entity string + rows.Scan(&scanID, &entity) + oldID, ok := entityScan[entity] + if !ok || oldID != scanID { + entityScan[entity] = scanID + toRefresh = append(toRefresh, entity) + } + } + rows.Close() + + if rows.Err() != nil { + logger.Log("hotload rset error: %v\n", rows.Err()) + time.Sleep(time.Duration(n) * time.Second) + continue + } + + if len(toRefresh) > 0 && !firstCheck { + mu.Lock() + refreshMetadata(toRefresh) + mu.Unlock() + } + if firstCheck { + firstCheck = false + } + } +} + +func refreshMetadata(entities []string) { + for _, e := range entities { + fmt.Printf("refreshing %s\n", e) + rows, err := metadataDB.Query(`select + metadata + from entities + where projekat = ` + fmt.Sprintf("'%s'", activeProject) + + ` and entity_type = ` + fmt.Sprintf("'%s'", e)) + + if err != nil { + logger.Log("webutility: refresh: prep: %v\n", err) + rows.Close() + continue + } + + for rows.Next() { + var load string + rows.Scan(&load) + p := Payload{} + err := json.Unmarshal([]byte(load), &p) + if err != nil { + logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), load) + } else { + metadata[e] = p + } + } + rows.Close() + } +} + +/* func UpdateEntityModels(command string) (total, upd, add int, err error) { if command != "force" && command != "missing" { return total, 0, 0, errors.New("uknown command: " + command) @@ -195,152 +318,4 @@ func DeleteEntityModel(entityType string) error { } return err } - -// NewPayload returs a payload sceleton for entity described with etype. -func NewPayload(r *http.Request, etype string) Payload { - pload := metadata[etype] - pload.Method = r.Method + " " + r.RequestURI - return pload -} - -// DecodeJSON decodes JSON data from r to v. -// Returns an error if it fails. -func DecodeJSON(r io.Reader, v interface{}) error { - return json.NewDecoder(r).Decode(v) -} - -func initMetadata(project string) error { - metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S)) - stmt, err := metadataDB.Prep(`select - entity_type, - metadata - from entities - where projekat = `+fmt.Sprintf("'%s'", project), - ora.S, - ora.S) - - defer stmt.Close() - if err != nil { - return err - } - - rset, err := stmt.Qry() - if err != nil { - return err - } - - count := 0 - success := 0 - if len(metadata) > 0 { - metadata = nil - } - metadata = make(map[string]Payload) - for rset.Next() { - name := rset.Row[0].(string) - load := []byte(rset.Row[1].(string)) - - p := Payload{} - err := json.Unmarshal(load, &p) - if err != nil { - logger.Log("couldn't init: '%s' metadata: %s\n%s\n", name, err.Error(), rset.Row[1].(string)) - } else { - success++ - metadata[name] = p - } - count++ - } - perc := float32(success/count) * 100.0 - logger.Log("loaded %d/%d (%.1f%%) entities\n", success, count, perc) - - return nil -} - -func hotload(n int) { - entityScan := make(map[string]int64) - firstCheck := true - for { - time.Sleep(time.Duration(n) * time.Second) - stmt, err := metadataDB.Prep(`select - ora_rowscn, - entity_type - from entities where projekat = `+fmt.Sprintf("'%s'", activeProject), - ora.I64, - ora.S) - if err != nil { - logger.Log("hotload failed: %v\n", err) - time.Sleep(time.Duration(n) * time.Second) - continue - } - - rset, err := stmt.Qry() - if err != nil { - fmt.Fprintf(os.Stderr, "hotload failed: %v\n", err) - time.Sleep(time.Duration(n) * time.Second) - continue - } - - var toRefresh []string - for rset.Next() { - scanID := rset.Row[0].(int64) - entity := rset.Row[1].(string) - oldID, ok := entityScan[entity] - if !ok || oldID != scanID { - entityScan[entity] = scanID - toRefresh = append(toRefresh, entity) - } - } - stmt.Close() - - if rset.Err() != nil { - logger.Log("hotload rset error: %v\n", rset.Err()) - time.Sleep(time.Duration(n) * time.Second) - continue - } - - if len(toRefresh) > 0 && !firstCheck { - mu.Lock() - refreshMetadata(toRefresh) - mu.Unlock() - } - if firstCheck { - firstCheck = false - } - } -} - -func refreshMetadata(entities []string) { - for _, e := range entities { - fmt.Printf("refreshing %s\n", e) - stmt, err := metadataDB.Prep(`select - metadata - from entities - where projekat = `+fmt.Sprintf("'%s'", activeProject)+ - ` and entity_type = `+fmt.Sprintf("'%s'", e), - ora.S) - - if err != nil { - logger.Log("webutility: refresh: prep: %v\n", err) - stmt.Close() - continue - } - - rset, err := stmt.Qry() - if err != nil { - fmt.Printf("webutility: refresh: query: %v\n", err) - stmt.Close() - continue - } - - for rset.Next() { - load := []byte(rset.Row[0].(string)) - p := Payload{} - err := json.Unmarshal(load, &p) - if err != nil { - logger.Log("couldn't refresh: '%s' metadata: %s\n%s\n", e, err.Error(), rset.Row[0].(string)) - } else { - metadata[e] = p - } - } - stmt.Close() - } -} +*/ diff --git a/list_config.go b/list_config.go index c81ca64..5b03e2f 100644 --- a/list_config.go +++ b/list_config.go @@ -1,13 +1,16 @@ package webutility -import "gopkg.in/rana/ora.v4" +import ( + "database/sql" + "fmt" +) type ListOptions struct { GlobalFilter bool `json:"globalFilter"` LocalFilters bool `json:"localFilters"` RemoteFilters bool `json:"remoteFilters"` Pagination bool `json:"pagination"` - PageSize uint64 `json:"pageSize"` + PageSize uint32 `json:"pageSize"` Pivot bool `json:"pivot"` Detail bool `json:"detail"` Total bool `json:"total"` @@ -93,7 +96,7 @@ type ListConfig struct { // GetListConfig returns list configuration for the provided object type for the front-end application // or an error if it fails. -func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { +func GetListConfig(db *sql.DB, objType string) (ListConfig, error) { resp := newDefaultList(objType) var err error @@ -116,29 +119,23 @@ func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { // GetListConfigObjectIDField takes in database connection and an object type and it returns the // ID field name for the provided object type. -func GetListConfigObjectIDField(db *ora.Ses, otype string) string { +func GetListConfigObjectIDField(db *sql.DB, otype string) string { var resp string - var err error - var stmt *ora.Stmt - stmt, err = db.Prep(`SELECT + rows, err := db.Query(`SELECT ID_FIELD FROM LIST_CONFIG_ID_FIELD - WHERE OBJECT_TYPE = '`+otype+`'`, - ora.S) - - defer stmt.Close() - + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", otype)) if err != nil { return "" } + defer rows.Close() - rset, err := stmt.Qry() - if rset.Next() { - resp = rset.Row[0].(string) + if rows.Next() { + rows.Scan(&resp) } - if rset.Err() != nil { + if rows.Err() != nil { return "" } @@ -175,121 +172,93 @@ func newDefaultList(objType string) ListConfig { } // setListParams sets the default parameters of the provided configuration list for the provided object type. -func setListParams(db *ora.Ses, list *ListConfig, objType string) error { - var err error - var stmt *ora.Stmt - query := `SELECT +func setListParams(db *sql.DB, list *ListConfig, objType string) error { + rows, err := db.Query(`SELECT OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT FROM LIST_CONFIG - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.U32, ora.U32) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return err } - defer stmt.Close() + defer rows.Close() + if rows.Next() { + otype, title := "", "" + lazyLoad, inlineEdit := 0, 0 + rows.Scan(&otype, &title, &lazyLoad, &inlineEdit) - rset, err := stmt.Qry() - if err != nil { - return err - } - if rset.Next() { - otype := rset.Row[0].(string) if otype != "" { list.ObjectType = otype } - - title := rset.Row[1].(string) if title != "" { list.Title = title } - list.LazyLoad = rset.Row[2].(uint32) != 0 - list.InlineEdit = rset.Row[3].(uint32) != 0 + list.LazyLoad = lazyLoad != 0 + list.InlineEdit = inlineEdit != 0 } - if rset.Err() != nil { - return rset.Err() + if rows.Err() != nil { + return rows.Err() } return nil } // getListNavigation returns list navigation node slice for the provided objectType. -func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) { +func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) { resp := make([]ListNavNode, 0) - var err error - var stmt *ora.Stmt - query := `SELECT - a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD, b.RB + rows, err := db.Query(`SELECT + a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD FROM LIST_CONFIG_NAVIGATION b JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID - WHERE b.LIST_OBJECT_TYPE = '` + listObjType + `' - ORDER BY b.RB ASC` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S) + WHERE b.LIST_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", listObjType) + + ` ORDER BY b.RB ASC`) if err != nil { return resp, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return resp, err - } - for rset.Next() { - resp = append(resp, ListNavNode{ - ObjectType: rset.Row[0].(string), - ParentObjectType: rset.Row[1].(string), - LabelField: rset.Row[2].(string), - Icon: rset.Row[3].(string), - ParentFilterField: rset.Row[4].(string), - ParentIDField: rset.Row[5].(string), - // RB is ignored - }) + var node ListNavNode + for rows.Next() { + rows.Scan(&node.ObjectType, &node.ParentObjectType, &node.LabelField, &node.Icon, + &node.ParentFilterField, &node.ParentIDField) + resp = append(resp, node) } - if rset.Err() != nil { - return nil, rset.Err() + if rows.Err() != nil { + return nil, rows.Err() } return resp, nil } // getListActions returns list actions for the provided object type. -func getListActions(db *ora.Ses, objType string) (ListActions, error) { +func getListActions(db *sql.DB, objType string) (ListActions, error) { var resp ListActions - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT, ACTION_PRINT, ACTION_GRAPH FROM LIST_CONFIG - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32, - ora.U32, ora.U32) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return ListActions{}, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return ListActions{}, err + var create, update, delete, export, print, graph uint32 + if rows.Next() { + rows.Scan(&create, &update, &delete, &export, &print, &graph) + resp.Create = create != 0 + resp.Update = update != 0 + resp.Delete = delete != 0 + resp.Export = export != 0 + resp.Print = print != 0 + resp.Graph = graph != 0 } - if rset.Next() { - resp.Create = rset.Row[0].(uint32) != 0 - resp.Update = rset.Row[1].(uint32) != 0 - resp.Delete = rset.Row[2].(uint32) != 0 - resp.Export = rset.Row[3].(uint32) != 0 - resp.Print = rset.Row[4].(uint32) != 0 - resp.Graph = rset.Row[5].(uint32) != 0 - } - if rset.Err() != nil { - return ListActions{}, rset.Err() + if rows.Err() != nil { + return ListActions{}, rows.Err() } return resp, nil } // getListFiters returns list filter slice for the provided object type. -func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { +func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) { resp := make([]ListFilter, 0) filtersFields, err := getFilterFieldsAndPosition(db, objType) if err != nil { @@ -321,30 +290,25 @@ func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { } // getFilterFieldsAndPosition returns a map of filter fields and their respective position in the menu. -func getFilterFieldsAndPosition(db *ora.Ses, objType string) (map[string]uint32, error) { +func getFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) { filtersField := make(map[string]uint32, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT FILTERS_FIELD, RB FROM LIST_CONFIG_FILTERS - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.U32) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return nil, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return nil, err - } - for rset.Next() { - filtersField[rset.Row[0].(string)] = rset.Row[1].(uint32) + for rows.Next() { + var field string + var rb uint32 + rows.Scan(&field, &rb) + filtersField[field] = rb } - if rset.Err() != nil { - return nil, rset.Err() + if rows.Err() != nil { + return nil, rows.Err() } return filtersField, nil } @@ -356,66 +320,44 @@ type _filter struct { } // getFiltersByFilterField returns filter slice for the provided filter field. -func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error) { +func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) { resp := make([]_filter, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES FROM LIST_FILTERS_FIELD - WHERE FILTERS_FIELD = '` + filtersField + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) if err != nil { return resp, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return resp, err + var f _filter + for rows.Next() { + rows.Scan(&f.Type, &f.Label, &f.DefaultValues) + resp = append(resp, f) } - for rset.Next() { - resp = append(resp, _filter{ - Type: rset.Row[0].(string), - Label: rset.Row[1].(string), - DefaultValues: rset.Row[2].(string), - }) - } - if rset.Err() != nil { - return resp, rset.Err() + if rows.Err() != nil { + return resp, rows.Err() } return resp, nil } // getFilterDropdownConfig returns dropdown menu for the provided filter field. -func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) { +func getFilterDropdownConfig(db *sql.DB, filtersField string) (Dropdown, error) { var resp Dropdown - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD FROM LIST_DROPDOWN_FILTER - WHERE FILTERS_FIELD = '` + filtersField + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) if err != nil { return resp, err } - defer stmt.Close() - - rset, err := stmt.Qry() - if err != nil { - return resp, err + defer rows.Close() + if rows.Next() { + rows.Scan(&resp.FiltersField, &resp.ObjectType, &resp.IDField, &resp.LabelField) } - if rset.Next() { - resp.FiltersField = rset.Row[0].(string) - resp.ObjectType = rset.Row[1].(string) - resp.IDField = rset.Row[2].(string) - resp.LabelField = rset.Row[3].(string) - } - if rset.Err() != nil { - return resp, rset.Err() + if rows.Err() != nil { + return resp, rows.Err() } return resp, nil } @@ -438,175 +380,124 @@ func sortFilters(filters []ListFilter) { } // getListGraph return list graph slice for the provided object type. -func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) { +func getListGraph(db *sql.DB, objType string) ([]ListGraph, error) { resp := make([]ListGraph, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL FROM LIST_GRAPHS - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return resp, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return resp, err - } - for rset.Next() { - resp = append(resp, ListGraph{ - ObjectType: rset.Row[0].(string), - X: rset.Row[1].(string), - Y: rset.Row[2].(string), - GroupField: rset.Row[3].(string), - Label: rset.Row[4].(string), - }) + var lg ListGraph + for rows.Next() { + rows.Scan(&lg.ObjectType, &lg.X, &lg.Y, &lg.GroupField, &lg.Label) + resp = append(resp, lg) } - if rset.Err() != nil { - return resp, rset.Err() + if rows.Err() != nil { + return resp, rows.Err() } return resp, nil } // getListOptions returns list options for the provided object type. -func getListOptions(db *ora.Ses, objType string) (ListOptions, error) { +func getListOptions(db *sql.DB, objType string) (ListOptions, error) { var resp ListOptions - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION, PAGE_SIZE, PIVOT, DETAIL, TOTAL FROM LIST_CONFIG - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32, - ora.U64, ora.U64, ora.U32, ora.U32) - if err != nil { - return ListOptions{}, err - } - defer stmt.Close() - - rset, err := stmt.Qry() + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return ListOptions{}, err } - if rset.Next() { - resp.GlobalFilter = rset.Row[0].(uint32) != 0 - resp.LocalFilters = rset.Row[1].(uint32) != 0 - resp.RemoteFilters = rset.Row[2].(uint32) != 0 - resp.Pagination = rset.Row[3].(uint32) != 0 - resp.PageSize = rset.Row[4].(uint64) - resp.Pivot = rset.Row[5].(uint64) != 0 - resp.Detail = rset.Row[6].(uint32) != 0 - resp.Total = rset.Row[7].(uint32) != 0 - } - if rset.Err() != nil { - return ListOptions{}, rset.Err() + 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() } return resp, nil } // getListParent returns list parent node slice for the provided object type. -func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) { +func getListParent(db *sql.DB, objType string) ([]ListParentNode, error) { resp := make([]ListParentNode, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD FROM LIST_CONFIG_CHILD - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return resp, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return resp, err - } - for rset.Next() { - resp = append(resp, ListParentNode{ - ObjectType: rset.Row[0].(string), - LabelField: rset.Row[1].(string), - FilterField: rset.Row[2].(string), - }) + var pnode ListParentNode + for rows.Next() { + rows.Scan(&pnode.ObjectType, &pnode.LabelField, &pnode.FilterField) + resp = append(resp, pnode) } - if rset.Err() != nil { - return nil, rset.Err() + if rows.Err() != nil { + return nil, rows.Err() } return resp, nil } // getListPivot list pivot slice for the provided object type. -func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) { +func getListPivot(db *sql.DB, objType string) ([]ListPivot, error) { resp := make([]ListPivot, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD FROM LIST_PIVOTS - WHERE OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return resp, err } - defer stmt.Close() + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return resp, err + var p ListPivot + for rows.Next() { + rows.Scan(&p.ObjectType, &p.GroupField, &p.DistinctField, &p.Value) + resp = append(resp, p) } - for rset.Next() { - resp = append(resp, ListPivot{ - ObjectType: rset.Row[0].(string), - GroupField: rset.Row[1].(string), - DistinctField: rset.Row[2].(string), - Value: rset.Row[3].(string), - }) - } - if rset.Err() != nil { - return nil, rset.Err() + if rows.Err() != nil { + return nil, rows.Err() } return resp, nil } // getListDetails returns list details for the provided object type. -func getListDetails(db *ora.Ses, objType string) (ListDetails, error) { +func getListDetails(db *sql.DB, objType string) (ListDetails, error) { var resp ListDetails - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL FROM LIST_CONFIG_DETAIL - WHERE PARENT_OBJECT_TYPE = '` + objType + `'` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.U32) - if err != nil { - return resp, err - } - defer stmt.Close() - - rset, err := stmt.Qry() + WHERE PARENT_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) if err != nil { return resp, err } - if rset.Next() { - resp.ObjectType = rset.Row[0].(string) - resp.ParentObjectType = rset.Row[1].(string) - resp.ParentFilterField = rset.Row[2].(string) - resp.SingleDetail = rset.Row[3].(uint32) != 0 + defer rows.Close() + if rows.Next() { + var singleDetail uint32 + rows.Scan(&resp.ObjectType, &resp.ParentObjectType, &resp.ParentFilterField, &singleDetail) + resp.SingleDetail = singleDetail != 0 } - if rset.Err() != nil { - return resp, rset.Err() + if rows.Err() != nil { + return resp, rows.Err() } return resp, nil diff --git a/select_config.go b/select_config.go index 7797503..c89277b 100644 --- a/select_config.go +++ b/select_config.go @@ -1,6 +1,6 @@ package webutility -import "gopkg.in/rana/ora.v4" +import "database/sql" type SelectConfig struct { ListObjType string `json:"listObjectType"` @@ -12,11 +12,9 @@ type SelectConfig struct { } // GetSelectConfig returns select configuration slice for the given object type. -func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) { +func GetSelectConfig(db *sql.DB, otype string) ([]SelectConfig, error) { resp := make([]SelectConfig, 0) - var err error - var stmt *ora.Stmt - query := `SELECT + rows, err := db.Query(`SELECT a.LIST_OBJECT_TYPE, a.OBJECT_TYPE, a.ID_FIELD, @@ -26,30 +24,19 @@ func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) { FROM LIST_SELECT_CONFIG a, LIST_VALUE_FIELD b WHERE a.LIST_OBJECT_TYPE` + otype + ` AND b.LIST_TYPE = a.LIST_OBJECT_TYPE - AND b.OBJECT_TYPE = a.OBJECT_TYPE` - - stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S, ora.S) - defer stmt.Close() + AND b.OBJECT_TYPE = a.OBJECT_TYPE`) if err != nil { return nil, err } + defer rows.Close() - rset, err := stmt.Qry() - if err != nil { - return nil, err - } - for rset.Next() { - resp = append(resp, SelectConfig{ - ListObjType: rset.Row[0].(string), - ObjType: rset.Row[1].(string), - IdField: rset.Row[2].(string), - LabelField: rset.Row[3].(string), - Type: rset.Row[4].(string), - ValueField: rset.Row[5].(string), - }) + var sc SelectConfig + for rows.Next() { + rows.Scan(&sc.ListObjType, &sc.ObjType, &sc.IdField, &sc.LabelField, &sc.Type, &sc.ValueField) + resp = append(resp, sc) } - if rset.Err() != nil { - return nil, rset.Err() + if rows.Err() != nil { + return nil, rows.Err() } return resp, nil -- 1.8.1.2