Exemple #1
0
        /// <summary>
        /// Execute query and insert result into another collection. Support external collections
        /// </summary>
        internal BsonDataReader ExecuteQueryInto(string into, BsonAutoId autoId)
        {
            IEnumerable <BsonDocument> GetResultset()
            {
                using (var reader = this.ExecuteQuery(false))
                {
                    while (reader.Read())
                    {
                        yield return(reader.Current.AsDocument);
                    }
                }
            }

            var result = 0;

            // if collection starts with $ it's system collection
            if (into.StartsWith("$"))
            {
                SqlParser.ParseCollection(new Tokenizer(into), out var name, out var options);

                var sys = _engine.GetSystemCollection(name);

                result = sys.Output(GetResultset(), options);
            }
            // otherwise insert as normal collection
            else
            {
                result = _engine.Insert(into, GetResultset(), autoId);
            }

            return(new BsonDataReader(result));
        }
Exemple #2
0
        /// <summary>
        /// Run query over collection using a query definition
        /// </summary>
        public IBsonDataReader Query(string collection, Query query)
        {
            if (string.IsNullOrWhiteSpace(collection))
            {
                throw new ArgumentNullException(nameof(collection));
            }
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            IEnumerable <BsonDocument> source = null;

            // test if is an system collection
            if (collection.StartsWith("$"))
            {
                SqlParser.ParseCollection(new Tokenizer(collection), out var name, out var options);

                // get registered system collection to get data source
                var sys = this.GetSystemCollection(name);

                source     = sys.Input(this, options);
                collection = sys.Name;
            }

            var exec = new QueryExecutor(this, _monitor, _sortDisk, _settings.UtcDate, collection, query, source);

            return(exec.ExecuteQuery());
        }
Exemple #3
0
        public override IEnumerable <BsonDocument> Input(LiteEngine engine, BsonValue options)
        {
            var query = options.AsString;

            var sql = new SqlParser(engine, new Tokenizer(query), null);

            using (var reader = sql.Execute())
            {
                while (reader.Read())
                {
                    var value = reader.Current;

                    yield return(value.IsDocument ? value.AsDocument : new BsonDocument {
                        ["expr"] = value
                    });
                }
            }
        }
        public override IEnumerable <BsonDocument> Input(BsonValue options)
        {
            var query = options?.AsString ?? throw new LiteException(0, $"Collection $query(sql) requires `sql` string parameter");

            var sql = new SqlParser(_engine, new Tokenizer(query), null);

            using (var reader = sql.Execute())
            {
                while (reader.Read())
                {
                    var value = reader.Current;

                    yield return(value.IsDocument ? value.AsDocument : new BsonDocument {
                        ["expr"] = value
                    });
                }
            }
        }