Ejemplo n.º 1
0
        ///获取xml中配置的 User.Select语句, 或者根据对象的属性映射生成语句
        private string GetMappedOrDefaultSql(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("MappingType", "QueryInfo未指定查询的对象类型!");
            }

            string key = type.Name + ".Select";
            string sql = StatementParser.GetMappedStaticSql(key);

            if (sql.Equals(key))
            {
                sql = (MappingInfo.GetMappingInfo(type).Select + "t");
            }
            return(sql);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据T创建select,结合param生成where语句
        /// </summary>
        string AddParamToSqlWhere(Type type, string mapSql, ref object param)
        {
            if (string.IsNullOrEmpty(mapSql))//类型获取
            {
                return(GetMappedOrDefaultSql(type));
            }

            if (param != null)
            {
                QueryInfo info = new QueryInfo();//添加当前参数,通过参数名可以控制语句类型:EQ,LK,GT等

                ////参数类型Anonymous对象? 转换为IDictionary
                //if (!typeof(System.Collections.IDictionary).IsAssignableFrom(param.GetType()))
                //{
                //    var props = Dappers.Context.Reflection.GetHolder(param.GetType());
                //    Dictionary<string, Dappers.Context.GetHandler> getters = props.Getters;

                //    Dictionary<string, object> di = new Dictionary<string, object>(getters.Count);
                //    foreach(var getter in getters){
                //        di.Add(getter.Key,
                //            getter.Value(param) );
                //    }
                //    param = di;
                //}

                info.Parameters = (IDictionary <string, object>)param;
                if (!string.IsNullOrEmpty(mapSql))//重新映射
                {
                    info.CustomSQL = mapSql;
                }
                StatementParser.ParseDynamicSql(info); //配置语句 动态构造
                param = info.Parameters;               //参数已被修改

                return(info.ToSQLString() + info.ToOrderBy());
            }
            else
            {
                string sql = StatementParser.GetMappedStaticSql(mapSql);
                if (sql.Equals(mapSql) && sql.IndexOf(" ") < 0)
                {
                    throw new ArgumentOutOfRangeException("无效的Sql配置项,请检查Key是否正确:" + mapSql);
                }
                return(sql);
            }
        }