public static string GetORMMapinngField(this ORMappingItemCollection coll, string property)
        {
            var attr = coll.Find(m => m.PropertyName == property);

            if (attr == null)
            {
                return(string.Empty);
            }
            return(attr.DataFieldName);
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="coll"></param>
        /// <param name="property"></param>
        /// <param name="alsName">别名</param>
        /// <returns></returns>
        public static string GetDataField(this ORMappingItemCollection coll, string property, string alsName = null)
        {
            var attr = coll.Find(m => m.PropertyName == property);

            if (attr == null)
            {
                return(string.Empty);
            }
            if (string.IsNullOrWhiteSpace(alsName))
            {
                alsName = coll.TableName;
            }
            return(string.Format("{0}.{1}", alsName, attr.DataFieldName));
        }
Exemple #3
0
        private static string GetFieldName(ORMappingItemCollection mapping, SchemaPropertyDefine pd)
        {
            string result = pd.SnapshotFieldName;

            if (result.IsNullOrEmpty())
            {
                result = pd.Name;

                ORMappingItem item = mapping.Find(m => m.PropertyName == pd.Name);

                if (item != null)
                {
                    result = item.DataFieldName;
                }
            }

            return(result);
        }
        /// <summary>
        /// 生成子表与父表联接查询语句
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="whereAction">Where条件</param>
        /// <param name="orderByAction">OrderBy条件</param>
        /// <returns>SQL语句</returns>
        public static string GetSubClassSelectSql(Type type, Action <WhereSqlClauseBuilder> whereAction = null, Action <OrderBySqlClauseBuilder> orderByAction = null)
        {
            StringBuilder sqlStr = new StringBuilder();

            //生成连接查询语句
            ORMappingItemCollection mappings    = ORMapping.GetMappingInfo(type);
            ORMappingItem           primaryItem = mappings.Find(p => p.PrimaryKey);

            sqlStr.Append(String.Format(" SELECT * FROM {0} ", mappings.TableName));

            //遍历父类,找出父表,生成SELECT语句
            Type baseType = type.BaseType;

            while (baseType != typeof(object))
            {
                //如果不存在TableMapping特性则跳过
                if (baseType != null)
                {
                    var tableMapping = (ORTableMappingAttribute)Attribute.GetCustomAttribute(baseType, typeof(ORTableMappingAttribute), true);
                    if (tableMapping == null)
                    {
                        baseType = baseType.BaseType;
                        continue;
                    }

                    //创建内联接查询语句
                    sqlStr.Append(String.Format(" INNER JOIN {0} ON {1}.{2} = {0}.{2} ", tableMapping.TableName, mappings.TableName, primaryItem.DataFieldName));
                }
                if (baseType != null)
                {
                    baseType = baseType.BaseType;
                }
            }

            //Where子句处理
            if (whereAction != null)
            {
                //生成Where子句
                WhereSqlClauseBuilder builder = new WhereSqlClauseBuilder();
                whereAction(builder);
                //将Where子句添加到SQL字串
                if (builder.Count > 0)
                {
                    sqlStr.Append(String.Format(" WHERE {0} ", builder.ToSqlString(TSqlBuilder.Instance)));
                }
            }

            //OrderBy子句处理
            if (orderByAction != null)
            {
                //生成OrderBy子句
                OrderBySqlClauseBuilder orderByBuilder = new OrderBySqlClauseBuilder();
                orderByAction(orderByBuilder);
                //将OrderBy子句添加到SQL字串
                if (orderByBuilder.Count > 0)
                {
                    sqlStr.Append(String.Format(" ORDER BY {0} ", orderByBuilder.ToSqlString(TSqlBuilder.Instance)));
                }
            }

            return(sqlStr.ToString());
        }