Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / examples / csharp / excs_getting_started / DatabaseReader.cs
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2009, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 using System;
8 using System.Collections.Generic;
9 using System.Collections;
10 using System.Diagnostics;
11 using System.IO;
12 using System.Text;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using BerkeleyDB; 
15
16 namespace excs_getting_started {
17     public class DatabaseReader {
18         private MyDbs myDbs = null;
19
20         public DatabaseReader(MyDbs dbs) {
21             myDbs = dbs;
22         }
23
24         public void showItem(string locateItem) {
25             SecondaryCursor secCursor;
26
27             /* searchKey is the key to find in the secondary db. */
28             DatabaseEntry searchKey = new DatabaseEntry();
29             searchKey.Data = System.Text.Encoding.ASCII.GetBytes(locateItem);
30
31             /* Open a secondary cursor. */
32             secCursor = this.myDbs.ItemSecbtreeDB.SecondaryCursor();
33
34             /* 
35              * Search for the secondary. The primary key/data pair 
36              * associated with the given secondary key is stored in Current.
37              * In the presence of duplicate key values, the first data
38              * item in the set of duplicates is stored in Current.
39              */
40             if (secCursor.Move(searchKey, true)) {
41                 Inventory theInventory = new Inventory(
42                     secCursor.Current.Value.Value.Data);
43
44                 /* Display the record. */
45                 displayInventory(theInventory);
46
47                 /* Get each duplicate. */
48                 while (secCursor.MoveNextDuplicate()) {
49                     theInventory = new Inventory(
50                         secCursor.Current.Value.Value.Data);
51                     displayInventory(theInventory);
52                 }
53             } else { 
54                 Console.WriteLine("Could not find itemname {0} ", locateItem);
55                 Console.WriteLine();
56                 Console.WriteLine();
57                 return;
58             }
59
60             /* Close the cursor and the duplicate cursor. */
61             secCursor.Close();
62         }
63
64         public void showAllInventory() {
65             BTreeCursor cursor = myDbs.InventoryDB.Cursor();
66             Inventory theInventory;
67
68             while (cursor.MoveNext()) {
69                 theInventory = new Inventory(cursor.Current.Value.Data);
70                 displayInventory(theInventory);
71             }
72             cursor.Close();
73         }
74
75         public void displayInventory(Inventory theInventory) {
76             Console.WriteLine("Item: {0} ",theInventory.Itemname);
77             Console.WriteLine("SKU: {0} ", theInventory.Sku);
78             Console.WriteLine("Price per unit: {0} ", theInventory.Price);
79             Console.WriteLine("Quantity: {0} ", theInventory.Quantity);
80             Console.WriteLine("Category: {0} ", theInventory.Category);
81             Console.WriteLine("Contact: {0} ", theInventory.Vendor);
82
83             /* Display the associated Vendor information. */
84             displayVendor(theInventory);
85         }
86
87         private void displayVendor(Inventory theInventory) {
88             BinaryFormatter formatter = new BinaryFormatter();
89             DatabaseEntry foundVendor = new DatabaseEntry();
90             MemoryStream memStream;
91             Vendor theVendor;
92
93             foundVendor.Data = System.Text.Encoding.ASCII.GetBytes(
94                 theInventory.Vendor);
95             try {
96                 KeyValuePair<DatabaseEntry, DatabaseEntry> pair = 
97                     new KeyValuePair<DatabaseEntry, DatabaseEntry>();
98                 string vendorData;
99
100                 pair = this.myDbs.VendorDB.Get(foundVendor);
101                 vendorData = System.Text.ASCIIEncoding.ASCII.GetString(
102                     pair.Value.Data);
103
104                 memStream = new MemoryStream(pair.Value.Data.Length);
105                 memStream.Write(pair.Value.Data, 0, pair.Value.Data.Length);
106
107                 /* Read from the begining of the stream. */
108                 memStream.Seek(0, SeekOrigin.Begin);
109                 theVendor = (Vendor)formatter.Deserialize(memStream);
110
111                 memStream.Close();
112
113                 System.Console.WriteLine("Vendor: {0}", theVendor.Name);
114                 System.Console.WriteLine("Vendor Phone: {0}", 
115                     theVendor.PhoneNumber);
116                 System.Console.Write("Vendor Address: {0}, ",theVendor.Street); 
117                 System.Console.Write("{0} ",theVendor.City); 
118                 System.Console.Write("{0} ",theVendor.State); 
119                 System.Console.WriteLine("{0} ",theVendor.Zipcode); 
120                 System.Console.WriteLine("Vendor Rep: {0}: {1}", 
121                     theVendor.SalesRep, theVendor.SalesRepPhone);
122                 System.Console.WriteLine();
123                 System.Console.WriteLine();
124             }
125             catch (Exception e) {
126                 Console.WriteLine("displayVendor Error.");
127                 Console.WriteLine(e.Message);
128                 throw e;
129             }
130         }
131     }
132 }