コード例 #1
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
        /// <summary>
        /// Converts a database value to a model value.
        /// </summary>
        /// <param name="property">Property to populate from the database.</param>
        /// <param name="fieldMetadata">Additional information about the field.</param>
        /// <param name="modelValue">Value from the model.</param>
        /// <returns>Value to store in the database.</returns>
        protected virtual object CoerceValueToDatabase(ModelProperty property, FieldMetadata fieldMetadata, object modelValue)
        {
            var converter = GetConverter(property);

            if (converter != null)
            {
                converter.Convert(ref modelValue);
            }

            return(modelValue);
        }
コード例 #2
0
        /// <summary>
        /// Registers metadata for a <see cref="ModelProperty"/>.
        /// </summary>
        /// <param name="property">Property to register metadata for.</param>
        /// <param name="metadata">Metadata for the field.</param>
        /// <param name="converter">Converter to use when transfering data from the source field to the model property.</param>
        protected virtual void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, IConverter converter)
        {
            _fieldMetadata = _fieldMetadata.AddOrUpdate(property.Key, metadata);

            if (converter != null)
            {
                _fieldConverters = _fieldConverters.AddOrUpdate(property.Key, converter);
            }

            if ((metadata.Attributes & InternalFieldAttributes.PrimaryKey) != 0 && PrimaryKeyProperty == null)
            {
                PrimaryKeyProperty = property;
            }
        }
コード例 #3
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
        private static object GetQueryValue(IDatabaseQuery query, int index, FieldMetadata fieldMetadata)
        {
            if (query.IsColumnNull(index))
            {
                return(null);
            }

            if (fieldMetadata is ByteFieldMetadata)
            {
                return(query.GetByte(index));
            }

            if (fieldMetadata is IntegerFieldMetadata)
            {
                return(query.GetInt32(index));
            }

            if (fieldMetadata is StringFieldMetadata)
            {
                return(query.GetString(index));
            }

            if (fieldMetadata is DoubleFieldMetadata)
            {
                return(Convert.ToDouble(query.GetFloat(index)));
            }

            if (fieldMetadata is FloatFieldMetadata)
            {
                return(query.GetFloat(index));
            }

            if (fieldMetadata is DateTimeFieldMetadata)
            {
                return(query.GetDateTime(index));
            }

            if (fieldMetadata is BooleanFieldMetadata)
            {
                return(query.GetBool(index));
            }

            throw new NotSupportedException(fieldMetadata.GetType().Name);
        }
コード例 #4
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
        /// <summary>
        /// Converts a database value to a model value.
        /// </summary>
        /// <param name="property">Property to populate from the database.</param>
        /// <param name="fieldMetadata">Additional information about the field.</param>
        /// <param name="databaseValue">Value read from the database.</param>
        /// <returns>Value to store in the model.</returns>
        protected virtual object CoerceValueFromDatabase(ModelProperty property, FieldMetadata fieldMetadata, object databaseValue)
        {
            var converter = GetConverter(property);

            if (converter != null)
            {
                converter.ConvertBack(ref databaseValue);
            }

            if (property.PropertyType.IsEnum && databaseValue is int)
            {
                return(Enum.ToObject(property.PropertyType, (int)databaseValue));
            }

            if (property.PropertyType == typeof(Date) && databaseValue is DateTime)
            {
                return(Date.FromDateTime((DateTime)databaseValue));
            }

            return(databaseValue);
        }
コード例 #5
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
 /// <summary>
 /// Registers metadata for a <see cref="ModelProperty"/>.
 /// </summary>
 /// <param name="property">Property to register metadata for.</param>
 /// <param name="metadata">Metadata for the field on the related object.</param>
 /// <param name="viaForeignKey">Metadata for the ForeignKey column on the primary object that maps to the related object.</param>
 protected void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, ForeignKeyFieldMetadata viaForeignKey)
 {
     RegisterFieldMetadata(property, metadata, viaForeignKey, null);
 }
コード例 #6
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
 /// <summary>
 /// Registers metadata for a <see cref="ModelProperty" />.
 /// </summary>
 /// <param name="property">Property to register metadata for.</param>
 /// <param name="metadata">Metadata for the field.</param>
 /// <param name="converter">Converter to use when transfering data from the source field to the model property.</param>
 protected override sealed void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, IConverter converter)
 {
     RegisterFieldMetadata(property, metadata, null, converter);
 }
コード例 #7
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
 /// <summary>
 /// Registers metadata for a <see cref="ModelProperty"/>.
 /// </summary>
 /// <param name="property">Property to register metadata for.</param>
 /// <param name="metadata">Metadata for the field.</param>
 protected void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata)
 {
     RegisterFieldMetadata(property, metadata, null, null);
 }
コード例 #8
0
ファイル: DatabaseModelMetadata.cs プロジェクト: Jamiras/Core
 private static void AppendQueryValue(StringBuilder builder, object value, FieldMetadata fieldMetadata, IDatabase database)
 {
     if (value == null)
     {
         AppendQueryNull(builder);
     }
     else if (value is int || value.GetType().IsEnum)
     {
         var iVal = (int)value;
         if (iVal == 0 && fieldMetadata is ForeignKeyFieldMetadata)
         {
             AppendQueryNull(builder);
         }
         else
         {
             builder.Append(iVal);
         }
     }
     else if (value is string)
     {
         var sVal = (string)value;
         if (sVal.Length == 0)
         {
             AppendQueryNull(builder);
         }
         else
         {
             builder.AppendFormat("'{0}'", database.Escape(sVal));
         }
     }
     else if (value is double)
     {
         var dVal = (double)value;
         builder.Append(dVal);
     }
     else if (value is float)
     {
         var dVal = (float)value;
         builder.Append(dVal);
     }
     else if (value is DateTime)
     {
         var dttm = (DateTime)value;
         builder.AppendFormat("#{0}#", dttm);
     }
     else if (value is Date)
     {
         var date = (Date)value;
         builder.AppendFormat("#{0}/{1}/{2}#", date.Month, date.Day, date.Year);
     }
     else if (value is bool)
     {
         if ((bool)value)
         {
             builder.Append("YES");
         }
         else
         {
             builder.Append("NO");
         }
     }
     else
     {
         throw new NotSupportedException(fieldMetadata.GetType().Name);
     }
 }
コード例 #9
0
 /// <summary>
 /// Constructs a new foreign key field metadata.
 /// </summary>
 /// <param name="fieldName">The foreign key field.</param>
 /// <param name="relatedField">The metadata describing the related field.</param>
 /// <param name="attributes">Additional attributes of the field.</param>
 public ForeignKeyFieldMetadata(string fieldName, FieldMetadata relatedField, FieldAttributes attributes = FieldAttributes.None)
     : base(fieldName, (relatedField is IntegerFieldMetadata) ? ((IntegerFieldMetadata)relatedField).MinimumValue : 0,
            (relatedField is IntegerFieldMetadata) ? ((IntegerFieldMetadata)relatedField).MaximumValue : Int32.MaxValue, attributes)
 {
     RelatedField = relatedField;
 }
コード例 #10
0
 protected override sealed void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, IConverter converter)
 {
     throw new NotSupportedException();
 }