Esempio n. 1
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            siaqodb.DropType <MyClassWithLists>();
            for (int i = 0; i < 10; i++)
            {
                MyClassWithLists ps = new MyClassWithLists();
                ps.ID     = i;
                ps.myList = new List <int>();
                if (i % 2 == 0)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        ps.myList.Add(j * i);
                    }
                }
                ps.blob = new byte[] { 1, 2, 3 };

                siaqodb.StoreObject(ps);
            }

            //this query will run optimazed, only instances that satify condition are created by engine
            var q = from MyClassWithLists myListObj in siaqodb
                    where myListObj.myList.Contains(2)
                    select myListObj;

            Log("Following objects contains in myList prop element = 2:");

            foreach (MyClassWithLists mcl in q)
            {
                //do somthing with obj
            }
        }
Esempio n. 2
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();
            //most of the times you to have be able to dynamic filter depending what user choose in UI
            //Example: once is needed filter by FirstName and another time by FirstName AND Age
            //we will pick random cases
            int val = DateTime.Now.Second % 2;

            //declare predicate
            Expression <Func <Employee, bool> > predicate = null;

            if (val == 1)
            {
                //build a predicate to filter only by FirstName
                predicate = employee => employee.FirstName.Contains("Emp");

                Log("Predicate with only FirstName is activated");
            }
            else if (val == 0)
            {
                //build a predicate to filter by FirstName and Age
                predicate = employee => employee.FirstName.Contains("Emp") && employee.Age > 20;

                Log("Predicate with FirstName and Age is activated");
            }

            var q = siaqodb.Cast <Employee>().Where(predicate).Select(e => e);

            foreach (Employee emp in q)
            {
                //do something with employee
            }
        }
Esempio n. 3
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            siaqodb.DropType <ClassWithIndex>();//all objects of this Type will be deleted from database

            for (int i = 0; i < 10000; i++)
            {
                ClassWithIndex myobj = new ClassWithIndex()
                {
                    MyID = i % 10, Name = "MyTest" + i.ToString()
                };

                siaqodb.StoreObject(myobj);
            }
            DateTime start = DateTime.Now;
            var      q     = from ClassWithIndex myobj in siaqodb
                             where myobj.MyID == 8//index will be used so very fast retrieve from DB
                             select myobj;
            int k = 0;

            foreach (ClassWithIndex obj in q)
            {
                //do something with object
                k++;
            }
            string timeElapsed = (DateTime.Now - start).ToString();

            Log("Time elapsed to load:" + k.ToString() + " objects from 10.000 stored objects filtered by index:" + timeElapsed);
        }
Esempio n. 4
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            Insert(siaqodb);

            Load(siaqodb);
        }
Esempio n. 5
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            siaqodb.DropType <Employee>();
            siaqodb.DropType <Company>();

            Insert(siaqodb);

            Load(siaqodb);
        }
        //siaqodb support as members of a storable class following types:
        //**********************************************************************
        //int,uint,short,string,ushort,byte,sbyte,long,ulong,float,double,decimal,char,
        //bool,TimeSpan,DateTime,Guid, enum
        //************************************************************************

        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            //clear all objects of Type BigClass created by previous run of app
            siaqodb.DropType <BigClass>();

            siaqodb.StoreObject(new BigClass());

            IObjectList <BigClass> list = siaqodb.LoadAll <BigClass>();
        }
Esempio n. 7
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            //clear all objects of Type Company created by previous run of this example
            siaqodb.DropType <Company>();

            Insert(siaqodb);

            LoadAndUpdate(siaqodb);

            Delete(siaqodb);
        }
Esempio n. 8
0
        public void FirstOrDefault()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();
            //this query run optimized-> only first Developer object will be created and read from database that match the condition
            Developer first = (from Developer emp in siaqodb
                               where emp.Age > 20
                               select emp).FirstOrDefault();

            //same query written in other way using lambda expression arguments
            Developer first2 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20).FirstOrDefault();

            // or similar-> also run optimized:
            Developer first3 = siaqodb.Cast <Developer>().FirstOrDefault(emp => emp.Age > 20);
        }
Esempio n. 9
0
        public void Any()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();
            //check if exists at least one object that match the condition
            //run optimized-> create only first fetched object if exists
            bool exists = (from Developer e in siaqodb
                           where e.Age > 20
                           select e).Any();

            //same query written in other way using lambda expression arguments
            bool exists2 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20).Any();

            //similar query->same result -> also optimized
            bool exists3 = siaqodb.Cast <Developer>().Any(emp => emp.Age > 20);
        }
Esempio n. 10
0
        public void Where_CrashUsingLocalMethod()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            try{
                //this query will crash, problem is TestMet method that get a member of a storable object
                //as argument

                var query = from Employee e in siaqodb
                            where TestMet(e.Age) == 30
                            select e;

                foreach (Employee e in query)
                {
                    //do something with e
                }
            }
            catch (Exception ex)
            {
                Log("Example with local method crash as expected:" + ex.Message);
            }
            try{
                //query bellow will crash also
                int myInt  = 30;
                var query2 = from Employee e in siaqodb
                             where e.Age == TestMet(myInt)
                             select e;
                foreach (Employee e in query2)
                {
                    //do something with e
                }
            }
            catch (Exception ex)
            {
                Log("Example with local method crash as expected:" + ex.Message);
            }
            //but as workaround for above query use this:
            int myInt2         = TestMet(30);
            var queryOptimized = from Employee e in siaqodb
                                 where e.Age == myInt2
                                 select e;

            foreach (Employee e in queryOptimized)
            {
                //do something with e
            }
            Log("Example with local method workaround works fine!");
        }
