public void BuildUsingAdHockSchema() { var schema = new Schema("TEZT", new Schema.FieldDef("ID", typeof(int), new List<FieldAttribute>{ new FieldAttribute(required: true, key: true)}), new Schema.FieldDef("Description", typeof(string), new List<FieldAttribute>{ new FieldAttribute(required: true)}) ); var tbl = new Table(schema); for(var i=0; i<1000; i++) { var row = new DynamicRow(tbl.Schema); row["ID"] = i; row["Description"] = "Item-{0}".Args(i); tbl.Insert( row ); } Assert.AreEqual(1000, tbl.Count); var match = tbl.FindByKey(178); Assert.IsNotNull( match ); Assert.AreEqual("Item-178", match["Description"]); }
public void Slim_SerializeTable_DynamicRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) { var row = new DynamicRow( tbl.Schema ); row["ID"] = "POP{0}".Args(i); row["FirstName"] = "Oleg"; row["LastName"] = "Popov-{0}".Args(i); row["DOB"] = new DateTime(1953, 12, 10); row["YearsInSpace"] = 12; tbl.Insert( row ); } var ser = new SlimSerializer(); using(var ms = new MemoryStream()) { ser.Serialize(ms, tbl); Console.WriteLine("{0} rows took {1} bytes".Args(tbl.Count, ms.Position)); ms.Position = 0; var tbl2 = ser.Deserialize(ms) as Table; Assert.IsNotNull( tbl2 ); Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) ); Assert.AreEqual( 1000, tbl2.Count); Assert.IsTrue( tbl.SequenceEqual( tbl2 ) ); } }
/// <summary> /// Creates a shallow copy from another table, optionally applying a filter /// </summary> public Table(Table other, Func<Row, bool> filter = null) : base(other.Schema) { if (filter==null) m_List = new List<Row>(other.m_List); else m_List = other.Where(filter).ToList(); }
public void TypedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); tbl.Insert( new Person{ ID = "POP1", FirstName = "Oleg", LastName = "Popov-1", DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); var ser = new SlimSerializer(); using(var ms = new MemoryStream()) { ser.Serialize(ms, tbl); ms.Position = 0; var tbl2 = ser.Deserialize(ms) as Table; Assert.IsNotNull( tbl2 ); Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) ); Assert.IsFalse( object.ReferenceEquals(tbl.Schema ,tbl2.Schema) ); Assert.IsTrue( tbl.Schema.IsEquivalentTo(tbl2.Schema)); } }
public void DynamicRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); var row = new DynamicRow( tbl.Schema ); row["ID"] = "POP1"; row["FirstName"] = "Oleg"; row["LastName"] = "Popov-1"; row["DOB"] = new DateTime(1953, 12, 10); row["YearsInSpace"] = 12; tbl.Insert( row ); var ser = new SlimSerializer(); using(var ms = new MemoryStream()) { ser.Serialize(ms, tbl); ms.Position = 0; var tbl2 = ser.Deserialize(ms) as Table; Assert.IsNotNull( tbl2 ); Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) ); Assert.IsFalse( object.ReferenceEquals(tbl.Schema ,tbl2.Schema) ); Assert.IsTrue( tbl.Schema.IsEquivalentTo(tbl2.Schema)); } }
public void LogChanges_Delete() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); tbl.Insert( new Person{ ID = "POP1", FirstName = "Oleg", LastName = "Popov", DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); tbl.LogChanges = true; tbl.Delete( tbl[0] ); Assert.AreEqual(1, tbl.ChangeCount); Assert.AreEqual(0, tbl.Count); Assert.AreEqual(RowChangeType.Delete, tbl.GetChangeAt(0).Value.ChangeType); }
public void Table_FromJSON_ShemaOnly() { var src = new Table(new TeztRow().Schema); var options = new NFX.Serialization.JSON.JSONWritingOptions { RowsetMetadata = true, SpaceSymbols = true, IndentWidth = 2, MemberLineBreak = true, ObjectLineBreak = true }; var json = src.ToJSON(options); var trg = RowsetBase.FromJSON(json, true); schemaAssertions(trg.Schema, src.Schema); Assert.AreEqual(trg.Count, 0); }
public void Table_FromJSON(bool rowsAsMap) { var row = new TeztRow(); var src = new Table(row.Schema); row.BoolField = true; row.CharField = 'a'; row.StringField = "aaa"; row.DateTimeField = new DateTime(2016, 1, 2); row.GDIDField = new GDID(1, 2, 3); row.ByteField = 100; row.ShortField = -100; row.IntField = -999; row.UIntField = 254869; row.LongField = -267392; row.FloatField = 32768.32768F; row.DoubleField = -1048576.1048576D; row.DecimalField = 1.0529M; row.NullableField = null; row.ArrayInt = new int[] {-1, 0, 1}; row.ListString = new List<string> {"one", "two", "three"}; row.DictionaryIntStr = new Dictionary<int, string> { {1, "first"}, {2, "second"} }; row.RowField = new Person { Name = "John", Age = 20 }; src.Add(row); row.BoolField = false; row.CharField = 'b'; row.StringField = "bbb"; row.DateTimeField = new DateTime(2016, 2, 1); row.GDIDField = new GDID(4, 5, 6); row.ByteField = 101; row.ShortField = 100; row.IntField = 999; row.UIntField = 109876; row.LongField = 267392; row.FloatField = -32768.32768F; row.DoubleField = -048576.1048576D; row.DecimalField = -1.0529M; row.NullableField = null; row.ArrayInt = new int[] {1, 0, -1}; row.ListString = new List<string> { "three","two", "one" }; row.DictionaryIntStr = new Dictionary<int, string> { {0, "zero"}, {1, "first"}, {2, "second"} }; row.RowField = new Person { Name = "Ann", Age = 19 }; src.Add(row); var options = new NFX.Serialization.JSON.JSONWritingOptions { RowsetMetadata = true, SpaceSymbols = true, IndentWidth = 2, MemberLineBreak = true, ObjectLineBreak = true, RowsAsMap = rowsAsMap }; var json = src.ToJSON(options); var trg = RowsetBase.FromJSON(json); schemaAssertions(trg.Schema, src.Schema); rowsAssertions(src, trg, rowsAsMap); }
public void Slim_SerializeTable_ComplexTypedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(PersonWithNesting))); for(var i=0; i<1000; i++) tbl.Insert( new PersonWithNesting{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12, LatestHistory = new HistoryItem{ ID = "111", StartDate = DateTime.Now, Description="Chaplin" }, History1 = new List<HistoryItem> { new HistoryItem{ ID = "789211", StartDate = DateTime.Now, Description="Chaplin with us" }, new HistoryItem{ ID = "234234", StartDate = DateTime.Now, Description="Chaplin with you" } }, History2 = new HistoryItem[2] }); var ser = new SlimSerializer(); using(var ms = new MemoryStream()) { ser.Serialize(ms, tbl); Console.WriteLine("{0} rows took {1} bytes".Args(tbl.Count, ms.Position)); ms.Position = 0; var tbl2 = ser.Deserialize(ms) as Table; Assert.IsNotNull( tbl2 ); Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) ); Assert.AreEqual( 1000, tbl2.Count); Assert.IsTrue( tbl.SequenceEqual( tbl2 ) ); } }
public void PopulateAndUpsertNonExisting() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) tbl.Insert( new Person{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); var update = new Person{ ID = "GOODMAN17", FirstName = "John", LastName = "Jeffer", DOB = new DateTime(1952, 12, 10), YearsInSpace = 14 }; var existed = tbl.Upsert(update);//<-------------!!!!!! Assert.IsFalse( existed ); Assert.AreEqual(1001, tbl.Count); var match = tbl.FindByKey("POP17") as Person; Assert.IsNotNull( match ); Assert.AreEqual("Oleg", match.FirstName); Assert.AreEqual("Popov-17", match.LastName); match = tbl.FindByKey("GOODMAN17") as Person; Assert.IsNotNull( match ); Assert.AreEqual("John", match.FirstName); Assert.AreEqual("Jeffer", match.LastName); }
public void PopulateAndUpdateNonExisting() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) tbl.Insert( new Person{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); var update = new Person{ ID = "NONE17", FirstName = "Yaroslav", LastName = "Suzkever", DOB = new DateTime(1952, 12, 10), YearsInSpace = 14 }; var idx = tbl.Update(update);//<-------------!!!!!! Assert.IsTrue( idx==-1 ); var match = tbl.FindByKey("NONE17") as Person; Assert.IsNull( match ); }
public void PopulateAndFindKey_TypedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) tbl.Insert( new Person{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); Assert.AreEqual(1000, tbl.Count); var match1 = tbl.FindByKey("POP35"); Assert.IsNotNull( match1 ); Assert.AreEqual("Popov-35", match1["LastName"]); //example of dynamic row access var match2 = tbl.FindByKey("POP36") as Person; Assert.IsNotNull( match2 ); Assert.AreEqual("Popov-36", match2.LastName);//example of typed row access var match3 = tbl.FindByKey("DoesNotExist"); Assert.IsNull( match3 ); }
public void PopulateAndFindKey_MixedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) { var row = new DynamicRow(tbl.Schema); row["ID"] = "DYN{0}".Args(i); row["FirstName"] = "Oleg"; row["LastName"] = "DynamicPopov-{0}".Args(i); row["DOB"] = new DateTime(1953, 12, 10); row["YearsInSpace"] = 12; tbl.Insert( row ); tbl.Insert( new Person{ ID = "TYPED{0}".Args(i), FirstName = "Oleg", LastName = "TypedPopov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); } Assert.AreEqual(2000, tbl.Count); var match1 = tbl.FindByKey("DYN35"); Assert.IsNotNull( match1 ); Assert.IsTrue( match1 is DynamicRow ); Assert.AreEqual("DynamicPopov-35", match1["LastName"]); var match2 = tbl.FindByKey("TYPED36") as Person; Assert.IsNotNull( match2 ); Assert.AreEqual("TypedPopov-36", match2["LastName"]); var match3 = tbl.FindByKey("DoesNotExist"); Assert.IsNull( match3 ); }
public void PopulateAndFindKey_DynamicRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) { var row = new DynamicRow(tbl.Schema); row["ID"] = "POP{0}".Args(i); row["FirstName"] = "Oleg"; row["LastName"] = "Popov-{0}".Args(i); row["DOB"] = new DateTime(1953, 12, 10); row["YearsInSpace"] = 12; tbl.Insert( row ); } Assert.AreEqual(1000, tbl.Count); var match1 = tbl.FindByKey("POP35"); Assert.IsNotNull( match1 ); Assert.AreEqual("Popov-35", match1["LastName"]); var match2 = tbl.FindByKey("POP36") as DynamicRow; Assert.IsNotNull( match2 ); Assert.AreEqual("Popov-36", match2["LastName"]); var match3 = tbl.FindByKey("DoesNotExist"); Assert.IsNull( match3 ); }
public void PopulateAndFindCompositeKey_TypedRows() { var tbl = new Table(Schema.GetForTypedRow(typeof(WithCompositeKey))); for(var i=0; i<1000; i++) tbl.Insert( new WithCompositeKey{ ID = "ID{0}".Args(i), StartDate = new DateTime(1953, 12, 10), Description = "Descr{0}".Args(i) }); Assert.AreEqual(1000, tbl.Count); var match1 = tbl.FindByKey("ID35", new DateTime(1953, 12, 10)); Assert.IsNotNull( match1 ); Assert.AreEqual("Descr35", match1["Description"]); var match2 = tbl.FindByKey("ID35", new DateTime(1953, 07, 10)); Assert.IsNull( match2 ); }
public void PopulateAndDeleteNonExisting() { var tbl = new Table(Schema.GetForTypedRow(typeof(Person))); for(var i=0; i<1000; i++) tbl.Insert( new Person{ ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); var delete = new Person{ ID = "NONE17" }; var idx = tbl.Delete( delete );//<-------------!!!!!! Assert.IsTrue( idx==-1 ); Assert.AreEqual(1000, tbl.Count); var match = tbl.FindByKey("POP17") as Person; Assert.IsNotNull( match ); }