7 static char *rcsid_samp1a_c = "$Header$";
10 #define terminatedb(x) /* nothin doin yet */
14 * A simple database query program.
16 * This sample program illustrates the use of the 'client library'
17 * for remote access to a relational database. This version of
18 * the sample is about the simplest possible. It accesses only
19 * a single database, it does the access syncrhonously, and it uses
20 * only system defined types. Many real applications will fit this
23 * For purposes of illustration, this program accesses a database
24 * of parts. For each part where the value of current inventory,
25 * defined as number on hand times cost per unit, exceeds $1000,
26 * this routine prints the description of the part, a code number,
27 * the manufacturer, the cost, and the number on hand.
29 * Because this program accesses the database using the client
30 * library, it may be run from any Berkeley Unix system on the internet.
31 * Necessary data conversions are done automatically if the database
32 * itself happens to live on an incompatible machine.
34 * Author: Noah Mendelsohn
40 char *field_names[] = {"desc",
45 FIELD_TYPE field_types[] = {STRING_T, /* desc */
49 INTEGER_T}; /* count */
57 \f /************************************************************
59 ************************************************************/
62 * Declare the names of fields to be retrieved and their types
69 * The following defines are for convenience in addressing
80 * Declare the relation and related data structures for
81 * storing the retrieved data.
84 TUPLE_DESCRIPTOR tuple_desc;
85 RELATION retrieved_data;
88 * Declare a handle to identify our session with the database
95 * Declarations for misc. variables
98 TUPLE t; /* next tuple to print */
99 int rc; /* A return code */
101 \f /************************************************************
102 * EXECUTION BEGINS HERE *
103 ************************************************************/
110 * Open a connection to the database - identify session as parts_data
113 if (access_db("noah@menelaus", &parts_data) != DB_OPEN) {
114 printf("Cannot connect to parts database--giving up\n");
119 * Build the descriptor describing the layout of the tuples
120 * to be retrieved, and create an empty relation into which
121 * the retrieval will be done.
124 tuple_desc = create_tuple_descriptor(field_count, field_names,
126 retrieved_data = create_relation(tuple_desc);
129 * Do the query for parts with inventory over $1000
130 * Put results in the relation named retrieved_data.
133 rc = db_query(parts_data, retrieved_data,
134 "(>*desc*< = parts.desc,>*code*< = parts.code,>*man*< = parts.man,>*cost*< = parts.cost,>*cnt*< = parts.cnt) where (parts.cnt * parts.cost > 1000.00)");
136 if (rc != OP_SUCCESS) {
137 printf("Error during retrieval--giving up\n");
138 terminatedb(parts_data);
143 * Print out the results
146 for (t = FIRST_TUPLE_IN_RELATION(retrieved_data); t!= NULL;
147 t = NEXT_TUPLE_IN_RELATION(retrieved_data,t))
150 printf("\nEnd of Report.\n\n");
156 terminatedb(parts_data);
163 * Given a tuple with parts data, print it on standard output.
164 * NOTE: for clarity, we've left out the casts which should be
165 * done on the pointers returned from FIELD_FROM_TUPLE.
171 printf("desc=%s ", STRING_DATA(*(STRING *)(FIELD_FROM_TUPLE(tup, DESC))));
172 printf("code=%d ", *(int *)(FIELD_FROM_TUPLE(tup, CODE)));
173 printf("manufacturer=%s ",STRING_DATA(*(STRING *)(FIELD_FROM_TUPLE(tup, MAN))));
174 printf("cost=%f ", *(double *)(FIELD_FROM_TUPLE(tup, COST)));
175 printf("count=%d\n", *(int *)(FIELD_FROM_TUPLE(tup, CNT)));