예제 #1
0
        /// <summary>
        /// 解析SQL语句并生成对应的SQL构造器SqlStringBuilder
        /// 前置条件:xnItem!=null,并且xnItem满足SQL语句格式
        /// </summary>
        /// <param name="xnItem"></param>
        private static void ParseMapItem(XmlNode xnItem)
        {
            string           key;
            string           sql;
            Type             paramClassType;
            Type             returnClassType;
            SqlStringBuilder ssb;

            // 检查mapitem是否包含TAG_KEY,TAG_SQL
            Check(xnItem, TAG_KEY);

            // 获取SQL语句key,并检查key是否有效
            key = "$" + xnItem.Attributes[TAG_KEY].Value;
            if (_Ssbs.ContainsKey(key))
            {
                throw new Exception(string.Format("SQL关键字重复异常.{0}", key));
            }

            // 获取主体SQL语句
            if (xnItem.Attributes[TAG_SQL] != null)
            {
                sql = xnItem.Attributes[TAG_SQL].Value;
            }
            else
            {
                // SQL语句
                sql = StringUtil.Suppress(xnItem.FirstChild.Value);
            }

            // 获取入口参数类
            if (xnItem.Attributes[TAG_PARAM_CLASS] == null)
            {
                paramClassType = null;
            }
            else
            {
                paramClassType = ReflectUtil.GetType(xnItem.Attributes[TAG_PARAM_CLASS].Value);
            }

            // 获取返回参数类
            if (xnItem.Attributes[TAG_RETURN_CLASS] == null)
            {
                returnClassType = null;
            }
            else
            {
                returnClassType = ReflectUtil.GetType(xnItem.Attributes[TAG_RETURN_CLASS].Value);
            }

            // 创建SQL构造器
            ssb = new SqlStringBuilder(sql, paramClassType, returnClassType);
            //bool bHasCondition = false;
            foreach (XmlNode subXn in xnItem.ChildNodes)
            {
                if (subXn.Name == TAG_CONDITON_SQL)
                {
                    ssb.AddConditionSql(StringUtil.Suppress(subXn.InnerText));
                    //bHasCondition = true;
                }
            }
            //if (bHasCondition)
            //{
            //    string lstText = xnItem.LastChild.InnerText;
            //    if (lstText.Length > 0 && lstText.ToLower().IndexOf("order by") != -1)
            //    {
            //        ssb.AddOrderBySql(lstText);
            //    }
            //}

            // 将SQL构造器纳入缓存管理
            _Ssbs.Add(key, ssb);
        }