aboutsummaryrefslogtreecommitdiff
path: root/doc/manual.xhtml
blob: 64a0952d8a78cc6f0cf27539e2ff4abf4e4494ba (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
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
<?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-2011 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.6"/>
  <meta name="version" content="1.6.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.

@@ Check that parts TOCs are up to date.

-->

  <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;
  }

  /* Force page break for both PDF and HTML (when printing). */
  hr.page-break {
    height: 0;
    width: 0;
    border: 0;
    visibility: hidden;

    page-break-after: always;
  }

  /* 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-2011 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.6, September 2011</p>
    <p>This revision of the manual describes ODB 1.6.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>

  <hr class="page-break"/>
  <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 colspan="2"><a href="#I">PART I OBJECT-RELATIONAL MAPPING</a></th>
    </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">Object Pointers</a></td></tr>
          <tr><th>3.3</th><td><a href="#3.3">Database</a></td></tr>
          <tr><th>3.4</th><td><a href="#3.4">Transactions</a></td></tr>
	  <tr><th>3.5</th><td><a href="#3.5">Connections</a></td></tr>
	  <tr><th>3.6</th><td><a href="#3.6">Error Handling and Recovery</a></td></tr>
          <tr><th>3.7</th><td><a href="#3.7">Making Objects Persistent</a></td></tr>
          <tr><th>3.8</th><td><a href="#3.8">Loading Persistent Objects</a></td></tr>
          <tr><th>3.9</th><td><a href="#3.9">Updating Persistent Objects</a></td></tr>
          <tr><th>3.10</th><td><a href="#3.10">Deleting Persistent Objects</a></td></tr>
	  <tr><th>3.11</th><td><a href="#3.11">Executing Native SQL Statements</a></td></tr>
          <tr><th>3.12</th><td><a href="#3.12">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">Containers</a>
        <table class="toc">
          <tr><th>5.1</th><td><a href="#5.1">Ordered Containers</a></td></tr>
          <tr><th>5.2</th><td><a href="#5.2">Set and Multiset Containers</a></td></tr>
          <tr><th>5.3</th><td><a href="#5.3">Map and Multimap Containers</a></td></tr>
          <tr><th>5.4</th><td><a href="#5.4">Using Custom Containers</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>6</th><td><a href="#6">Relationships</a>
        <table class="toc">
          <tr>
            <th>6.1</th><td><a href="#6.1">Unidirectional Relationships</a>
              <table class="toc">
                <tr><th>6.1.1</th><td><a href="#6.1.1">To-One Relationships</a></td></tr>
		<tr><th>6.1.2</th><td><a href="#6.1.2">To-Many Relationships</a></td></tr>
              </table>
            </td>
          </tr>
	  <tr>
            <th>6.2</th><td><a href="#6.2">Bidirectional Relationships</a>
              <table class="toc">
                <tr><th>6.2.1</th><td><a href="#6.2.1">One-to-One Relationships</a></td></tr>
		<tr><th>6.2.2</th><td><a href="#6.2.2">One-to-Many Relationships</a></td></tr>
		<tr><th>6.2.3</th><td><a href="#6.2.3">Many-to-Many Relationships</a></td></tr>
              </table>
            </td>
          </tr>
          <tr><th>6.3</th><td><a href="#6.3">Lazy Pointers</a></td></tr>
          <tr><th>6.4</th><td><a href="#6.4">Using Custom Smart Pointers</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>7</th><td><a href="#7">Value Types</a>
        <table class="toc">
	  <tr><th>7.1</th><td><a href="#7.1">Simple Value Types</a></td></tr>
	  <tr>
            <th>7.2</th><td><a href="#7.2">Composite Value Types</a>
              <table class="toc">
                <tr><th>7.2.1</th><td><a href="#7.2.1">Composite Value Column and Table Names</a></td></tr>
              </table>
            </td>
          </tr>
	  <tr><th>7.3</th><td><a href="#7.3">Pointers and <code>NULL</code> Value Semantics</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>8</th><td><a href="#8">Inheritance</a>
        <table class="toc">
          <tr><th>8.1</th><td><a href="#8.1">Reuse Inheritance</a></td></tr>
	  <tr><th>8.2</th><td><a href="#8.2">Polymorphism Inheritance</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>9</th><td><a href="#9">Session</a>
        <table class="toc">
          <tr><th>9.1</th><td><a href="#9.1">Object Cache</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>10</th><td><a href="#10">ODB Pragma Language</a>
        <table class="toc">
          <tr>
            <th>10.1</th><td><a href="#10.1">Object Type Pragmas</a>
              <table class="toc">
                <tr><th>10.1.1</th><td><a href="#10.1.1"><code>table</code></a></td></tr>
		<tr><th>10.1.2</th><td><a href="#10.1.2"><code>pointer</code></a></td></tr>
		<tr><th>10.1.3</th><td><a href="#10.1.3"><code>abstract</code></a></td></tr>
		<tr><th>10.1.4</th><td><a href="#10.1.4"><code>callback</code></a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>10.2</th><td><a href="#10.2">Value Type Pragmas</a>
              <table class="toc">
                <tr><th>10.2.1</th><td><a href="#10.2.1"><code>type</code></a></td></tr>
		<tr><th>10.2.2</th><td><a href="#10.2.2"><code>id_type</code></a></td></tr>
		<tr><th>10.2.3</th><td><a href="#10.2.3"><code>null</code>/<code>not_null</code></a></td></tr>
		<tr><th>10.2.4</th><td><a href="#10.2.4"><code>default</code></a></td></tr>
		<tr><th>10.2.5</th><td><a href="#10.2.5"><code>options</code></a></td></tr>
		<tr><th>10.2.6</th><td><a href="#10.2.6"><code>unordered</code></a></td></tr>
		<tr><th>10.2.7</th><td><a href="#10.2.7"><code>index_type</code></a></td></tr>
		<tr><th>10.2.8</th><td><a href="#10.2.8"><code>key_type</code></a></td></tr>
		<tr><th>10.2.9</th><td><a href="#10.2.9"><code>value_type</code></a></td></tr>
		<tr><th>10.2.10</th><td><a href="#10.2.10"><code>value_null</code>/<code>value_not_null</code></a></td></tr>
		<tr><th>10.2.11</th><td><a href="#10.2.11"><code>id_options</code></a></td></tr>
		<tr><th>10.2.12</th><td><a href="#10.2.12"><code>index_options</code></a></td></tr>
		<tr><th>10.2.13</th><td><a href="#10.2.13"><code>key_options</code></a></td></tr>
		<tr><th>10.2.14</th><td><a href="#10.2.14"><code>value_options</code></a></td></tr>
		<tr><th>10.2.15</th><td><a href="#10.2.15"><code>id_column</code></a></td></tr>
		<tr><th>10.2.16</th><td><a href="#10.2.16"><code>index_column</code></a></td></tr>
		<tr><th>10.2.17</th><td><a href="#10.2.17"><code>key_column</code></a></td></tr>
		<tr><th>10.2.18</th><td><a href="#10.2.18"><code>value_column</code></a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>10.3</th><td><a href="#10.3">Data Member Pragmas</a>
              <table class="toc">
                <tr><th>10.3.1</th><td><a href="#10.3.1"><code>id</code></a></td></tr>
                <tr><th>10.3.2</th><td><a href="#10.3.2"><code>auto</code></a></td></tr>
                <tr><th>10.3.3</th><td><a href="#10.3.3"><code>type</code></a></td></tr>
		<tr><th>10.3.4</th><td><a href="#10.3.4"><code>null</code>/<code>not_null</code></a></td></tr>
		<tr><th>10.3.5</th><td><a href="#10.3.5"><code>default</code></a></td></tr>
		<tr><th>10.3.6</th><td><a href="#10.3.6"><code>options</code></a></td></tr>
		<tr><th>10.3.7</th><td><a href="#10.3.7"><code>column</code></a></td></tr>
		<tr><th>10.3.8</th><td><a href="#10.3.8"><code>transient</code></a></td></tr>
		<tr><th>10.3.9</th><td><a href="#10.3.9"><code>inverse</code></a></td></tr>
		<tr><th>10.3.10</th><td><a href="#10.3.10"><code>unordered</code></a></td></tr>
		<tr><th>10.3.11</th><td><a href="#10.3.11"><code>table</code></a></td></tr>
		<tr><th>10.3.12</th><td><a href="#10.3.12"><code>index_type</code></a></td></tr>
		<tr><th>10.3.13</th><td><a href="#10.3.13"><code>key_type</code></a></td></tr>
		<tr><th>10.3.14</th><td><a href="#10.3.14"><code>value_type</code></a></td></tr>
		<tr><th>10.3.15</th><td><a href="#10.3.15"><code>value_null</code>/<code>value_not_null</code></a></td></tr>
		<tr><th>10.3.16</th><td><a href="#10.3.16"><code>id_options</code></a></td></tr>
		<tr><th>10.3.17</th><td><a href="#10.3.17"><code>index_options</code></a></td></tr>
		<tr><th>10.3.18</th><td><a href="#10.3.18"><code>key_options</code></a></td></tr>
		<tr><th>10.3.19</th><td><a href="#10.3.19"><code>value_options</code></a></td></tr>
		<tr><th>10.3.20</th><td><a href="#10.3.20"><code>id_column</code></a></td></tr>
		<tr><th>10.3.21</th><td><a href="#10.3.21"><code>index_column</code></a></td></tr>
		<tr><th>10.3.22</th><td><a href="#10.3.22"><code>key_column</code></a></td></tr>
		<tr><th>10.3.23</th><td><a href="#10.3.23"><code>value_column</code></a></td></tr>
              </table>
            </td>
          </tr>
          <tr>
            <th>10.4</th><td><a href="#10.4">C++ Compiler Warnings</a>
              <table class="toc">
                <tr><th>10.4.1</th><td><a href="#10.4.1">GNU C++</a></td></tr>
                <tr><th>10.4.2</th><td><a href="#10.4.2">Visual C++</a></td></tr>
                <tr><th>10.4.3</th><td><a href="#10.4.3">Sun C++</a></td></tr>
		<tr><th>10.4.4</th><td><a href="#10.4.4">IBM XL C++</a></td></tr>
		<tr><th>10.4.5</th><td><a href="#10.4.5">HP aC++</a></td></tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>

    <tr>
      <th colspan="2"><a href="#II">PART II DATABASE SYSTEMS</a></th>
    </tr>

    <tr>
      <th>11</th><td><a href="#11">MySQL Database</a>
        <table class="toc">
          <tr><th>11.1</th><td><a href="#11.1">MySQL Type Mapping</a></td></tr>
          <tr><th>11.2</th><td><a href="#11.2">MySQL Database Class</a></td></tr>
          <tr><th>11.3</th><td><a href="#11.3">MySQL Connection and Connection Factory</a></td></tr>
	  <tr><th>11.4</th><td><a href="#11.4">MySQL Exceptions</a></td></tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>12</th><td><a href="#12">SQLite Database</a>
        <table class="toc">
          <tr><th>12.1</th><td><a href="#12.1">SQLite Type Mapping</a></td></tr>
          <tr><th>12.2</th><td><a href="#12.2">SQLite Database Class</a></td></tr>
          <tr><th>12.3</th><td><a href="#12.3">SQLite Connection and Connection Factory</a></td></tr>
	  <tr><th>12.4</th><td><a href="#12.4">SQLite Exceptions</a></td></tr>
          <tr>
            <th>12.5</th><td><a href="#12.5">SQLite Limitations</a>
              <table class="toc">
                <tr><th>12.5.1</th><td><a href="#12.5.1">Query Result Caching</a></td></tr>
		<tr><th>12.5.2</th><td><a href="#12.5.2">Automatic Assignment of Object Ids</a></td></tr>
		<tr><th>12.5.3</th><td><a href="#12.5.3">Foreign Key Constraints</a></td></tr>
		<tr><th>12.5.4</th><td><a href="#12.5.4">Constraint Violations</a></td></tr>
		<tr><th>12.5.5</th><td><a href="#12.5.5">Sharing of Queries</a></td></tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>13</th><td><a href="#13">PostgreSQL Database</a>
        <table class="toc">
          <tr><th>13.1</th><td><a href="#13.1">PostgreSQL Type Mapping</a></td></tr>
          <tr><th>13.2</th><td><a href="#13.2">PostgreSQL Database Class</a></td></tr>
          <tr><th>13.3</th><td><a href="#13.3">PostgreSQL Connection and Connection Factory</a></td></tr>
	  <tr><th>13.4</th><td><a href="#13.4">PostgreSQL Exceptions</a></td></tr>
          <tr>
            <th>13.5</th><td><a href="#13.5">PostgreSQL Limitations</a>
              <table class="toc">
                <tr><th>13.5.1</th><td><a href="#13.5.1">Query Result Caching</a></td></tr>
                <tr><th>13.5.2</th><td><a href="#13.5.2">Foreign Key Constraints</a></td></tr>
		<tr><th>13.5.3</th><td><a href="#13.5.3">Date-Time Format</a></td></tr>
		<tr><th>13.5.4</th><td><a href="#13.5.4">Timezones</a></td></tr>
		<tr><th>13.5.5</th><td><a href="#13.5.5"><code>NUMERIC</code> Type Support</a></td></tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>

    <tr>
      <th colspan="2"><a href="#III">PART III PROFILES</a></th>
    </tr>

    <tr>
      <th>14</th><td><a href="#14">Profiles Introduction</a></td>
    </tr>

    <tr>
      <th>15</th><td><a href="#15">Boost Profile</a>
        <table class="toc">
          <tr><th>15.1</th><td><a href="#15.1">Smart Pointers Library</a></td></tr>
          <tr><th>15.2</th><td><a href="#15.2">Unordered Containers Library</a></td></tr>
	  <tr><th>15.3</th><td><a href="#15.3">Optional Library</a></td></tr>
          <tr>
	    <th>15.4</th><td><a href="#15.4">Date Time Library</a>
	      <table class="toc">
	        <tr><th>15.4.1</th><td><a href="#15.4.1">MySQL Database Type Mapping</a></td></tr>
		<tr><th>15.4.2</th><td><a href="#15.4.2">SQLite Database Type Mapping</a></td></tr>
		<tr><th>15.4.3</th><td><a href="#15.4.3">PostgreSQL Database Type Mapping</a></td></tr>
	      </table>
	    </td>
	  </tr>
        </table>
      </td>
    </tr>

    <tr>
      <th>16</th><td><a href="#16">Qt Profile</a>
        <table class="toc">
          <tr>
	    <th>16.1</th><td><a href="#16.1">Basic Types Library</a>
	      <table class="toc">
	        <tr><th>16.1.1</th><td><a href="#16.1.1">MySQL Database Type Mapping</a></td></tr>
		<tr><th>16.1.2</th><td><a href="#16.1.2">SQLite Database Type Mapping</a></td></tr>
		<tr><th>16.1.3</th><td><a href="#16.1.3">PostgreSQL Database Type Mapping</a></td></tr>	      </table>
	    </td>
	  </tr>
          <tr><th>16.2</th><td><a href="#16.2">Smart Pointers Library</a></td></tr>
          <tr><th>16.3</th><td><a href="#16.3">Containers Library</a></td></tr>
          <tr>
	    <th>16.4</th><td><a href="#16.4">Date Time Library</a>
	      <table class="toc">
	        <tr><th>16.4.1</th><td><a href="#16.4.1">MySQL Database Type Mapping</a></td></tr>
		<tr><th>16.4.2</th><td><a href="#16.4.2">SQLite Database Type Mapping</a></td></tr>
		<tr><th>16.4.3</th><td><a href="#16.4.3">PostgreSQL Database Type Mapping</a></td></tr>
	      </table>
	    </td>
	  </tr>
        </table>
      </td>
    </tr>

  </table>
  </div>

  <hr class="page-break"/>
  <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>


  <!-- PART -->


  <hr class="page-break"/>
  <h1><a name="I">PART I&nbsp;&nbsp;
      <span style="font-weight: normal;">OBJECT-RELATIONAL MAPPING</span></a></h1>

  <p>Part I describes the essential database concepts, APIs, and tools that
     together comprise the object-relational mapping for C++ as implemented
     by ODB. It consists of the following chapters.</p>

  <table class="toc">
    <tr><th>1</th><td><a href="#1">Introduction</a></td></tr>
    <tr><th>2</th><td><a href="#2">Hello World Example</a></td></tr>
    <tr><th>3</th><td><a href="#3">Working with Persistent Objects</a></td></tr>
    <tr><th>4</th><td><a href="#4">Querying the Database</a></td></tr>
    <tr><th>5</th><td><a href="#5">Containers</a></td></tr>
    <tr><th>6</th><td><a href="#6">Relationships</a></td></tr>
    <tr><th>7</th><td><a href="#7">Value Types</a></td></tr>
    <tr><th>8</th><td><a href="#8">Inheritance</a></td></tr>
    <tr><th>9</th><td><a href="#9">Session</a></td></tr>
    <tr><th>10</th><td><a href="#10">ODB Pragma Language</a></td></tr>
  </table>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <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 -->


  <hr class="page-break"/>
  <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&nbsp;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&nbsp;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>. Beware that after executing this
    command, all the data stored in the <code>odb_test</code> database
    will be deleted.</p>

  <p>Note also that using a standalone generated SQL file is not the
     only way to create a database schema in ODB. We can also embed
     the schema directly into our application or use custom schemas
     that were not generated by the ODB compiler. Refer to
     <a href="#3.3">Section 3.3, "Database"</a> for details.</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::core;

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr&lt;database> db (new odb::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 ());

      // Make objects persistent and save their ids for later use.
      //
      john_id = db->persist (john);
      jane_id = db->persist (jane);
      joe_id = db->persist (joe);

      t.commit ();
    }
  }
  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>Then we have two <code>using namespace</code> directives. The first
     one brings in the names from the standard namespace and the second
     brings in the ODB declarations which we will use later in the file.
     Notice that in the second directive we use the <code>odb::core</code>
     namespace instead of just <code>odb</code>. The former only brings
     into the current namespace the essential ODB names, such as the
     <code>database</code> and <code>transaction</code> classes, without
     any of the auxiliary objects. This minimizes the likelihood of name
     conflicts with other libraries. Note also that you should continue
     using the <code>odb</code> namespace when qualifying individual names.
     For example, you should write <code>odb::database</code>, not
     <code>odb::core::database</code>.</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
     (<a href="#11.2">Section 11.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.
     As a convenience, the <code>persist()</code> function also returns
     a copy of the object's identifier that it made persistent. We
     save the returned identifier for each object in a local variable.
     We will use these identifiers later in the chapter to perform other
     database operations on our persistent objects.</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>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 (<a href="#3.12">Section 3.12, "ODB
     Exceptions"</a>) and printing the diagnostics.</p>

  <p>Let's now compile (<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. While here we use
     <code>std::auto_ptr</code> to manage the returned object, we
     could have also used another smart pointer, for example
     <code>shared_ptr</code> from TR1 or Boost. For more information
     on the object lifetime management and the smart pointers that we
     can use for that, see <a href="#3.2">Section 3.2, "Object
     Pointers"</a>.</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 -->


  <hr class="page-break"/>
  <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.3">Section 3.3</a> and transactions in
     <a href="#3.4">Section 3.4</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.
     An object has an identifier, called <em>object id</em>, that is
     unique among all instances of an object type within a database. 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 consists of data members which are either values
     (<a href="#7">Chapter 7, "Value Types"</a>), pointers
     to other objects (<a href="#6">Chapter 6, "Relationships"</a>), or
     containers of values or pointers to other objects (<a href="#5">Chapter
     5, "Containers")</a>. Pointers to other objects and containers can
     be viewed as special kinds of values since they also can only
     be stored in the database as part of an object.</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> (<a href="#7.2">Section 7.2,
     "Composite Value Types"</a>). Otherwise, the value is
     called <em>simple value</em> and its type &mdash; a
     <em>simple value type</em> (<a href="#7.1">Section 7.1,
     "Simple Value Types"</a>). 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>Understanding 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
{
  ...

  #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 (<a href="#10">Chapter 10, "ODB Pragma Language"</a>).</p>

  <p>Normally, an object class should define the default constructor. The
     generated database support code uses this constructor when
     instantiating an object from the persistent state. If we add the
     default constructor only for the database support code, then we
     can make it private. It is also possible to have an object type
     without the default constructor. However, in this case, the database
     operations can only load the persistent state into an existing instance
     (<a href="#3.8">Section 3.8, "Loading Persistent Objects"</a>,
     <a href="#4.4">Section 4.4, "Query Result"</a>). <p>The object id type
     should be default-constructible.</p></p>

  <p>If an object class has private or protected non-transient data
     members or if its default constructor is not public, then the
     <code>odb::access</code> class, defined in the
     <code>&lt;odb/core.hxx></code> header, should be declared a
     friend of this object type. For example:</p>

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

#pragma db object
class person
{
  ...

private:
  friend class odb::access;

  person () {}

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

  <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 we will need to provide the
     mapping to the database system type and, possibly, the code to
     convert between the two. For more information on how to achieve
     this refer to the <code>db&nbsp;type</code> pragma description
     in <a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>. Similar
     to object types, composite value types have to be explicitly declared
     as persistent using the <code>db&nbsp;value</code> pragma, for example:</p>

  <pre class="c++">
#pragma db value
class name
{
  ...

  std::string first_;
  std::string last_;
};
  </pre>

  <p>Composite value types are discussed in more detail in
     <a href="#7.2">Section 7.2, "Composite Value Types"</a>.</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 pointers 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 Object Pointers</a></h2>

  <p>As we have seen in the previous chapter, some database operations
     create dynamically allocated instances of persistent classes and
     return pointers to these instances. As we will see in later chapters,
     pointers are also used to establish relationships between objects
     (<a href="#6">Chapter 6, "Relationships"</a>) as well as to cache
     persistent objects in a session (<a href="#9">Chapter 9,
     "Session"</a>).</p>

  <p>By default, all these mechanisms use raw pointers to return,
     pass, and cache objects. This is normally sufficient for applications
     that have simple object lifetime requirements and do not use sessions
     or object relationships. In particular, a dynamically allocated object
     that is returned as a raw pointer from a database operation can be
     assigned to a smart pointer of our choice, for example
     <code>std::auto_ptr</code> or <code>shared_ptr</code> from TR1 or
     Boost.</p>

  <p>However, to avoid any possibility of a mistake, such as forgetting
     to use a smart pointer for a returned object, as well as to simplify the
     use of more advanced ODB functionality, such as sessions and bidirectional
     object relationships, it is recommended that you use smart pointers
     with the sharing semantics as object pointers. The <code>shared_ptr</code>
     smart pointer from TR1 or Boost is a good default choice.</p>

  <p>ODB provides two mechanisms for changing the object pointer type. We
     can use the <code>--default-pointer</code> option to specify the
     default object pointer type. All objects that don't have the object
     pointer explicitly specified with the <code>db&nbsp;pointer</code>
     pragma (see below) will use the default pointer type. Refer to the
     <a href="http://www.codesynthesis.com/products/odb/doc/odb.xhtml">ODB
     Compiler Command Line Manual</a> for details on this option's argument.
     The typical usage is shown below:</p>

  <pre class="terminal">
--default-pointer std::tr1::shared_ptr
  </pre>

  <p>The second mechanism allows us to specify the object pointer on
     an object by object basis using the <code>db&nbsp;pointer</code>
     pragma, for example:</p>

  <pre class="c++">
#pragma db object pointer(std::tr1::shared_ptr)
class person
{
  ...
};
  </pre>

  <p>Refer to <a href="#10.1.2">Section 10.1.2, "<code>pointer</code>"</a>
     for more information on this pragma.</p>

  <p>Built-in support that is provided by the ODB runtime library allows us
     to use the TR1 <code>shared_ptr</code> and <code>std::auto_ptr</code> as
     object pointers. Plus, ODB profile libraries, that are available for
     commonly used frameworks and libraries (such as Boost and Qt),
     provide support for smart pointers found in these frameworks and
     libraries (<a href="#III">Part III, "Profiles"</a>). It is also
     easy to add support for our own smart pointers, as described in
     <a href="#6.4"> Section 6.4, "Using Custom Smart Pointers"</a>.</p>

  <h2><a name="3.3">3.3 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. We 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. We 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 the 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="#II">Part II, "Database Systems"</a>.</p>

  <p>Before we can persist our objects, the corresponding database schema has
     to be created in the database. The schema contains table definitions and
     other relational database artifacts that are used to store the state of
     persistent objects in the database.</p>

  <p>There are several ways to create the database schema. The easiest is to
     instruct the ODB compiler to generate the corresponding schema from the
     persistent classes (<code>--generate-schema</code> option). The ODB
     compiler can generate the schema either as a standalone SQL file or
     embedded into the generated C++ code (<code>--schema-format</code>
     option).  If we are using the SQL file to create the database schema, then
     this file should be executed, normally only once, before the application
     is started.</p>

  <p>Alternatively, the schema can be embedded directly into the generated
     code and we can use the <code>odb::schema_catalog</code> class to
     create it in the database from within our application,
     for example:</p>

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

odb::transaction t (db->begin ());
odb::schema_catalog::create_schema (*db);
t.commit ();
  </pre>

  <p>Refer to the next section for information on the
     <code>odb::transaction</code> class.  The complete version of the above
     code fragment is available in the <code>schema/embedded</code> example in
     the <code>odb-examples</code> package.</p>

  <p>The <code>odb::schema_catalog</code> class has the following interface.
     You will need to include the <code>&lt;odb/schema-catalog.hxx></code>
     header file to make this class available in your application.</p>

  <pre class="c++">
namespace odb
{
  class schema_catalog
  {
  public:
    static void
    create_schema (database&amp;, const std::string&amp; name = "");
  };
}
  </pre>

  <p>The first argument to the <code>create_schema()</code> function
     is the database instance that we would like to create the schema in.
     The second argument is the schema name. By default, the ODB
     compiler generates all embedded schemas with the default schema
     name (empty string). However, if your application needs to
     have several separate schemas, you can use the
     <code>--default-schema</code> ODB compiler option to assign
     custom schema names and then use these names as a second argument
     to <code>create_schema()</code>. If the schema is not found,
     <code>create_schema()</code> throws the
     <code>odb::unknown_schema</code> exception. The
     <code>create_schema()</code> function should be called within
     a transaction.</p>

  <p>Finally, we can also use a custom database schema with ODB. This approach
     can work similarly to the standalone SQL file described above except that
     the database schema is hand-written or produced by another program. Or we
     could execute custom SQL statements that create the schema directly from
     our application. To map persistent classes to custom database schemas, ODB
     provides a wide range of mapping customization pragmas, such
     as <code>db&nbsp;table</code>, <code>db&nbsp;column</code>,
     and <code>db&nbsp;type</code> (<a href="#10">Chapter 10, "ODB Pragma
     Language"</a>). For sample code that shows how to perform such mapping
     for various C++ constructs, refer to the <code>schema/custom</code>
     example in the <code>odb-examples</code> package.</p>

  <h2><a name="3.4">3.4 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 either the
     <code>database::begin()</code> or <code>connection::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.
     For example:</p>

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

transaction t (db.begin ())

// Perform database operations.

t.commit ();
  </pre>

  <p>The <code>odb::transaction</code> class has the following
     interface:</p>

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

    transaction (transaction_impl*, bool make_current = true)

    void
    commit ();

    void
    rollback ();

    database_type&amp;
    database ();

    connection_type&amp;
    connection ();

    static bool
    has_current ();

    static transaction&amp;
    current ();

    static void
    current (transaction&amp;);

    static bool
    reset_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 we 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> accessor returns the database this
     transaction is working on. Similarly, the <code>connection()</code>
     accessor returns the database connection this transaction is on
     (<a href="#3.5">Section 3.5, "Connections"</a>).</p>

  <p>The static <code>current()</code> accessor 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. We can check whether there is a transaction in effect in
     this thread using the <code>has_current()</code> static function.</p>

  <p>The <code>make_current</code> argument in the <code>transaction</code>
     constructor as well as the static <code>current()</code> modifier and
     <code>reset_current()</code> function give us additional
     control over the nomination of the currently active transaction.
     If we pass <code>false</code> as the <code>make_current</code>
     argument, then the newly created transaction will not
     automatically be made the active transaction for this
     thread. Later, we can use the static <code>current()</code> modifier
     to set this transaction as the active transaction.
     The <code>reset_current()</code> static function clears the
     currently active transaction. Together, these mechanisms
     allow for more advanced use cases, such as multiplexing
     two or more transactions on the same thread. For example:</p>

  <pre class="c++">
transaction t1 (db1.begin ());        // Active transaction.
transaction t2 (db2.begin (), false); // Not active.

// Perform database operations on db1.

transaction::current (t2);            // Deactivate t1, activate t2.

// Perform database operations on db2.

transaction::current (t1);            // Switch back to t1.

// Perform some more database operations on db1.

t1.commit ();

transaction::current (t2);            // Switch to t2.

// Perform some more database operations on db2.

t2.commit ();
  </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.5">3.5 Connections</a></h2>

  <p>The <code>odb::connection</code> class represents a connection
     to the database. Normally, you wouldn't work with connections
     directly but rather let the ODB runtime obtain and release
     connections as needed. However, certain use cases may require
     obtaining a connection manually. For completeness, this section
     describes the <code>connection</code> class and discusses some
     of its use cases. You may want to skip this section if you are
     reading through the manual for the first time.</p>

  <p>Similar to <code>odb::database</code>, the <code>odb::connection</code>
     class is a common interface for all the database system-specific
     classes provided by ODB. For details on the system-specific
     <code>connection</code> classes, refer to <a href="#II">Part II,
     "Database Systems"</a>.</p>

  <p>To make the <code>odb::connection</code> class available in your
     application you will need to include the <code>&lt;odb/connection.hxx></code>
     header file. The <code>odb::connection</code> class has the
     following interface:</p>

  <pre>
namespace odb
{
  class connection
  {
  public:
    typedef odb::database database_type;

    transaction
    begin () = 0;

    unsigned long long
    execute (const char* statement);

    unsigned long long
    execute (const std::string&amp; statement);

    unsigned long long
    execute (const char* statement, std::size_t length);

    database_type&amp;
    database ();
  };

  typedef details::shared_ptr&lt;connection> connection_ptr;
}
  </pre>

  <p>The <code>begin()</code> function is used to start a transaction
     on the connection. The <code>execute()</code> functions allow
     us to execute native database statements on the connection.
     Their semantics are equivalent to the <code>database::execute()</code>
     functions (<a href="#3.11">Section 3.11, "Executing Native SQL
     Statements"</a>) except that they can be legally called outside
     a transaction. Finally, the <code>database()</code> accessor
     returns a reference to the <code>odb::database</code> instance
     to which this connection corresponds.</p>

  <p>To obtain a connection we call the <code>database::connection()</code>
     function. The connection is returned as <code>odb::connection_ptr</code>,
     which is an implementation-specific smart pointer with the shared
     pointer semantics. This, in particular, means that the connection
     pointer can be copied and returned from functions. Once the last
     instance of <code>connection_ptr</code> pointing to the same
     connection is destroyed, the connection is returned to the
     <code>database</code> instance. The following code fragment
     shows how we can obtain, use, and release a connection:</p>

  <pre>
using namespace odb::core;

database&amp; db = ...
connection_ptr c (db.connection ());

// Temporarily disable foreign key constraints.
//
c->execute ("SET FOREIGN_KEY_CHECKS = 0");

// Start a transaction on this connection.
//
transaction t (c->begin ());
...
t.commit ();

// Restore foreign key constraints.
//
c->execute ("SET FOREIGN_KEY_CHECKS = 1");

// When 'c' goes out of scope, the connection is returned to 'db'.
  </pre>

  <p>Some of the use cases which may require direct manipulation of
     connections include out-of-transaction statement execution,
     such as the execution of connection configuration statements,
     the implementation of a connection-per-thread policy, and making
     sure that a set of transactions is executed on the same
     connection.</p>

  <h2><a name="3.6">3.6 Error Handling and Recovery</a></h2>

  <p>ODB uses C++ exceptions to report database operation errors. Most
     ODB exceptions signify <em>hard</em> errors or errors that cannot
     be corrected without some intervention from the application.
     For example, if we try to load an object with an unknown object
     id, the <code>odb::object_not_persistent</code> exception is
     thrown. Our application may be able to correct this error, for
     instance, by obtaining a valid object id and trying again.
     The hard errors and corresponding ODB exceptions that can be
     thrown by each database function are described in the remainder
     of this chapter with <a href="#3.12">Section 3.12, "ODB Exceptions"</a>
     providing a quick reference for all the ODB exceptions.</p>

  <p>The second group of ODB exceptions signify <em>soft</em> or
     <em>recoverable</em> errors. Such errors are temporary
     failures which normally can be corrected by simply re-executing
     the transaction. ODB defines three such exceptions:
     <code>odb::connection_lost</code>, <code>odb::timeout</code>,
     and <code>odb::deadlock</code>. All recoverable ODB exceptions
     are derived from the common <code>odb::recoverable</code> base
     exception which can be used to handle all the recoverable
     conditions with a single <code>catch</code> block.</p>

  <p>The <code>odb::connection_lost</code> exception is thrown if
     a connection to the database is lost in the middle of
     a transaction. In this situation the transaction is aborted but
     it can be re-tried without any changes. Similarly, the
     <code>odb::timeout</code> exception is thrown if one of the
     database operations or the whole transaction has timed out.
     Again, in this case the transaction is aborted but can be
     re-tried as is.</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>
     recoverable exception being thrown from one of the database functions.</p>

  <p>The following code fragment shows how to handle the recoverable
     exceptions by restarting the affected transaction:</p>

  <pre class="c++">
const unsigned short max_retries = 5;

for (unsigned short retry_count (0); ; retry_count++)
{
  try
  {
    transaction t (db.begin ());

    ...

    t.commit ();
    break;
  }
  catch (const odb::recoverable&amp; e)
  {
    if (retry_count > max_retries)
      throw retry_limit_exceeded (e.what ());
    else
      continue;
  }
}
  </pre>

  <h2><a name="3.7">3.7 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 four
     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 (const object_traits&lt;T>::const_pointer_type&amp; object);

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

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

  <p>Here and in the rest of the manual,
     <code>object_traits&lt;T>::pointer_type</code> and
     <code>object_traits&lt;T>::const_pointer_type</code> denote the
     unrestricted and constant object pointer types (<a href="#3.2">Section
     3.2, "Object Pointer"</a>), respectively.
     Similarly, <code>object_traits&lt;T>::id_type</code> denotes the object
     id type. The <code>odb::object_traits</code> template is part of the
     database support code generated by the ODB compiler.</p>

  <p>The first <code>persist()</code> function expects a constant reference
     to an instance being persisted. The second function expects a constant
     object pointer. Both of these functions can only be used on objects with
     application-assigned object ids (<a href="#10.3.2">Section 10.3.2,
     "<code>auto</code>"</a>).</p>

  <p>The second and third <code>persist()</code> functions are similar to the
     first two except that they operate on unrestricted references and object
     pointers. If the identifier of the object being persisted is assigned
     by the database, these functions update the id member of the passed
     instance with the assigned value. All four functions return the object
     id of the newly persisted 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 following example shows
     how we can call these functions:</p>

<pre class="c++">
person john ("John", "Doe", 33);
shared_ptr&lt;person> jane (new person ("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 released and made available to other
     threads in your application and to other applications as soon as
     possible.</p>

  <h2><a name="3.8">3.8 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
     (<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.9">3.9 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. This function has three overloaded versions with
     the following signatures:</p>

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

  template &lt;typename T>
  void
  update (const object_traits&lt;T>::const_pointer_type&amp; object);

  template &lt;typename T>
  void
  update (const object_traits&lt;T>::pointer_type&amp; object);
  </pre>

  <p>The first <code>update()</code> function expects an object reference,
     while the other two expect object pointers. If the object passed to
     one of these functions 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>

  <p>The same can be accomplished using dynamically allocated objects
     and the <code>update()</code> function with object pointer argument,
     for example:</p>

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

shared_ptr&lt;bank_account> from (db.load&lt;bank_account> (from_acc));

if (from->balance () &lt; amount)
  throw insufficient_funds ();

shared_ptr&lt;bank_account> to (db.load&lt;bank_account> (to_acc));

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

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

t.commit ();
  </pre>

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

  <p>To delete a persistent object's state from the database we use the
     <code>database::erase()</code> or <code>database::erase_query()</code>
     function templates. 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 object_traits&lt;T>::const_pointer_type&amp; object);

  template &lt;typename T>
  void
  erase (const object_traits&lt;T>::pointer_type&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, in
     the form of an object reference, to delete its state from the
     database. The next two functions accomplish the same result but using
     object pointers. Note that all three functions leave the passed
     object unchanged. It simply becomes transient. The last function
     uses the object id to identify the object to be deleted. If the
     object does not exist in the database, then all four functions
     throw the <code>odb::object_not_persistent</code> exception.</p>

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

  <pre class="c++">
person&amp; john = ...
shared_ptr&lt;jane> jane = ...
unsigned long joe_id = ...

transaction t (db.begin ());

db.erase (john);
db.erase (jane);
db.erase&lt;person> (joe_id);

t.commit ();
  </pre>

  <p>The <code>erase_query()</code> function allows us to delete
     the state of multiple objects matching certain criteria. It uses
     the query expression of the <code>database::query()</code> function
     (<a href="#4">Chapter 4, "Querying the Database"</a>) and,
     because the ODB query facility is optional, it is only available
     if the <code>--generate-query</code> ODB compiler option was
     specified. The <code>erase_query()</code> function has the
     following overloaded versions:</p>

  <pre class="c++">
  template &lt;typename T>
  unsigned long long
  erase_query ();

  template &lt;typename T>
  unsigned long long
  erase_query (const odb::query&lt;T>&amp;);
  </pre>

  <p>The first <code>erase_query()</code> function is used to delete
     the state of all the persistent objects of a given type stored
     in the database. The second function uses the passed query instance
     to only delete the state of objects matching the query criteria.
     Both functions return the number of objects erased. When calling
     the <code>erase_query()</code> function, we have to explicitly
     specify the object type we are erasing. For example:</p>

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

transaction t (db.begin ());

db.erase_query&lt;person> (query::last == "Doe" &amp;&amp; query::are &lt; 30);

t.commit ();
  </pre>

  <p>Unlike the <code>query()</code> function, when calling
     <code>erase_query()</code> we cannot use members from pointed-to
     objects in the query expression. However, we can still use
     a member corresponding to a pointer as an ordinary object
     member that has the id type of the pointed-to object
     (<a href="#6">Chapter 6, "Relationships"</a>). This allows us
     to compare object ids as well as test the pointer for
     <code>NULL</code>. As an example, the following transaction
     makes sure that all the <code>employee</code> objects that
     reference an <code>employer</code> object that is about to
     be deleted are deleted as well. Here we assume that the
     <code>employee</code> class contains a pointer to the
     <code>employer</code> class. Refer to <a href="#6">Chapter 6,
     "Relationships"</a> for complete definitions of these
     classes.</p>

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

transaction t (db.begin ());

employer&amp; e = ... // Employer object to be deleted.

db.erase_query&lt;employee> (query::employer == e.id ());
db.erase (e);

t.commit ();
  </pre>


  <h2><a name="3.11">3.11 Executing Native SQL Statements</a></h2>

  <p>In some situations we may need to execute native SQL statements
     instead of using the object-oriented database API described above.
     For example, we may want to tune the database schema generated
     by the ODB compiler or take advantage of a feature that is
     specific to the database system we are using. The
     <code>database::execute()</code> function, which has three
     overloaded versions, provides this functionality:</p>

  <pre class="c++">
  unsigned long long
  execute (const char* statement);

  unsigned long long
  execute (const std::string&amp; statement);

  unsigned long long
  execute (const char* statement, std::size_t length)
  </pre>

  <p>The first <code>execute()</code> function expects the SQL statement
     as a zero-terminated C-string. The last version expects the explicit
     statement length as the second argument and the statement itself
     may contain <code>'\0'</code> characters, for example, to represent
     binary data, if the database system supports it. All three functions
     return the number of rows that were affected by the statement. For
     example:</p>

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

db.execute ("DROP TABLE test");
db.execute ("CREATE TABLE test (n INT PRIMARY KEY)");

t.commit ();
  </pre>

  <p>While these functions must always be called within a transaction,
     it may be necessary to execute a native statement outside a
     transaction. This can be done using the
     <code>connection::execute()</code> functions as described in
     <a href="#3.5">Section 3.5, "Connections"</a>.</p>

  <h2><a name="3.12">3.12 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 derives
     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 we 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 null_pointer: exception
  {
    virtual const char*
    what () const throw ();
  };

  // Transaction exceptions.
  //
  struct already_in_transaction: exception
  {
    virtual const char*
    what () const throw ();
  };

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

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

  // Session exceptions.
  //
  struct already_in_session: exception
  {
    virtual const char*
    what () const throw ();
  };

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

  // Database operations exceptions.
  //
  struct recoverable: exception
  {
  };

  struct connection_lost: recoverable
  {
    virtual const char*
    what () const throw ();
  };

  struct timeout: recoverable
  {
    virtual const char*
    what () const throw ();
  };

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

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

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

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

  struct database_exception: exception
  {
  };

  // Schema catalog exceptions.
  //
  struct unknown_schema: exception
  {
    const std::string&amp;
    name () const;

    virtual const char*
    what () const throw ();
  };
}
  </pre>

  <p>The <code>null_pointer</code> exception is thrown when a
     pointer to a persistent object declared non-<code>NULL</code>
     with the <code>db&nbsp;not_null</code> or
     <code>db&nbsp;value_not_null</code> pragma has the <code>NULL</code>
     value. See <a href="#6">Chapter 6, "Relationships"</a> for details.</p>

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

  <p>The next two exceptions (<code>already_in_session</code>, and
     <code>not_in_session</code>) are thrown by the <code>odb::session</code>
     class and are discussed in <a href="#9">Chapter 9, "Session"</a>.</p>

  <p>The <code>recoverable</code> exception serves as a common base
     for all the recoverable exceptions, which are: <code>connection_lost</code>,
     <code>timeout</code>, and <code>deadlock</code>. The
     <code>connection_lost</code> exception is thrown when a connection
     to the database is lost. Similarly, the <code>timeout</code> exception
     is thrown if one of the database operations or the whole transaction
     has timed out. The <code>deadlock</code> exception is thrown when a
     transaction deadlock is detected by the database system. These
     exceptions can be thrown by any database function. See
     <a href="#3.6">Section 3.6, "Error Handling and Recovery"</a>
     for details.</p>

  <p>The <code>object_already_persistent</code> exception is thrown
     by the <code>persist()</code> database function. See
     <a href="#3.7">Section 3.7, "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.8">Section 3.8, "Loading Persistent Objects"</a> and
     <a href="#3.9">Section 3.9, "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> exception is a base class for all
     database system-specific exceptions that are thrown by the
     database system-specific runtime library. Refer to <a href="#II">Part
     II, "Database Systems"</a> for more information.</p>

  <p>The <code>unknown_schema</code> exception is thrown by the
     <code>odb::schema_catalog</code> class if a schema with the specified
     name is not found. Refer to <a href="#3.3">Section 3.3, "Database"</a>
     for details.</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 -->


  <hr class="page-break"/>
  <h1><a name="4">4 Querying the Database</a></h1>

  <p>If we don't know the identifiers of the objects that we are looking
     for, we can use queries to search the database for objects matching
     certain criteria. The ODB query facility is optional and we 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 we 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
     us 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 we 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>We 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 we 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. We 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 us 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 we 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, we would create a named query instance if we are
     planning to run the same query multiple times and would use the
     in-line version for those that are executed only once. A named
     query instance that does not have any by-reference parameters is
     immutable and can be shared between multiple threads without
     synchronization. On the other hand, a query instance with
     by-reference parameters is modified every time it is executed.
     If such a query is shared among multiple threads, then access
     to this query instance must be synchronized from the execution
     point and until the completion of the iteration over the result.</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 we 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 us 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 we have a large
     number of objects in the result or if we 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 we 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 we have
     two iterators pointing to the current position and then we
     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, we 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 we 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 we 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 us 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
     us 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 -->

  <hr class="page-break"/>
  <h1><a name="5">5 Containers</a></h1>

  <p>The ODB runtime library provides built-in persistence support for all the
     commonly used standard C++ containers, namely,
     <code>std::vector</code>, <code>std::list</code>, <code>std::set</code>,
     <code>std::multiset</code>, <code>std::map</code>, and
     <code>std::multimap</code>. Plus, ODB profile libraries, that are
     available for commonly used frameworks and libraries (such as Boost and
     Qt), provide persistence support for containers found in these frameworks
     and libraries (<a href="#III">Part III, "Profiles"</a>).  It is also easy
     to persist custom container types as discussed later
     in <a href="#5.4">Section 5.4, "Using Custom Containers"</a>.</p>

  <p>We don't need to do anything special to declare a member of a
     container type in a persistent class. For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  std::vector&lt;std::string> nicknames_;
  ...
};
  </pre>

  <p>The complete version of the above code fragment and the other code
     samples presented in this chapter can be found in the <code>container</code>
     example in the <code>odb-examples</code> package.</p>

  <p>A data member in a persistent class that is of a container type
     behaves like a value type. That is, when an object is made persistent,
     the elements of the container are stored in the database. Similarly,
     when a persistent object is loaded from the database, the contents
     of the container are automatically loaded as well. A data member
     of a container type can also use a smart pointer, as discussed
     in <a href="#7.3">Section 7.3, "Pointers and <code>NULL</code>
     Value Semantics"</a>.</p>

  <p>While an ordinary member is mapped to one or more columns in the
     object's table, a member of a container type is mapped to a separate
     table. The exact schema of such a table depends on the kind of
     container. ODB defines the following container kinds: ordered,
     set, multiset, map, and multimap. The container kinds and the
     contents of the tables to which they are mapped are discussed
     in detail in the following sections.</p>

  <p>Containers in ODB can contain simple value types (<a href="#7.1">Section
     7.1, "Simple Value Types"</a>), composite value types
     (<a href="#7.2">Section 7.2, "Composite Value Types"</a>), and pointers
     to objects (<a href="#6">Chapter 6, "Relationships"</a>). Containers of
     containers, either directly or indirectly via a composite value
     type, are not allowed. A key in a map or multimap container can
     be a simple or composite value type but not a pointer to an object.
     An index in the ordered container should be a simple integer value
     type.</p>

  <p>The value type in the ordered, set, and map containers as well as
     the key type in the map containers should be default-constructible.
     The default constructor in these types can be made private in which
     case the <code>odb::access</code> class should be made a friend of
     the value or key type. For example:</p>

  <pre class="c++">
#pragma db value
class name
{
public:
  name (const std::string&amp;, const std::string&amp;);
  ...
private:
  friend class odb::access;
  name ();
  ...
};

#pragma db object
class person
{
  ...
private:
  std::vector&lt;name> aliases_;
  ...
};
  </pre>


  <h2><a name="5.1">5.1 Ordered Containers</a></h2>

  <p>In ODB an ordered container is any container that maintains (explicitly
     or implicitly) an order of its elements in the form of an integer index.
     Standard C++ containers that are ordered include <code>std::vector</code>
     and <code>std::list</code>. While elements in <code>std::set</code>
     are also kept in a specific order, this order is not based on an
     integer index but rather on the relationship between elements. As
     a result, <code>std::set</code> is not considered an ordered
     container for the purpose of persistence.</p>

  <p>The database table for an ordered container consists of at least
     three columns. The first column contains the object id of a
     persistent class instance of which the container is a member.
     The second column contains the element index within a container.
     And the last column contains the element value. If the object
     id or element value are composite, then, instead of a single
     column, they can occupy multiple columns.</p>

  <p>Consider the following persistent object as an example:</p>

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

  std::vector&lt;std::string> nicknames_;
  ...
};
  </pre>

  <p>The resulting database table (called <code>person_nicknames</code>) will
     contain the object id column of type <code>unsigned long</code>
     (called <code>object_id</code>), the index column of an integer type
     (called <code>index</code>), and the value column of type
     <code>std::string</code> (called <code>value</code>).</p>

  <p>A number of ODB pragmas allow us to customize the table name, column
     names, and native database types of an ordered container both, on
     the per-container and per-member basis. For more information on
     these pragmas, refer to <a href="#10">Chapter 10, "ODB Pragma
     Language"</a>. The following example shows some of the possible
     customizations:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db table("nicknames")              \
             id_column("person_id")          \
             index_type("SMALLINT UNSIGNED") \
             index_column("nickname_number") \
             value_type("VARCHAR(255)")      \
             value_column("nickname")
  std::vector&lt;std::string> nicknames_;
  ...
};
  </pre>

  <p>While the C++ container used in a persistent class may be ordered,
     sometimes we may wish to store such a container in the database without
     the order information. In the example above, for instance, the order
     of person's nicknames is probably not important. To instruct the ODB
     compiler to ignore the order in ordered containers we can use the
     <code>db&nbsp;unordered</code> pragma (<a href="#10.2.6">Section 10.2.6,
     "<code>unordered</code>"</a>, <a href="#10.3.10">Section 10.3.10,
     "<code>unordered</code>"</a>). For example:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db unordered
  std::vector&lt;std::string> nicknames_;
  ...
};
  </pre>

  <p>The table for an ordered container that is marked unordered won't
     have the index column and the order in which elements are retrieved
     from the database may not be the same as the order in which they
     were stored.</p>

  <h2><a name="5.2">5.2 Set and Multiset Containers</a></h2>

  <p>In ODB set and multiset containers (referred to as just set
     containers) are associative containers that contain elements
     based on some relationship between them. A set container may
     or may not guarantee a particular order of the elements that
     it stores. Standard C++ containers that are considered set
     containers for the purpose of persistence include
     <code>std::set</code> and <code>std::multiset</code>.</p>

  <p>The database table for a set container consists of at least
     two columns. The first column contains the object id of a
     persistent class instance of which the container is a member.
     And the second column contains the element value. If the object
     id or element value are composite, then, instead of a single
     column, they can occupy multiple columns.</p>

  <p>Consider the following persistent object as an example:</p>

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

  std::set&lt;std::string> emails_;
  ...
};
  </pre>

  <p>The resulting database table (called <code>person_emails</code>) will
     contain the object id column of type <code>unsigned long</code>
     (called <code>object_id</code>) and the value column of type
     <code>std::string</code> (called <code>value</code>).</p>

  <p>A number of ODB pragmas allow us to customize the table name,
     column names, and native database types of a set container, both on
     the per-container and per-member basis. For more information on
     these pragmas, refer to <a href="#10">Chapter 10, "ODB Pragma
     Language"</a>. The following example shows some of the possible
     customizations:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db table("emails")            \
             id_column("person_id")     \
             value_type("VARCHAR(255)") \
             value_column("email")
  std::set&lt;std::string> emails_;
  ...
};
  </pre>

  <h2><a name="5.3">5.3 Map and Multimap Containers</a></h2>

  <p>In ODB map and multimap containers (referred to as just map
     containers) are associative containers that contain key-value
     elements based on some relationship between keys. A map container
     may or may not guarantee a particular order of the elements that
     it stores. Standard C++ containers that are considered map
     containers for the purpose of persistence include
     <code>std::map</code> and <code>std::multimap</code>.</p>

  <p>The database table for a map container consists of at least
     three columns. The first column contains the object id of a
     persistent class instance of which the container is a member.
     The second column contains the element key. And the last column
     contains the element value. If the object id, element key, or
     element value are composite, then instead of a single column
     they can occupy multiple columns.</p>

  <p>Consider the following persistent object as an example:</p>

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

  std::map&lt;unsigned short, float> age_weight_map_;
  ...
};
  </pre>

  <p>The resulting database table (called <code>person_age_weight_map</code>)
     will contain the object id column of type <code>unsigned long</code>
     (called <code>object_id</code>), the key column of type
     <code>unsigned short</code> (called <code>key</code>), and the value
     column of type <code>float</code> (called <code>value</code>).</p>

  <p>A number of ODB pragmas allow us to customize the table name,
     column names, and native database types of a map container, both on
     the per-container and per-member basis. For more information on
     these pragmas, refer to <a href="#10">Chapter 10, "ODB Pragma
     Language"</a>. The following example shows some of the possible
     customizations:</p>

  <pre class="c++">
#pragma db object
class person
{
  ...
private:
  #pragma db table("weight_map")      \
             id_column("person_id")   \
             key_type("INT UNSIGNED") \
             key_column("age")        \
             value_type("DOUBLE")     \
             value_column("weight")
  std::map&lt;unsigned short, float> age_weight_map_;
  ...
};
  </pre>

  <h2><a name="5.4">5.4 Using Custom Containers</a></h2>

  <p>While the ODB runtime and profile libraries provide support for
     a wide range of containers, it is also easy to persist custom
     container types.</p>

  <p>To achieve this you will need to implement the
     <code>container_traits</code> class template specialization for
     your container. First, determine the container kind (ordered, set,
     multiset, map, or multimap) for your container type. Then use a
     specialization for one of the standard C++ containers found in
     the common ODB runtime library (<code>libodb</code>) as a base
     for your own implementation.</p>

  <p>Once the container traits specialization is ready for your container,
     you will need to include it into the ODB compilation process using
     the <code>--odb-epilogue</code> option and into the generated header
     files with the <code>--hxx-prologue</code> option. As an example,
     suppose we have a hash table container for which we have the traits
     specialization implemented in the <code>hashtable-traits.hxx</code>
     file. Then, we can create an ODB compiler options file for this
     container and save it to <code>hashtable.options</code>:</p>

  <pre>
# Options file for the hash table container.
#
--odb-epilogue '#include "hashtable-traits.hxx"'
--hxx-prologue '#include "hashtable-traits.hxx"'
  </pre>

  <p>Now, whenever we compile a header file that uses the hashtable
     container, we can specify the following command line option to
     make sure it is recognized by the ODB compiler as a container
     and the traits file is included in the generated code:</p>

  <pre>
--options-file hashtable.options
  </pre>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="6">6 Relationships</a></h1>

  <p>Relationships between persistent objects are expressed with pointers or
     containers of pointers. The ODB runtime library provides built-in support
     for the TR1 <code>shared_ptr</code>/<code>weak_ptr</code>,
     <code>std::auto_ptr</code>, and raw pointers. Plus, ODB profile
     libraries, that available for commonly used frameworks and libraries
     (such as Boost and Qt), provide support for smart pointers found in these
     frameworks and libraries (<a href="#III">Part III, "Profiles"</a>). It is
     also easy to add support for a custom smart pointer as discussed later
     in <a href="#6.4"> Section 6.4, "Using Custom Smart Pointers"</a>. Any
     supported smart pointer can be used in a data member as long as it can be
     explicitly constructed from the canonical object pointer
     (<a href="#3.2">Section 3.2, "Object Pointers"</a>).  For example, we can
     use <code>weak_ptr</code> if the object pointer
     is <code>shared_ptr</code>.</p>

  <p>When an object containing a pointer to another object is loaded,
     the pointed-to object is loaded as well. In some situations this
     eager loading of the relationships is undesirable since it
     can lead to a large number of otherwise unused objects being
     instantiated from the database. To support finer control
     over relationships loading, the ODB runtime and profile
     libraries provide the so-called <em>lazy</em> versions of
     the supported pointers. An object pointed-to by a lazy pointer
     is not loaded automatically when the containing object is loaded.
     Instead, we have to explicitly request the instantiation of the
     pointed-to object. Lazy pointers are discussed in
     detail in <a href="#6.3">Section 6.3, "Lazy Pointers"</a>.</p>

  <p>As a simple example, consider the following employee-employer
     relationship. Code examples presented in this chapter
     will use the <code>shared_ptr</code> and <code>weak_ptr</code>
     smart pointers from the TR1 (<code>std::tr1</code>) namespace.</p>

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

  #pragma db id
  std::string name_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  std::string first_name_;
  std::string last_name_;

  shared_ptr&lt;employer> employer_;
};
  </pre>

  <p>By default, an object pointer can be <code>NULL</code>. To
     specify that a pointer always points to a valid object we can
     use the <code>not_null</code> pragma (<a href="#10.3.4">Section
     10.3.4, "<code>null</code>/<code>not_null</code>"</a>) for
     single object pointers and the <code>value_not_null</code> pragma
     (<a href="#10.3.15">Section
     10.3.15, "<code>value_null</code>/<code>value_not_null</code>"</a>)
     for containers of object pointers. For example:</p>

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

  #pragma db not_null
  shared_ptr&lt;employer> current_employer_;

  #pragma db value_not_null
  std::vector&lt;shared_ptr&lt;employer> > previous_employers_;
};
  </pre>

  <p>In this case, if we perform a database operation on the
     <code>employee</code> object and the <code>current_employer_</code>
     pointer or one of the pointers stored in the
     <code>previous_employers_</code> container is <code>NULL</code>,
     then the <code>odb::null_pointer</code> exception will be thrown.</p>

  <p>We don't need to do anything special to establish or navigate a
     relationship between two persistent objects, as shown in the
     following code fragment:</p>

  <pre class="c++">
// Create an employer and a few employees.
//
unsigned long john_id, jane_id;
{
  shared_ptr&lt;employer> er (new employer ("Example Inc"));
  shared_ptr&lt;employee> john (new employee ("John", "Doe"));
  shared_ptr&lt;employee> jane (new employee ("Jane", "Doe"));

  john->employer_ = er;
  jane->employer_ = er;

  transaction t (db.begin ());

  db.persist (er);
  john_id = db.persist (john);
  jane_id = db.persist (jane);

  t.commit ();
}

// Load a few employee objects and print their employer.
//
{
  session s;
  transaction t (db.begin ());

  shared_ptr&lt;employee> john (db.load&lt;employee> (john_id));
  shared_ptr&lt;employee> jane (db.load&lt;employee> (jane_id));

  cout &lt;&lt; john->employer_->name_ &lt;&lt; endl;
  cout &lt;&lt; jane->employer_->name_ &lt;&lt; endl;

  t.commit ();
}
  </pre>

  <p>The only notable line in the above code is the creation of a
     session before the second transaction starts. As discussed in
     <a href="#9">Chapter 9, "Session"</a>, a session acts as a cache
     of persistent objects.
     By creating a session before loading the <code>employee</code>
     objects we make sure that their <code>employer_</code> pointers
     point to the same <code>employer</code> object. Without a
     session, each <code>employee</code> would have ended up pointing
     to its own, private instance of the Example Inc employer.</p>

  <p>As a general guideline, you should use a session when loading
     objects that have pointers to other persistent objects. A
     session makes sure that for a given object id, a single instance
     is shared among all other objects that relate to it.</p>

  <p>We can also use data members from pointed-to
     objects in database queries (<a href="#4">Chapter 4, "Querying the
     Database"</a>). For each pointer in a persistent class, the query
     class defines a smart pointer-like member that contains members
     corresponding to the data members in the pointed-to object. We
     can then use the access via pointer syntax (<code>-></code>)
     to refer to data members in pointed-to objects.
     For example, the query class for the <code>employee</code> object
     contains the <code>employer</code> member (its name is derived from the
     <code>employer_</code> pointer) which in turn contains the
     <code>name</code> member (its name is derived from the
     <code>employer::name_</code> data member of the pointed-to object).
     As a result, we can use the <code>query::employer->name</code>
     expression while querying the database for the <code>employee</code>
     objects. For example, the following transaction finds all the
     employees of Example Inc that have the Doe last name:</p>

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

session s;
transaction t (db.begin ());

result r (db->query&lt;employee> (
  query::employer->name == "Example Inc" &amp;&amp; query::last == "Doe"));

for (result::iterator i (r.begin ()); i != r.end (); ++i)
  cout &lt;&lt; i->first_ &lt;&lt; " " &lt;&lt; i->last_ &lt;&lt; endl;

t.commit ();
  </pre>

  <p>A query class member corresponding to a non-inverse
     (<a href="#6.2">Section 6.2, "Bidirectional Relationships"</a>) object
     pointer can also be used as a normal member that has the id type
     of the pointed-to object. For example, the following query locates
     all the <code>employee</code> objects that don't have an associated
     <code>employer</code> object:</p>

  <pre class="c++">
result r (db->query&lt;employee> (query::employer.is_null ()));
  </pre>

  <p>An important concept to keep in mind when working with object
     relationships is the independence of persistent objects. In particular,
     when an object containing a pointer to another object is made persistent
     or is updated, the pointed-to object is not automatically persisted
     or updated. Rather, only a reference to the object (in the form of the
     object id) is stored for the pointed-to object in the database.
     The pointed-to object itself is a separate entity and should
     be made persistent or updated independently.</p>

  <p>When persisting or updating an object containing a pointer to another
     object, the pointed-to object must have a valid object id. This,
     however, may not always be easy to achieve in complex relationships that
     involve objects with automatically assigned identifiers. In such
     cases it may be necessary to first persist an object with a pointer
     set to <code>NULL</code> and then, once the pointed-to object is
     made persistent and its identifier assigned, set the pointer
     to the correct value and update the object in the database.</p>

  <p>Persistent object relationships can be divided into two groups:
     unidirectional and bidirectional. Each group in turn contains
     several configurations that vary depending on the cardinality
     of the sides of the relationship. All possible unidirectional
     and bidirectional configurations are discussed in the following
     sections.</p>

  <h2><a name="6.1">6.1 Unidirectional Relationships</a></h2>

  <p>In unidirectional relationships we are only interested in navigating
     from object to object in one direction. Because there is no interest
     in navigating in the opposite direction, the cardinality of the other
     end of the relationship is unimportant. As a result, there are only
     two possible unidirectional relationships: to-one and to-many. Each
     of these relationships is described in the following sections. For
     sample code that shows how to work with these relationships, refer
     to the <code>relationship</code> example in the <code>odb-examples</code>
     package.</p>

  <h3><a name="6.1.1">6.1.1 To-One Relationships</a></h3>

  <p>An example of a unidirectional to-one relationship is the
     employee-employer relationship (an employee has one employer).
     The following persistent C++ classes model this relationship:</p>

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

  #pragma db id
  std::string name_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db not_null
  shared_ptr&lt;employer> employer_;
};
  </pre>

  <p>The corresponding database tables look like this:</p>

  <pre class="sql">
CREATE TABLE employer (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  employer VARCHAR (255) NOT NULL REFERENCES employer (name));
  </pre>

  <h3><a name="6.1.2">6.1.2 To-Many Relationships</a></h3>

  <p>An example of a unidirectional to-many relationship is the
     employee-project relationship (an employee can be involved
     in multiple projects). The following persistent C++ classes
     model this relationship:</p>

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

  #pragma db id
  std::string name_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db value_not_null unordered
  std::vector&lt;shared_ptr&lt;project> > projects_;
};
  </pre>

  <p>The corresponding database tables look like this:</p>

  <pre class="sql">
CREATE TABLE project (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);

CREATE TABLE employee_projects (
  object_id BIGINT UNSIGNED NOT NULL,
  value VARCHAR (255) NOT NULL REFERENCES project (name));
  </pre>

  <p>To obtain a more canonical database schema, the names of tables
     and columns above can be customized using ODB pragmas
     (<a href="#10">Chapter 10, "ODB Pragma Language"</a>). For example:</p>

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

  #pragma db value_not_null unordered \
             id_column("employee_id") value_column("project_name")
  std::vector&lt;shared_ptr&lt;project> > projects_;
};
  </pre>

  <p>The resulting <code>employee_projects</code> table would then
     look like this:</p>

  <pre class="sql">
CREATE TABLE employee_projects (
  employee_id BIGINT UNSIGNED NOT NULL,
  project_name VARCHAR (255) NOT NULL REFERENCES project (name));
  </pre>


  <h2><a name="6.2">6.2 Bidirectional Relationships</a></h2>

  <p>In bidirectional relationships we are interested in navigating
     from object to object in both directions. As a result, each
     object class in a relationship contains a pointer to the other
     object. If smart pointers are used, then a weak pointer should
     be used as one of the pointers to avoid ownership cycles. For
     example:</p>

  <pre class="c++">
class employee;

#pragma db object
class position
{
  ...

  #pragma db id
  unsigned long id_;

  weak_ptr&lt;employee> employee_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db not_null
  shared_ptr&lt;position> position_;
};
  </pre>

  <p>Note that when we establish a bidirectional relationship, we
     have to set both pointers consistently. One way to make sure
     that a relationship is always in a consistent state is to
     provide a single function that updates both pointers at the
     same time. For example:</p>

  <pre class="c++">
#pragma db object
class position: public enable_shared_from_this&lt;position>
{
  ...

  void
  fill (shared_ptr&lt;employee> e)
  {
    employee_ = e;
    e->positions_ = shared_from_this ();
  }

private:
  weak_ptr&lt;employee> employee_;
};

#pragma db object
class employee
{
  ...

private:
  friend class position;

  #pragma db not_null
  shared_ptr&lt;position> position_;
};
  </pre>


  <p>Above, to model a bidirectional relationship in persistent classes,
     we used two pointers, one in each object. While this is a natural
     representation in C++, it does not translate to a canonical
     relational model. Consider the database schema generated for
     the above two classes:</p>

  <pre class="sql">
CREATE TABLE position (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  employee BIGINT UNSIGNED REFERENCES employee (id));

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  position BIGINT UNSIGNED NOT NULL REFERENCES position (id));
  </pre>

  <p>While this database schema is valid, it is unconventional. We have
     a reference from a row in the <code>position</code> table to a row
     in the <code>employee</code> table. We also have a reference
     from this same row in the <code>employee</code> table back to
     the row in the <code>position</code> table. From the relational
     point of view, one of these references is redundant since
     in SQL we can easily navigate in both directions using just one
     of these references.</p>

  <p>To eliminate redundant database schema references we can use the
     <code>inverse</code> pragma (<a href="#10.3.9">Section 10.3.9,
     "<code>inverse</code>"</a>) which tells the ODB compiler that
     a pointer is the inverse side of a bidirectional relationship.
     Either side of a relationship can be made inverse. For example:</p>

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

  #pragma db inverse(position_)
  weak_ptr&lt;employee> employee_;
};

#pragma db object
class employee
{
  ...

  #pragma db not_null
  shared_ptr&lt;position> position_;
};
  </pre>

  <p>The resulting database schema looks like this:</p>

  <pre class="sql">
CREATE TABLE position (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  position BIGINT UNSIGNED NOT NULL REFERENCES position (id));
  </pre>

  <p>As you can see, an inverse member does not have a corresponding
     column (or table, in case of an inverse container of pointers)
     and, from the point of view of database operations, is effectively
     read-only. The only way to change a bidirectional relationship
     with an inverse side is to set its direct (non-inverse)
     pointer. Also note that an ordered container (<a href="#5.1">Section
     5.1, "Ordered Containers"</a>) of pointers that is an inverse side
     of a bidirectional relationship is always treated as unordered
     (<a href="#10.3.10">Section 10.3.10, "<code>unordered</code>"</a>)
     because the contents of such a container are implicitly built from
     the direct side of the relationship which does not contain the
     element order (index).</p>

  <p>There are three distinct bidirectional relationships that we
     will cover in the following sections: one-to-one, one-to-many,
     and many-to-many. We will only talk about bidirectional
     relationships with inverse sides since they result in canonical
     database schemas. For sample code that shows how to work with
     these relationships, refer to the <code>inverse</code> example
     in the <code>odb-examples</code> package.</p>

  <h3><a name="6.2.1">6.2.1 One-to-One Relationships</a></h3>

  <p>An example of a bidirectional one-to-one relationship is the
     presented above employee-position relationship (an employee
     fills one position and a position is filled by one employee).
     The following persistent C++ classes model this relationship:</p>

  <pre class="c++">
class employee;

#pragma db object
class position
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db inverse(position_)
  weak_ptr&lt;employee> employee_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db not_null
  shared_ptr&lt;position> position_;
};
  </pre>

  <p>The corresponding database tables look like this:</p>

  <pre class="sql">
CREATE TABLE position (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  position BIGINT UNSIGNED NOT NULL REFERENCES position (id));
  </pre>

  <p>If instead the other side of this relationship is made inverse,
     then the database tables will change as follows:</p>

  <pre class="sql">
CREATE TABLE position (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  employee BIGINT UNSIGNED REFERENCES employee (id));

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);
  </pre>

  <h3><a name="6.2.2">6.2.2 One-to-Many Relationships</a></h3>

  <p>An example of a bidirectional one-to-many relationship is the
     employer-employee relationship (an employer has multiple
     employees and an employee is employed by one employer).
     The following persistent C++ classes model this relationship:</p>

  <pre class="c++">
class employee;

#pragma db object
class employer
{
  ...

  #pragma db id
  std::string name_;

  #pragma db value_not_null inverse(employer_)
  std::vector&lt;weak_ptr&lt;employee> > employees_
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db not_null
  shared_ptr&lt;employer> employer_;
};
  </pre>

  <p>The corresponding database tables differ significantly depending
     on which side of the relationship is made inverse. If the <em>one</em>
     side (<code>employer</code>) is inverse as in the code
     above, then the resulting database schema looks like this:</p>

  <pre class="sql">
CREATE TABLE employer (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  employer VARCHAR (255) NOT NULL REFERENCES employer (name));
  </pre>

  <p>If instead the <em>many</em> side (<code>employee</code>) of this
     relationship is made inverse, then the database tables will change
     as follows:</p>

  <pre class="sql">
CREATE TABLE employer (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE employer_employees (
  object_id VARCHAR (255) NOT NULL,
  value BIGINT UNSIGNED NOT NULL REFERENCES employee (id));

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);
  </pre>

  <h3><a name="6.2.3">6.2.3 Many-to-Many Relationships</a></h3>

  <p>An example of a bidirectional many-to-many relationship is the
     employee-project relationship (an employee can work on multiple
     projects and a project can have multiple participating employees).
     The following persistent C++ classes model this relationship:</p>

  <pre class="c++">
class employee;

#pragma db object
class project
{
  ...

  #pragma db id
  std::string name_;

  #pragma db value_not_null inverse(projects_)
  std::vector&lt;weak_ptr&lt;employee> > employees_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db value_not_null unordered
  std::vector&lt;shared_ptr&lt;project> > projects_;
};
  </pre>

  <p>The corresponding database tables look like this:</p>

  <pre class="sql">
CREATE TABLE project (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);

CREATE TABLE employee_projects (
  object_id BIGINT UNSIGNED NOT NULL,
  value VARCHAR (255) NOT NULL REFERENCES project (name));
  </pre>

  <p>If instead the other side of this relationship is made inverse,
     then the database tables will change as follows:</p>

  <pre class="sql">
CREATE TABLE project (
  name VARCHAR (255) NOT NULL PRIMARY KEY);

CREATE TABLE project_employees (
  object_id VARCHAR (255) NOT NULL,
  value BIGINT UNSIGNED NOT NULL REFERENCES employee (id));

CREATE TABLE employee (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY);
  </pre>

  <h2><a name="6.3">6.3 Lazy Pointers</a></h2>

  <p>Consider again the bidirectional, one-to-many employer-employee
     relationship that was presented earlier in this chapter:</p>

  <pre class="c++">
class employee;

#pragma db object
class employer
{
  ...

  #pragma db id
  std::string name_;

  #pragma db value_not_null inverse(employer_)
  std::vector&lt;weak_ptr&lt;employee> > employees_;
};

#pragma db object
class employee
{
  ...

  #pragma db id
  unsigned long id_;

  #pragma db not_null
  shared_ptr&lt;employer> employer_;
};
  </pre>

  <p>Consider also the following transaction which obtains the employer
     name given the employee id:</p>

  <pre class="c++">
unsigned long id = ...
string name;

session s;
transaction t (db.begin ());

shared_ptr&lt;employee> e (db.load&lt;employee> (id));
name = e->employer_->name_;

t.commit ();
  </pre>

  <p>While this transaction looks very simple, it actually does a lot more
     than what meets the eye and is necessary. Consider what happens when
     we load the <code>employee</code> object: the <code>employer_</code>
     pointer is also automatically loaded which means the <code>employer</code>
     object corresponding to this employee is also loaded. But the
     <code>employer</code> object in turn contains the list of pointers
     to all the employees, which are also loaded. A a result, when object
     relationships are involved, a simple transaction like the above can
     load many more objects than is necessary.</p>

  <p>To overcome this problem ODB offers finer grained control over
     the relationship loading in the form of lazy pointers. A lazy
     pointer does not automatically load the pointed-to object
     when the containing object is loaded. Instead, we have to
     explicitly load the pointed-to object if and when we need to
     access it.</p>

  <p>The ODB runtime library provides lazy counterparts for all the
     supported pointers, namely: <code>odb::lazy_shared_ptr</code> and
     <code>odb::lazy_weak_ptr</code> for TR1 <code>shared_ptr</code> and
     <code>weak_ptr</code>, <code>odb::lazy_auto_ptr</code> for
     <code>std::auto_ptr</code>, and <code>odb::lazy_ptr</code> for raw
     pointers. The ODB profile libraries provide lazy pointer
     implementations for smart pointers from popular frameworks and
     libraries (<a href="#III">Part III, "Profiles"</a>).</p>

  <p>While we will discuss the interface of lazy pointers in more detail
     shortly, the most commonly used extra function provided by these
     pointers is <code>load()</code>. This function loads the
     pointed-to object if it hasn't already been loaded. After
     the call to this function, the lazy pointer can be used
     in the the same way as its eager counterpart. The <code>load()</code>
     function also returns the eager pointer, in case you need to pass
     it around. For a lazy weak pointer, the
     <code>load()</code> function also locks the pointer.</p>

  <p>The following example shows how we can change our employer-employee
     relationship to use lazy pointers. Here we choose to use lazy pointers
     for both sides of the relationship.</p>

  <pre class="c++">
class employee;

#pragma db object
class employer
{
  ...

  #pragma db value_not_null inverse(employer_)
  std::vector&lt;lazy_weak_ptr&lt;employee> > employees_;
};

#pragma db object
class employee
{
  ...

  #pragma db not_null
  lazy_shared_ptr&lt;employer> employer_;
};
  </pre>

  <p>And the transaction is changed like this:</p>

  <pre class="c++">
unsigned long id = ...
string name;

session s;
transaction t (db.begin ());

shared_ptr&lt;employee> e (db.load&lt;employee> (id));
e->employer_.load ();
name = e->employer_->name_;

t.commit ();
  </pre>


  <p>As a general guideline we recommend that you make at least one side
     of a bidirectional relationship lazy, especially for relationships
     with a <em>many</em> side.</p>

  <p>A lazy pointer implementation mimics the interface of its eager
     counterpart which can be used once the pointer is loaded. It also
     adds a number of additional functions that are specific to the
     lazy loading functionality. Overall, the interface of a lazy
     pointer follows this general outline:</p>

  <pre class="c++">
template &lt;class T>
class lazy_ptr
{
public:
  //
  // The eager pointer interface.
  //

  // Initialization/assignment from an eager pointer.
  //
public:
  template &lt;class Y> lazy_ptr (const eager_ptr&lt;Y>&amp;);
  template &lt;class Y> lazy_ptr&amp; operator= (const eager_ptr&lt;Y>&amp;);

  // Lazy loading interface.
  //
public:
  //  NULL      loaded()
  //
  //  true       true      NULL pointer to transient object
  //  false      true      valid pointer to persistent object
  //  true       false     unloaded pointer to persistent object
  //  false      false     valid pointer to transient object
  //
  bool loaded () const;

  eager_ptr&lt;T> load () const;

  // Unload the pointer. For transient objects this function is
  // equivalent to reset().
  //
  void unload () const;

  // Initialization with a persistent loaded object.
  //
  template &lt;class Y> lazy_ptr (database&amp;, Y*);
  template &lt;class Y> lazy_ptr (database&amp;, const eager_ptr&lt;Y>&amp;);

  template &lt;class Y> void reset (database&amp;, Y*);
  template &lt;class Y> void reset (database&amp;, const eager_ptr&lt;Y>&amp;);

  // Initialization with a persistent unloaded object.
  //
  template &lt;class ID> lazy_ptr (database&amp;, const ID&amp;);

  template &lt;class ID> void reset (database&amp;, const ID&amp;);

  // Query object id and database of a persistent object.
  //
  template &lt;class O /* = T */>
  object_traits&lt;O>::id_type object_id () const;

  odb::database&amp; database () const;
};
  </pre>

  <p>In a lazy weak pointer interface, the <code>load()</code> function
     returns the <em>strong</em> (shared) eager pointer. The following
     transaction demonstrates the use of a lazy weak pointer based on
     the <code>employer</code> and <code>employee</code> classes
     presented earlier.</p>

  <pre class="c++">
