7 static char *rcsid_samp1_c = "$Header$";
13 * A simple database query program.
15 * This sample program illustrates the use of the 'client library'
16 * for remote access to a relational database. This version of
17 * the sample is about the simplest possible. It accesses only
18 * a single database, it does the access syncrhonously, and it uses
19 * only system defined types. Many real applications will fit this
22 * For purposes of illustration, this program accesses a database
23 * of parts. For each part where the value of current inventory,
24 * defined as number on hand times cost per unit, exceeds $1000,
25 * this routine prints the description of the part, a code number,
26 * the manufacturer, the cost, and the number on hand.
28 * Because this program accesses the database using the client
29 * library, it may be run from any Berkeley Unix system on the internet.
30 * Necessary data conversions are done automatically if the database
31 * itself happens to live on an incompatible machine.
33 * Author: Noah Mendelsohn
45 \f /************************************************************
47 ************************************************************/
50 * Declare the names of fields to be retrieved and their types
54 char *field_names[] = {"desc",
59 FIELD_TYPE field_types[] = {STRING_T, /* desc */
63 INTEGER_T}; /* count */
66 * The following defines are for convenience in addressing
77 * Declare the relation and related data structures for
78 * storing the retrieved data.
81 TUPLE_DESCRIPTOR tuple_desc;
82 RELATION retrieved_data;
85 * Declare a handle to identify our session with the database
92 * Declarations for misc. variables
95 TUPLE t; /* next tuple to print */
96 int rc; /* A return code */
98 \f /************************************************************
99 * EXECUTION BEGINS HERE *
100 ************************************************************/
103 * Open a connection to the database - identify session as parts_data
106 if (accessdb("partsdata@hostname", &parts_data) != DB_SUCCESS) {
107 printf("Cannot connect to parts database--giving up\n");
112 * Build the descriptor describing the layout of the tuples
113 * to be retrieved, and create an empty relation into which
114 * the retrieval will be done.
117 tuple_desc = create_tuple_descriptor(field_count, field_names,
119 retrieved_data = create_relation(tuple_desc);
122 * Do the query for parts with inventory over $1000
123 * Put results in the relation named retrieved_data.
126 rc = db_query(parts_data, retrieved_data,
127 "(>*desc*< = parts.desc,
128 >*code*< = parts.code,
130 >*cost*< = parts.cost,
131 >*count*< = parts.count)
132 where (parts.count * parts.cost > 1000.00)");
134 if (rc != DB_SUCCESS) {
135 printf("Error during retrieval--giving up\n");
136 terminatedb(parts_data);
141 * Print out the results
144 for (t = FIRST_TUPLE_IN_RELATION(retrieved_data); t!= NULL;
145 t = NEXT_TUPLE_IN_RELATION(retrieved_data))
148 printf("\nEnd of Report.\n\n");
154 terminatedb(parts_data);
161 * Given a tuple with parts data, print it on standard output.
162 * NOTE: for clarity, we've left out the casts which should be
163 * done on the pointers returned from FIELD_FROM_TUPLE.
169 printf("desc=%s ", *(FIELD_FROM_TUPLE(tup, DESC)));
170 printf("code=%d ", *(FIELD_FROM_TUPLE(tup, CODE)));
171 printf("manufacturer=%s ",*(FIELD_FROM_TUPLE(tup, MAN)));
172 printf("cost=%f ", *(FIELD_FROM_TUPLE(tup, COST)));
173 printf("count=%d\n", *(FIELD_FROM_TUPLE(tup, COUNT)));