Exemplo n.º 1
0
        public void SimpleTransactionTest()
        {
            var store = new SqlCeDataStore("test_trans.sdf");

            store.AddType <LateAddItem>();

            if (store.StoreExists)
            {
                store.DeleteStore();
            }
            store.CreateStore();

            var testEntity = new LateAddItem()
            {
                ID = -1
            };

            store.Insert(testEntity);

            var id = testEntity.ID;

            store.BeginTransaction();
            store.Delete <LateAddItem>(id);
            // timeout here, at insert
            store.Insert(new LateAddItem()
            {
                ID = -1
            });
            store.Commit();
        }
Exemplo n.º 2
0
        public void SimpleCRUDTest()
        {
            var store = new SqlCeDataStore("test.sdf");

            store.AddType <TestItem>();
            store.CreateStore();

            var itemA = new TestItem("ItemA");
            var itemB = new TestItem("ItemB");
            var itemC = new TestItem("ItemC");

            // INSERT
            store.Insert(itemA);
            store.Insert(itemB);
            store.Insert(itemC);

            // COUNT
            var count = store.Count <TestItem>();

            Assert.AreEqual(3, count);

            // SELECT
            var item = store.Select <TestItem>("Name", itemB.Name).FirstOrDefault();

            Assert.IsTrue(item.Equals(itemB));

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Equals(itemC));

            // FETCH

            // UPDATE
            itemC.Name    = "NewItem";
            itemC.Address = "Changed Address";
            itemC.TS      = new TimeSpan(8, 23, 30);
            store.Update(itemC);

            item = store.Select <TestItem>("Name", "ItemC").FirstOrDefault();
            Assert.IsNull(item);
            item = store.Select <TestItem>("Name", itemC.Name).FirstOrDefault();
            Assert.IsTrue(item.Equals(itemC));

            // CONTAINS
            var exists = store.Contains(itemA);

            Assert.IsTrue(exists);

            // DELETE
            store.Delete(itemA, false);
            item = store.Select <TestItem>("Name", itemA.Name).FirstOrDefault();
            Assert.IsNull(item);

            // CONTAINS
            exists = store.Contains(itemA);
            Assert.IsFalse(exists);

            // COUNT
            count = store.Count <TestItem>();
            Assert.AreEqual(2, count);
        }
Exemplo n.º 3
0
        private void CreateTestData(SqlCeDataStore store)
        {
            DataGenerator generator = new DataGenerator();

            var authors = generator.GenerateAuthors(100);

            int         authorID  = 0;
            int         bookID    = 0;
            List <long> authorEts = new List <long>();
            List <long> bookEts   = new List <long>();

            var r = new Random(Environment.TickCount);

            foreach (var author in authors)
            {
                author.AuthorID = authorID++;

                // each author will have 0 to 5 books
                var books = generator.GenerateBooks(r.Next(5));

                foreach (var book in books)
                {
                    book.BookID   = bookID++;
                    book.AuthorID = author.AuthorID;

                    store.Insert(book);
//                    LastBookID = book.BookID;
                }

                store.Insert(author);
//                LastAuthorID = author.AuthorID;
            }
        }
Exemplo n.º 4
0
        public Customer AddCustomer(string name)
        {
            var customer = new Customer()
            {
                Name = name
            };

            m_store.Insert(customer, true);

            return(customer);
        }
Exemplo n.º 5
0
        private void TestCascadingUpdates(SqlCeDataStore store)
        {
            var author = new Author
            {
                Name = "Theodore Geisel"
            };

            store.Insert(author);

            var book = new Book
            {
                BookType = BookType.Fiction,
                Title    = "Fox in Sox"
            };

            author.Books = new Book[] { book };

            store.Update(author);

            var existing = store.Select <Author>(author.AuthorID, true);

            Assert.AreEqual(1, existing.Books.Length);
            Assert.AreEqual("Fox in Sox", existing.Books[0].Title);

            // replace the book title in the author's collection
            existing.Books[0].Title = "Green Eggs and Ham";

            // now Update the Author - this should cascade update the book title
            store.Update(existing);

            existing = store.Select <Author>(author.AuthorID, true);
            Assert.AreEqual("Green Eggs and Ham", existing.Books[0].Title);
        }