typedef std::vector&lt;lazy_weak_ptr&lt;employee> > employees;

session s;
transaction t (db.begin ());

shared_ptr&lt;employer> er (db.load&lt;employer> ("Example Inc"));
employees&amp; es (er->employees ());

for (employees::iterator i (es.begin ()); i != es.end (); ++i)
{
  // We are only interested in employees with object id less than
  // 100.
  //
  lazy_weak_ptr&lt;employee>&amp; lwp (*i);

  if (lwp.object_id&lt;employee> () &lt; 100)
  {
    shared_ptr&lt;employee> e (lwp.load ()); // Load and lock.
    cout &lt;&lt; e->first_ &lt;&lt; " " &lt;&lt; e->last_ &lt;&lt; endl;
  }
}

t.commit ();
  </pre>

  <p>Notice that inside the for-loop we use a reference to the lazy
     weak pointer instead of making a copy. This is not merely to
     avoid a copy. When a lazy pointer is loaded, all other lazy
     pointers that point to the same object do not automatically
     become loaded (though an attempt to load such copies will
     result in them pointing to the same object, provided the
     same session is still in effect). By using a reference
     in the above transaction we make sure that we load the
     pointer that is contained in the <code>employer</code>
     object. This way, if we later need to re-examine this
     <code>employee</code> object, the pointer will already
     be loaded.</p>

  <p>As another example, suppose we want to add an employee
     to Example Inc. The straightforward implementation of this
     transaction is presented below:</p>

  <pre class="c++">
