aboutsummaryrefslogtreecommitdiff
path: root/doc/manual.xhtml
blob: f49bed2bcc9bbc99c39a87e71b10da8130d8a0c8 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
  <title>C++ Object Persistence with ODB</title>

  <meta name="copyright" content="&copy; 2009-2010 Code Synthesis Tools CC"/>
  <meta name="keywords" content="odb,c++,object,persistence,ORM,relational,database,RDBMS,ODBMS,OODBMS"/>
  <meta name="description" content="C++ Object Persistence with ODB"/>
  <meta name="revision" content="1.0"/>
  <meta name="version" content="1.0.0"/>

<!--

If you make changes to this document, follow these stylistic rules
for consistency.

 - Don't use 'object' for instances of non-persistent classes. Use
   'instance' instead.

 - Each overloaded function should still be referred to as function.
   When saying that a function is overloaded, use term 'version',
   for example The persist() function has two overloaded versions.
   Don't use version to refer to individual functions, use function
   instead. The same holds for constructors.

 - Use 'object id' and 'object's identifier'. But not other combinations
   of the two.

-->

  <link rel="stylesheet" type="text/css" href="default.css" />

<style type="text/css">
  pre {
    padding    : 0 0 0 0em;
    margin     : 0em 0em 0em 0;

    font-size  : 102%
  }

  body {
    min-width: 48em;
  }

  h1 {
    font-weight: bold;
    font-size: 200%;
    line-height: 1.2em;
  }

  h2 {
    font-weight : bold;
    font-size   : 150%;

    padding-top : 0.8em;
  }

  h3 {
    font-size   : 140%;
    padding-top : 0.8em;
  }

  /* Adjust indentation for three levels. */
  #container {
    max-width: 48em;
  }

  #content {
    padding: 0 0.1em 0 4em;
    /*background-color: red;*/
  }

  #content h1 {
    margin-left: -2.06em;
  }

  #content h2 {
    margin-left: -1.33em;
  }

  /* Title page */

  #titlepage {
    padding: 2em 0 1em 0;
    border-bottom: 1px solid black;
  }

  #titlepage .title {
    font-weight: bold;
    font-size: 200%;
    text-align: center;
    padding: 1em 0 2em 0;
  }

  #titlepage p {
    padding-bottom: 1em;
  }

  #titlepage #revision {
    padding-bottom: 0em;
  }

  /* Lists */
  ul.list li, ol.list li {
    padding-top      : 0.3em;
    padding-bottom   : 0.3em;
  }

  div.img {
    text-align: center;
    padding: 2em 0 2em 0;
  }

  /*  */
  dl dt {
    padding   : 0.8em 0 0 0;
  }

  /* TOC */
  table.toc {
    border-style      : none;
    border-collapse   : separate;
    border-spacing    : 0;

    margin            : 0.2em 0 0.2em 0;
    padding           : 0 0 0 0;
  }

  table.toc tr {
    padding           : 0 0 0 0;
    margin            : 0 0 0 0;
  }

  table.toc * td, table.toc * th {
    border-style      : none;
    margin            : 0 0 0 0;
    vertical-align    : top;
  }

  table.toc * th {
    font-weight       : normal;
    padding           : 0em 0.1em 0em 0;
    text-align        : left;
    white-space       : nowrap;
  }

  table.toc * table.toc th {
    padding-left      : 1em;
  }

  table.toc * td {
    padding           : 0em 0 0em 0.7em;
    text-align        : left;
  }

  /* operators table */
  #operators {
    margin: 2em 0 2em 0;

    border-collapse   : collapse;
    border            : 1px solid;
    border-color      : #000000;

    font-size        : 11px;
    line-height      : 14px;
  }

  #operators th, #operators td {
    border: 1px solid;
    padding           : 0.9em 0.9em 0.7em 0.9em;
  }

  #operators th {
    background : #cde8f6;
  }

  #operators td {
    text-align: left;
  }

  /* specifiers table */
  #specifiers {
    margin: 2em 0 2em 0;

    border-collapse   : collapse;
    border            : 1px solid;
    border-color      : #000000;

    font-size        : 11px;
    line-height      : 14px;
  }

  #specifiers th, #specifiers td {
    border: 1px solid;
    padding           : 0.9em 0.9em 0.7em 0.9em;
  }

  #specifiers th {
    background : #cde8f6;
  }

  #specifiers td {
    text-align: left;
  }

  /* mapping table */
  #mapping {
    margin: 2em 0 2em 0;

    border-collapse   : collapse;
    border            : 1px solid;
    border-color      : #000000;

    font-size        : 11px;
    line-height      : 14px;
  }

  #mapping th, #mapping td {
    border: 1px solid;
    padding           : 0.9em 0.9em 0.7em 0.9em;
  }

  #mapping th {
    background : #cde8f6;
  }

  #mapping td {
    text-align: left;
  }

</style>


</head>

