コード例 #1
0
        private NPathSelectQuery ParseSelectQuery()
        {
            NPathSelectQuery query = new NPathSelectQuery();

            queries.Push(query);

            tokenizer.GetCurrentToken("select", "Select");

            NPathSelectClause selectClause = new NPathSelectClause();

            ParseSelectClause(selectClause);
            query.Select = selectClause;

            tokenizer.GetCurrentToken("from", "From");
            NPathFromClause fromClause = new NPathFromClause();

            ParseFromClause(fromClause);
            query.From = fromClause;

            if (tokenizer.GetCurrentToken().IsType("where"))
            {
                NPathWhereClause whereClause = new NPathWhereClause();
                query.Where = whereClause;
                ParseWhereClause(whereClause);
            }

            if (tokenizer.GetCurrentToken().IsType("order by"))             // do not localize
            {
                NPathOrderByClause orderByClause = new NPathOrderByClause();
                ParseOrderByClause(orderByClause);
                query.OrderBy = orderByClause;
            }

            return(query);
        }
コード例 #2
0
        protected virtual void ApplyTop(NPathSelectQuery query, ref ArrayList destList)
        {
            //apply "top"
            if (query.Select.HasTop)
            {
                int count = 0;
                if (query.Select.Percent)
                {
                    double percent = ((double)query.Select.Top) / 100;
                    count = (int)((double)destList.Count * percent);
                }
                else
                {
                    count = (int)query.Select.Top;
                }
                ArrayList tmp = new ArrayList(count);

                if (destList.Count < count)
                {
                    count = destList.Count;
                }

                tmp.AddRange(destList.GetRange(0, count));
                destList = tmp;
            }
        }
コード例 #3
0
        protected virtual ArrayList PopulateResult(IList sortedSourceList, NPathSelectQuery query)
        {
            ArrayList destList       = new ArrayList(sortedSourceList.Count);
            Hashtable distinctLookup = new Hashtable();

            foreach (object o in sortedSourceList)
            {
                if (IsMatch(o, query))
                {
                    if (query.Select.Distinct)
                    {
                        #region distinct add

                        //add if not present
                        if (!distinctLookup.ContainsKey(o))
                        {
                            //flag precense
                            distinctLookup.Add(o, o);
                            destList.Add(o);
                        }

                        #endregion
                    }
                    else
                    {
                        #region normal add

                        destList.Add(o);

                        #endregion
                    }
                }
            }
            return(destList);
        }
コード例 #4
0
        protected virtual object EvalQuery(object item, NPathSelectQuery query)
        {
            NPathClassName className    = (NPathClassName)query.From.Classes[0];
            string         propertyPath = className.Name;
            IList          childList    = (IList)EvalStringPropertyPath(item, propertyPath);
            ArrayList      result       = InternalGetTable(query, childList);

            object[] resultList = new object[result.Count];
            int      i          = 0;

            foreach (object[] values in result)
            {
                object scalarRes = values[0];
                if (IsNumber(scalarRes))
                {
                    scalarRes = double.Parse(scalarRes.ToString());
                }
                resultList[i] = scalarRes;
                i++;
            }

            if (query.IsAggregate)
            {
                return(resultList[0]);
            }
            else
            {
                return(resultList);
            }
        }
コード例 #5
0
        public virtual DataTable GetDataTable(NPathSelectQuery query, IList sourceList)
        {
            FixQuery(query);
            DataTable resultTable = new DataTable();

            resultTable.BeginInit();

            #region build columns

            int id = 0;
            foreach (NPathSelectField field in query.Select.SelectFields)
            {
                string          fieldName = field.Alias;
                NPathIdentifier path      = field.Expression as NPathIdentifier;
                if (path != null)
                {
                    if (path.IsWildcard)
                    {
                        throw new Exception("this can not happen");                         // do not localize
                    }
                    else
                    {
                        if (fieldName == null)
                        {
                            fieldName = path.Path;
                        }

                        resultTable.Columns.Add(path.Path, typeof(object));
                    }
                }
                else
                {
                    if (fieldName == null)
                    {
                        fieldName = "col" + id.ToString();
                        id++;
                    }
                    resultTable.Columns.Add(fieldName, typeof(object));
                }
            }

            #endregion

            resultTable.EndInit();

            resultTable.BeginLoadData();

            IList resultList = InternalGetTable(query, sourceList);

            foreach (object[] values in resultList)
            {
                resultTable.Rows.Add(values);
            }

            resultTable.EndLoadData();

            return(resultTable);
        }
