Example #1
0
func handleInteractiveMode() {

	unqlParser := tuql.NewTuqlParser(*debugTokens, *debugGrammar, *crashHard)
	naivePlanner := naiveplanner.NewNaivePlanner()
	naiveOptimizer := naiveoptimizer.NewNaiveOptimizer()
	nullOptimizer := nulloptimizer.NewNullOptimizer()

	currentUser, err := user.Current()
	if err != nil {
		log.Printf("Unable to determine home directory, history file disabled")
	}

	var liner = liner.NewLiner()
	defer liner.Close()

	LoadHistory(liner, currentUser)

	go signalCatcher(liner)

	for {
		line, err := liner.Prompt("tuq> ")
		if err != nil {
			break
		}

		if line == "" {
			continue
		}

		UpdateHistory(liner, currentUser, line)

		query, err := unqlParser.Parse(line)
		if err != nil {
			log.Printf("%v", err)
		} else {
			if *debugGrammar {
				log.Printf("Query is: %#v", query)
			}
			if query.WasParsedSuccessfully() {
				// check to make sure the query is semantically valid
				err := query.Validate()
				if err != nil {
					log.Printf("%v", err)
				} else {
					plans := naivePlanner.Plan(*query)

					if plans != nil {
						var plan planner.Plan
						if *disableOptimizer {
							plan = nullOptimizer.Optimize(plans)
						} else {
							plan = naiveOptimizer.Optimize(plans)
						}

						if query.IsExplainOnly {
							result := plan.Explain()
							if err != nil {
								log.Printf("Error: %v", err)
							} else {
								FormatChannelOutput(result, os.Stdout)
							}
						} else {
							result := plan.Run()
							if err != nil {
								log.Printf("Error: %v", err)
							} else {
								FormatChannelOutput(result, os.Stdout)
							}
						}
					}
				}

			}
		}
	}

}
Example #2
0
File: stdin.go Project: mschoch/tuq
func handleStdinMode() {

	unqlParser := tuql.NewTuqlParser(*debugTokens, *debugGrammar, *crashHard)
	naivePlanner := naiveplanner.NewNaivePlanner()
	naiveOptimizer := naiveoptimizer.NewNaiveOptimizer()
	nullOptimizer := nulloptimizer.NewNullOptimizer()

	reader := bufio.NewReader(os.Stdin)

	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			break
		}

		if line == "" {
			continue
		}

		query, err := unqlParser.Parse(line)
		if err != nil {
			log.Printf("%v", err)
		} else {
			if *debugGrammar {
				log.Printf("Query is: %#v", query)
			}
			if query.WasParsedSuccessfully() {
				// check to make sure the query is semantically valid
				err := query.Validate()
				if err != nil {
					log.Printf("%v", err)
				} else {
					plans := naivePlanner.Plan(*query)

					if plans != nil {

						var plan planner.Plan
						if *disableOptimizer {
							plan = nullOptimizer.Optimize(plans)
						} else {
							plan = naiveOptimizer.Optimize(plans)
						}

						if query.IsExplainOnly {
							result := plan.Explain()
							if err != nil {
								log.Printf("Error: %v", err)
							} else {
								FormatChannelOutput(result, os.Stdout)
							}
						} else {
							result := plan.Run()
							if err != nil {
								log.Printf("Error: %v", err)
							} else {
								FormatChannelOutput(result, os.Stdout)
							}
						}

					}
				}

			}
		}
	}

}
Example #3
0
File: http.go Project: mschoch/tuq
func doPost(w http.ResponseWriter, req *http.Request) {

	d := json.NewDecoder(req.Body)
	request := map[string]interface{}{}

	err := d.Decode(&request)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintf(w, "Error parsing request JSON: %v", err)
		return
	}

	line, ok := request["query"].(string)
	if !ok {
		w.WriteHeader(500)
		fmt.Fprintf(w, "Error reading query string: %v", err)
		return
	}

	unqlParser := tuql.NewTuqlParser(false, false, *crashHard)
	naiveOptimizer := naiveoptimizer.NewNaiveOptimizer()
	nullOptimizer := nulloptimizer.NewNullOptimizer()
	query, err := unqlParser.Parse(line)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintf(w, "Error parsing query string: %v", err)
		return
	} else {

		if query.WasParsedSuccessfully() {
			// check to make sure the query is semantically valid
			err := query.Validate()

			if err != nil {
				w.WriteHeader(500)
				fmt.Fprintf(w, "Error validating query string: %v", err)
				return
			} else {
				naivePlanner := naiveplanner.NewNaivePlanner()
				plans := naivePlanner.Plan(*query)

				if plans != nil {
					var plan planner.Plan
					if *disableOptimizer {
						plan = nullOptimizer.Optimize(plans)
					} else {
						plan = naiveOptimizer.Optimize(plans)
					}

					if query.IsExplainOnly {
						result := plan.Explain()
						if err != nil {
							w.WriteHeader(500)
							fmt.Fprintf(w, "Error explaining query string: %v", err)
							return
						} else {
							FormatChannelOutput(result, w)
						}
					} else {
						result := plan.Run()
						if err != nil {
							w.WriteHeader(500)
							fmt.Fprintf(w, "Error running query string: %v", err)
							return
						} else {
							FormatChannelOutput(result, w)
						}
					}

				}
			}
		} else {
			w.WriteHeader(500)
			fmt.Fprintf(w, "Error parsing query string: %v", err)
			return
		}
	}
}