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
        //NAME | NAME JOIN SOURCE
        private TableSource ParseSource(RQ q)
        {
            if (!q.IsNext(tokens["name"]))
            {
                return(null);
            }
            string left = q.Pop().value;

            if (!q.IsNext(tokens["join"]))
            {
                RawSource r = new RawSource();
                r.table = left;
                return(r);
            }
            q.Pop();

            TableSource right = ParseSource(q);

            JoinSource j = new JoinSource();

            j.left  = left;
            j.right = right;
            return(j);
        }
Beispiel #3
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 #4
0
        //'INSERT INTO' NAME ('(' NAME (',' NAME)* ')') 'VALUES' expression (',' expression)*;
        private Query ParseInsert(RQ q)
        {
            if (!q.IsNext(tokens["insert"]))
            {
                return(null);
            }
            q.Pop();

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

            TableSource source = ParseSource(q);

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

            List <string> names = new List <string>();

            while (q.IsNext(tokens["name"]))
            {
                string name = q.Pop().value;

                names.Add(name);

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

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

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

            List <Expression> ex = new List <Expression>();

            while (true)
            {
                Expression e = ParseExpression(q);
                if (e == null)
                {
                    break;
                }

                ex.Add(e);

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

            InsertIntoQuery ii = new InsertIntoQuery();

            ii.exp    = ex.ToArray();
            ii.fields = names.ToArray();
            ii.source = source;
            return(ii);
        }
Beispiel #5
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);
        }