// NewStatus returns a *Status structure to the user func NewStatus(dbh *sql.DB) *Status { if dbh == nil { logger.Fatal("NewStatus() dbh is nil") } s := new(Status) s.dbh = dbh return s }
// NewVariables returns a pointer to an initialised Variables structure func NewVariables(dbh *sql.DB) *Variables { if dbh == nil { logger.Fatal("NewVariables(): dbh == nil") } v := new(Variables) v.dbh = dbh v.selectAll() return v }
// Get returns the value of the variable name requested (if found), or if not an error // - note: we assume we have checked a variable first as there's no logic here to switch between I_S and P_S func (status *Status) Get(name string) int { var value int query := "SELECT VARIABLE_VALUE from " + globalVariablesSchema + ".GLOBAL_STATUS WHERE VARIABLE_NAME = ?" err := status.dbh.QueryRow(query, name).Scan(&value) switch { case err == sql.ErrNoRows: logger.Println("global.SelectStatusByName(" + name + "): no status with this name") case err != nil: logger.Fatal(err) default: // fmt.Println("value for", name, "is", value) } if err != nil { logger.Fatal("Unable to retrieve status for '"+name+"':", err) } return value }
// selectAll() collects all variables from the database and stores for later use. // - all returned keys are lower-cased. func (v *Variables) selectAll() { hashref := make(map[string]string) query := "SELECT VARIABLE_NAME, VARIABLE_VALUE FROM " + globalVariablesSchema + ".GLOBAL_VARIABLES" logger.Println("query:", query) rows, err := v.dbh.Query(query) if err != nil { if (globalVariablesSchema == "INFORMATION_SCHEMA") && (err.Error() == showCompatibility56Error) { logger.Println("selectAll() I_S query failed, trying with P_S") globalVariablesSchema = "PERFORMANCE_SCHEMA" // Change global variable to use P_S query = "SELECT VARIABLE_NAME, VARIABLE_VALUE FROM " + globalVariablesSchema + ".GLOBAL_VARIABLES" logger.Println("query:", query) rows, err = v.dbh.Query(query) } if err != nil { logger.Fatal("selectAll() query failed with:", err) } } logger.Println("selectAll() query succeeded") defer rows.Close() for rows.Next() { var variable, value string if err := rows.Scan(&variable, &value); err != nil { logger.Fatal(err) } hashref[strings.ToLower(variable)] = value } if err := rows.Err(); err != nil { logger.Fatal(err) } logger.Println("selectAll() result has", len(hashref), "rows") v.variables = hashref }