select_config.go 1.23 KB
package restutility

import (
	"gopkg.in/rana/ora.v3"
)

type SelectConfig struct {
	ListObjType string `json:"listObjectType"`
	ObjType     string `json:"objectType"`
	Type        string `json:"type"`
	IdField     string `json:"idField"`
	LabelField  string `json:"labelField"`
	ValueField  string `json:"valueField"`
}

func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) {
	resp := make([]SelectConfig, 0)
	var err error
	var stmt *ora.Stmt
	query := `SELECT a.LIST_OBJECT_TYPE, a.OBJECT_TYPE, a.ID_FIELD,
		a.LABEL_FIELD, a.TYPE, b.FIELD
		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()
	if err != nil {
		return nil, err
	}

	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),
		})
	}
	if rset.Err != nil {
		return nil, rset.Err
	}

	return resp, nil
}