Ejemplo n.º 1
0
        public DateTimeFieldMapping(string fieldName, string indexName, DataMapping mapping, bool isNullable,
                                    string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(typeof(DateTime), fieldName, indexName, mapping, isNullable, dbType)
        {
            if (isIdentity)
            {
                throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType,
                                                           fieldName, ObjectType));
            }

            IsPrimaryKey = isPrimaryKey;

            var nullType = Type.GetType("System.Nullable`1", true);

            NullableType = nullType.MakeGenericType(ObjectType);
            _minValue    = default(DateTime);

            if (defaultValue != null)
            {
                var defaultValueType = defaultValue.GetType();
                if (defaultValueType == typeof(DefaultTime))
                {
                    var defaultTime = (DefaultTime)defaultValue;
                    _defaultTimeFunction = DefaultTimeFunction.GetFunction(defaultTime);
                    if (defaultTime == DefaultTime.TimeStamp || defaultTime == DefaultTime.UtcTimeStamp)
                    {
                        _isTimeStamp = true;
                    }

                    _defaultValue = _defaultTimeFunction;
                }
                else if (defaultValueType == typeof(DateTime))
                {
                    _defaultValue = defaultValue;
                }
                else if (defaultValueType == typeof(string))
                {
                    var str = defaultValue as string;
                    if (DateTime.TryParse(str, out var dt))
                    {
                        _defaultValue = dt;
                    }
                    else
                    {
                        throw new LightDataException(string.Format(SR.DefaultValueError, ObjectType, fieldName,
                                                                   ObjectType));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public PrimitiveFieldMapping(Type type, string fieldName, string indexName, DataMapping mapping, bool isNullable, string dbType, object defaultValue, bool isIdentity, bool isPrimaryKey)
            : base(type, fieldName, indexName, mapping, isNullable, dbType)
        {
            if (type != typeof(string))
            {
                Type itemstype = Type.GetType("System.Nullable`1");
                _nullableType = itemstype.MakeGenericType(type);
            }
            else
            {
                _nullableType = type;
            }
            if (defaultValue != null)
            {
                Type defaultValueType = defaultValue.GetType();
                if (_typeCode == TypeCode.DateTime)
                {
                    if (defaultValueType == typeof(DefaultTime))
                    {
                        DefaultTime defaultTime = (DefaultTime)defaultValue;
                        this._defaultTimeFunction = DefaultTimeFunction.GetFunction(defaultTime);
                        if (defaultTime == DefaultTime.TimeStamp || defaultTime == DefaultTime.UtcTimeStamp)
                        {
                            isTimeStamp = true;
                        }
                        this._defaultValue = this._defaultTimeFunction;
                    }
                    else if (defaultValueType == typeof(DateTime))
                    {
                        this._defaultValue = defaultValue;
                    }
                    else if (defaultValueType == typeof(string))
                    {
                        string str = defaultValue as string;
                        if (DateTime.TryParse(str, out DateTime dt))
                        {
                            this._defaultValue = dt;
                        }
                    }
                }
                else if (defaultValueType == type)
                {
                    this._defaultValue = defaultValue;
                }
                else
                {
                    this._defaultValue = Convert.ChangeType(defaultValue, type);
                }
            }
            if (isIdentity)
            {
                if (_typeCode == TypeCode.Int32 || _typeCode == TypeCode.Int64 || _typeCode == TypeCode.UInt32 || _typeCode == TypeCode.UInt64)
                {
                    _isIdentity = true;
                }
                else
                {
                    throw new LightDataException(string.Format(SR.DataMappingUnsupportIdentityFieldType, ObjectType, fieldName, type));
                }
            }

            _isPrimaryKey = isPrimaryKey;
            switch (_typeCode)
            {
            case TypeCode.String:
                _minValue = string.Empty;
                break;

            case TypeCode.Boolean:
                _minValue = false;
                break;

            case TypeCode.Byte:
                _minValue = byte.MinValue;
                break;

            case TypeCode.SByte:
                _minValue = MinSByte;
                break;

            case TypeCode.DateTime:
                _minValue = DateTime.MinValue;
                break;

            case TypeCode.Char:
                _minValue = Char.MinValue;
                break;

            case TypeCode.Int16:
                _minValue = MinInt16;
                break;

            case TypeCode.Int32:
                _minValue = MinInt32;
                break;

            case TypeCode.Int64:
                _minValue = MinInt64;
                break;

            case TypeCode.UInt16:
                _minValue = UInt16.MinValue;
                break;

            case TypeCode.UInt32:
                _minValue = UInt32.MinValue;
                break;

            case TypeCode.UInt64:
                _minValue = UInt64.MinValue;
                break;

            case TypeCode.Decimal:
                _minValue = MinDecimal;
                break;

            case TypeCode.Single:
                _minValue = MinSingle;
                break;

            case TypeCode.Double:
                _minValue = MinDouble;
                break;
            }
        }