예제 #1
0
        //<------------performs delete operation on DB and generates response msg accordingly.
        XElement delete_elem <Key, Data>(XElement request_msg, DBEngine <Key, DBElement <Key, Data> > db)
        {
            Console.Write("\n  Request to Delete DB Element");
            XElement response_msg = new XElement("Query_Response");
            XElement type         = new XElement("Query_Type", "Delete");
            XElement result       = new XElement("Result", "Could not delete element");

            try
            {
                Key k = (Key)Convert.ChangeType(request_msg.Element("Key").Value, typeof(Key));
                Console.WriteLine(" with key - {0}", k);
                if (db.remove(k))
                {
                    result = new XElement("Result", "DB Element deleted successfully from database.");
                }
                else
                {
                    result = new XElement("Result", "Could not delete element from database");
                }
                response_msg.Add(type);
                response_msg.Add(result);
                return(response_msg);
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine("\n  XML file for request message is not in the expected format. A particular tag could not be found.\n  Exception = {0}", ex);
                response_msg.Add(type);
                response_msg.Add(result);
                return(response_msg);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            "Testing DBExtensions Package".title('=');
            WriteLine();

            DBElement <int, string> elem1 = new DBElement <int, string>("Element-7", "Description of Element-7");

            elem1.payload = "Payload of element-7.";
            elem1.children.AddRange(new List <int> {
                8, 9
            });
            DBElement <int, string> elem2 = new DBElement <int, string>("Element-8", "Description of Element-8");
            //elem2.payload = "Payload of element-8.";
            DBElement <int, string> elem3 = new DBElement <int, string>("Element-9", "Description of Element-9");

            elem3.payload = "Payload of element-3.";

            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            db.insert(7, elem1);
            db.insert(8, elem2);
            db.insert(9, elem3);

            Write("  Created a new dataBase  with following contents:");
            db.showDB();
            WriteLine("\n\n  Now going to persist the database contents to an XML file.");
            PersistEngine pe        = new PersistEngine();
            string        file_name = pe.persistDB <int, DBElement <int, string>, string>(db);

            WriteLine("  Database contents are saved as - {0}", file_name);
            WriteLine("\n  Going to remove all DB elements and load the xml file saved above.");
            db.remove(7);
            db.remove(8);
            db.remove(9);
            WriteLine("  DB contents before calling load-DB");
            db.showDB();
            WriteLine("  Now send command to load database from the XML file.");
            pe.loadDB <int, DBElement <int, string>, string>(db, file_name);
            Write("  DB contents after calling load-DB ");
            db.showDB();

            DBEngine <string, DBElement <string, List <string> > > db2 = new DBEngine <string, DBElement <string, List <string> > >();

            Write("\n\n --- DBElement<string,List<string>> ---");
            DBElement <string, List <string> > elem_str = new DBElement <string, List <string> >();

            elem_str.name      = "Element-One";
            elem_str.descr     = "DB Element whose key type is string and payload type is List of strings.";
            elem_str.timeStamp = DateTime.Now;
            elem_str.children  = new List <string> {
                "Two", "Three", "Four"
            };
            elem_str.payload = new List <string> {
                "Element payload is of type List of strings.", "This is string two.", "And third"
            };
            elem_str.showEnumerableElement();

            db2.insert("One", elem_str);
            Write("\n\n --- DBEngine<string,List<string>> ---");
            db2.showEnumerableDB();

            XElement noSqlDb     = new XElement("NoSqlDb");
            XElement keyType     = new XElement("KeyType", typeof(int));
            XElement payloadType = new XElement("PayloadType", typeof(string));

            XElement request_msg = new XElement("Request_Message");
            XElement req_type    = new XElement("Request_Type", "Insert");
            XElement key         = new XElement("Key", 45);
            XElement name        = new XElement("Name", "element_one");
            XElement descr       = new XElement("Description", "Descr of element_one");
            XElement payload     = new XElement("Payload", "This the payload of element one.");

            request_msg.Add(req_type);
            request_msg.Add(key);
            request_msg.Add(name);
            request_msg.Add(descr);
            request_msg.Add(payload);

            Console.WriteLine("Message = \n{0}", request_msg.ToString());

            XElement Rmsg  = XElement.Parse(request_msg.ToString());
            string   Rtype = Rmsg.Element("Request_Type").Value;

            Console.WriteLine("Request type = {0}", Rtype);

            XElement abc    = new XElement("dummy");
            XElement result = new XElement("Result", "Success");

            //Console.WriteLine("abc= {0}\nresult = {1}\n", abc.ToString(), result.ToString());
            //abc = result;
            Console.WriteLine("abc= {0}\nresult = {1}", abc.ToString(), result.ToString());

            int    k  = 5;
            string ts = "Could not find element with key " + k.ToString() + " in database";
            //XElement tst = new XElement("Result", "Could not find element with key {0} in  database", k);
            XElement tst = new XElement("Result", ts);

            Console.WriteLine("tst = {0}", tst.ToString());

            XElement qry = new XElement("Query_Response");
            XElement par = new XElement("Partial", "This is \njust payload");

            qry.Add(tst);
            qry.Add(par);
            Console.WriteLine("qry = \n{0}", qry.ToString());
            Console.WriteLine("value of qry response =\n{0}", qry.Value.ToString());
            //noSqlDb.Add(keyType);
            //noSqlDb.Add(payloadType);
            //Console.WriteLine("\nString mesage = \n{0}", noSqlDb.ToString());
            //string xml_string = noSqlDb.ToString();

            //XElement abc = XElement.Parse(xml_string);
            //XElement root = abc.Element("KeyType");

            //Console.WriteLine("abc= {0}", abc.ToString());
            //Console.WriteLine("root= {0}", root.ToString());

            //elem.name = xml_dbElement.Element("Name").Value;
            //string par = root.Value;
            //Console.WriteLine("par = {0}", par);
        }