Пример #1
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col    = this.ReadCollection(engine, s);
            var value  = JsonSerializer.Deserialize(s);
            var sid    = s.Scan(@"\s+_?id:(int32|int64|int|long|objectid|datetime|date|guid)", 1).Trim().ToLower();
            var autoId =
                sid == "int32" || sid == "int" ? BsonType.Int32 :
                sid == "int64" || sid == "long" ? BsonType.Int64 :
                sid == "date" || sid == "datetime" ? BsonType.DateTime :
                sid == "guid" ? BsonType.Guid : BsonType.ObjectId;

            s.ThrowIfNotFinish();

            if (value.IsArray)
            {
                var count = engine.InsertBulk(col, value.AsArray.RawValue.Select(x => x.AsDocument), autoId: autoId);

                yield return(count);
            }
            else if (value.IsDocument)
            {
                engine.Insert(col, new BsonDocument[] { value.AsDocument }, autoId);

                yield return(value.AsDocument["_id"]);
            }
            else
            {
                throw LiteException.SyntaxError(s, "Invalid JSON value (must be a document or an array)");
            }
        }
Пример #2
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var fs = new LiteStorage(engine);
            var id = this.ReadId(s);

            s.ThrowIfNotFinish();

            yield return(fs.Delete(id));
        }
Пример #3
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col   = this.ReadCollection(engine, s);
            var query = this.ReadQuery(s, false);

            s.ThrowIfNotFinish();

            yield return(engine.Count(col, query));
        }
Пример #4
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col   = this.ReadCollection(engine, s);
            var index = s.Scan(this.FieldPattern).Trim().ThrowIfEmpty("Missing field index name", s);

            s.ThrowIfNotFinish();

            yield return(engine.DropIndex(col, index));
        }
Пример #5
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col     = this.ReadCollection(engine, s);
            var newName = s.Scan(@"[\w-]+").ThrowIfEmpty("Invalid new collection name", s);

            s.ThrowIfNotFinish();

            yield return(engine.RenameCollection(col, newName));
        }
Пример #6
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col = this.ReadCollection(engine, s);

            // try read any kind of expression
            var expression = BsonExpression.ReadExpression(s, false, false);

            // if not found a valid one, try read only as path (will add $. before)
            if (expression == null)
            {
                expression = BsonExpression.ReadExpression(s, true, true);
            }

            var query = Query.All();

            // support into new_collection
            var into   = s.Scan(@"\s*into\s+([\w-]+)", 1);
            var autoId = BsonType.ObjectId;

            // checks for autoId
            if (into.Length > 0)
            {
                var sid = s.Scan(@"\s+_?id:(int32|int64|int|long|objectid|datetime|date|guid)", 1).Trim().ToLower();
                autoId =
                    sid == "int32" || sid == "int" ? BsonType.Int32 :
                    sid == "int64" || sid == "long" ? BsonType.Int64 :
                    sid == "date" || sid == "datetime" ? BsonType.DateTime :
                    sid == "guid" ? BsonType.Guid : BsonType.ObjectId;
            }

            if (s.Scan(@"\s*where\s*").Length > 0)
            {
                query = this.ReadQuery(s, true);
            }

            var skipLimit = this.ReadSkipLimit(s);
            var includes  = this.ReadIncludes(s);

            s.ThrowIfNotFinish();

            var docs = engine.Find(col, query, includes, skipLimit.Key, skipLimit.Value);

            if (into.Length > 0)
            {
                // insert into results to other collection collection
                var count = engine.InsertBulk(into, this.Execute(docs, expression), autoId: autoId);

                // return inserted documents
                return(new BsonValue[] { count });
            }
            else
            {
                return(this.Execute(docs, expression).Select(x => x as BsonValue));
            }
        }
Пример #7
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var fs       = new LiteStorage(engine);
            var id       = this.ReadId(s);
            var metadata = JsonSerializer.Deserialize(s.ToString()).AsDocument;

            s.ThrowIfNotFinish();

            fs.SetMetadata(id, metadata);

            yield break;
        }
