public bool Delete(int id)
        {
            var users = _liteDb.GetCollection <User>("Users");

            var value = new LiteDB.BsonValue(id);

            return(users.Delete(value));
        }
Пример #2
0
 public static void DeleteTodo(int id)
 {
     using (var db = new LiteDatabase(connectionString))
     {
         var todos  = db.GetCollection <Note>("todos");
         var bsonId = new LiteDB.BsonValue(id);
         todos.Delete(bsonId);
     }
 }
Пример #3
0
 /// <summary>
 /// Delete an Question Item by Question ID (GUID)
 /// </summary>
 /// <param name="QuestionId">Question Id(Guid)</param>
 public void Delete(Guid QuestionId)
 {
     using (var db = new LiteDatabase(liteDBPath))
     {
         var Questions = db.GetCollection <Question>("Questions");
         var value     = new LiteDB.BsonValue(QuestionId);//id is an int parameter passed in
         Questions.Delete(value);
         //Questions.Delete(i => i.QuestionId == QuestionId);
     }
 }
Пример #4
0
        public DBResult DeleteFavoriteProduct(ProductInfo entity)
        {
            DBResult result = new DBResult();

            using (var db = new LiteDatabase(AppRuntime.LiteDbConnectionString))
            {
                try
                {
                    var data  = db.GetCollection <Schemas.Product.ProductInfo>("ProductInfo");
                    var value = new LiteDB.BsonValue(entity.Id);
                    data.Delete(value);
                    result.IsSucess = true;
                }
                catch (Exception ex)
                {
                    result.IsSucess = false;
                    result.Message  = ex.Message;
                }
            }
            return(result);
        }
Пример #5
0
 /// <summary>
 /// Return true if value is BINARY
 /// </summary>
 public static BsonValue IS_BINARY(BsonValue value) => value.IsBinary;
Пример #6
0
 /// <summary>
 /// Return true if value is ARRAY
 /// </summary>
 public static BsonValue IS_ARRAY(BsonValue value) => value.IsArray;
Пример #7
0
 public QueryStartsWith(string field, BsonValue value)
     : base(field)
 {
     _value = value;
 }
Пример #8
0
 /// <summary>
 /// Return true if value is DATE (alias to DATETIME)
 /// </summary>
 public static BsonValue IS_MAXVALUE(BsonValue value) => value.IsMaxValue;
Пример #9
0
 /// <summary>
 /// Alias to INT64(values)
 /// </summary>
 public static BsonValue LONG(BsonValue value) => INT64(value);
Пример #10
0
 /// <summary>
 /// Return true if value is INT64
 /// </summary>
 public static BsonValue IS_INT64(BsonValue value) => value.IsInt64;
Пример #11
0
 /// <summary>
 /// Return true if value is BOOLEAN
 /// </summary>
 public static BsonValue IS_BOOLEAN(BsonValue value) => value.IsBoolean;
Пример #12
0
 /// <summary>
 /// Return true if value is NULL
 /// </summary>
 public static BsonValue IS_NULL(BsonValue value) => value.IsNull;
Пример #13
0
 /// <summary>
 /// Return true if value is INT32
 /// </summary>
 public static BsonValue IS_INT32(BsonValue value) => value.IsInt32;
Пример #14
0
 /// <summary>
 /// Return true if value is MINVALUE
 /// </summary>
 public static BsonValue IS_MINVALUE(BsonValue value) => value.IsMinValue;
Пример #15
0
        /// <summary>
        /// Return entity by _id key. Throws InvalidOperationException if no document
        /// </summary>
        public T SingleById(BsonValue id)
        {
            _query = Query.EQ("_id", id);

            return(this.ToEnumerable().Single());
        }
Пример #16
0
 internal override void NormalizeValues(IndexOptions options)
 {
     _value = _value.Normalize(options);
 }
Пример #17
0
 /// <summary>
 /// Return true if value is OBJECTID
 /// </summary>
 public static BsonValue IS_OBJECTID(BsonValue value) => value.IsObjectId;
Пример #18
0
 /// <summary>
 /// Return true if value is DOUBLE
 /// </summary>
 public static BsonValue IS_DOUBLE(BsonValue value) => value.IsDouble;
Пример #19
0
 /// <summary>
 /// Return true if value is GUID
 /// </summary>
 public static BsonValue IS_GUID(BsonValue value) => value.IsGuid;
