コード例 #1
0
ファイル: Tutorial3.cs プロジェクト: WooZoo86/ctree
        //
        // Update_CustomerMaster_Records()
        //
        // Update one record under locking control to demonstrate the effects
        // of locking
        //

        static void Update_CustomerMaster_Record()
        {
            Console.WriteLine("\tUpdate Record...");

            try
            {
                // enable session-wide lock flag
                MySession.Lock(LOCK_MODE.WRITE_BLOCK_LOCK);

                MyRecord.Clear();
                MyRecord.SetFieldAsString(0, "1003");
                // find record by customer number
                if (MyRecord.Find(FIND_MODE.EQ))
                {
                    MyRecord.SetFieldAsString(4, "KEYON DOOLING");
                    // rewrite record
                    MyRecord.Write();

                    Console.WriteLine("\tPress <ENTER> key to unlock");
                    Console.ReadLine();
                }

                // reset session-wide locks
                MySession.Unlock();
            }
            catch (CTException E)
            {
                Handle_Exception(E);
            }
        }
コード例 #2
0
        static void Add_Transactions()
        {
            ORDER_DATA[] orders = new ORDER_DATA[3];
            orders[0] = new ORDER_DATA("09/01/2002", "09/05/2002", "1", "1001");
            orders[1] = new ORDER_DATA("09/02/2002", "09/06/2002", "2", "9999"); // bad customer number
            orders[2] = new ORDER_DATA("09/22/2002", "09/26/2002", "3", "1003");
            int nOrders = orders.Length;

            ORDERITEM_DATA[] items = new ORDERITEM_DATA[6];
            items[0] = new ORDERITEM_DATA("1", 1, 2, "1");
            items[1] = new ORDERITEM_DATA("1", 2, 1, "2");
            items[2] = new ORDERITEM_DATA("2", 1, 1, "3");
            items[3] = new ORDERITEM_DATA("2", 2, 3, "4");
            items[4] = new ORDERITEM_DATA("3", 1, 2, "3");
            items[5] = new ORDERITEM_DATA("3", 2, 2, "99"); // bad item number
            int nItems = items.Length;

            CTDate orderdate = new CTDate();
            CTDate promdate  = new CTDate();
            int    savepoint;
            int    j = 0;

            Delete_Records(recordCustOrdr);
            Delete_Records(recordOrdrItem);

            Console.WriteLine("\tAdd transaction records...");

            // process orders
            for (int i = 0; i < nOrders; i++)
            {
                // start a transaction
                MySession.Begin();

                try
                {
                    recordCustOrdr.Clear();

                    // populate record buffer with order data
                    orderdate.StringToDate(orders[i].orderdate, DATE_TYPE.MDCY_DATE);
                    promdate.StringToDate(orders[i].promdate, DATE_TYPE.MDCY_DATE);
                    recordCustOrdr.SetFieldValue(0, orderdate);
                    recordCustOrdr.SetFieldValue(1, promdate);
                    recordCustOrdr.SetFieldAsString(2, orders[i].ordernum);
                    recordCustOrdr.SetFieldAsString(3, orders[i].custnum);

                    // add order record
                    recordCustOrdr.Write();
                }
                catch (CTException E)
                {
                    // abort transaction on error
                    Handle_Exception(E);
                }

                // set transaction savepoint
                savepoint = recordCustOrdr.SetSavePoint();

                // process order items
                while (items[j].ordernum == orders[i].ordernum)
                {
                    try
                    {
                        recordOrdrItem.Clear();

                        // populate record buffer with order item data
                        recordOrdrItem.SetFieldValue(0, items[j].seqnumber);
                        recordOrdrItem.SetFieldValue(1, items[j].quantity);
                        recordOrdrItem.SetFieldAsString(2, items[j].ordernum);
                        recordOrdrItem.SetFieldAsString(3, items[j].itemnum);

                        // add order item record
                        recordOrdrItem.Write();

                        // check that item exists in ItemMaster table
                        recordItemMast.Clear();
                        recordItemMast.SetFieldAsString(2, items[j].itemnum);
                        if (!recordItemMast.Find(FIND_MODE.EQ))
                        {
                            // if not found, restore back to previous savepoint
                            recordItemMast.RestoreSavePoint(savepoint);
                        }
                        else
                        {
                            // set transaction savepoint
                            savepoint = recordItemMast.SetSavePoint();
                        }
                    }
                    catch (CTException E)
                    {
                        // abort transaction on error
                        Handle_Exception(E);
                    }

                    // bump to next item
                    j++;

                    // exit the while loop on last item
                    if (j >= nItems)
                    {
                        break;
                    }
                }

                // check that customer exists in CustomerMaster table
                recordCustMast.Clear();
                recordCustMast.SetFieldAsString(0, orders[i].custnum);

                // commit or abort the transaction
                if (!recordCustMast.Find(FIND_MODE.EQ))
                {
                    MySession.Abort();
                }
                else
                {
                    MySession.Commit();
                }
            }
        }
