Beispiel #1
0
        //'DELETE FROM' NAME ('WHERE' expression)?
        private Query ParseDelete(RQ q)
        {
            if (!q.IsNext(tokens["delete"]))
            {
                return(null);
            }
            q.Pop();

            if (!q.IsNext(tokens["from"]))
            {
                return(null);
            }
            q.Pop();

            TableSource source = ParseSource(q);

            DeleteFromQuery df = new DeleteFromQuery();

            df.table = source;
            df.eval  = new TrueExpression();

            if (q.IsNext(tokens["where"]))
            {
                q.Pop();
                Expression e = ParseExpression(q);
                if (e == null)
                {
                    e = new TrueExpression();
                }
                else
                {
                    df.eval = e;
                }
            }

            return(df);
        }
Beispiel #2
0
        //'UPDATE' NAME 'SET' (NAME '=' expression) (',' NAME '=' expression)* ('WHERE' expression)?
        private Query ParseUpdate(RQ q)
        {
            if (!q.IsNext(tokens["update"]))
            {
                return(null);
            }
            q.Pop();

            TableSource s = ParseSource(q);

            if (!q.IsNext(tokens["set"]))
            {
                return(null);
            }
            q.Pop();

            List <string>     fields = new List <string>();
            List <Expression> values = new List <Expression>();

            while (true)
            {
                if (!q.IsNext(tokens["name"]))
                {
                    break;
                }
                string field = q.Pop().value;
                if (!q.IsNext(tokens["compare"]))
                {
                    break;
                }
                string op = q.Peek().value;
                if (op != "=")
                {
                    break;
                }
                q.Pop();

                Expression e2 = ParseExpression(q);

                fields.Add(field);
                values.Add(e2);

                if (!q.IsNext(tokens["comma"]))
                {
                    break;
                }
            }

            if (!q.IsNext(tokens["where"]))
            {
                return(null);
            }
            q.Pop();

            Expression e = ParseExpression(q);

            if (e == null)
            {
                e = new TrueExpression();
            }

            UpdateQuery u = new UpdateQuery();

            u.table  = s;
            u.fields = fields.ToArray();
            u.values = values.ToArray();
            u.exp    = e;
            return(u);
        }
Beispiel #3
0
        //'SELECT' ('*' | (NAME (',' NAME)*)) 'FROM' table (WHERE expression)?;
        private Query ParseSelect(RQ q)
        {
            if (!q.IsNext(tokens["select"]))
            {
                return(null);
            }
            q.Pop();

            SelectFromQuery sf = new SelectFromQuery();

            if (q.IsNext(tokens["*"]))
            {
                sf.all = true;
                q.Pop();
            }
            else
            {
                List <string> names = new List <string>();
                while (true)
                {
                    if (!q.IsNext(tokens["name"]))
                    {
                        break;
                    }
                    string name = q.Pop().value;

                    names.Add(name);

                    if (!q.IsNext(tokens["comma"]))
                    {
                        break;
                    }
                    q.Pop();
                }
                sf.fields = names.ToArray();
            }

            if (!q.IsNext(tokens["from"]))
            {
                return(null);
            }
            q.Pop();

            TableSource source = ParseSource(q);

            if (!q.IsNext(tokens["where"]))
            {
                return(null);
            }
            q.Pop();

            Expression e = ParseExpression(q);

            if (e == null)
            {
                e = new TrueExpression();
            }

            sf.source = source;
            sf.expr   = e;
            return(sf);
        }