Beispiel #1
0
func listFilesForQuery(store *storage.Storage, tx *storage.Tx, queryText, path string, dirOnly, fileOnly, print0, showCount, explicitOnly, ignoreCase bool, sort string) (error, warnings) {
	log.Info(2, "parsing query")

	expression, err := query.Parse(queryText)
	if err != nil {
		return fmt.Errorf("could not parse query: %v", err), nil
	}

	log.Info(2, "checking tag names")

	warnings := make(warnings, 0, 10)

	tagNames, err := query.TagNames(expression)
	if err != nil {
		return fmt.Errorf("could not identify tag names: %v", err), nil
	}

	tags, err := store.TagsByCasedNames(tx, tagNames, ignoreCase)
	for _, tagName := range tagNames {
		if err := entities.ValidateTagName(tagName); err != nil {
			warnings = append(warnings, err.Error())
			continue
		}

		if !tags.ContainsCasedName(tagName, ignoreCase) {
			warnings = append(warnings, fmt.Sprintf("no such tag '%v'", tagName))
			continue
		}
	}

	valueNames, err := query.ExactValueNames(expression)
	if err != nil {
		return fmt.Errorf("could not identify value names: %v", err), nil
	}

	values, err := store.ValuesByCasedNames(tx, valueNames, ignoreCase)
	for _, valueName := range valueNames {
		if err := entities.ValidateValueName(valueName); err != nil {
			warnings = append(warnings, err.Error())
			continue
		}

		if !values.ContainsCasedName(valueName, ignoreCase) {
			warnings = append(warnings, fmt.Sprintf("no such value '%v'", valueName))
			continue
		}
	}

	log.Info(2, "querying database")

	files, err := store.FilesForQuery(tx, expression, path, explicitOnly, ignoreCase, sort)
	if err != nil {
		if strings.Index(err.Error(), "parser stack overflow") > -1 {
			return fmt.Errorf("the query is too complex (see the troubleshooting wiki for how to increase the stack size)"), warnings
		} else {
			return fmt.Errorf("could not query files: %v", err), warnings
		}
	}

	if err = listFiles(tx, files, dirOnly, fileOnly, print0, showCount); err != nil {
		return err, warnings
	}

	return nil, warnings
}