コード例 #3
0
        //
        // Manage()
        //
        // Populates table and perform a simple query
        //

        static void Manage()
        {
            int    quantity;
            double price, total;
            string itemnumb, custnumb, ordrnumb, custname;
            bool   isOrderFound, isItemFound;

            Console.WriteLine("MANAGE");

            // populate the tables with data
            Add_CustomerMaster_Records();
            Add_CustomerOrders_Records();
            Add_OrderItems_Records();
            Add_ItemMaster_Records();

            // perform a query:
            // list customer name and total amount per order

            // name               total
            // @@@@@@@@@@@@@      $xx.xx

            // for each order in the CustomerOrders table
            //    fetch order number
            //    fetch customer number
            //    fetch name from CustomerMaster table based on customer number
            //    for each order item in OrderItems table
            //       fetch item quantity
            //       fetch item number
            //       fetch item price from ItemMaster table based on item number
            //    next
            // next

            Console.WriteLine("\n\tQuery Results");

            try
            {
                // get the first order
                isOrderFound = recordCustOrdr.First();

                while (isOrderFound) // for each order in the CustomerOrders table
                {
                    // fetch order number
                    ordrnumb = recordCustOrdr.GetFieldAsString(2);
                    // fetch customer number
                    custnumb = recordCustOrdr.GetFieldAsString(3);

                    // fetch name from CustomerMaster table based on customer number
                    recordCustMast.Clear();
                    recordCustMast.SetFieldAsString(0, custnumb);
                    if (!recordCustMast.Find(FIND_MODE.EQ))
                    {
                        continue; // not possible in our canned example
                    }
                    custname = recordCustMast.GetFieldAsString(4);

                    // fetch item price from OrderItems table
                    recordOrdrItem.Clear();
                    recordOrdrItem.SetFieldAsString(2, ordrnumb);
                    // define a recordset to scan only items applicable to this order
                    recordOrdrItem.RecordSetOn(6);
                    isItemFound = recordOrdrItem.First();

                    total = 0;
                    while (isItemFound) // for each order item in OrderItems table
                    {
                        // fetch item quantity
                        recordOrdrItem.GetFieldValue(1, out quantity);
                        // fetch item number
                        itemnumb = recordOrdrItem.GetFieldAsString(3);

                        // fetch item price from ItemMaster table based on item number
                        recordItemMast.Clear();
                        recordItemMast.SetFieldAsString(2, itemnumb);
                        recordItemMast.Find(FIND_MODE.EQ);
                        recordItemMast.GetFieldValue(1, out price);

                        // calculate order total
                        total += (price * quantity);

                        isItemFound = recordOrdrItem.Next();
                    }

                    recordOrdrItem.RecordSetOff();

                    // output data to stdout
                    Console.WriteLine("\t\t{0,-20}{1,-8}", custname, total);

                    // read next order
                    if (!recordCustOrdr.Next())
                    {
                        isOrderFound = false;
                    }
                }
            }
            catch (CTException E)
            {
                Handle_Exception(E);
            }
        }