summaryrefslogtreecommitdiff
path: root/docs/gsg/C/joins.html
blob: a2d7136dc26d9a9d5df8b4774828d3e3f7c0da15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Database Joins</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
    <link rel="up" href="indexes.html" title="Chapter 5. Secondary Databases" />
    <link rel="prev" href="secondaryCursor.html" title="Using Cursors with Secondary Databases" />
    <link rel="next" href="coreindexusage.html" title="Secondary Database Example" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 12.1.6.1</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Database Joins</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="secondaryCursor.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 5. Secondary Databases</th>
          <td width="20%" align="right"> <a accesskey="n" href="coreindexusage.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="joins"></a>Database Joins</h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <dl>
          <dt>
            <span class="sect2">
              <a href="joins.html#joinUsage">Using Join Cursors</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
        If you have two or more secondary databases associated with a primary
        database, then you can retrieve primary records based on the intersection of 
        multiple secondary entries. You do this using a
        
        <span>join cursor.</span>
    </p>
      <p>
        Throughout this document we have presented a 
            
            <span>structure</span>
        that stores 
            
        information on grocery
            
            <span>vendors.</span>
        That 
            
            <span>structure</span>
        is fairly simple with a limited
        number of data members, few of which would be interesting from a query
        perspective. But suppose, instead, that we were storing 
        information on something with many more characteristics that can be queried, such
        as an automobile. In that case, you may be storing information such as
        color, number of doors, fuel mileage, automobile type, number of
        passengers, make, model, and year, to name just a few.
    </p>
      <p>
        In this case, you would still likely be using some unique value to key your
        primary entries (in the United States, the automobile's VIN would be
        ideal for this purpose). You would then create a 
            
            <span>structure</span>
        that identifies
        all the characteristics of the automobiles in your inventory. 
        
        
    </p>
      <p>
        To query this data, you might then create multiple secondary databases,
        one for each of the characteristics that you want to query. For
        example, you might create a secondary for color, another for number of
        doors, another for number of passengers, and so forth. Of course, you
        will need a unique 
            
            <span>key extractor function</span>
        for each such secondary database. You do
        all of this using the concepts and techniques described throughout this
        chapter.
    </p>
      <p>
        Once you have created this primary database and all interesting
        secondaries, what you have is the ability to retrieve automobile records
        based on a single characteristic. You can, for example, find all the
        automobiles that are red. Or you can find all the automobiles that have
        four doors. Or all the automobiles that are minivans. 
    </p>
      <p>
        The next most natural step, then, is to form compound queries, or joins.
        For example, you might want to find all the automobiles that are red,
        and that were built by Toyota, and that are minivans. You can do this
        using a 
            
            <span>join cursor.</span>
    </p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="joinUsage"></a>Using Join Cursors</h3>
            </div>
          </div>
        </div>
        <p>
            To use a join cursor:
        </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
                    Open two or more 
                        
                    cursors 
                        
                        <span>for</span> 
                    secondary databases that are associated with
                    the same primary database.
                </p>
            </li>
            <li>
              <p>
                    Position each such cursor to the secondary key
                    value in which you are interested. For example, to build on
                    the previous description, the cursor for the color
                    database is positioned to the <code class="literal">red</code> records
                    while the cursor for the model database is positioned to the
                    <code class="literal">minivan</code> records, and the cursor for the
                    make database is positioned to <code class="literal">Toyota</code>.
                </p>
            </li>
            <li>
              <p>

                    Create an array of  cursors, and
                    place in it each of the cursors that are participating in your join query. 
                    <span>Note that this array must be null terminated.</span>

                </p>
            </li>
            <li>
              <p>

                    Obtain a join cursor. You do this using the 
                         
                        <code class="methodname">DB-&gt;join()</code> 
                         
                    method.  You must pass this method the array of secondary cursors that you
                    opened and positioned in the previous steps.
                </p>
            </li>
            <li>
              <p>
                    Iterate over the set of matching records
                        
                    until
                    
                    <span>the return code is not <code class="literal">0</code>.</span>
                </p>
            </li>
            <li>
              <p>
                    Close your  cursor.
                </p>
            </li>
            <li>
              <p>
                    If you are done with them, close all your  cursors.
                </p>
            </li>
          </ul>
        </div>
        <p>
            For example:
        </p>
        <a id="c_index9"></a>
        <pre class="programlisting">#include &lt;db.h&gt;
#include &lt;string.h&gt;

...

DB *automotiveDB;
DB *automotiveColorDB;
DB *automotiveMakeDB;
DB *automotiveTypeDB;
DBC *color_curs, *make_curs, *type_curs, *join_curs;
DBC *carray[4];
DBT key, data;
int ret;

char *the_color = "red";
char *the_type = "minivan";
char *the_make = "Toyota";

/* Database and secondary database opens omitted for brevity.
 * Assume a primary database handle:
 *   automotiveDB
 * Assume 3 secondary database handles:
 *   automotiveColorDB  -- secondary database based on automobile color
 *   automotiveMakeDB  -- secondary database based on the manufacturer
 *   automotiveTypeDB  -- secondary database based on automobile type
 */

/* initialize pointers and structures */
color_curs = NULL;
make_curs = NULL;
type_curs = NULL;
join_curs = NULL;

memset(&amp;key, 0, sizeof(DBT));
memset(&amp;data, 0, sizeof(DBT));

/* open the cursors */
if (( ret =
    automotiveColorDB-&gt;cursor(automotiveColorDB, NULL, 
      &amp;color_curs, 0)) != 0) {
        /* Error handling goes here */
}

if (( ret =
    automotiveMakeDB-&gt;cursor(automotiveMakeDB, NULL, 
      &amp;make_curs, 0)) != 0) {
        /* Error handling goes here */
}

if (( ret =
    automotiveTypeDB-&gt;cursor(automotiveTypeDB, NULL, 
      &amp;type_curs, 0)) != 0) {
        /* Error handling goes here */
}

/* Position the cursors */
key.data = the_color;
key.size = strlen(the_color) + 1;
if ((ret = color_curs-&gt;get(color_curs, &amp;key, &amp;data, DB_SET)) != 0)
    /* Error handling goes here */

key.data = the_make;
key.size = strlen(the_make) + 1;
if ((ret = make_curs-&gt;get(make_curs, &amp;key, &amp;data, DB_SET)) != 0)
    /* Error handling goes here */

key.data = the_type;
key.size = strlen(the_type) + 1;
if ((ret = type_curs-&gt;get(type_curs, &amp;key, &amp;data, DB_SET)) != 0)
    /* Error handling goes here */

/* Set up the cursor array */
carray[0] = color_curs;
carray[1] = make_curs;
carray[2] = type_curs;
carray[3] = NULL;

/* Create the join */
if ((ret = automotiveDB-&gt;join(automotiveDB, carray, &amp;join_curs, 0)) != 0)
    /* Error handling goes here */

/* Iterate using the join cursor */
while ((ret = join_curs-&gt;get(join_curs, &amp;key, &amp;data, 0)) == 0) {
    /* Do interesting things with the key and data */
}

/*
 * If we exited the loop because we ran out of records,
 * then it has completed successfully.
 */
if (ret == DB_NOTFOUND) {
    /* 
     * Close all our cursors and databases as is appropriate,  and 
     * then exit with a normal exit status (0). 
     */
} </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="secondaryCursor.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="indexes.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="coreindexusage.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">
        
        <span>Using Cursors with Secondary Databases</span>
     </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Secondary Database Example</td>
        </tr>
      </table>
    </div>
  </body>
</html>