Пример #20
0
 /// <summary>
 /// Return true if value is DECIMAL
 /// </summary>
 public static BsonValue IS_DECIMAL(BsonValue value) => value.IsDecimal;
Пример #21
0
 /// <summary>
 /// Return true if value is DATETIME
 /// </summary>
 public static BsonValue IS_DATETIME(BsonValue value) => value.IsDateTime;
Пример #22
0
 /// <summary>
 /// Return true if value is NUMBER (int, double, decimal)
 /// </summary>
 public static BsonValue IS_NUMBER(BsonValue value) => value.IsNumber;
Пример #23
0
 /// <summary>
 /// Alias to INT32(values)
 /// </summary>
 public static BsonValue INT(BsonValue value) => INT32(value);
Пример #24
0
        /// <summary>
        /// Deserilize a BsonValue to .NET object based on type parameter
        /// </summary>
        public object Deserialize(Type type, BsonValue value)
        {
            // null value - null returns
            if (value.IsNull)
            {
                return(null);
            }

            // if is nullable, get underlying type
            if (Reflection.IsNullable(type))
            {
                type = Reflection.UnderlyingTypeOf(type);
            }

            // test if has a custom type implementation
            if (_customDeserializer.TryGetValue(type, out Func <BsonValue, object> custom))
            {
                return(custom(value));
            }

            // check if your type is already a BsonValue/BsonDocument/BsonArray
            if (type == typeof(BsonValue))
            {
                return(value);
            }
            else if (type == typeof(BsonDocument))
            {
                return(value.AsDocument);
            }
            else if (type == typeof(BsonArray))
            {
                return(value.AsArray);
            }

            // raw values to native bson values
            else if (_bsonTypes.Contains(type))
            {
                return(value.RawValue);
            }

            // simple ConvertTo to basic .NET types
            else if (_basicTypes.Contains(type))
            {
                return(Convert.ChangeType(value.RawValue, type));
            }

            // special cast to UInt64 to Int64
            else if (type == typeof(UInt64))
            {
                return(unchecked ((UInt64)value.AsInt64));
            }

            // enum value is an int
            else if (type.IsEnum)
            {
                if (value.IsString)
                {
                    return(Enum.Parse(type, value.AsString));
                }

                if (value.IsNumber)
                {
                    return(Enum.ToObject(type, value.AsInt32));
                }
            }

            // if value is array, deserialize as array
            else if (value.IsArray)
            {
                // when array are from an object (like in Dictionary<string, object> { ["array"] = new string[] { "a", "b" }
                if (type == typeof(object))
                {
                    return(this.DeserializeArray(typeof(object), value.AsArray));
                }
                if (type.IsArray)
                {
                    return(this.DeserializeArray(type.GetElementType(), value.AsArray));
                }
                else
                {
                    return(this.DeserializeList(type, value.AsArray));
                }
            }

            // if value is document, deserialize as document
            else if (value.IsDocument)
            {
                // if type is anonymous use special handler
                if (type.IsAnonymousType())
                {
                    return(this.DeserializeAnonymousType(type, value.AsDocument));
                }

                var doc = value.AsDocument;

                // test if value is object and has _type
                if (doc.TryGetValue("_type", out var typeField) && typeField.IsString)
                {
                    type = _typeNameBinder.GetType(typeField.AsString);

                    if (type == null)
                    {
                        throw LiteException.InvalidTypedName(typeField.AsString);
                    }
                }
                // when complex type has no definition (== typeof(object)) use Dictionary<string, object> to better set values
                else if (type == typeof(object))
                {
                    type = typeof(Dictionary <string, object>);
                }

                var entity = this.GetEntityMapper(type);

                // initialize CreateInstance
                if (entity.CreateInstance == null)
                {
                    entity.CreateInstance =
                        this.GetTypeCtor(entity) ??
                        ((BsonDocument v) => Reflection.CreateInstance(entity.ForType));
                }

                var o = _typeInstantiator(type) ?? entity.CreateInstance(doc);

                if (o is IDictionary dict)
                {
                    if (o.GetType().IsGenericType)
                    {
                        var k = type.GetGenericArguments()[0];
                        var t = type.GetGenericArguments()[1];

                        this.DeserializeDictionary(k, t, dict, value.AsDocument);
                    }
                    else
                    {
                        this.DeserializeDictionary(typeof(object), typeof(object), dict, value.AsDocument);
                    }
                }
                else
                {
                    this.DeserializeObject(entity, o, doc);
                }

                return(o);
            }

            // in last case, return value as-is - can cause "cast error"
            // it's used for "public object MyInt { get; set; }"
            return(value.RawValue);
        }
