1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>DB->associate_foreign()</title> 7 <link rel="stylesheet" href="apiReference.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB C API Reference" /> 10 <link rel="up" href="db.html" title="Chapter 2. The DB Handle" /> 11 <link rel="prev" href="dbassociate.html" title="DB->associate()" /> 12 <link rel="next" href="dbclose.html" title="DB->close()" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">DB->associate_foreign()</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbassociate.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 2. 23 The DB Handle 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="dbclose.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="dbassociate_foreign"></a>DB->associate_foreign()</h2> 35 </div> 36 </div> 37 </div> 38 <pre class="programlisting">#include <db.h> 39 40int 41DB->associate_foreign(DB *foreign, DB *secondary,, 42 int (*callback)(DB *secondary, 43 const DBT *key, DBT *data, const DBT *foreignkey, int *changed), 44 u_int32_t flags); </pre> 45 <p> 46 The <code class="methodname">DB->associate_foreign()</code> function is used to declare one database a 47 foreign constraint for a secondary database. The 48 <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a> handle that you call the 49 <code class="methodname">associate_foreign()</code> method from is the foreign 50 database. 51 </p> 52 <p> 53 After a foreign database has been "associated" with a secondary 54 database, all keys inserted into the secondary must exist in the 55 foreign database. Attempting to add a record with a foreign key 56 that does not exist in the foreign database will cause the put 57 method to fail and return <code class="literal">DB_FOREIGN_CONFLICT</code>. 58 </p> 59 <p> 60 Deletions in the foreign database affect the secondary in a manner 61 defined by the flags parameter. See 62 <a href="../../programmer_reference/am_foreign.html" class="olink">Foreign Indices</a> 63 in the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information. 64 </p> 65 <p> 66 The <code class="methodname">DB->associate_foreign()</code> <span> 67 <span> 68 method returns a non-zero error value on failure and 0 on success. 69 </span> 70 71 </span> 72 </p> 73 <div class="sect2" lang="en" xml:lang="en"> 74 <div class="titlepage"> 75 <div> 76 <div> 77 <h3 class="title"><a id="id1631161"></a>Parameters</h3> 78 </div> 79 </div> 80 </div> 81 <div class="sect3" lang="en" xml:lang="en"> 82 <div class="titlepage"> 83 <div> 84 <div> 85 <h4 class="title"><a id="id1630956"></a>callback</h4> 86 </div> 87 </div> 88 </div> 89 <p> 90 The <span class="bold"><strong>callback</strong></span> parameter is a callback 91 function that nullifies the foreign key portion of a data <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>. 92 </p> 93 <p> 94 The callback parameter must be NULL if either DB_FOREIGN_ABORT or DB_FOREIGN_CASCADE is set. 95 </p> 96 <p> 97 The callback takes four arguments: 98 </p> 99 <div class="itemizedlist"> 100 <ul type="disc"> 101 <li> 102 <p> 103 <code class="literal">secondary</code> 104 </p> 105 <p> 106 The <span class="bold"><strong>secondary</strong></span> parameter is the 107 database handle for the secondary. 108 </p> 109 </li> 110 <li> 111 <p> 112 <code class="literal">key</code> 113 </p> 114 <p> 115 The <span class="bold"><strong>key</strong></span> parameter is a 116 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary key. 117 </p> 118 </li> 119 <li> 120 <p> 121 <code class="literal">data</code> 122 </p> 123 <p> 124 The <span class="bold"><strong>data</strong></span> parameter is a 125 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary data 126 item to be updated. 127 </p> 128 </li> 129 <li> 130 <p> 131 <code class="literal">foreignkey</code> 132 </p> 133 <p> 134 The <span class="bold"><strong>foreignkey</strong></span> parameter is a 135 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the foreign 136 key which is being deleted. 137 </p> 138 </li> 139 <li> 140 <p> 141 <code class="literal">changed</code> 142 </p> 143 <p> 144 The <span class="bold"><strong>changed</strong></span> parameter is a 145 pointer to a boolean value, indicated whether <span class="bold"><strong>data</strong></span> 146 has changed. 147 </p> 148 </li> 149 </ul> 150 </div> 151 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 152 <h3 class="title">Note</h3> 153 <p> 154 Berkeley DB is not re-entrant. Callback functions should not 155 attempt to make library calls (for example, to release locks or 156 close open handles). Re-entering Berkeley DB is not guaranteed 157 to work correctly, and the results are undefined. 158 </p> 159 </div> 160 </div> 161 <div class="sect3" lang="en" xml:lang="en"> 162 <div class="titlepage"> 163 <div> 164 <div> 165 <h4 class="title"><a id="id1632271"></a>flags</h4> 166 </div> 167 </div> 168 </div> 169 <p> 170 The <span class="bold"><strong>flags</strong></span> parameter must be set to 171 one of the following values: 172 </p> 173 <div class="itemizedlist"> 174 <ul type="disc"> 175 <li> 176 <p><a id="associate_foreign_DB_FOREIGN_ABORT"></a> 177 <code class="literal">DB_FOREIGN_ABORT</code> 178 </p> 179 <p> 180 Abort the deletion of a key in the foreign database and return DB_FOREIGN_CONFLICT 181 if that key exists in the secondary database. The deletion should be protected by 182 a transaction to ensure database integrity after the aborted delete. 183 </p> 184 </li> 185 <li> 186 <p><a id="associate_foreign_DB_FOREIGN_CASCADE"></a> 187 <code class="literal">DB_FOREIGN_CASCADE</code> 188 </p> 189 <p> 190 The deletion of a key in the foreign database will also delete that key from the 191 secondary database (and the corresponding entry in the secondary's primary database.) 192 </p> 193 </li> 194 <li> 195 <p><a id="associate_foreign_DB_FOREIGN_NULLIFY"></a> 196 <code class="literal">DB_FOREIGN_NULLIFY</code> 197 </p> 198 <p> 199 The deletion of a key in the foreign database will call the nullification function 200 passed to associate_foreign and update the secondary database with the changed data. 201 </p> 202 </li> 203 </ul> 204 </div> 205 </div> 206 <div class="sect3" lang="en" xml:lang="en"> 207 <div class="titlepage"> 208 <div> 209 <div> 210 <h4 class="title"><a id="id1632009"></a>foreign</h4> 211 </div> 212 </div> 213 </div> 214 <p> 215 The <span class="bold"><strong>foreign</strong></span> parameter should be a 216 database handle for the foreign database. 217 </p> 218 </div> 219 <div class="sect3" lang="en" xml:lang="en"> 220 <div class="titlepage"> 221 <div> 222 <div> 223 <h4 class="title"><a id="id1631694"></a>secondary</h4> 224 </div> 225 </div> 226 </div> 227 <p> 228 The <span class="bold"><strong>secondary</strong></span> parameter should be an 229 open database handle of a database that contains a secondary index who's 230 keys also exist in the <span class="bold"><strong>foreign</strong></span> database. 231 </p> 232 </div> 233 </div> 234 <div class="sect2" lang="en" xml:lang="en"> 235 <div class="titlepage"> 236 <div> 237 <div> 238 <h3 class="title"><a id="id1631353"></a>Errors</h3> 239 </div> 240 </div> 241 </div> 242 <p> 243 The <code class="methodname">DB->associate_foreign()</code> <span> 244 <span> 245 method may fail and return one of the following non-zero errors: 246 </span> 247 248 </span> 249 </p> 250 <div class="sect3" lang="en" xml:lang="en"> 251 <div class="titlepage"> 252 <div> 253 <div> 254 <h4 class="title"><a id="id1631714"></a> DB_REP_HANDLE_DEAD</h4> 255 </div> 256 </div> 257 </div> 258 <p> 259 When a client synchronizes with the master, it is possible for committed 260 transactions to be rolled back. This invalidates all the database and cursor 261 handles opened in the replication environment. Once this occurs, an attempt to use 262 such a handle will 263 264 return <code class="literal">DB_REP_HANDLE_DEAD</code>. 265 The application will need to discard the handle and open a new one in order to 266 continue processing. 267 </p> 268 </div> 269 <div class="sect3" lang="en" xml:lang="en"> 270 <div class="titlepage"> 271 <div> 272 <div> 273 <h4 class="title"><a id="id1631807"></a>DB_REP_LOCKOUT</h4> 274 </div> 275 </div> 276 </div> 277 <p> 278 The operation was blocked by client/master synchronization. 279 </p> 280 </div> 281 <div class="sect3" lang="en" xml:lang="en"> 282 <div class="titlepage"> 283 <div> 284 <div> 285 <h4 class="title"><a id="id1632034"></a>EINVAL</h4> 286 </div> 287 </div> 288 </div> 289 <p> 290 If the foreign database handle is a secondary index; the foreign 291 database handle has been configured to allow duplicates; the foreign 292 database handle is a renumbering recno database; callback is 293 configured and DB_FOREIGN_NULLIFY is not; DB_FOREIGN_NULLIFY is 294 configured and callback is not. 295 </p> 296 </div> 297 </div> 298 <div class="sect2" lang="en" xml:lang="en"> 299 <div class="titlepage"> 300 <div> 301 <div> 302 <h3 class="title"><a id="id1631953"></a>Class</h3> 303 </div> 304 </div> 305 </div> 306 <p> 307 <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a> 308 </p> 309 </div> 310 <div class="sect2" lang="en" xml:lang="en"> 311 <div class="titlepage"> 312 <div> 313 <div> 314 <h3 class="title"><a id="id1630442"></a>See Also</h3> 315 </div> 316 </div> 317 </div> 318 <p> 319 <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> 320 </p> 321 </div> 322 </div> 323 <div class="navfooter"> 324 <hr /> 325 <table width="100%" summary="Navigation footer"> 326 <tr> 327 <td width="40%" align="left"><a accesskey="p" href="dbassociate.html">Prev</a> </td> 328 <td width="20%" align="center"> 329 <a accesskey="u" href="db.html">Up</a> 330 </td> 331 <td width="40%" align="right"> <a accesskey="n" href="dbclose.html">Next</a></td> 332 </tr> 333 <tr> 334 <td width="40%" align="left" valign="top">DB->associate() </td> 335 <td width="20%" align="center"> 336 <a accesskey="h" href="index.html">Home</a> 337 </td> 338 <td width="40%" align="right" valign="top"> DB->close()</td> 339 </tr> 340 </table> 341 </div> 342 </body> 343</html> 344