<body>
<div id="container">
  <div id="content">

  <div class="noprint">

  <div id="titlepage">
    <div class="title">C++ Object Persistence with ODB</div>

    <p>Copyright &copy; 2009-2010 Code Synthesis Tools CC</p>

    <p>Permission is granted to copy, distribute and/or modify this
    document under the terms of the
    <a href="http://www.codesynthesis.com/licenses/fdl-1.3.txt">GNU Free
    Documentation License, version 1.3</a>; with no Invariant Sections,
    no Front-Cover Texts and no Back-Cover Texts.</p>

    <!-- REMEMBER TO CHANGE VERSIONS IN THE META TAGS ABOVE! -->
    <p id="revision">Revision 1.0, September 2010</p>
    <p>This revision of the manual describes ODB 1.0.0 and is available
    in the following formats:
    <a href="http://www.codesynthesis.com/products/odb/doc/manual.xhtml">XHTML</a>,
    <a href="http://www.codesynthesis.com/products/odb/doc/odb-manual.pdf">PDF</a>, and
    <a href="http://www.codesynthesis.com/products/odb/doc/odb-manual.ps">PostScript</a>.</p>
  </div>

  <h1>Table of Contents</h1>

  <table class="toc">
    <tr>
      <th></th><td><a href="#0">Preface</a>
        <table class="toc">
          <tr><th></th><td><a href="#0.1">About This Document</a></td></tr>
	  <tr><th></th><td><a href="#0.2">More Information</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>1</th><td><a href="#1">Introduction</a>
        <table class="toc">
          <tr><th>1.1</th><td><a href="#1.1">Architecture and Workflow</a></td></tr>
	  <tr><th>1.2</th><td><a href="#1.2">Benefits</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>2</th><td><a href="#2">Hello World Example</a>
        <table class="toc">
          <tr><th>2.1</th><td><a href="#2.1">Declaring a Persistent Class</a></td></tr>
          <tr><th>2.2</th><td><a href="#2.2">Generating Database Support Code</a></td></tr>
          <tr><th>2.3</th><td><a href="#2.3">Compiling and Running</a></td></tr>
          <tr><th>2.4</th><td><a href="#2.4">Making Objects Persistent</a></td></tr>
          <tr><th>2.5</th><td><a href="#2.5">Querying the Database for Objects</a></td></tr>
          <tr><th>2.6</th><td><a href="#2.6">Updating Persistent Objects</a></td></tr>
          <tr><th>2.7</th><td><a href="#2.7">Deleting Persistent Objects</a></td></tr>
          <tr><th>2.8</th><td><a href="#2.8">Summary</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>3</th><td><a href="#3">Working with Persistent Objects</a>
        <table class="toc">
          <tr><th>3.1</th><td><a href="#3.1">Concepts and Terminology</a></td></tr>
          <tr><th>3.2</th><td><a href="#3.2">Database</a></td></tr>
          <tr><th>3.3</th><td><a href="#3.3">Transactions</a></td></tr>
          <tr><th>3.4</th><td><a href="#3.4">Making Objects Persistent</a></td></tr>
          <tr><th>3.5</th><td><a href="#3.5">Loading Persistent Objects</a></td></tr>
          <tr><th>3.6</th><td><a href="#3.6">Updating Persistent Objects</a></td></tr>
          <tr><th>3.7</th><td><a href="#3.7">Deleting Persistent Objects</a></td></tr>
          <tr><th>3.8</th><td><a href="#3.8">ODB Exceptions</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>4</th><td><a href="#4">Querying the Database</a>
        <table class="toc">
          <tr><th>4.1</th><td><a href="#4.1">ODB Query Language</a></td></tr>
          <tr><th>4.2</th><td><a href="#4.2">Parameter Binding</a></td></tr>
          <tr><th>4.3</th><td><a href="#4.3">Executing a Query</a></td></tr>
          <tr><th>4.4</th><td><a href="#4.4">Query Result</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>5</th><td><a href="#5">ODB Pragma Language</a>
        <table class="toc">
          <tr>
            <th>5.1</th><td><a href="#5.1">C++ Compiler Warnings</a>
              <table class="toc">
                <tr><th>5.1.1</th><td><a href="#5.1.1">GNU C++</a></td></tr>
                <tr><th>5.1.2</th><td><a href="#5.1.2">Visual C++</a></td></tr>
                <tr><th>5.1.3</th><td><a href="#5.1.3">Sun C++</a></td></tr>
		<tr><th>5.1.4</th><td><a href="#5.1.4">IBM XL C++</a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>5.2</th><td><a href="#5.2">Object Type Pragmas</a>
              <table class="toc">
                <tr><th>5.2.1</th><td><a href="#5.2.1"><code>table</code></a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>5.3</th><td><a href="#5.3">Value Type Pragmas</a>
              <table class="toc">
                <tr><th>5.3.1</th><td><a href="#5.3.1"><code>type</code></a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>5.4</th><td><a href="#5.4">Data Member Pragmas</a>
              <table class="toc">
                <tr><th>5.4.1</th><td><a href="#5.4.1"><code>id</code></a></td></tr>
                <tr><th>5.4.2</th><td><a href="#5.4.2"><code>auto</code></a></td></tr>
                <tr><th>5.4.3</th><td><a href="#5.4.3"><code>type</code></a></td></tr>
		<tr><th>5.4.4</th><td><a href="#5.4.4"><code>column</code></a></td></tr>
		<tr><th>5.4.5</th><td><a href="#5.4.5"><code>transient</code></a></td></tr>
              </table>
            </td>
          </tr>

        </table>
      </td>
    </tr>

    <tr>
      <th>6</th><td><a href="#6">Database Systems</a>
        <table class="toc">
          <tr>
            <th>6.1</th><td><a href="#6.1">MySQL Database</a>
              <table class="toc">
                <tr><th>6.1.1</th><td><a href="#6.1.1">MySQL Type Mapping</a></td></tr>
                <tr><th>6.1.2</th><td><a href="#6.1.2">MySQL Database Class</a></td></tr>
                <tr><th>6.1.3</th><td><a href="#6.1.3">Connection Factory</a></td></tr>
		<tr><th>6.1.4</th><td><a href="#6.1.4">MySQL Exceptions</a></td></tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>

  </table>
  </div>

  <h1><a name="0">Preface</a></h1>

  <p>As more critical aspects of our lives become dependant on software
     systems, more and more applications are required to save the data
     they work on in persistent and reliable storage. Database management
     systems and, in particular, relational database management systems
     (RDBMS) are commonly used for such storage. However, while the
     application development techniques and programming languages have
     evolved significantly over the past decades, the relational database
     technology in this area stayed relatively unchanged. In particular,
     this led to the now infamous mismatch between the object-oriented
     model used by many modern applications and the relational model still
     used by RDBMS.</p>

  <p>While relational databases may be inconvenient to use from modern
     programming languages, they are still the main choice for many
     applications due to their maturity, reliability, as well as the
     availability of tools and alternative implementations.</p>

  <p>To allow application developers to utilize relational databases
     from their object-oriented applications, a technique called
     object-relational mapping (ORM) is often used. It involves a
     conversion layer that maps between objects in the application's
     memory and their relational representation in the database. While
     the object-relational mapping code can be written manually,
     automated ORM systems are available for most object-oriented
     programming languages in use today.</p>

  <p>ODB is an ORM system for the C++ programming language. It was
     designed and implemented with the following main goals:</p>

  <ul class="list">
    <li>Provide a fully-automatic ORM system. In particular, the
        application developer should not have to manually write any
        mapping code, neither for persistent classes nor for their
        data member. </li>

    <li>Provide clean and easy to use object-oriented persistence
        model and database APIs that support the development of realistic
        applications for a wide variety of domains.</li>

    <li>Provide a portable and thread-safe implementation. ODB should be
        written in standard C++ and capable of persisting any standard
        C++ classes.</li>

    <li>Provide profiles that integrate ODB with type systems of
        widely-used frameworks and libraries such as Qt and Boost.</li>

    <li>Provide a high-performance and low overhead implementation. ODB
        should make efficient use of database and application resources.</li>

  </ul>


  <h2><a name="0.1">About This Document</a></h2>

  <p>The goal of this manual is to provide you with an understanding
     of the object persistence model and APIs which are implemented by ODB.
     As such, this document is intended for C++ application developers and
     software architects who are looking for a C++ object persistence
     solution. Prior experience with C++ is required to understand
     this document. A basic understanding of relational database systems
     is advantageous but not expected or required.</p>


  <h2><a name="0.2">More Information</a></h2>

  <p>Beyond this manual, you may also find the following sources of
     information useful:</p>

  <ul class="list">
    <li><a href="http://www.codesynthesis.com/products/odb/doc/odb.xhtml">ODB
        Compiler Command Line Manual.</a></li>

    <li>The <code>INSTALL</code> files in the ODB source packages provide
        build instructions for various platforms.</li>

    <li>The <code>odb-examples</code> package contains a collection of
        examples and a README file with an overview of each example.</li>

    <li>The <a href="http://www.codesynthesis.com/mailman/listinfo/odb-users">odb-users</a>
        mailing list is the place to ask technical questions about ODB.
        Furthermore, the searchable
        <a href="http://www.codesynthesis.com/pipermail/odb-users/">archives</a>
        may already have answers to some of your questions.</li>

  </ul>


  <!-- CHAPTER -->


  <h1><a name="1">1 Introduction</a></h1>

  <p>ODB is an object-relational mapping (ORM) system for C++. It provides
     tools, APIs, and library support that allow you to persist C++ objects
     to a relational database (RDBMS) without having to deal with tables,
     columns, or SQL and without manually writing any of the mapping code.</p>

  <p>ODB is highly flexible and customizable. It can either completely
     hide the relational nature of the underlying database or expose
     some of the details as required. For example, you can automatically
     map basic C++ types to suitable SQL types, generate the relational
     database schema for your persistent classes, and use simple, safe,
     and yet powerful object query language instead of SQL. Or you can
     assign SQL types to individual data members, use the existing
     database schema, and run native SQL <code>SELECT</code> queries.</p>

  <p>ODB is not a framework. It does not dictate how you should write
     your application. Rather, it is designed to fit into your
     style and architecture by only handling object persistence
     and not interfering with any other functionality. There is
     no common base type that all persistent classes should derive
     from nor are there any restrictions on the data member types
     in persistent classes. Existing classes can be made persistent
     with a few or no modifications.</p>

  <p>ODB has been designed for high performance and low memory
     overhead. Prepared statements are used to send and receive
     object state in binary format instead of text which reduces
     the load on the application and the database server. Extensive
     caching of connections, prepared statements, and buffers saves
     time and resources on connection establishment, statement parsing
     and memory allocations. For each supported database system the
     native C API is used instead of ODBC or higher-level wrapper
     APIs to reduce overhead and provide the most efficient implementation
     for each database operation. Finally, persistent classes have
     zero memory overhead. There are no hidden "database" members
     that each class must have nor are there per-object data structures
     allocated by ODB.</p>

  <p>In this chapter we present a high-level overview of ODB.
     We will start with the ODB architecture and then outline the
     workflow of building an application that uses ODB. We will
     conclude the chapter by contrasting the drawbacks of
     the traditional way of saving C++ objects to relational
     databases with the benefits of using ODB for object
     persistence. The next chapter takes a more hands-on approach
     and shows the concrete steps necessary to implement object
     persistence in a simple "Hello World" application.</p>

  <h2><a name="1.1">1.1 Architecture and Workflow</a></h2>

  <p>From the application developer's perspective, ODB
     consists of three main components: the ODB compiler, the common
     runtime library, called <code>libodb</code>, and the
     database-specific runtime libraries, called
     <code>libodb-&lt;database></code>, where &lt;database> is
     the name of the database system  this runtime
     is for, for example, <code>libodb-mysql</code>. For instance,
     if the application is going to use the MySQL database for
     object persistence, then the three ODB components that this
     application will use are the ODB compiler, <code>libodb</code>
     and <code>libodb-mysql</code>.</p>

  <p>The ODB compiler generates the database support code for
     persistent classes in your application. The input to the ODB
     compiler is one or more C++ header files defining C++ classes
     that you want to make persistent. For each input header file
     the ODB compiler generates a set of C++ source files implementing
     conversion between persistent C++ classes defined in this
     header and their database representation. The ODB compiler
     can also generate a database schema file that creates tables
     necessary to store the persistent classes.</p>

  <p>The ODB compiler is a real C++ compiler except that it produces
     C++ instead of assembly or machine code. In particular, it is not
     an ad-hoc header pre-processor that is only capable of recognizing
     a subset of C++. ODB is capable of parsing any standard C++ code.</p>

  <p>The common runtime library defines database system-independent
     interfaces that your application can use to manipulate persistent
     objects. The database-specific runtime library provides implementations
     of these interfaces for a concrete database as well as other
     database-specific utilities that are used by the generated code.
     Normally, the application does not use the database-specific
     runtime library directly but rather works with it via the common
     interfaces from <code>libodb</code>. The following diagram shows
     the object persistence architecture of an application that uses
     MySQL as the underlying database system:</p>

  <!-- align=center is needed for html2ps -->
  <div class="img" align="center"><img src="odb-arch.png"/></div>

  <p>The ODB system also defines two special-purpose languages:
     the ODB Pragma Language and ODB Query Language. The ODB Pragma
     Language is used to communicate various properties of persistent
     classes to the ODB compiler by means of special <code>#pragma</code>
     directives embedded in the C++ header files. It controls aspects
     of the object-relational mapping such as names of tables and columns
     that are used for persistent classes and their members or mapping between
     C++ types and database types.</p>

  <p>The ODB Query Language is an object-oriented database query
     language that can be used to search for objects matching
     certain criteria. It is modeled after and is integrated into
     C++ allowing you to write expressive and safe queries that look
     and feel like ordinary C++.</p>

  <p>The use of the ODB compiler to generate database support code
     adds an additional step to your application build sequence. The
     following diagram outlines the typical build workflow of an
     application that uses ODB:</p>

  <!-- align=center is needed for html2ps -->
  <div class="img" align="center"><img src="odb-flow.png"/></div>

  <h2><a name="1.2">1.2 Benefits</a></h2>

  <p>The traditional way of saving C++ objects to relational databases
     requires that you manually write code which converts between the database
     and C++ representations of each persistent class. The actions that
     such code usually performs include conversion between C++ values and
     strings or database types, preparation and execution of SQL queries,
     as well as handling the result sets. Writing this code manually has
     the following drawbacks:</p>

  <ul class="list">
    <li><b>Difficult and time consuming.</b> Writing database conversion
        code for any non-trivial application requires extensive
        knowledge of the specific database system and its APIs.
        It can also take a considerable amount of time to write
        and maintain. Supporting multi-threaded applications can
        complicate this task even further.</li>

    <li><b>Suboptimal performance.</b> Optimal conversion often
        requires writing large amounts of extra code, such as
        parameter binding for prepared statements and caching
        of connections, statements, and buffers. Writing code
        like this in an ad-hoc manner is often too difficult
        and time consuming.</li>

    <li><b>Database vendor lock-in.</b> The conversion code is written for
        a specific database which makes it hard to switch to another
        database vendor.</li>

    <li><b>Lack of type safety.</b> It is easy to misspell column names or
        pass incompatible values in SQL queries. Such errors will
        only be detected at runtime.</li>

    <li><b>Complicates the application.</b> The database conversion code
        often ends up interspersed throughout the application making it
        hard to debug, change, and maintain.</li>
  </ul>

  <p>In contrast, using ODB for C++ object persistence has the
     following benefits:</p>

  <ul class="list">
    <li><b>Ease of use.</b> ODB automatically generates database conversion
        code from your C++ class declarations and allows you to manipulate
        persistent objects using simple and thread-safe object-oriented
        database APIs.</li>

    <li><b>Concise code.</b> With ODB hiding the details of the underlying
        database, the application logic is written using the natural object
        vocabulary instead of tables, columns and SQL. The resulting code
        is simpler and thus easier to read and understand.</li>

    <li><b>Optimal performance.</b> ODB has been designed for high performance
        and low memory overhead. All the available optimization techniques,
        such as prepared statements and extensive connection, statement,
        and buffer caching, are used to provide the most efficient
        implementation for each database operation.</li>

    <li><b>Database portability.</b> Because the database conversion code
        is automatically generated, it is easy to switch from one database
        vendor to another. In fact, it is possible to test your application
        on several database systems before making a choice.</li>

    <li><b>Safety.</b> The ODB object persistence and query APIs are
        statically typed. You use C++ identifiers instead of strings
        to refer to object members and the generated code makes sure
        database and C++ types are compatible. All this helps catch
        programming errors at compile-time rather than at runtime.</li>

    <li><b>Maintainability.</b> Automatic code generation minimizes the
        effort needed to adapt the application to changes in persistent
        classes. The database support code is kept separately from the
        class declarations and application logic. This makes the
        application easier to debug and maintain.</li>
  </ul>

  <p>Overall, ODB provides an easy to use yet flexible and powerful
     object-relational mapping (ORM) system for C++. Unlike other
     ORM implementations for C++ that still require you to write
     database conversion or member registration code for each
     persistent class, ODB keeps persistent classes purely
     declarative. The functional part, the database conversion
     code, is automatically generated by the ODB compiler from
     these declarations.</p>


  <!-- CHAPTER -->


  <h1><a name="2">2 Hello World Example</a></h1>

  <p>In this chapter we will show how to create a simple C++
     application that relies on ODB for object persistence using
     the traditional "Hello World" example. In particular, we will
     discuss how to declare persistent classes, generate database
     support code, as well as compile and run our application. We
     will also learn how to make objects persistent, load, update
     and delete persistent objects, as well as query the database
     for persistent objects that match certain criteria.</p>

  <p>The code presented in this chapter is based on the
     <code>hello</code> example which can be found in the
     <code>odb-examples</code> package of the ODB distribution.</p>

  <h2><a name="2.1">2.1 Declaring a Persistent Class</a></h2>

  <p>In our "Hello World" example we will depart slightly from
     the norm and say hello to people instead of the world. People
     in our application will be represented as objects of C++ class
     <code>person</code> which is saved in <code>person.hxx</code>:</p>

  <pre class="c++">
// person.hxx
//

#include &lt;string>

class person
{
public:
  person (const std::string&amp; first,
          const std::string&amp; last,
          unsigned short age);

  const std::string&amp;
  first () const;

  const std::string&amp;
  last () const;

  unsigned short
  age () const;

  void
  age (unsigned short);

private:
  std::string first_;
  std::string last_;
  unsigned short age_;
};
  </pre>

  <p>In order not to miss anyone whom we need to greet, we would like
  to save the <code>person</code> objects in a database. To achieve this
  we declare the <code>person</code> class as persistent:</p>

  <pre class="c++">
// person.hxx
//

#include &lt;string>

#include &lt;odb/core.hxx>     // (1)

#pragma db object           // (2)
class person
{
  ...

private:
  person () {}              // (3)

  friend class odb::access; // (4)

  #pragma db id auto        // (5)
  unsigned long id_;        // (5)

  std::string first_;
  std::string last_;
  unsigned short age_;
};
  </pre>

  <p>To be able to save the <code>person</code> objects in the database
     we had to make five changes, marked with (1) to (5), to the original
     class definition. The first change is the inclusion of the ODB
     header <code>&lt;odb/core.hxx></code>. This header provides a number
     of core ODB declarations, such as <code>odb::access</code>, that
     are used to define persistent classes.</p>

  <p>The second change is the addition of <code>db object</code>
     pragma just before the class definition. This pragma tells the
     ODB compiler that the class that follows is persistent. Note
     that making a class persistent does not mean that all objects
     of this class will automatically be stored in the database.
     You would still create ordinary or <em>transient</em> instances
     of this class just as you would before. The difference is that
     now you can make such transient instances persistent, as we will
     see shortly.</p>

  <p>The third change is the addition of the default constructor.
     The ODB-generated database support code will use this constructor
     when instantiating an object from the persistent state. Just as we have
     done for the <code>person</code> class, you can make the default
     constructor private or protected if you don't want to make it
     available to the users of your class.</p>

  <p>With the fourth change we make the <code>odb::access</code> class a
     friend of our <code>person</code> class. This is necessary to make
     the default constructor and the data members accessible to the
     ODB support code. If your class has public default constructor and
     public data members, then the <code>friend</code> declaration is
     unnecessary.</p>

  <p>The final change adds a data member called <code>id_</code> which
     is preceded by another pragma. In ODB every persistent object must
     have a unique, within its class, identifier. Or, in other words,
     no two persistent instances of the same type have equal
     identifiers. For our class we use an integer id. The
     <code>db id auto</code> pragma that precedes the <code>id_</code>
     member tells the ODB compiler that the following member is the
     object's identifier. The <code>auto</code> specifier indicates that it
     is a database-assigned id. A unique id will be automatically generated
     by the database and assigned to the object when it is made
     persistent.</p>

  <p>In this example we chose to add an identifier because none of
     the existing members could serve the same purpose. However, if
     a class already has a member with suitable properties, then it
     is natural to use that member as an identifier. For example,
     if our <code>person</code> class contained some form of personal
     identification (SSN in the United States or ID/passport number
     in other countries), then we could use that as an id. Or, if
     we stored an email associated with each person, then we could
     have used that since each person is presumed to have a unique
     email address, for example:</p>

  <pre class="c++">
class person
{
  ...

  #pragma db id
  std::string email_;

  std::string first_;
  std::string last_;
  unsigned short age_;
};
  </pre>

  <p>Now that we have the header file with the persistent class, let's
     see how we can generate that database support code.</p>

  <h2><a name="2.2">2.2 Generating Database Support Code</a></h2>

  <p>The persistent class definition that we created in the previous
     section was particularly light on any code that could actually
     do the job and store the person's data to a database. There
     was no serialization or deserialization code, not even data member
     registration, that you would normally have to write by hand in
     other ORM libraries for C++. This is because in ODB code
     that translates between the database and C++ representations
     of an object is automatically generated by the ODB compiler.</p>

  <p>To compile the <code>person.hxx</code> header we created in the
     previous section and generate the support code for the MySQL
     database, we invoke the ODB compiler from a terminal (UNIX) or
     a command prompt (Windows):</p>

  <pre class="terminal">
odb -d mysql --generate-query person.hxx
  </pre>

  <p>We will use MySQL as the database of choice in the remainder of
     this chapter, though other supported database systems can be used
     instead.</p>

  <p>If you haven't installed the common ODB runtime library
     (<code>libodb</code>) or installed it into a directory where
     C++ compilers don't search for headers by default,
     then you may get the following error:</p>

  <pre class="terminal">
person.hxx:10:24: fatal error: odb/core.hxx: No such file or directory
  </pre>

  <p>To resolve this you will need to specify the <code>libodb</code> headers
     location with the <code>-I</code> preprocessor option, for example:</p>

  <pre class="terminal">