Пример #25
0
 /// <summary>
 /// Alias to BOOLEAN(values)
 /// </summary>
 public static BsonValue BOOL(BsonValue value) => BOOLEAN(value);
Пример #26
0
 public QueryLess(string field, BsonValue value, bool equals)
     : base(field)
 {
     _value  = value;
     _equals = equals;
 }
Пример #27
0
 /// <summary>
 /// Return true if value is STRING
 /// </summary>
 public static BsonValue IS_STRING(BsonValue value) => value.IsString;
Пример #28
0
 public QueryContains(string field, BsonValue value)
     : base(field)
 {
     _value = value;
 }
Пример #29
0
 /// <summary>
 /// Return true if value is DOCUMENT
 /// </summary>
 public static BsonValue IS_DOCUMENT(BsonValue value) => value.IsDocument;
Пример #30
0
        internal object Deserialize(Type type, BsonValue value)
        {
            Func <BsonValue, object> custom;

            // null value - null returns
            if (value.IsNull)
            {
                return(null);
            }

            // if is nullable, get underlying type
            else if (Reflection.IsNullable(type))
            {
                type = Reflection.UnderlyingTypeOf(type);
            }

            // check if your type is already a BsonValue/BsonDocument/BsonArray
            if (type == typeof(BsonValue))
            {
                return(new BsonValue(value));
            }
            else if (type == typeof(BsonDocument))
            {
                return(value.AsDocument);
            }
            else if (type == typeof(BsonArray))
            {
                return(value.AsArray);
            }

            // raw values to native bson values
            else if (_bsonTypes.Contains(type))
            {
                return(value.RawValue);
            }

            // simple ConvertTo to basic .NET types
            else if (_basicTypes.Contains(type))
            {
                return(Convert.ChangeType(value.RawValue, type));
            }

            // special cast to UInt64 to Int64
            else if (type == typeof(UInt64))
            {
                return(unchecked ((UInt64)((Int64)value.RawValue)));
            }

            // enum value is an int
            else if (type.GetTypeInfo().IsEnum)
            {
                return(Enum.Parse(type, value.AsString));
            }

            // test if has a custom type implementation
            else if (_customDeserializer.TryGetValue(type, out custom))
            {
                return(custom(value));
            }

            // if value is array, deserialize as array
            else if (value.IsArray)
            {
                // when array are from an object (like in Dictionary<string, object> { ["array"] = new string[] { "a", "b" }
                if (type == typeof(object))
                {
                    return(this.DeserializeArray(typeof(object), value.AsArray));
                }
                if (type.IsArray)
                {
                    return(this.DeserializeArray(type.GetElementType(), value.AsArray));
                }
                else
                {
                    return(this.DeserializeList(type, value.AsArray));
                }
            }

            // if value is document, deserialize as document
            else if (value.IsDocument)
            {
                BsonValue typeField;
                var       doc = value.AsDocument;

                // test if value is object and has _type
                if (doc.RawValue.TryGetValue("_type", out typeField))
                {
                    type = Type.GetType(typeField.AsString);

                    if (type == null)
                    {
                        throw LiteException.InvalidTypedName(typeField.AsString);
                    }
                }
                // when complex type has no definition (== typeof(object)) use Dictionary<string, object> to better set values
                else if (type == typeof(object))
                {
                    type = typeof(Dictionary <string, object>);
                }

                var o = _typeInstantiator(type);

                if (o is IDictionary && type.GetTypeInfo().IsGenericType)
                {
#if NET35
                    var k = type.GetGenericArguments()[0];
                    var t = type.GetGenericArguments()[1];
#else
                    var k = type.GetTypeInfo().GenericTypeArguments[0];
                    var t = type.GetTypeInfo().GenericTypeArguments[1];
#endif

                    this.DeserializeDictionary(k, t, (IDictionary)o, value.AsDocument);
                }
                else
                {
                    this.DeserializeObject(type, o, doc);
                }

                return(o);
            }

            // in last case, return value as-is - can cause "cast error"
            // it's used for "public object MyInt { get; set; }"
            return(value.RawValue);
        }