/// <summary>
        /// Creates the specified selectorElement.
        /// </summary>
        public ControlFlowBase Create(SelectorControlFlowElement selector)
        {
            Assume.NotNull(selector, nameof(selector));

            var combinator = SelectorResolver.Create(selector.SelectorElement);

            return(new SelectorControlFlow(combinator));
        }
Esempio n. 2
0
        public virtual QueryModel CreateNewQueryModel(LambdaExpression selector)
        {
            QueryModel newQueryModel = this._queryModel.Clone();

            ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(selector.Parameters[0], this._queryModel.ResultModel);
            IObjectModel             newResultModel  = SelectorResolver.Resolve(selector, scopeParameters, this._queryModel.ScopeTables);

            newQueryModel.ResultModel = newResultModel;

            return(newQueryModel);
        }
Esempio n. 3
0
        public virtual QueryModel CreateNewQueryModel(LambdaExpression selector)
        {
            QueryModel newQueryModel = this._queryModel.Clone();

            ComplexObjectModel complexObjectModel = this._queryModel.ResultModel as ComplexObjectModel;

            if (complexObjectModel != null)
            {
                complexObjectModel.SetupFilters(this._queryModel.IgnoreFilters);
            }

            ScopeParameterDictionary scopeParameters = this._queryModel.ScopeParameters.Clone(selector.Parameters[0], this._queryModel.ResultModel);
            IObjectModel             newResultModel  = SelectorResolver.Resolve(selector, scopeParameters, this._queryModel.ScopeTables);

            newQueryModel.ResultModel = newResultModel;

            return(newQueryModel);
        }
Esempio n. 4
0
        public virtual ResultElement CreateNewResult(LambdaExpression selector)
        {
            ResultElement result = new ResultElement(this._resultElement.ScopeParameters, this._resultElement.ScopeTables);

            result.FromTable = this._resultElement.FromTable;

            ScopeParameterDictionary scopeParameters = this._resultElement.ScopeParameters.Clone(selector.Parameters[0], this._resultElement.MappingObjectExpression);

            IMappingObjectExpression r = SelectorResolver.Resolve(selector, scopeParameters, this._resultElement.ScopeTables);

            result.MappingObjectExpression = r;
            result.Orderings.AddRange(this._resultElement.Orderings);
            result.AppendCondition(this._resultElement.Condition);

            result.GroupSegments.AddRange(this._resultElement.GroupSegments);
            result.AppendHavingCondition(this._resultElement.HavingCondition);

            return(result);
        }
        public override IQueryState Visit(JoinQueryExpression exp)
        {
            IQueryState qs = QueryExpressionResolver.Resolve(exp.PrevExpression, this._scopeParameters, this._scopeTables);

            ResultElement resultElement = qs.ToFromQueryResult();

            List <IMappingObjectExpression> moeList = new List <IMappingObjectExpression>();

            moeList.Add(resultElement.MappingObjectExpression);

            foreach (JoiningQueryInfo joiningQueryInfo in exp.JoinedQueries)
            {
                ScopeParameterDictionary scopeParameters = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count);
                for (int i = 0; i < moeList.Count; i++)
                {
                    ParameterExpression p = joiningQueryInfo.Condition.Parameters[i];
                    scopeParameters[p] = moeList[i];
                }

                JoinQueryResult joinQueryResult = JoinQueryExpressionResolver.Resolve(joiningQueryInfo.Query.QueryExpression, resultElement, joiningQueryInfo.JoinType, joiningQueryInfo.Condition, scopeParameters);

                var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type);

                if (joiningQueryInfo.JoinType == JoinType.LeftJoin)
                {
                    joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking);
                }
                else if (joiningQueryInfo.JoinType == JoinType.RightJoin)
                {
                    foreach (IMappingObjectExpression item in moeList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }
                else if (joiningQueryInfo.JoinType == JoinType.FullJoin)
                {
                    joinQueryResult.MappingObjectExpression.SetNullChecking(nullChecking);
                    foreach (IMappingObjectExpression item in moeList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }

                resultElement.FromTable.JoinTables.Add(joinQueryResult.JoinTable);
                moeList.Add(joinQueryResult.MappingObjectExpression);
            }

            ScopeParameterDictionary scopeParameters1 = resultElement.ScopeParameters.Clone(resultElement.ScopeParameters.Count + moeList.Count);

            for (int i = 0; i < moeList.Count; i++)
            {
                ParameterExpression p = exp.Selector.Parameters[i];
                scopeParameters1[p] = moeList[i];
            }
            IMappingObjectExpression moe = SelectorResolver.Resolve(exp.Selector, scopeParameters1, resultElement.ScopeTables);

            resultElement.MappingObjectExpression = moe;

            GeneralQueryState queryState = new GeneralQueryState(resultElement);

            return(queryState);
        }
