Пример #1
0
        void QueryCondition_BeforeQuery(object sender, QueryCondition.BeforeQueryEventArgs e)
        {
            //leixu 2015年10月29日16:01:30
            if (HttpContext.Current == null)
            {
                return;
            }

            QueryCondition q = sender as QueryCondition;

            JContext jc = JContext.Current;

            Qc qc = null;

            string qId     = q.Id;
            string sitekey = jc.Area.AreaKey;

            if (qId == null)
            {
                qId = jc.Navigation.ToString();
            }
            else
            {
                if (qId.Contains(":"))
                {
                    string[] ar = StringUtil.Split(qId, ":", true, true);
                    sitekey = ar[0];
                    qId     = ar[1];
                }
            }

            if (string.IsNullOrEmpty(qId))
            {
                return;
            }

            qc = GetById(sitekey, string.Format("{0}.{1}.{2}", qId, e.Method, e.DbProviderName));

            if (qc == null)
            {
                qc = GetById(sitekey, string.Format("{0}.{1}", qId, e.Method));
            }

            if (qc == null)
            {
                qc = GetById(sitekey, string.Format("{0}.{1}", qId, e.DbProviderName));
            }

            if (qc == null)
            {
                qc = GetById(sitekey, qId);
            }

            if (qc == null)
            {
                _logger.Warn("query:{0} not found!", q.Id);
                return;
            }

            if (qc.PageSize > -1 && q.PageSize == -1)
            {
                q.PageSize = qc.PageSize;
            }

            q.Parameters.Clear();

            if ((string.IsNullOrEmpty(q.TableField) || q.TableField == "*" || q.EventFiredTimes > 1) && StringUtil.HasText(qc.Field))
            {
                if (qc.Field.Contains("$"))
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        Dictionary <string, object> di = new Dictionary <string, object>(jc.ViewData);
                        di["this"] = sender;

                        ServiceLocator.Instance.Resolve <ITemplateEngine>().Process(di,
                                                                                    string.Empty,
                                                                                    writer,
                                                                                    qc.Field);

                        q.TableField = writer.GetStringBuilder().ToString();
                    }
                }
                else
                {
                    q.TableField = qc.Field;
                }
            }

            // 解析field里的@参数
            Match m = Regex.Match(q.TableField, @"@\w+");

            while (m.Success)
            {
                string param_name = m.Value.Substring(1).Trim();

                if (q[param_name] != null)
                {
                    q.Parameters[param_name] = q[param_name];
                }
                m = m.NextMatch();
            }

            if (StringUtil.HasText(qc.AllowedOrderbyColumns))
            {
                q.AllowedOrderbyColumns.AddRange(StringUtil.CommaDelimitedListToStringArray(qc.AllowedOrderbyColumns));
            }

            if (StringUtil.HasText(qc.Orderby))
            {
                List <string> ls = new List <string>(StringUtil.CommaDelimitedListToStringArray(qc.Orderby));

                if (q.IsAddOrderBy2First)
                {
                    ls.Reverse();

                    foreach (string oderby in ls)
                    {
                        q.InsertOrderby(0, oderby.TrimStart('-'), !oderby.StartsWith("-"));
                    }
                }
                else
                {
                    foreach (string oderby in ls)
                    {
                        q.AddOrderby(oderby.TrimStart('-'), !oderby.StartsWith("-"));
                    }
                }
            }

            foreach (string key in qc.Keys)
            {
                q[key] = qc[key];
            }

            using (StringWriter writer = new StringWriter())
            {
                Dictionary <string, object> di = new Dictionary <string, object>(jc.ViewData);
                di["this"] = sender;

                ServiceLocator.Instance.Resolve <ITemplateEngine>().Process(di,
                                                                            string.Empty,
                                                                            writer,
                                                                            qc.Where);

                string sql = Regex.Replace(writer.GetStringBuilder().ToString(), @"\s{1,}|\t|\r|\n", " ");

                // 解析where里的@参数
                m = Regex.Match(sql, @"@\w+");
                while (m.Success)
                {
                    string param_name = m.Value.Substring(1).Trim();

                    if (q[param_name] != null)
                    {
                        q.Parameters[param_name] = q[param_name];
                    }

                    m = m.NextMatch();
                }

                if (StringUtil.HasText(sql))
                {
                    q.WhereClause = sql;
                }
            }
        }