NOODB: Query Update

Last night I was preparing a demo of the major working features of NOODB. I wrote a test to demo the query, and one of the asserts failed.

I picked apart the sql and found the problem. I was originally planning to create a pivot table. As I built it, though, I thought it was better to go with another approach. I thought the other approach worked, but it didn’t hold up with more complex queries. After slapping it around, I ended up back with a pivot table.

One key difference is that before, the query attempted to resolve everything by joining to the meta tables. This resulted in some funky queries due to the hierarchical nature of the meta data. Since those intricacies are already worked out in code, I changed it to take advantage of that.

I offloaded the construction of the pivot table to a new object. The table is wrapped up in a CTE that can be queried by things consuming the CTE, such as the query engine.

In the end, it’s a lot cleaner and simpler, and I’m back to the pivot table. The following test now passes. (Prior to the refactor, the third assert failed.)

        [TestMethod]
        public void Query()
        {
            TestUtility.DeleteTestData();
            NoodbClient client = new NoodbClient(new Server(TestUtility.UnityContainer));
            client.RegisterType<Movie>();

            // have to cheat and use single word director.
            // problem with query parser and spaces.
            client.Save(new Movie { Director = "Lucas", Name = "Star Wars", ReleaseYear = 1977 });
            client.Save(new Movie { Director = "Kasdan", Name = "The Empire Strikes Back", ReleaseYear = 1980 });
            client.Save(new Movie { Director = "Marquand", Name = "Return of the Jedi", ReleaseYear = 1983 });
            client.Save(new Movie { Director = "Lucas", Name = "The Phantom Menace", ReleaseYear = 1999 });
            client.Save(new Movie { Director = "Lucas", Name = "Attack of the Clones", ReleaseYear = 2002 });
            client.Save(new Movie { Director = "Lucas", Name = "Revenge of the Sith", ReleaseYear = 2005 });
            client.Save(new Movie { Director = "Whedon", Name = "Serenity", ReleaseYear = 2005 });
            client.Save(new Movie { Director = "Jackson", Name = "King Kong", ReleaseYear = 2005 });

            Assert.AreEqual(4, client.Query<Movie>("from m in Movie where m.Director='Lucas'").Count);
            Assert.AreEqual(3, client.Query<Movie>("from m in Movie where m.ReleaseYear = 2005").Count);
            Assert.AreEqual(3, client.Query<Movie>("from m in movie where (m.ReleaseYear = 2005 && m.Director='Lucas') || m.ReleaseYear = 1999 || m.ReleaseYear = 2002").Count);
        }
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: