Example #1
0
        private object ProcessFLORExpr(Notation notation, Notation.Record rec)
        {                        
            int stack_pos = _varTable.BeginFrame();
            Symbol[] arr = Lisp.ToArray<Symbol>(rec.args[0]);
            List<FLWORItem> flworItems = new List<FLWORItem>();
            bool stable = false;
            for (int k = 0; k < arr.Length; k++)
            {
                Notation.Record[] recs = notation.Select(arr[k]);
                switch (recs[0].descriptor)
                {
                    case Descriptor.For:
                        {
                            Symbol[] arr2 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                            for (int s = 0; s < arr2.Length; s++)
                            {
                                Notation.Record[] recs2 = notation.Select(arr2[s], 
                                    Descriptor.ForClauseOperator, 4);
                                if (recs.Length > 0)
                                {
                                    FLWORItem item = new FLWORItem();
                                    item.desc = Descriptor.For;
                                    if (notation.Flag(arr[k], Descriptor.Parallel))
                                        item.parallel = true;
                                    VarName name = (VarName)recs2[0].Arg0;
                                    item.var = ProcessVarName(name);
                                    item.assignExpr = ProcessExprSingle(notation, recs2[0].Arg3);
                                    item.convert = true;
                                    if (recs2[0].Arg1 == null)
                                    {
                                        XQuerySequenceType type = EvalExprType(item.assignExpr);
                                        if (type.TypeCode != XmlTypeCode.Item)
                                        {
                                            item.varType = new XQuerySequenceType(type.TypeCode, XmlTypeCardinality.One);
                                            item.convert = false;
                                        }
                                        else
                                            item.varType = XQuerySequenceType.Item;
                                    }
                                    else
                                        item.varType = ProcessTypeDecl(notation, recs2[0].Arg1);
                                    item.pos = null;
                                    if (recs2[0].Arg2 != null)
                                    {
                                        VarName posname = (VarName)recs2[0].Arg2;
                                        if (posname.Name == name.Name)
                                            throw new XQueryException(Properties.Resources.XQST0089, posname.Name);
                                        item.pos = ProcessVarName(posname);
                                    }                                    
                                    _varTable.PushVar(item.var, item.varType);
                                    if (item.pos != null)
                                        _varTable.PushVar(item.pos, new XQuerySequenceType(XmlTypeCode.Integer));
                                    flworItems.Add(item);
                                }
                            }
                        }
                        break;

                    case Descriptor.Let:
                        {
                            Symbol[] arr2 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                            for (int s = 0; s < arr2.Length; s++)
                            {
                                Notation.Record[] recs2 = notation.Select(arr2[s],
                                    Descriptor.LetClauseOperator, 3);
                                if (recs.Length > 0)
                                {
                                    FLWORItem item = new FLWORItem();
                                    item.desc = Descriptor.Let;
                                    item.var = ProcessVarName((VarName)recs2[0].Arg0);
                                    item.assignExpr = ProcessExprSingle(notation, recs2[0].Arg2);
                                    item.convert = true;                                    
                                    if (recs2[0].Arg1 == null)
                                    {
                                        XQuerySequenceType type = EvalExprType(item.assignExpr);
                                        if (type.Cardinality == XmlTypeCardinality.One)
                                        {
                                            item.varType = type;
                                            item.convert = false;
                                        }
                                        else
                                            item.varType = XQuerySequenceType.Item;
                                    }
                                    else
                                        item.varType = ProcessTypeDecl(notation, recs2[0].Arg1);
                                    _varTable.PushVar(item.var, item.varType);
                                    flworItems.Add(item);
                                }
                            }
                        }
                        break;
                }
            }
            XQueryOrderSpec[] orderSpec = null;
            XQueryExprBase expr = XQueryExpr.Create(_context,
                new object[] { ProcessExprSingle(notation, rec.Arg3) });
            if (rec.Arg2 != null)
            {
                Notation.Record[] recs = notation.Select(rec.Arg2, new Descriptor[] { 
                    Descriptor.OrderBy, Descriptor.StableOrderBy });
                if (recs.Length > 0)
                {
                    if (!(expr is XQueryExpr))
                        expr = new XQueryExpr(_context, new object[] { expr.ToLispFunction() });
                    stable = (recs[0].descriptor == Descriptor.StableOrderBy);
                    Symbol[] arr3 = Lisp.ToArray<Symbol>(recs[0].args[0]);
                    orderSpec = new XQueryOrderSpec[arr3.Length];
                    object[] sortKey = new object[arr3.Length];
                    for (int k = 0; k < arr3.Length; k++)
                    {
                        sortKey[k] = ProcessExprSingle(notation, arr3[k]);
                        orderSpec[k].emptySpec = _context.EmptyOrderSpec;
                        orderSpec[k].collation = _context.DefaultCollation;
                        Notation.Record[] recs1 = notation.Select(arr3[k], Descriptor.Modifier, 1);
                        if (recs1.Length > 0)
                        {
                            Symbol[] modifier = Lisp.ToArray<Symbol>(recs1[0].args[0]);
                            if (modifier[0] != null)
                                switch (((TokenWrapper)modifier[0]).Data)
                                {
                                    case Token.ASCENDING:
                                        orderSpec[k].direction = XQueryOrderDirection.Ascending;
                                        break;
                                    case Token.DESCENDING:
                                        orderSpec[k].direction = XQueryOrderDirection.Descending;
                                        break;
                                }
                            if (modifier[1] != null)
                                switch (((TokenWrapper)modifier[1]).Data)
                                {
                                    case Token.EMPTY_GREATEST:
                                        orderSpec[k].emptySpec = XQueryEmptyOrderSpec.Greatest;
                                        break;
                                    case Token.EMPTY_LEAST:
                                        orderSpec[k].emptySpec = XQueryEmptyOrderSpec.Least;
                                        break;
                                }
                            if (modifier[2] != null)
                                orderSpec[k].collation = ((Literal)modifier[2]).Data;
                        }
                    }
                    ((XQueryExpr)expr).Annotation = sortKey;
                }
            }
            object whereExpr = null;
            if (rec.Arg1 != null)
            {
                Notation.Record[] recs3 = notation.Select(rec.Arg1, Descriptor.Where, 1);
                if (recs3.Length > 0)
                    whereExpr = ProcessExprSingle(notation, recs3[0].Arg0);
            }
            for (int k = flworItems.Count - 1; k >= 0; k--)
            {
                FLWORItem item = flworItems[k];                
                switch (item.desc)
                {
                    case Descriptor.For:
                        expr = new XQueryFLWOR(_context, item.var, item.varType, item.pos, item.assignExpr, expr, item.convert);
                        if (item.parallel && _context.EnableHPC)
                            ((XQueryFLWOR)expr).Parallel = true;
                        break;

                    case Descriptor.Let:
                        expr = new XQueryLET(_context, item.var, item.varType, item.assignExpr, expr, item.convert);
                        break;
                }
                if (k == flworItems.Count - 1)
                {
                    XQueryFLWORBase flworExpr = (XQueryFLWORBase)expr;
                    flworExpr.ConditionExpr = whereExpr;
                }
            }
            _varTable.EndFrame(stack_pos);
            if (orderSpec != null)
                expr = new XQuerySorter(_context, orderSpec, stable, expr);
            return Lisp.List(ID.DynExecuteExpr, expr, ID.Context, Lisp.ARGV, Lisp.MPOOL);
        }