示例#1
0
func main() {

	if len(os.Args) != 2 {
		usage()
		return
	}

	s := os.Args[1]

	// Creates the query

	query, err := charlatan.QueryFromString(s)
	if err != nil {
		fmt.Println(">>> ", err)
		return
	}

	reader, err := os.Open(query.From())
	if err != nil {
		fmt.Printf(">>> Error opening %s: %v\n", query.From(), err)
		return
	}

	executeRequest(csv.NewReader(reader), query)
}
示例#2
0
func runTest() (string, error) {
	s, err := generateQueryString()
	if err != nil {
		log.Fatalf("Query generation error: %v", err)
	}

	_, err = charlatan.QueryFromString(s)
	return s, err
}
示例#3
0
func main() {
	var limit int64

	if len(os.Args) != 2 {
		usage()
	}

	query, err := charlatan.QueryFromString(os.Args[1])
	if err != nil {
		fatalf("Error: %v\n", err)
	}

	reader, err := os.Open(query.From())
	if err != nil {
		fatalf("Error: %v\n", err)
	}

	defer reader.Close()

	decoder := json.NewDecoder(reader)

	hasLimit := query.HasLimit()

	if hasLimit {
		limit = query.Limit()

		if limit <= 0 {
			return
		}
	}

	line := 0
	offset := query.StartingAt()

	for {
		r, err := record.NewJSONRecordFromDecoder(decoder)

		// end of file
		if err == io.EOF {
			return
		}

		// unknown error
		if err != nil {
			fatalf("Error at line %d: %v\n", line, err)
		}

		match, err := query.Evaluate(r)
		if err != nil {
			fatalf("Error while evaluating the query at line %d: %v\n", line, err)
		}

		if !match {
			continue
		}

		offset--

		if offset > 0 {
			continue
		}

		line++

		// extract the fields
		values, err := query.FieldsValues(r)
		if err != nil {
			fatalf("Error while extracting the fields at line %d: %v\n", line, err)
		}

		fmt.Println("# ", values)
		if hasLimit {
			limit--
			if limit <= 0 {
				break
			}
		}
	}
}