Esempio n. 6
0
 private void ScanDynatableAccessors(QueryNode queryNode, SelectorResolver resolver)
 {
     if (queryNode is DataFilter || queryNode is DataAggregator || queryNode is DataSorter)
         ScanDynatableAccessors(queryNode.ChildNodes[0], resolver);
     else if (queryNode is DataJoin || queryNode is DetailJoin)
     {
         ScanDynatableAccessors(queryNode.ChildNodes[0], resolver);
         ScanDynatableAccessors(queryNode.ChildNodes[1], resolver);
     }
     else if (queryNode is DynatableAccessor)
     {
         DynatableAccessor accessor = (DynatableAccessor)queryNode;
         object[] terms = Lisp.GetTerms(accessor.AccessPredicate);
         foreach (object o1 in resolver.Resultsets)
             foreach(object o2 in terms)
                 if (o1 == o2)
                 {
                     accessor.CopyContext = true;
                     break;
                 }
     }
 }
Esempio n. 7
0
 public override Resultset Get(QueryContext queryContext, object[] parameters)
 {
     // Prepare bindings
     Resultset source = ChildNodes[0].Get(queryContext, parameters);
     List<ColumnBinding> bindings = new List<ColumnBinding>();            
     ExpressionTransformer transformer = new ExpressionTransformer(bindings);
     foreach (RowType.TypeInfo ti in source.RowType.Fields)
         foreach (RowType.TypeInfo nested_ti in ti.NestedType.Fields)
             if (!nested_ti.IsHidden && nested_ti.IsNatural)
             {
                 ColumnBinding b = new ColumnBinding();
                 b.typecode = Type.GetTypeCode(nested_ti.DataType);
                 b.rnum = ti.Ordinal;
                 b.TableName = ti.Name;
                 b.Name = nested_ti.Name;
                 b.fieldType = nested_ti;
                 b.natural = nested_ti.IsNatural;
                 b.container = nested_ti.IsContainer;
                 b.caseSensitive = nested_ti.IsCaseSensitive;
                 b.data = new SymbolLink(nested_ti.DataType);
                 bindings.Add(b);
                 if (nested_ti.IsContainer)
                     transformer.NeedTransform = true;
             }
     foreach (RowType.TypeInfo ti in source.RowType.Fields)
         foreach (RowType.TypeInfo nested_ti in ti.NestedType.Fields)
             if (!nested_ti.IsHidden && !nested_ti.IsNatural)
             {
                 ColumnBinding b = new ColumnBinding();
                 b.typecode = Type.GetTypeCode(nested_ti.DataType);
                 b.rnum = ti.Ordinal;
                 b.TableName = ti.Name;
                 b.Name = nested_ti.Name;
                 b.fieldType = nested_ti;
                 b.natural = nested_ti.IsNatural;
                 b.container = nested_ti.IsContainer;
                 b.caseSensitive = nested_ti.IsCaseSensitive;
                 b.data = new SymbolLink(nested_ti.DataType);
                 bindings.Add(b);
                 if (nested_ti.IsContainer)
                     transformer.NeedTransform = true;
             }
     // Expand columns.
     // On this step we transform select table.* and select * to select field1,...
     List<Column> columns = new List<Column>();
     if (_targets == null) // Select fields from all selected tables
     {
         foreach (ColumnBinding b in bindings)
             columns.Add(new Column(ATOM.Create(null, new string[] { b.TableName, b.Name }, false)));
     }
     else
         foreach (Column col in _targets)
         {
             if (Lisp.IsFunctor(col.Expr, Table)) // Select fields from specified table
             {
                 bool found = false;
                 String name = (String)Lisp.Second(col.Expr);
                 var tableBindings =
                         from b in bindings
                         where b.TableName == name
                         select b;
                 foreach (ColumnBinding b in tableBindings)
                 {
                     columns.Add(new Column(ATOM.Create(null, new string[] { b.TableName, b.Name }, false)));
                     found = true;
                 }
                 if (!found)
                     throw new ESQLException(Properties.Resources.InvalidIdentifier, name);
             }
             else // Select expression specified
                 if (transformer.NeedTransform)
                 {
                     Column c = new Column();
                     c.Alias = col.Alias;
                     c.Expr = transformer.Process(col.Expr);
                     columns.Add(c);
                 }
                 else
                     columns.Add(col);
         }
     // Create demand context   
     FunctionLink[] compiledBody = new FunctionLink[columns.Count];
     SelectorResolver resolver = new SelectorResolver(new Binder(bindings));
     SelectorContext context = new SelectorContext(this, _distinct, columns.ToArray(),
         resolver, source, queryContext, parameters, compiledBody);
     // Create columns type info and generate unique column names by field name and alias
     RowType.TypeInfo[] fields = new RowType.TypeInfo[columns.Count];
     List<String> fieldNames = new List<string>();
     int p = 1;
     for (int k = 0; k < fields.Length; k++)
     {
         String name;
         RowType.TypeInfo fieldType = null;                
         object expr = columns[k].Expr;
         compiledBody[k] = new FunctionLink();
         Type resType = context.LispExecutive.Compile(null, expr, compiledBody[k]);                
         if (Lisp.IsAtom(expr))
         {
             ColumnBinding b = resolver.GetBinding(expr);
             if (b != null)
                 fieldType = b.fieldType;
         }
         else if (Lisp.IsNode(expr) && expr is String)
         {
             fieldType = new RowType.TypeInfo(null, typeof(String),
                 TypeConverter.GetValueSize(expr));
         }
         else if (Lisp.IsFunctor(expr, ID.ParamRef))
         {
             object value = parameters[(int)Lisp.Arg1(expr)];
             fieldType = new RowType.TypeInfo(null, value.GetType(),
                 TypeConverter.GetValueSize(value));
         }
         else if (Lisp.IsFunctor(expr, ID.ToString))
         {
             if (Lisp.Length(expr) == 3)
                 fieldType = new RowType.TypeInfo(null, typeof(String), (int)Lisp.Arg2(expr));
             else
                 fieldType = new RowType.TypeInfo(null, typeof(String), 0);
         }
         else if (Lisp.IsFunctor(expr, ID.ToInt16))
             fieldType = new RowType.TypeInfo(null, typeof(Int16), 0);
         else if (Lisp.IsFunctor(expr, ID.ToInt32))
             fieldType = new RowType.TypeInfo(null, typeof(Int32), 0);
         else if (Lisp.IsFunctor(expr, ID.ToInt64))
             fieldType = new RowType.TypeInfo(null, typeof(Int64), 0);
         else if (Lisp.IsFunctor(expr, ID.ToSingle))
             fieldType = new RowType.TypeInfo(null, typeof(Single), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDouble))
             fieldType = new RowType.TypeInfo(null, typeof(Double), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDecimal))
             fieldType = new RowType.TypeInfo(null, typeof(Decimal), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDateTime))
             fieldType = new RowType.TypeInfo(null, typeof(DateTime), 0);
         else
             fieldType = new RowType.TypeInfo(null, resType, 0);
         if (!String.IsNullOrEmpty(columns[k].Alias))
             name = columns[k].Alias;
         else
             if (fieldType.Name == null)
                 name = String.Format("Expr{0}", p++);
             else
                 name = fieldType.Name;                
         fields[k] = new RowType.TypeInfo(k, 
             Util.CreateUniqueName(fieldNames, name), fieldType);                
     }
     ScanDynatableAccessors(ChildNodes[0], resolver);
     return new Resultset(new RowType(fields), context);            
 }        