Exemplo n.º 6
0
        public void SeekTestDynamic()
        {
            var sw1 = new Stopwatch();

            var store = new SqlCeDataStore("test.sdf");

            store.CreateOrUpdateStore();

            // populate test data
            var fieldList = new List <FieldAttribute>();

            fieldList.Add(new FieldAttribute()
            {
                FieldName    = "ID",
                IsPrimaryKey = true,
                DataType     = System.Data.DbType.Int32
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "SeekField",
                DataType    = System.Data.DbType.Int64,
                AllowsNulls = false,
                SearchOrder = FieldSearchOrder.Ascending
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "Data",
                DataType    = System.Data.DbType.String,
                AllowsNulls = false
            });

            var definition = new DynamicEntityDefinition("DynamicSeekItem", fieldList, KeyScheme.Identity);

            store.RegisterDynamicEntity(definition);


            for (int i = 0; i < 100; i++)
            {
                var de = new DynamicEntity("DynamicSeekItem");
                de.Fields["SeekField"] = i * 10;
                de.Fields["Data"]      = "Item " + (i * 10).ToString();

                store.Insert(de);
            }



            // no delegate
            sw1.Reset();
            sw1.Start();

            var item = store.First("DynamicSeekItem", System.Data.SqlServerCe.DbSeekOptions.BeforeEqual, "SeekField", 11);

            sw1.Stop();

            // item should have a value of 10
        }
Exemplo n.º 7
0
        public void DateTimeTest()
        {
            m_store.TruncateTable("TestItem");

            var itemA = new TestItem("ItemA");

            m_store.Insert(itemA);

            var checkItem = m_store.Select <TestItem>().FirstOrDefault();

            Assert.AreEqual(itemA.TestDate, checkItem.TestDate);

            var now = DateTime.Now;

            itemA.TestDate = now;

            m_store.Update(itemA);
            checkItem = m_store.Select <TestItem>().FirstOrDefault();
            Assert.AreEqual(itemA.TestDate.Second, checkItem.TestDate.Second);
        }
Exemplo n.º 8
0
        public void BasicLocalReplicationTest()
        {
            var source = new SqlCeDataStore("source.sdf");

            if (!source.StoreExists)
            {
                source.CreateStore();
            }
            source.AddType <TestItem>();

            var destination = new SqlCeDataStore("dest.sdf");

            if (!destination.StoreExists)
            {
                destination.CreateStore();
            }

            // build a replictor to send data to the destiantion store
            var replicator = new Replicator(destination, ReplicationBehavior.ReplicateAndDelete);

            // replication is opt-in, so tell it what type(s) we want to replicate
            replicator.RegisterEntity <TestItem>();

            // add the replicator to the source
            source.Replicators.Add(replicator);

            // watch an event for when data batches go out
            replicator.DataReplicated += delegate
            {
                // get a count
                Debug.WriteLine(string.Format("Sent {0} rows", replicator.GetCount <TestItem>()));
            };

            var rows = 200;

            // put some data in the source
            for (int i = 0; i < rows; i++)
            {
                var item = new TestItem(string.Format("Item {0}", i));
                source.Insert(item);
            }

            int remaining = 0;

            // loop until the source table is empty
            do
            {
                Thread.Sleep(500);
                remaining = source.Count <TestItem>();
            } while(remaining > 0);

            // make sure the destination has all rows
            Assert.AreEqual(rows, destination.Count <TestItem>());
        }
Exemplo n.º 9
0
        public void DelegatePerfTest()
        {
            var iterations = 1000;
            var sw1        = new Stopwatch();
            var sw2        = new Stopwatch();

            var store = new SqlCeDataStore("test.sdf");

            store.AddType <TestItem>();
            store.AddType <TestItemD>();
            store.CreateOrUpdateStore();

            // populate test data
            var generator = new DataGenerator();
            var items     = generator.GenerateTestItems(100);

            store.BulkInsert(items);
            foreach (var i in items)
            {
                store.Insert((TestItemD)i);
            }


            // no delegate
            sw1.Reset();
            sw1.Start();
            for (int i = 0; i < iterations; i++)
            {
                var list = store.Select <TestItem>();
            }
            sw1.Stop();
            // with delegate
            sw2.Reset();
            sw2.Start();
            for (int i = 0; i < iterations; i++)
            {
                var list = store.Select <TestItemD>();
            }
            sw2.Stop();

            var noDelegate   = sw1.ElapsedMilliseconds;
            var withDelegate = sw2.ElapsedMilliseconds;

            Debug.WriteLine(string.Format("Delegate gave a {0}% improvement", ((float)(noDelegate - withDelegate) / withDelegate) * 100f));
        }
Exemplo n.º 10
0
        public void TestEnumCRUD(SqlCeDataStore store)
        {
            // truncate the table for this test
            store.Delete <TestTable>();

            var testRow = new TestTable
            {
                EnumField = TestEnum.ValueB
            };

            store.Insert(testRow);

            var existing = store.Select <TestTable>().First();

            Assert.AreEqual(existing.EnumField, testRow.EnumField);

            existing.EnumField = TestEnum.ValueC;
            store.Update(existing);
            var secondPull = store.Select <TestTable>().First();

            Assert.AreEqual(existing.EnumField, secondPull.EnumField);
        }