odb -I.../libodb -d mysql --generate-query person.hxx
  </pre>

  <p>Here <code>.../libodb</code> represents the path to the
     <code>libodb</code> directory.</p>

  <p>The above invocation of the ODB compiler produces three C++ files:
     <code>person-odb.hxx</code>, <code>person-odb.ixx</code>,
     <code>person-odb.cxx</code>. You normally don't use types
     or functions contained in these files directly. Rather, all
     you have to do is include <code>person-odb.hxx</code> in
     C++ files where you are performing database operations
     with classes from <code>person.hxx</code> as well as compile
     <code>person-odb.cxx</code> and link the resulting object
     file to your application.</p>

  <p>You may be wondering what the <code>--generate-query</code>
     option is for. It instructs the ODB compiler to generate
     optional query support code that we will use later in our
     "Hello World" example. Another option that we will find
     useful is <code>--generate-schema</code>. This option
     makes the ODB compiler generate a fourth file,
     <code>person.sql</code>, which is the database schema
     for the persistent classes defined in <code>person.hxx</code>:</p>

  <pre class="terminal">
odb -d mysql --generate-query --generate-schema person.hxx
  </pre>

  <p>The database schema file contains SQL statements that creates
     tables necessary to store the persistent classes. We will learn
     how to use it in the next section.</p>

  <p>If you would like to see a list of all the available ODB compiler
     options, refer to the
     <a href="http://www.codesynthesis.com/products/odb/doc/odb.xhtml">ODB
     Compiler Command Line Manual</a>.</p>

  <p>Now that we have the persistent class and the database support
     code, the only part that is left is the application code that
     does something useful with all of this. But before we move on to
     the fun part, let's first learn how to build and run an application
     that uses ODB. This way when we have some application code
     to try, there are no more delays before we can run it.</p>

  <h2><a name="2.3">2.3 Compiling and Running</a></h2>

  <p>Assuming that the <code>main()</code> function with the application
     code is saved in <code>driver.cxx</code> and the database support
     code and schema are generated as described in the previous section,
     to build our application we will first need to compile all the C++
     source files and then link them with two ODB runtime libraries.</p>

  <p>On UNIX, the compilation part can be done with the following commands
     (substitute <code>c++</code> with your C++ compiler name; for Microsoft
     Visual Studio setup, see the <code>odb-examples</code> package):</p>

  <pre class="terminal">
c++ -c driver.cxx
c++ -c person-odb.cxx
  </pre>

  <p>Similar to the ODB compilation, if you get an error stating that
  a header in <code>odb/</code> or <code>odb/mysql</code> directory
  is not found, you will need to use the <code>-I</code>
  preprocessor option to specify the location of the common ODB runtime
  library (<code>libodb</code>) and MySQL ODB runtime library
  (<code>libodb-mysql</code>).</p>

  <p>Once the compilation is done, we can link the application with
  the following command:</p>

  <pre class="terminal">
c++ -o driver driver.o person-odb.o -lodb-mysql -lodb
  </pre>

  <p>Notice that we link our application with two ODB libraries:
    <code>libodb</code> which is a common runtime library and
    <code>libodb-mysql</code> which is a MySQL runtime library
    (if you use another database, then the name of this library
    will change accordingly). If you get an error saying that
    one of these libraries could not be found, then you will need
    to use the <code>-L</code> linker option to specify their locations.</p>

  <p>Before we can run our application we need to create a database
    schema using the generated <code>person.sql</code> file. For MySQL
    we can use the <code>mysql</code> client program, for example:</p>

  <pre class="terminal">
mysql --user=odb_test --database=odb_test &lt; person.sql
  </pre>

  <p>The above command will log in to a local MySQL server as user
    <code>odb_test</code> without a password and use the database
    named <code>odb_test</code>. Note that after executing this
    command, all the data stored in the <code>odb_test</code> database
    will be deleted.</p>

  <p>Once the database schema is ready, we run our application
  using the same login and database name:</p>

  <pre class="terminal">
./driver --user odb_test --database odb_test
  </pre>


  <h2><a name="2.4">2.4 Making Objects Persistent</a></h2>

  <p>Now that we have the infrastructure work out of the way, it
  is time to see our first code fragment that interacts with the
  database. In this section we will learn how to make <code>person</code>
  objects persistent:</p>

  <pre class="c++">
// driver.cxx
//

#include &lt;memory>   // std::auto_ptr
#include &lt;iostream>

#include &lt;odb/database.hxx>
#include &lt;odb/transaction.hxx>

#include &lt;odb/mysql/database.hxx>

#include "person.hxx"
#include "person-odb.hxx"

