Example #1
0
 private void ParseSubDataField()
 {
     PropertyInfo[] propertys = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
     foreach (PropertyInfo pi in propertys)
     {
         IDataFieldConfig config = ConfigManager.LoadDataFieldConfig(pi);
         if (config != null)
         {
             Type                  type                  = pi.PropertyType;
             string                name                  = string.IsNullOrEmpty(config.Name) ? pi.Name : config.Name;
             string                filedName             = string.Format("{0}_{1}", Name, name);
             string                indexName             = string.Format("{0}_{1}", IndexName, pi.Name);
             DataFieldMapping      mapping               = DataFieldMapping.CreateDataFieldMapping(type, pi, filedName, indexName, config, EntityMapping, ObjectType);
             PrimitiveFieldMapping primitiveFieldMapping = mapping as PrimitiveFieldMapping;
             if (primitiveFieldMapping != null)
             {
                 primitiveFieldMapping.IsIdentity   = false;
                 primitiveFieldMapping.IsPrimaryKey = false;
             }
             mapping.Handler = new PropertyHandler(pi);
             _fieldMappingDictionary.Add(mapping.IndexName, mapping);
             if (mapping.Name != mapping.IndexName)
             {
                 _fieldMappingAlterNameDictionary.Add(mapping.Name, mapping);
             }
         }
     }
     if (_fieldMappingDictionary.Count == 0)
     {
         throw new LightDataException(RE.ComplexFieldHaveNotSubFields);
     }
 }
        public IEnumerable <RelationKeyValue> GetRelationKeyValues()
        {
            if (_relationKeyValues == null)
            {
                lock (_synobj2) {
                    if (_relationKeyValues == null)
                    {
                        List <RelationKeyValue> tables = new List <RelationKeyValue> ();
                        foreach (KeyValuePair <string, string> keys in _relationKeys)
                        {
                            PrimitiveFieldMapping masterField = MasterTableMapping.FindFieldMapping(keys.Key) as PrimitiveFieldMapping;
                            if (masterField == null)
                            {
                                throw new LightDataException(string.Format(RE.RelationKeyIsNotPrimitiveField, keys.Key));
                            }
                            PrimitiveFieldMapping relateField = RelateTableMapping.FindFieldMapping(keys.Value) as PrimitiveFieldMapping;
                            if (relateField == null)
                            {
                                throw new LightDataException(string.Format(RE.RelationKeyIsNotPrimitiveField, keys.Value));
                            }
                            tables.Add(new RelationKeyValue(masterField, relateField));
                        }
                        _relationKeyValues = tables.ToArray();
                    }
                }
            }

            foreach (RelationKeyValue kv in _relationKeyValues)
            {
                yield return(kv);
            }
        }
        void GetPrimaryKey()
        {
            List <PrimitiveFieldMapping> primaryKeys = new List <PrimitiveFieldMapping> ();

            foreach (DataFieldMapping field in GetFieldMappings())
            {
                PrimitiveFieldMapping mapping = field as PrimitiveFieldMapping;
                if (mapping != null)
                {
                    if (mapping.IsIdentity)
                    {
                        if (IdentityField == null)
                        {
                            TypeCode code = Type.GetTypeCode(mapping.ObjectType);
                            if (code == TypeCode.Int32 || code == TypeCode.Int64 || code == TypeCode.UInt32 || code == TypeCode.UInt64)
                            {
                                IdentityField = mapping;
                            }
                            else
                            {
                                throw new LightDataException(RE.TheTypeOfIdentityFieldError);
                            }
                        }
                        else
                        {
                            throw new LightDataException(RE.DataTableNotAllowMoreIdentityField);
                        }
                    }
                    if (mapping.IsPrimaryKey)
                    {
                        primaryKeys.Add(mapping);
                    }
                }
            }
            PrimaryKeyFields = primaryKeys.ToArray();
        }
        public static DataFieldMapping CreateDataFieldMapping(Type type, PropertyInfo property, string fieldName, string indexName, IDataFieldConfig config, DataEntityMapping mapping, Type mainType)
        {
            if (!Regex.IsMatch(fieldName, _fieldRegex, RegexOptions.IgnoreCase))
            {
                throw new LightDataException(RE.FieldNameIsInvalid);
            }

            DataFieldMapping fieldMapping = null;

            bool   isNullable = false;
            string dbType     = config.DBType;

            if (type.IsGenericType)
            {
                Type frameType = type.GetGenericTypeDefinition();
                if (frameType.FullName == "System.Nullable`1")
                {
                    Type[] arguments = type.GetGenericArguments();
                    type       = arguments [0];
                    isNullable = true;
                }
            }
            isNullable = isNullable || config.IsNullable;
            if (type.IsArray && type.FullName != "System.Byte[]")
            {
                throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight);
            }
            else if (type.IsGenericParameter | type.IsGenericTypeDefinition)
            {
                throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight);
            }
            else if (type.IsEnum)
            {
                EnumFieldMapping enumFieldMapping = new EnumFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType);