session s;
transaction t (db.begin ());

shared_ptr&lt;employer> er (db.load&lt;employer> ("Example Inc"));
shared_ptr&lt;employee> e (new employee ("John", "Doe"));

e->employer_ = er;
er->employees ().push_back (e);

db.persist (e);
t.commit ();
  </pre>

  <p>Notice here that we didn't have to update the employer object
     in the database since the <code>employees_</code> list of
     pointers is an inverse side of a bidirectional relationship
     and is effectively read-only, from the persistence point of
     view.</p>

  <p>A faster implementation of this transaction, that avoids loading
     the employer object, relies on the ability to initialize an
     <em>unloaded</em> lazy pointer with the database where the object
     is stored as well as its identifier:</p>

  <pre class="c++">
lazy_shared_ptr&lt;employer> er (db, std::string ("Example Inc"));
shared_ptr&lt;employee> e (new employee ("John", "Doe"));

e->employer_ = er;

session s;
transaction t (db.begin ());

db.persist (e);

t.commit ();
  </pre>

  <h2><a name="6.4">6.4 Using Custom Smart Pointers</a></h2>

  <p>While the ODB runtime and profile libraries provide support for
     the majority of widely-used pointers, it is also easy to add
     support for a custom smart pointer.</p>

  <p>To achieve this you will need to implement the
     <code>pointer_traits</code> class template specialization for
     your pointer. The first step is to determine the pointer kind
     since the interface of the <code>pointer_traits</code> specialization
     varies depending on the pointer kind. The supported pointer kinds
     are: <em>raw</em> (raw pointer or equivalent, that is, unmanaged),
          <em>unique</em> (smart pointer that doesn't support sharing),
          <em>shared</em> (smart pointer that supports sharing), and
          <em>weak</em> (weak counterpart of the shared pointer). Any of
     these pointers can be lazy, which also affects the
     interface of the <code>pointer_traits</code> specialization.</p>

  <p>Once you have determined the pointer kind for your smart pointer,
     use a specialization for one of the standard pointers found in
     the common ODB runtime library (<code>libodb</code>) as a base
     for your own implementation.</p>

  <p>Once the pointer traits specialization is ready, you will need to
     include it into the ODB compilation process using the
     <code>--odb-epilogue</code> option and into the generated header
     files with the <code>--hxx-prologue</code> option. As an example,
     suppose we have the <code>smart_ptr</code> smart pointer for which
     we have the traits specialization implemented in the
     <code>smart-ptr-traits.hxx</code> file. Then, we can create an ODB
     compiler options file for this pointer and save it to
     <code>smart-ptr.options</code>:</p>

  <pre>
# Options file for smart_ptr.
#
--odb-epilogue '#include "smart-ptr-traits.hxx"'
--hxx-prologue '#include "smart-ptr-traits.hxx"'
  </pre>

  <p>Now, whenever we compile a header file that uses <code>smart_ptr</code>,
     we can specify the following command line option to make sure it is
     recognized by the ODB compiler as a smart pointer and the traits file
     is included in the generated code:</p>

  <pre>
--options-file smart-ptr.options
  </pre>

  <p>It is also possible to implement a lazy counterpart for your
     smart pointer. The ODB runtime library provides a class template
     that encapsulates the object id management and loading
     functionality that is needed to implement a lazy pointer. All
     you need to do is wrap it with an interface that mimics
     your smart pointer. Using one of the existing lazy pointer
     implementations (either from the ODB runtime library or one
     of the profile libraries) as a base for your implementation
     is the easiest way to get started.</p>


  <!-- CHAPTER -->

  <hr class="page-break"/>
  <h1><a name="7">7 Value Types</a></h1>

  <p>In <a href="#3.1">Section 3.1, "Concepts and Terminology"</a> we have
     already discussed the notion of values and value types as well as the
     distinction between simple and composite values. This chapter covers
     simple and composite value types in more detail.</p>

  <h2><a name="7.1">7.1 Simple Value Types</a></h2>

  <p>A simple value type is a fundamental C++ type or a class type that
     is mapped to a single database column. For each supported database
     system the ODB compiler provides a default mapping to suitable
     database types for most fundamental C++ types, such as <code>int</code>
     or <code>float</code> as well as some class types, such as
     <code>std::string</code>. For more information about the default
     mapping for each database system refer to <a href="#II">Part II,
     Database Systems</a>. We can also provide a custom mapping for
     these or our own value types using the <code>db&nbsp;type</code>
     pragma (<a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>).</p>

  <h2><a name="7.2">7.2 Composite Value Types</a></h2>

  <p>A composite value type is a <code>class</code> or <code>struct</code>
     type that is mapped to more than one database column. To declare
     a composite value type we use the <code>db&nbsp;value</code> pragma,
     for example:</p>

  <pre class="c++">
#pragma db value
class basic_name
{
  ...

  std::string first_;
  std::string last_;
};
  </pre>

  <p>The complete version of the above code fragment and the other code
     samples presented in this chapter can be found in the <code>composite</code>
     example in the <code>odb-examples</code> package.</p>

  <p>A composite value type does not have to define a default constructor,
     unless it is used as an element of a container, in which case the
     default constructor can be made private. If a composite value type
     has private or protected non-transient data members or if its
     default constructor is not public and the value type is used as
     an element of a container, then the <code>odb::access</code> class
     should be declared a friend of this value type. For example:</p>

  <pre class="c++">
#pragma db value
class basic_name
{
public:
  basic_name (const std::string&amp; first, const std::string&amp; last);

  ...

private:
  friend class odb::access;

  basic_name () {} // Needed for storing basic_name in containers.

  std::string first_;
  std::string last_;
};
  </pre>

  <p>The members of a composite value can be other value types (either
     simple or composite), containers (<a href="#5">Chapter 5,
     "Containers"</a>), and pointers to objects (<a href="#6">Chapter 6,
     "Relationships"</a>).
     Similarly, a composite value type can be used in object members,
     as an element of a container, and as a base for another composite
     value type. In particular, composite value types can be used as
     element types in set containers (<a href="#5.2">Section 5.2, "Set
     and Multiset Containers"</a>) and as key types in map containers
     (<a href="#5.3">Section 5.3, "Map and Multimap Containers"</a>).
     A composite value type that is used as an element of a container
     cannot contain other containers since containers of containers
     are not allowed. The following example illustrates some of the
     possible use cases:</p>

<pre class="c++">
#pragma db value
class basic_name
{
  ...

  std::string first_;
  std::string last_;
};

typedef std::vector&lt;basic_name> basic_names;

#pragma db value
class name_extras
{
  ...

  std::string nickname_;
  basic_names aliases_;
};

#pragma db value
class name: public basic_name
{
  ...

  std::string title_;
  name_extras extras_;
};

#pragma db object
class person
{
  ...

  name name_;
};
</pre>

  <p>We can also use data members from composite value types
     in database queries (<a href="#4">Chapter 4, "Querying the
     Database"</a>). For each composite value in a persistent class, the
     query class defines a nested member that contains members corresponding
     to the data members in the value type. We can then use the member access
     syntax (.) to refer to data members in value types. For example, the
     query class for the <code>person</code> object presented above
     contains the <code>name</code> member (its name is derived from
     the <code>name_</code> data member) which in turn contains the
     <code>extras</code> member (its name is derived from the
     <code>name::extras_</code> data member of the composite value type).
     This process continues recursively for nested composite value types
     and, as a result, we can use the <code>query::name.extras.nickname</code>
     expression while querying the database for the <code>person</code>
     objects. For example:</p>

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

transaction t (db->begin ());

result r (db->query&lt;person> (
  query::name.extras.nickname == "Squeaky"));

...

t.commit ();
  </pre>

  <h3><a name="7.2.1">7.2.1 Composite Value Column and Table Names</a></h3>

  <p>Customizing a column name for a data member of a simple value
     type is straightforward: we simply specify the desired name with
     the <code>db&nbsp;column</code> pragma (<a href="#10.3.7">Section
     10.3.7, "<code>column</code>"</a>). For composite value
     types things are slightly more complex since they are mapped to
     multiple columns. Consider the following example:</p>

<pre class="c++">
#pragma db value
class name
{
  ...

  std::string first_;
  std::string last_;
};

#pragma db object
class person
{
  ...

  #pragma db id auto
  unsigned long id_;

  name name_;
};
</pre>

  <p>The column names for the <code>first_</code> and <code>last_</code>
     members are constructed by using the sanitized name of the
     <code>person::name_</code> member as a prefix and the names of the
     members in the value type (<code>first_</code> and <code>last_</code>)
     as suffixes. As a result, the database schema for the above classes
     will look like this:</p>

  <pre class="sql">
CREATE TABLE person (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  name_first TEXT NOT NULL,
  name_last TEXT NOT NULL);
  </pre>

 <p>We can customize both the prefix and the suffix using the
    <code>db&nbsp;column</code> pragma as shown in the following
    example:</p>

  <pre class="c++">
#pragma db value
class name
{
  ...

  #pragma db column("first_name")
  std::string first_;

  #pragma db column("last_name")
  std::string last_;
};

#pragma db object
class person
{
  ...

  #pragma db column("person_")
  name name_;
};
  </pre>

  <p>The database schema changes as follows:</p>

  <pre class="sql">
CREATE TABLE person (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  person_first_name TEXT NOT NULL,
  person_last_name TEXT NOT NULL);
  </pre>

  <p>We can also make the column prefix empty, for example:</p>

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

  #pragma db column("")
  name name_;
};
  </pre>

  <p>This will result in the following schema:</p>

  <pre class="sql">
CREATE TABLE person (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  first_name TEXT NOT NULL,
  last_name TEXT NOT NULL);
  </pre>

  <p>The same principle applies when a composite value type is used
     as an element of a container, except that instead of
     <code>db&nbsp;column</code>, either the <code>db&nbsp;value_column</code>
     (<a href="#10.3.23">Section 10.3.23, "<code>value_column</code>"</a>) or
     <code>db&nbsp;key_column</code>
     (<a href="#10.3.22">Section 10.3.22, "<code>key_column</code>"</a>)
     pragmas are used to specify the column prefix.</p>

  <p>When a composite value type contains a container, an extra table
     is used to store its elements (<a href="#5">Chapter 5, "Containers"</a>).
     The names of such tables are constructed in a way similar to the
     column names, except that by default both the object name and the
     member name are used as a prefix. For example:</p>

  <pre class="c++">
#pragma db value
class name
{
  ...

  std::string first_;
  std::string last_;
  std::vector&lt;std::string> nicknames_;
};

#pragma db object
class person
{
  ...

  name name_;
};
  </pre>

  <p>The corresponding database schema will look like this:</p>

  <pre class="sql">
CREATE TABLE person_name_nicknames (
  object_id BIGINT UNSIGNED NOT NULL,
  index BIGINT UNSIGNED NOT NULL,
  value TEXT NOT NULL)

CREATE TABLE person (
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
  name_first TEXT NOT NULL,
  name_last TEXT NOT NULL);
  </pre>

  <p>To customize the container table name we can use the
     <code>db&nbsp;table</code> pragma (<a href="#10.3.11">Section
     10.3.11, "<code>table</code>"</a>), for example:</p>

  <pre class="c++">
#pragma db value
class name
{
  ...

  #pragma db table("nickname")
  std::vector&lt;std::string> nicknames_;
};

#pragma db object
class person
{
  ...

  #pragma db table("person_")
  name name_;
};
  </pre>

  <p>This will result in the following schema changes:</p>

  <pre class="sql">
CREATE TABLE person_nickname (
  object_id BIGINT UNSIGNED NOT NULL,
  index BIGINT UNSIGNED NOT NULL,
  value TEXT NOT NULL)
  </pre>

  <p>Similar to columns, we can make the table prefix empty.</p>


  <h2><a name="7.3">7.3 Pointers and <code>NULL</code> Value Semantics</a></h2>

  <p>Relational database systems have a notion of the special
     <code>NULL</code> value that is used to indicate the absence
     of a valid value in a column. While by default ODB maps
     values to columns that do not allow <code>NULL</code> values,
     it is possible to change that with the <code>db&nbsp;null</code>
     pragma (<a href="#10.3.4">Section 10.3.4,
     "<code>null</code>/<code>not_null</code>"</a>).</p>

  <p>To properly support the <code>NULL</code> semantics, the
     C++ value type must have a notion or a <code>NULL</code>
     value or a similar special state concept. Most basic
     C++ types, such as <code>int</code> or <code>std::string</code>,
     do not have this notion and therefore cannot be used directly
     for <code>NULL</code>-enabled data members (in the case of a
     <code>NULL</code> value being loaded from the database,
     such data members will be default-initialized).</p>

  <p>To allow the easy conversion of value types that do not support
     the <code>NULL</code> semantics into the ones that do, ODB
     provides the <code>odb::nullable</code> class template. It
     allows us to wrap an existing C++ type into a container-like
     class that can either be <code>NULL</code> or contain a
     value of the wrapped type. ODB also automatically enables
     the <code>NULL</code> values for data members of the
     <code>odb::nullable</code> type. For example:</p>

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

#pragma db object
class person
{
  ...

  std::string first_;                    // TEXT NOT NULL
  odb::nullable&lt;std::string> middle_;    // TEXT NULL
  std::string last_;                     // TEXT NOT NULL
};
  </pre>

  <p>The <code>odb::nullable</code> class template is defined
     in the <code>&lt;odb/nullable.hxx></code> header file and
     has the following interface:</p>

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

    nullable ();
    nullable (const T&amp;);
    nullable (const nullable&amp;);
    template &lt;typename Y> explicit nullable (const nullable&lt;Y>&amp;);

    nullable&amp; operator= (const T&amp;);
    nullable&amp; operator= (const nullable&amp;);
    template &lt;typename Y> nullable&amp; operator= (const nullable&lt;Y>&amp;);

    void swap (nullable&amp;);

    // Accessor interface.
    //
    bool null () const;

    T&amp;       get ();
    const T&amp; get () const;

    // Pointer interface.
    //
    operator bool_convertible () const;

    T*       operator-> ();
    const T* operator-> () const;

    T&amp;       operator* ();
    const T&amp; operator* () const;

    // Reset to the NULL state.
    //
    void reset ();
  };
}
  </pre>

  <p>The following example shows how we can use this interface:</p>

  <pre class="c++">
  nullable&lt;string> ns;

  // Using the accessor interface.
  //
  if (ns.null ())
  {
    s = "abc";
  }
  else
  {
    string s (ns.get ());
    ns.reset ();
  }

  // The same using the pointer interface.
  //
  if (ns)
  {
    s = "abc";
  }
  else
  {
    string s (*ns);
    ns.reset ();
  }
  </pre>


  <p>The <code>odb::nullable</code> class template requires the wrapped
     type to have public default and copy constructors as well as the
     copy assignment operator. Note also that the <code>odb::nullable</code>
     implementation is not the most efficient in that it always contains
     a fully constructed value of the wrapped type. This is normally
     not a concern for simple types such as the C++ fundamental
     types or <code>std::string</code>. However, it may become
     an issue for more complex types. In such cases you may want to
     consider using a more efficient implementation of the
     <em>optional value</em> concept such as the
     <code>optional</code> class template from Boost
     (<a href="#15.3">Section 15.3, "Optional Library"</a>).</p>

  <p>Another common C++ representation of a value that can be
     <code>NULL</code> is a pointer. ODB will automatically
     handle data members that are pointers to values, however,
     it will not automatically enable <code>NULL</code> values
     for such data members, as is the case for <code>odb::nullable</code>.
     Instead, if the <code>NULL</code> value is desired, we will
     need to enable it explicitly using the <code>db&nbsp;null</code>
     pragma. For example:</p>

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

  std::string first_;

  #pragma db null
  std::auto_ptr&lt;std::string> middle_;

  std::string last_;
};
  </pre>

  <p>The ODB compiler includes built-in support for using
     <code>std::auto_ptr</code> and <code>std::tr1::shared_ptr</code>
     as pointers to values. Plus, ODB profile libraries, that are
     available for commonly used frameworks and libraries (such as Boost and
     Qt), provide support for smart pointers found in these frameworks
     and libraries (<a href="#III">Part III, "Profiles"</a>).</p>

  <p>Currently, ODB supports the <code>NULL</code> semantics only
     for simple values. In future versions this support will be extended
     to composite values and containers. With this limitation in mind,
     we can still use smart pointers in data members of composite value
     and container types. The only restriction is that these pointers
     must not be <code>NULL</code>. For example:</p>

  <pre class="c++">