Esempio n. 11
0
        public void PaginationWith_Skip_Take()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            //if you need pagination in your application you can use Skip/Take methods
            //in optimized way to pull only needed objects from DB
            //bellow query only pull and create 10 objects
            var query = (from Developer emp in siaqodb
                         where emp.Age > 20
                         select emp).Skip(10).Take(10);

            foreach (var c in query)
            {
                //do something with c object
            }
        }
Esempio n. 12
0
        public void Run()
        {
            //it is possible that you have and object at runtime that came from server side or similar but is not loaded from Siaqodb database
            //and you need to update or delete it only, without load, in this case UpdateObjectBy/DeleteObjectBy method can be used like:

            //prepare some data
            Guid    IdKept  = Guid.Empty;
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            for (int i = 0; i < 10; i++)
            {
                RecordWithManualID rec = new RecordWithManualID {
                    ID = Guid.NewGuid(), Name = "Name" + i.ToString()
                };

                siaqodb.StoreObject(rec);
                if (i == 5)
                {
                    IdKept = rec.ID;
                }
            }


            RecordWithManualID recNew = new RecordWithManualID();

            recNew.Name = "Updated Name";
            recNew.ID   = IdKept;

            //update object by ID(similar in relational databases with "UPDATE ....WHERE ID=yourID"
            siaqodb.UpdateObjectBy("_id", recNew);
            //or use overloaded method:
            //siaqodb.UpdateObjectBy(recNew, new string[] { "_id" });

            var q = from RecordWithManualID r in siaqodb
                    where r.ID == IdKept
                    select r;

            foreach (RecordWithManualID recc in q)
            {
                Log("Record with ID:" + recc.ID.ToString() + " was updated with UpdateObjectBy method and now it has Name=" + recc.Name);
            }

            //DeleteObjectBy works similar(relational DB equivalent: DELETE FROM ... WHERE Id=yourId):

            siaqodb.DeleteObjectBy(recNew, "_id");
            Log("Record with ID:" + recNew.ID.ToString() + " was deleted with DeleteObjectBy from database");
        }
Esempio n. 13
0
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            siaqodb.DropType <POCOSimple>();

            //define an Index in POCO way (not by an attribute)
            SiaqodbConfigurator.AddIndex("ID", typeof(POCOSimple));

            Guid id1 = Guid.NewGuid();
            Guid id2 = Guid.NewGuid();

            for (int i = 0; i < 1000; i++)
            {
                POCOSimple ps = new POCOSimple();

                if (i % 2 == 0)
                {
                    ps.ID = id1;
                }
                else
                {
                    ps.ID = id2;
                }

                siaqodb.StoreObject(ps);
            }

            DateTime start = DateTime.Now;

            var q = from POCOSimple ps in siaqodb
                    where ps.ID == id1
                    select ps;
            int k = 0;

            foreach (POCOSimple pocoSimple in q)
            {
                //do something with object
                k++;
            }

            string timeElapsed = (DateTime.Now - start).ToString();

            Log("Time elapsed to get:" + k.ToString() + " objects by index filtered, is:" + timeElapsed);
        }
Esempio n. 14
0
        public void Count()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();
            // to count all objects stored in db of a certain Type:
            int nrObjects = siaqodb.Count <Developer>();

            //count all objects that match a condition
            //-> will run optimized, so NO object is created, the engine only counts objects stored
            int nrObjects2 = (from Developer e in siaqodb
                              where e.Age > 20
                              select e).Count();

            //same query written in other way using lambda expression arguments
            int nrObjects3 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20).Count();

            //similar query->same result -> also optimized
            int nrObjects4 = siaqodb.Cast <Developer>().Count(emp => emp.Age > 20);
        }
Esempio n. 15
0
        public void Where()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();
            //this query run optimized-> only Developers objects will be created that match the condition
            //it does not matter how complex your WHERE is, it will run OPTIMIZED except some cases: see LINQ_UnOptimizedExamples
            DateTime dt    = new DateTime(2008, 1, 1);
            var      query = from Developer emp in siaqodb
                             where emp.Age > 20 && emp.HireDate < dt
                             select emp;

            //same query written in other way using lambda expression arguments
            var q2 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20 && emp.HireDate < dt);

            foreach (Developer c in query)
            {
                //do something with c object
            }
        }