using namespace std;
using namespace odb;

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr&lt;database> db (new mysql::database (argc, argv));

    unsigned long john_id, jane_id, joe_id;

    // Create a few persistent person objects.
    //
    {
      person john ("John", "Doe", 33);
      person jane ("Jane", "Doe", 32);
      person joe ("Joe", "Dirt", 30);

      transaction t (db->begin ());

      db->persist (john);
      db->persist (jane);
      db->persist (joe);

      t.commit ();

      // Save object ids for later use.
      //
      john_id = john.id ();
      jane_id = jane.id ();
      joe_id = joe.id ();
    }
  }
  catch (const odb::exception&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>Let's examine this code piece by piece. At the beginning we include
     a bunch of headers. After the standard C++ headers we include
     <code>&lt;odb/database.hxx></code>
     and <code>&lt;odb/transaction.hxx></code> which define database
     system-independent <code>odb::database</code> and
     <code>odb::transaction</code> interfaces. Then we include
     <code>&lt;odb/mysql/database.hxx></code> which defines the
     MySQL implementation of the <code>database</code> interface. Finally,
     we include <code>person.hxx</code> and <code>person-odb.hxx</code>
     which define our persistent <code>person</code> class.</p>

  <p>Once we are in <code>main()</code>, the first thing we do is create
     the MySQL database object. Notice that this is the last line in
     <code>driver.cxx</code> that mentions MySQL explicitly; the rest
     of the code works through the common interfaces and is database
     system-independent. We use the <code>argc</code>/<code>argv</code>
     <code>mysql::database</code> constructor which automatically
     extract the database parameters, such as login name, password,
     database name, etc., from the command line. In your own applications
     you may prefer to use other <code>mysql::database</code>
     constructors which allow you to pass this information directly
     (see <a href="#6.1.2">Section 6.1.2, "MySQL Database Class"</a>).</p>

  <p>Next, we create three <code>person</code> objects. Right now they are
     transient objects, which means that if we terminate the application
     at this point, they will be gone without any evidence of them ever
     existing. The next line starts a database transaction. We discuss
     transactions in detail later in this manual. For now, all we need
     to know is that all ODB database operations must be performed within
     a transaction and that a transaction is an atomic unit of work; all
     database operations performed within a transaction either succeed
     (committed) together or are automatically undone (rolled back).</p>

  <p>Once we are in a transaction, we call the <code>persist()</code>
     database function on each of our <code>person</code> objects.
     At this point the state of each object is saved in the database.
     However, note that this state is not permanent until and unless
     the transaction is committed. If, for example, our application
     crashes at this point, there will still be no evidence of our
     objects ever existing.</p>

  <p>In our case, one more thing happens when we call <code>persist()</code>.
     Remember that we decided to use database-assigned identifiers for our
     <code>person</code> objects. The call to <code>persist()</code> is
     where this assignment happens. Once this function returns, the
     <code>id_</code> member contains this object's unique identifier.</p>

  <p>After we have persisted our objects, it is time to commit the
     transaction and make the changes permanent. Only after the
     <code>commit()</code> function returns successfully, are we
     guaranteed that the objects are made persistent. Continuing
     with the crash example, if our application terminates after
     the commit for whatever reason, the objects' state in the
     database will remain intact. In fact, as we will discover
     shortly, our application can be restarted and load the
     original objects from the database. Note also that a
     transaction must be committed explicitly with the
     <code>commit()</code> call. If the <code>transaction</code>
     object leaves scope without the transaction being
     explicitly committed or rolled back, it will automatically be
     rolled back. This behavior allows you not to worry about
     exceptions being thrown within a transaction; if they
     cross the transaction boundary, the transaction will
     automatically be rolled back and all the changes made
     to the database undone.</p>

  <p>After the transaction has been committed, we save the
     objects' identifiers in local variables. We will use them later in this
     chapter to perform other database operations on our persistent
     objects. You might have noticed that our <code>person</code>
     class doesn't have the <code>id()</code> function that we use
     here. To make our code compile we need to add a simple accessor
     with this name that returns the value of the <code>id_</code>
     data member.</p>

  <p>The final bit of code in our example is the <code>catch</code>
     block that handles the database exceptions. We do this by catching
     the base ODB exception (see <a href="#3.8">Section 3.8, "ODB
     Exceptions"</a>) and printing the diagnostics.</p>

  <p>Let's now compile (see <a href="#2.3">Section 2.3, "Compiling and
     Running"</a>) and then run our first ODB application:</p>

  <pre class="terminal">
mysql --user=odb_test --database=odb_test &lt; person.sql
./driver --user odb_test --database odb_test
  </pre>

  <p>Our first application doesn't print anything except for error
     messages so we can't really tell whether it actually stored the
     objects' state in the database. While we will make our application
     more entertaining shortly, for now we can use the <code>mysql</code>
     client to examine the database content. It will also give us a feel
     for how the objects are stored:</p>

  <pre class="terminal">
mysql --user=odb_test --database=odb_test

Welcome to the MySQL monitor.

mysql> select * from person;

+----+-------+------+-----+
| id | first | last | age |
+----+-------+------+-----+
|  1 | John  | Doe  |  33 |
|  2 | Jane  | Doe  |  32 |
|  3 | Joe   | Dirt |  30 |
+----+-------+------+-----+
3 rows in set (0.00 sec)

mysql> quit
  </pre>

  <p>In the next section we will see how to access persistent objects
     from our application.</p>

  <h2><a name="2.5">2.5 Querying the Database for Objects</a></h2>

  <p>So far our application doesn't resemble a typical "Hello World"
     example. It doesn't print anything except for error messages.
     Let's change that and teach our application to say hello to
     people from our database. To make it a bit more interesting,
     let's say hello only to people over 30:</p>

  <pre class="c++">
// driver.cxx
//

...

int
main (int argc, char* argv[])
{
  try
  {
    ...

    // Create a few persistent person objects.
    //
    {
      ...
    }

    typedef odb::query&lt;person> query;
    typedef odb::result&lt;person> result;

    // Say hello to those over 30.
    //
    {
      transaction t (db->begin ());

      result r (db->query&lt;person> (query::age > 30));

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
      {
        cout &lt;&lt; "Hello, " &lt;&lt; i->first () &lt;&lt; "!" &lt;&lt; endl;
      }

      t.commit ();
    }
  }
  catch (const odb::exception&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>The first half of our application is the same as before and is
     replaced with "..." in the above listing for brevity. Again, let's
     examine the rest of it piece by piece.</p>

  <p>The two <code>typedef</code>s create convenient aliases for two
     template instantiations that will be used a lot in our application.
     The first is the query type for the <code>person</code> objects
     and the second is the result type for that query.</p>

  <p>Then we begin a new transaction and call the <code>query()</code>
     database function. We pass a query expression
     (<code>query::age > 30</code>) which limits the returned objects
     only to those with the age greater than 30. We also save the result
     of the query in a local variable.</p>

  <p>The next few lines perform a standard for-loop iteration
     over the result sequence printing hello for every returned person.
     Then we commit the transaction and that's it. Let's see what
     this application will print:</p>

  <pre class="terminal">
mysql --user=odb_test --database=odb_test &lt; person.sql
./driver --user odb_test --database odb_test

Hello, John!
Hello, Jane!
  </pre>


  <p>That looks about right, but how do we know that the query actually
     used the database instead of just using some in-memory artifacts of
     the earlier <code>persist()</code> calls? One way to test this
     would be to comment out the first transaction in our application
     and re-run it without re-creating the database schema. This way the
     objects that were persisted during the previous run will be returned.
     Alternatively, we can just re-run the same application without
     re-creating the schema and notice that we now show duplicate
     objects:</p>

  <pre class="terminal">
./driver --user odb_test --database odb_test

Hello, John!
Hello, Jane!
Hello, John!
Hello, Jane!
  </pre>

  <p>What happens here is that the previous run of our application
     persisted a set of <code>person</code> objects and when we re-run
     the application, we persist another set with the same names but
     with different ids. When we later run the query, matches from
     both sets are returned. We can change the line where we print
     the "Hello" string as follows to illustrate this point:</p>

  <pre class="c++">
cout &lt;&lt; "Hello, " &lt;&lt; i->first () &lt;&lt; " (" &lt;&lt; i->id () &lt;&lt; ")!" &lt;&lt; endl;
  </pre>

  <p>If we now re-run this modified program, again without re-creating
     the database schema, we will get the following output:</p>

  <pre class="terminal">
./driver --user odb_test --database odb_test

Hello, John (1)!
Hello, Jane (2)!
Hello, John (4)!
Hello, Jane (5)!
Hello, John (7)!
Hello, Jane (8)!
  </pre>

  <p>The identifiers 3, 6, and 9 that are missing from the above list belong
     to the "Joe Dirt" objects which are not selected by this query.</p>

  <h2><a name="2.6">2.6 Updating Persistent Objects</a></h2>

  <p>While making objects persistent and then selecting some of them using
     queries are two useful operations, most applications will also need
     to change the object's state and then make these changes persistent.
     Let's illustrate this by updating Joe's age who just had a birthday:</p>

  <pre class="c++">
// driver.cxx
//

...

int
main (int argc, char* argv[])
{
  try
  {
    ...

    unsigned long john_id, jane_id, joe_id;

    // Create a few persistent person objects.
    //
    {
      ...

      // Save object ids for later use.
      //
      john_id = john.id ();
      jane_id = jane.id ();
      joe_id = joe.id ();
    }

    // Joe Dirt just had a birthday, so update his age.
    //
    {
      transaction t (db->begin ());

      auto_ptr&lt;person> joe (db->load&lt;person> (joe_id));
      joe->age (joe->age () + 1);
      db->update (*joe);

      t.commit ();
    }

    // Say hello to those over 30.
    //
    {
      ...
    }
  }
  catch (const odb::exception&amp; e)
  {
    cerr &lt;&lt; e.what () &lt;&lt; endl;
    return 1;
  }
}
  </pre>

  <p>The beginning and the end of the new transaction are the same as
     the previous two. Once within a transaction, we call the
     <code>load()</code> database function to instantiate a
     <code>person</code> object with Joe's persistent state. We
     pass Joe's object identifier that we stored earlier when we
     made this object persistent.</p>

  <p>With the instantiated object in hand we increment the age
     and call the <code>update()</code> function to update
     the object's state in the database. Once the transaction is
     committed, the changes are made permanent.</p>

  <p>If we now run this application, we will see Joe in the output
     since he is now over 30:</p>

  <pre class="terminal">
mysql --user=odb_test --database=odb_test &lt; person.sql
./driver --user odb_test --database odb_test

Hello, John!
Hello, Jane!
Hello, Joe!
  </pre>

  <p>What if we didn't have an identifier for Joe? Maybe this object
     was made persistent in another run of our application or by another
     application altogether. Provided that we only have one Joe Dirt
     in the database, we can use the query facility to come up with
     an alternative implementation of the above transaction:</p>

  <pre class="c++">
    // Joe Dirt just had a birthday, so update his age. An
    // alternative implementation without using the object id.
    //
    {
      transaction t (db->begin ());

      result r (db->query&lt;person> (query::first == "Joe" &amp;&amp;
                                   query::last == "Dirt"));

      result::iterator i (r.begin ());

      if (i != r.end ())
      {
        auto_ptr&lt;person> joe (i.load ());
        joe->age (joe->age () + 1);
        db->update (*joe);
      }

      t.commit ();
    }
  </pre>

  <h2><a name="2.7">2.7 Deleting Persistent Objects</a></h2>

  <p>The last operation that we will discuss in this chapter is deleting
     the persistent object from the database. The following code
     fragment shows how we can delete an object given its identifier:</p>

  <pre class="c++">
    // John Doe is no longer in our database.
    //
    {
      transaction t (db->begin ());
      db->erase&lt;person> (john_id);
      t.commit ();
    }
  </pre>

  <p>To delete John from the database we start a transaction, call
     the <code>erase()</code> database function with John's object
     id, and commit the transaction. After the transaction is committed,
     the erased object is no longer persistent.</p>

  <p>If we don't have an object id handy, we can use queries to find
     and delete the object:</p>

  <pre class="c++">
    // John Doe is no longer in our database. An alternative
    // implementation without using the object id.
    //
    {
      transaction t (db->begin ());

      result r (db->query&lt;person> (query::first == "John" &amp;&amp;
                                   query::last == "Doe"));

      result::iterator i (r.begin ());

      if (i != r.end ())
      {
        auto_ptr&lt;person> john (i.load ());
        db->erase (*john);
      }

      t.commit ();
    }
  </pre>

  <h2><a name="2.8">2.8 Summary</a></h2>

  <p>This chapter presented a very simple application which, nevertheless,
     exercised all of the core database functions: <code>persist()</code>,
     <code>query()</code>, <code>load()</code>, <code>update()</code>,
     and <code>erase()</code>. We also saw that writing an application
     that uses ODB involves the following steps:</p>

  <ol>
    <li>Declare persistent classes in header files.</li>
    <li>Compile these headers to generate database support code.</li>
    <li>Link the application with the generated code and two ODB runtime
        libraries.</li>
  </ol>



  <p>Do not be concerned if, at this point, much appears unclear. The intent
     of this chapter is to give you only a general idea of how to persist C++
     objects with ODB. We will cover all the details throughout the remainder
     of this manual.</p>


  <!-- CHAPTER -->


  <h1><a name="3">3 Working with Persistent Objects</a></h1>

  <p>The previous chapters gave us a high-level overview of ODB and
     showed how to use it to store C++ objects in a database. In this
     chapter we will examine the ODB object persistence model as
     well as the core database APIs in greater detail. We will
     start with basic concepts and terminology in <a href="#3.1">Section 3.1</a>
     and continue with the discussion of the <code>odb::database</code>
     class in <a href="#3.2">Section 3.2</a> and transactions in
     <a href="#3.3">Section 3.3</a>. The remainder of this chapter
     deals with the core database operations and concludes with
     the discussion of ODB exceptions.</p>

  <p>In this chapter we will continue to use and expand the
     <code>person</code> persistent class that we have developed in the
     previous chapter.</p>

  <h2><a name="3.1">3.1 Concepts and Terminology</a></h2>

  <p>The term <em>database</em> can refer to three distinct things:
     a general notion of a place where an application stores its data,
     a software implementation for managing this data (for example
     MySQL), and, finally, some database software implementations
     may manage several data stores which are usually distinguished
     by name. This name is also commonly referred to as a database.</p>

  <p>In this manual, when we use the word <em>database</em>, we
     refer to the first meaning above, for example,
     "The <code>update()</code> function saves the object's state to
     the database." The term Database Management System (DBMS) is
     often used to refer to the second meaning of the word database.
     In this manual we will use the term <em>database system</em>
     for short, for example, "Database system-independent
     application code." Finally, to distinguish the third meaning
     from the other two, we will use the term <em>database name</em>,
     for example, "The second option specifies the database name
     that the application should use to store its data."</p>

  <p>In C++ there is only one notion of a type and an instance
     of a type. For example, a fundamental type, such as <code>int</code>,
     is, for the most part, treated the same as a user defined class
     type. However, when it comes to persistence, we have to place
     certain restrictions and requirements on certain C++ types that
     can be stored in the database. As a result, we divide persistent
     C++ types into two groups: <em>object types</em> and <em>value
     types</em>. An instance of an object type is called an <em>object</em>
     and an instance of a value type &mdash; a <em>value</em>.</p>

  <p>An object is an independent entity. It can be stored, updated,
     and deleted in the database independent of other objects or values.
     An object has an identifier, called <em>object id</em>, that is
     unique among all instances of an object type within a database.
     An object consists of data members which are either values or
     references to other objects. In contrast, a value can only be
     stored in the database as part of an object and doesn't have
     its own unique identifier.</p>

  <p>An object type is a C++ class. Because of this one-to-one
     relationship, we will use terms <em>object type</em>
     and <em>object class</em> interchangeably. In contrast,
     a value type can be a fundamental C++ type, such as
     <code>int</code> or a class type, such as <code>std::string</code>.
     If a value consists of other values, then it is called a
     <em>composite value</em> and its type &mdash; a
     <em>composite value type</em>. Otherwise, the value is
     called <em>simple value</em> and its type &mdash; a
     <em>simple value type</em>. Note that the distinction between
     simple and composite values is conceptual rather than
     representational. For example, <code>std::string</code>
     is a simple value type because conceptually string is a
     single value even though the representation of the string
     class may contain several data members each of which could be
     considered a value. In fact, the same value type can be
     viewed (and mapped) as both simple and composite by different
     applications.</p>

  <p>Seeing how all these concepts map to the relational model
     will hopefully make these distinctions clearer. In a relational
     database an object type is mapped to a table and a value type is
     mapped to one or more columns. A simple value type is mapped
     to a single column while a composite value type is mapped to
     several columns. An object is stored as a row in this
     table and a value is stored as one or more cells in this row.
     A simple value is stored in a single cell while a composite
     value occupies several cells.</p>

  <p>Going back to the distinction between simple and composite
     values, consider a date type which has three integer
     members: year, month, and day. In one application it can be
     considered a composite value and each member will get its
     own column in a relational database. In another application
     it can be considered a simple value and stored in a single
     column as a number of days from some predefined date.</p>

  <p>Until now, we have been using the term <em>persistent class</em>
     to refer to object classes. We will continue to do so even though
     a value type can also be a class. The reason for this asymmetry
     is the subordinate nature of value types when it comes to
     database operations. Remember that values are never stored
     directly but rather as part of an object that contains them.
     As a result, when we say that we want to make a C++ class
     persistent or persist an instance of a class in the database,
     we invariably refer to an object class rather than a value
     class.</p>

  <p>To make a C++ class a persistent object class we declare
     it as such using the <code>db&nbsp;object</code> pragma, for
     example:</p>

  <pre class="c++">
    #pragma db object
    class person
    {
      ...
    };
  </pre>

  <p>The other pragma that we often use is <code>db&nbsp;id</code>
     which designates one of the data members as an object id, for
     example:</p>

  <pre class="c++">
    #pragma db object
    class person
    {

    private:
      #pragma db id
      unsigned long id_;
    };
  </pre>

  <p>These two pragmas are the minimum required to declare a
     persistent class. Other pragmas can be used to fine-tune
     the database-related properties of a class and its
     members (see <a href="#5">Chapter 5, "ODB Pragma Language"</a>).</p>

  <p>You may be wondering whether we also have to declare value types
     as persistent. We don't need to do anything special for simple value
     types such as <code>int</code> or <code>std::string</code> since the
     ODB compiler knows how to map them to suitable database system types and
     how to convert between the two. On the other hand, if a simple value
     is unknown to the ODB compiler then you will need to provide the
     mapping to the database system type and, possibly, the code to
     convert between the two. For more information on this refer to
     <a href="#5.3">Section 5.3, "Value Type Pragmas"</a>. Composite
     value types are not yet supported by ODB and we will not discuss
     them further in this revision of the manual.</p>

  <p>Normally, you would use object types to model real-world entities,
     things that have their own identity. For example, in the
     previous chapter we created a <code>person</code> class to model
     a person, which is a real-world entity. Name and age, which we
     used as data members in our <code>person</code> class are clearly
     values. It is hard to think of age 31 or name "Joe" as having their
     own identities.</p>

  <p>A good test to determine whether something is an object or
     a value, is to consider if other objects might reference
     it. A person is clearly an object because it can be referred
     to by other objects such as a spouse, an employer, or a
     bank. On the other hand, a person's age or name is not
     something that other objects would normally refer to.</p>

  <p>Also, when an object represents a real entity, it is easy to
     choose a suitable object id. For example, for a
     person there is an established notion of an identifier
     (SSN, student id, passport number, etc). Another alternative
     is to use a person's email address as an identifier.</p>

  <p>Note, however, that these are only guidelines. There could
     be good reasons to make something that would normally be
     a value an object. Consider, for example, a database that
     stores a vast number of people. Many of the <code>person</code>
     objects in this database have the same names and surnames and
     the overhead of storing them in every object may negatively
     affect the performance. In this case, we could make the first name
     and last name each an object and only store references to
     these objects in the <code>person</code> class.</p>

  <p>An instance of a persistent class can be in one of two states:
    <em>transient</em> and <em>persistent</em>. A transient
    instance only has a representation in the application's
    memory and will cease to exist when the application terminates,
    unless it is explicitly made persistent. In other words, a
    transient instance of a persistent class behaves just like an
    instance of any ordinary C++ class. A persistent instance
    has a representation in both the application's memory and the
    database. A persistent instance will remain even after the
    application terminates unless and until it is explicitly
    deleted from the database.</p>


  <h2><a name="3.2">3.2 Database</a></h2>

  <p>Before an application can make use of persistence services
     offered by ODB, it has to create a database class instance. A
     database instance is the representation of the place where
     the application stores its persistent objects. You create
     a database instance by instantiating one of the database
     system-specific classes. For example, <code>odb::mysql::database</code>
     would be such a class for the MySQL database system. You will
     also normally pass a database name as an argument to the
     class' constructor. The following code fragment
     shows how we can create a database instance for the MySQL
     database system:</p>

  <pre class="c++">
  #include &lt;odb/database.hxx>
  #include &lt;odb/mysql/database.hxx>

  auto_ptr&lt;odb::database> db (
    new odb::mysql::database (
      "test_user"     // database login name
      "test_password" // database password
      "test_database" // database name
      ));
  </pre>

  <p>The <code>odb::database</code> class is a common interface for
     all database system-specific classes provided by ODB. You
     would normally work with the database
     instance via this interface unless there is a specific
     functionality that your application depends on and which is
     only exposed by a particular system's <code>database</code>
     class. You will need to include the <code>&lt;odb/database.hxx></code>
     header file to make this class available in your application.</p>

  <p>The <code>odb::database</code> interface defines functions for
     starting transactions and manipulating persistent objects.
     These are discussed in detail in the remainder of this chapter
     as well as the next chapter which is dedicated to the topic of
     querying the database for persistent objects. For details on the
     system-specific <code>database</code> classes, refer to
     <a href="#6">Chapter 6, "Database Systems"</a>.</p>

  <h2><a name="3.3">3.3 Transactions</a></h2>

  <p>A transaction is an atomic, consistent, isolated and durable
     (ACID) unit of work. Database operations can only be
     performed within a transaction and each thread of execution
     in an application can have only one active transaction at a
     time.</p>

  <p>By atomicity we mean that when it comes to making changes to
     the database state within a transaction,
     either all the changes are applied or none at all. Consider,
     for example, a transaction that transfers funds between two
     objects representing bank accounts. If the debit function
     on the first object succeeds but the credit function on
     the second fails, the transaction is rolled back and the
     database state of the first object remains unchanged.</p>

  <p>By consistency we mean that a transaction must take all the
     objects stored in the database from one consistent state
     to another. For example, if a bank account object must
     reference a person object as its owner and we forget to
     set this reference before making the object persistent,
     the transaction will be rolled back and the database
     will remain unchanged.</p>

  <p>By isolation we mean that the changes made to the database
     state during a transaction are only visible inside this
     transaction until and unless it is committed. Using the
     above example with the bank transfer, the results of the
     debit operation performed on the first object is not
     visible to other transactions until the credit operation
     is successfully completed and the transaction is committed.</p>

  <p>By durability we mean that once the transaction is committed,
     the changes that it made to the database state are permanent
     and will survive failures such as an application crash. From
     now on the only way to alter this state is to execute and commit
     another transaction.</p>

  <p>A transaction is started by calling the
     <code>database::begin()</code>
     function. The returned transaction handle is stored in
     an instance of the <code>odb::transaction</code> class.
     You will need to include the <code>&lt;odb/transaction.hxx></code>
     header file to make this class available in your application.
     The <code>odb::transaction</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  class transaction
  {
  public:
    typedef odb::database database_type;

    void
    commit ();

    void
    rollback ();

    database_type&amp;
    database ();

    static transaction&amp;
    current ();

    static bool
    has_current ();
  };
}
  </pre>

  <p>The <code>commit()</code> function commits a transaction and
     <code>rollback()</code> rolls it back. Unless the transaction
     has been <em>finalized</em>, that is, explicitly committed or rolled
     back, the destructor of the <code>odb::transaction</code> class will
     automatically roll it back when the transaction instance goes
     out of scope. If you try to commit or roll back a finalized
     transaction, the <code>odb::transaction_already_finalized</code>
     exception is thrown.</p>

  <p>The <code>database()</code> function returns the database this
     transaction is working on. The <code>current()</code> static
     function returns the currently active transaction for this
     thread. If there is no active transaction, this function
     throws the <code>odb::not_in_transaction</code> exception.
     You can check whether there is a transaction in effect in
     this thread using the <code>has_current()</code> static function.</p>

  <p>If two or more transactions access or modify more than one object
     and are executed concurrently by different applications or by
     different threads within the same application, then it is possible
     that these transactions will try to access objects in an incompatible
     order and deadlock. The canonical example of a deadlock are
     two transactions in which the first has modified <code>object1</code>
     and is waiting for the second transaction to commit its changes to
     <code>object2</code> so that it can also update <code>object2</code>.
     At the same time the second transaction has modified <code>object2</code>
     and is waiting for the first transaction to commit its changes to
     <code>object1</code> because it also needs to modify <code>object1</code>.
     As a result, none of the two transactions can be completed.</p>

  <p>The database system detects such situations and automatically
     aborts the waiting operation in one of the deadlocked transactions.
     In ODB this translates to the <code>odb::deadlock</code> exception
     being thrown from one of the database functions. You would normally
     handle a deadlock by restarting the transaction, for example:</p>

  <pre class="c++">
for (;;)
{
  try
  {
    transaction t (db.begin ());

    ...

    t.commit ();
    break;
  }
  catch (const odb::deadlock&amp;)
  {
    continue;
  }
}
  </pre>

  <p>Note that in the above discussion of atomicity, consistency,
     isolation, and durability, all of those guarantees only apply
     to the object's state in the database as opposed to the object's
     state in the application's memory. It is possible to roll
     a transaction back but still have changes from this
     transaction in the application's memory. An easy way to
     avoid this potential inconsistency is to instantiate
     persistent objects only within the transaction scope. Consider,
     for example, these two implementations of the same transaction:</p>

  <pre class="c++">
void
update_age (database&amp; db, person&amp; p)
{
  transaction t (db.begin ());

  p.age (p.age () + 1);
  db.update (p);

  t.commit ();
}
  </pre>

  <p>In the above implementation, if the <code>update()</code> call fails
     and the transaction is rolled back, the state of the <code>person</code>
     object in the database and the state of the same object in the
     application's memory will differ. Now consider an
     alternative implementation which only instantiates the
     <code>person</code> object for the duration of the transaction:</p>

  <pre class="c++">
void
update_age (database&amp; db, unsigned long id)
{
  transaction t (db.begin ());

  auto_ptr&lt;person> p (db.load&lt;person> (id));
  p.age (p.age () + 1);
  db.update (p);

  t.commit ();
}
  </pre>

  <p>Of course, it may not always be possible to write the
     application in this style. Oftentimes we need to access and
     modify the application's state of persistent objects out of
     transactions. In this case it may make sense to try to
     roll back the changes made to the application state if
     the transaction was rolled back and the database state
     remains unchanged. One way to do this is to re-load
     the object's state from the database, for example:</p>

  <pre class="c++">
void
update_age (database&amp; db, person&amp; p)
{
  try
  {
    transaction t (db.begin ());

    p.age (p.age () + 1);
    db.update (p);

    t.commit ();
  }
  catch (...)
  {
    transaction t (db.begin ());
    db.load (p.id (), p);
    t.commit ();

    throw;
  }
}
  </pre>


  <h2><a name="3.4">3.4 Making Objects Persistent</a></h2>

  <p>A newly created instance of a persistent class is transient.
     We use the <code>database::persist()</code> function template
     to make a transient instance persistent. This function has two
     overloaded versions with the following signatures:</p>

  <pre class="c++">
  template &lt;typename T>
  typename object_traits&lt;T>::id_type
  persist (const T&amp; object);

  template &lt;typename T>
  typename object_traits&lt;T>::id_type
  persist (T&amp; object);
  </pre>

  <p>The first <code>persist()</code> function expects a constant reference
     to an instance being persisted and is used on objects with
     application-assigned object ids (see <a href="#5.4">Section 5.4,
     "Data Member Pragmas"</a>). The second
     function expects an unrestricted reference and, if the object id is
     assigned by the database, it updates the id member of the passed instance
     with the assigned value. Both functions return the object id of the
     newly persistent object.</p>

  <p>If the database already contains an object of this type with this
     identifier, the <code>persist()</code> functions throw the
     <code>odb::object_already_persistent</code> exception. This should
     never happen for database-assigned object ids as long as the
     number of objects persisted does not exceed the value space of
     the id type.</p>

  <p>When calling the <code>persist()</code> functions, we don't need to
     explicitly specify the template type since it will be automatically
     deduced from the argument being passed. The <code>odb::object_traits</code>
     template used in the signature above is part of the database support
     code generated by the ODB compiler.</p>

  <p>The following example shows how we can call these functions:</p>

<pre class="c++">
person john ("John", "Doe", 33);
person jane ("Jane", "Doe", 32);

transaction t (db->begin ());

db->persist (john);
unsigned long jane_id (db->persist (jane));

t.commit ();

cerr &lt;&lt; "Jane's id: " &lt;&lt; jane_id &lt;&lt; endl;
</pre>

  <p>Notice that in the above code fragment we have created instances
     that we were planning to make persistent before starting the
     transaction. Likewise, we printed Jane's id after we have committed
     the transaction. As a general rule, you should avoid performing
     operations within the transaction scope that can be performed
     before the transaction starts or after it terminates. An active
     transaction consumes both your application's resources, such as
     a database connection, as well as the database server's
     resources, such as object locks. By following the above rule you
     make sure these resources are made available to other threads
     in your application and to other applications as soon as
     possible.</p>

  <h2><a name="3.5">3.5 Loading Persistent Objects</a></h2>

  <p>Once an object is made persistent, and you know its object id, it
     can be loaded by the application using the <code>database::load()</code>
     function template. This function has two overloaded versions with
     the following signatures:</p>

  <pre class="c++">
  template &lt;typename T>
  typename object_traits&lt;T>::pointer_type
  load (const typename object_traits&lt;T>::id_type&amp; id);

  template &lt;typename T>
  void
  load (const typename object_traits&lt;T>::id_type&amp; id, T&amp; object);
  </pre>

  <p>Given an object id, the first function allocates a new instance
     of the object class in the dynamic memory, loads its state from
     the database, and returns the pointer to the new instance. The
     second function loads the object's state into an existing instance.
     Both functions throw <code>odb::object_not_persistent</code> if
     there is no object of this type with this id in the database.</p>

  <p>When we call the first <code>load()</code> function, we need to
     explicitly specify the object type. We don't need to do this for
     the second function because the object type will be automatically
     deduced from the second argument, for example:</p>

  <pre class="c++">
transaction t (db->begin ());

auto_ptr&lt;person> jane (db->load&lt;person> (jane_id));

db->load (jane_id, *jane);

t.commit ();
  </pre>

  <p>If we don't know for sure whether an object with a given id
     is persistent, we can use the <code>find()</code> function
     instead of <code>load()</code>, for example:</p>

  <pre class="c++">
  template &lt;typename T>
  typename object_traits&lt;T>::pointer_type
  find (const typename object_traits&lt;T>::id_type&amp; id);

  template &lt;typename T>
  bool
  find (const typename object_traits&lt;T>::id_type&amp; id, T&amp; object);
  </pre>

  <p>If an object with this id is not found in the database, the first
     <code>find()</code> function returns a <code>NULL</code> pointer
     while the second function leaves the passed instance unmodified and
     returns <code>false</code>.</p>

  <p>If we don't know the object id, then we can use queries to
     find the object (or objects) matching some criteria (see
     <a href="#4">Chapter 4, "Querying the Database"</a>). Note,
     however, that loading an object's state using its
     identifier can be significantly faster than executing a query.</p>


  <h2><a name="3.6">3.6 Updating Persistent Objects</a></h2>

  <p>If a persistent object has been modified, we can store the updated
     state in the database using the <code>database::update()</code>
     function template:</p>

  <pre class="c++">
  template &lt;typename T>
  void
  update (const T&amp; object);
  </pre>

  <p>If the object passed to this function does not exist in the
     database, <code>update()</code> throws the
     <code>odb::object_not_persistent</code> exception.</p>

  <p>Below is an example of the funds transfer that we talked about
     in the earlier section on transactions. It uses the hypothetical
     <code>bank_account</code> persistent class:</p>

  <pre class="c++">
void
transfer (database&amp; db,
          unsigned long from_acc,
          unsigned long to_acc,
          unsigned int amount)
{
  bank_account from, to;

  transaction t (db.begin ());

  db.load (from_acc, from);

  if (from.balance () &lt; amount)
    throw insufficient_funds ();

  db.load (to_acc, to);

  to.balance (to.balance () + amount);
  from.balance (from.balance () - amount);

  db.update (to);
  db.update (from);

  t.commit ();
}
  </pre>

  <h2><a name="3.7">3.7 Deleting Persistent Objects</a></h2>

  <p>To delete a persistent object's state from the database we use the
     <code>database::erase()</code> function template. If the application
     still has an instance of the erased object, this instance becomes
     transient. The <code>erase()</code> function has the following
     overloaded versions:</p>

  <pre class="c++">
  template &lt;typename T>
  void
  erase (const T&amp; object);

  template &lt;typename T>
  void
  erase (const typename object_traits&lt;T>::id_type&amp; id);
  </pre>

  <p>The first <code>erase()</code> function uses an object itself to
     delete its state from the database. Note that the passed object
     is unchanged. It simply becomes transient. The second function uses
     the object id to identify the object to be deleted. If the object
     does not exist in the database, both functions throw the
     <code>odb::object_not_persistent</code> exception.</p>


  <p>We have to specify the object type when calling the second
     <code>erase()</code> function. The same is unnecessary for the
     first function because the object type will be automatically
     deduced from its argument. The following example shows how we
     can call these functions:</p>

  <pre class="c++">
const person&amp; john = ...

transaction t (db->begin ());

db->erase (john);
db->erase&lt;person> (jane_id);

t.commit ();
  </pre>

  <h2><a name="3.8">3.8 ODB Exceptions</a></h2>

  <p>In the previous sections we have already mentioned some of the
     exceptions that can be thrown by the database functions. In this
     section we will discuss the ODB exception hierarchy and document
     all the exceptions that can be thrown by the common ODB
     runtime.</p>

  <p>The root of the ODB exception hierarchy is the abstract
     <code>odb::exception</code> class. This class inherits
     from <code>std::exception</code> and has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  struct exception: std::exception
  {
    virtual const char*
    what () const throw () = 0;
  };
}
  </pre>

  <p>Catching this exception guarantees that you will catch all the
     exceptions thrown by ODB. The <code>what()</code> function
     returns a human-readable description of the condition that
     triggered the exception.</p>

  <p>The concrete exceptions that can be thrown by ODB are presented
     in the following listing:</p>

  <pre class="c++">
namespace odb
{
  struct already_in_transaction: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct not_in_transaction: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct transaction_already_finalized: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct deadlock: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct object_not_persistent: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct object_already_persistent: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct result_not_cached: odb::exception
  {
    virtual const char*
    what () const throw ();
  };

  struct database_exception: odb::exception
  {
  };
}
  </pre>

  <p>The first four exceptions (<code>already_in_transaction</code>,
     <code>not_in_transaction</code>,
     <code>transaction_already_finalized</code>, and
     <code>deadlock</code>) are thrown by the
     <code>odb::transaction</code> class and are discussed
     in <a href="#3.3">Section 3.3, "Transactions"</a>.</p>

  <p>The <code>object_already_persistent</code> exception is thrown
     by the <code>persist()</code> database function. See
     <a href="#3.4">Section 3.4, "Making Objects Persistent"</a>
     for details.</p>

  <p>The <code>object_not_persistent</code> exception is thrown
     by the <code>load()</code>  and <code>update()</code>
     database functions. Refer to
     <a href="#3.5">Section 3.5, "Loading Persistent Objects"</a> and
     <a href="#3.6">Section 3.6, "Updating Persistent Objects"</a> for
     more information.</p>

  <p>The <code>result_not_cached</code> exception is thrown by
     the query result class. Refer to <a href="#4.4">Section 4.4,
     "Query Result"</a> for details.</p>

  <p>The <code>database_exception</code> is a base class for all
     database system-specific exceptions that are thrown by the
     database system-specific runtime library. See <a href="#6">Chapter
     6, "Database Systems"</a> for more information.</p>

  <p>The <code>odb::exception</code> class is defined in the
     <code>&lt;odb/exception.hxx></code> header file. All the
     concrete ODB exceptions are defined in
     <code>&lt;odb/exceptions.hxx></code> which also includes
     <code>&lt;odb/exception.hxx></code>. Normally you don't
     need to include either of these two headers because they are
     automatically included by <code>&lt;odb/database.hxx></code>.
     However, if the source file that handles ODB exceptions
     does not include <code>&lt;odb/database.hxx></code>, then
     you will need to explicitly include one of these headers.</p>


  <!-- CHAPTER -->


  <h1><a name="4">4 Querying the Database</a></h1>

  <p>If you don't know the identifiers of the objects that you are looking
     for, you can use queries to search the database for objects matching
     certain criteria. The ODB query facility is optional and you need to
     explicitly request the generation of the necessary database support
     code with the <code>--generate-query</code> ODB compiler option.</p>

  <p>ODB provides a flexible query API that offers two distinct levels of
     abstraction from the database system query language such as SQL.
     At the high level you are presented with an easy to use yet powerful
     object-oriented query language, called ODB Query Language. This
     query language is modeled after and is integrated into C++ allowing
     you to write expressive and safe queries that look and feel like
     ordinary C++. We have already seen examples of these queries in the
     introductory chapters. Below is another, more interesting, example:</p>

  <pre class="c++">
  typedef odb::query&lt;person> query;
  typedef odb::result&lt;person> result;

  unsigned short age;
  query q (query::first == "John" &amp;&amp; query::age &lt; query::_ref (age));

  for (age = 10; age &lt; 100; age += 10)
  {
    result r (db->query&lt;person> (q));
    ...
  }
  </pre>

  <p>At the low level, queries can be written as predicates using
     the database system-native query language such as the
     <code>WHERE</code> predicate from the SQL <code>SELECT</code>
     statement. This language will be referred to as native query
     language. At this level ODB still takes care of converting
     query parameters from C++ to the database system format. Below
     is the re-implementation of the above example using SQL as
     the native query language:</p>

  <pre class="c++">
  query q ("first = 'John' AND age = " + query::_ref (age));
  </pre>

  <p>Note that at this level you lose the static typing of
     query expressions. For example, if we wrote something like this:</p>

  <pre class="c++">
  query q (query::first == 123 &amp;&amp; query::agee &lt; query::_ref (age));
  </pre>

  <p>We would get two errors during the C++ compilation. The first would
     indicate that we cannot compare <code>query::first</code> to an
     integer and the second would pick the misspelling in
     <code>query::agee</code>. On the other hand, if we wrote something
     like this:</p>

  <pre class="c++">
  query q ("first = 123 AND agee = " + query::_ref (age));
  </pre>

  <p>It would compile fine and would trigger an error only when executed
     by the database system.</p>

  <p>You can also combine the two query languages in a single query, for
     example:</p>

  <pre class="c++">
  query q ("first = 'John'" + (query::age &lt; query::_ref (age)));
  </pre>


  <h2><a name="4.1">4.1 ODB Query Language</a></h2>

  <p>An ODB query is an expression that tells the database system whether
     any given object matches the desired criteria. As such, a query expression
     always evaluates as <code>true</code> or <code>false</code>. At
     the higher level, an expression consists of other expressions
     combined with logical operators such as <code>&amp;&amp;</code> (AND),
     <code>||</code> (OR), and <code>!</code> (NOT). For example:</p>

  <pre class="c++">
  typedef odb::query&lt;person> query;

  query q (query::first == "John" || query::age == 31);
  </pre>

  <p>At the core of every query expression lie simple expressions which
     involve one or more object members, values, or parameters. To
     refer to an object member you use an expression such as
     <code>query::first</code> above. The names of members in the
     <code>query</code> class are derived from the names of data members
     in the object class by removing the common member name decorations,
     such as leading and trailing underscores, the <code>m_</code> prefix,
     etc.</p>

  <p>In a simple expression an object member can be compared to a value,
     parameter, or another member using a number of predefined operators
     and functions. The following table gives an overview of the available
     expressions:</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="operators" border="1">
    <tr>
      <th>Operator</th>
      <th>Description</th>
      <th>Example</th>
    </tr>

    <tr>
      <td><code>==</code></td>
      <td>equal</td>
      <td><code>query::age == 31</code></td>
    </tr>

    <tr>
      <td><code>!=</code></td>
      <td>unequal</td>
      <td><code>query::age != 31</code></td>
    </tr>

    <tr>
      <td><code>&lt;</code></td>
      <td>less than</td>
      <td><code>query::age &lt; 31</code></td>
    </tr>

    <tr>
      <td><code>></code></td>
      <td>greater than</td>
      <td><code>query::age > 31</code></td>
    </tr>

    <tr>
      <td><code>&lt;=</code></td>
      <td>less than or equal</td>
      <td><code>query::age &lt;= 31</code></td>
    </tr>

    <tr>
      <td><code>>=</code></td>
      <td>greater than or equal</td>
      <td><code>query::age >= 31</code></td>
    </tr>

    <tr>
      <td><code>in()</code></td>
      <td>one of the values</td>
      <td><code>query::age.in (30, 32, 34)</code></td>
    </tr>

    <tr>
      <td><code>in_range()</code></td>
      <td>one of the values in range</td>
      <td><code>query::age.in_range (begin, end)</code></td>
    </tr>

    <tr>
      <td><code>is_null()</code></td>
      <td>value is NULL</td>
      <td><code>query::age.is_null ()</code></td>
    </tr>

    <tr>
      <td><code>is_not_null()</code></td>
      <td>value is not NULL</td>
      <td><code>query::age.is_not_null ()</code></td>
    </tr>
  </table>

  <p>The <code>in()</code> function accepts a maximum of five arguments.
     Use the <code>in_range()</code> function if you need to compare
     to more than five values. This function accepts a pair of
     standard C++ iterators and compares to all the values from
     the <code>begin</code> position inclusive and until and
     excluding the <code>end</code> position. The following
     code fragment shows how we can use these functions:</p>

  <pre class="c++">
  std::vector&lt;string> names;

  names.push_back ("John");
  names.push_back ("Jack");
  names.push_back ("Jane");

  query q1 (query::first.in ("John", "Jack", "Jane"));
  query q2 (query::first.in_range (names.begin (), names.end ()));
  </pre>



  <p>The operator precedence in the query expressions are the same
     as for equivalent C++ operators. You can use parentheses to
     make sure the expression is evaluated in the desired order.
     For example:</p>

  <pre class="c++">
  query q ((query::first == "John" || query::first == "Jane") &amp;&amp;
           query::age &lt; 31);
  </pre>



  <h2><a name="4.2">4.2 Parameter Binding</a></h2>

  <p>An instance of the <code>odb::query</code> class encapsulates two
     parts of information about the query: the query expression and
     the query parameters. Parameters can be bound to C++ variables
     either by value or by reference.</p>

  <p>If a parameter is bound by value, then the value for this parameter
     is copied from the C++ variable to the query instance at the query
     construction time. On the other hand, if a parameter is bound by
     reference, then the query instance stores a reference to the
     bound variable. The actual value of the parameter is only extracted
     at the query execution time. Consider, for example, the following
     two queries:</p>

  <pre class="c++">
  string name ("John");

  query q1 (query::first == query::_val (name));
  query q2 (query::first == query::_ref (name));

  name = "Jane";

  db->query&lt;person> (q1); // Find John.
  db->query&lt;person> (q2); // Find Jane.
  </pre>

  <p>The <code>odb::query</code> class provides two special functions,
     <code>_val()</code> and <code>_ref()</code>, that allow you to
     bind the parameter either by value or by reference, respectively.
     In the ODB query language, if the binding is not specified
     explicitly, the value semantic is used by default. In the
     native query language, binding must always be specified
     explicitly. For example:</p>

  <pre class="c++">
  query q1 (query::age &lt; age);                // By value.
  query q2 (query::age &lt; query::_val (age));  // By value.
  query q3 (query::age &lt; query::_ref (age));  // By reference.

  query q4 ("age &lt; " + age);                  // Error.
  query q5 ("age &lt; " + query::_val (age));    // By value.
  query q6 ("age &lt; " + query::_ref (age));    // By reference.
  </pre>

  <p>A query that only has by-value parameters does not depend on any
     other variables and is self-sufficient once constructed. A query
     that has one or more by-reference parameters depends on the
     bound variables until the query is executed. If one such variable
     goes out of scope and you execute the query, the behavior is
     undefined.</p>

  <h2><a name="4.3">4.3 Executing a Query</a></h2>

  <p>Once we have the query instance ready and by-reference parameters
     initialized, we can execute the query using the
     <code>database::query()</code> function template. It has two
     overloaded versions:</p>

  <pre class="c++">
  template &lt;typename T>
  result&lt;T>
  query (bool cache = true);

  template &lt;typename T>
  result&lt;T>
  query (const odb::query&lt;T>&amp;, bool cache = true);
  </pre>

  <p>The first <code>query()</code> function is used to return all the
     persistent objects of a given type stored in the database.
     The second function uses the passed query instance to only return
     objects matching the query criteria. The <code>cache</code> argument
     determines whether the objects' states should be cached in the
     application's memory or if they should be returned by the database
     system one by one as the iteration over the result progresses. The
     result caching is discussed in detail in the next section.</p>

  <p>When calling the <code>query()</code> function, we have to
     explicitly specify the object type we are querying. For example:</p>

  <pre class="c++">
  typedef odb::query&lt;person> query;
  typedef odb::result&lt;person> result;

  result all (db->query&lt;person> ());
  result johns (db->query&lt;person> (query::first == "John"));
  </pre>

  <p>Note that it is not required to explicitly create a named
     query variable before executing it. For example, the following
     two queries are equivalent:</p>

  <pre class="c++">
  query q (query::first == "John");

  result r1 (db->query&lt;person> (q));
  result r1 (db->query&lt;person> (query::first == "John"));
  </pre>

  <p>Normally you would create a named query instance if you are
     planning to run the same query multiple times and would use the
     in-line version for those that are executed only once.</p>

  <p>It is also possible to create queries from other queries by
     combining them using logical operators. For example:</p>

  <pre class="c++">
result
find_minors (database&amp; db, const query&amp; name_query)
{
  return db.query&lt;person> (name_query &amp;&amp; query::age &lt; 18);
}

result r (find_underage (db, query::first == "John"));
  </pre>

  <h2><a name="4.4">4.4 Query Result</a></h2>

  <p>The result of executing a query is zero, one, or more objects
     matching the query criteria. The result is returned as an instance
     of the <code>odb::result</code> class template, for example:</p>

  <pre class="c++">
  typedef odb::query&lt;person> query;
  typedef odb::result&lt;person> result;

  result johns (db->query&lt;person> (query::first == "John"));
  </pre>

  <p>It is best to view an instance of <code>odb::result</code>
     as a handle to a stream, such as a file stream. While you can
     make a copy of a result or assign one result to another, the
     two instances will refer to the same result stream. Advancing
     the current position in one instance will also advance it in
     another. The result instance is only usable within the transaction
     it was created in. Trying to manipulate the result after the
     transaction has terminated leads to undefined behavior.</p>

  <p>The <code>odb::result</code> class template conforms to the
     standard C++ sequence requirements and has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  template &lt;typename T>
  class result
  {
  public:
    typedef odb::result_iterator&lt;T> iterator;

  public:
    result ();

    result (const result&amp;);

    result&amp;
    operator= (const result&amp;);

    void
    swap (result&amp;)

  public:
    iterator
    begin ();

    iterator
    end ();

  public:
    void
    cache ();

    bool
    empty () const;

    std::size_t
    size () const;
  };
}
  </pre>

  <p>The default constructor creates an empty result set. The
    <code>cache()</code> function caches the returned objects'
    state in the application's memory. We have already mentioned
    result caching when we talked about query execution. As you
    may remember the <code>database::query()</code> function
    caches the result unless instructed not to by the caller.
    The <code>cache()</code> function allows you to
    cache the result at a later stage if it wasn't already
    cached during query execution.</p>

  <p>If the result is cached, the database state of all the returned
     objects is stored in the application's memory. Note that
     the actual objects are still only instantiated on demand
     during result iteration. It is the raw database state that
     is cached in memory. In contrast, for uncached results
     the object's state is sent by the database system one object
     at a time as the iteration progresses.</p>

  <p>Uncached results can improve the performance of both the application
     and the database system in situations where you have a large
     number of objects in the result or if you will only examine
     a small portion of the returned objects. However, uncached
     results have a number of limitations. There can only be one
     uncached result in a transaction. Creating another result
     (cached or uncached) by calling <code>database::query()</code>
     will invalidate the existing uncached result. Furthermore,
     calling any other database functions, such as <code>update()</code>
     or <code>erase()</code> will also invalidate the uncached result.</p>

  <p>The <code>empty()</code> function returns <code>true</code> if
     there are no objects in the result and <code>false</code> otherwise.
     The <code>size()</code> function can only be called for cached results.
     It returns the number of objects in the result. If you call this
     function on an uncached result, the <code>odb::result_not_cached</code>
     exception is thrown.</p>

  <p>To iterate over the objects in a result we use the
     <code>begin()</code> and <code>end()</code> functions
     together with the <code>odb::result&lt;T>::iterator</code>
     type, for example:</p>

  <pre class="c++">
  result r (db->query&lt;person> (query::first == "John"));

  for (result::iterator i (r.begin ()); i != r.end (); ++i)
  {
    ...
  }
  </pre>

  <p>The result iterator is an input iterator which means that the
     only two position operations that it supports are to move to the
     next object and to determine whether the end of the result stream
     has been reached. In fact, the result iterator can only be in two
     states: the current position and the end position. If you have
     two iterators pointing to the current position and then you
     advance one of them, the other will advance as well. This,
     for example, means that it doesn't make sense to store an
     iterator that points to some object of interest in the result
     stream with the intent of dereferencing it after the iteration
     is over. Instead, you would need to store the object itself.</p>

  <p>The result iterator has the following dereference functions
     that can be used to access the pointed-to object:</p>

  <pre class="c++">
namespace odb
{
  template &lt;typename T>
  class result_iterator
  {
  public:
    T*
    operator-> () const;

    T&amp;
    operator* () const;

    typename object_traits&lt;T>::pointer_type
    load ();

    void
    load (T&amp; x);
  };
}
  </pre>

  <p>When you call the <code>*</code> or <code>-></code> operator,
     the iterator will allocate a new instance of the object class
     in the dynamic memory, load its state from the database
     state, and return a reference or pointer to the new instance. The
     iterator maintains the ownership of the returned object and will
     return the same pointer for subsequent calls to either of these
     operators until it is advanced to the next object or you call
     the first <code>load()</code> function (see below). For example:</p>

  <pre class="c++">
  result r (db->query&lt;person> (query::first == "John"));

  for (result::iterator i (r.begin ()); i != r.end ();)
  {
    cout &lt;&lt; i->last () &lt;&lt; endl; // Create an object.
    person&amp; p (*i);             // Reference to the same object.
    cout &lt;&lt; p.age () &lt;&lt; endl;
    ++i;                        // Free the object.
  }
  </pre>

  <p>The overloaded <code>result_iterator::load()</code> functions are
     similar to <code>database::load()</code>. The first function
     returns a dynamically allocated instance of the current
     object. As an optimization, if the iterator already owns an object
     as a result of an earlier
     call to the <code>*</code> or <code>-></code> operator, then it
     relinquishes the ownership of this object and returns it instead.
     This allows you to write code like this without worrying about
     a double allocation:</p>

  <pre class="c++">
  result r (db->query&lt;person> (query::first == "John"));

  for (result::iterator i (r.begin ()); i != r.end (); ++i)
  {
    if (i->last == "Doe")
    {
      auto_ptr p (i.load ());
      ...
    }
  }
  </pre>

  <p>Note, however, that because of this optimization, a subsequent
     to <code>load()</code> call to the <code>*</code> or <code>-></code>
     operator results in the allocation of a new object.</p>

  <p>The second <code>load()</code> function allows
     you to load the current object's state into an existing instance.
     For example:</p>

  <pre class="c++">
  result r (db->query&lt;person> (query::first == "John"));

  person p;
  for (result::iterator i (r.begin ()); i != r.end (); ++i)
  {
    i.load (p);
    cout &lt;&lt; p.last () &lt;&lt; endl;
    cout &lt;&lt; i.age () &lt;&lt; endl;
  }
  </pre>


  <!-- CHAPTER -->


  <h1><a name="5">5 ODB Pragma Language</a></h1>

  <p>As we have already seen in previous chapters, ODB uses a pragma-based
     language to capture database-specific information about C++ types.
     This chapter describes the ODB pragma language in more detail. It
     can be read together with other chapters in the manual to get a
     sense of what kind of configurations and mapping fine-tuning are
     possible. You can also use this chapter as a reference at a later
     stage.</p>

  <p>An ODB pragma has the following syntax:</p>

  <p><code>#pragma db <i>qualifier</i> [<i>specifier</i> <i>specifier</i> ...]</code></p>

  <p>The <em>qualifier</em> tells the ODB compiler what kind of C++ construct
     this pragma describes. Valid qualifiers are <code>object</code>,
     <code>value</code>, and <code>member</code>. Pragmas with the
     <code>object</code> qualifier describe persistent object types.
     It tells the ODB compiler that the C++ class it describes is a
     persistent class. Similarly, pragmas with the <code>value</code>
     qualifier describes value types and the <code>member</code>
     qualifier is used to describe data members of persistent object
     and value types.</p>

  <p>The <em>specifier</em> informs the ODB compiler about a particular
     database-related property of the C++ declaration. For example, the
     <code>id</code> member specifier tells the ODB compiler that this
     member contains this object's identifier. Below is the declaration
     of the <code>person</code> class that shows how we can use ODB
     pragmas:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db member id
  unsigned long id_;
  ...
};
  </pre>

  <p>In the above example we don't explicitly specify which C++ class or
     data member the pragma belongs to. Rather, the pragma applies to
     a C++ declaration that immediately follows the pragma. Such pragmas
     are called <em>positioned pragmas</em>. In positioned pragmas that
     apply to data members, the <code>member</code> qualifier can be
     omitted for brevity, for example:</p>

  <pre class="c++">
  #pragma db id
  unsigned long id_;
  </pre>

  <p>Note also that if the C++ declaration immediately following a
     position pragma is incompatible with the pragma qualifier, an
     error will be issued. For example:</p>

  <pre class="c++">
  #pragma db object  // Error: expected class instead of data member.
  unsigned long id_;
  </pre>


  <p>While keeping the C++ declarations and database declarations close
     together eases maintenance and increases readability, you can also
     place them in different parts of the same header file or even
     factor them to a separate file. To achieve this we use the so called
     <em>named pragmas</em>. Unlike positioned pragmas, named pragmas
     explicitly specify the C++ declaration to which they apply by
     adding the declaration name after the pragma qualifier. For example:</p>

  <pre class="c++">
