00001
00002
00003
00004
00005
00006
00007
00008 #include "gettingstarted_common.h"
00009
00010 int get_item_name(DB *, const DBT *, const DBT *, DBT *);
00011
00012
00013
00014
00015
00016
00017 int
00018 get_item_name(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey)
00019 {
00020 u_int offset;
00021
00022 dbp = NULL;
00023 pkey = NULL;
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 offset = sizeof(float) + sizeof(int);
00036
00037
00038 if (pdata->size < offset)
00039 return (-1);
00040
00041
00042
00043
00044 memset(skey, 0, sizeof(DBT));
00045 skey->data = (u_int8_t *)pdata->data + offset;
00046 skey->size = (u_int32_t)strlen(skey->data) + 1;
00047
00048 return (0);
00049 }
00050
00051
00052 int
00053 open_database(DB **dbpp, const char *file_name,
00054 const char *program_name, FILE *error_file_pointer,
00055 int is_secondary)
00056 {
00057 DB *dbp;
00058 u_int32_t open_flags;
00059 int ret;
00060
00061
00062 ret = db_create(&dbp, NULL, 0);
00063 if (ret != 0) {
00064 fprintf(error_file_pointer, "%s: %s\n", program_name,
00065 db_strerror(ret));
00066 return (ret);
00067 }
00068
00069 *dbpp = dbp;
00070
00071
00072 dbp->set_errfile(dbp, error_file_pointer);
00073 dbp->set_errpfx(dbp, program_name);
00074
00075
00076
00077
00078
00079 if (is_secondary) {
00080 ret = dbp->set_flags(dbp, DB_DUPSORT);
00081 if (ret != 0) {
00082 dbp->err(dbp, ret, "Attempt to set DUPSORT flags failed.",
00083 file_name);
00084 return (ret);
00085 }
00086 }
00087
00088
00089 open_flags = DB_CREATE;
00090
00091
00092 ret = dbp->open(dbp,
00093 NULL,
00094 file_name,
00095 NULL,
00096 DB_BTREE,
00097 open_flags,
00098 0);
00099 if (ret != 0) {
00100 dbp->err(dbp, ret, "Database '%s' open failed.", file_name);
00101 return (ret);
00102 }
00103
00104 return (0);
00105 }
00106
00107
00108 int
00109 databases_setup(STOCK_DBS *my_stock, const char *program_name,
00110 FILE *error_file_pointer)
00111 {
00112 int ret;
00113
00114
00115 ret = open_database(&(my_stock->vendor_dbp),
00116 my_stock->vendor_db_name,
00117 program_name, error_file_pointer,
00118 PRIMARY_DB);
00119 if (ret != 0)
00120
00121
00122
00123
00124 return (ret);
00125
00126
00127 ret = open_database(&(my_stock->inventory_dbp),
00128 my_stock->inventory_db_name,
00129 program_name, error_file_pointer,
00130 PRIMARY_DB);
00131 if (ret != 0)
00132
00133
00134
00135
00136 return (ret);
00137
00138
00139
00140
00141
00142
00143 ret = open_database(&(my_stock->itemname_sdbp),
00144 my_stock->itemname_db_name,
00145 program_name, error_file_pointer,
00146 SECONDARY_DB);
00147 if (ret != 0)
00148
00149
00150
00151
00152 return (0);
00153
00154
00155
00156
00157
00158 my_stock->inventory_dbp->associate(
00159 my_stock->inventory_dbp,
00160 NULL,
00161 my_stock->itemname_sdbp,
00162 get_item_name,
00163 0);
00164
00165 printf("databases opened successfully\n");
00166 return (0);
00167 }
00168
00169
00170 void
00171 initialize_stockdbs(STOCK_DBS *my_stock)
00172 {
00173 my_stock->db_home_dir = DEFAULT_HOMEDIR;
00174 my_stock->inventory_dbp = NULL;
00175 my_stock->vendor_dbp = NULL;
00176 my_stock->itemname_sdbp = NULL;
00177 my_stock->vendor_db_name = NULL;
00178 my_stock->inventory_db_name = NULL;
00179 my_stock->itemname_db_name = NULL;
00180 }
00181
00182
00183 void
00184 set_db_filenames(STOCK_DBS *my_stock)
00185 {
00186 size_t size;
00187
00188
00189 size = strlen(my_stock->db_home_dir) + strlen(INVENTORYDB) + 1;
00190 my_stock->inventory_db_name = malloc(size);
00191 snprintf(my_stock->inventory_db_name, size, "%s%s",
00192 my_stock->db_home_dir, INVENTORYDB);
00193
00194
00195 size = strlen(my_stock->db_home_dir) + strlen(VENDORDB) + 1;
00196 my_stock->vendor_db_name = malloc(size);
00197 snprintf(my_stock->vendor_db_name, size, "%s%s",
00198 my_stock->db_home_dir, VENDORDB);
00199
00200
00201 size = strlen(my_stock->db_home_dir) + strlen(ITEMNAMEDB) + 1;
00202 my_stock->itemname_db_name = malloc(size);
00203 snprintf(my_stock->itemname_db_name, size, "%s%s",
00204 my_stock->db_home_dir, ITEMNAMEDB);
00205
00206 }
00207
00208
00209 int
00210 databases_close(STOCK_DBS *my_stock)
00211 {
00212 int ret;
00213
00214
00215
00216
00217 if (my_stock->itemname_sdbp != NULL) {
00218 ret = my_stock->itemname_sdbp->close(my_stock->itemname_sdbp, 0);
00219 if (ret != 0)
00220 fprintf(stderr, "Itemname database close failed: %s\n",
00221 db_strerror(ret));
00222 }
00223
00224 if (my_stock->inventory_dbp != NULL) {
00225 ret = my_stock->inventory_dbp->close(my_stock->inventory_dbp, 0);
00226 if (ret != 0)
00227 fprintf(stderr, "Inventory database close failed: %s\n",
00228 db_strerror(ret));
00229 }
00230
00231 if (my_stock->vendor_dbp != NULL) {
00232 ret = my_stock->vendor_dbp->close(my_stock->vendor_dbp, 0);
00233 if (ret != 0)
00234 fprintf(stderr, "Vendor database close failed: %s\n",
00235 db_strerror(ret));
00236 }
00237
00238 printf("databases closed.\n");
00239 return (0);
00240 }