Esempio n. 8
0
        public override IQueryState Visit(JoinQueryExpression exp)
        {
            IQueryState qs = QueryExpressionResolver.Resolve(exp.PrevExpression, this._scopeParameters, this._scopeTables);

            QueryModel queryModel = qs.ToFromQueryModel();

            List <IObjectModel> modelList = new List <IObjectModel>();

            modelList.Add(queryModel.ResultModel);

            foreach (JoinQueryInfo joinQueryInfo in exp.JoinedQueries)
            {
                ScopeParameterDictionary scopeParameters = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count);
                for (int i = 0; i < modelList.Count; i++)
                {
                    ParameterExpression p = joinQueryInfo.Condition.Parameters[i];
                    scopeParameters[p] = modelList[i];
                }

                JoinQueryResult joinQueryResult = JoinQueryExpressionResolver.Resolve(joinQueryInfo, queryModel, scopeParameters);

                var nullChecking = DbExpression.CaseWhen(new DbCaseWhenExpression.WhenThenExpressionPair(joinQueryResult.JoinTable.Condition, DbConstantExpression.One), DbConstantExpression.Null, DbConstantExpression.One.Type);

                if (joinQueryInfo.JoinType == JoinType.LeftJoin)
                {
                    joinQueryResult.ResultModel.SetNullChecking(nullChecking);
                }
                else if (joinQueryInfo.JoinType == JoinType.RightJoin)
                {
                    foreach (IObjectModel item in modelList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }
                else if (joinQueryInfo.JoinType == JoinType.FullJoin)
                {
                    joinQueryResult.ResultModel.SetNullChecking(nullChecking);
                    foreach (IObjectModel item in modelList)
                    {
                        item.SetNullChecking(nullChecking);
                    }
                }

                queryModel.FromTable.JoinTables.Add(joinQueryResult.JoinTable);
                modelList.Add(joinQueryResult.ResultModel);
            }

            ScopeParameterDictionary scopeParameters1 = queryModel.ScopeParameters.Clone(queryModel.ScopeParameters.Count + modelList.Count);

            for (int i = 0; i < modelList.Count; i++)
            {
                ParameterExpression p = exp.Selector.Parameters[i];
                scopeParameters1[p] = modelList[i];
            }
            IObjectModel model = SelectorResolver.Resolve(exp.Selector, scopeParameters1, queryModel.ScopeTables);

            queryModel.ResultModel = model;

            GeneralQueryState queryState = new GeneralQueryState(queryModel);

            return(queryState);
        }