Exemplo n.º 11
0
        public void BinaryTest()
        {
            var store = new SqlCeDataStore("typetest.sdf");

            store.AddType <BinaryData>();

            Assert.IsTrue(store.StoreExists);

            store.TruncateTable("BinaryData");

            var text = "Section 1\r\n" +
                       "All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives.\r\n" +
                       "Section 2\r\n" +
                       "1:  The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.\r\n" +
                       "2:  No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen.\r\n" +
                       "3:  Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons.2   The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct.  The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three.\r\n" +
                       "4:  When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies.\r\n" +
                       "5:  The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment.\r\n";

            var item = new BinaryData()
            {
                NTextField  = text,
                ImageField  = Encoding.UTF8.GetBytes(text),
                BinaryField = Encoding.Unicode.GetBytes(text)
            };

            store.Insert(item);

            var item2 = store.Select <BinaryData>(item.ID);

            Assert.AreEqual(item.NTextField, item2.NTextField);
            var text2 = Encoding.UTF8.GetString(item2.ImageField);

            Assert.IsTrue(string.Compare(text, text2) == 0);
            var text3 = Encoding.Unicode.GetString(item2.BinaryField);

            Assert.IsTrue(string.Compare(text, text3) == 0);
        }
Exemplo n.º 12
0
        private void TestCascadingInsert(SqlCeDataStore store)
        {
            var testBooks = new Book[]
            {
                new Book
                {
                    Title    = "CSS: The Missing Manual",
                    BookType = BookType.NonFiction
                },

                new Book
                {
                    Title    = "JavaScript: The Missing Manual",
                    BookType = BookType.NonFiction
                },

                new Book
                {
                    Title    = "Dreamweaver: The Missing Manual",
                    BookType = BookType.NonFiction
                },
            };

            // ensures that the entity *and its references* get inserted
            Author a = new Author
            {
                Name = "David McFarland",

                Books = testBooks
            };


            var initialCount = store.Count <Book>();

            // insert, telling ORM to insert references (cascade)
            store.Insert(a, true);

            // pull back to verify
            var author = store.Select <Author>(a.AuthorID, true);
            var count  = store.Count <Book>();

            // we should have inserted 3 new books
            var diff = count - initialCount;

            Assert.IsTrue(diff == 3);


            // create a new author with the same books - the books should *not* get re-inserted - plus one new book
            List <Book> newList = new List <Book>(testBooks);

            newList.Add(
                new Book
            {
                Title    = "My Coauthors Book",
                BookType = BookType.NonFiction
            }
                );

            Author a2 = new Author
            {
                Name = "Test CoAuthor",

                Books = newList.ToArray()
            };

            initialCount = store.Count <Book>();

            // insert, telling ORM to insert references (cascade)
            store.Insert(a2, true);

            author = store.Select <Author>(a.AuthorID, true);
            count  = store.Count <Book>();

            // we should have inserted 1 new book
            diff = count - initialCount;
            Assert.IsTrue(diff == 1);
        }
