Exemplo n.º 1
0
        private void test1()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            //------------Search for value---------------------
            "Search for a value".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            int key = 2;

            WriteLine("\n\nSearching for value of Key {0}\n\n", key);
            var value = db.searchValue <int, string>(key);

            if (value != null)
            {
                value.showElement();
            }
            else
            {
                WriteLine("\nKey not found..!\n\n");
            }
            //------------Search for value---------------------

            //------------Search for children---------------------
            "Search for children:".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            key = 3;
            WriteLine("\n\nSearching for Children of Key {0}\n", key);
            dynamic children = db.searchChildren(key);

            foreach (var child in children)
            {
                Write("{0}, ", child);
            }
            //------------Search for children---------------------
        }
        /// <summary>
        /// Queries the database.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public String QueryDatabase(String input)
        {
            String result    = String.Empty;
            var    dbElement = ParseXml(input);

            switch (dbElement.dbOperation)
            {
            case "Add":
                db.insert(dbElement.key, dbElement);
                result = "Key " + dbElement.key + " inserted successfully!";
                break;

            case "Edit":
                db.replaceKeyInstance(dbElement.key, dbElement);
                result = "Key " + dbElement.key + "'s value modified successfully!";
                break;

            case "Delete":
                if (db.delete(dbElement.key))
                {
                    result = "Key " + dbElement.key + " deleted successfully!";
                }
                else
                {
                    result = "Key " + dbElement.key + " not found!!";
                }
                break;

            case "SearchChild":
                var children = db.searchChildren(dbElement.key);
                result = "Children are " + string.Join(", ", children.ToArray());
                break;

            case "Search":
                var value = db.searchValue(dbElement.key);
                if (value != null)
                {
                    result  = "Key " + dbElement.key + " found!!!!!\n\n";
                    result += value.showEnumerableElement();
                }
                else
                {
                    result = "Key " + dbElement.key + " not found!!";
                }
                break;

            case "Persist":
                db.toXml();
                result = "Database persisted successfully";
                break;

            case "Restore":
                db.restoreDatabase();
                result = "Database successfully restored!!";
                break;

            default: break;
            }
            string dbView = db.showEnumerableDB();

            "\nDB Contents:\n".title();
            result += dbView;
            return(result);
        }