Example #1
0
        public static void WriteValue(this StringBuilder sb, object value, AttributeCollection attributes)
        {
            string varName = ":v" + attributes.Count.ToString();

            var convertor = DbValueConverterFactory.Get(value.GetType());

            attributes[varName] = convertor.FromObject(value, null !);

            sb.Append(varName);
        }
        private void WriteValue(Constant constant)
        {
            var variableName = ":v" + AttributeValues.Count.ToString();

            var convertor = DbValueConverterFactory.Get(constant.Value.GetType());

            AttributeValues[variableName] = convertor.FromObject(constant.Value);

            sb.Append(variableName);
        }
Example #3
0
        public void SerializeBoolTest()
        {
            var converter = DbValueConverterFactory.Get(typeof(bool));

            var cv = converter.FromObject(true, null);

            Assert.Equal(DbValueType.BOOL, cv.Kind);
            Assert.True((bool)cv.Value);

            Assert.False((bool)converter.ToObject(new DbValue(0), null));
            Assert.True((bool)converter.ToObject(new DbValue(1), null));

            Assert.False((bool)converter.ToObject(new DbValue(false), null));
            Assert.True((bool)converter.ToObject(new DbValue(true), null));
        }
Example #4
0
        public void SerializeDbValueListTest()
        {
            var converter = DbValueConverterFactory.Get(typeof(DbValue[]));

            var sampleList = new DbValue[2]
            {
                new DbValue(true),
                new DbValue("hello"),
            };

            var converted = converter.FromObject(sampleList, null);

            Assert.Equal(DbValueType.L, converted.Kind);

            DbValue[] backToObject = (DbValue[])converter.ToObject(converted, null);

            Assert.Equal(sampleList[0].Value, backToObject[0].Value);
            Assert.Equal(sampleList[1].Value, backToObject[1].Value);
        }
        private object DeserializeMap(DatasetInfo model)
        {
            var instance = Activator.CreateInstance(model.Type);

            foreach (var attribute in items)
            {
                var member = model[attribute.Key];

                if (member == null)
                {
                    continue;
                }

                IDbValueConverter converter;

                if (DbValueConverterFactory.TryGet(member.Type, out converter))
                {
                    var value = converter.ToObject(attribute.Value, member);

                    member.SetValue(instance, value);
                }
                else if (attribute.Value.Kind == DbValueType.M)
                {
                    var map = (AttributeCollection)attribute.Value.Value;

                    member.SetValue(instance, map.DeserializeMap(DatasetInfo.Get(member.Type)));

                    continue;
                }
                else if (member.Type.IsArray)
                {
                    var list = DeserializeArray(member.Type.GetElementType(), (DbValue[])attribute.Value.Value);

                    member.SetValue(instance, list);
                }
            }

            return(instance);
        }
        internal static AttributeCollection FromObject(object instance, DatasetInfo schema)
        {
            #region Preconditions

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            #endregion

            var attributes = new AttributeCollection();

            foreach (var member in schema.Members)
            {
                var value = member.GetValue(instance);

                if (value == null)
                {
                    continue;
                }

                var typeInfo = TypeDetails.Get(member.Type);

                IDbValueConverter converter;

                if (DbValueConverterFactory.TryGet(member.Type, out converter))
                {
                    var dbValue = converter.FromObject(value, member);

                    if (dbValue.Kind == DbValueType.Unknown)
                    {
                        continue;
                    }

                    attributes.Add(member.Name, dbValue);
                }
                else if (member.Type.IsArray)
                {
                    var a = (Array)value;

                    var list = new List <DbValue>(a.Length);

                    foreach (var item in a)
                    {
                        list.Add(new DbValue(FromObject(item)));
                    }

                    attributes.Add(member.Name, new DbValue(list));
                }
                else if (typeInfo.IsList && typeInfo.ElementType != null)
                {
                    var a = (IList)value;

                    var list = new List <DbValue>(a.Count);

                    IDbValueConverter c;

                    if (DbValueConverterFactory.TryGet(typeInfo.ElementType, out c))
                    {
                        foreach (var item in a)
                        {
                            list.Add(c.FromObject(item, member));
                        }
                    }
                    else
                    {
                        foreach (var item in a)
                        {
                            list.Add(new DbValue(FromObject(item)));
                        }
                    }

                    attributes.Add(member.Name, new DbValue(list));
                }
                else
                {
                    try
                    {
                        attributes.Add(member.Name, new DbValue(FromObject(value)));
                    }
                    catch
                    {
                        throw new Exception(value.GetType().Name + "/" + typeInfo.IsList);
                    }
                }
            }

            return(attributes);
        }
