Debian Codegraph @

docs/api_reference/CXX/db_sql_codegen.html

raw symbols SCIP
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_sql_codegen</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="utilities.html" title="Appendix A.  Berkeley DB Command Line Utilities" />
11 <link rel="prev" href="db_replicate.html" title="db_replicate" />
12 <link rel="next" href="dbsql.html" title="dbsql" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">db_sql_codegen</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="db_replicate.html">Prev</a> </td>
25 <th width="60%" align="center">Appendix A. 
26 Berkeley DB Command Line Utilities
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="dbsql.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="db_sql_codegen"></a>db_sql_codegen</h2>
38 </div>
39 </div>
40 </div>
41 <pre class="programlisting">db_sql_codegen [-i &lt;ddl input file&gt;] [-o &lt;output C code file&gt;]
42 [-h &lt;output header file&gt;] [-t &lt;test output file&gt;] </pre>
43 <p>
44 <span class="command"><strong>Db_sql_codegen</strong></span> is a utility program that translates a
45 schema description written in a SQL Data Definition Language dialect
46 into C code that implements the schema using Berkeley DB. It is
47 intended to provide a quick and easy means of getting started with
48 Berkeley DB for users who are already conversant with SQL. It also
49 introduces a convenient way to express a Berkeley DB schema in a
50 format that is both external to the program that uses it and
51 compatible with relational databases.
52 </p>
53 <p>
54 The <span class="command"><strong>db_sql_codegen</strong></span> command reads DDL from an input stream,
55 and writes C code to an output stream. With no command line options,
56 it will read from stdin and write to stdout. A more common usage mode
57 would be to supply the DDL in a named input file (-i option). With
58 only the -i option, <span class="command"><strong>db_sql_codegen</strong></span> will produce two files:
59 a C-language source code (.c) file and a C-language header (.h) file,
60 with names that are derived from the name of the input file. You can
61 also control the names of these output files with the -o and -h
62 options. The -x option causes the generated code to be
63 transaction-aware. Finally, the -t option will produce a simple application
64 that invokes the generated function API. This is a C-language source
65 file that includes a main function, and serves the dual purposes of
66 providing a simple test for the generated C code, and of being an
67 example of how to use the generated API.
68 </p>
69 <p>
70 The options are as follows:
71 </p>
72 <div class="itemizedlist">
73 <ul type="disc">
74 <li>
75 <p>
76 <span class="bold"><strong>-i</strong></span>&lt;ddl input file&gt;
77 </p>
78 <p>
79 Names the input file containing SQL DDL.
80 </p>
81 </li>
82 <li>
83 <p>
84 <span class="bold"><strong>-o</strong></span> &lt;output C code file&gt;
85 </p>
86 <p>
87 Names the output C-language source code file.
88 </p>
89 </li>
90 <li>
91 <p>
92 <span class="bold"><strong>-h</strong></span> &lt;output header file&gt;
93 </p>
94 <p>
95 Names the output C-language header file.
96 </p>
97 </li>
98 <li>
99 <p>
100 <span class="bold"><strong>-t</strong></span> &lt;test output file&gt;
101 </p>
102 <p>
103 Names the output C-langage test file.
104 </p>
105 </li>
106 <li>
107 <p>
108 <span class="bold"><strong>-x</strong></span>
109 </p>
110 <p>
111 Sets the default transaction mode to TRANSACTIONAL.
112 </p>
113 </li>
114 </ul>
115 </div>
116 <p>
117 The <span class="command"><strong>db_sql_codegen</strong></span> utility exits 0 on success, and &gt;0 if an error occurs.
118 </p>
119 <p> Note that the <span class="command"><strong>db_sql_codegen</strong></span> utility is built only when --enable-sql_codegen option is passed as an argument when you are configuring Berkeley DB. For more information, see <a href="../../installation/build_unix_conf.html" class="olink">"Configuring Berkeley DB"</a></p>
120 <div class="sect2" lang="en" xml:lang="en">
121 <div class="titlepage">
122 <div>
123 <div>
124 <h3 class="title"><a id="idp10852024"></a>Input Syntax</h3>
125 </div>
126 </div>
127 </div>
128 <p>
129 The input file can contain the following SQL DDL statements.
130 </p>
131 <div class="itemizedlist">
132 <ul type="disc">
133 <li>
134 <p>
135 <span class="bold"><strong>CREATE DATABASE</strong></span>
136 </p>
137 <p>
138 The DDL must contain a CREATE DATABASE statement. The syntax is simply
139 </p>
140 <pre class="programlisting">CREATE DATABASE name;</pre>
141 <p>. The
142 name given here is used as the name of the Berkeley DB
143 environment in which the Berkeley DB databases are created.
144 </p>
145 </li>
146 <li>
147 <p>
148 <span class="bold"><strong>CREATE TABLE</strong></span>
149 </p>
150 <p>
151 Each CREATE TABLE statement produces functions to create and
152 delete a primary Berkeley DB database. Also produced are
153 functions to perform record insertion, retrieval and deletion
154 on this database.
155 </p>
156 <p>
157 CREATE TABLE establishes the field set of records that can
158 be stored in the Berkeley DB database. Every CREATE TABLE
159 statement must identify a primary key to be used as the
160 lookup key in the Berkeley DB database.
161 </p>
162 <p>
163 Here is an example to illustrate the syntax of CREATE TABLE that
164 is accepted by <span class="command"><strong>db_sql_codegen</strong></span>:
165 </p>
166 <p>
167 </p>
168 <pre class="programlisting">CREATE TABLE person (person_id INTEGER PRIMARY KEY,
169 name VARCHAR(64),
170 age INTEGER);</pre>
171 <p>
172 </p>
173 <p>
174 This results in the creation of functions to manage a database in
175 which every record is an instance of the following C language
176 data structure:
177 </p>
178 <p>
179 </p>
180 <pre class="programlisting">typedef struct _person_data {
181 int person_id;
182 char name[PERSON_DATA_NAME_LENGTH];
183 int age;
184 } person_data; </pre>
185 <p>
186 </p>
187 </li>
188 <li>
189 <p>
190 <span class="bold"><strong>CREATE INDEX</strong></span> You can create
191 secondary Berkeley DB databases to be used as indexes into a
192 primary database. For example, to make an index on the "name"
193 field of the "person" table mentioned above, the SQL DDL would
194 be:
195 </p>
196 <p>
197 </p>
198 <pre class="programlisting">CREATE INDEX name_index ON person(name);</pre>
199 <p>
200 </p>
201 <p>
202 This causes <span class="command"><strong>db_sql_codegen</strong></span> to emit functions to
203 manage creation and deletion of a secondary database called
204 "name_index," which is associated with the "person" database
205 and is set up to perform lookups on the "name" field.
206 </p>
207 </li>
208 </ul>
209 </div>
210 </div>
211 <div class="sect2" lang="en" xml:lang="en">
212 <div class="titlepage">
213 <div>
214 <div>
215 <h3 class="title"><a id="idp10847120"></a>Hint Comments</h3>
216 </div>
217 </div>
218 </div>
219 <p>
220 The SQL DDL input may contain comments. Two types of comments are
221 recognized. C-style comments begin with "/*" and end with "*/".
222 These comments may extend over multiple lines.
223 </p>
224 <p>
225 Single line comments begin with "--" and run to the end of the line.
226 </p>
227 <p>
228 If the first character of a comment is "+" then the comment is
229 interpreted as a "hint comment." Hint comments can be used to
230 configure Berkeley DB features that cannot be represented in SQL DDL.
231 </p>
232 <p>
233 Hint comments are comma-separated lists of property assignments of the
234 form "property=value." Hint comments apply to the SQL DDL statement
235 that immediately precedes their appearance in the input. For example:
236 </p>
237 <p>
238 </p>
239 <pre class="programlisting">CREATE DATABASE peopledb; /*+ CACHESIZE = 16m */</pre>
240 <p>
241 </p>
242 <p>
243 This causes the generated environment creation function to set the
244 cache size to sixteen megabytes.
245 </p>
246 <p>
247 In addition to the CACHESIZE example above, two other
248 hint comment keywords are recognized: DBTYPE and MODE.
249 </p>
250 <p>
251 After a CREATE TABLE or CREATE INDEX statement, you may set the
252 database type by assigning the DBTYPE property in a hint comment.
253 Possible values for DBTYPE are BTREE and HASH.
254 </p>
255 <p>
256 After a CREATE DATABASE or CREATE TABLE statement, you may tell
257 <span class="command"><strong>db_sql_codegen</strong></span> whether to generate transaction-aware
258 code by assigning the MODE property in a hint comment. The
259 possible values for MODE are TRANSACTIONAL and NONTRANSACTIONAL.
260 By default, generated code is not transaction-aware. If
261 MODE=TRANSACTIONAL appears on a CREATE DATABASE statement, then
262 the default for every CREATE TABLE statement becomes
263 TRANSACTIONAL. Individual CREATE TABLE statements may have
264 MODE=TRANSACTIONAL or MODE=NONTRANSACTIONAL, to control whether
265 the code generated for accessing and updating the associated
266 Berkeley DB database is transaction aware.
267 </p>
268 </div>
269 <div class="sect2" lang="en" xml:lang="en">
270 <div class="titlepage">
271 <div>
272 <div>
273 <h3 class="title"><a id="idp10854568"></a>Transactions</h3>
274 </div>
275 </div>
276 </div>
277 <p>
278 By default, the code generated by <span class="command"><strong>db_sql_codegen</strong></span> is
279 not transaction-aware. This means that the generated API for
280 reading and updating BDB databases operates in nontransactional
281 mode. When transactional mode is enabled, either through the
282 command-line option <span class="bold"><strong>-x</strong></span> or by the
283 inclusion of MODE-setting hint comments in the DDL source, the
284 generated data access functions take an extra argument which is a
285 pointer to DB_TXN. To use transactions, application code must
286 acquire a DB_TXN from a call to DB_ENV-&gt;txn_begin, and supply a
287 pointer to this object when invoking the db_sql_codegen-generated
288 functions that requre such an argument.
289 </p>
290 <p>
291 Transaction-aware APIs that were generated by db_sql_codegen can be used in
292 nontransactional mode by passing NULL for the DB_TXN pointer
293 arguments.
294 </p>
295 <p>
296 For more information about using BDB transactions, please consult
297 the documentation for <a class="xref" href="txn.html#txnlist" title="Transaction Subsystem and Related Methods">Transaction Subsystem and Related Methods</a> .
298 </p>
299 </div>
300 <div class="sect2" lang="en" xml:lang="en">
301 <div class="titlepage">
302 <div>
303 <div>
304 <h3 class="title"><a id="idp10860160"></a>Type Mapping</h3>
305 </div>
306 </div>
307 </div>
308 <p>
309 <span class="command"><strong>db_sql_codegen</strong></span> must map the schema expressed as SQL
310 types into C language types. It implements the following mappings:
311 </p>
312 <p>
313 </p>
314 <pre class="programlisting">BIN char[]
315 VARBIN char[]
316 CHAR char[]
317 VARCHAR char[]
318 VARCHAR2 char[]
319 BIT char
320 TINYINT char
321 SMALLINT short
322 INTEGER int
323 INT int
324 BIGINT long
325 REAL float
326 DOUBLE double
327 FLOAT double
328 DECIMAL double
329 NUMERIC double
330 NUMBER(p,s) int, long, float, or double </pre>
331 <p>
332 </p>
333 <p>
334 While BIN/VARBIN and CHAR/VARCHAR are both represented as char arrays,
335 the latter are treated as null-terminated C strings, while the former
336 are treated as binary data.
337 </p>
338 <p>
339 The Oracle type NUMBER is mapped to different C types, depending
340 on its precision and scale values. If scale is 0, then it is
341 mapped to an integer type (long if precision is greater than 9).
342 Otherwise it is mapped to a floating point type (float if
343 precision is less than 7, otherwise double).
344 </p>
345 </div>
346 <div class="sect2" lang="en" xml:lang="en">
347 <div class="titlepage">
348 <div>
349 <div>
350 <h3 class="title"><a id="idp10860688"></a>Output</h3>
351 </div>
352 </div>
353 </div>
354 <p>
355 Depending on the options given on the command
356 line, <span class="command"><strong>db_sql_codegen</strong></span> can produce three separate files: a .c
357 file containing function definitions that implement the generated API;
358 a .h file containing constants, data structures and prototypes of the
359 generated functions; and a second .c file that contains a sample
360 program that invokes the generated API. The latter program is usually
361 referred to as a smoke test.
362 </p>
363 <p>
364 Given the following sample input in a file named "people.sql":
365 </p>
366 <p>
367 </p>
368 <pre class="programlisting">CREATE DATABASE peopledb;
369 CREATE TABLE person (person_id INTEGER PRIMARY KEY,
370 name VARCHAR(64),
371 age INTEGER);
372 CREATE INDEX name_index ON person(name);</pre>
373 <p>
374 </p>
375 <p>
376 The command
377 </p>
378 <p>
379 </p>
380 <pre class="programlisting">db_sql_codegen -i people.sql -t test_people.c</pre>
381 <p>
382 </p>
383 <p>
384 Will produce files named people.h, people.c, and test_people.c.
385 </p>
386 <p>
387 The file people.h will contain the information needed to use the
388 generated API. Among other things, an examination of the generated .h
389 file will reveal:
390 </p>
391 <p>
392 </p>
393 <pre class="programlisting">#define PERSON_DATA_NAME_LENGTH 63</pre>
394 <p>
395 </p>
396 <p>
397 This is just a constant for the length of the string mapped from
398 the VARCHAR field.
399 </p>
400 <p>
401 </p>
402 <pre class="programlisting">typedef struct _person_data {
403 int person_id;
404 char name[PERSON_DATA_NAME_LENGTH];
405 int age;
406 } person_data; </pre>
407 <p>
408 </p>
409 <p>
410 This is the data structure that represents the record type that is
411 stored in the person database. There's that constant being used.
412 </p>
413 <p>
414 </p>
415 <pre class="programlisting">int create_peopledb_env(DB_ENV **envpp);
416 int create_person_database(DB_ENV *envp, DB **dbpp);
417 int create_name_index_secondary(DB_ENV *envp, DB *primary_dbp,
418 DB **secondary_dbpp); </pre>
419 <p>
420 </p>
421 <p>
422 These functions must be invoked to initialize the Berkeley DB
423 environment. However, see the next bit:
424 </p>
425 <p>
426 </p>
427 <pre class="programlisting">extern DB_ENV * peopledb_envp;
428 extern DB *person_dbp;
429 extern DB *name_index_dbp;
430
431 int initialize_peopledb_environment(); </pre>
432 <p>
433 </p>
434 <p>
435 For convenience, <span class="command"><strong>db_sql_codegen</strong></span> provides global
436 variables for the environment and database, and a single
437 initialization function that sets up the environment for you.
438 You may choose to use the globals and the single initialization
439 function, or you may declare your own DB_ENV and DB pointers,
440 and invoke the individual create_* functions yourself.
441 </p>
442 <p>
443 The word "create" in these function names might be confusing. It means
444 "create the environment/database if it doesn't already exist;
445 otherwise open it."
446 </p>
447 <p>
448 All of the functions in the generated API return Berkeley DB error
449 codes. If the return value is non-zero, there was an error of some
450 kind, and an explanatory message should have been printed on stderr.
451 </p>
452 <p>
453 </p>
454 <pre class="programlisting">int person_insert_struct(DB *dbp, person_data *personp);
455 int person_insert_fields(DB * dbp,
456 int person_id,
457 char *name,
458 int age); </pre>
459 <p>
460 </p>
461 <p>
462 These are the functions that you'd use to store a record in the
463 database. The first form takes a pointer to the data structure that
464 represents this record. The second form takes each field as a
465 separate argument.
466 </p>
467 <p>
468 If two records with the same primary key value are stored, the first
469 one is lost.
470 </p>
471 <p>
472 </p>
473 <pre class="programlisting">int get_person_data(DB *dbp, int person_key, person_data *data);</pre>
474 <p>
475 </p>
476 <p>
477 This function retrieves a record from the database. It seeks the
478 record with the supplied key, and populates the supplied structure
479 with the contents of the record. If no matching record is found, the
480 function returns DB_NOTFOUND.
481 </p>
482 <p>
483 </p>
484 <pre class="programlisting">int delete_person_key(DB *dbp, int person_key);</pre>
485 <p>
486 </p>
487 <p>
488 This function removes the record matching the given key.
489 </p>
490 <p>
491 </p>
492 <pre class="programlisting">typedef void (*person_iteration_callback)(void *user_data,
493 person_data *personp);
494
495 int person_full_iteration(DB *dbp,
496 person_iteration_callback user_func,
497 void *user_data); </pre>
498 <p>
499 </p>
500 <p>
501 This function performs a complete iteration over every record in
502 the person table. The user must provide a callback function
503 which is invoked once for every record found. The user's
504 callback function must match the prototype provided in the
505 typedef "person_iteration_callback." In the callback, the
506 "user_data" argument is passed unchanged from the "user_data"
507 argument given to person_full_iteration. This is provided
508 so that the caller of person_full_iteration can communicate
509 some context information to the callback function. The
510 "personp" argument to the callback is a pointer to the record
511 that was retrieved from the database. Personp points to data
512 that is valid only for the duration of the callback invocation.
513 </p>
514 <p>
515 </p>
516 <pre class="programlisting">int name_index_query_iteration(DB *secondary_dbp,
517 char *name_index_key,
518 person_iteration_callback user_func,
519 void *user_data); </pre>
520 <p>
521 </p>
522 <p>
523 This function performs lookups through the secondary index
524 database. Because duplicate keys are allowed in secondary
525 indexes, this query might return multiple instances. This
526 function takes as an argument a pointer to a user-written
527 callback function, which must match the function prototype
528 typedef mentioned above (person_iteration_callback). The
529 callback is invoked once for each record that matches the
530 secondary key.
531 </p>
532 </div>
533 <div class="sect2" lang="en" xml:lang="en">
534 <div class="titlepage">
535 <div>
536 <div>
537 <h3 class="title"><a id="idp10862328"></a>Test output</h3>
538 </div>
539 </div>
540 </div>
541 <p>
542 The test output file is useful as an example of how to invoke the
543 generated API. It will contain calls to the functions mentioned above,
544 to store a single record and retrieve it by primary key and through
545 the secondary index.
546 </p>
547 <p>
548 To compile the test, you would issue a command such as
549 </p>
550 <p>
551 </p>
552 <pre class="programlisting"> cc -I$BDB_INSTALL/include -L$BDB_INSTALL/lib -o test_people people.c \
553 test_people.c -ldb-4.8</pre>
554 <p>
555 </p>
556 <p>
557 This will produce the executable file test_people, which can be
558 run to exercise the generated API. The program generated from
559 people.sql will create a database environment in a directory
560 named "peopledb." This directory must be created before the
561 program is run.
562 </p>
563 </div>
564 </div>
565 <div class="navfooter">
566 <hr />
567 <table width="100%" summary="Navigation footer">
568 <tr>
569 <td width="40%" align="left"><a accesskey="p" href="db_replicate.html">Prev</a> </td>
570 <td width="20%" align="center">
571 <a accesskey="u" href="utilities.html">Up</a>
572 </td>
573 <td width="40%" align="right"> <a accesskey="n" href="dbsql.html">Next</a></td>
574 </tr>
575 <tr>
576 <td width="40%" align="left" valign="top">db_replicate </td>
577 <td width="20%" align="center">
578 <a accesskey="h" href="index.html">Home</a>
579 </td>
580 <td width="40%" align="right" valign="top"> dbsql</td>
581 </tr>
582 </table>
583 </div>
584 </body>
585 </html>