Ejemplo n.º 1
0
        public void TestHasAndBelongsToMany()
        {
            var u = new cmmReader("Tom");

            u.arts.Add(new cmmArticle("do"));
            u.arts.Add(new cmmArticle("ok"));
            u.arts.Add(new cmmArticle("go"));
            DbEntry.Save(u);
            var a = DbEntry.GetObject <cmmArticle>(u.arts[2].Id);

            a.rads.Add(new cmmReader("Jerry"));
            a.rads[0].arts.Add(new cmmArticle("pp"));
            DbEntry.Save(a);
            var o1 = DbEntry.GetObject <cmmReader>(u.Id);

            Assert.AreEqual("Tom", o1.Name);
            Assert.AreEqual(3, o1.arts.Count);
            Assert.AreEqual("do", o1.arts[0].Title);
            Assert.AreEqual("ok", o1.arts[1].Title);
            Assert.AreEqual("go", o1.arts[2].Title);
            var o2 = DbEntry.GetObject <cmmReader>(a.rads[0].Id);

            Assert.AreEqual("Jerry", o2.Name);
            Assert.AreEqual(2, o2.arts.Count);
            Assert.AreEqual("go", o2.arts[0].Title);
            Assert.AreEqual("pp", o2.arts[1].Title);
        }
Ejemplo n.º 2
0
        public virtual string Create()
        {
            var ctx = ModelContext.GetInstance(typeof(T));
            var obj = (T)ctx.NewObject();

            foreach (MemberHandler m in ctx.Info.Members)
            {
                if (!m.Is.RelationField && !m.Is.DbGenerate && !m.Is.AutoSavedValue)
                {
                    string s = HttpContextHandler.Instance[ControllerName + "[" + m.Name.ToLower() + "]"];
                    if (m.Is.LazyLoad)
                    {
                        object       ll = m.MemberInfo.GetValue(obj);
                        PropertyInfo pi = m.MemberInfo.MemberType.GetProperty("Value");
                        object       v  = ControllerHelper.ChangeType(s, m.MemberType.GetGenericArguments()[0]);
                        pi.SetValue(ll, v, null);
                    }
                    else
                    {
                        m.MemberInfo.SetValue(obj, ControllerHelper.ChangeType(s, m.MemberType));
                    }
                }
            }
            if (obj is DbObjectSmartUpdate)
            {
                (obj as DbObjectSmartUpdate).Save();
            }
            else
            {
                DbEntry.Save(obj);
            }
            Flash.Notice = string.Format("{0} was successfully created", ControllerName);
            return(UrlTo().Action("list"));
        }
Ejemplo n.º 3
0
        public void Test9()
        {
            // A.Save, if A.B is loaded item and remove it from A, delete A_B
            var a = DbEntry.GetObject <Article>(1);

            Assert.AreEqual(3, a.Readers.Count);
            Assert.AreEqual("tom", a.Readers[0].Name);
            Assert.AreEqual("jerry", a.Readers[1].Name);
            Assert.AreEqual("mike", a.Readers[2].Name);

            a.Readers.RemoveAt(1);
            Assert.AreEqual(2, a.Readers.Count);
            Assert.AreEqual("tom", a.Readers[0].Name);
            Assert.AreEqual("mike", a.Readers[1].Name);

            DbEntry.Save(a);

            a = DbEntry.GetObject <Article>(1);
            Assert.AreEqual(2, a.Readers.Count);
            Assert.AreEqual("tom", a.Readers[0].Name);
            Assert.AreEqual("mike", a.Readers[1].Name);

            var r = DbEntry.GetObject <Reader>(2);

            Assert.IsNotNull(r);
            Assert.AreEqual("jerry", r.Name);
            Assert.AreEqual(1, r.Articles.Count);
            Assert.AreEqual("The world is float", r.Articles[0].Name);
        }
Ejemplo n.º 4
0
 public void Save(DM_ITEMSPLIT itemsplit, DM_ITEM itemA, DM_ITEM itemB, DM_ITEM itemC, DM_ITEM itemD)
 {
     try
     {
         DbEntry.UsingTransaction(delegate
         {
             if (itemsplit.ItemCodeA == "" && itemsplit.Id > 0)
             {
                 this.ExecuteDelete(itemsplit);
             }
             else
             {
                 this.ExecuteUpdate(itemsplit);
                 if (itemA != null)
                 {
                     DbEntry.Save(itemA);
                     //itemA.Save();
                 }
                 if (itemB != null)
                 {
                     DbEntry.Save(itemB);
                 }
                 if (itemC != null)
                 {
                     DbEntry.Save(itemC);
                 }
                 if (itemD != null)
                 {
                     DbEntry.Save(itemD);
                 }
             }
         });
     }
     catch (Exception e) { throw e; }
 }