#pragma db value
struct name
{
  std::string first_;
  std::string last_;
};

#pragma db object
class person
{
  ...

  std::auto_ptr&lt;name> name_;
  std::auto_ptr&lt;std::vector&lt;name> > aliases_;
};
  </pre>

  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="8">8 Inheritance</a></h1>

  <p>In C++ inheritance can be used to achieve two different goals.
     We can employ inheritance to reuse common data and functionality
     in multiple classes. For example:</p>

  <pre class="c++">
class person
{
public:
  const std::string&amp;
  first () const;

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

private:
  std::string first_;
  std::string last_;
};

class employee: public person
{
  ...
};

class contractor: public person
{
  ...
};
</pre>

 <p>In the above example both the <code>employee</code> and
    <code>contractor</code> classes inherit the <code>first_</code>
    and <code>last_</code> data members as well as the <code>first()</code>
    and <code>last()</code> accessors from the <code>person</code> base
    class.</p>

 <p>A common trait of this inheritance style, referred to as <em>reuse
    inheritance</em> from now on, is the lack of virtual functions and
    a virtual destructor in the base class. Also with this style the
    application code is normally written in terms of derived classes
    instead of a base.</p>

 <p>The second way to utilize inheritance in C++ is to provide polymorphic
    behavior through a common interface. In this case the base class
    defines a number of virtual functions and, normally, a virtual
    destructor while the derived classes provide specific
    implementations of these virtual functions. For example:</p>

  <pre class="c++">
class person
{
public:
  enum employment_status
  {
    unemployed,
    temporary,
    permanent,
    self_employed
  };

  virtual employment_status
  employment () const = 0;

  virtual
  ~person ();
};

class employee: public person
{
public:
  virtual employment_status
  employment () const
  {
    return temporary_ ? temporary : permanent;
  }

private:
  bool temporary_;
};

class contractor: public person
{
public:
  virtual employment_status
  employment () const
  {
    return self_employed;
  }
};
</pre>

  <p>With this inheritance style, which we will call <em>polymorphism
     inheritance</em>, the application code normally works with derived
     classes via the base class interface. Note also that it is very common
     to mix both styles in the same hierarchy. For example, the above two
     code fragments can be combined so that the <code>person</code> base
     class provides the common data members and functions as well as
     defines the polymorphic interface.</p>

  <p>The following sections describe the available strategies for
     mapping reuse and polymorphism inheritance styles to a relational
     data model. Note also that the distinction between the two styles is
     conceptual rather than formal. For example, it is possible to treat
     a class hierarchy that defines virtual functions as a case of reuse
     inheritance if this results in the desired database mapping and
     semantics.</p>

  <p>Generally, classes that employ reuse inheritance are mapped to
     completely independent entities in the database. They use different
     object id spaces and should always be passed to and returned from
     the database operations as pointers or references to derived types.
     In other words, from the persistence point of view, such classes
     behave as if the data members from the base classes were copied
     verbatim into the derived ones.</p>

  <p>In contrast, classes that employ polymorphism inheritance share
     the object id space and can be passed to and returned from the
     database operations <em>polymorphically</em> as pointers or
     references to the base class.</p>

  <p>For both inheritance styles it is sometimes desirable to prevent
     instances of a base class from being stored in the database.
     To achieve this a persistent
     class can be declared abstract using the <code>db&nbsp;abstract</code>
     pragma (<a href="#10.1.3">Section 10.1.3, "<code>abstract</code>"</a>).
     Note that a <em>C++-abstract</em> class, or a class that
     has one or more pure virtual functions and therefore cannot be
     instantiated, is also <em>database-abstract</em>. However, a
     database-abstract class is not necessarily C++-abstract. The
     ODB compiler automatically treats C++-abstract classes as
     database-abstract.</p>

  <h2><a name="8.1">8.1 Reuse Inheritance</a></h2>

  <p>Each non-abstract class from the reuse inheritance hierarchy is
     mapped to a separate database table that contains all its data
     members, including those inherited from base classes. An abstract
     persistent class does not have to define an object id, nor a default
     constructor, and it does not have a corresponding database table.
     An abstract class cannot be a pointed-to object in a relationship.
     Multiple inheritance is supported as long as each base
     class is only inherited once. The following example shows a
     persistent class hierarchy employing reuse inheritance:</p>

<pre class="c++">
// Abstract person class. Note that it does not declare the
// object id.
//
#pragma db object abstract
class person
{
  ...

  std::string first_;
  std::string last_;
};

// Abstract employee class. It derives from the person class and
// declares the object id for all the concrete employee types.
//
#pragma db object abstract
class employee: public person
{
  ...

  #pragma db id auto
  unsigned long id_;
};

// Concrete permanent_employee class. Note that it doesn't define
// any data members of its own.
//
#pragma db object
class permanent_employee: public employee
{
  ...
};

// Concrete temporary_employee class. It adds the employment
// duration in months.
//
#pragma db object
class temporary_employee: public employee
{
  ...

  unsigned long duration_;
};

// Concrete contractor class. It derives from the person class
// (and not employee; an independent contractor is not considered
// an employee). We use the contractor's external email address
// as the object id.
//
#pragma db object
class contractor: public person
{
  ...

  #pragma db id
  std::string email_;
};
</pre>

  <p>The sample database schema for this hierarchy is shown below.</p>

<pre class="sql">
CREATE TABLE permanent_employee (
  first TEXT NOT NULL,
  last TEXT NOT NULL,
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT);

CREATE TABLE temporary_employee (
  first TEXT NOT NULL,
  last TEXT NOT NULL,
  id BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  duration BIGINT UNSIGNED NOT NULL);

CREATE TABLE contractor (
  first TEXT NOT NULL,
  last TEXT NOT NULL,
  email VARCHAR (255) NOT NULL PRIMARY KEY);
</pre>

  <p>The complete version of the code presented in this section is
     available in the <code>inheritance</code> example in the
     <code>odb-examples</code> package.</p>

  <h2><a name="8.2">8.2 Polymorphism Inheritance</a></h2>

  <p>Polymorphism inheritance mapping is not yet implemented. Future
     versions of ODB will add support for this functionality.</p>

  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="9">9 Session</a></h1>

  <p>A session is an application's unit of work that may encompass several
     database transactions. In this version of ODB a session is just an
     object cache. In future versions it will provide additional
     functionality, such as automatic object state change tracking
     and optimistic concurrency.</p>

  <p>Each thread of execution in an application can have only one active
     session at a time. A session is started by creating an instance of
     the <code>odb::session</code> class and is automatically terminated
     when this instance is destroyed. You will need to include the
     <code>&lt;odb/session.hxx></code> header file to make this class
     available in your application. For example:</p>

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

using namespace odb::core;

{
  session s;

  // First transaction.
  //
  {
    transaction t (db.begin ());
    ...
    t.commit ();
  }

  // Second transaction.
  //
  {
    transaction t (db.begin ());
    ...
    t.commit ();
  }

  // Session 's' is terminated here.
}
  </pre>

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

  <pre class="c++">
namespace odb
{
  class session
  {
  public:
    session ();
    ~session ();

    // Copying or assignment of sessions is not supported.
    //
  private:
    session (const session&amp;);
    session&amp; operator= (const session&amp;);

    // Current session interface.
    //
  public:
    static session&amp;
    current ();

    static bool
    has_current ();

    static void
    current (session&amp;);

    static void
    reset_current ();

    // Object cache interface.
    //
  public:
    typedef odb::database database_type;

    template &lt;typename T>
    void
    insert (database_type&amp;,
            const object_traits&lt;T>::id_type&amp;,
            const object_traits&lt;T>::pointer_type&amp;);

    template &lt;typename T>
    object_traits&lt;T>::pointer_type
    find (database_type&amp;, const object_traits&lt;T>::id_type&amp;) const;

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

  <p>The session constructor creates a new session and sets it as a
     current session for this thread. If we try to create another
     session while there is already a current session in effect,
     the constructor throws the <code>odb::already_in_session</code>
     exception. The destructor clears the current session for this
     thread if this session is the current one.</p>

  <p>The static <code>current()</code> accessor returns the currently active
     session for this thread. If there is no active session, this function
     throws the <code>odb::not_in_session</code> exception. We can check
     whether there is a session in effect in this thread using the
     <code>has_current()</code> static function.</p>

  <p>The static <code>current()</code> modifier allows us to set the
     current session for this thread. The <code>reset_current()</code>
     static function clears the current session. These two functions
     allow for more advanced use cases, such as multiplexing
     two or more sessions on the same thread.</p>

  <p>We normally don't use the object cache interface directly. However,
     it could be useful in some cases, for example, to find out whether
     an object has already been loaded.</p>

  <h2><a name="9.1">9.1 Object Cache</a></h2>

