Exemplo n.º 1
0
        /// <summary>
        /// Adds a field named TypeIDFieldName with the type id of the object instance, force=true to emit the field even if it is a known type.
        /// Returns true if type id element was added
        /// </summary>
        public virtual bool AddTypeIDField(BSONDocument doc, IBSONSerializable parent, IBSONSerializable self, object ctx, bool force = false)
        {
            if (doc == null)
            {
                return(false);
            }
            if (self == null)
            {
                return(false);
            }

            var add = force || parent == null;
            var t   = self.GetType();

            if (!add)
            {
                add = !parent.IsKnownTypeForBSONDeserialization(t);
            }

            if (add)
            {
                var id = BSONSerializableAttribute.GetGuidTypeAttribute <object, BSONSerializableAttribute>(t)
                         .TypeGuid;

                var telm = new BSONBinaryElement(TypeIDFieldName, new BSONBinary(BSONBinaryType.UUID, id.ToByteArray()));
                doc.Set(telm);
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
 private void buildFromTemplateArgs(BSONDocument root, JSONDataMap template, TemplateArg[] args)
 {
     foreach (var kvp in template)
     {
         root.Set(jToB(kvp.Key, kvp.Value, args));
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Converts row to BSON document suitable for storage in MONGO.DB.
        /// Pass target name (name of particular store/epoch/implementation) to get targeted field metadata.
        /// Note: the supplied row MAY NOT CONTAIN REFERENCE CYCLES - either direct or transitive
        /// </summary>
        public virtual BSONDocumentElement RowToBSONDocumentElement(Row row, string targetName, bool useAmorphousData = true, string name = null, FieldFilterFunc filter = null)
        {
            if (row == null)
            {
                return(null);
            }

            var amrow = row as IAmorphousData;

            if (amrow != null && useAmorphousData && amrow.AmorphousDataEnabled)
            {
                amrow.BeforeSave(targetName);
            }

            var result = new BSONDocument();

            foreach (var field in row.Schema)
            {
                var attr = field[targetName];
                if (attr != null && attr.StoreFlag != StoreFlag.OnlyStore && attr.StoreFlag != StoreFlag.LoadAndStore)
                {
                    continue;
                }

                if (filter != null)//20160210 Dkh+SPol
                {
                    if (!filter(row, null, field))
                    {
                        continue;
                    }
                }

                var el = GetFieldAsBSON(row, field, targetName);
                result.Set(el);
            }

            if (amrow != null && useAmorphousData && amrow.AmorphousDataEnabled)
            {
                foreach (var kvp in amrow.AmorphousData)
                {
                    result.Set(GetAmorphousFieldAsBSON(kvp, targetName));
                }
            }

            return(name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result));
        }
Exemplo n.º 4
0
        public static BSONDocumentElement Amount_CLRtoBSON(string name, Amount amount)
        {
            var curEl = new BSONStringElement("c", amount.CurrencyISO);
            var valEl = Decimal_CLRtoBSON("v", amount.Value);
            var doc   = new BSONDocument();

            doc.Set(curEl).Set(valEl);

            return(name != null ? new BSONDocumentElement(name, doc) : new BSONDocumentElement(doc));
        }
Exemplo n.º 5
0
        /// <summary>
        /// override to perform the conversion. the data is never null here, and ref cycles a ruled out
        /// </summary>
        protected virtual BSONElement DoConvertCLRtoBSON(string name, object data, Type dataType, string targetName)
        {
            //1 Primitive/direct types
            Func <string, object, BSONElement> func;

            if (m_CLRtoBSON.TryGetValue(dataType, out func))
            {
                return(func(name, data));
            }

            //2 Enums
            if (dataType.IsEnum)
            {
                return(name != null ? new BSONStringElement(name, data.ToString()) : new BSONStringElement(data.ToString()));
            }

            //3 Complex Types
            if (data is Row)
            {
                return(this.RowToBSONDocumentElement((Row)data, targetName, name: name));
            }

            //IDictionary //must be before IEnumerable
            if (data is IDictionary)
            {
                var dict   = (IDictionary)data;
                var result = new BSONDocument();
                foreach (var key in dict.Keys)
                {
                    var fldName = key.ToString();
                    var el      = ConvertCLRtoBSON(fldName, dict[key], targetName);
                    result.Set(el);
                }
                return(name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result));
            }

            //IEnumerable
            if (data is IEnumerable)
            {
                var list = (IEnumerable)data;
                List <BSONElement> elements = new List <BSONElement>();
                foreach (var obj in list)
                {
                    var el = ConvertCLRtoBSON(null, obj, targetName);
                    elements.Add(el);
                }
                var result = name != null ? new BSONArrayElement(name, elements.ToArray()) : new BSONArrayElement(elements.ToArray());
                return(result);
            }


            throw new BSONException(StringConsts.CLR_BSON_CONVERSION_TYPE_NOT_SUPPORTED_ERROR.Args(dataType.FullName));
        }
Exemplo n.º 6
0
 private static BSONDocument onNullOrEmpty(BSONDocument document, string name, bool skipNull, bool required)
 {
     if (required)
     {
         throw new BSONException("BSONDocument.Add(required=true&&value=null)");
     }
     if (!skipNull)
     {
         return(document.Set(new BSONNullElement(name)));
     }
     return(document);
 }
Exemplo n.º 7
0
        public static BSONDocument Add(this BSONDocument document, string name, object value, bool skipNull = false, bool required = false)
        {
            if (value == null)
            {
                return(onNullOrEmpty(document, name, skipNull, required));
            }

            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:   return(document.Set(new BSONNullElement(name)));

            case TypeCode.Boolean:  return(document.Set(new BSONBooleanElement(name, (bool)value)));

            case TypeCode.Char:     return(document.Set(new BSONStringElement(name, value.ToString())));

            case TypeCode.SByte:    return(document.Set(new BSONInt32Element(name, (sbyte)value)));

            case TypeCode.Byte:     return(document.Set(new BSONInt32Element(name, (byte)value)));

            case TypeCode.Int16:    return(document.Set(new BSONInt32Element(name, (short)value)));

            case TypeCode.UInt16:   return(document.Set(new BSONInt32Element(name, (ushort)value)));

            case TypeCode.Int32:    return(document.Set(new BSONInt32Element(name, (int)value)));

            case TypeCode.UInt32:   return(document.Set(new BSONInt32Element(name, (int)(uint)value)));

            case TypeCode.Int64:    return(document.Set(new BSONInt64Element(name, (long)value)));

            case TypeCode.UInt64:   return(document.Set(new BSONInt64Element(name, (long)(ulong)value)));

            case TypeCode.Single:   return(document.Set(new BSONDoubleElement(name, (float)value)));

            case TypeCode.Double:   return(document.Set(new BSONDoubleElement(name, (double)value)));

            case TypeCode.Decimal:  return(document.Set(RowConverter.Decimal_CLRtoBSON(name, (decimal)value)));

            case TypeCode.DateTime: return(document.Set(new BSONDateTimeElement(name, (DateTime)value)));

            case TypeCode.String:   return(document.Set(new BSONStringElement(name, (string)value)));

            case TypeCode.Object:
            {
                if (value is Guid)
                {
                    var guid = (Guid)value;
                    if (guid == Guid.Empty)
                    {
                        return(onNullOrEmpty(document, name, skipNull, required));
                    }
                    return(document.Set(new BSONBinaryElement(name, new BSONBinary(BSONBinaryType.UUID, ((Guid)value).ToByteArray()))));
                }
                else if (value is GDID)
                {
                    var gdid = (GDID)value;
                    if (gdid.IsZero)
                    {
                        return(onNullOrEmpty(document, name, skipNull, required));
                    }
                    return(document.Set(RowConverter.GDID_CLRtoBSON(name, gdid)));
                }
                else if (value is TimeSpan)
                {
                    return(document.Set(new BSONInt64Element(name, ((TimeSpan)value).Ticks)));
                }
                else if (value is BSONDocument)
                {
                    return(document.Set(new BSONDocumentElement(name, (BSONDocument)value)));
                }
                else if (value is byte[])
                {
                    return(document.Set(new BSONBinaryElement(name, new BSONBinary(BSONBinaryType.GenericBinary, (byte[])value))));
                }
                throw new BSONException("BSONDocument.Add(not supported object type '{0}')".Args(value.GetType().Name));
            }

            default: throw new BSONException("BSONDocument.Add(not supported object type '{0}')".Args(value.GetType().Name));
            }
        }