Debian Codegraph @

examples/cxx/getting_started/excxx_example_database_read.cpp

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8
9 // File: excxx_example_database_read.cpp
10
11 #include <iostream>
12 #include <fstream>
13 #include <cstdlib>
14
15 #include "MyDb.hpp"
16 #include "gettingStartedCommon.hpp"
17
18 #ifdef _WIN32
19 extern "C" {
20 extern int getopt(int, char * const *, const char *);
21 extern char *optarg;
22 }
23 #else
24 #include <unistd.h>
25 #endif
26
27 // Forward declarations
28 int show_item(MyDb &itemnameSDB, MyDb &vendorDB, std::string &itemName);
29 int show_all_records(MyDb &inventoryDB, MyDb &vendorDB);
30 int show_vendor(MyDb &vendorDB, const char *vendor);
31
32 int
33 usage()
34 {
35 std::cout << "example_database_read [-i <path to data files>]"
36 << " [-h <database home directory>]" << std::endl;
37
38 std::cout << "Note: Any path specified to the -h parameter must end"
39 << " with your system's path delimiter (/ or \\)"
40 << std::endl;
41 return (-1);
42 }
43
44 int
45 main (int argc, char *argv[])
46 {
47
48 int ch;
49 char lastChar;
50
51 // Initialize the path to the database files
52 std::string databaseHome("./");
53 std::string itemName;
54
55 // Database names
56 std::string vDbName("vendordb.db");
57 std::string iDbName("inventorydb.db");
58 std::string itemSDbName("itemname.sdb");
59
60 // Parse the command line arguments
61 while ((ch = getopt(argc, argv, "h:i:")) != EOF)
62 switch (ch) {
63 case 'h':
64 databaseHome = optarg;
65 lastChar = databaseHome[databaseHome.size() -1];
66 if (lastChar != '/' && lastChar != '\\')
67 return (usage());
68 break;
69 case 'i':
70 itemName = optarg;
71 break;
72 case '?':
73 default:
74 return (usage());
75 break;
76 }
77
78 try
79 {
80 // Open all databases.
81 MyDb inventoryDB(databaseHome, iDbName);
82 MyDb vendorDB(databaseHome, vDbName);
83 MyDb itemnameSDB(databaseHome, itemSDbName, true);
84
85 // Associate the secondary to the primary
86 inventoryDB.getDb().associate(NULL,
87 &(itemnameSDB.getDb()),
88 get_item_name,
89 0);
90
91 if (itemName.empty())
92 {
93 show_all_records(inventoryDB, vendorDB);
94 } else {
95 show_item(itemnameSDB, vendorDB, itemName);
96 }
97 } catch(DbException &e) {
98 std::cerr << "Error reading databases. " << std::endl;
99 return (e.get_errno());
100 } catch(std::exception &e) {
101 std::cerr << "Error reading databases. " << std::endl;
102 std::cerr << e.what() << std::endl;
103 return (-1);
104 }
105
106 return (0);
107 } // End main
108
109 // Shows the records in the inventory database that
110 // have a specific item name. For each inventory record
111 // shown, the appropriate vendor record is also displayed.
112 int
113 show_item(MyDb &itemnameSDB, MyDb &vendorDB, std::string &itemName)
114 {
115
116 // Get a cursor to the itemname secondary db
117 Dbc *cursorp;
118
119 try {
120 itemnameSDB.getDb().cursor(NULL, &cursorp, 0);
121
122 // Get the search key. This is the name on the inventory
123 // record that we want to examine.
124 std::cout << "Looking for " << itemName << std::endl;
125 Dbt key((void *)itemName.c_str(), (u_int32_t)itemName.length() + 1);
126 Dbt data;
127
128 // Position the cursor to the first record in the secondary
129 // database that has the appropriate key.
130 int ret = cursorp->get(&key, &data, DB_SET);
131 if (!ret) {
132 do {
133 InventoryData inventoryItem(data.get_data());
134 inventoryItem.show();
135
136 show_vendor(vendorDB, inventoryItem.getVendor().c_str());
137
138 } while (cursorp->get(&key, &data, DB_NEXT_DUP) == 0);
139 } else {
140 std::cerr << "No records found for '" << itemName
141 << "'" << std::endl;
142 }
143 } catch(DbException &e) {
144 itemnameSDB.getDb().err(e.get_errno(), "Error in show_item");
145 cursorp->close();
146 throw e;
147 } catch(std::exception &e) {
148 itemnameSDB.getDb().errx("Error in show_item: %s", e.what());
149 cursorp->close();
150 throw e;
151 }
152
153 cursorp->close();
154 return (0);
155 }
156
157 // Shows all the records in the inventory database.
158 // For each inventory record shown, the appropriate
159 // vendor record is also displayed.
160 int
161 show_all_records(MyDb &inventoryDB, MyDb &vendorDB)
162 {
163
164 // Get a cursor to the inventory db
165 Dbc *cursorp;
166 try {
167 inventoryDB.getDb().cursor(NULL, &cursorp, 0);
168
169 // Iterate over the inventory database, from the first record
170 // to the last, displaying each in turn
171 Dbt key, data;
172 int ret;
173 while ((ret = cursorp->get(&key, &data, DB_NEXT)) == 0 )
174 {
175 InventoryData inventoryItem(data.get_data());
176 inventoryItem.show();
177
178 show_vendor(vendorDB, inventoryItem.getVendor().c_str());
179 }
180 } catch(DbException &e) {
181 inventoryDB.getDb().err(e.get_errno(), "Error in show_all_records");
182 cursorp->close();
183 throw e;
184 } catch(std::exception &e) {
185 cursorp->close();
186 throw e;
187 }
188
189 cursorp->close();
190 return (0);
191 }
192
193 // Shows a vendor record. Each vendor record is an instance of
194 // a vendor structure. See loadVendorDB() in
195 // example_database_load for how this structure was originally
196 // put into the database.
197 int
198 show_vendor(MyDb &vendorDB, const char *vendor)
199 {
200 Dbt data;
201 VENDOR my_vendor;
202
203 try {
204 // Set the search key to the vendor's name
205 // vendor is explicitly cast to char * to stop a compiler
206 // complaint.
207 Dbt key((char *)vendor, (u_int32_t)strlen(vendor) + 1);
208
209 // Make sure we use the memory we set aside for the VENDOR
210 // structure rather than the memory that DB allocates.
211 // Some systems may require structures to be aligned in memory
212 // in a specific way, and DB may not get it right.
213
214 data.set_data(&my_vendor);
215 data.set_ulen(sizeof(VENDOR));
216 data.set_flags(DB_DBT_USERMEM);
217
218 // Get the record
219 vendorDB.getDb().get(NULL, &key, &data, 0);
220 std::cout << " " << my_vendor.street << "\n"
221 << " " << my_vendor.city << ", "
222 << my_vendor.state << "\n"
223 << " " << my_vendor.zipcode << "\n"
224 << " " << my_vendor.phone_number << "\n"
225 << " Contact: " << my_vendor.sales_rep << "\n"
226 << " " << my_vendor.sales_rep_phone
227 << std::endl;
228
229 } catch(DbException &e) {
230 vendorDB.getDb().err(e.get_errno(), "Error in show_vendor");
231 throw e;
232 } catch(std::exception &e) {
233 throw e;
234 }
235 return (0);
236 }