  <p>A session is an object cache. Every time an object is made persistent
     by calling the <code>database::persist()</code> function
     (<a href="#3.7">Section 3.7, "Making Objects Persistent"</a>), loaded
     by calling the <code>database::load()</code> or <code>database::find()</code>
     function (<a href="#3.8">Section 3.8, "Loading Persistent Objects"</a>),
     or loaded by iterating over a query result (<a href="#4.4">Section 4.4,
     "Query Result"</a>), the pointer to the persistent object, in the form
     of the canonical object pointer (<a href="#3.2">Section 3.2, "Object
     Pointers"</a>), is stored in the session. For as long as the
     session is in effect, any subsequent calls to load the same object will
     return the cached instance. When an object's state is deleted from the
     database with the <code>database::erase()</code> function
     (<a href="#3.10">Section 3.10, "Deleting Persistent Objects"</a>), the
     cached object pointer is removed from the session. For example:</p>

  <pre class="c++">
shared_ptr&lt;person> p (new person ("John", "Doe"));

session s;
transaction t (db.begin ());

unsigned long id (db.persist (p));            // p is cached in s.
shared_ptr&lt;person> p1 (db.load&lt;person> (id)); // p1 same as p.

t.commit ();
  </pre>


  <p>The per-object caching policies depend on the object pointer kind
     (<a href="#6.4">Section 6.4, "Using Custom Smart Pointers"</a>).
     Objects with a unique pointer, such as <code>std::auto_ptr</code>,
     as an object pointer are never cached since it is not possible to have
     two such pointers pointing to the same object. When an object is
     persisted via a pointer or loaded as a dynamically allocated instance,
     objects with both raw and shared pointers as object pointers are
     cached. If an object is persisted as a reference or loaded into
     a pre-allocated instance, the object is only cached if its object
     pointer is a raw pointer.</p>

  <p>Also note that when we persist an object as a constant reference
     or constant pointer, the session caches such an object as
     unrestricted (non-<code>const</code>). This can lead to undefined
     behavior if the object being persisted was actually created as
     <code>const</code> and is later found in the session cache and
     used as non-<code>const</code>. As a result, when using sessions,
     it is recommended that all persistent objects be created as
     non-<code>const</code> instances. The following code fragment
     illustrates this point:</p>

  <pre class="c++">
void save (database&amp; db, shared_ptr&lt;const person> p)
{
  transaction t (db.begin ());
  db.persist (p); // Persisted as const pointer.
  t.commit ();
}

session s;

shared_ptr&lt;const person> p1 (new const person ("John", "Doe"));
unsigned long id1 (save (db, p1)); // p1 is cached in s as non-const.

{
  transaction t (db.begin ());
  shared_ptr&lt;person> p (db.load&lt;person> (id1)); // p == p1
  p->age (30); // Undefined behavior since p1 was created const.
}

shared_ptr&lt;const person> p2 (new person ("Jane", "Doe"));
unsigned long id2 (save (db, p2)); // p2 is cached in s as non-const.

{
  transaction t (db.begin ());
  shared_ptr&lt;person> p (db.load&lt;person> (id2)); // p == p2
  p->age (30); // Ok, since p2 was not created const.
}
  </pre>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="10">10 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, we 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")

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

  <p>If we would like to factor the ODB pragmas into a separate file,
     we can include this file into the original header file (the one
     that defines the persistent types) using the <code>#include</code>
     directive, for example:</p>

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

class person
{
  ...
};

#ifdef ODB_COMPILER
#  include "person-pragmas.hxx"
#endif
  </pre>

  <p>Alternatively, instead of using the <code>#include</code> directive,
     we can use the <code>--odb-epilogue</code> option to make the pragmas
     known to the ODB compiler when compiling the original header file,
     for example:</p>

  <pre class="terminal">
--odb-epilogue  '#include "person-pragmas.hxx"'
  </pre>

  <p>The following three sections cover the specifiers applicable
     to the <code>object</code>, <code>value</code>, and <code>member</code>
     qualifiers.</p>

  <p>The C++ header file that defines our 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 the application. Some C++ compilers
     issue warnings about pragmas that they do not recognize. There
     are several ways to deal with this problem which are covered
     at the end of this chapter in <a href="#10.4">Section 10.4,
     "C++ Compiler Warnings"</a>.</p>

  <h2><a name="10.1">10.1 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,
     in any order, by one or more specifiers summarized in the table below:</p>

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

    <tr>
      <td><code>table</code></td>
      <td>table name for a persistent class</td>
      <td><a href="#10.1.1">10.1.1</a></td>
    </tr>

    <tr>
      <td><code>pointer</code></td>
      <td>pointer type for a persistent class</td>
      <td><a href="#10.1.2">10.1.2</a></td>
    </tr>

    <tr>
      <td><code>abstract</code></td>
      <td>persistent class is abstract</td>
      <td><a href="#10.1.3">10.1.3</a></td>
    </tr>

    <tr>
      <td><code>callback</code></td>
      <td>database operations callback</td>
      <td><a href="#10.1.4">10.1.4</a></td>
    </tr>

  </table>

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

  <p>The <code>table</code> specifier specifies the table name that should
     be used to store objects of a 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>

  <h3><a name="10.1.2">10.1.2 <code>pointer</code></a></h3>

  <p>The <code>pointer</code> specifier specifies the object pointer type
     for a persistent class. The object pointer type is used to return,
     pass, and cache dynamically allocated instances of a persistent
     class. For example:</p>

  <pre class="c++">
#pragma db object pointer(std::tr1::shared_ptr&lt;person>)
class person
{
  ...
};
  </pre>

  <p>There are several ways to specify an object pointer with the
     <code>pointer</code> specifier. We can use a complete pointer
     type as shown in the example above. Alternatively, we can
     specify only the template name of a smart pointer in which
     case the ODB compiler will automatically append the class
     name as a template argument. The following example is therefore
     equivalent to the one above:</p>

  <pre class="c++">
#pragma db object pointer(std::tr1::shared_ptr)
class person
{
  ...
};
  </pre>

  <p>If you would like to use the raw pointer as an object pointer,
     you can use <code>*</code> as a shortcut:</p>

  <pre class="c++">
#pragma db object pointer(*) // Same as pointer(person*)
class person
{
  ...
};
  </pre>

  <p>If a pointer type is not explicitly specified, the default
     pointer, specified with the <code>--default-pointer</code>
     ODB compiler option, is used. If this option is not specified
     either, then the raw pointer is used by default.</p>

  <p>For a more detailed discussion of object pointers, refer to
     <a href="#3.2">Section 3.2, "Object Pointers"</a>.</p>

  <h3><a name="10.1.3">10.1.3 <code>abstract</code></a></h3>

  <p>The <code>abstract</code> specifier specifies that a persistent class
     is abstract. An instance of an abstract class cannot be stored in
     the database and is normally used as a base for other persistent
     classes. For example:</p>

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

#pragma db object
class employee: public person
{
  ...
};

#pragma db object
class contractor: public person
{
  ...
};
  </pre>

  <p>Persistent classes with pure virtual functions are automatically
     treated as abstract by the ODB compiler. For a more detailed
     discussion of persistent class inheritance, refer to
     <a href="#8">Chapter 8, "Inheritance"</a>.</p>

  <h3><a name="10.1.4">10.1.4 <code>callback</code></a></h3>

  <p>The <code>callback</code> specifier specifies the persist class
     member function that should be called before and after a
     database operation is performed on an object of this class.
     For example:</p>

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

#pragma db object callback(init)
class person
{
  ...

  void
  init (odb::callback_event, odb::database&amp;);
};
 </pre>

  <p>The callback function has the following signature and can be
     overloaded for constant objects:</p>

  <pre class="c++">
void
name (odb::callback_event, odb::database&amp;);

void
name (odb::callback_event, odb::database&amp;) const;
  </pre>

  <p>The first argument to the callback function is the event that
     triggered this call. The <code>odb::callback_event</code>
     enum-like type is defined in the <code>&lt;odb/callback.hxx></code>
     header file and has the following interface:</p>

  <pre class="c++">
namespace odb
{
  struct callback_event
  {
    enum value
    {
      pre_persist,
      post_persist,
      pre_load,
      post_load,
      pre_update,
      post_update,
      pre_erase,
      post_erase
    };

    callback_event (value v);
    operator value () const;
  };
}
  </pre>

  <p>The second argument to the callback function is the database
     on which the operation is about to be performed or has just
     been performed.</p>

  <p>If only the non-<code>const</code> version of the callback function
     is provided, then only database operations that are performed on
     unrestricted objects will trigger callback calls. If only the
     <code>const</code> version is provided, then the database
     operations on both constant and unrestricted objects will trigger
     callback calls but the object will always be passed as constant.
     Finally, if both versions are provided, then the <code>const</code>
     overload will be called for constant objects and the non-<code>const</code>
     overload for unrestricted objects. These rules are modeled after
     the standard C++ overload resolution rules. A callback function can
     be inline or virtual.</p>

  <p>A database operations callback can be used to implement object-specific
     pre and post initializations, registrations, and cleanups. As an example,
     the following code fragment outlines an implementation of a
     <code>person</code> class that maintains the transient <code>age</code>
     data member in addition to the persistent date of birth. A callback
     is used to calculate the value of the former from the latter every
     time a <code>person</code> object is loaded from the database.</p>

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

#pragma db object callback(init)
class person
{
  ...

private:
  friend class odb::access;

  date born_;

  #pragma db transient
  unsigned short age_;

  void
  init (odb::callback_event e, odb::database&amp;)
  {
    switch (e)
    {
    case odb::callback_event::post_load:
    {
      // Calculate age from the date of birth.
      ...
      break;
    }
    default:
      break;
    }
  }
};
 </pre>

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

  <p>A pragma with the <code>value</code> qualifier describes a value
     type. 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 class="specifiers" border="1">
    <tr>
      <th>Specifier</th>
      <th>Summary</th>
      <th>Section</th>
    </tr>

    <tr>
      <td><code>type</code></td>
      <td>database type for a value type</td>
      <td><a href="#10.2.1">10.2.1</a></td>
    </tr>

    <tr>
      <td><code>id_type</code></td>
      <td>database type for a value type when used as an object id</td>
      <td><a href="#10.2.2">10.2.2</a></td>
    </tr>

    <tr>
      <td><code>null</code>/<code>not_null</code></td>
      <td>type can/cannot be <code>NULL</code></td>
      <td><a href="#10.2.3">10.2.3</a></td>
    </tr>

    <tr>
      <td><code>default</code></td>
      <td>default value for a value type</td>
      <td><a href="#10.2.4">10.2.4</a></td>
    </tr>

    <tr>
      <td><code>options</code></td>
      <td>database options for a value type</td>
      <td><a href="#10.2.5">10.2.5</a></td>
    </tr>

    <tr>
      <td><code>unordered</code></td>
      <td>ordered container should be stored unordered</td>
      <td><a href="#10.2.6">10.2.6</a></td>
    </tr>

    <tr>
      <td><code>index_type</code></td>
      <td>database type for a container's index type</td>
      <td><a href="#10.2.7">10.2.7</a></td>
    </tr>

    <tr>
      <td><code>key_type</code></td>
      <td>database type for a container's key type</td>
      <td><a href="#10.2.8">10.2.8</a></td>
    </tr>

    <tr>
      <td><code>value_type</code></td>
      <td>database type for a container's value type</td>
      <td><a href="#10.2.9">10.2.9</a></td>
    </tr>

    <tr>
      <td><code>value_null</code>/<code>value_not_null</code></td>
      <td>container's value can/cannot be <code>NULL</code></td>
      <td><a href="#10.2.10">10.2.10</a></td>
    </tr>

    <tr>
      <td><code>id_options</code></td>
      <td>database options for a container's id column</td>
      <td><a href="#10.2.11">10.2.11</a></td>
    </tr>

    <tr>
      <td><code>index_options</code></td>
      <td>database options for a container's index column</td>
      <td><a href="#10.2.12">10.2.12</a></td>
    </tr>

    <tr>
      <td><code>key_options</code></td>
      <td>database options for a container's key column</td>
      <td><a href="#10.2.13">10.2.13</a></td>
    </tr>

    <tr>
      <td><code>value_options</code></td>
      <td>database options for a container's value column</td>
      <td><a href="#10.2.14">10.2.14</a></td>
    </tr>

    <tr>
      <td><code>id_column</code></td>
      <td>column name for a container's object id</td>
      <td><a href="#10.2.15">10.2.15</a></td>
    </tr>

    <tr>
      <td><code>index_column</code></td>
      <td>column name for a container's index</td>
      <td><a href="#10.2.16">10.2.16</a></td>
    </tr>

    <tr>
      <td><code>key_column</code></td>
      <td>column name for a container's key</td>
      <td><a href="#10.2.17">10.2.17</a></td>
    </tr>

    <tr>
      <td><code>value_column</code></td>
      <td>column name for a container's value</td>
      <td><a href="#10.2.18">10.2.18</a></td>
    </tr>

  </table>

  <p>Many of the value type specifiers have corresponding member type
     specifiers with the same names (<a href="#10.3">Section 10.3,
     "Data Member Pragmas"</a>). The behavior of such specifiers
     for members is similar to that for value types. The only difference
     is the scope. A particular value type specifier applies to all the
     members of this value type that don't have a pre-member version
     of the specifier, while the member specifier always applies only
     to a single member. Also, with a few exceptions, member specifiers
     take precedence over and override parameters specified with value
     specifiers.</p>

  <h3><a name="10.2.1">10.2.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")

#pragma db object
class person
{
  ...

  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="#II">Part II, "Database Systems"</a>. The
     <code>null</code> and <code>not_null</code> (<a href="#10.2.3">Section
     10.2.3, "<code>null</code>/<code>not_null</code>"</a>) specifiers
     can be used to control the NULL semantics of a type.</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)")
  </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 could be 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>

  <h3><a name="10.2.2">10.2.2 <code>id_type</code></a></h3>

  <p>The <code>id_type</code> specifier specifies the native database type
     that should be used for data members of this type that are designated as
     object identifiers (<a href="#10.3.1">Section 10.3.1,
     "<code>id</code>"</a>). In combination with the <code>type</code>
     specifier (<a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>)
     <code>id_type</code> allows you to map a C++ type differently depending
     on whether it is used in an ordinary member or an object id. For
     example:</p>

  <pre class="c++">
#pragma db value(std::string) type("TEXT") id_type("VARCHAR(128)")

#pragma db object
class person
{
  ...

  #pragma db id
  std::string email_; // Mapped to VARCHAR(128) NOT NULL.

  std::string name_;  // Mapped to TEXT NOT NULL.
};
  </pre>

  <p>Note that there is no corresponding member type specifier for
     <code>id_type</code> since the desired result can be achieved
     with just the <code>type</code> specifier, for example:</p>

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

  #pragma db id type("VARCHAR(128)")
  std::string email_;
};
  </pre>

  <h3><a name="10.2.3">10.2.3 <code>null</code>/<code>not_null</code></a></h3>

  <p>The <code>null</code> and <code>not_null</code> specifiers specify that
     a value type or object pointer can or cannot be <code>NULL</code>,
     respectively. By default, value types are assumed not to allow
     <code>NULL</code> values while object pointers are assumed to
     allow <code>NULL</code> values. Data members of types that allow
     <code>NULL</code> values are mapped in a relational database to
     columns that allow <code>NULL</code> values. For example:</p>

  <pre class="c++">
using std::tr1::shared_ptr;

typedef shared_ptr&lt;std::string> string_ptr;
#pragma db value(string_ptr) type("TEXT") null

#pragma db object
class person
{
  ...

  string_ptr name_; // Mapped to TEXT NULL.
};

typedef shared_ptr&lt;person> person_ptr;
#pragma db value(person_ptr) not_null
  </pre>

  <p>The <code>NULL</code> semantics can also be specified on the
     per-member basis (<a href="#10.3.4">Section 10.3.4,
     "<code>null</code>/<code>not_null</code>"</a>). If both a type and
     a member have <code>null</code>/<code>not_null</code> specifiers,
     then the member specifier takes precedence. If a member specifier
     relaxes the <code>NULL</code> semantics (that is, if a member has
     the <code>null</code> specifier and the type has the explicit
     <code>not_null</code> specifier), then a warning is issued.</p>

  <p>It is also possible to override a previously specified
     <code>null</code>/<code>not_null</code> specifier. This is
     primarily useful if a third-party type, for example,
     one provided by a profile library (<a href="#III">Part III,
     "Profiles"</a>), allows <code>NULL</code> values but in your
     object model data members of this type should never be
     <code>NULL</code>. In this case you can use the <code>not_null</code>
     specifier to disable <code>NULL</code> values for this type for the
     entire translation unit. For example:</p>

  <pre class="c++">
// By default, null_string allows NULL values.
//
#include &lt;null-string.hxx>

// Disable NULL values for all the null_string data members.
//
#pragma db value(null_string) not_null
  </pre>

  <p>For a more detailed discussion of the <code>NULL</code> semantics
     for values, refer to <a href="#7.3">Section 7.3, "Pointers and
     <code>NULL</code> Value Semantics"</a>. For a more detailed
     discussion of the <code>NULL</code> semantics for object pointers,
     refer to <a href="#6">Chapter 6, "Relationships"</a>.</p>

  <h3><a name="10.2.4">10.2.4 <code>default</code></a></h3>

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

  <pre class="c++">
#pragma db value(std::string) default("")

#pragma db object
class person
{
  ...

  std::string name_; // Mapped to TEXT NOT NULL DEFAULT ''.
};
  </pre>

  <p>The semantics of the <code>default</code> specifier for a value type
     are similar to those of the <code>default</code> specifier for a
     data member (<a href="#10.3.5">Section 10.3.5,
     "<code>default</code>"</a>).</p>

  <h3><a name="10.2.5">10.2.5 <code>options</code></a></h3>

  <p>The <code>options</code> specifier specifies additional column
     definition options that should be used for data members of this
     type. For example:</p>

  <pre class="c++">
#pragma db value(std::string) options("COLLATE binary")

#pragma db object
class person
{
  ...

  std::string name_; // Mapped to TEXT NOT NULL COLLATE binary.
};
  </pre>

  <p>The semantics of the <code>options</code> specifier for a value type
     are similar to those of the <code>options</code> specifier for a
     data member (<a href="#10.3.6">Section 10.3.6,
     "<code>options</code>"</a>).</p>

  <h3><a name="10.2.6">10.2.6 <code>unordered</code></a></h3>

  <p>The <code>unordered</code> specifier specifies that the ordered
     container should be stored unordered in the database. The database
     table for such a container will not contain the index column
     and the order in which elements are retrieved from the database may
     not be the same as the order in which they were stored. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> names;
#pragma db value(names) unordered
  </pre>

  <p>For a more detailed discussion of ordered containers and their
     storage in the database, refer to <a href="#5.1">Section 5.1,
     "Ordered Containers"</a>.</p>

  <h3><a name="10.2.7">10.2.7 <code>index_type</code></a></h3>

  <p>The <code>index_type</code> specifier specifies the native
     database type that should be used for an ordered container's
     index column. The semantics of <code>index_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>). The native
     database type is expected to be an integer type. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> names;
#pragma db value(names) index_type("SMALLINT UNSIGNED")
  </pre>

  <h3><a name="10.2.8">10.2.8 <code>key_type</code></a></h3>

  <p>The <code>key_type</code> specifier specifies the native
     database type that should be used for a map container's
     key column. The semantics of <code>key_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>). For
     example:</p>

  <pre class="c++">
typedef std::map&lt;unsigned short, float> age_weight_map;
#pragma db value(age_weight_map) key_type("INT UNSIGNED")
  </pre>

  <h3><a name="10.2.9">10.2.9 <code>value_type</code></a></h3>

  <p>The <code>value_type</code> specifier specifies the native
     database type that should be used for a container's
     value column. The semantics of <code>value_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.2.1">Section 10.2.1, "<code>type</code>"</a>). For
     example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> names;
#pragma db value(names) value_type("VARCHAR(255)")
  </pre>

  <p>The <code>value_null</code> and <code>value_not_null</code>
     (<a href="#10.2.10">Section 10.2.10,
     "<code>value_null</code>/<code>value_not_null</code>"</a>) specifiers
     can be used to control the NULL semantics of a value column.</p>

  <h3><a name="10.2.10">10.2.10 <code>value_null</code>/<code>value_not_null</code></a></h3>

  <p>The <code>value_null</code> and <code>value_not_null</code> specifiers
     specify that a container type's element value can or cannot be
     <code>NULL</code>, respectively. The semantics of <code>value_null</code>
     and <code>value_not_null</code> are similar to those of the
     <code>null</code> and <code>not_null</code> specifiers
     (<a href="#10.2.3">Section 10.2.3, "<code>null</code>/<code>not_null</code>"</a>).
     For example:</p>

  <pre class="c++">
using std::tr1::shared_ptr;

#pragma db object
class account
{
  ...
};

typedef std::vector&lt;shared_ptr&lt;account> > accounts;
#pragma db value(accounts) value_not_null
  </pre>

  <p>For set and multiset containers (<a href="#5.2">Section 5.2, "Set and
     Multiset Containers"</a>) the element value is automatically treated
     as not allowing a <code>NULL</code> value.</p>


  <h3><a name="10.2.11">10.2.11 <code>id_options</code></a></h3>

  <p>The <code>id_options</code> specifier specifies additional
     column definition options that should be used for a container's
     id column. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> nicknames;
#pragma db value(nicknames) id_options("COLLATE binary")
  </pre>

  <p>The semantics of the <code>id_options</code> specifier for a container
     type are similar to those of the <code>id_options</code> specifier for
     a container data member (<a href="#10.3.16">Section 10.3.16,
     "<code>id_options</code>"</a>).</p>


  <h3><a name="10.2.12">10.2.12 <code>index_options</code></a></h3>

  <p>The <code>index_options</code> specifier specifies additional
     column definition options that should be used for a container's
     index column. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> nicknames;
#pragma db value(nicknames) index_options("ZEROFILL")
  </pre>

  <p>The semantics of the <code>index_options</code> specifier for a container
     type are similar to those of the <code>index_options</code> specifier for
     a container data member (<a href="#10.3.17">Section 10.3.17,
     "<code>index_options</code>"</a>).</p>


  <h3><a name="10.2.13">10.2.13 <code>key_options</code></a></h3>

  <p>The <code>key_options</code> specifier specifies additional
     column definition options that should be used for a container's
     key column. For example:</p>

  <pre class="c++">
typedef std::map&lt;std::string, std::string> properties;
#pragma db value(properties) key_options("COLLATE binary")
  </pre>

  <p>The semantics of the <code>key_options</code> specifier for a container
     type are similar to those of the <code>key_options</code> specifier for
     a container data member (<a href="#10.3.18">Section 10.3.18,
     "<code>key_options</code>"</a>).</p>


  <h3><a name="10.2.14">10.2.14 <code>value_options</code></a></h3>

  <p>The <code>value_options</code> specifier specifies additional
     column definition options that should be used for a container's
     value column. For example:</p>

  <pre class="c++">
typedef std::set&lt;std::string> nicknames;
#pragma db value(nicknames) value_options("COLLATE binary")
  </pre>

  <p>The semantics of the <code>value_options</code> specifier for a container
     type are similar to those of the <code>value_options</code> specifier for
     a container data member (<a href="#10.3.19">Section 10.3.19,
     "<code>value_options</code>"</a>).</p>


  <h3><a name="10.2.15">10.2.15 <code>id_column</code></a></h3>

  <p>The <code>id_column</code> specifier specifies the column
     name that should be used to store the object id in a
     container's table. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> names;
#pragma db value(names) id_column("id")
  </pre>

  <p>If the column name is not specified, then <code>object_id</code>
     is used by default.</p>

  <h3><a name="10.2.16">10.2.16 <code>index_column</code></a></h3>

  <p>The <code>index_column</code> specifier specifies the column
     name that should be used to store the element index in an
     ordered container's table. For example:</p>

  <pre class="c++">
typedef std::vector&lt;std::string> names;
#pragma db value(names) index_column("name_number")
  </pre>

  <p>If the column name is not specified, then <code>index</code>
     is used by default.</p>

  <h3><a name="10.2.17">10.2.17 <code>key_column</code></a></h3>

  <p>The <code>key_column</code> specifier specifies the column
     name that should be used to store the key in a map
     container's table. For example:</p>

  <pre class="c++">
typedef std::map&lt;unsigned short, float> age_weight_map;
#pragma db value(age_weight_map) key_column("age")
  </pre>

  <p>If the column name is not specified, then <code>key</code>
     is used by default.</p>

  <h3><a name="10.2.18">10.2.18 <code>value_column</code></a></h3>

  <p>The <code>value_column</code> specifier specifies the column
     name that should be used to store the element value in a
     container's table. For example:</p>

  <pre class="c++">
typedef std::map&lt;unsigned short, float> age_weight_map;
#pragma db value(age_weight_map) value_column("weight")
  </pre>

  <p>If the column name is not specified, then <code>value</code>
     is used by default.</p>

  <!-- Data Member Pragmas -->


  <h2><a name="10.3">10.3 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 class="specifiers" border="1">
    <tr>
      <th>Specifier</th>
      <th>Summary</th>
      <th>Section</th>
    </tr>

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

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

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

    <tr>
      <td><code>null</code>/<code>not_null</code></td>
      <td>member can/cannot be <code>NULL</code></td>
      <td><a href="#10.3.4">10.3.4</a></td>
    </tr>

    <tr>
      <td><code>default</code></td>
      <td>default value for a member</td>
      <td><a href="#10.3.5">10.3.5</a></td>
    </tr>

    <tr>
      <td><code>options</code></td>
      <td>database options for a member</td>
      <td><a href="#10.3.6">10.3.6</a></td>
    </tr>

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

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

    <tr>
      <td><code>inverse</code></td>
      <td>member is an inverse side of a bidirectional relationship</td>
      <td><a href="#10.3.9">10.3.9</a></td>
    </tr>

    <tr>
      <td><code>unordered</code></td>
      <td>ordered container should be stored unordered</td>
      <td><a href="#10.3.10">10.3.10</a></td>
    </tr>

    <tr>
      <td><code>table</code></td>
      <td>table name for a container</td>
      <td><a href="#10.3.11">10.3.11</a></td>
    </tr>

    <tr>
      <td><code>index_type</code></td>
      <td>database type for a container's index type</td>
      <td><a href="#10.3.12">10.3.12</a></td>
    </tr>

    <tr>
      <td><code>key_type</code></td>
      <td>database type for a container's key type</td>
      <td><a href="#10.3.13">10.3.13</a></td>
    </tr>

    <tr>
      <td><code>value_type</code></td>
      <td>database type for a container's value type</td>
      <td><a href="#10.3.14">10.3.14</a></td>
    </tr>

    <tr>
      <td><code>value_null</code>/<code>value_not_null</code></td>
      <td>container's value can/cannot be <code>NULL</code></td>
      <td><a href="#10.3.15">10.3.15</a></td>
    </tr>

    <tr>
      <td><code>id_options</code></td>
      <td>database options for a container's id column</td>
      <td><a href="#10.3.16">10.3.16</a></td>
    </tr>

    <tr>
      <td><code>index_options</code></td>
      <td>database options for a container's index column</td>
      <td><a href="#10.3.17">10.3.17</a></td>
    </tr>

    <tr>
      <td><code>key_options</code></td>
      <td>database options for a container's key column</td>
      <td><a href="#10.3.18">10.3.18</a></td>
    </tr>

    <tr>
      <td><code>value_options</code></td>
      <td>database options for a container's value column</td>
      <td><a href="#10.3.19">10.3.19</a></td>
    </tr>

    <tr>
      <td><code>id_column</code></td>
      <td>column name for a container's object id</td>
      <td><a href="#10.3.20">10.3.20</a></td>
    </tr>

    <tr>
      <td><code>index_column</code></td>
      <td>column name for a container's index</td>
      <td><a href="#10.3.21">10.3.21</a></td>
    </tr>

    <tr>
      <td><code>key_column</code></td>
      <td>column name for a container's key</td>
      <td><a href="#10.3.22">10.3.22</a></td>
    </tr>

    <tr>
      <td><code>value_column</code></td>
      <td>column name for a container's value</td>
      <td><a href="#10.3.23">10.3.23</a></td>
    </tr>

  </table>

  <p>Many of the member specifiers have corresponding value type
     specifiers with the same names (<a href="#10.2">Section 10.2,
     "Value Type Pragmas"</a>). The behavior of such specifiers
     for members is similar to that for value types. The only difference
     is the scope. A particular value type specifier applies to all the
     members of this value type that don't have a pre-member version
     of the specifier, while the member specifier always applies only
     to a single member. Also, with a few exceptions, member specifiers
     take precedence over and override parameters specified with value
     specifiers.</p>

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

  <p>The <code>id</code> specifier specifies that a 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
{
  ...

  #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="10.3.2">10.3.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
{
  ...

  #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.7">Section 3.7, "Making Objects Persistent"</a>.</p>

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

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

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

  #pragma db type("INT")
  bool married_;
};
  </pre>

  <p>The <code>null</code> and <code>not_null</code> (<a href="#10.3.4">Section
     10.3.4, "<code>null</code>/<code>not_null</code>"</a>) specifiers
     can be used to control the NULL semantics of a data member.</p>

  <h3><a name="10.3.4">10.3.4 <code>null</code>/<code>not_null</code></a></h3>

  <p>The <code>null</code> and <code>not_null</code> specifiers specify that
     a data member can or cannot be <code>NULL</code>, respectively.
     By default, data members of basic value types for which database
     mapping is provided by the ODB compiler do not allow <code>NULL</code>
     values while data members of object pointers allow <code>NULL</code>
     values. Other value types, such as those provided by the profile
     libraries (<a href="#III">Part III, "Profiles"</a>), may or may
     not allow <code>NULL</code> values, depending on the semantics
     of each value type. Consult the relevant documentation to find
     out more about the <code>NULL</code> semantics for such value
     types. A data member containing the object id (<a href="#10.3.1">Section
     10.3.1, "<code>id</code>"</a> ) is automatically treated as not
     allowing a <code>NULL</code> value. Data members that
     allow <code>NULL</code> values are mapped in a relational database
     to columns that allow <code>NULL</code> values. For example:</p>

  <pre class="c++">
using std::tr1::shared_ptr;

#pragma db object
class person
{
  ...

  #pragma db null
  std::string name_;
};

#pragma db object
class account
{
  ...

  #pragma db not_null
  shared_ptr&lt;person> holder_;
};
</pre>

  <p>The <code>NULL</code> semantics can also be specified on the
     per-type basis (<a href="#10.2.3">Section 10.2.3,
     "<code>null</code>/<code>not_null</code>"</a>). If both a type and
     a member have <code>null</code>/<code>not_null</code> specifiers,
     then the member specifier takes precedence. If a member specifier
     relaxes the <code>NULL</code> semantics (that is, if a member has
     the <code>null</code> specifier and the type has the explicit
     <code>not_null</code> specifier), then a warning is issued.</p>

  <p>For a more detailed discussion of the <code>NULL</code> semantics
     for values, refer to <a href="#7.3">Section 7.3, "Pointers and
     <code>NULL</code> Value Semantics"</a>. For a more detailed
     discussion of the <code>NULL</code> semantics for object pointers,
     refer to <a href="#6">Chapter 6, "Relationships"</a>.</p>

  <h3><a name="10.3.5">10.3.5 <code>default</code></a></h3>

  <p>The <code>default</code> specifier specifies the database default value
     that should be used for a data member. For example:</p>

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

  #pragma db default(-1)
  int age_;              // Mapped to INT NOT NULL DEFAULT -1.
};
  </pre>

  <p>A default value can be the special <code>null</code> keyword,
     a <code>bool</code> literal (<code>true</code> or <code>false</code>),
     an integer literal, a floating point literal, a string literal, or
     an enumerator name. If you need to specify a default value that is
     an expression, for example an SQL function call, then you can use
     the <code>options</code> specifier (<a href="#10.3.6">Section
     10.3.6, "<code>options</code>"</a>) instead. For example:</p>

  <pre class="c++">
enum gender {male, female, undisclosed};

#pragma db object
class person
{
  ...

  #pragma db default(null)
  odb::nullable&lt;std::string> middle_; // DEFAULT NULL

  #pragma db default(false)
  bool married_;                      // DEFAULT 0/FALSE

  #pragma db default(0.0)
  float weight_;                      // DEFAULT 0.0

  #pragma db default("Mr")
  string title_;                      // DEFAULT 'Mr'

  #pragma db default(undisclosed)
  gender gender_;                     // DEFAULT 2/'undisclosed'

  #pragma db options("DEFAULT CURRENT_TIMESTAMP()")
  date timestamp_;                    // DEFAULT CURRENT_TIMESTAMP()
};
  </pre>

  <p>Default values specified as enumerators are only supported for
     members that are mapped to an <code>ENUM</code> or an integer
     type in the database, which is the case for the automatic
     mapping of C++ enums to suitable database types as performed
     by the ODB compiler. If you have mapped a C++ enum to another
     database type, then you should use a literal corresponding
     to that type to specify the default value. For example:</p>

  <pre class="c++">
enum gender {male, female, undisclosed};
#pragma db value(gender) type("VARCHAR(11)")

#pragma db object
class person
{
  ...

  #pragma db default("undisclosed")
  gender gender_;                   // DEFAULT 'undisclosed'
};
  </pre>

  <p>A default value can also be specified on the per-type basis
     (<a href="#10.2.4">Section 10.2.4, "<code>default</code>"</a>).
     An empty <code>default</code> specifier can be used to reset
     a default value that was previously specified on the per-type
     basis. For example:</p>

  <pre class="c++">
#pragma db value(std::string) default("")

#pragma db object
class person
{
  ...

  #pragma db default()
  std::string name_;   // No default value.
};
  </pre>

  <p>A data member containing the object id (<a href="#10.3.1">Section
     10.3.1, "<code>id</code>"</a> ) is automatically treated as not
     having a default value even if its type specifies a default value.</p>

  <p>Note also that default values do not affect the generated C++ code
     in any way. In particular, no automatic initialization of data members
     with their default values is performed at any point. If you need such
     an initialization, you will need to implement it yourself, for example,
     in your persistent class constructors. The default values only
     affect the generated database schemas and, in the context of ODB,
     are primarily useful for schema evolution.</p>


  <h3><a name="10.3.6">10.3.6 <code>options</code></a></h3>

  <p>The <code>options</code> specifier specifies additional column
     definition options that should be used for a data member. For
     example:</p>

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

  #pragma db options("UNIQUE")
  std::string email_; // Mapped to TEXT NOT NULL UNIQUE.
};
  </pre>

  <p>Options can also be specified on the per-type basis
     (<a href="#10.2.5">Section 10.2.5, "<code>options</code>"</a>).
     By default, options are accumulating. That is, the ODB compiler
     first adds all the options specified for a value type followed
     by all the options specified for a data member. To clear the
     accumulated options at any point in this sequence you can use
     an empty <code>options</code> specifier. For example:</p>

  <pre class="c++">
