Example #1
0
        /// <summary>
        /// Translates a native value to a MySql value.
        /// </summary>
        public static object TranslateToDb(DbDataType dataType, Type type, object value)
        {
            switch (dataType)
            {
            case DbDataType.Unknown:
                return(DBNull.Value);

            case DbDataType.Boolean:
            case DbDataType.Char:
            case DbDataType.UInt8:
            case DbDataType.SInt8:
            case DbDataType.UInt16:
            case DbDataType.SInt16:
            case DbDataType.UInt24:
            case DbDataType.SInt24:
            case DbDataType.UInt32:
            case DbDataType.SInt32:
            case DbDataType.UInt64:
            case DbDataType.SInt64:
            case DbDataType.Decimal:
            case DbDataType.Float:
            case DbDataType.Double:
            case DbDataType.Text:
            case DbDataType.Blob:
            case DbDataType.Enumerated:
                return(DbTranslation.ScalarToDb(type, value));

            case DbDataType.DateTime:
                return(value);

            case DbDataType.Timestamp:
                return(((DateTimeOffset)value).DateTime);

            case DbDataType.TimeSpan:
                return(((TimeSpan)value).Ticks);

            case DbDataType.Records:
            case DbDataType.Json:
                return(JsonConvert.SerializeObject(value, Formatting.None));

            case DbDataType.Time:
            case DbDataType.Date:
            default:
                throw new NotImplementedException();
            }
        }
Example #2
0
        /// <summary>
        /// Translates a MySql value to a native value.
        /// </summary>
        public static object TranslateFromDb(DbDataType dataType, Type type, object value)
        {
            switch (dataType)
            {
            case DbDataType.Unknown:
            case DbDataType.Boolean:
            case DbDataType.Char:
            case DbDataType.UInt8:
            case DbDataType.SInt8:
            case DbDataType.UInt16:
            case DbDataType.SInt16:
            case DbDataType.UInt24:
            case DbDataType.SInt24:
            case DbDataType.UInt32:
            case DbDataType.SInt32:
            case DbDataType.UInt64:
            case DbDataType.SInt64:
            case DbDataType.Decimal:
            case DbDataType.Float:
            case DbDataType.Double:
            case DbDataType.Text:
            case DbDataType.Blob:
            case DbDataType.Enumerated:
                return(DbTranslation.ScalarFromDb(type, value));

            case DbDataType.DateTime:
                return(value);

            case DbDataType.Timestamp:
                return(new DateTimeOffset((DateTime)value));

            case DbDataType.TimeSpan:
                return(new TimeSpan((long)value));

            case DbDataType.Records:
            case DbDataType.Json:
                return(JsonConvert.DeserializeObject((string)value, type));

            case DbDataType.Time:
            case DbDataType.Date:
            default:
                throw new NotImplementedException();
            }
        }