コード例 #6
0
 public virtual string EmitQuery(NPathSelectQuery query)
 {
     code = new StringBuilder();
     EmitSelect(query);
     EmitFrom(query);
     EmitWhere(query);
     EmitOrderBy(query);
     return(code.ToString());
 }
コード例 #7
0
        public void ExpandWildcards(NPathSelectQuery query)
        {
            ArrayList newSelectFieldList = new ArrayList();

            foreach (NPathSelectField field in query.Select.SelectFields)
            {
                string          fieldName = field.Alias;
                NPathIdentifier path      = field.Expression as NPathIdentifier;
                if (path != null && path.IsWildcard)
                {
                    string[]       parts     = path.Path.Split('.');
                    NPathClassName className = (NPathClassName)query.From.Classes[0];

                    IClassMap classMap = Context.DomainMap.MustGetClassMap(className.Name);

                    int i = 0;
                    foreach (string part in parts)
                    {
                        if (i == parts.Length - 1)
                        {
                            break;
                        }

                        IPropertyMap property = classMap.MustGetPropertyMap(part);
                        classMap = Context.DomainMap.MustGetClassMap(property.DataType);
                        i++;
                    }

                    ArrayList properties = classMap.GetAllPropertyMaps();

                    foreach (PropertyMap property in properties)
                    {
                        if (property.ReferenceType != ReferenceType.None)
                        {
                            continue;
                        }

                        NPathSelectField newField = new NPathSelectField();
                        newField.Alias = null;
                        NPathIdentifier newPath = new NPathIdentifier();
                        if (parts.Length > 1)
                        {
                            newPath.Path = string.Join(".", parts, 0, parts.Length - 1) + ".";
                        }

                        newPath.Path       += property.Name;
                        newField.Expression = newPath;
                        newSelectFieldList.Add(newField);
                    }
                }
                else
                {
                    newSelectFieldList.Add(field);
                }
            }
            query.Select.SelectFields = newSelectFieldList;
        }
コード例 #8
0
        public virtual bool IsMatch(object item, NPathSelectQuery query)
        {
            if (query.Where == null)
            {
                return(true);
            }

            return(EvalExpression(item, query.Where.Expression));
        }
コード例 #9
0
        public virtual IClassMap GetRootClassMap(string npath, IDomainMap domainMap)
        {
            NPathParser      parser = new NPathParser();
            NPathSelectQuery query  = parser.ParseSelectQuery(npath);

            string className = ((NPathClassName)query.From.Classes[0]).Name;

            IClassMap rootClassMap = domainMap.MustGetClassMap(className);

            return(rootClassMap);
        }
コード例 #10
0
        protected virtual void EmitWhere(NPathSelectQuery query)
        {
            if (query.Where == null)
            {
                return;
            }

            Write("where ");             // do not localize
            EmitExpression(query.Where.Expression);
            WriteLine();
        }
コード例 #11
0
        private NPathSelectQuery Begin()
        {
            NPathSelectQuery query = ParseSelectQuery();


            if (tokenizer.GetCurrentToken().Text != "")
            {
                throw new UnexpectedTokenException(string.Format("Unexpected token '{0}' found at end of expression", tokenizer.GetCurrentToken().Text));                 // do not localize
            }

            queries.Pop();
            return(query);
        }
コード例 #12
0
        protected virtual IList SortSourceList(IList sourceList, NPathSelectQuery query)
        {
            if (query.OrderBy == null)
            {
                return(sourceList);
            }

            ArrayList sortedSourceList = new ArrayList(sourceList);


            sortedSourceList.Sort(new SortOrderComparer(query.OrderBy, this));
            return(sortedSourceList);
        }
コード例 #13
0
 protected virtual void EmitFrom(NPathSelectQuery query)
 {
     Write("from ");             // do not localize
     foreach (NPathClassName className in query.From.Classes)
     {
         Write(className.Name);
         if (className != query.From.Classes[query.From.Classes.Count - 1])
         {
             Write(",");
         }
     }
     WriteLine();
 }
コード例 #14
0
        public virtual IList GetObjects(NPathSelectQuery query, IList sourceList)
        {
            //sort the source list according to the orderby clause
            IList sortedSourceList = SortSourceList(sourceList, query);

            //fill result list with matches of the where clause
            ArrayList destList = PopulateResult(sortedSourceList, query);

            //apply top statement
            ApplyTop(query, ref destList);

            return(destList);
        }
