public void Default()
        {
            var test  = new MemoryStorage().CreateDatabase("db").CreateTable <SmallTestStruct>();
            var typed = new Table <long, SmallTestStruct>(test);

            for (var i = 0; i < 1000; i++)
            {
                typed.Insert(new SmallTestStruct()
                {
                    Content = "", DateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddHours(i), Name = "host" + (i % 10), Level = TestEnum.A, Source = "this",
                });
            }
            Assert.AreEqual(1000, test.RowCount);
            Assert.AreEqual(1, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.Content))));
            Assert.AreEqual(1000, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.DateTime))));
            Assert.AreEqual(10, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.Name))));

            Assert.AreEqual(10, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.Content)) + ResultOption.Group(nameof(SmallTestStruct.Name))));

            var rows = typed.GetStructs(Search.None, ResultOption.Group(nameof(SmallTestStruct.Name)) + ResultOption.SortDescending(nameof(SmallTestStruct.Name)));

            Assert.AreEqual(10, rows.Count);
            for (var i = 0; i < 10; i++)
            {
                Assert.AreEqual("host" + (9 - i), rows[i].Name);
            }

            rows = typed.GetStructs(
                Search.FieldGreater(nameof(SmallTestStruct.DateTime), new DateTime(1970, 1, 1, 5, 0, 0, DateTimeKind.Local)) &
                Search.FieldSmallerOrEqual(nameof(SmallTestStruct.DateTime), new DateTime(1970, 1, 1, 10, 0, 0, DateTimeKind.Local)),
                ResultOption.SortDescending(nameof(SmallTestStruct.DateTime)));
            var rowsExpected = typed.GetStructs().
                               Where(i => i.DateTime > new DateTime(1970, 1, 1, 5, 0, 0, DateTimeKind.Local) &&
                                     i.DateTime <= new DateTime(1970, 1, 1, 10, 0, 0, DateTimeKind.Local)).
                               OrderBy(i => - i.DateTime.Ticks).ToList();

            CollectionAssert.AreEqual(rowsExpected, rows);

            rows = typed.GetStructs(
                Search.FieldGreaterOrEqual(nameof(SmallTestStruct.DateTime), new DateTime(1970, 1, 1, 5, 0, 0, DateTimeKind.Local)) &
                Search.FieldSmaller(nameof(SmallTestStruct.DateTime), new DateTime(1970, 1, 1, 10, 0, 0, DateTimeKind.Local)),
                ResultOption.SortAscending(nameof(SmallTestStruct.DateTime)));
            rowsExpected = typed.GetStructs().
                           Where(i => i.DateTime >= new DateTime(1970, 1, 1, 5, 0, 0, DateTimeKind.Local) &&
                                 i.DateTime < new DateTime(1970, 1, 1, 10, 0, 0, DateTimeKind.Local)).
                           OrderBy(i => i.DateTime).ToList();
            CollectionAssert.AreEqual(rowsExpected, rows);

            for (var i = 0; i < 1000; i++)
            {
                var e = new SmallTestStruct()
                {
                    ID = i + 1, Content = "Updated" + i.ToString(), DateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddHours(i % 100), Name = "this", Level = TestEnum.B, Source = "this",
                };
                typed.Update(e);
                Assert.AreEqual(e, typed.GetStruct(i + 1));
            }
            Assert.AreEqual(100, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.DateTime))));
            Assert.AreEqual(1000, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.Content))));
            Assert.AreEqual(1000, test.RowCount);

            for (var i = 0; i < 1000; i++)
            {
                var e = new SmallTestStruct()
                {
                    ID = i + 1, Content = "Replaced", DateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddHours(i), Name = "this", Level = TestEnum.B, Source = "this",
                };
                typed.Update(e);
                Assert.AreEqual(e, typed.GetStruct(i + 1));
            }
            Assert.AreEqual(1000, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.DateTime))));
            Assert.AreEqual(1, test.Count(Search.None, ResultOption.Group(nameof(SmallTestStruct.Content))));
            Assert.AreEqual(1000, test.RowCount);
        }
Esempio n. 2
0
        void FilterList(WebData webData, ITable table, string nameField, string guidField, string text)
        {
            var ids = new Set <long>();

            if (text == null)
            {
                ids.IncludeRange(table.FindRows(Search.None, ResultOption.Limit(20)));
            }
            else
            {
                ids.AddRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20)));
                if (ids.Count < 20)
                {
                    ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20)));
                }
                if (ids.Count < 20)
                {
                    ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count)));
                }
                if (ids.Count < 20)
                {
                    ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count)));
                }
            }
            var json = new JsonWriter();

            json.BeginArray("results");
            if (ids.Count > 0)
            {
                //get items
                var values = table.GetValues <string>(nameField, false, ids.SubRange(0, Math.Min(20, ids.Count)));
                foreach (var value in values)
                {
                    json.BeginObject();
                    json.String("id", value);
                    json.String("text", value);
                    json.EndObject();
                }
            }
            json.EndArray();
            var message = WebMessage.Create(webData.Method, $"Filter {nameField} {text}");

            webData.Answer = WebAnswer.Json(webData.Request, message, json.ToString());
        }