class person
{
  ...
private:
  unsigned long id_;
  ...
};

#pragma db object(person)
#pragma db member(person::id_) id
  </pre>

  <p>Note that in the named pragmas for data members the <code>member</code>
     qualifier is no longer optional. The C++ declaration name in the
     named pragmas is resolved using the standard C++ name resolution
     rules, for example:</p>

  <pre class="c++">
namespace db
{
  class person
  {
    ...
  private:
    unsigned long id_;
    ...
  };
}

namespace db
{
  #pragma db object(person)  // Resolves db::person.
}

#pragma db member(db::person::id_) id
  </pre>

  <p>As another example, the following code fragment shows how to use the
     named value type pragma to map a C++ type to a native database type:</p>

  <pre class="c++">
#pragma db value(bool) type("INT NOT NULL")

#pragma db object
class person
{
  ...
private:
  bool married_; // Mapped to INT NOT NULL database type.
  ...
};
  </pre>

  <h2><a name="5.1">5.1 C++ Compiler Warnings</a></h2>

  <p>The C++ header file that defines your persistent classes and
     normally contains one or more ODB pragmas is compiled by both
     the ODB compiler to generate the database support code and
     the C++ compiler to build your application. Some C++ compilers
     issue warnings about pragmas that they do not recognize. There
     are several ways to deal with this problem. The easiest is to
     disable such warnings using one of the compiler-specific command
     line options or warning control pragmas. This method is described
     in the following sub-section for popular C++ compilers.</p>

  <p>There are also several C++ compiler-independent methods that you
     can employ. The first is to use the <code>PRAGMA_DB</code> macro,
     defined in <code>&lt;odb/core.hxx></code>, instead of using
     <code>#pragma&nbsp;db</code> directly. This macro expands to the
     ODB pragma when compiled with the ODB compiler and to an empty
     declaration when compiled with other compilers. The following example
     shows how we can use this macro:</p>

  <pre class="c++">
