Ejemplo n.º 1
0
        public async Task Data_QueryAsync()
        {
            await All <IDataRepository>(async repo =>
            {
                var d1 = await repo.AddAsync("test", JObject.FromObject(new { Text = "11" }).ToString(), "tester", DateTimeOffset.UtcNow);
                var d2 = await repo.AddAsync("test", JObject.FromObject(new { Text = "12" }).ToString(), "tester", DateTimeOffset.UtcNow);
                var d3 = await repo.AddAsync("test", JObject.FromObject(new { Text = "13" }).ToString(), "tester", DateTimeOffset.UtcNow);

                var asc = (await repo.QueryAsync("test", QParser.Parse("$.Text like '%'").Expression, false, null, 100)).ToArray();
                Assert.Equal("11", asc[0].Data("Text"));
                Assert.Equal("12", asc[1].Data("Text"));
                Assert.Equal("13", asc[2].Data("Text"));

                var desc = (await repo.QueryAsync("test", QParser.Parse("$.Text like '%'").Expression, true, null, 100)).ToArray();
                Assert.Equal("13", desc[0].Data("Text"));
                Assert.Equal("12", desc[1].Data("Text"));
                Assert.Equal("11", desc[2].Data("Text"));

                Assert.Equal(3, (await repo.QueryAsync("test", QParser.Parse("$.Text like '%'").Expression, true, null, 100)).Count());
                Assert.Equal(3, (await repo.QueryAsync("test", QParser.Parse("$.Text like '1%'").Expression, true, null, 100)).Count());
                Assert.Equal(1, (await repo.QueryAsync("test", QParser.Parse("$.Text like '%2'").Expression, true, null, 100)).Count());
                Assert.Equal(1, (await repo.QueryAsync("test", QParser.Parse("$.Text like '%3'").Expression, true, null, 100)).Count());
                Assert.Equal(2, (await repo.QueryAsync("test", QParser.Parse("$.Text like '1%'").Expression, false, d1.Id, 100)).Count());
                Assert.Equal(1, (await repo.QueryAsync("test", QParser.Parse("$.Text like '1%'").Expression, false, d2.Id, 100)).Count());
            });
        }
Ejemplo n.º 2
0
        public void SQLiteTest()
        {
            var builder = new SqliteQueryBuilder();

            Assert.Equal(" json_extract(Data, '$.value') = 100", builder.Build(QParser.Parse("$.value = 100").Expression));
            Assert.Equal(" json_extract(Data, '$.Text') like '%'", builder.Build(QParser.Parse("$.Text like '%'").Expression));
            Assert.Equal(" json_extract(Data, '$.value') = 100 and  json_extract(Data, '$.value') = 100",
                         builder.Build(QParser.Parse("$.value = 100 and $.value = 100").Expression));
            Assert.Equal(" json_extract(Data, '$.value') = 100 and ( json_extract(Data, '$.value') = 100 or  json_extract(Data, '$.value') = 100)",
                         builder.Build(QParser.Parse("$.value = 100 and ($.value = 100 or $.value = 100)").Expression));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <IItem> > QueryAsync(string collectionName, string query, string skipToken = null, int limit = 100, bool desc = true)
        {
            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException(nameof(query));
            }

            var result = QParser.Parse(query);

            if (result.Success == false)
            {
                throw new ArgumentException(nameof(query), $"Invalid query: {result.ErrorMessage}");
            }

            return((await _repository.QueryAsync(collectionName, result.Expression, desc, skipToken, limit))
                   .Select(Map)
                   .ToArray());
        }
Ejemplo n.º 4
0
        public async Task <Template> FindAsync(string templateName)
        {
            var result = QParser.Parse($"$.Name = {templateName}");
            var query  = result.Expression;
            var item   = (await _data.QueryAsync(COLLECTION_NAME, query, true, null, int.MaxValue)).SingleOrDefault();

            if (item == null)
            {
                return(null);
            }
            var data = JsonConvert.DeserializeObject <Entities.Template>(item.Data);

            return(new Template
            {
                Id = item.Id,
                CurrentId = item.CurrentId,
                Name = data.Name,
                Text = data.Text,
                LastModifiedBy = data.LastModifiedBy,
                LastModifiedAt = data.LastModifiedAt
            });
        }
Ejemplo n.º 5
0
 public async Task <Collection> FindCollectionAsync(string collectionName)
 {
     return(ToCollection((await _repository.QueryAsync(COLLECTIONS, QParser.Parse($"$.name = '{collectionName}'").Expression, false, null, 1)).FirstOrDefault()));
 }
Ejemplo n.º 6
0
        public static HtmlResult <HElement> HView(ConsoleState state, JsonData[] jsons, HttpRequestMessage context)
        {
            foreach (var json in jsons.OrEmpty())
            {
                switch (json.JPath("data", "command")?.ToString())
                {
                default:
                    break;
                }
            }
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            var qs_functions_doc  = QParser.Parse(System.IO.File.ReadAllText(HostingEnvironment.MapPath("~/") + "/Data/functions.qs"));
            var qs_functions      = QTransformator.ResolveFunctions(qs_functions_doc.P_s("function"));
            var qs_function_index = qs_functions.GroupBy(function => function.P_("name", "*").AsString())
                                    .ToDictionary(group => group.Key, group => group.ToArray());


            var calculator = new QCalculator(qs_functions);

            var qs_text = System.IO.File.ReadAllText(HostingEnvironment.MapPath("~/") + "/Data/textbook.math.6.qs");
            var qs      = QParser.Parse(qs_text);


            var prepareDuration = watch.Elapsed;

            watch.Reset();
            watch.Start();
            var typed_qs = QTransformator.ProcessAll(qs.P_s("point", "*"), qs_function_index);

            typed_qs = QTransformator.Transform(typed_qs, calculator.Identify).ToArray();
            var transformDuration = watch.Elapsed;

            watch.Reset();
            watch.Start();
            var results = typed_qs.Select(typedNode => Tuple.Create(typedNode, calculator.Calculate(typedNode)))
                          .ToArray();
            var calculateDuration = watch.Elapsed;

            watch.Reset();
            watch.Start();
            var notifies      = QTransformator.Browse(typed_qs, QTransformator.Error).ToArray();
            var types         = QTransformator.Browse(typed_qs, q => q.C(0).P("type").OrEmpty().FirstOrDefault().C(0)).ToArray();
            var usedFunctions = QTransformator.Browse(typed_qs, q => QTransformator.IsAnyType(q.C(0).P("type").OrEmpty().FirstOrDefault().C(0)) ? null : q)
                                .Where(pair => pair.Item2 != null).ToArray();

            watch.Stop();

            var version = ConvertHlp.ToInt(qs.P_("version", "*").RawValue());
            var resultVersionFilename = HostingEnvironment.MapPath("~/App_Data") + $"/textbook.math.6.({version}).qs";

            if (!System.IO.File.Exists(resultVersionFilename))
            {
                System.IO.File.WriteAllText(resultVersionFilename, typed_qs.ToText());
            }

            var elements = new[]
            {
                h.Div($"Подготовка: {prepareDuration}"),
                h.Div($"Преобразование: {transformDuration}"),
                h.Div($"Вычисление: {calculateDuration}"),
                h.Div($"Notifies: {watch.Elapsed}"),
            };


            return(new HtmlResult <HElement>
            {
                Html = Page(state, typed_qs, notifies, types, usedFunctions, calculator.FunctionIndex.Values.SelectMany(_ => _), results, elements, context),
                State = state,
                RefreshPeriod = TimeSpan.FromSeconds(2),
            });
        }