2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2009, 2012 Oracle and/or its affiliates. All rights reserved.
8 using System.Collections.Generic;
9 using System.Collections;
10 using System.Diagnostics;
13 using System.Runtime.Serialization.Formatters.Binary;
16 namespace excs_getting_started {
17 public class DatabaseReader {
18 private MyDbs myDbs = null;
20 public DatabaseReader(MyDbs dbs) {
24 public void showItem(string locateItem) {
25 SecondaryCursor secCursor;
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);
31 /* Open a secondary cursor. */
32 secCursor = this.myDbs.ItemSecbtreeDB.SecondaryCursor();
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.
40 if (secCursor.Move(searchKey, true)) {
41 Inventory theInventory = new Inventory(
42 secCursor.Current.Value.Value.Data);
44 /* Display the record. */
45 displayInventory(theInventory);
47 /* Get each duplicate. */
48 while (secCursor.MoveNextDuplicate()) {
49 theInventory = new Inventory(
50 secCursor.Current.Value.Value.Data);
51 displayInventory(theInventory);
54 Console.WriteLine("Could not find itemname {0} ", locateItem);
60 /* Close the cursor and the duplicate cursor. */
64 public void showAllInventory() {
65 BTreeCursor cursor = myDbs.InventoryDB.Cursor();
66 Inventory theInventory;
68 while (cursor.MoveNext()) {
69 theInventory = new Inventory(cursor.Current.Value.Data);
70 displayInventory(theInventory);
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);
83 /* Display the associated Vendor information. */
84 displayVendor(theInventory);
87 private void displayVendor(Inventory theInventory) {
88 BinaryFormatter formatter = new BinaryFormatter();
89 DatabaseEntry foundVendor = new DatabaseEntry();
90 MemoryStream memStream;
93 foundVendor.Data = System.Text.Encoding.ASCII.GetBytes(
96 KeyValuePair<DatabaseEntry, DatabaseEntry> pair =
97 new KeyValuePair<DatabaseEntry, DatabaseEntry>();
100 pair = this.myDbs.VendorDB.Get(foundVendor);
101 vendorData = System.Text.ASCIIEncoding.ASCII.GetString(
104 memStream = new MemoryStream(pair.Value.Data.Length);
105 memStream.Write(pair.Value.Data, 0, pair.Value.Data.Length);
107 /* Read from the begining of the stream. */
108 memStream.Seek(0, SeekOrigin.Begin);
109 theVendor = (Vendor)formatter.Deserialize(memStream);
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();
125 catch (Exception e) {
126 Console.WriteLine("displayVendor Error.");
127 Console.WriteLine(e.Message);