Пример #1
0
 private void execute(StatementCollection coll, Context context)
 {
     foreach (Statement statement in coll)
         execute(statement, context);
 }
Пример #2
0
        private void execute(Statement statement, Context context)
        {
            if (statement is SelectStatement)
            {
                // Join'den tabloları al
                // her bir kayıt için hashtable oluştur, context'e ekle, where expressionı üzerinde excute ettir. true ise listeye ekle.
                // falan filan uzun iş bu. niye yapıyorum ki ben bunu?

                SelectStatement ss = statement as SelectStatement;
                Expression filter = null;
                if (ss.From[0].On == null && ss.Where == null) filter = null;
                else if (ss.From[0].On != null && ss.Where != null) filter = new AndExpression(ss.From[0].On, ss.Where);
                else if (ss.From[0].On != null) filter = ss.From[0].On;
                else filter = ss.Where;

                this.FieldNames = ss.Select;
                this.ResultSet = context.GetData(ss.From[0], filter, ss.Select);
                this.FieldTypes = new List<Type>();
                if (this.ResultSet.Count > 0)
                {
                    foreach (Select key in ss.Select)
                        if (this.ResultSet[0][key.Alias] != null)
                            this.FieldTypes.Add(this.ResultSet[0][key.Alias].GetType());
                        else
                            this.FieldTypes.Add(typeof(string));
                }
                if (ss.OrderBy.Count > 0)
                {
                    IOrderedEnumerable<Hashtable> orderedList = this.ResultSet.OrderBy(ht=>1);
                    for (int i = 0; i < ss.OrderBy.Count; i++)
                    {
                        object orderBy = null;
                        if (ss.OrderBy[i].By is IntegerConstant)
                            orderBy = ((IntegerConstant)ss.OrderBy[i].By).Value;
                        else
                            orderBy = ((DbObjectName)ss.OrderBy[i].By).Name;

                        if (orderBy.GetType() == typeof(int))
                        {
                            int fieldNo = (int)orderBy - 1;
                            if (!(fieldNo < 0 || fieldNo >= FieldNames.Count))
                            {
                                if (ss.OrderBy[i].Desc)
                                    orderedList = orderedList.ThenByDescending(ht => ht[FieldNames[(int)fieldNo].Alias]);
                                else
                                    orderedList = orderedList.ThenBy(ht => ht[FieldNames[(int)fieldNo].Alias]);
                            }
                        }
                        else
                        {
                            string alias = orderBy.ToString();
                            if (FieldNames.IndexOf(alias) > -1)
                            {
                                if (ss.OrderBy[i].Desc)
                                    orderedList = orderedList.ThenByDescending(ht => ht[alias]);
                                else
                                    orderedList = orderedList.ThenBy(ht => ht[alias]);
                            }
                        }
                    }
                    this.ResultSet = orderedList.ToList();
                }

                if (ss.Limit != null)
                {
                    int offset = ss.Offset == null ? 0 : Convert.ToInt32(ss.Offset.Calculate(context));
                    int limit = Convert.ToInt32(ss.Limit.Calculate(context));
                    this.ResultSet = this.ResultSet.Skip(offset).Take(limit).ToList();
                }
            }
        }
Пример #3
0
        public void Execute(TextWriter output)
        {
            context = new Context();
            context.Output = output;
            context.Variables = attributes;
            context.Variables["true"] = true;
            context.Variables["false"] = false;
            context.Variables["null"] = null;

            Stopwatch watch = new Stopwatch();
            watch.Start();
            try
            {
                StatementCollection coll = new StatementCollection(statements);
                execute(coll, context);
            }
            catch (Exception ex)
            {
                context.Output.Write(ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : ""));
            }
            watch.Stop();
            this.ExecutingTime = watch.ElapsedMilliseconds;
        }