#pragma db value(std::string) options("COLLATE binary")

#pragma db object
class person
{
  ...

  std::string first_; // TEXT NOT NULL COLLATE binary

  #pragma db options("UNIQUE")
  std::string last_;  // TEXT NOT NULL COLLATE binary UNIQUE

  #pragma db options()
  std::string title_; // TEXT NOT NULL

  #pragma db options() options("UNIQUE")
  std::string email_; // TEXT NOT NULL UNIQUE
};
  </pre>

  <p>ODB provides dedicated specifiers for specifying column types
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>),
     <code>NULL</code> constraints (<a href="#10.3.4">Section 10.3.4,
     "<code>null</code>/<code>not_null</code>"</a>), and default
     values (<a href="#10.3.5">Section 10.3.5, "<code>default</code>"</a>).
     For ODB to function correctly these specifiers should always be
     used instead of the opaque <code>options</code> specifier for
     these components of a column definition.</p>

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

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

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

  #pragma db id column("person_id")
  unsigned long id_;
};
  </pre>

  <p>For a member of a composite value type, the <code>column</code> specifier
     specifies the column name prefix. Refer to <a href="#7.2.1">Section 7.2.1,
     "Composite Value Column and Table Names"</a> for details.</p>

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

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

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

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

  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>

  <h3><a name="10.3.9">10.3.9 <code>inverse</code></a></h3>

  <p>The <code>inverse</code> specifier specifies that a data member of
     an object pointer or a container of object pointers type is an
     inverse side of a bidirectional object relationship. The single
     required argument to this specifier is the corresponding data
     member name in the referenced object. For example:</p>

<pre class="c++">
using std::tr1::shared_ptr;
using std::tr1::weak_ptr;

class person;

#pragma db object pointer(shared_ptr)
class employer
{
  ...

  std::vector&lt;shared_ptr&lt;person> > employees_;
};

#pragma db object pointer(shared_ptr)
class person
{
  ...

  #pragma db inverse(employee_)
  weak_ptr&lt;employer> employer_;
};
  </pre>

  <p>An inverse member does not have a corresponding column or, in case
     of a container, table in the resulting database schema. Instead, the
     column or table from the referenced object is used to retrieve the
     relationship information. Only ordered and set containers can be used
     for inverse members. If an inverse member is of an ordered container
     type, it is automatically marked as unordered
     (<a href="#10.3.10">Section 10.3.10, "<code>unordered</code>"</a>).</p>

  <p>For a more detailed discussion of inverse members, refer to
     <a href="#6.2">Section 6.2, "Bidirectional Relationships"</a>.</p>

  <h3><a name="10.3.10">10.3.10 <code>unordered</code></a></h3>

  <p>The <code>unordered</code> specifier specifies that a member of
     an ordered container type should be stored unordered in the database.
     The database table for such a member will not contain the index column
     and the order in which elements are retrieved from the database may
     not be the same as the order in which they were stored. For example:</p>

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

  #pragma db unordered
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>For a more detailed discussion of ordered containers and their
     storage in the database, refer to <a href="#5.1">Section 5.1,
     "Ordered Containers"</a>.</p>

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

  <p>The <code>table</code> specifier specifies the table name that should
     be used to store the contents of a container member. For example:</p>

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

  #pragma db table("nicknames")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>If the table name is not specified, then the container table name
     is constructed by concatenating the object's table name, underscore,
     and the sanitized member name. The sanitized member name is obtained
     by removing the common member name decorations, such as leading and
     trailing underscores, the <code>m_</code> prefix, etc. In the example
     above, without the <code>table</code> specifier, the container's
     table name would have been <code>person_nicknames</code>.</p>

  <p>The <code>table</code> specifier can also be used for members of
     composite value types. In this case it specifies the table name
     prefix for container members inside the composite value type. Refer
     to <a href="#7.2.1">Section 7.2.1, "Composite Value Column and Table
     Names"</a> for details.</p>

  <h3><a name="10.3.12">10.3.12 <code>index_type</code></a></h3>

  <p>The <code>index_type</code> specifier specifies the native
     database type that should be used for an ordered container's
     index column of a data member. The semantics of <code>index_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>). The native
     database type is expected to be an integer type. For example:</p>

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

  #pragma db index_type("SMALLINT UNSIGNED")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <h3><a name="10.3.13">10.3.13 <code>key_type</code></a></h3>

  <p>The <code>key_type</code> specifier specifies the native
     database type that should be used for a map container's
     key column of a data member. The semantics of <code>key_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>). For
     example:</p>

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

  #pragma db key_type("INT UNSIGNED")
  std::map&lt;unsigned short, float> age_weight_map_;
};
  </pre>

  <h3><a name="10.3.14">10.3.14 <code>value_type</code></a></h3>

  <p>The <code>value_type</code> specifier specifies the native
     database type that should be used for a container's
     value column of a data member. The semantics of <code>value_type</code>
     are similar to those of the <code>type</code> specifier
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>). For
     example:</p>

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

  #pragma db value_type("VARCHAR(255)")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>The <code>value_null</code> and <code>value_not_null</code>
     (<a href="#10.3.15">Section 10.3.15,
     "<code>value_null</code>/<code>value_not_null</code>"</a>) specifiers
     can be used to control the NULL semantics of a value column.</p>

  <h3><a name="10.3.15">10.3.15 <code>value_null</code>/<code>value_not_null</code></a></h3>

  <p>The <code>value_null</code> and <code>value_not_null</code> specifiers
     specify that a container's element value for a data member can or
     cannot be <code>NULL</code>, respectively. The semantics of
     <code>value_null</code> and <code>value_not_null</code> are similar
     to those of the <code>null</code> and <code>not_null</code> specifiers
     (<a href="#10.3.4">Section 10.3.4, "<code>null</code>/<code>not_null</code>"</a>).
     For example:</p>

  <pre class="c++">
using std::tr1::shared_ptr;

#pragma db object
class person
{
  ...
};

#pragma db object
class account
{
  ...

  #pragma db value_not_null
  std::vector&lt;shared_ptr&lt;person> > holders_;
};
  </pre>

  <p>For set and multiset containers (<a href="#5.2">Section 5.2, "Set and
     Multiset Containers"</a>) the element value is automatically treated
     as not allowing a <code>NULL</code> value.</p>

  <h3><a name="10.3.16">10.3.16 <code>id_options</code></a></h3>

  <p>The <code>id_options</code> specifier specifies additional
     column definition options that should be used for a container's
     id column of a data member. For example:</p>

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

  #pragma db id options("COLLATE binary")
  std::string name_;

  #pragma db id_options("COLLATE binary")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>The semantics of <code>id_options</code> are similar to those
     of the <code>options</code> specifier (<a href="#10.3.6">Section
     10.3.6, "<code>options</code>"</a>).</p>

  <h3><a name="10.3.17">10.3.17 <code>index_options</code></a></h3>

  <p>The <code>index_options</code> specifier specifies additional
     column definition options that should be used for a container's
     index column of a data member. For example:</p>

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

  #pragma db index_options("ZEROFILL")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>The semantics of <code>index_options</code> are similar to those
     of the <code>options</code> specifier (<a href="#10.3.6">Section
     10.3.6, "<code>options</code>"</a>).</p>

  <h3><a name="10.3.18">10.3.18 <code>key_options</code></a></h3>

  <p>The <code>key_options</code> specifier specifies additional
     column definition options that should be used for a container's
     key column of a data member. For example:</p>

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

  #pragma db key_options("COLLATE binary")
  std::map&lt;std::string, std::string> properties_;
};
  </pre>

  <p>The semantics of <code>key_options</code> are similar to those
     of the <code>options</code> specifier (<a href="#10.3.6">Section
     10.3.6, "<code>options</code>"</a>).</p>

  <h3><a name="10.3.19">10.3.19 <code>value_options</code></a></h3>

  <p>The <code>value_options</code> specifier specifies additional
     column definition options that should be used for a container's
     value column of a data member. For example:</p>

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

  #pragma db value_options("COLLATE binary")
  std::set&lt;std::string> nicknames_;
};
  </pre>

  <p>The semantics of <code>value_options</code> are similar to those
     of the <code>options</code> specifier (<a href="#10.3.6">Section
     10.3.6, "<code>options</code>"</a>).</p>

  <h3><a name="10.3.20">10.3.20 <code>id_column</code></a></h3>

  <p>The <code>id_column</code> specifier specifies the column
     name that should be used to store the object id in a
     container's table for a data member. The semantics of
     <code>id_column</code> are similar to those of the
     <code>column</code> specifier
     (<a href="#10.3.7">Section 10.3.7, "<code>column</code>"</a>).
     For example:</p>

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

  #pragma db id_column("person_id")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>If the column name is not specified, then <code>object_id</code>
     is used by default.</p>

  <h3><a name="10.3.21">10.3.21 <code>index_column</code></a></h3>

  <p>The <code>index_column</code> specifier specifies the column
     name that should be used to store the element index in an
     ordered container's table for a data member. The semantics of
     <code>index_column</code> are similar to those of the
     <code>column</code> specifier
     (<a href="#10.3.7">Section 10.3.7, "<code>column</code>"</a>).
     For example:</p>

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

  #pragma db index_column("nickname_number")
  std::vector&lt;std::string> nicknames_;
};
  </pre>

  <p>If the column name is not specified, then <code>index</code>
     is used by default.</p>

  <h3><a name="10.3.22">10.3.22 <code>key_column</code></a></h3>

  <p>The <code>key_column</code> specifier specifies the column
     name that should be used to store the key in a map
     container's table for a data member. The semantics of
     <code>key_column</code> are similar to those of the
     <code>column</code> specifier
     (<a href="#10.3.7">Section 10.3.7, "<code>column</code>"</a>).
     For example:</p>

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

  #pragma db key_column("age")
  std::map&lt;unsigned short, float> age_weight_map_;
};
  </pre>

  <p>If the column name is not specified, then <code>key</code>
     is used by default.</p>

  <h3><a name="10.3.23">10.3.23 <code>value_column</code></a></h3>

  <p>The <code>value_column</code> specifier specifies the column
     name that should be used to store the element value in a
     container's table for a data member. The semantics of
     <code>value_column</code> are similar to those of the
     <code>column</code> specifier
     (<a href="#10.3.7">Section 10.3.7, "<code>column</code>"</a>).
     For example:</p>

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

  #pragma db value_column("weight")
  std::map&lt;unsigned short, float> age_weight_map_;
};
  </pre>

  <p>If the column name is not specified, then <code>value</code>
     is used by default.</p>

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

  <p>When a C++ header file defining persistent classes and containing
     ODB pragmas is used to build the application, the C++ compiler may
     issue warnings about pragmas that it doesn't 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 we
     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
{
  ...

  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
{
  ...

  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="10.4.1">10.4.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, we 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="10.4.2">10.4.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 we have disabled
     the warnings altogether (level 0), we will see this warning.</p>

  <p>To disable this warning via the compiler command line, we 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 we can enter warning numbers that should be disabled. Simply
     enter 4068 into this field.</p>

  <p>We 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
{
  ...

  #pragma db id
  unsigned long id_;
};

#pragma warning (pop)
  </pre>

  <h3><a name="10.4.3">10.4.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 we 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="10.4.4">10.4.4 IBM XL C++</a></h3>

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

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

  <h3><a name="10.4.5">10.4.5 HP aC++</a></h3>

  <p>HP aC++ (aCC) issues an unknown pragma warning (2161) by default.
     To disable this warning we can add the <code>+W2161</code>
     command line option, for example:</p>

  <pre class="terminal">
aCC +W2161 ...
  </pre>


  <!-- PART -->


  <hr class="page-break"/>
  <h1><a name="II">PART II&nbsp;&nbsp;
      <span style="font-weight: normal;">DATABASE SYSTEMS</span></a></h1>

  <p>Part II 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. Part II consists of the following
     chapters.</p>

  <table class="toc">
    <tr><th>11</th><td><a href="#11">MySQL Database</a></td></tr>
    <tr><th>12</th><td><a href="#12">SQLite Database</a></td></tr>
    <tr><th>13</th><td><a href="#13">PostgreSQL Database</a></td></tr>
  </table>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="11">11 MySQL Database</a></h1>

  <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>

  <h2><a name="11.1">11.1 MySQL Type Mapping</a></h2>

  <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
     Pragma Language (<a href="#10">Chapter 10, "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>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    <tr>
      <td><code>std::string</code></td>
      <td><code>TEXT/VARCHAR(255)</code></td>
      <td><code>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 the <code>VARCHAR(255)</code> MySQL type. Otherwise,
     it is mapped to <code>TEXT</code>.</p>

  <p>The MySQL ODB runtime library also provides support for mapping the
     <code>std::vector&lt;char></code> type to the MySQL BLOB types.
     However, this mapping is not enabled by default (by default,
     <code>std::vector&lt;char></code> will be treated as a container).
     To enable the BLOB mapping for this type we need to specify
     the database type explicitly using the <code>db&nbsp;type</code>
     pragma (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>),
     for example:</p>

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

  #pragma db type("BLOB")
  std::vector&lt;char> buf_;
};
  </pre>

  <p>Alternatively, this can be done on the per-type basis, for example:</p>

  <pre class="c++">
typedef std::vector&lt;char> buffer;
#pragma db value(buffer) type("BLOB")

#pragma db object
class object
{
  ...

  buffer buf_; // Mapped to BLOB.
};
  </pre>

  <p>Additionally, by default, C++ enumerations are automatically
     mapped to a suitable MySQL type. Contiguous enumerations with
     the zero first enumerator are mapped to the MySQL <code>ENUM</code>
     type. All other enumerations are mapped to <code>INT</code> or
     <code>INT UNSIGNED</code>. In both cases the default <code>NULL</code>
     semantics is <code>NOT NULL</code>. For example:</p>

  <pre class="c++">
enum color {red, green, blue};
enum taste
{
  bitter = 1, // Non-zero first enumerator.
  sweet,
  sour = 4,   // Non-contiguous.
  salty
};

#pragma db object
class object
{
  ...

  color color_; // Mapped to ENUM ('red', 'green', 'blue') NOT NULL.
  taste taste_; // Mapped to INT UNSIGNED NOT NULL.
};
  </pre>

  <h2><a name="11.2">11.2 MySQL Database Class</a></h2>

  <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,
                const char* charset = 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,
                const std::string&amp; charset = "",
                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,
                const std::string&amp; charset = "",
                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,
                const std::string&amp; charset = "",
                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,
                const std::string&amp; charset = "",
                unsigned long client_flags = 0,
                std::auto_ptr&lt;connection_factory> = 0);

      database (int&amp; argc,
                char* argv[],
                bool erase = false,
                const std::string&amp; charset = "",
                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;

      const char*
      charset () const;

      unsigned long
      client_flags () const;

    public:
      connection_ptr
      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 us
     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>.</p>

  <p>The <code>charset</code> argument allows us to specify the client
     character set, that is, the character set in which the application
     will encode its text data. Note that this can be different from
     the MySQL server character set. If this argument is not specified or
     is empty, then the default MySQL client character set is used, normally
     <code>latin1</code>. Commonly used values for this argument are
     <code>latin1</code> (equivalent to Windows cp1252 and similar to
     ISO-8859-1) and <code>utf8</code>. For other possible values
     as well as more information on character set support in MySQL,
     refer to the MySQL documentation.</p>

  <p>The <code>client_flags</code> argument allows us 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 us to specify some
     or all of the database options in a file with each option appearing
     on a separate line followed by a 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="#11.4">Section 11.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 a
     pointer to the connection factory. If we 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 allows us
     to query the parameters of the <code>database</code> instance.</p>

  <p>The <code>connection()</code> function returns a pointer to the
     MySQL database connection encapsulated by the
     <code>odb::mysql::connection</code> class. For more information
     on <code>mysql::connection</code>, refer to <a href="#11.3">Section
     11.3, "MySQL Connection and Connection Factory"</a>.</p>

  <h2><a name="11.3">11.3 MySQL Connection and Connection Factory</a></h2>

  <p>The <code>mysql::connection</code> class has the following interface:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class connection: public odb::connection
    {
    public:
      connection (database&amp;);
      connection (database&amp;, MYSQL*);

      MYSQL*
      handle ();
    };

    typedef details::shared_ptr&lt;connection> connection_ptr;
  }
}
  </pre>

  <p>For more information on the <code>odb::connection</code> interface,
     refer to <a href="#3.5">Section 3.5, "Connections"</a>. The first
     overloaded <code>mysql::connection</code> constructor establishes a
     new MySQL connection. The second constructor allows us to create
     a <code>connection</code> instance by providing an already connected
     native MySQL handle. Note that the <code>connection</code>
     instance assumes ownership of this handle. The <code>handle()</code>
     accessor returns the MySQL handle corresponding to the connection.</p>

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

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

      virtual connection_ptr
      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
     <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>new_connection_factory</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  namespace mysql
  {
    class new_connection_factory: public connection_factory
    {
    public:
      new_connection_factory ();
    };
};
  </pre>

  <p>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
    {
    public:
      connection_pool_factory (std::size_t max_connections = 0,
                               std::size_t min_connections = 0,
                               bool ping = true);

    protected:
      class pooled_connection: public connection
      {
      public:
        pooled_connection (database_type&amp;);
        pooled_connection (database_type&amp;, MYSQL*);
      };

      typedef details::shared_ptr&lt;pooled_connection> pooled_connection_ptr;

      virtual pooled_connection_ptr
      create ();
    };
};
  </pre>

  <p>The <code>max_connections</code> argument in the
     <code>connection_pool_factory</code> constructor 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. The <code>ping</code> argument specifies
     whether the factory should validate the connection before
     returning it to the caller.</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 caller is blocked until
     a connection becomes available.</p>

  <p>When a connection is released, the pool factory first checks
     if there are blocked callers waiting for a connection. If so, then
     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 <code>min_connections</code>
     and there are no callers 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>Connection validation (the <code>ping</code> argument) is useful
     if your application may experience long periods of inactivity. In
     such cases the MySQL server may close network connections that have
     been inactive for too long. If during connection validation the pool
     factory detects that the connection has been terminated, it silently
     closes it and tries to find or create another connection instead.</p>

  <p>The <code>create()</code> virtual function is called whenever the
     pool needs to create a new connection. By deriving from the
     <code>connection_pool_factory</code> class and overriding this
     function we can implement custom connection establishment
     and configuration.</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 <code>0</code> and connection validation enabled.
     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>

  <h2><a name="11.4">11.4 MySQL Exceptions</a></h2>

  <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>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="12">12 SQLite Database</a></h1>

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

  <h2><a name="12.1">12.1 SQLite Type Mapping</a></h2>

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

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>C++ Type</th>
      <th>SQLite Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>bool</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

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

    <tr>
      <td><code>unsigned char</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

    <tr>
      <td><code>unsigned short</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

    <tr>
      <td><code>unsigned int</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

    <tr>
      <td><code>unsigned long</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

    <tr>
      <td><code>unsigned long long</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

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

    <tr>
      <td><code>std::string</code></td>
      <td><code>TEXT</code></td>
      <td><code>NOT NULL</code></td>
    </tr>
  </table>

  <p>The SQLite ODB runtime library also provides support for mapping the
     <code>std::vector&lt;char></code> type to the SQLite BLOB type.
     However, this mapping is not enabled by default (by default,
     <code>std::vector&lt;char></code> will be treated as a container).
     To enable the BLOB mapping for this type we need to specify
     the database type explicitly using the <code>db&nbsp;type</code>
     pragma (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>),
     for example:</p>

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

  #pragma db type("BLOB")
  std::vector&lt;char> buf_;
};
  </pre>

  <p>Alternatively, this can be done on the per-type basis, for example:</p>

  <pre class="c++">
typedef std::vector&lt;char> buffer;
#pragma db value(buffer) type("BLOB")

#pragma db object
class object
{
  ...

