示例#1
0
        /// <summary>
        /// 给select count()语句构建带where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName"></param>
        /// <param name="filterProterties"></param>
        /// <returns></returns>
        private string GetCountSelectStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList, string[] filterProterties)
        {
            fieldPropertyList = new List <PropertyInfo>();
            string fields = "", query = "";

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                EntityMappingFieldAttribute fieldAttr = mfi.MappingFieldAttribute;
                if (fields != "")
                {
                    fields += ",";
                }
                fields += GetQuotedName(fieldAttr.FieldName);
                //当不选择过滤条件则判定是否是主键、当选择过滤字段则判断当前属性是否包含在过滤字段内
                if ((filterProterties == null && fieldAttr.IsKey == true) || (filterProterties != null && filterProterties.Contains(property.Name)))
                {
                    if (query != "")
                    {
                        query += " AND ";
                    }
                    query += GetQuotedName(fieldAttr.FieldName) + "=@" + property.Name;
                    fieldPropertyList.Add(property);
                }
            }
            return(string.Format(" COUNT(*) FROM {0} WHERE {1}", this.GetQuotedName(entityInfo.MappingTableAttribute.TableName), query));
        }
示例#2
0
        private string GetInsertStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList)
        {
            string fields = "", values = "";

            fieldPropertyList = new List <PropertyInfo>();
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (mfi.MappingFieldAttribute.IsAutoField)
                {
                    continue;
                }

                if (fields != "")
                {
                    fields += ",";
                }
                if (values != "")
                {
                    values += ",";
                }
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
                values += "@" + mfi.ProInfo.Name;

                fieldPropertyList.Add(mfi.ProInfo);
            }
            return(string.Format("({0}) VALUES ({1})", fields, values));
        }
示例#3
0
        public static string GetEntityInfoMessage(object entity)
        {
            TypeSchema    entityInfo  = ORMSchemaCache.GetTypeSchema(entity.GetType());
            StringBuilder infoBuilder = new StringBuilder();

            infoBuilder.AppendLine("Entity Type: " + entity.GetType().FullName);
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                infoBuilder.AppendLine("[" + property.Name + "]: " + property.GetValue(entity, null));
            }
            return(infoBuilder.ToString());
        }
示例#4
0
        /// <summary>
        /// 构建无where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo)
        {
            string fields = "";

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (fields != "")
                {
                    fields += ",";
                }
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
            }

            return(string.Format("{0} FROM {1}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName)));
        }
        private string GetInsertStatement(TypeSchema entityInfo, out List<PropertyInfo> fieldPropertyList)
        {
            string fields = "", values = "";
            fieldPropertyList = new List<PropertyInfo>();
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (mfi.MappingFieldAttribute.IsAutoField) continue;

                if (fields != "") fields += ",";
                if (values != "") values += ",";
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
                values += "@" + mfi.ProInfo.Name;

                fieldPropertyList.Add(mfi.ProInfo);
            }
            return string.Format("({0}) VALUES ({1})", fields, values);
        }
示例#6
0
        public static T ConvertDataRowToEntity <T>(DataRow dataRow) where T : new()
        {
            PreconditionAssert.IsNotNull(dataRow, ErrorMessages.NullReferenceException);

            T          tempT      = new T();
            Type       entityType = typeof(T);
            string     fieldName;
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                fieldName = mfi.MappingFieldAttribute.FieldName;
                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }

                if (dataRow.Table.Columns.Contains(fieldName))
                {
                    object value = null;
                    if (!dataRow[fieldName].Equals(DBNull.Value))
                    {
                        Type type = Nullable.GetUnderlyingType(mfi.ProInfo.PropertyType) ?? mfi.ProInfo.PropertyType;
                        if (type.IsEnum)
                        {
                            value = Enum.Parse(type, dataRow[fieldName].ToString());
                        }
                        else
                        {
                            value = Convert.ChangeType(dataRow[fieldName], type);
                        }
                    }
                    mfi.ProInfo.SetValue(tempT, value, null);
                }
            }
            return(tempT);
        }
示例#7
0
        internal static List <SchemaItem> GetSchemaItems(Type objectType)
        {
            TypeSchema entityInfo = GetTypeSchema(objectType);

            return(entityInfo.GetAllFieldInfos());
        }
        /// <summary>
        /// 构建无where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo)
        {
            string fields = "";
            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                if (fields != "") fields += ",";
                fields += GetQuotedName(mfi.MappingFieldAttribute.FieldName);
            }

            return string.Format("{0} FROM {1}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName));
        }
        /// <summary>
        /// 构建带where条件查询的sql语句
        /// </summary>
        /// <param name="fieldPropertyList"></param>
        /// <param name="tableName">表名</param>
        /// <param name="filterProterties">要查询的字段</param>
        /// <returns>sql语句</returns>
        private string GetSelectStatement(TypeSchema entityInfo, out List<PropertyInfo> fieldPropertyList, string[] filterProterties)
        {
            fieldPropertyList = new List<PropertyInfo>();
            string fields = "", query = "";

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                EntityMappingFieldAttribute fieldAttr = mfi.MappingFieldAttribute;
                if (fields != "") fields += ",";
                fields += GetQuotedName(fieldAttr.FieldName);
                //当不选择过滤条件则判定是否是主键、当选择过滤字段则判断当前属性是否包含在过滤字段内
                if ((filterProterties == null && fieldAttr.IsKey == true) || (filterProterties != null && filterProterties.Contains(property.Name)))
                {
                    if (query != "") query += " AND ";
                    query += GetQuotedName(fieldAttr.FieldName) + "=@" + property.Name;
                    fieldPropertyList.Add(property);
                }
            }

            return string.Format("{0} FROM {1} WHERE {2}", fields, this.GetQuotedName(entityInfo.MappingTableAttribute.TableName), query);
        }