Ejemplo n.º 5
0
        public void TestHasMany9()
        {
            // If not loading B, and insert a new item in B, then don't loading, when save it, only save which in the memory
            var c = DbEntry.GetObject <Category>(2);
            var b = new Book {
                Name = "Next"
            };

            c.Books.Add(b);
            Assert.AreEqual(1, c.Books.Count);
            DbEntry.Save(c);

            // verify
            c = DbEntry.GetObject <Category>(2);
            Assert.IsNotNull(c);
            Assert.IsTrue(4 == c.Books.Count);

            Assert.AreEqual("Game", c.Name);
            Assert.AreEqual("Diablo", c.Books[0].Name);
            Assert.AreEqual(1, c.Books[0].Id);
            Assert.AreEqual("Pal95", c.Books[1].Name);
            Assert.AreEqual(4, c.Books[1].Id);
            Assert.AreEqual("Wow", c.Books[2].Name);
            Assert.AreEqual(5, c.Books[2].Id);
            Assert.AreEqual("Next", c.Books[3].Name);
            Assert.AreEqual(6, c.Books[3].Id);
        }
Ejemplo n.º 6
0
        public void Test3()
        {
            DbEntry.Delete <Unit>(WhereCondition.EmptyCondition);

            Unit u = DynamicObject.NewObject <Unit>("00001");

            u.Name      = "吨";
            u.CreatedBy = "Jacky";
            u.UpdatedBy = "Admin";

            DbEntry.Save(u);
            var u1 = DbEntry.GetObject <Unit>(u.UnitId);

            Assert.AreEqual(u1.UnitId, u.UnitId);

            u1.Name = "M3";
            DbEntry.Update(u1);

            Unit u2 = DynamicObject.NewObject <Unit>("00002");

            u2.Init("M3", "Admin");
            Assert.AreEqual("M3", u2.Name);
            DbEntry.Save(u2);

            Assert.AreEqual(2, DbEntry.From <Unit>().Where(WhereCondition.EmptyCondition).GetCount());
        }
Ejemplo n.º 7
0
        public void Test2()
        {
            //Role r = new Role() {
            //    Name = "Jacky",
            //    CreatedBy = "Admin",
            //    UpdatedBy = "Admin"
            //};
            Role r = Role.New();

            r.Name      = "Jacky";
            r.CreatedBy = "Admin";
            r.UpdatedBy = "Admin";

            DbEntry.Save(r);

            Role r1 = DbEntry.GetObject <Role>(r.Id);

            Assert.IsNotNull(r1);
            Assert.AreEqual("Admin", r1.CreatedBy);
            r1.Name = "Modified by jacky.";
            DbEntry.Update(r1);

            Role r2 = DbEntry.GetObject <Role>(r.Id);

            Assert.AreEqual("Modified by jacky.", r2.Name);
        }
Ejemplo n.º 8
0
        public void Test1()
        {
            var p = new SinglePerson {
                Name = "abc"
            };

            Assert.AreEqual(0, p.Id);

            DbEntry.Save(p);
            Assert.IsTrue(0 != p.Id);
            var p1 = DbEntry.GetObject <SinglePerson>(p.Id);

            Assert.AreEqual(p.Name, p1.Name);

            p.Name = "xyz";
            DbEntry.Save(p);
            Assert.AreEqual(p.Id, p1.Id);

            p1 = DbEntry.GetObject <SinglePerson>(p.Id);
            Assert.AreEqual("xyz", p1.Name);

            long id = p.Id;

            DbEntry.Delete(p);
            Assert.AreEqual(0, p.Id);
            p1 = DbEntry.GetObject <SinglePerson>(id);
            Assert.IsNull(p1);
        }
Ejemplo n.º 9
0
        public void TestSmartUpdateForDynamicObject()
        {
            var u = LoadAsUser("Tom", 18);

            DbEntry.Save(u);
            Assert.AreEqual(0, StaticRecorder.Messages.Count);
            Assert.AreEqual("", StaticRecorder.LastMessage);
        }