#include &lt;odb/core.hxx>

PRAGMA_DB(object)
class person
{
  ...
private:
  PRAGMA_DB(id)
  unsigned long id_;
  ...
};
  </pre>

  <p>An alternative to using the <code>PRAGMA_DB</code> macro is to
     group the <code>#pragma&nbsp;db</code> directives in blocks that are
     conditionally included into compilation only when compiled with the
     ODB compiler. For example:</p>

  <pre class="c++">
class person
{
  ...
private:
  unsigned long id_;
  ...
};

#ifdef ODB_COMPILER
#  pragma db object(person)
#  pragma db member(person::id_) id
#endif
  </pre>

  <p>The disadvantage of this approach is that it can quickly become
     overly verbose when positioned pragmas are used.</p>

  <h3><a name="5.1.1">5.1.1 GNU C++</a></h3>

  <p>GNU g++ does not issue warnings about unknown pragmas
     unless requested with the <code>-Wall</code> command line option.
     To disable only the unknown pragma warning, you can add the
     <code>-Wno-unknown-pragmas</code> option after <code>-Wall</code>,
     for example:</p>

  <pre class="terminal">
g++ -Wall -Wno-unknown-pragmas ...
  </pre>

  <h3><a name="5.1.2">5.1.2 Visual C++</a></h3>

  <p>Microsoft Visual C++ issues an unknown pragma warning (C4068) at
     warning level 1 or higher. This means that unless you have disabled
     the warnings altogether (level 0), you will see this warning.</p>

  <p>To disable this warning via the compiler command line, you can add
     the <code>/wd4068</code> C++ compiler option in Visual Studio 2008
     and earlier. In Visual Studio 2010 there is now a special GUI field
     where you can enter warning numbers that should be disabled. Simply
     enter 4068 into this field.</p>

  <p>You can also disable this warning for only a specific header or
     a fragment of a header using the warning control pragma. For
     example:</p>

  <pre class="c++">