Пример #8
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col       = this.ReadCollection(engine, s);
            var query     = this.ReadQuery(s, false);
            var skipLimit = this.ReadSkipLimit(s);
            var includes  = this.ReadIncludes(s);

            s.ThrowIfNotFinish();

            var docs = engine.Find(col, query, includes, skipLimit.Key, skipLimit.Value);

            foreach (var doc in docs)
            {
                yield return(doc);
            }
        }
Пример #9
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col = this.ReadCollection(engine, s);

            s.ThrowIfNotFinish();

            var indexes = engine.GetIndexes(col);

            foreach (var index in indexes)
            {
                yield return(new BsonDocument
                {
                    { "slot", index.Slot },
                    { "field", index.Field },
                    { "expression", index.Expression },
                    { "unique", index.Unique }
                });
            }
        }
Пример #10
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var    col        = this.ReadCollection(engine, s);
            var    field      = s.Scan(this.FieldPattern).Trim().ThrowIfEmpty("Invalid field/index name", s);
            var    unique     = false;
            string expression = null;

            s.Scan(@"\s*");

            if (s.HasTerminated == false)
            {
                unique = s.Scan(@"unique\s*").Length > 0;

                if (s.Scan(@"\s*using\s+(.+)").Length > 0)
                {
                    expression = BsonExpression.ReadExpression(s, true, false)?.Source;
                }
            }

            s.ThrowIfNotFinish();

            yield return(engine.EnsureIndex(col, field, unique, expression));
        }
Пример #11
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var fs = new LiteStorage(engine);
            IEnumerable <LiteFileInfo> files;

            if (s.HasTerminated)
            {
                files = fs.FindAll();
            }
            else
            {
                var id = this.ReadId(s);

                s.ThrowIfNotFinish();

                files = fs.Find(id);
            }

            foreach (var file in files.Select(x => x.AsDocument))
            {
                yield return(file);
            }
        }
Пример #12
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col = this.ReadCollection(engine, s);

            // single document update
            if (s.Match(@"\s*\{"))
            {
                var doc = JsonSerializer.Deserialize(s.ToString()).AsDocument;

                s.ThrowIfNotFinish();

                yield return(engine.Update(col, doc));
            }
            // query update
            else
            {
                // db.colName.update
                //     field = value,
                //     array += valueToAdd,
                // where _id = 1
                //   and ...

                var query   = Query.All();
                var updates = new Update();

                while (!s.HasTerminated)
                {
                    var path   = BsonExpression.ReadExpression(s, true, true).Source;
                    var action = s.Scan(@"\s*\+?=\s*").Trim().ThrowIfEmpty("Invalid operator (support = or +=)", s);
                    var value  = this.ReadBsonValue(s);
                    var expr   = value == null?BsonExpression.ReadExpression(s, true, false)?.Source : null;

                    if (action == "+=" && value != null)
                    {
                        updates.Add(path, value);
                    }
                    else if (action == "+=" && expr != null)
                    {
                        updates.AddExpr(path, expr);
                    }
                    else if (action == "=" && value != null)
                    {
                        updates.Set(path, value);
                    }
                    else if (action == "=" && expr != null)
                    {
                        updates.SetExpr(path, expr);
                    }
                    else
                    {
                        throw LiteException.SyntaxError(s);
                    }

                    s.Scan(@"\s*");

                    if (s.Scan(@",\s*").Length > 0)
                    {
                        continue;
                    }
                    else if (s.Scan(@"where\s*").Length > 0 || s.HasTerminated)
                    {
                        break;
                    }
                    else
                    {
                        throw LiteException.SyntaxError(s);
                    }
                }

                if (!s.HasTerminated)
                {
                    query = this.ReadQuery(s, false);
                }

                s.ThrowIfNotFinish();

                yield return(engine.Update(col, query, updates));
            }
        }