Ejemplo n.º 10
0
        public void TestCountTable2()
        {
            StaticRecorder.ClearMessages();
            var ct = CountTable2Sql.AsLoaded(1, "tom");

            DbEntry.Save(ct);
            Assert.AreEqual("UPDATE [Count_Table2Sql] SET [Name]=@Name_0,[Count]=[Count]+(1)  WHERE [Id] = @Id_1;\n<Text><30>(@Name_0=tom:String,@Id_1=1:Int64)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 11
0
        public void TestDontUpdateIfNotSetValue()
        {
            var u = new sUser(1, "Tom", 18);

            DbEntry.Save(u);
            Assert.AreEqual(0, StaticRecorder.Messages.Count);
            Assert.AreEqual("", StaticRecorder.LastMessage);
        }
Ejemplo n.º 12
0
 public virtual int ExecuteUpdate(List <T> list)
 {
     if (list.Count > 0)
     {
         DbEntry.Save(list.ToArray());
         return(1);
     }
     return(0);
 }
Ejemplo n.º 13
0
        public void Test4()
        {
            Person p = Person.New();

            p.Name    = "Jacky";
            p.PC      = PersonalComputer.New();
            p.PC.Name = "hp dl380 g3";
            DbEntry.Save(p);
        }
Ejemplo n.º 14
0
        public void TestSavedOnWithSetValue()
        {
            var o = new DateTable6 {
                Name = "tom", SavedOn = DateTime.Now, ShopId = 100
            };

            DbEntry.Save(o);
            Assert.AreEqual("INSERT INTO [DateTable] ([ShopId],[SavedOn],[Name]) VALUES (@ShopId_0,DATETIME(CURRENT_TIMESTAMP, 'localtime'),@Name_1);\nSELECT LAST_INSERT_ROWID();\n<Text><30>(@ShopId_0=100:Int64,@Name_1=tom:String)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 15
0
        public void TestSmartUpdateForDynamicObject2()
        {
            var u = LoadAsUser("jerry", 18);

            u.Name = "Tom";
            DbEntry.Save(u);
            Assert.AreEqual(1, StaticRecorder.Messages.Count);
            Assert.AreEqual("UPDATE [as_User] SET [theName]=@theName_0  WHERE [Id] = @Id_1;\n<Text><30>(@theName_0=Tom:String,@Id_1=1:Int64)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 16
0
        public void TestCountTable()
        {
            StaticRecorder.ClearMessages();
            var ct = new CountTableSql {
                Id = 1
            };

            DbEntry.Save(ct);
            Assert.AreEqual("UPDATE [Count_Table_Sql] SET [Count]=[Count]+(1)  WHERE [Id] = @Id_0;\n<Text><30>(@Id_0=1:Int64)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 17
0
        public void TestSmartUpdateForDynamicObject3()
        {
            var u = LoadAsUser("Tom", 18);

            u.Name = "Jerry";
            u.Age  = 25;
            DbEntry.Save(u);
            Assert.AreEqual(1, StaticRecorder.Messages.Count);
            Assert.AreEqual("UPDATE [as_User] SET [theName]=@theName_0,[Age]=@Age_1  WHERE [Id] = @Id_2;\n<Text><30>(@theName_0=Jerry:String,@Age_1=25:Int32,@Id_2=1:Int64)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 18
0
 public void SaveAccess(int userid, List <ND_USER_MENUACCESS> list)
 {
     DbEntry.UsingTransaction(delegate {
         string csql = string.Format("delete from ND_USER_MENUACCESS  where userid={0} ", userid);
         DbEntry.Provider.ExecuteNonQuery(csql);
         DbEntry.Save(list.ToArray());
         //foreach (ND_USER_MENUACCESS a in list) {
         //    a.Save();
         //}
     });
 }
Ejemplo n.º 19
0
        public void TestListClear2()
        {
            var c = DbEntry.GetObject <Category>(3);

            c.Books.Clear();
            DbEntry.Save(c);

            var c1 = DbEntry.GetObject <Category>(3);

            Assert.AreEqual(0, c1.Books.Count);
        }
Ejemplo n.º 20
0
        public void TestPartialUpdateThatSetValue()
        {
            var u = new sUser("Tom", 18)
            {
                Id = 1, Name = "Tom"
            };

            DbEntry.Save(u);
            Assert.AreEqual(1, StaticRecorder.Messages.Count);
            Assert.AreEqual("UPDATE [s_User] SET [Name]=@Name_0  WHERE [Id] = @Id_1;\n<Text><30>(@Name_0=Tom:String,@Id_1=1:Int64)", StaticRecorder.LastMessage);
        }
Ejemplo n.º 21
0
        public void TestUpdateFieldAfterSave()
        {
            var u = LoadAsUser("Tom", 18);

            u.Name = "jerry";
            DbEntry.Save(u);
            u.Age = 25;
            DbEntry.Save(u);
            Assert.AreEqual(2, StaticRecorder.Messages.Count);
            Assert.AreEqual("UPDATE [as_User] SET [theName]=@theName_0  WHERE [Id] = @Id_1;\n<Text><30>(@theName_0=jerry:String,@Id_1=1:Int64)", StaticRecorder.Messages[0]);
            Assert.AreEqual("UPDATE [as_User] SET [Age]=@Age_0  WHERE [Id] = @Id_1;\n<Text><30>(@Age_0=25:Int32,@Id_1=1:Int64)", StaticRecorder.Messages[1]);
        }
Ejemplo n.º 22
0
        public void TestRemoveSubItem()
        {
            var c = DbEntry.GetObject <Acategory>(2);

            Assert.AreEqual(3, c.Books.Count);

            c.Books.RemoveAt(1);
            DbEntry.Save(c);

            var c2 = DbEntry.GetObject <Acategory>(2);

            Assert.AreEqual(2, c2.Books.Count);
        }
Ejemplo n.º 23
0
        public void TestRemoveAnItem2()
        {
            var b = DbEntry.GetObject <Book>(2);

            Assert.AreEqual("Beijing", b.Name);
            Assert.IsNotNull(b.CurCategory);

            b.CurCategory.Value = null;
            DbEntry.Save(b);

            b = DbEntry.GetObject <Book>(2);
            Assert.AreEqual("Beijing", b.Name);
            Assert.IsNull(b.CurCategory.Value);
        }
Ejemplo n.º 24
0
        public void TestHasMany12()
        {
            // A.Save will save B, if b(b.Value) is null, then don't save B
            var c = new Category {
                Name = "Sport"
            };

            DbEntry.Save(c);
            var c1 = DbEntry.GetObject <Category>(c.Id);

            Assert.IsNotNull(c1);
            Assert.IsTrue(0 == c1.Books.Count);
            Assert.AreEqual(c.Name, c1.Name);
        }
Ejemplo n.º 25
0
        public void TestHasOne()
        {
            var u = new ctUser {
                Name = "Tom", info = { Value    = new ctInfo {
                                           iMsg = "ok"
                                       } }
            };

            DbEntry.Save(u);
            var o = DbEntry.GetObject <ctUser>(u.Id);

            Assert.AreEqual("Tom", o.Name);
            Assert.AreEqual("ok", o.info.Value.iMsg);
        }
Ejemplo n.º 26
0
 public void TestPartialUpdateThatSetedValueByTransactionWithException()
 {
     Util.CatchAll(() =>
                   DbEntry.NewTransaction(delegate {
         var u = new sUser(1, "Tom", 18)
         {
             Name = "Tom"
         };
         DbEntry.Save(u);
         throw new Exception();     // emulate exception
     }));
     Assert.AreEqual(0, StaticRecorder.Messages.Count);
     Assert.AreEqual("", StaticRecorder.LastMessage);
 }
Ejemplo n.º 27
0
        public void TestRemoveRelation2()
        {
            var pc = DbEntry.GetObject <PersonalComputer>(1);

            Assert.IsNotNull(pc);
            Assert.IsNotNull(pc.Owner.Value);

            pc.Owner.Value = null;
            DbEntry.Save(pc);

            pc = DbEntry.GetObject <PersonalComputer>(1);
            Assert.IsNotNull(pc);
            Assert.IsNull(pc.Owner.Value);
        }
Ejemplo n.º 28
0
        public void TestListClear3()
        {
            var c = DbEntry.GetObject <Category>(3);
            var b = new Book {
                Name = "test"
            };

            c.Books.Add(b);
            c.Books.Clear();
            DbEntry.Save(c);

            var c1 = DbEntry.GetObject <Category>(3);

            Assert.AreEqual(2, c1.Books.Count);
        }
Ejemplo n.º 29
0
        public void Test8()
        {
            // A.Save, if A.B is a loaded item but insert into A this time, insert A_B
            var a = DbEntry.GetObject <Article>(3);
            var r = DbEntry.GetObject <Reader>(2);

            a.Readers.Add(r);
            DbEntry.Save(a);

            a = DbEntry.GetObject <Article>(3);
            Assert.IsNotNull(a);
            Assert.AreEqual(2, a.Readers.Count);
            Assert.AreEqual("tom", a.Readers[0].Name);
            Assert.AreEqual("jerry", a.Readers[1].Name);
        }
Ejemplo n.º 30
0
        public void TestHasOne13()
        {
            // For readed A, A.Save will save B, if B(B.Value) is null, then don't save B (if there is data B in the database, it will still there)
            var p = DbEntry.GetObject <Person>(1);

            Assert.IsNotNull(p);
            Assert.IsNull(p.PC.Value);
            DbEntry.Save(p);
            var p1 = DbEntry.GetObject <Person>(p.Id);

            Assert.IsNotNull(p1);
            Assert.AreEqual(p.Name, p1.Name);
            Assert.AreEqual(p.Id, p1.Id);
            Assert.AreEqual(null, p1.PC.Value);
        }