Esempio n. 1
0
        public virtual string ToSql(QueryFieldSet fieldSet)
        {
            StringBuilder sb = new StringBuilder();

            AppendSql(sb, fieldSet);
            return(sb.ToString());
        }
Esempio n. 2
0
        public virtual string ToXml(QueryFieldSet fieldSet, bool pretty)
        {
            XmlDocument doc = new XmlDocument();

            XmlElement request = doc.CreateElement("request");

            doc.AppendChild(request);

            XmlElement query = doc.CreateElement("query");

            query.SetAttribute("banshee-version", "1");
            request.AppendChild(query);

            AppendXml(doc, query, fieldSet);

            if (!pretty)
            {
                return(doc.OuterXml);
            }

            using (StringWriter sw = new StringWriter()) {
                using (XmlTextWriter xtw = new XmlTextWriter(sw)) {
                    xtw.Formatting  = System.Xml.Formatting.Indented;
                    xtw.Indentation = 2;
                    doc.WriteTo(xtw);
                    return(sw.ToString());
                }
            }
        }
Esempio n. 3
0
        public override void AppendSql(StringBuilder sb, QueryFieldSet fieldSet)
        {
            if (ChildCount == 0)
            {
                return;
            }

            if (Keyword != Keyword.Not)
            {
                sb.Append("(");
                bool first = true;
                foreach (QueryNode child in Children)
                {
                    if (!first)
                    {
                        sb.AppendFormat(" {0} ", Keyword);
                    }
                    else
                    {
                        first = false;
                    }
                    child.AppendSql(sb, fieldSet);
                }
                sb.Append(")");
            }
            else
            {
                sb.Append("NOT(");
                Children [0].AppendSql(sb, fieldSet);
                sb.Append(")");
            }
        }
Esempio n. 4
0
        public override void AppendSql(StringBuilder sb, QueryFieldSet fieldSet)
        {
            if (Field == null)
            {
                sb.Append("(");
                int emitted = 0;

                foreach (QueryField field in fieldSet.Fields)
                {
                    if (field.IsDefault)
                    {
                        if (EmitTermMatch(sb, field, emitted > 0))
                        {
                            emitted++;
                        }
                    }
                }

                sb.Append(")");
            }
            else
            {
                EmitTermMatch(sb, Field, false);
            }
        }
Esempio n. 5
0
        public override QueryNode BuildTree(QueryFieldSet fieldSet)
        {
            field_set = fieldSet;
            root      = current_parent = new QueryListNode(Keyword.And);
            bool last_was_term = false;

            while (true)
            {
                var token = Scan();

                if (token.ID == TokenID.Unknown)
                {
                    break;
                }

                token.Column = token_start_column;
                token.Line   = token_start_line;

                // If we have two terms in a row, put an AND between them
                if (last_was_term && token.ID == TokenID.Term)
                {
                    ParseToken(new QueryToken(TokenID.And));
                }

                ParseToken(token);

                last_was_term = token.ID == TokenID.Term;
            }

            return(root.Trim());
        }
        public override QueryNode BuildTree(QueryFieldSet fieldSet)
        {
            field_set = fieldSet;
            XmlDocument doc = new XmlDocument();

            try {
                doc.LoadXml(str);
                XmlElement request = doc.FirstChild as XmlElement;
                if (request == null || request.Name != "request")
                {
                    throw new Exception("Invalid request");
                }

                XmlElement query = request.FirstChild as XmlElement;
                if (query == null || query.Name != "query" || query.GetAttribute("banshee-version") != "1")
                {
                    throw new Exception("Invalid query");
                }

                QueryNode node = Parse(query.FirstChild as XmlElement, null);
                return((node != null) ? node.Trim() : null);
            } catch (Exception) {
            }
            return(null);
        }
        public override void AppendXml(XmlDocument doc, XmlNode parent, QueryFieldSet fieldSet)
        {
            if (ChildCount == 0)
                return;

            XmlElement node = doc.CreateElement (Keyword.ToString ().ToLower ());
            parent.AppendChild (node);
            foreach (QueryNode child in Children)
                child.AppendXml (doc, node, fieldSet);
        }
Esempio n. 8
0
        public static QueryTermNode ParseUserQuery(QueryFieldSet field_set, string token)
        {
            QueryTermNode term = new QueryTermNode();

            // See if the query specifies a field, and if so, pull out the operator as well
            string field_alias = field_set.FindAlias(token);

            if (field_alias != null)
            {
                term.Field = field_set [field_alias];
                string token_without_field = token.Substring(field_alias.Length);

                foreach (QueryValue val in term.Field.CreateQueryValues())
                {
                    term.Value = val;

                    string op_alias = term.Value.OperatorSet.FindAlias(token_without_field);
                    if (op_alias != null)
                    {
                        term.Operator = term.Value.OperatorSet [op_alias];
                        int    field_separator = token.IndexOf(op_alias);
                        string temp            = token.Substring(field_separator + op_alias.Length);

                        term.Value.ParseUserQuery(temp);

                        if (!term.Value.IsEmpty)
                        {
                            break;
                        }
                    }

                    term.Operator = null;
                    term.Value    = null;
                }
            }

            if (term.Value == null)
            {
                term.Field    = null;
                term.Value    = QueryValue.CreateFromUserQuery(token, term.Field);
                term.Operator = StringQueryValue.Contains;
            }

            return(term);
        }
Esempio n. 9
0
        public override void AppendXml(XmlDocument doc, XmlNode parent, QueryFieldSet fieldSet)
        {
            XmlElement op_node = doc.CreateElement(op == null ? "contains" : op.Name);

            parent.AppendChild(op_node);

            QueryField field = Field;

            if (field != null)
            {
                XmlElement field_node = doc.CreateElement("field");
                field_node.SetAttribute("name", field.Name);
                op_node.AppendChild(field_node);
            }

            XmlElement val_node = doc.CreateElement(Value.XmlElementName);

            Value.AppendXml(val_node);
            op_node.AppendChild(val_node);
        }
Esempio n. 10
0
        public override QueryNode BuildTree(QueryFieldSet fieldSet)
        {
            field_set = fieldSet;
            var doc = new XmlDocument();

            try {
                doc.LoadXml(str);
                if (!(doc.FirstChild is XmlElement request) || request.Name != "request")
                {
                    throw new Exception("Invalid request");
                }

                if (!(request.FirstChild is XmlElement query) || query.Name != "query" || query.GetAttribute("banshee-version") != "1")
                {
                    throw new Exception("Invalid query");
                }

                var node = Parse(query.FirstChild as XmlElement, null);
                return(node?.Trim());
            } catch (Exception) {
            }
            return(null);
        }
Esempio n. 11
0
 public string ToXml(QueryFieldSet fieldSet)
 {
     return(ToXml(fieldSet, false));
 }
Esempio n. 12
0
 public abstract void AppendSql(StringBuilder sb, QueryFieldSet fieldSet);
Esempio n. 13
0
 public abstract void AppendXml(XmlDocument doc, XmlNode parent, QueryFieldSet fieldSet);
 public abstract QueryNode BuildTree(QueryFieldSet fieldSet);
Esempio n. 15
0
 public static QueryNode Parse(string input, QueryFieldSet fieldSet)
 {
     return(new UserQueryParser(input).BuildTree(fieldSet));
 }