Пример #13
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col    = this.ReadCollection(engine, s);
            var fields = new Dictionary <string, BsonExpression>();
            var index  = 0;

            // read all fields definitions (support AS as keyword no name field)
            while (!s.HasTerminated)
            {
                // try read any kind of expression
                var expression = BsonExpression.ReadExpression(s, false, false);

                // if not found a valid one, try read only as path (will add $. before)
                if (expression == null)
                {
                    expression = BsonExpression.ReadExpression(s, true, true);
                }

                var key = s.Scan(@"\s*as\s+([\w-]+)", 1).TrimToNull()
                          ?? this.NamedField(expression)
                          ?? ("expr" + (++index));

                // if key already exits, add with another name
                while (fields.ContainsKey(key))
                {
                    key = "expr" + (++index);
                }

                fields.Add(key, expression);

                if (s.Scan(@"\s*,\s*").Length > 0)
                {
                    continue;
                }
                break;
            }

            // select command required output value, path or expression
            if (fields.Count == 0)
            {
                throw LiteException.SyntaxError(s, "Missing select path");
            }

            var query = Query.All();

            if (s.Scan(@"\s*where\s*").Length > 0)
            {
                query = this.ReadQuery(s, true);
            }

            var skipLimit = this.ReadSkipLimit(s);
            var includes  = this.ReadIncludes(s);

            s.ThrowIfNotFinish();

            var docs = engine.Find(col, query, includes, skipLimit.Key, skipLimit.Value);

            foreach (var doc in docs)
            {
                // if is a single value, return as just field
                if (fields.Count == 1)
                {
                    foreach (var value in fields.Values.First().Execute(doc, false))
                    {
                        yield return(value);
                    }
                }
                else
                {
                    var output = new BsonDocument();

                    foreach (var field in fields)
                    {
                        output[field.Key] = field.Value.Execute(doc, true).First();
                    }

                    yield return(output);
                }
            }
        }
Пример #14
0
        public IEnumerable <BsonValue> Execute(StringScanner s, LiteEngine engine)
        {
            var col = this.ReadCollection(engine, s);

            // single document update
            if (s.Match(@"\s*\{"))
            {
                var doc = JsonSerializer.Deserialize(s.ToString()).AsDocument;

                s.ThrowIfNotFinish();

                yield return(engine.Update(col, doc));
            }
            // query update
            else
            {
                // db.colName.update
                //     field = value,
                //     array += valueToAdd,
                // where _id = 1
                //   and ...

                var updates = new List <UpdateData>();
                var query   = Query.All();

                while (!s.HasTerminated)
                {
                    var path   = BsonExpression.ReadExpression(s, true, true).Source;
                    var action = s.Scan(@"\s*\+?=\s*").Trim().ThrowIfEmpty("Invalid operator (support = or +=)", s);
                    var value  = this.ReadBsonValue(s);
                    var expr   = value == null?BsonExpression.ReadExpression(s, true, false) : null;

                    if (action != "+=" && action != "=")
                    {
                        throw LiteException.SyntaxError(s);
                    }
                    if (value == null && expr == null)
                    {
                        throw LiteException.SyntaxError(s);
                    }

                    updates.Add(new UpdateData {
                        Path = path, Value = value, Expr = expr, Add = action == "+="
                    });

                    s.Scan(@"\s*");

                    if (s.Scan(@",\s*").Length > 0)
                    {
                        continue;
                    }
                    else if (s.Scan(@"where\s*").Length > 0 || s.HasTerminated)
                    {
                        break;
                    }
                    else
                    {
                        throw LiteException.SyntaxError(s);
                    }
                }

                if (!s.HasTerminated)
                {
                    query = this.ReadQuery(s, false);
                }

                s.ThrowIfNotFinish();

                // execute command
                var count = 0;

                foreach (var doc in engine.Find(col, query))
                {
                    var docChanged = false;

                    foreach (var update in updates)
                    {
                        var itemChanged = false;

                        if (update.Value == null)
                        {
                            itemChanged = doc.Set(update.Path, update.Expr, update.Add);
                        }
                        else
                        {
                            itemChanged = doc.Set(update.Path, update.Value, update.Add);
                        }

                        if (itemChanged)
                        {
                            docChanged = true;
                        }
                    }

                    // execute update only if document was changed
                    if (docChanged)
                    {
                        engine.Update(col, doc);

                        count++;
                    }
                }

                yield return(count);
            }
        }