Esempio n. 16
0
        public void SelectOnlySomeProperties()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            //the query will NOT create any Company objects,
            //it will read ONLY that 2 properties values and anonymous Type will be created with that values
            //so query is optimal
            var query = from Company c in siaqodb
                        select new { Name = c.Name, Phone = c.Phone };

            //same query written using lambda expression arguments
            var q2 = siaqodb.Cast <Company>().Select(c => new { Name = c.Name, Phone = c.Phone });

            foreach (var c in query)
            {
                //do something with c object
            }
        }
        public void Run()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            siaqodb.DropType <AttUsage>();

            AttUsage at = new AttUsage();

            at.anotherInt = 100;
            at.SetInt(20);
            at.SetString("very long string");
            at.UniqueField = 10;
            siaqodb.StoreObject(at);

            IObjectList <AttUsage> list = siaqodb.LoadAll <AttUsage>();

            foreach (AttUsage a in list)
            {
                if (a.GetInt() == 0)
                {
                    Log("Memeber was ignored by siaqodb!");
                }
            }

            AttUsage at1 = new AttUsage();

            at1.anotherInt = 100;
            at1.SetInt(20);
            at1.SetString("very long string");
            //try to violate constraint
            at1.UniqueField = 10;

            try
            {
                siaqodb.StoreObject(at1);
            }
            catch (UniqueConstraintException ex)
            {
                Log("A unique contraint attempt to be violated!");
            }
        }
Esempio n. 18
0
        public void Select()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            for (int i = 0; i < 30; i++)
            {
                Developer deve = new Developer()
                {
                    Age = 18 + i, FirstName = "Devo" + i.ToString(), LastName = "None"
                };
                siaqodb.StoreObject(deve);
            }
            var query = from Company c in siaqodb
                        select c;
            //same query written using lambda expression arguments
            var q2 = siaqodb.Cast <Company>().Select(c => c);

            foreach (Company c in query)
            {
                //do something with c object
            }
        }
Esempio n. 19
0
        public void Last()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            try
            {
                //this query run optimized-> only last Developer object will be created and read from database that match the condition,otherwise InvalidOperationException is thrown
                Developer last = (from Developer emp in siaqodb
                                  where emp.Age > 20
                                  select emp).Last();

                //same query written in other way using lambda expression arguments
                Developer last2 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20).Last();

                // or similar-> also run optimized:
                Developer last3 = siaqodb.Cast <Developer>().Last(emp => emp.Age > 20);
            }
            catch (InvalidOperationException ex)
            {
                Log(ex.Message);
            }
        }
Esempio n. 20
0
        public void Where_CrashUsingUnaryOperator()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            for (int i = 0; i < 10; i++)
            {
                BoolAndIntExample e = new BoolAndIntExample()
                {
                    SomeBool = i % 2 == 0, SomeInt = i
                };
                siaqodb.StoreObject(e);
            }

            try{
                //this query crash, problem is !(Not) operator
                var query = from BoolAndIntExample e in siaqodb
                            where e.SomeInt > 5 && !e.SomeBool
                            select e;
                foreach (BoolAndIntExample e in query)
                {
                    //do something with e
                }
            }
            catch (Exception ex)
            {
                Log("Example with unary operator crash as expected:" + ex.Message);
            }

            //to optimize query above, just use Equal operator:
            var queryOptimized = from BoolAndIntExample e in siaqodb
                                 where e.SomeInt > 5 && e.SomeBool == false
                                 select e;

            foreach (BoolAndIntExample e in queryOptimized)
            {
                //do something with e
            }
        }
Esempio n. 21
0
        public void SingleOrDefault()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            try
            {
                //this query run optimized-> only one Developer object will be created and read from database IF EXISTS and IF is the only one in database that match the condition,
                //if not exists return null, if there are MANY InvalidOperationException is thrown
                Developer single = (from Developer emp in siaqodb
                                    where emp.Age > 20
                                    select emp).SingleOrDefault();

                //same query written in other way using lambda expression arguments
                Developer single2 = siaqodb.Cast <Developer>().Where(emp => emp.Age > 20).SingleOrDefault();

                // or similar-> also run optimized:
                Developer single3 = siaqodb.Cast <Developer>().SingleOrDefault(emp => emp.Age > 20);
            }
            catch (InvalidOperationException ex)
            {
                Log(ex.Message);
            }
        }
Esempio n. 22
0
        public void Where_CrashUsingStringMethods()
        {
            Siaqodb siaqodb = SiaqodbFactoryExample.GetInstance();

            //this query will crash, problem is Length property of String
            //siaqodb can only handle optimized following String methods:StartsWith,EndsWith and Contains
            //all other String methods/properties for object members used in LINQ statements will crash

            try{
                var query = from Company c in siaqodb
                            where c.Phone.Length == 3
                            select c;

                foreach (Company c in query)
                {
                    //do something with c
                }
            }
            catch (Exception ex)
            {
                Log("Example crash as expected:" + ex.Message);
            }
            // !!! Important: if those String methods are used for a local variable and NOT to a member of a stored object
            //query run optimized so:
            string d = "test";
            var    queryOptimized = from Company c in siaqodb
                                    where c.Name == d.Substring(2)
                                    select c;

            //the above query runs well
            foreach (Company c in queryOptimized)
            {
                //do something with c
            }
            Log("String methods of a local variable and NOT of a member of a stored object,works fine!");
        }