コード例 #15
0
        protected virtual void EmitSelect(NPathSelectQuery query)
        {
            NPathSelectClause select = query.Select;

            Write("select ");             // do not localize

            if (query.Select.HasTop)
            {
                Write("top {0}", query.Select.Top);                 // do not localize
                if (query.Select.Percent)
                {
                    Write("% ");
                }
            }
            int i = 0;

            foreach (NPathSelectField field in select.SelectFields)
            {
                if (field.Expression is NPathIdentifier)
                {
                    NPathIdentifier path = field.Expression as NPathIdentifier;
                    Write(path.Path);
                }
                if (field.Expression is NPathFunction)
                {
                    NPathFunction function = field.Expression as NPathFunction;
                    EmitFunction(function);
                }
                if (field.Expression is NPathExpression)
                {
                    NPathExpression expression = field.Expression as NPathExpression;
                    EmitExpression(expression);
                }

                if (field.Alias != null && field.Alias != "")
                {
                    WriteLine(" as [{0}]", field.Alias);                     // do not localize
                }

                if (i < select.SelectFields.Count - 1)
                {
                    Write(",");
                }
                i++;
            }
            WriteLine();
        }
コード例 #16
0
        public virtual string ToScalarSql(string npath, Type type, ref IList outParameters, IList inParameters)
        {
            NPathParser      parser = new NPathParser();
            NPathSelectQuery query  = parser.ParseSelectQuery(npath, inParameters);

            IClassMap rootClassMap = this.Context.DomainMap.MustGetClassMap(type);

            this.ResultParameters = new ArrayList();

            SqlEmitter sqlEmitter = new SqlEmitter(this, query, NPathQueryType.SelectScalar, rootClassMap);

            string sql = sqlEmitter.EmitSql();

            outParameters = sqlEmitter.ResultParameters;

            return(sql);
        }
コード例 #17
0
        public virtual NPathQueryType GetNPathQueryType(string npath)
        {
            NPathQueryType npathQueryType = NPathQueryType.SelectObjects;

            try
            {
                NPathParser      parser = new NPathParser();
                NPathSelectQuery query  = parser.ParseSelectQuery(npath);

                npathQueryType = GetNPathQueryType(query);
            }
            catch
            {
                npathQueryType = NPathQueryType.SelectObjects;
            }

            return(npathQueryType);
        }
コード例 #18
0
        protected virtual void EmitOrderBy(NPathSelectQuery query)
        {
            if (query.OrderBy != null)
            {
                Write("order by ");                 // do not localize
                foreach (SortProperty property in query.OrderBy.SortProperties)
                {
                    EmitExpression(property.Expression);
                    Write(" ");
                    Write(property.Direction);

                    if (property != query.OrderBy.SortProperties[query.OrderBy.SortProperties.Count - 1])
                    {
                        Write(",");
                    }
                }
                WriteLine();
            }
        }
コード例 #19
0
        protected virtual ArrayList InternalGetTable(NPathSelectQuery query, IList sourceList)
        {
            ArrayList result = new ArrayList();

            IList resultList = GetObjects(query, sourceList);

            if (query.IsAggregate)
            {
                #region build rows

                object[] values = new object[query.Select.SelectFields.Count];
                for (int i = 0; i < query.Select.SelectFields.Count; i++)
                {
                    NPathSelectField selectField = (NPathSelectField)query.Select.SelectFields[i];
                    object           res         = EvalAggregate(resultList, selectField.Expression);
                    values[i] = res;
                }
                result.Add(values);

                #endregion
            }
            else
            {
                #region build rows

                foreach (object item in resultList)
                {
                    object[] values = new object[query.Select.SelectFields.Count];
                    for (int i = 0; i < query.Select.SelectFields.Count; i++)
                    {
                        NPathSelectField selectField = (NPathSelectField)query.Select.SelectFields[i];
                        object           res         = EvalValue(item, selectField.Expression);
                        values[i] = res;
                    }
                    result.Add(values);
                }

                #endregion
            }
            return(result);
        }
コード例 #20
0
 //transforms all * fields into real property path fields
 private void FixQuery(NPathSelectQuery query)
 {
     ObjectQueryEngineHelper.ExpandWildcards(query);
 }
コード例 #21
0
        public virtual DataTable GetDataTableByNPath(string npathQuery, IList sourceList, IList parameters)
        {
            NPathSelectQuery query = Parse(npathQuery, parameters);

            return(GetDataTable(query, sourceList));
        }
コード例 #22
0
        public virtual IList GetObjectsByNPath(string npathQuery, IList sourceList, IList parameters)
        {
            NPathSelectQuery query = Parse(npathQuery, parameters);

            return(GetObjects(query, sourceList));
        }
コード例 #23
0
 private NPathQueryType GetNPathQueryType(NPathSelectQuery query)
 {
     return(SqlEmitter.DeduceQueryType(query));
 }