  buffer buf_; // Mapped to BLOB.
};
  </pre>

  <p>Additionally, by default, C++ enumerations are automatically mapped to
     the SQLite <code>INTEGER</code> type with the default <code>NULL</code>
     semantics being <code>NOT NULL</code>.</p>

  <p>Note also that SQLite only operates with signed integers and the largest
     value that an SQLite database can store is a signed 64-bit integer. As
     a result, greater <code>unsigned&nbsp;long&nbsp;long</code> values will be
     represented in the database as negative values.</p>

  <h2><a name="12.2">12.2 SQLite Database Class</a></h2>

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

  <pre class="c++">
namespace odb
{
  namespace sqlite
  {
    class database: public odb::database
    {
    public:
      database (const std::string&amp; name,
                int flags = SQLITE_OPEN_READWRITE,
                bool foreign_keys = true,
                std::auto_ptr&lt;connection_factory> = 0);

      database (int&amp; argc,
                char* argv[],
                bool erase = false,
                int flags = SQLITE_OPEN_READWRITE,
                bool foreign_keys = true,
                std::auto_ptr&lt;connection_factory> = 0);

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

    public:
      const std::string&amp;
      name () const;

      int
      flags () const;

    public:
      transaction
      begin_immediate ();

      transaction
      begin_exclusive ();

    public:
      connection_ptr
      connection ();
    };
  }
}
  </pre>

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

  <p>The first constructor opens the specified SQLite database. The
     <code>name</code> argument is the database file
     name to open. If this argument is empty, then a temporary,
     on-disk database is created. If this argument is the
     <code>:memory:</code> special value, then a temporary, in-memory
     database is created. The <code>flags</code> argument allows us to
     specify SQLite opening flags. For more information on the possible
     values, refer to the <code>sqlite3_open_v2()</code> function description
     in the SQLite C API documentation. The <code>foreign_keys</code>
     argument specifies whether foreign key constraints checking
     should be enabled. See <a href="#12.5.3">Section 12.5.3,
     "Foreign Key Constraints"</a> for more information on foreign
     keys.</p>

  <p>The following example shows how we can open the <code>test.db</code>
     database in the read-write mode and create it if it does not exist:</p>

  <pre class="c++">
auto_ptr&lt;odb::database> db (
  new odb::sqlite::database (
    "test.db",
    SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
  </pre>

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

  <pre class="terminal">
  --database &lt;name>
  --create
  --read-only
  --options-file &lt;file>
  </pre>

  <p>By default, this constructor opens the database in the read-write mode
     (<code>SQLITE_OPEN_READWRITE</code> flag). If the <code>--create</code>
     flag is specified, then the database file is created if it does
     not already exist (<code>SQLITE_OPEN_CREATE</code> flag). If the
     <code>--read-only</code> flag is specified, then the database is
     opened in the read-only mode (<code>SQLITE_OPEN_READONLY</code>
     flag instead of <code>SQLITE_OPEN_READWRITE</code>). The
     <code>--options-file</code> option allows us to specify some
     or all of the database options in a file with each option appearing
     on a separate line followed by a 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 SQLite
     options out of the <code>argv</code> array.</p>

  <p>The <code>flags</code> argument has the same semantics as in
     the first constructor. Flags from the command line always override
     the corresponding values specified with this argument.</p>

  <p>The second constructor throws the <code>odb::sqlite::cli_exception</code>
     exception if the SQLite option values are missing or invalid.
     See <a href="#12.4">Section 12.4, "SQLite 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 the second constructor.</p>

  <p>The last argument to both constructors is a pointer to the connection
     factory. If we 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 allows us
     to query the parameters of the <code>database</code> instance.</p>

  <p>The <code>begin_immediate()</code> and <code>begin_exclusive()</code>
     functions are the SQLite-specific extensions to the standard
     <code>odb::database::begin()</code> function (see
     <a href="#3.4">Section 3.4, "Transactions"</a>). They allow us
     to start an immediate (<code>BEGIN IMMEDIATE</code>) and an exclusive
     (<code>BEGIN EXCLUSIVE</code>) SQLite transaction, respectively.
     For more information on the semantics of the immediate and exclusive
     transactions, refer to the <code>BEGIN</code> statement description
     in the SQLite documentation.</p>

  <p>The <code>connection()</code> function returns a pointer to the
     SQLite database connection encapsulated by the
     <code>odb::sqlite::connection</code> class. For more information
     on <code>sqlite::connection</code>, refer to <a href="#12.3">Section
     12.3, "SQLite Connection and Connection Factory"</a>.</p>

  <h2><a name="12.3">12.3 SQLite Connection and Connection Factory</a></h2>

  <p>The <code>sqlite::connection</code> class has the following interface:</p>

  <pre class="c++">
namespace odb
{
  namespace sqlite
  {
    class connection: public odb::connection
    {
    public:
      connection (database&amp;, int extra_flags = 0);
      connection (database&amp;, sqlite3*);

      transaction
      begin_immediate ();

      transaction
      begin_exclusive ();

      sqlite3*
      handle ();
    };

    typedef details::shared_ptr&lt;connection> connection_ptr;
  }
}
  </pre>

  <p>For more information on the <code>odb::connection</code> interface,
     refer to <a href="#3.5">Section 3.5, "Connections"</a>. The first
     overloaded <code>sqlite::connection</code> constructor opens
     a new SQLite connection. The <code>extra_flags</code> argument can
     be used to specify extra <code>sqlite3_open_v2()</code> flags
     that are combined with the flags specified in the
     <code>sqlite::database</code> constructor. The second constructor
     allows us to create a <code>connection</code> instance by providing
     an already open native SQLite handle. Note that the
     <code>connection</code> instance assumes ownership of this handle.</p>

  <p>The <code>begin_immediate()</code> and <code>begin_exclusive()</code>
     functions allow us to start an immediate and an exclusive SQLite
     transaction on the connection, respectively. Their semantics are
     equivalent to the corresponding functions defined in the
     <code>sqlite::database</code> class (<a href="#12.2">Section 12.2,
     "SQLite Database Class"</a>). The <code>handle()</code> accessor
     returns the SQLite handle corresponding to the connection.</p>

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

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

      virtual connection_ptr
      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::sqlite::database</code> class constructors. The
     <code>connect()</code> function is called whenever a database
     connection is requested.</p>

  <p>The three implementations of the <code>connection_factory</code>
     interface provided by the SQLite ODB runtime library are
     <code>single_connection_factory</code>,
     <code>new_connection_factory</code>, and
     <code>connection_pool_factory</code>. You will need to include
     the <code>&lt;odb/sqlite/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>single_connection_factory</code> class creates a
     single connection that is shared between all the threads in
     an application. If the connection is currently not in use,
     then it is returned to the caller. Otherwise, the caller is
     blocked until the connection becomes available. The
     <code>single_connection_factory</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  namespace sqlite
  {
    class single_connection_factory: public connection_factory
    {
    public:
      single_connection_factory ();

    protected:
      class single_connection: public connection
      {
      public:
        single_connection (database_type&amp;);
        single_connection (database_type&amp;, MYSQL*);
      };

      typedef details::shared_ptr&lt;single_connection> single_connection_ptr;

      virtual single_connection_ptr
      create ();
    };
};
  </pre>

  <p>The <code>create()</code> virtual function is called when the
     factory needs to create the connection. By deriving from the
     <code>single_connection_factory</code> class and overriding this
     function we can implement custom connection establishment
     and configuration.</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>new_connection_factory</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  namespace sqlite
  {
    class new_connection_factory: public connection_factory
    {
    public:
      new_connection_factory ();
    };
};
  </pre>

  <p>The <code>connection_pool_factory</code> class implements a
     connection pool. It has the following interface:</p>

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

    protected:
      class pooled_connection: public connection
      {
      public:
        pooled_connection (database_type&amp;, int extra_flags = 0);
        pooled_connection (database_type&amp;, sqlite3*);
      };

      typedef details::shared_ptr&lt;pooled_connection> pooled_connection_ptr;

      virtual pooled_connection_ptr
      create ();
    };
};
  </pre>

  <p>The <code>max_connections</code> argument in the
     <code>connection_pool_factory</code> constructor 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 caller is blocked until
     a connection becomes available.</p>

  <p>When a connection is released, the pool factory first checks
     if there are blocked callers waiting for a connection. If so, then
     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 <code>min_connections</code>
     and there are no callers 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>The <code>create()</code> virtual function is called whenever the
     pool needs to create a new connection. By deriving from the
     <code>connection_pool_factory</code> class and overriding this
     function we can implement custom connection establishment
     and configuration.</p>

  <p>By default, connections created by <code>new_connection_factory</code>
     and <code>connection_pool_factory</code> enable the SQLite shared cache
     mode and use the unlock notify functionality to aid concurrency. To
     disable the shared cache mode you can pass the
     <code>SQLITE_OPEN_PRIVATECACHE</code> flag when creating the database
     instance. For more information on the shared cache mode refer to the
     SQLite documentation.</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 <code>0</code>. 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/sqlite/database.hxx>
#include &lt;odb/sqlite/connection-factory.hxx>

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

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

  <h2><a name="12.4">12.4 SQLite Exceptions</a></h2>

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

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

      int
      extended_error () 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/sqlite/exceptions.hxx></code>
     header file to make these exceptions available in your application.</p>

  <p>The <code>odb::sqlite::database_exception</code> is thrown if
     an SQLite database operation fails. The SQLite-specific error
     information is accessible via the <code>error()</code>,
     <code>extended_error()</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::sqlite::cli_exception</code> is thrown by the
     command line parsing constructor of the <code>odb::sqlite::database</code>
     class if the SQLite option values are missing or invalid. The
     <code>what()</code> function returns a human-readable description
     of an error.</p>


  <h2><a name="12.5">12.5 SQLite Limitations</a></h2>

  <p>The following sections describe SQLite-specific limitations imposed by
     the current SQLite and ODB runtime versions.</p>

  <h3><a name="12.5.1">12.5.1 Query Result Caching</a></h3>

  <p>SQLite ODB runtime implementation does not perform query result caching
     (<a href="#4.4">Section 4.4, "Query Result"</a>) even when explicitly
     requested. The SQLite API supports interleaving execution of multiple
     prepared statements on a single connection. As a result, with SQLite, it
     is possible to have multiple uncached results and calls to other database
     functions do not invalidate them. The only limitation of the uncached
     SQLite results is the unavailability of the <code>result::size()</code>
     function. If you call this function on an SQLite query result, then
     the <code>odb::result_not_cached</code> exception
     (<a href="#3.12">Section 3.12, "ODB Exceptions"</a>) is always
     thrown. Future versions of the SQLite ODB runtime library may add support
     for result caching.</p>

  <h3><a name="12.5.2">12.5.2 Automatic Assignment of Object Ids</a></h3>

  <p>Due to SQLite API limitations, every automatically assigned object id
     (<a href="#10.3.2">Section 10.3.2, "<code>auto</code>"</a>) should have
     the <code>INTEGER</code> SQLite type. While SQLite will treat other
     integer type names (such as <code>INT</code>, <code>BIGINT</code>, etc.)
     as <code>INTEGER</code>, automatic id assignment will not work. By default,
     ODB maps all C++ integral types to <code>INTEGER</code>. This means that
     the only situation that requires consideration is the assignment of a
     custom database type using the <code>db&nbsp;type</code> pragma
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>). For
     example:</p>

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

  //#pragma db id auto type("INT")     // Will not work.
  //#pragma db id auto type("INTEGER") // Ok.
  #pragma db id auto                   // Ok, Mapped to INTEGER.
  unsigned int id_;
};
</pre>

  <h3><a name="12.5.3">12.5.3 Foreign Key Constraints</a></h3>

  <p>By default the SQLite ODB runtime enables foreign key constraints
     checking (<code>PRAGMA foreign_keys=ON</code>). You can disable foreign
     keys by passing <code>false</code> as the <code>foreign_keys</code>
     argument to one of the <code>odb::sqlite::database</code> constructors.
     Foreign keys will also be disabled if the SQLite library is built without
     support for foreign keys (<code>SQLITE_OMIT_FOREIGN_KEY</code> and
     <code>SQLITE_OMIT_TRIGGER</code> macros) or if you are using
     an SQLite version prior to 3.6.19, which does not support foreign
     key constraints checking.</p>

  <p>If foreign key constraints checking is disabled or not available,
     then inconsistencies in object relationships will not be detected.
     Furthermore, using the <code>erase_query()</code> function
     (<a href="#3.10">Section 3.10, "Deleting Persistent Objects"</a>)
     to delete persistent objects that contain containers will not work
     correctly. Container data for such objects will not be deleted.</p>

  <p>When foreign key constraints checking is enabled, then you may
     get the "foreign key constraint failed" error while re-creating the
     database schema. This error is due to bugs in the SQLite DDL foreign
     keys support. The recommended work-around for this problem is to
     temporarily disable foreign key constraints checking while
     re-creating the schema. The following code fragment shows how
     this can be done:</p>

<pre>
#include &lt;odb/connection.hxx>
#include &lt;odb/transaction.hxx>
#include &lt;odb/schema-catalog.hxx>

odb::database&amp; db = ...

{
  odb::connection_ptr c (db.connection ());

  c->execute ("PRAGMA foreign_keys=OFF");

  odb::transaction t (c->begin ());
  odb::schema_catalog::create_schema (db);
  t.commit ();

  c->execute ("PRAGMA foreign_keys=ON");
}
</pre>

  <p>Finally, ODB relies on standard SQL behavior which requires
     that foreign key constraints checking is deferred until the
     transaction is committed. Default SQLite behavior is to check such
     constraints immediately. As a result, when used with ODB, a custom
     database schema that defines foreign key constraints must declare
     such constraints as <code>DEFERRABLE INITIALLY DEFERRED</code>, as
     shown in the following example. Schemas generated by the ODB compiler
     meet this requirement automatically.</p>

  <pre class="sql">
CREATE TABLE Employee (
  ...
  employer BIGINT REFERENCES Employer (name) DEFERRABLE INITIALLY DEFERRED);
  </pre>


  <h3><a name="12.5.4">12.5.4 Constraint Violations</a></h3>

  <p>Due to the granularity of the SQLite error codes, it is impossible
     to distinguish between the duplicate primary key and other constraint
     violations. As a result, when making an object persistent, The SQLite
     ODB runtime will translate all constraint violation errors to the
     <code>object_not_persistent</code> exception (<a href="#3.12">Section
     3.12, "ODB Exceptions"</a>).</p>

  <h3><a name="12.5.5">12.5.5 Sharing of Queries</a></h3>

  <p>As discussed in <a href="#4.3">Section 4.3, "Executing a Query"</a>, a
     query instance that does not have any by-reference parameters is
     immutable and can be shared between multiple threads without
     synchronization. Currently, the SQLite ODB runtime does not support this
     functionality. Future versions of the library will remove this
     limitation.</p>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="13">13 PostgreSQL Database</a></h1>

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

  <p>ODB utilizes prepared statements extensively. Support for prepared
     statements was added in PostgreSQL version 7.4 with the introduction
     of the messaging protocol version 3.0. For this reason, ODB supports
     only PostgreSQL version 7.4 and later.</p>

  <h2><a name="13.1">13.1 PostgreSQL Type Mapping</a></h2>

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

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>C++ Type</th>
      <th>PostgreSQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>bool</code></td>
      <td><code>BOOLEAN</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

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

    <tr>
      <td><code>unsigned char</code></td>
      <td><code>SMALLINT</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

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

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

    <tr>
      <td><code>unsigned int</code></td>
      <td><code>INTEGER</code></td>
      <td><code>NOT NULL</code></td>
    </tr>

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

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

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

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

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

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

    <tr>
      <td><code>std::string</code></td>
      <td><code>TEXT</code></td>
      <td><code>NOT NULL</code></td>
    </tr>
  </table>

  <p>The PostgreSQL ODB runtime library also provides support for mapping the
     <code>std::vector&lt;char></code> type to the PostgreSQL BYTEA type.
     However, this mapping is not enabled by default (by default,
     <code>std::vector&lt;char></code> will be treated as a container).
     To enable the BYTEA mapping for this type we need to specify
     the database type explicitly using the <code>db&nbsp;type</code>
     pragma (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>),
     for example:</p>

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

  #pragma db type("BYTEA")
  std::vector&lt;char> buf_;
};
  </pre>

  <p>Alternatively, this can be done on the per-type basis, for example:</p>

  <pre class="c++">
typedef std::vector&lt;char> buffer;
#pragma db value(buffer) type("BYTEA")

#pragma db object
class object
{
  ...

  buffer buf_; // Mapped to BYTEA.
};
  </pre>

  <p>Additionally, by default, C++ enumerations are automatically
     mapped to <code>INTEGER</code> with the default <code>NULL</code>
     semantics being <code>NOT NULL</code>.</p>

  <p>Note also that because PostgreSQL does not support unsigned integers,
     the <code>unsigned short</code>, <code>unsigned int</code>, and
     <code>unsigned long long</code> C++ types are by default mapped
     to the <code>SMALLINT</code>, <code>INTEGER</code>, and
     <code>BIGINT</code> PostgreSQL types, respectively. The sign bit
     of the value stored by the database for these types will contain
     the most significant bit of the actual unsigned value being
     persisted.</p>

  <h2><a name="13.2">13.2 PostgreSQL Database Class</a></h2>

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

  <pre class="c++">
namespace odb
{
  namespace pgsql
  {
    class database: public odb::database
    {
    public:
      database (const std::string&amp; user,
                const std::string&amp; password,
                const std::string&amp; db,
                const std::string&amp; host = "",
                unsigned int port = 0,
                const std::string&amp; extra_conninfo = "",
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; user,
                const std::string&amp; password,
                const std::string&amp; db,
                const std::string&amp; host = "",
                const std::string&amp; socket_ext = "",
                const std::string&amp; extra_conninfo = "",
                std::auto_ptr&lt;connection_factory> = 0);

      database (const std::string&amp; conninfo,
                std::auto_ptr&lt;connection_factory> = 0);

      database (int&amp; argc,
                char* argv[],
                bool erase = false,
                const std::string&amp; extra_conninfo = "",
                std::auto_ptr&lt;connection_factory> = 0);

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


    public:
      const std::string&amp;
      user () const;

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

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

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

      unsigned int
      port () const;

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

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

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

    public:
      connection_ptr
      connection ();
    };
  }
}
  </pre>

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

  <p>The overloaded <code>database</code> constructors allow us to specify
     the PostgreSQL database parameters that should be used when connecting
     to the database. The <code>port</code> argument in the first constructor
     is an integer value specifying the TCP/IP port number to connect to. A
     zero port number indicates that the default port should be used.
     The <code>socket_ext</code> argument in the second constructor is a
     string value specifying the UNIX-domain socket file name extension.</p>

  <p>The third constructor allows us to specify all the database parameters
     as a single <code>conninfo</code> string. All other constructors
     accept additional database connection parameters as the
     <code>extra_conninfo</code> argument. For more information
     about the format of the <code>conninfo</code> string, refer to
     the <code>PQconnectdb()</code> function description in the PostgreSQL
     documentation. In the case of <code>extra_conninfo</code>, all the
     database parameters provided in this string will take precedence
     over those explicitly specified with other constructor arguments.</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> | --username &lt;login>
  --password &lt;password>
  --database &lt;name> | --dbname &lt;name>
  --host &lt;host>
  --port &lt;integer>
  --options-file &lt;file>
  </pre>

  <p>The <code>--options-file</code> option allows us to specify some
     or all of the database options in a file with each option appearing
     on a separate line followed by a 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 PostgreSQL
     options out of the <code>argv</code> array.</p>

  <p>This constructor throws the <code>odb::pgsql::cli_exception</code>
     exception if the PostgreSQL option values are missing or invalid.
     See section <a href="#13.4">Section 13.4, "PostgreSQL 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 a pointer to the
     connection factory. If we 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 allows us
     to query the parameters of the <code>database</code> instance. Note that
     the <code>conninfo()</code> accessor returns a complete
     <code>conninfo</code> string which includes parameters that were
     explicitly specified with the various constructor arguments, as well as
     the extra parameters passed in the <code>extra_conninfo</code> argument.
     The <code>extra_conninfo()</code> accessor will return the
     <code>conninfo</code> string as passed in the <code>extra_conninfo</code>
     argument.</p>

  <p>The <code>connection()</code> function returns a pointer to the
     PostgreSQL database connection encapsulated by the
     <code>odb::pgsql::connection</code> class. For more information
     on <code>pgsql::connection</code>, refer to <a href="#13.3">Section
     13.3, "PostgreSQL Connection and Connection Factory"</a>.</p>

  <h2><a name="13.3">13.3 PostgreSQL Connection and Connection Factory</a></h2>

  <p>The <code>pgsql::connection</code> class has the following interface:</p>

  <pre class="c++">
namespace odb
{
  namespace pgsql
  {
    class connection: public odb::connection
    {
    public:
      connection (database&amp;);
      connection (database&amp;, PGconn*);

      PGconn*
      handle ();
    };

    typedef details::shared_ptr&lt;connection> connection_ptr;
  }
}
  </pre>

  <p>For more information on the <code>odb::connection</code> interface,
     refer to <a href="#3.5">Section 3.5, "Connections"</a>. The first
     overloaded <code>pgsql::connection</code> constructor establishes a
     new PostgreSQL connection. The second constructor allows us to create
     a <code>connection</code> instance by providing an already connected
     native PostgreSQL handle. Note that the <code>connection</code>
     instance assumes ownership of this handle. The <code>handle()</code>
     accessor returns the PostgreSQL handle corresponding to the connection.</p>

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

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

      virtual connection_ptr
      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::pgsql::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 PostgreSQL ODB runtime are
     <code>new_connection_factory</code> and
     <code>connection_pool_factory</code>. You will need to include
     the <code>&lt;odb/pgsql/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>new_connection_factory</code> class has the following
     interface:</p>

  <pre class="c++">
namespace odb
{
  namespace pgsql
  {
    class new_connection_factory: public connection_factory
    {
    public:
      new_connection_factory ();
    };
};
  </pre>

  <p>The <code>connection_pool_factory</code> class implements a
     connection pool. It has the following interface:</p>

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

    protected:
      class pooled_connection: public connection
      {
      public:
        pooled_connection (database_type&amp;);
        pooled_connection (database_type&amp;, PGconn*);
      };

      typedef details::shared_ptr&lt;pooled_connection> pooled_connection_ptr;

      virtual pooled_connection_ptr
      create ();
    };
};
  </pre>

  <p>The <code>max_connections</code> argument in the
     <code>connection_pool_factory</code> constructor 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 caller is blocked until
     a connection becomes available.</p>

  <p>When a connection is released, the pool factory first checks
     if there are blocked callers waiting for a connection. If so, then
     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 <code>min_connections</code>
     and there are no callers waiting for a new connection,
     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>The <code>create()</code> virtual function is called whenever the
     pool needs to create a new connection. By deriving from the
     <code>connection_pool_factory</code> class and overriding this
     function we can implement custom connection establishment
     and configuration.</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 <code>0</code>. 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/pgsql/database.hxx>
#include &lt;odb/pgsql/connection-factory.hxx>

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

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

  <h2><a name="13.4">13.4 PostgreSQL Exceptions</a></h2>

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

  <pre class="c++">
namespace odb
{
  namespace pgsql
  {
    class database_exception: odb::database_exception
    {
    public:
      const std::string&amp;
      message () const;

      const std::string&amp;
      sqlstate () 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/pgsql/exceptions.hxx></code>
     header file to make these exceptions available in your application.</p>

  <p>The <code>odb::pgsql::database_exception</code> is thrown if
     a PostgreSQL database operation fails. The PostgreSQL-specific error
     information is accessible via the <code>message()</code> and
     <code>sqlstate()</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::pgsql::cli_exception</code> is thrown by the
     command line parsing constructor of the <code>odb::pgsql::database</code>
     class if the PostgreSQL option values are missing or invalid. The
     <code>what()</code> function returns a human-readable description
     of an error.</p>

  <h2><a name="13.5">13.5 PostgreSQL Limitations</a></h2>

  <p>The following sections describe PostgreSQL-specific limitations imposed
     by the current PostgreSQL and ODB runtime versions.</p>

  <h3><a name="13.5.1">13.5.1 Query Result Caching</a></h3>

  <p>The PostgreSQL ODB runtime implementation will always return a
     cached query result (<a href="#4.4">Section 4.4, "Query Result"</a>)
     even when explicitly requested not to. This is a limitation of the
     PostgreSQL client library (<code>libpq</code>) which does not
     support uncached (streaming) query results.</p>

  <h3><a name="13.5.2">13.5.2 Foreign Key Constraints</a></h3>

  <p>ODB relies on standard SQL behavior which requires that
     foreign key constraints checking is deferred until the
     transaction is committed. Default PostgreSQL behavior is
     to check such constraints immediately. As a result, when
     used with ODB, a custom database schema that defines foreign
     key constraints must declare such constraints as
     <code>INITIALLY DEFERRED</code>, as shown in the following example.
     Schemas generated by the ODB compiler meet this requirement
     automatically.</p>

  <pre class="sql">
CREATE TABLE Employee (
  ...
  employer BIGINT REFERENCES Employer (name) INITIALLY DEFERRED);
  </pre>

  <h3><a name="13.5.3">13.5.3 Date-Time Format</a></h3>

  <p>ODB expects the PostgreSQL server to use integers as a binary
     format for the date-time types, which is the default for most
     PostgreSQL configurations. When creating a connection, ODB
     examines the <code>integer_datetimes</code> PostgreSQL server
     parameter and if it is <code>false</code>,
     <code>odb::pgsql::database_exception</code> is thrown. You may
     check the value of this parameter for your server by executing
     the following SQL query:</p>

  <pre class="sql">
SHOW integer_datetimes
  </pre>

  <h3><a name="13.5.4">13.5.4 Timezones</a></h3>

  <p>ODB does not currently support the PostgreSQL date-time types
     with timezone information.</p>

  <h3><a name="13.5.5">13.5.5 <code>NUMERIC</code> Type Support</a></h3>

  <p>Support for the PostgreSQL <code>NUMERIC</code> type is limited
     to providing a binary buffer containing the binary representation
     of the value. For more information on the binary format used to
     store <code>NUMERIC</code> values refer to the PostgreSQL
     documentation.</p>

  <!-- PART -->


  <hr class="page-break"/>
  <h1><a name="III">PART III&nbsp;&nbsp;
      <span style="font-weight: normal;">PROFILES</span></a></h1>

  <p>Part III covers the integration of ODB with popular C++ frameworks
     and libraries. It consists of the following chapters.</p>