Exemplo n.º 13
0
        public void SimpleCRUDTest()
        {
            bool beforeInsert = false;
            bool afterInsert  = false;
            bool beforeUpdate = false;
            bool afterUpdate  = false;
            bool beforeDelete = false;
            bool afterDelete  = false;

            var store = new SqlCeDataStore("simpleCRUDTest.sdf");

            store.AddType <TestItem>();
            store.CreateStore();

            store.BeforeInsert += delegate
            {
                beforeInsert = true;
            };
            store.AfterInsert += delegate
            {
                afterInsert = true;
            };
            store.BeforeUpdate += delegate
            {
                beforeUpdate = true;
            };
            store.AfterUpdate += delegate
            {
                afterUpdate = true;
            };
            store.BeforeDelete += delegate
            {
                beforeDelete = true;
            };
            store.AfterDelete += delegate
            {
                afterDelete = true;
            };

            var itemA = new TestItem("ItemA");

            itemA.UUID   = Guid.NewGuid();
            itemA.ITest  = 5;
            itemA.FTest  = 3.14F;
            itemA.DBTest = 1.4D;
            itemA.DETest = 2.678M;

            var itemB = new TestItem("ItemB");
            var itemC = new TestItem("ItemC");

            // INSERT
            store.Insert(itemA);
            Assert.IsTrue(beforeInsert, "BeforeInsert never fired");
            Assert.IsTrue(afterInsert, "AfterInsert never fired");

            store.Insert(itemB);
            store.Insert(itemC);

            // COUNT
            var count = store.Count <TestItem>();

            Assert.AreEqual(3, count);

            // SELECT
            var items = store.Select <TestItem>();

            Assert.AreEqual(3, items.Count());

            var item = store.Select <TestItem>("Name", itemB.Name).FirstOrDefault();

            Assert.IsTrue(item.Equals(itemB));

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Equals(itemC));

            // FETCH

            // UPDATE
            itemC.Name      = "NewItem";
            itemC.Address   = "Changed Address";
            itemC.TS        = new TimeSpan(8, 23, 30);
            itemC.BigString = "little string";

            // test rollback
            store.BeginTransaction();
            store.Update(itemC);
            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Name == itemC.Name);
            store.Rollback();

            item = store.Select <TestItem>(3);
            Assert.IsTrue(item.Name != itemC.Name);

            // test commit
            store.BeginTransaction(System.Data.IsolationLevel.Unspecified);
            store.Update(itemC);
            store.Commit();

            Assert.IsTrue(beforeUpdate, "BeforeUpdate never fired");
            Assert.IsTrue(afterUpdate, "AfterUpdate never fired");

            item = store.Select <TestItem>("Name", "ItemC").FirstOrDefault();
            Assert.IsNull(item);
            item = store.Select <TestItem>("Name", itemC.Name).FirstOrDefault();
            Assert.IsTrue(item.Equals(itemC));

            // CONTAINS
            var exists = store.Contains(itemA);

            Assert.IsTrue(exists);

            // DELETE
            store.Delete(itemA);
            Assert.IsTrue(beforeDelete, "BeforeDelete never fired");
            Assert.IsTrue(afterDelete, "AfterDelete never fired");
            item = store.Select <TestItem>("Name", itemA.Name).FirstOrDefault();
            Assert.IsNull(item);

            // CONTAINS
            exists = store.Contains(itemA);
            Assert.IsFalse(exists);

            // COUNT
            count = store.Count <TestItem>();
            Assert.AreEqual(2, count);

            // this will create the table in newer versions of ORM
            store.AddType <LateAddItem>();

            var newitems = store.Select <LateAddItem>(false);

            Assert.IsNotNull(newitems);
        }
Exemplo n.º 14
0
        public void BasicDynamicCRUDTest()
        {
            var fieldList = new List <FieldAttribute>();

            fieldList.Add(new FieldAttribute()
            {
                FieldName    = "ID",
                IsPrimaryKey = true,
                DataType     = System.Data.DbType.Int32
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName = "FirstName",
                DataType  = System.Data.DbType.String
            });

            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "LastName",
                DataType    = System.Data.DbType.String,
                AllowsNulls = false
            });

            var definition = new DynamicEntityDefinition("People", fieldList, KeyScheme.Identity);

            var store = new SqlCeDataStore(Path.Combine(TestContext.TestDir, "test.sdf"));

            store.CreateStore();

            Assert.IsFalse(store.TableExists(definition.EntityName));

            store.RegisterDynamicEntity(definition);

            Assert.IsTrue(store.TableExists(definition.EntityName));

            var entity = new DynamicEntity("People");

            entity.Fields["FirstName"] = "John";
            entity.Fields["LastName"]  = "Doe";
            store.Insert(entity);

            entity = new DynamicEntity("People");
            entity.Fields["FirstName"] = "Jim";
            entity.Fields["LastName"]  = "Smith";
            store.Insert(entity);

            entity = new DynamicEntity("People");
            entity.Fields["FirstName"] = "Sam";
            entity.Fields["LastName"]  = "Adams";
            store.Insert(entity);

            var items = store.Select("People");

            DumpData(items);

            store.Delete("People", items.First().Fields["ID"]);

            items = store.Select("People");
            DumpData(items);

            store.Delete("People", "LastName", "Smith");

            items = store.Select("People");
            DumpData(items);

            var person = items.First();

            person.Fields["FirstName"] = "Joe";
            person.Fields["LastName"]  = "Satriani";
            store.Update(person);

            items = store.Select("People");
            DumpData(items);

            // now let's change the structure and see what happens
            fieldList.Add(new FieldAttribute()
            {
                FieldName   = "Middle_Name",
                DataType    = System.Data.DbType.Double,
                AllowsNulls = true // this has to be true to add a column
            });

            var newDefinition = new DynamicEntityDefinition("People", fieldList, KeyScheme.Identity);

            store.RegisterDynamicEntity(newDefinition, true);

            items = store.Select("People");
        }