#include &lt;odb/core.hxx>

#pragma warning (push)
#pragma warning (disable:4068)

#pragma db object
class person
{
  ...
private:
  #pragma db id
  unsigned long id_;
  ...
};

#pragma warning (pop)
  </pre>

  <h3><a name="5.1.3">5.1.3 Sun C++</a></h3>

  <p>The Sun C++ compiler does not issue warnings about unknown pragmas
     unless the <code>+w</code> or <code>+w2</code> option is specified.
     To disable only the unknown pragma warning you can add the
     <code>-erroff=unknownpragma</code> option anywhere on the
     command line, for example:</p>

  <pre class="terminal">
CC +w -erroff=unknownpragma ...
  </pre>

  <h3><a name="5.1.4">5.1.4 IBM XL C++</a></h3>

  <p>IBM XL C++ issues an unknown pragma warning (1540-1401) by default.
     To disable this warning you can add the <code>-qsuppress=1540-1401</code>
     command line option, for example:</p>

  <pre class="terminal">
xlC -qsuppress=1540-1401 ...
  </pre>


  <h2><a name="5.2">5.2 Object Type Pragmas</a></h2>

  <p>A pragma with the <code>object</code> qualifier declares a C++ class
     as a persistent object type. The qualifier can be optionally followed
     by the <code>table</code> specifier.</p>

  <h3><a name="5.2.1">5.2.1 <code>table</code></a></h3>

  <p>The <code>table</code> specifier specifies the table name that should
     be used to store objects of this class in a relational database. For
     example:</p>

  <pre class="c++">
#pragma db object table("people")
class person
{
  ...
};
  </pre>

  <p>If the table name is not specified, the class name is used as the
     table name.</p>

  <h2><a name="5.3">5.3 Value Type Pragmas</a></h2>

  <p>A pragma with the <code>value</code> qualifier describes a value
     type and can be optionally followed by the <code>type</code>
     specifier.</p>

  <h3><a name="5.3.1">5.3.1 <code>type</code></a></h3>

  <p>The <code>type</code> specifier specifies the native database type
     that should be used for data members of this type. For example:</p>

  <pre class="c++">
#pragma db value(bool) type("INT NOT NULL")

#pragma db object
class person
{
  ...
private:
  bool married_; // Mapped to INT NOT NULL database type.
  ...
};
  </pre>

  <p>The ODB compiler provides the default mapping between common C++
     types, such as <code>bool</code>, <code>int</code>, and
     <code>std::string</code> and the database types for each supported
     database system. For more information on the default mapping,
     refer to <a href="#6">Chapter 6, "Database Systems"</a>.</p>

  <p>In the above example we changed the mapping for the <code>bool</code>
     type which is now mapped to the <code>INT</code> database type. In
     this case, the <code>value</code> pragma is all that is necessary
     since the ODB compiler will be able to figure out how to store
     a boolean value as an integer in the database. However, there
     could be situations where the ODB compiler will not know how to
     handle the conversion between the C++ and database representations
     of a value. Consider, as an example, a situation where the
     boolean value is stored in the database as a string:</p>

  <pre class="c++">