  <table class="toc">
    <tr><th>14</th><td><a href="#14">Profiles Introduction</a></td></tr>
    <tr><th>15</th><td><a href="#15">Boost Profile</a></td></tr>
    <tr><th>16</th><td><a href="#16">Qt Profile</a></td></tr>
  </table>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="14">14 Profiles Introduction</a></h1>

  <p>ODB profiles are a generic mechanism for integrating ODB with
     widely-used C++ frameworks and libraries. A profile provides glue
     code which allows you to seamlessly persist various components, such
     as smart pointers, containers, and value types found in these
     frameworks or libraries. The code necessary to implement a profile
     is packaged into the so called profile library. For example, the
     Boost profile implementation is provided by the <code>libodb-boost</code>
     profile library.</p>

  <p>Besides linking the profile library to our application, it is also
     necessary to let the ODB compiler know which profiles we
     are using. This is accomplished with the <code>--profile</code>
     (or <code>-p</code> alias) option. For example:</p>

  <pre class="terminal">
odb --profile boost ...
  </pre>

  <p>Some profiles, especially those covering frameworks or libraries that
     consist of multiple sub-libraries, provide sub-profiles that allow you
     to pick and choose which components you would like to use in your
     application. For example, the <code>boost</code> profile contains
     the <code>boost/data-time</code> sub-profile. If we are only
     interested in the <code>date_time</code> types, then we can
     pass <code>boost/data-time</code> instead of <code>boost</code>
     to the <code>--profile</code> option, for example:</p>

  <pre class="terminal">
odb --profile boost/date-time ...
  </pre>

  <p>To summarize, you will need to perform the following steps in order
     to make use of a profile in your application:</p>

  <ol>
    <li>ODB compiler: if necessary, specify the path to the profile library
        headers (<code>-I</code> option).</li>
    <li>ODB compiler: specify the profile you would like to use with
        the <code>--profile</code> option.</li>
    <li>C++ compiler: if necessary, specify the path to the profile library
        headers (normally <code>-I</code> option).</li>
    <li>Linker: link the profile library to the application.</li>
  </ol>

  <p>The remaining chapters in this part of the manual describe the
     standard profiles provided by ODB.</p>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="15">15 Boost Profile</a></h1>

  <p>The ODB profile implementation for Boost is provided by the
     <code>libodb-boost</code> library and consists of multiple sub-profiles
     corresponding to the individual Boost libraries. To enable all the
     available Boost sub-profiles, pass <code>boost</code> as the profile
     name to the <code>--profile</code> ODB compiler option. Alternatively,
     you can enable only specific sub-profiles by passing individual
     sub-profile names to <code>--profile</code>. The following sections in
     this chapter discuss each Boost sub-profile in detail. The
     <code>boost</code> example in the <code>odb-examples</code>
     package shows how to enable and use the Boost profile.</p>

  <p>Some sub-profiles may throw exceptions to indicate error conditions,
     such as the inability to store a specific value in a particular database
     system. All such exceptions derive from the
     <code>odb::boost::exception</code> class which in turn derives from
     the root of the ODB exception hierarchy, class <code>odb::exception</code>
     (<a href="#3.12">Section 3.12, "ODB Exceptions"</a>). The
     <code>odb::boost::exception</code> class is defined in the
     <code>&lt;odb/boost/exception.hxx></code> header file and has the
     same interface as <code>odb::exception</code>. The concrete exceptions
     that can be thrown by the Boost sub-profiles are described in the
     following sections.</p>

  <h2><a name="15.1">15.1 Smart Pointers Library</a></h2>

  <p>The <code>smart-ptr</code> sub-profile provides persistence
     support for a subset of smart pointers from the Boost
     <code>smart_ptr</code> library. To enable only this profile,
     pass <code>boost/smart-ptr</code> to the <code>--profile</code>
     ODB compiler option.</p>

  <p>The currently supported smart pointers are
     <code>boost::shared_ptr</code> and <code>boost::weak_ptr</code>. For
     more information on using smart pointers as pointers to objects,
     refer to <a href="#3.2">Section 3.2, "Object Pointers"</a> and
     <a href="#6">Chapter 6, "Relationships"</a>. For more information
     on using smart pointers as pointers to values, refer to
     <a href="#7.3">Section 7.3, "Pointers and <code>NULL</code> Value
     Semantics"</a>. When used as a pointer to a value, only
     <code>boost::shared_ptr</code> is supported. For example:</p>

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

  #pragma db null
  boost::shared_ptr&lt;std::string> middle_name_;
};
  </pre>

  <p>To provide finer grained control over object relationship loading,
     the <code>smart-ptr</code> sub-profile also provides the lazy
     counterparts for the above  pointers: <code>odb::boost::lazy_shared_ptr</code> and
     <code>odb::boost::lazy_weak_ptr</code>. You will need to include the
     <code>&lt;odb/boost/lazy-ptr.hxx></code> header file to make the lazy
     variants available in your application. For the description of the lazy
     pointer interface and semantics refer to <a href="#6.3">Section 6.3,
     "Lazy Pointers"</a>. The following example shows how we can use these
     smart pointers to establish a relationship between persistent objects.</p>

<pre class="c++">
class employee;

#pragma db object
class position
{
  ...

  #pragma db inverse(position_)
  odb::boost::lazy_weak_ptr&lt;employee> employee_;
};

#pragma db object
class employee
{
  ...

  #pragma db not_null
  boost::shared_ptr&lt;position> position_;
};
</pre>

  <p>Besides providing persistence support for the above smart pointers,
     the <code>smart-ptr</code> sub-profile also changes the default object
     pointer (<a href="#3.2">Section 3.2, "Object Pointers"</a>)
     to <code>boost::shared_ptr</code>.  In particular, this means that
     database functions that return dynamically allocated objects will return
     them as <code>boost::shared_ptr</code> pointers.  To override this
     behavior, add the <code>--default-pointer</code> option specifying the
     alternative object pointer after the <code>--profile</code> option.</p>

  <h2><a name="15.2">15.2 Unordered Containers Library</a></h2>

  <p>The <code>unordered</code> sub-profile provides persistence support for
     the containers from the Boost <code>unordered</code> library. To enable
     only this profile, pass <code>boost/unordered</code> to
     the <code>--profile</code> ODB compiler option.</p>

  <p>The supported containers are <code>boost::unordered_set</code>,
     <code>boost::unordered_map</code>, <code>boost::unordered_multiset</code>,
     and <code>boost::unordered_multimap</code>. For more information on using
     the set and multiset containers with ODB refer to <a href="#5.2">Section
     5.2, "Set and Multiset Containers"</a>. For more information on using the
     map and multimap containers with ODB refer to <a href="#5.3"> Section
     5.3, "Map and Multimap Containers"</a>. The following example shows how
     the <code>unordered_set</code> container may be used within a persistent
     object.</p>

<pre class="c++">
#pragma db object
class person
{
  ...
  boost::unordered_set&lt;std::string&gt; emails_;
};
</pre>

  <h2><a name="15.3">15.3 Optional Library</a></h2>

  <p>The <code>optional</code> sub-profile provides persistence support for
     the <code>boost::optional</code> container from the Boost
     <code>optional</code> library. To enable only this profile, pass
     <code>boost/optional</code> to the <code>--profile</code> ODB compiler
     option.</p>

  <p>In a relational database <code>boost::optional</code> is mapped to
     a column that can have a <code>NULL</code> value. Similar to
     <code>odb::nullable</code> (<a href="#7.3">Section 7.3, "Pointers and
     <code>NULL</code> Value Semantics"</a>), it can be used to add the
     <code>NULL</code> semantics to existing C++ types. For example:</p>

  <pre class="c++">
#include &lt;boost/optional.hpp>

#pragma db object
class person
{
  ...

  std::string first_;                    // TEXT NOT NULL
  boost::optional&lt;std::string> middle_;  // TEXT NULL
  std::string last_;                     // TEXT NOT NULL
};
  </pre>

  <p>Note also that similar to <code>odb::nullable</code>, when
     this profile is used, the <code>NULL</code> values are automatically
     enabled for data members of the  <code>boost::optional</code> type.</p>

  <h2><a name="15.4">15.4 Date Time Library</a></h2>

  <p>The <code>date-time</code> sub-profile provides persistence support for a
     subset of types from the Boost <code>date_time</code> library. It is
     further subdivided into two sub-profiles, <code>gregorian</code>
     and <code>posix_time</code>. The <code>gregorian</code> sub-profile
     provides support for types from the <code>boost::gregorian</code>
     namespace, while the <code>posix-time</code> sub-profile provides support
     for types from the <code>boost::posix_time</code> namespace. To enable
     the entire <code>date-time</code> sub-profile, pass
     <code>boost/date-time</code> to the <code>--profile</code> ODB compiler
     option. To enable only the <code>gregorian</code> sub-profile, pass
     <code>boost/date-time/gregorian</code>, and to enable only the
     <code>posix-time</code> sub-profile, pass
     <code>boost/date-time/posix-time</code>.</p>

  <p>The only type that the <code>gregorian</code> sub-profile currently
     supports is <code>gregorian::date</code>. The types currently supported
     by the <code>posix-time</code> sub-profile are
     <code>posix_time::ptime</code> and
     <code>posix_time::time_duration</code>. The manner in which these types
     are persisted is database system dependent and is discussed in the
     sub-sections that follow. The example below shows how
     <code>gregorian::date</code> may be used within a persistent object.</p>

<pre class="c++">
#pragma db object
class person
{
  ...
  boost::gregorian::date date_of_birth_;
};
</pre>

  <p>The concrete exceptions that can be thrown by the <code>date-time</code>
     sub-profile implementation are presented below.</p>


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

      struct value_out_of_range: odb::boost::exception
      {
        virtual const char*
        what () const throw ();
      };
    }
  }
}
</pre>

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

  <p>The <code>special_value</code> exception is thrown if an attempt is made
     to store a Boost date-time special value that cannot be represented in
     the target database. The <code>value_out_of_range</code> exception is
     thrown if an attempt is made to store a date-time value that is out of
     the target database range. The specific conditions under which these
     exceptions are thrown are database system dependent and are discussed in
     more detail in the following sub-sections.</p>

  <h3><a name="15.4.1">15.4.1 MySQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Boost <code>date_time</code> types and the MySQL database
     types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Boost <code>date_time</code> Type</th>
      <th>MySQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>gregorian::date</code></td>
      <td><code>DATE</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::ptime</code></td>
      <td><code>DATETIME</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::time_duration</code></td>
      <td><code>TIME</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>The Boost special value <code>date_time::not_a_date_time</code> is stored
     as a <code>NULL</code> value in a MySQL database.</p>

  <p>The <code>posix-time</code> sub-profile implementation also provides
     support for mapping <code>posix_time::ptime</code> to the
     <code>TIMESTAMP</code> MySQL type. However, this mapping has to be
     explicitly requested using the <code>db&nbsp;type</code> pragma
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>), as shown in
     the following example:</p>

<pre class="c++">
#pragma db object
class person
{
  ...
  #pragma db type("TIMESTAMP") not_null
  boost::posix_time::ptime updated_;
};
</pre>

  <p>Some valid Boost date-time values cannot be stored in a MySQL database.
     An attempt to persist any Boost date-time special value other than
     <code>date_time::not_a_date_time</code> will result in the
     <code>special_value</code> exception. An attempt to persist a Boost
     date-time value that is out of the MySQL type range will result in
     the <code>out_of_range</code> exception.  Refer to the MySQL
     documentation for more information on the MySQL data type ranges.</p>

  <h3><a name="15.4.2">15.4.2 SQLite Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Boost <code>date_time</code> types and the SQLite database
     types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Boost <code>date_time</code> Type</th>
      <th>SQLite Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>gregorian::date</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::ptime</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::time_duration</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>The Boost special value <code>date_time::not_a_date_time</code> is stored
     as a <code>NULL</code> value in an SQLite database.</p>

  <p>The <code>date-time</code> sub-profile implementation also provides
     support for mapping <code>gregorian::date</code> and
     <code>posix_time::ptime</code> to the <code>INTEGER</code> SQLite type,
     with the integer value representing the UNIX time. Similarly, an
     alternative mapping for <code>posix_time::time_duration</code> to the
     <code>INTEGER</code> type represents the duration as a number of
     seconds. These mappings have to be explicitly requested using the
     <code>db&nbsp;type</code> pragma (<a href="#10.3.3">Section 10.3.3,
     "<code>type</code>"</a>), as shown in the following example:</p>

<pre class="c++">
#pragma db object
class person
{
  ...
  #pragma db type("INTEGER")
  boost::gregorian::date born_;
};
</pre>

  <!--

  <p>The Boost UNIX time interface does not support 64 bit time arithmetic.
     As a result, the UNIX time representations of <code>gregorian::date</code>
     and <code>posix_time::ptime</code> are restricted to the 32 bit range.
     The minimum and maximum date representable by
     <code>gregorian::date</code> is 1901-12-14 and 2038-01-19 respectively,
     while the minimum and maximum date-time representable by
     <code>posix_time::ptime</code> is 1901-12-13&nbsp;20:45:54 GMT and
     2038-01-19&nbsp;03:14:07&nbsp;GMT respectively. Persisting and loading
     of values outside of these ranges will result in undefined behavior.</p>

  -->

  <p>Some valid Boost date-time values cannot be stored in an SQLite database.
     An attempt to persist any Boost date-time special value other than
     <code>date_time::not_a_date_time</code> will result in the
     <code>special_value</code> exception. An attempt to persist a negative
     <code>posix_time::time_duration</code> value as SQLite <code>TEXT</code>
     will result in the <code>out_of_range</code> exception.</p>


  <h3><a name="15.4.3">15.4.3 PostgreSQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Boost <code>date_time</code> types and the PostgreSQL database
     types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Boost <code>date_time</code> Type</th>
      <th>PostgreSQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>gregorian::date</code></td>
      <td><code>DATE</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::ptime</code></td>
      <td><code>TIMESTAMP</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>posix_time::time_duration</code></td>
      <td><code>TIME</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>The Boost special value <code>date_time::not_a_date_time</code> is stored
     as a <code>NULL</code> value in a PostgreSQL database.
     <code>posix_time::ptime</code> values representing the special values
     <code>date_time::pos_infin</code> and <code>date_time::neg_infin</code>
     are stored as the special PostgreSQL TIMESTAMP values
     <code>infinity</code> and <code>-infinity</code>, respectively.</p>

  <p>Some valid Boost date-time values cannot be stored in a PostgreSQL
     database. The PostgreSQL TIME type represents a clock time, and can
     therefore only store positive durations with a total length of time less
     than 24 hours. An attempt to persist a
     <code>posix_time::time_duration</code> value outside of this range will
     result in the <code>value_out_of_range</code> exception. An attempt to
     persist a <code>posix_time::time_duration</code> value representing any
     special value other than <code>date_time::not_a_date_time</code>  will
     result in the <code>special_value</code> exception.</p>


  <!-- CHAPTER -->


  <hr class="page-break"/>
  <h1><a name="16">16 Qt Profile</a></h1>

  <p>The ODB profile implementation for Qt is provided by the
     <code>libodb-qt</code> library and consists of multiple sub-profiles
     corresponding to the common type groups within Qt. Currently,
     only types from the <code>QtCore</code> module are supported. To
     enable all the available Qt sub-profiles, pass <code>qt</code> as the
     profile name to the <code>--profile</code> ODB compiler  option.
     Alternatively, you can enable only specific sub-profiles by passing
     individual sub-profile names to <code>--profile</code>. The following
     sections in this chapter discuss each Qt sub-profile in detail. The
     <code>qt</code> example in the <code>odb-examples</code>
     package shows how to enable and use the Qt profile.</p>

  <p>Some sub-profiles may throw exceptions to indicate error conditions,
     such as the inability to store a specific value in a particular database
     system. All such exceptions derive from the
     <code>odb::qt::exception</code> class which in turn derives from
     the root of the ODB exception hierarchy, class <code>odb::exception</code>
     (<a href="#3.12">Section 3.12, "ODB Exceptions"</a>). The
     <code>odb::qt::exception</code> class is defined in the
     <code>&lt;odb/qt/exception.hxx></code> header file and has the
     same interface as <code>odb::exception</code>. The concrete exceptions
     that can be thrown by the Qt sub-profiles are described in the
     following sections.</p>

  <h2><a name="16.1">16.1 Basic Types</a></h2>

  <p>The <code>basic</code> sub-profile provides persistence support for basic
     types defined by Qt. To enable only this profile, pass
     <code>qt/basic</code> to the <code>--profile</code> ODB compiler
     option.</p>

  <p>The currently supported basic types are <code>QString</code> and
    <code>QByteArray</code>. The manner in which these types
     are persisted is database system dependent and is discussed in the
     sub-sections that follow. The example below shows how
     <code>QString</code> may be used within a persistent object.</p>

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

  <h3><a name="16.1.1">16.1.1 MySQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported basic Qt types and the MySQL database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Type</th>
      <th>MySQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QString</code></td>
      <td><code>TEXT/VARCHAR(255)</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QByteArray</code></td>
      <td><code>BLOB</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QString</code> and <code>QByteArray</code> types
     are stored as a NULL value if their <code>isNull()</code> member
     function returns <code>true</code>.</p>

  <p>Note also that the <code>QString</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>QString</code> is mapped
     to the <code>VARCHAR(255)</code> MySQL type. Otherwise,
     it is mapped to <code>TEXT</code>.</p>


  <h3><a name="16.1.2">16.1.2 SQLite Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported basic Qt types and the SQLite database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Type</th>
      <th>SQLite Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QString</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QByteArray</code></td>
      <td><code>BLOB</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QString</code> and <code>QByteArray</code> types
     are stored as a NULL value if their <code>isNull()</code> member
     function returns <code>true</code>.</p>

  <h3><a name="16.1.3">16.1.3 PostgreSQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported basic Qt types and the PostgreSQL database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Type</th>
      <th>PostgreSQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QString</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QByteArray</code></td>
      <td><code>BYTEA</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QString</code> and <code>QByteArray</code> types
     are stored as a NULL value if their <code>isNull()</code> member
     function returns <code>true</code>.</p>

  <h2><a name="16.2">16.2 Smart Pointers</a></h2>

  <p>The <code>smart-ptr</code> sub-profile provides persistence support the
     Qt smart pointers. To enable only this profile, pass
     <code>qt/smart-ptr</code> to the <code>--profile</code> ODB compiler
     option.</p>

  <p>The currently supported smart pointers are
     <code>QSharedPointer</code> and <code>QWeakPointer</code>.
     For more information on using smart pointers as pointers to objects,
     refer to <a href="#3.2">Section 3.2, "Object Pointers"</a> and
     <a href="#6">Chapter 6, "Relationships"</a>. For more information
     on using smart pointers as pointers to values, refer to
     <a href="#7.3">Section 7.3, "Pointers and <code>NULL</code> Value
     Semantics"</a>. When used as a pointer to a value, only
     <code>QSharedPointer</code> is supported. For example:</p>

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

  #pragma db null
  QSharedPointer&lt;QString> middle_name_;
};
  </pre>

  <p>To provide finer grained control over object relationship loading,
     the <code>smart-ptr</code> sub-profile also provides the lazy
     counterparts for the above pointers: <code>QLazySharedPointer</code>
     and <code>QLazyWeakPointer</code>. You will need to include the
     <code>&lt;odb/qt/lazy-ptr.hxx></code> header file to make the lazy
     variants available in your application. For the description of the lazy
     pointer interface and semantics refer to <a href="#6.3">Section 6.3,
     "Lazy Pointers"</a>. The following example shows how we can use these
     smart pointers to establish a relationship between persistent objects.</p>

<pre class="c++">
class Employee;

#pragma db object
class Position
{
  ...

  #pragma db inverse(position_)
  QLazyWeakPointer&lt;Employee> employee_;
};

#pragma db object
class Employee
{
  ...

  #pragma db not_null
  QSharedPointer&lt;Position> position_;
};
</pre>

  <p>Besides providing persistence support for the above smart pointers,
     the <code>smart-ptr</code> sub-profile also changes the default object
     pointer (<a href="#3.2">Section 3.2, "Object Pointers"</a>)
     to <code>QSharedPointer</code>.  In particular, this means that
     database functions that return dynamically allocated objects will return
     them as <code>QSharedPointer</code> pointers.  To override this
     behavior, add the <code>--default-pointer</code> option specifying the
     alternative object pointer after the <code>--profile</code> option.</p>

  <h2><a name="16.3">16.3 Containers Library</a></h2>

  <p>The <code>container</code> sub-profile provides persistence support for
     Qt containers. To enable only this profile, pass
     <code>qt/containers</code> to the <code>--profile</code> ODB compiler
     option.</p>

  <p>The currently supported ordered containers are <code>QVector</code>,
     <code>QList</code>, and <code>QLinkedList</code>. Supported map
     containers are <code>QMap</code>, <code>QMultiMap</code>,
     <code>QHash</code>, and <code>QMultiHash</code>. The supported set
     container is <code>QSet</code>. For more information on using
     containers with ODB refer to <a href="#5">Chapter 5, "Containers"</a>.
     The following example shows how the <code>QSet</code> container may
     be used within a persistent object.</p>

<pre class="c++">
#pragma db object
class Person
{
  ...
  QSet&lt;QString> emails_;
};
</pre>

  <h2><a name="16.4">16.4 Date Time Types</a></h2>

  <p>The <code>date-time</code> sub-profile provides persistence support for
     the Qt date-time types. To enable only this profile, pass
     <code>qt/date-time</code> to the <code>--profile</code> ODB compiler
     option.</p>

  <p>The currently supported date-time types are <code>QDate</code>,
     <code>QTime</code>, and <code>QDateTime</code>. The manner in which
     these types are persisted is database system dependent and is
     discussed in the sub-sections that follow. The example below shows how
     <code>QDate</code> may be used within a persistent object.</p>

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

  <p>The single concrete exception that can be thrown by the
     <code>date-time</code> sub-profile implementation is presented below.</p>


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

  <p>You will need to include the
     <code>&lt;odb/qt/date-time/exceptions.hxx&gt;</code> header file to
     make this exception available in your application.</p>

  <p>The <code>value_out_of_range</code> exception is thrown if an attempt
     is made to store a date-time value that is out of the target database
     range. The specific conditions under which it is thrown is database
     system dependent and is discussed in more detail in the
     following sub-sections.</p>

  <h3><a name="16.4.1">16.4.1 MySQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Qt date-time types and the MySQL database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Date Time Type</th>
      <th>MySQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QDate</code></td>
      <td><code>DATE</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QTime</code></td>
      <td><code>TIME</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QDateTime</code></td>
      <td><code>DATETIME</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QDate</code>, <code>QTime</code>, and
     <code>QDateTime</code> types are stored as a NULL value if their
     <code>isNull()</code> member function returns true.</p>

  <p>The <code>date-time</code> sub-profile implementation also provides
     support for mapping <code>QDateTime</code> to the <code>TIMESTAMP</code>
     MySQL type. However, this mapping has to be explicitly requested using
     the <code>db&nbsp;type</code> pragma
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>), as shown in
     the following example:</p>

<pre class="c++">
#pragma db object
class Person
{
  ...
  #pragma db type("TIMESTAMP") not_null
  QDateTime updated_;
};
</pre>

  <p>Some valid Qt date-time values cannot be stored in a MySQL database.  An
     attempt to persist a Qt date-time value that is out of the MySQL type
     range will result in the <code>out_of_range</code> exception.  Refer to
     the MySQL documentation for more information on the MySQL data type
     ranges.</p>

  <h3><a name="16.4.2">16.4.2 SQLite Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Qt date-time types and the SQLite database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Date Time Type</th>
      <th>SQLite Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QDate</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QTime</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QDateTime</code></td>
      <td><code>TEXT</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QDate</code>, <code>QTime</code>, and
     <code>QDateTime</code> types are stored as a NULL value if their
     <code>isNull()</code> member function returns true.</p>

  <p>The <code>date-time</code> sub-profile implementation also provides
     support for mapping <code>QDate</code> and <code>QDateTime</code> to the
     SQLite <code>INTEGER</code> type, with the integer value representing the
     UNIX time. Similarly, an alternative mapping for <code>QTime</code> to
     the <code>INTEGER</code> type represents a clock time as the number of
     seconds since midnight. These mappings have to be explicitly requested
     using the <code>db&nbsp;type</code> pragma
     (<a href="#10.3.3">Section 10.3.3, "<code>type</code>"</a>), as shown
     in the following example:</p>

<pre class="c++">
#pragma db object
class Person
{
  ...
  #pragma db type("INTEGER")
  QDate born_;
};
</pre>

  <p>Some valid Qt date-time values cannot be stored in an SQLite database.
     An attempt to persist any Qt date-time value representing a negative UNIX
     time (any point in time prior to the 1970-01-01&nbsp;00:00:00 UNIX time
     epoch) as an SQLite <code>INTEGER</code> will result in the
     <code>out_of_range</code> exception.</p>

  <h3><a name="16.4.3">16.4.3 PostgreSQL Database Type Mapping</a></h3>

  <p>The following table summarizes the default mapping between the currently
     supported Qt date-time types and the PostgreSQL database types.</p>

  <!-- border="1" is necessary for html2ps -->
  <table id="mapping" border="1">
    <tr>
      <th>Qt Date Time Type</th>
      <th>PostgreSQL Type</th>
      <th>Default <code>NULL</code> Semantics</th>
    </tr>

    <tr>
      <td><code>QDate</code></td>
      <td><code>DATE</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QTime</code></td>
      <td><code>TIME</code></td>
      <td><code>NULL</code></td>
    </tr>

    <tr>
      <td><code>QDateTime</code></td>
      <td><code>TIMESTAMP</code></td>
      <td><code>NULL</code></td>
    </tr>
  </table>

  <p>Instances of the <code>QDate</code>, <code>QTime</code>, and
     <code>QDateTime</code> types are stored as a NULL value if their
     <code>isNull()</code> member function returns true.</p>

  <p>Some valid Qt date-time values cannot be stored in a PostgreSQL database.
     An attempt to persist a <code>QDateTime</code> value representing a
     negative UNIX time (any point in time prior to the
     1970-01-01&nbsp;00:00:00 UNIX time epoch) as a PostgreSQL
     <code>TIMESTAMP</code> will result in the <code>out_of_range</code>
     exception.</p>

  </div>
</div>

</body>
</html>