func ResetConditions(rw http.ResponseWriter, req *http.Request) { resetType := req.FormValue("resetType") beforeEach("no") switch resetType { case "simple": treeRoot := &treeNode{Parent: nil, Children: nil, Node: Condition{Text: "AND", Type: "logic", Operator: "AND"}} child1 := treeNode{Parent: treeRoot, Children: nil, Node: Condition{Text: "age gt 4", Type: "equality", Field: "age", Operator: "gt", Value: "4"}} child2 := treeNode{Parent: treeRoot, Children: nil, Node: Condition{Text: "num_pets lt 2", Type: "equality", Field: "num_pets", Operator: "lt", Value: "2"}} treeRoot.Children = []*treeNode{&child1, &child2} equalitySql, logicSql, err := treeRoot.toMysql() common.CheckError(err, 2) updateDatabase(equalitySql, logicSql, testingMysqlUsersInput) case "advanced": fallthrough default: updateDatabase(testingMysqlEqualityInput, testingMysqlLogicInput, testingMysqlUsersInput) } frontendJSON, _, _ := getFrontendJSON() rw.Write([]byte(frontendJSON)) }
// Gets users from mysql - set matchingOnly to true to only receive users who match the conditions func getUsers(matchingOnly bool) ([]userSqlRow, error) { var whereCondition string conditions := getConditions() tree := unserializeRawTree(conditions) conditionSql, err := tree.toConditionMysql() if err != nil { return nil, err } if matchingOnly { whereCondition = "WHERE " + conditionSql } sql := "SELECT id, name, age, num_pets FROM logictree.users " + whereCondition var name string var id, age, numPets int var userRowsReturned []userSqlRow rows, err := common.DB.Query(sql) common.CheckError(err, 2) defer rows.Close() for rows.Next() { rows.Scan(&id, &name, &age, &numPets) userRowsReturned = append(userRowsReturned, userSqlRow{Id: id, Name: name, Age: age, NumPets: numPets}) } return userRowsReturned, nil }
func updateDatabase(equalityStr, logicStr, usersStr string) { _, err := common.DB.Query("TRUNCATE TABLE logictree.conditions") common.CheckError(err, 2) if equalityStr != "" { _, err = common.DB.Query("INSERT INTO logictree.conditions (field, operator, value, type, lt, rt) VALUES " + equalityStr) common.CheckError(err, 2) } if logicStr != "" { _, err = common.DB.Query("INSERT INTO logictree.conditions (operator, type, lt, rt) VALUES " + logicStr) common.CheckError(err, 2) } if usersStr != "" { _, err = common.DB.Query("TRUNCATE TABLE logictree.users") common.CheckError(err, 2) _, err = common.DB.Query("INSERT INTO logictree.users (name, age, num_pets) VALUES " + usersStr) common.CheckError(err, 2) } }
func getFrontendJSON() (string, []conditionSqlRow, []Condition) { sqlConditions := getConditions() if len(sqlConditions) == 0 { return fmt.Sprintf(`{"tree": %s, "matchingUsers": %s}`, "{}", "{}"), nil, nil } conditionsTree := unserializeRawTree(sqlConditions) conditionsTreeJSON := conditionsTree.toJSON() formattedConditions, err := serializeTree(conditionsTree) common.CheckError(err, 2) matchingUsers, err := getUsers(true) common.CheckError(err, 2) matchingUsersJSON := usersToJSON(matchingUsers) allUsers, err := getUsers(false) common.CheckError(err, 2) allUsersJSON := usersToJSON(allUsers) combinedJSON := fmt.Sprintf(`{"tree": %s, "allUsers": %s, "matchingUsers": %s}`, conditionsTreeJSON, allUsersJSON, matchingUsersJSON) return combinedJSON, sqlConditions, formattedConditions }
func getConditions() []conditionSqlRow { var Field, Operator, Value, Type string var Left, Right int var conditionRowsReturned []conditionSqlRow // Get equality sql rows rows, err := common.DB.Query("SELECT COALESCE(field, ''), operator, COALESCE(value, ''), type, lt, rt FROM logictree.conditions ORDER BY lt") common.CheckError(err, 2) defer rows.Close() for rows.Next() { rows.Scan(&Field, &Operator, &Value, &Type, &Left, &Right) conditionRowsReturned = append(conditionRowsReturned, conditionSqlRow{Field: Field, Operator: Operator, Value: Value, Type: Type, Left: Left, Right: Right}) } return conditionRowsReturned }
func getUserSqlRows() []userSqlRow { var name string var id, age, numPets int var userRowsReturned []userSqlRow // Get equality sql rows rows, err := common.DB.Query("SELECT id, name, age, num_pets FROM logictree.users") common.CheckError(err, 2) defer rows.Close() for rows.Next() { rows.Scan(&id, &name, &age, &numPets) userRowsReturned = append(userRowsReturned, userSqlRow{Id: id, Name: name, Age: age, NumPets: numPets}) } return userRowsReturned }
func GetHomePage(rw http.ResponseWriter, req *http.Request) { type Page struct { Title, FrontendJSON string Conditions []Condition ConditionSqlRows []conditionSqlRow UserSqlRows []userSqlRow } frontendJSON, conditionSqlRows, formattedConditions := getFrontendJSON() p := Page{ Title: "home", Conditions: formattedConditions, FrontendJSON: frontendJSON, ConditionSqlRows: conditionSqlRows, UserSqlRows: getUserSqlRows(), } common.Templates = template.Must(template.ParseFiles(common.AppDir+"/templates/home/home.html", common.LayoutPath)) err := common.Templates.ExecuteTemplate(rw, "base", p) common.CheckError(err, 2) }