//				enumFieldMapping.IsNullable = config.IsNullable;
                if (config.DefaultValue != null)
                {
                    if (config.DefaultValue is String)
                    {
                        enumFieldMapping.DefaultValue = Enum.Parse(type, config.DefaultValue as String, true);
                    }
                    else
                    {
                        Array arr = Enum.GetValues(type);
                        foreach (object obj in arr)
                        {
                            if (obj.Equals(config.DefaultValue))
                            {
                                enumFieldMapping.DefaultValue = config.DefaultValue;
                                break;
                            }
                        }
                    }
                }
                fieldMapping = enumFieldMapping;
            }
            else
            {
                TypeCode code = Type.GetTypeCode(type);
                if (code == TypeCode.DBNull)
                {
                    throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight);
                }
                if (code == TypeCode.Empty)
                {
                    throw new LightDataException(RE.TheTypeOfDataFieldIsNotRight);
                }
                else if (code == TypeCode.Object && type.FullName != "System.Byte[]")
                {
                    ComplexFieldMapping complexFieldMapping = new ComplexFieldMapping(type, fieldName, indexName, mapping, isNullable);
//					complexFieldMapping.IsNullable = config.IsNullable;
                    fieldMapping = complexFieldMapping;
                }
                else
                {
                    PrimitiveFieldMapping primitiveFieldMapping = new PrimitiveFieldMapping(type, fieldName, indexName, mapping, isNullable, dbType);
                    primitiveFieldMapping.IsIdentity   = config.IsIdentity;
                    primitiveFieldMapping.IsPrimaryKey = config.IsPrimaryKey;
//					primitiveFieldMapping.IsNullable = config.IsNullable;
//					primitiveFieldMapping.DBType = config.DBType;
                    if (config.DefaultValue != null)
                    {
                        if (config.DefaultValue.GetType() == type)
                        {
                            primitiveFieldMapping.DefaultValue = config.DefaultValue;
                        }
                        else
                        {
                            primitiveFieldMapping.DefaultValue = Convert.ChangeType(config.DefaultValue, type);
                        }
                    }
                    fieldMapping = primitiveFieldMapping;
                }
            }
//			if (isNullAbleType) {
//				fieldMapping.IsNullable = true;
//			}
            if (fieldMapping.IsNullable)
            {
                PropertyInfo specifiedProperty = GetSpecifiedProperty(property.Name + "Specified", mapping.ObjectType);
                if (specifiedProperty != null)
                {
                    fieldMapping._specifiedHandler = new PropertyHandler(specifiedProperty);
                }
            }
            if (config.DataOrder > 0)
            {
                fieldMapping.DataOrder = config.DataOrder - 1;
            }

            return(fieldMapping);
        }
 public RelationKeyValue(PrimitiveFieldMapping masterField, PrimitiveFieldMapping relateMapping)
 {
     this._masterField = masterField;
     this._relateField = relateMapping;
 }