Example #7
0
        public DbValue(object value)
        {
            var type = value.GetType();

            if (type.IsEnum)
            {
                this.kind  = DbValueType.N;
                this.value = EnumConverter.Default.FromObject(value, null !).Value;

                return;
            }

            if (type.IsArray)
            {
                var elementType = type.GetElementType();

                switch (Type.GetTypeCode(elementType))
                {
                case TypeCode.Byte:                     this.kind = DbValueType.B;              break;

                case TypeCode.String:           this.kind = DbValueType.SS;             break;

                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:           this.kind = DbValueType.NS;             break;

                default: throw new Exception("Invalid array element type:" + type.Name);
                }
            }
            else
            {
                switch (Type.GetTypeCode(type))
                {
                // String
                case TypeCode.String:
                    if ((string)value == string.Empty)
                    {
                        throw new ArgumentException(paramName: nameof(value), message: "Must not be empty");
                    }

                    this.kind = DbValueType.S;

                    break;

                // Numbers
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64: this.kind = DbValueType.N; break;

                case TypeCode.DateTime:
                    // Convert dates to unixtime

                    this.value = new DateTimeOffset((DateTime)value).ToUnixTimeSeconds();
                    this.kind  = DbValueType.N;

                    return;

                // Boolean
                case TypeCode.Boolean:
                    this.value = (bool)value ? 1 : 0;
                    this.kind  = DbValueType.N;

                    return;

                default: {
                    if (type == typeof(DbValue))
                    {
                        var dbValue = (DbValue)value;

                        this.kind  = dbValue.Kind;
                        this.value = dbValue.Value;
                    }
                    else if (type == typeof(AttributeCollection))
                    {
                        this.value = value;
                        this.kind  = DbValueType.M;
                    }
                    else
                    {
                        if (!DbValueConverterFactory.TryGet(type, out IDbValueConverter converter))
                        {
                            throw new Exception("Invalid value type. Was: " + type.Name);
                        }

                        var result = converter.FromObject(value, null !);

                        this.value = result.Value;
                        this.kind  = result.Kind;
                    }

                    break;
                }
                }
            }


            this.value = value;
        }
Example #8
0
        public DbValue(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var t = value.GetType();

            if (t.GetTypeInfo().IsEnum)
            {
                this.kind  = DbValueType.N;
                this.value = EnumConverter.Default.FromObject(value, null).Value;

                return;
            }

            if (t.IsArray)
            {
                var elementType = t.GetElementType();

                var list = value as IList;

                switch (Type.GetTypeCode(elementType))
                {
                case TypeCode.Byte:                     this.kind = DbValueType.B;              break;

                case TypeCode.String:           this.kind = DbValueType.SS;             break;

                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:           this.kind = DbValueType.NS;             break;

                default: throw new Exception("Unexpected array element type:" + t.Name);
                }
            }
            else
            {
                switch (Type.GetTypeCode(t))
                {
                // String
                case TypeCode.String:
                    if (value.ToString() == string.Empty)
                    {
                        throw new ArgumentException(paramName: nameof(value), message: "Must not be empty");
                    }

                    this.kind = DbValueType.S;

                    break;

                // Numbers
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Single:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64: this.kind = DbValueType.N; break;

                case TypeCode.DateTime:
                    // Convert dates to unicode
                    this.value = new XDate((DateTime)value).ToUnixTime();
                    this.kind  = DbValueType.N;

                    return;

                // Boolean
                case TypeCode.Boolean:
                    this.value = (bool)value ? 1 : 0;
                    this.kind  = DbValueType.N;

                    return;

                default: {
                    if (t == typeof(DbValue))
                    {
                        var dbValue = (DbValue)value;

                        this.kind  = dbValue.Kind;
                        this.value = dbValue.Value;
                    }
                    else if (t == typeof(AttributeCollection))
                    {
                        this.value = value;
                        this.kind  = DbValueType.M;
                    }
                    else
                    {
                        IDbValueConverter converter;

                        if (!DbValueConverterFactory.TryGet(t, out converter))
                        {
                            throw new Exception("Unexpected value type. Was: " + t.Name);
                        }

                        var result = converter.FromObject(value, null);

                        this.value = result.Value;
                        this.kind  = result.Kind;
                    }

                    break;
                }
                }
            }


            this.value = value;
        }