#pragma db value(bool) type("VARCHAR(5) NOT NULL")
  </pre>

  <p>The possible database values for the C++ <code>true</code> value could
     be <code>"true"</code>, or <code>"TRUE"</code>, or <code>"True"</code>.
     Or, maybe, all of the above are valid. The ODB compiler has no way
     of knowing how your application wants to convert <code>bool</code>
     to a string and back. To support such custom value type mappings,
     ODB allows you to provide your own database conversion functions
     by specializing the <code>value_traits</code> class template. The
     <code>mapping</code> example in the <code>odb-examples</code>
     package shows how to do this for all the supported database systems.</p>

  <p>It is also possible to change the database type mapping for individual
     members, as described in <a href="#5.4">Section 5.4, "Data Member
     Pragmas"</a>.</p>

  <h2><a name="5.4">5.4 Data Member Pragmas</a></h2>

  <p>A pragma with the <code>member</code> qualifier or a positioned
     pragma without a qualifier describes a data member. It can
     be optionally followed, in any order, by one or more specifiers
     summarized in the table below:</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="specifiers" border="1">
    <tr>
      <th>Specifier</th>
      <th>Summary</th>
      <th>Section</th>
    </tr>

    <tr>
      <td><code>id</code></td>
      <td>the member is an object id</td>
      <td><a href="#5.4.1">5.4.1</a></td>
    </tr>

    <tr>
      <td><code>auto</code></td>
      <td>id is assigned by the database</td>
      <td><a href="#5.4.2">5.4.2</a></td>
    </tr>

    <tr>
      <td><code>type</code></td>
      <td>the database type for the member</td>
      <td><a href="#5.4.3">5.4.3</a></td>
    </tr>

    <tr>
      <td><code>column</code></td>
      <td>the column name for the member</td>
      <td><a href="#5.4.4">5.4.4</a></td>
    </tr>

    <tr>
      <td><code>transient</code></td>
      <td>the member is not stored in the database</td>
      <td><a href="#5.4.5">5.4.5</a></td>
    </tr>

  </table>

  <h3><a name="5.4.1">5.4.1 <code>id</code></a></h3>

  <p>The <code>id</code> specifier specifies that the data member contains
     the object id. Every persistent class must have a member designated
     as an object's identifier. For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db id
  std::string email_;
  ...
};
  </pre>

  <p>In a relational database, an identifier member is mapped to a
     primary key.</p>

  <h3><a name="5.4.2">5.4.2 <code>auto</code></a></h3>

  <p>The <code>auto</code> specifier specifies that the object's identifier
     is automatically assigned by the database. Only a member that was
     designated as an object id can have this specifier. For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db id auto
  unsigned long id_;
  ...
};
  </pre>

  <p>Note that automatically-assigned object ids are not reused.
     If you have a high object turnover (that is, objects are routinely
     made persistent and then erased), then care must be taken not to
     run out of object ids. In such situations, using
     <code>unsigned long long</code> as the identifier type is a safe
     choice.</p>

  <p>For additional information on the automatic identifier assignment,
     refer to <a href="#3.4">Section 3.4, "Making Objects Persistent"</a>.</p>

  <h3><a name="5.4.3">5.4.3 <code>type</code></a></h3>

  <p>The <code>type</code> specifier specifies the native database type
     that should be used for this data member. For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db type("INT NOT NULL")
  bool married_;
  ...
};
  </pre>

  <p>The behavior of this specifier for members is similar to that
     for value types. The only difference is the scope. The value
     type pragma applies to all members with this value type that
     don't have their own <code>type</code> specifiers, while the
     member pragma applies only to a single member. For more
     information on the semantics of this specifier, refer to
     <a href="#5.3">Section 5.3, "Value Type Pragmas"</a>.</p>

  <h3><a name="5.4.4">5.4.4 <code>column</code></a></h3>

  <p>The <code>column</code> specifier specifies the column name
     that should be used to store this member in a relational database.
     For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db id column("person_id")
  unsigned long id_;
  ...
};
  </pre>

  <p>If the column name is not specified, it is derived from the member
     name by removing the common member name decorations, such as leading
     and trailing underscores, the <code>m_</code> prefix, etc.</p>

  <h3><a name="5.4.5">5.4.5 <code>transient</code></a></h3>

  <p>The <code>transient</code> specifier instructs the ODB compiler
     not to store the data member in the database. For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  date born_;

  #pragma db transient
  unsigned short age_; // Computed from born_.
  ...
};
  </pre>

  <p>This pragma is usually used on computed members, pointers and
     references that are only meaningful in the application's
     memory, as well as utility members such as mutexes, etc.</p>


  <!-- CHAPTER -->


  <h1><a name="6">6 Database Systems</a></h1>

  <p>This chapter covers topics specific to the database system
     implementations and their support in ODB. In particular, it
     describes the system-specific <code>database</code> classes
     as well as the default mapping between basic C++ value types
     and native database types.</p>


  <h2><a name="6.1">6.1 MySQL Database</a></h2>

  <p>To generate support code for the MySQL database you will need
     to pass the "<code>--database&nbsp;mysql</code>"
     (or "<code>-d&nbsp;mysql</code>") option to the ODB compiler.
     Your application will also need to link to the MySQL ODB runtime
     library (<code>libodb-mysql</code>). All MySQL-specific ODB
     classes are defined in the <code>odb::mysql</code> namespace.</p>

  <h3><a name="6.1.1">6.1.1 MySQL Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between basic
     C++ value types and MySQL database types. This mapping can be
     customized on the per-type and per-member basis using the ODB
     Pragmas Language (see <a href="#5">Chapter 5, "ODB Pragma
     Language"</a>).</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>C++ Type</th>
      <th>MySQL type</th>
    </tr>

    <tr>
      <td><code>bool</code></td>
      <td><code>TINYINT(1) NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>char</code></td>
      <td><code>TINYINT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>signed char</code></td>
      <td><code>TINYINT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>unsigned char</code></td>
      <td><code>TINYINT UNSIGNED NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>short</code></td>
      <td><code>SMALLINT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>unsigned short</code></td>
      <td><code>SMALLINT UNSIGNED NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>int</code></td>
      <td><code>INT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>unsigned int</code></td>
      <td><code>INT UNSIGNED NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>long</code></td>
      <td><code>BIGINT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>unsigned long</code></td>
      <td><code>BIGINT UNSIGNED NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>long long</code></td>
      <td><code>BIGINT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>unsigned long long</code></td>
      <td><code>BIGINT UNSIGNED NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>float</code></td>
      <td><code>FLOAT NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>double</code></td>
      <td><code>DOUBLE NOT NULL</code></td>
    </tr>

    <tr>
      <td><code>std::string</code></td>
      <td><code>TEXT NOT NULL/VARCHAR(255) NOT NULL</code></td>
    </tr>
  </table>

  <p>Note that the <code>std::string</code> type is mapped
     differently depending on whether the member of this type
     is an object id or not. If the member is an object id,
     then for this member <code>std::string</code> is mapped
     to <code>VARCHAR(255) NOT NULL</code> MySQL type. Otherwise,
     it is mapped to <code>TEXT NOT NULL</code>.</p>

  <h3><a name="6.1.2">6.1.2 MySQL Database Class</a></h3>

  <p>The MySQL <code>database</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class database: public odb::database
    {
    public:
      database (const char* user,
                const char* passwd,
                const char* db,
                const char* host = 0,
                unsigned int port = 0,
                const char* socket = 0,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; user,
                const std::string&amp; passwd,
                const std::string&amp; db,
                const std::string&amp; host = "",
                unsigned int port = 0,
                const std::string* socket = 0,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; user,
                const std::string* passwd,
                const std::string&amp; db,
                const std::string&amp; host = "",
                unsigned int port = 0,
                const std::string* socket = 0,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; user,
                const std::string&amp; passwd,
                const std::string&amp; db,
                const std::string&amp; host,
                unsigned int port,
                const std::string&amp; socket,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; user,
                const std::string* passwd,
                const std::string&amp; db,
                const std::string&amp; host,
                unsigned int port,
                const std::string&amp; socket,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (int&amp; argc,
                char* argv[],
                bool erase = false,
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      static void
      print_usage (std::ostream&amp;);


    public:
      const char*
      user () const;

      const char*
      password () const;

      const char*
      db () const;

      const char*
      host () const;

      unsigned int
      port () const;

      const char*
      socket () const;

      unsigned long
      client_flags () const;

    public:
      details::shared_ptr&lt;mysql::connection>
      connection ();
    };
  }
}
  </pre>

  <p>You will need to include the <code>&lt;odb/mysql/database.hxx></code>
     header file to make this class available in your application.</p>

  <p>The overloaded <code>database</code> constructors allow you
     to specify MySQL database parameters that should be used when
     connecting to the database. In MySQL <code>NULL</code> and an
     empty string are treated as the same values for all the
     string parameters except <code>password</code> and
     <code>socket</code>. The <code>client_flags</code> argument
     allows you to specify various MySQL client library flags. For more
     information on the possible values, refer to the MySQL C API
     documentation. The <code>CLIENT_FOUND_ROWS</code> flag is always set
     by the MySQL ODB runtime regardless of whether it was passed in the
     <code>client_flags</code> argument.</p>

  <p>The last constructor extracts the database parameters
     from the command line. The following options are recognized:</p>

  <pre class="terminal">
  --user &lt;login>
  --password &lt;password>
  --database &lt;name>
  --host &lt;host>
  --port &lt;integer>
  --socket &lt;socket>
  --options-file &lt;file>
  </pre>

  <p>The <code>--options-file</code> option allows you to specify some
  or all of the database options in a file with each option appearing
  on a separate line followed by space and an option value.</p>

  <p>If the <code>erase</code> argument to this constructor is true,
     then the above options are removed from the <code>argv</code>
     array and the <code>argc</code> count is updated accordingly.
     This is primarily useful if your application accepts other
     options or arguments and you would like to get the MySQL
     options out of the <code>argv</code> array.</p>

  <p>This constructor throws the <code>odb::mysql::cli_exception</code>
     exception if the MySQL option values are missing or invalid.
     See section <a href="#6.1.4">Section 6.1.4, "MySQL Exceptions"</a>
     for more information on this exception.</p>

  <p>The static <code>print_usage()</code> function prints the list of options
     with short descriptions that are recognized by this constructor.</p>

  <p>The last argument to all of the constructors is the
     pointer to the connection factory. If you pass a
     non-<code>NULL</code> value, the database instance assumes
     ownership of the factory instance. The connection factory
     interface as well as the available implementations are described
     in the next section.</p>

  <p>The set of accessor functions following the constructors allow you
     to query the parameters of the <code>database</code> instance.</p>

  <p>The <code>connection()</code> function returns the MySQL database
     connection encapsulated by the <code>odb::mysql::connection</code>
     class. Normally, you wouldn't call this function directly and
     instead let the ODB runtime manage the database connections. However,
     if for some reason you need to access the underlying MySQL connection
     handle, refer to the MySQL ODB runtime source code for the interface
     of the <code>connection</code> class.</p>

  <h3><a name="6.1.3">6.1.3 Connection Factory</a></h3>

  <p>The <code>connection_factory</code> abstract class has the
     following interface:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class connection_factory
    {
    public:
      virtual void
      database (mysql::database&amp;) = 0;

      virtual details::shared_ptr&lt;mysql::connection>
      connect () = 0;
    };
  }
}
  </pre>

  <p>The <code>database()</code> function is called when a connection
     factory is associated with a database instance. This happens in
     the <code>odb::mysql::database</code> class constructors. The
     <code>connect()</code> function is called whenever a database
     connection is requested.</p>

  <p>The two implementations of the <code>connection_factory</code>
     interface provided by the MySQL ODB runtime are
     the <code>new_connection_factory</code> and
     <code>connection_pool_factory</code>. You will need to include
     the <code>&lt;odb/mysql/connection-factory.hxx></code>
     header file to make the <code>connection_factory</code> interface
     and these implementation classes available in your application.</p>


  <p>The <code>new_connection_factory</code> class creates a new
     connection whenever one is requested. When a connection is no
     longer needed, it is released and closed. The
     <code>connection_pool_factory</code> class implements a
     connection pool. It has the following interface:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class connection_pool_factory: public connection_factory
    {
      connection_pool_factory (std::size_t max_connections = 0,
                               std::size_t min_connections = 0)
    };
};
  </pre>

  <p>The <code>max_connections</code> argument specifies the maximum
     number of concurrent connections that this pool factory will
     maintain. Similarly, the <code>min_connections</code> argument
     specifies the minimum number of available connections that
     should be kept open.</p>

  <p>Whenever a connection is requested, the pool factory first
     checks if there is an unused connection that can be returned.
     If there is none, the pool factory checks the
     <code>max_connections</code> value to see if a new connection
     can be created. If the total number of connections maintained
     by the pool is less than this value, then a new connection is
     created and returned. Otherwise, the calling thread is blocked
     until a connection becomes available.</p>

  <p>When a connection is released, the pool factory first checks
     if there are blocked threads waiting for a connection. If so,
     one of them is unblocked and is given the connection. Otherwise,
     the pool factory checks whether the total number of connections
     maintained by the pool is greater than the <code>min_connections</code>
     value. If that's the case, the connection is closed. Otherwise, the
     connection is added to the pool of available connections to be
     returned on the next request. In other words, if the number of
     connections maintained by the pool exceeds the <code>min_connections</code>
     number and there are no threads waiting for a new connection,
     then the pool will close the excess connections.</p>

  <p>If the <code>max_connections</code> value is 0, then the pool will
     create a new connection whenever all of the existing connections
     are in use.  If the <code>min_connections</code> value is 0, then
     the pool will never close a connection and instead maintain all
     the connections that were ever created.</p>

  <p>If you pass <code>NULL</code> as the connection factory to
     one of the <code>database</code> constructors, then the
     <code>connection_pool_factory</code> instance will be
     created by default with the min and max connections values
     set to 0. The following code fragment shows how we can
     pass our own connection factory instance:</p>

  <pre class="c++">
#include &lt;odb/database.hxx>

#include &lt;odb/mysql/database.hxx>
#include &lt;odb/mysql/connection-factory.hxx>

int
main (int argc, char* argv[])
{
  auto_ptr&lt;odb::mysql::connection_factory> f (
    new odb::mysql::connection_pool_factory (20));

  auto_ptr&lt;odb::database> db (
    new mysql::database (argc, argv, false, 0, f));
}
  </pre>

  <h3><a name="6.1.4">6.1.4 MySQL Exceptions</a></h3>

  <p>The MySQL ODB runtime library defines the following MySQL-specific
     exceptions:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class database_exception: odb::database_exception
    {
    public:
      unsigned int
      error () const;

      const std::string&amp;
      sqlstate () const;

      const std::string&amp;
      message () const;

      virtual const char*
      what () const throw ();
    };

    class cli_exception: odb::exception
    {
    public:
      virtual const char*
      what () const throw ();
    };
  }
}
  </pre>

  <p>You will need to include the <code>&lt;odb/mysql/exceptions.hxx></code>
     header file to make these exceptions available in your application.</p>

  <p>The <code>odb::mysql::database_exception</code> is thrown if
     a MySQL database operation fails. The MySQL-specific error
     information is accessible via the <code>error()</code>,
     <code>sqlstate()</code>, and <code>message()</code> functions.
     All this information is also combined and returned in a
     human-readable form by the <code>what()</code> function.</p>

  <p>The <code>odb::mysql::cli_exception</code> is thrown by the
     command line parsing constructor of the <code>odb::mysql::database</code>
     class if the MySQL option values are missing or invalid. The
     <code>what()</code> function returns a human-readable description
     of an error.</p>

  </div>
</div>


</body>
</html>