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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.2.7" />
<style type="text/css">
/* Debug borders */
p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
/*
border: 1px solid red;
*/
}
body {
margin: 1em 5% 1em 5%;
}
a {
color: blue;
text-decoration: underline;
}
a:visited {
color: fuchsia;
}
em {
font-style: italic;
color: navy;
}
strong {
font-weight: bold;
color: #083194;
}
tt {
color: navy;
}
h1, h2, h3, h4, h5, h6 {
color: #527bbd;
font-family: sans-serif;
margin-top: 1.2em;
margin-bottom: 0.5em;
line-height: 1.3;
}
h1, h2, h3 {
border-bottom: 2px solid silver;
}
h2 {
padding-top: 0.5em;
}
h3 {
float: left;
}
h3 + * {
clear: left;
}
div.sectionbody {
font-family: serif;
margin-left: 0;
}
hr {
border: 1px solid silver;
}
p {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
ul, ol, li > p {
margin-top: 0;
}
pre {
padding: 0;
margin: 0;
}
span#author {
color: #527bbd;
font-family: sans-serif;
font-weight: bold;
font-size: 1.1em;
}
span#email {
}
span#revision {
font-family: sans-serif;
}
div#footer {
font-family: sans-serif;
font-size: small;
border-top: 2px solid silver;
padding-top: 0.5em;
margin-top: 4.0em;
}
div#footer-text {
float: left;
padding-bottom: 0.5em;
}
div#footer-badges {
float: right;
padding-bottom: 0.5em;
}
div#preamble,
div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
margin-right: 10%;
margin-top: 1.5em;
margin-bottom: 1.5em;
}
div.admonitionblock {
margin-top: 2.5em;
margin-bottom: 2.5em;
}
div.content { /* Block element content. */
padding: 0;
}
/* Block element titles. */
div.title, caption.title {
color: #527bbd;
font-family: sans-serif;
font-weight: bold;
text-align: left;
margin-top: 1.0em;
margin-bottom: 0.5em;
}
div.title + * {
margin-top: 0;
}
td div.title:first-child {
margin-top: 0.0em;
}
div.content div.title:first-child {
margin-top: 0.0em;
}
div.content + div.title {
margin-top: 0.0em;
}
div.sidebarblock > div.content {
background: #ffffee;
border: 1px solid silver;
padding: 0.5em;
}
div.listingblock {
margin-right: 0%;
}
div.listingblock > div.content {
border: 1px solid silver;
background: #f4f4f4;
padding: 0.5em;
}
div.quoteblock {
padding-left: 2.0em;
}
div.quoteblock > div.attribution {
padding-top: 0.5em;
text-align: right;
}
div.verseblock {
padding-left: 2.0em;
}
div.verseblock > div.content {
white-space: pre;
}
div.verseblock > div.attribution {
padding-top: 0.75em;
text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
text-align: left;
}
div.admonitionblock .icon {
vertical-align: top;
font-size: 1.1em;
font-weight: bold;
text-decoration: underline;
color: #527bbd;
padding-right: 0.5em;
}
div.admonitionblock td.content {
padding-left: 0.5em;
border-left: 2px solid silver;
}
div.exampleblock > div.content {
border-left: 2px solid silver;
padding: 0.5em;
}
div.imageblock div.content { padding-left: 0; }
div.imageblock img { border: 1px solid silver; }
span.image img { border-style: none; }
dl {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
dt {
margin-top: 0.5em;
margin-bottom: 0;
font-style: normal;
}
dd > *:first-child {
margin-top: 0.1em;
}
ul, ol {
list-style-position: outside;
}
div.olist > ol {
list-style-type: decimal;
}
div.olist2 > ol {
list-style-type: lower-alpha;
}
div.tableblock > table {
border: 3px solid #527bbd;
}
thead {
font-family: sans-serif;
font-weight: bold;
}
tfoot {
font-weight: bold;
}
div.hlist {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
div.hlist td {
padding-bottom: 15px;
}
td.hlist1 {
vertical-align: top;
font-style: normal;
padding-right: 0.8em;
}
td.hlist2 {
vertical-align: top;
}
@media print {
div#footer-badges { display: none; }
}
div#toctitle {
color: #527bbd;
font-family: sans-serif;
font-size: 1.1em;
font-weight: bold;
margin-top: 1.0em;
margin-bottom: 0.1em;
}
div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
margin-top: 0;
margin-bottom: 0;
}
div.toclevel2 {
margin-left: 2em;
font-size: 0.9em;
}
div.toclevel3 {
margin-left: 4em;
font-size: 0.9em;
}
div.toclevel4 {
margin-left: 6em;
font-size: 0.9em;
}
/* Workarounds for IE6's broken and incomplete CSS2. */
div.sidebar-content {
background: #ffffee;
border: 1px solid silver;
padding: 0.5em;
}
div.sidebar-title, div.image-title {
color: #527bbd;
font-family: sans-serif;
font-weight: bold;
margin-top: 0.0em;
margin-bottom: 0.5em;
}
div.listingblock div.content {
border: 1px solid silver;
background: #f4f4f4;
padding: 0.5em;
}
div.quoteblock-attribution {
padding-top: 0.5em;
text-align: right;
}
div.verseblock-content {
white-space: pre;
}
div.verseblock-attribution {
padding-top: 0.75em;
text-align: left;
}
div.exampleblock-content {
border-left: 2px solid silver;
padding-left: 0.5em;
}
/* IE6 sets dynamically generated links as visited. */
div#toc a:visited { color: blue; }
/* Because IE6 child selector is broken. */
div.olist2 ol {
list-style-type: lower-alpha;
}
div.olist2 div.olist ol {
list-style-type: decimal;
}
</style>
<script type="text/javascript">
/*<+'])');
// Function that scans the DOM tree for header elements (the DOM2
// nodeIterator API would be a better technique but not supported by all
// browsers).
var iterate = function (el) {
for (var i = el.firstChild; i != null; i = i.nextSibling) {
if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
var mo = re.exec(i.tagName)
if (mo)
result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
iterate(i);
}
}
}
iterate(el);
return result;
}
// This function does the work. toclevels = 1..4.
function generateToc(toclevels) {
var toc = document.getElementById("toc");
var entries = tocEntries(document.getElementsByTagName("body")[0], toclevels);
for (var i = 0; i < entries.length; ++i) {
var entry = entries[i];
if (entry.element.id == "")
entry.element.id = "toc" + i;
var a = document.createElement("a");
a.href = "#" + entry.element.id;
a.appendChild(document.createTextNode(entry.text));
var div = document.createElement("div");
div.appendChild(a);
div.className = "toclevel" + entry.toclevel;
toc.appendChild(div);
}
if (entries.length == 0)
document.getElementById("header").removeChild(toc);
}
/*]]>*/
</script>
<title>Wt::Dbo Tutorial</title>
</head>
<body>
<div id="header">
<h1>Wt::Dbo Tutorial</h1>
<div id="toc">
<div id="toctitle">Table of Contents</div>
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="para"><p>Koen Deforche <<a href="mailto:koen@emweb.be">koen@emweb.be</a>></p></div>
</div>
</div>
<h2 id="_introduction">1. Introduction</h2>
<div class="sectionbody">
<div class="para"><p><tt>Wt::Dbo</tt> is a C++ ORM (Object-Relational-Mapping) library.</p></div>
<div class="para"><p>The library is distributed as part of <a href="http://www.webtoolkit.eu/wt">Wt</a>
for building database-driven web applications, but may be equally well
used independently from it.</p></div>
<div class="para"><p>The library provides a class-based view on database tables which keeps
an object-hiearchy of database object automatically synchronized with
a database by inserting, updating and deleting database records. C++
classes map to database tables, class fields to table columns, and
pointers and collections of pointers to database relations. An object
from a mapped class is called a <strong>database object</strong> (dbo). Query results
may be defined in terms of database objects, primitives, or tuples of
these.</p></div>
<div class="para"><p>A modern C++ approach is used to solving the mapping problem. Rather
than resorting to XML-based descriptions of how C++ classes and
fields should map onto tables and columns, or using obscure macros,
the mapping is defined entirely in C++ code.</p></div>
<div class="para"><p>In this tutorial, we will work our way through a blogging example,
similar to the one that is distributed with the library.</p></div>
</div>
<h2 id="_mapping_a_single_class">2. Mapping a single class</h2>
<div class="sectionbody">
<div class="para"><p>We will start off with using <tt>Wt::Dbo</tt> for mapping a single class <tt>User</tt>
to a corresponding table <tt>user</tt>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Warning</div>
</td>
<td class="content">
<div class="para"><p>In this tutorial and the examples, we alias the namespace <tt>Wt::Dbo</tt> to
<tt>dbo</tt>, and in our explanation we will refer to types and methods
available in that namespace directly.</p></div>
</td>
</tr></table>
</div>
<div class="para"><p>To build the following example, you need to link against the <tt>wtdbo</tt>
and <tt>wtdbosqlite3</tt> libraries.</p></div>
<div class="listingblock">
<div class="title">Example: Blog.h: Mapping a single class</div>
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><Wt/Dbo/Dbo></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><string></span>
<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> dbo <span style="color: #990000">=</span> Wt<span style="color: #990000">::</span>Dbo<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">User</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
<span style="font-weight: bold"><span style="color: #0000FF">enum</span></span> Role <span style="color: #FF0000">{</span>
Visitor <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">,</span>
Admin <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">,</span>
Alien <span style="color: #990000">=</span> <span style="color: #993399">42</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">string</span> name<span style="color: #990000">;</span>
std<span style="color: #990000">::</span><span style="color: #008080">string</span> password<span style="color: #990000">;</span>
<span style="color: #008080">Role</span> role<span style="color: #990000">;</span>
<span style="color: #009900">int</span> karma<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000"><</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">></span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">field</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> name<span style="color: #990000">,</span> <span style="color: #FF0000">"name"</span><span style="color: #990000">);</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">field</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> password<span style="color: #990000">,</span> <span style="color: #FF0000">"password"</span><span style="color: #990000">);</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">field</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> role<span style="color: #990000">,</span> <span style="color: #FF0000">"role"</span><span style="color: #990000">);</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">field</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> karma<span style="color: #990000">,</span> <span style="color: #FF0000">"karma"</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>This example shows how persistence support is defined for a C++
class. A template member method <tt>persist()</tt> is defined which serves as
a persistence definition for the class. For each member in the class,
a call to <tt><a href="../../reference/html/group__dbo.html#g7d270dd99aa29c13ebdc5e1c394ed8b1">Wt::Dbo::field()</a></tt> is used to map the field to a table
column name.</p></div>
<div class="para"><p>As you may see, standard C++ types such as <tt>int</tt>, <tt>std::string</tt> and
<tt>enum</tt> types are readily supported by the library. Support for other
types can be added by specializing
<tt><a href="../../reference/html/structWt_1_1Dbo_1_1sql__value__traits.html">Wt::Dbo::sql_value_traits<<em>T</em>></a></tt>.</p></div>
<div class="para"><p>The library defines a number of actions which will be applied to a
database object using its <tt>persist()</tt> method, which applies it in turn
to all its members. These actions will then read, update or insert
database objects, create the schema, or propagate transaction
outcomes.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
<div class="para"><p>For brevity, our example uses public members. There is no nothing that
prevents you to encapsulate your state in private members and provide
accessor methods. You may even define the persistence method in terms of
accessor methods by differentiating between read and write actions.</p></div>
</td>
</tr></table>
</div>
</div>
<h2 id="_a_first_session">3. A first session</h2>
<div class="sectionbody">
<div class="para"><p>Now that we have a mapping definition for our <tt>User</tt> class, we can
start a database session, create our schema (if necessary) and add a
user to the database.</p></div>
<div class="para"><p>Let us walk through the code for doing this.</p></div>
<div class="listingblock">
<div class="title">Example: A first session</div>
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Blog.h"</span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">test</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
<span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * Setup a session, would typically be done once at application startup.</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> */</span></span>
dbo<span style="color: #990000">::</span>backend<span style="color: #990000">::</span><span style="color: #008080">Sqlite3</span> <span style="font-weight: bold"><span style="color: #000000">sqlite3</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"blog.db"</span><span style="color: #990000">);</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">Session</span> session<span style="color: #990000">;</span>
session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setConnection</span></span><span style="color: #990000">(</span>sqlite3<span style="color: #990000">);</span>
<span style="color: #990000">...</span>
</tt></pre></div></div>
<div class="para"><p>The <tt><a href="../../reference/html/classWt_1_1Dbo_1_1Session.html">Session</a></tt> object is a long
living object that provides access to our database objects. You will
typically create a Session object during the entire lifetime of an
application session, and one per user. None of the
<tt><a href="../../reference/html/group__dbo.html">Wt::Dbo</a></tt> classes are thread safe, and session
objects are (currently) not shared between sessions.</p></div>
<div class="para"><p>The lack of thread-safety is not simply a consequence of laziness on
our part. It coincides with the promises made by transactional
integrity on the database: you will not want to see the changes made
by one session in another session while its transaction has not been
committed (Read-Committed transaction isolation level). It might make
sense however to implement a copy-on-write strategy in the future, to
allow sharing of the bulk of database objects between sessions.</p></div>
<div class="para"><p>The session is given a connection which it may use to communicate with
the database. A session will use a connection only during a
transaction, and thus does not really need a dedicated connection. It
therefore makes alot of sense to use a connection pool. Although
<tt>Wt::Dbo</tt> does not yet provide a connection pool implementation, support
for it has been foreseen. <tt>Wt::Dbo</tt> uses an abstraction layer for
database access, but currently only an SQLite3 backend has been
implemented.</p></div>
<div class="listingblock">
<div class="title">Example: (example continued)</div>
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt> <span style="color: #990000">...</span>
session<span style="color: #990000">.</span>mapClass<span style="color: #990000"><</span>User<span style="color: #990000">>(</span><span style="color: #FF0000">"user"</span><span style="color: #990000">);</span>
<span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * Try to create the schema (will fail if already exists).</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> */</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">try</span></span> <span style="color: #FF0000">{</span>
session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">createTables</span></span><span style="color: #990000">();</span>
<span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">catch</span></span> <span style="color: #990000">(...)</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">}</span>
<span style="color: #990000">...</span>
</tt></pre></div></div>
<div class="para"><p>Next, we use
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Session.html#5886d450c052ae0ee15ab3c91e439229">mapClass()</a></tt>
to register each database class with the session, indicating the
database table onto which the class must be mapped.</p></div>
<div class="para"><p>Certainly during development, but also for initial deployment, it is
convenient to let <tt>Wt::Dbo</tt> generate itself the database tables.</p></div>
<div class="para"><p>This generates the following SQL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>begin transaction
<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> user<span style="color: #990000">(</span>
id integer <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> autoincrement<span style="color: #990000">,</span>
version integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
name <span style="color: #009900">text</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
password <span style="color: #009900">text</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
role integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
karma integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span>
<span style="color: #990000">)</span>
commit transaction
</tt></pre></div></div>
<div class="para"><p>As you can see, next to the four columns that map to C++ fields,
Wt::Dbo adds another two columns: <tt>id</tt> and <tt>version</tt>. The id is a
surrogate primary key, and version is used for version-based
optimistic locking.</p></div>
<div class="para"><p>Finally, we can add a user to the database. All database operations
happen within a transaction.</p></div>
<div class="listingblock">
<div class="title">Example: (example continued)</div>
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt> <span style="color: #990000">...</span>
<span style="font-style: italic"><span style="color: #9A1900">/*</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> * A unit of work happens always within a transaction.</span></span>
<span style="font-style: italic"><span style="color: #9A1900"> */</span></span>
dbo<span style="color: #990000">::</span><span style="color: #008080">Transaction</span> <span style="font-weight: bold"><span style="color: #000000">transaction</span></span><span style="color: #990000">(</span>session<span style="color: #990000">);</span>
<span style="color: #008080">User</span> <span style="color: #990000">*</span>user <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">User</span></span><span style="color: #990000">();</span>
user<span style="color: #990000">-></span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"Joe"</span><span style="color: #990000">;</span>
user<span style="color: #990000">-></span>password <span style="color: #990000">=</span> <span style="color: #FF0000">"Secret"</span><span style="color: #990000">;</span>
user<span style="color: #990000">-></span>role <span style="color: #990000">=</span> User<span style="color: #990000">::</span>Visitor<span style="color: #990000">;</span>
user<span style="color: #990000">-></span>karma <span style="color: #990000">=</span> <span style="color: #993399">13</span><span style="color: #990000">;</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> userPtr <span style="color: #990000">=</span> session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>user<span style="color: #990000">);</span>
transaction<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">commit</span></span><span style="color: #990000">();</span>
<span style="color: #FF0000">}</span>
</tt></pre></div></div>
<div class="para"><p>A call to
<a href="../../reference/html/classWt_1_1Dbo_1_1Session.html#e255e99ca81e9642f52f20adc6fb752c">Session::add()</a>
adds an object to the database. This call returns a
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1ptr.html">ptr<<em>Dbo</em>></a></tt> to reference a database
object of type <tt><em>Dbo</em></tt>. This is a shared pointer which also keeps
track of the persistence state of the referenced object. Within each
session, a database object will be loaded at most once: the session
keeps track of loaded database objects and returns an existing object
whenever a query to the database requires this. When the last pointer
to a database object goes out of scope, the <em>transient</em> (in-memory)
copy of the database object is also deleted (unless it was modified,
in which case the transient copy will only be be deleted after changes
have been successfully committed to the database).</p></div>
<div class="para"><p>The session also keeps track of objects that have been modified and
which need to be flushed (using SQL statements) to the
database. Flushing happens automatically when committing the
transaction, or whenever needed to maintain consistency between the
transient objects and the database copy (e.g. before doing a query).</p></div>
<div class="para"><p>This generates the following SQL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>begin transaction
<span style="font-weight: bold"><span style="color: #0000FF">insert</span></span> <span style="font-weight: bold"><span style="color: #0000FF">into</span></span> user<span style="color: #990000">(</span>version<span style="color: #990000">,</span> name<span style="color: #990000">,</span> password<span style="color: #990000">,</span> role<span style="color: #990000">,</span> karma<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">values</span></span> <span style="color: #990000">(?,</span> <span style="color: #990000">?,</span> <span style="color: #990000">?,</span> <span style="color: #990000">?,</span> <span style="color: #990000">?)</span>
commit transaction
</tt></pre></div></div>
<div class="para"><p>All SQL statements are prepared once and reused later, which have the
benefit of avoiding SQL injection problems, and allows potentially
better performance.</p></div>
</div>
<h2 id="_querying_objects">4. Querying objects</h2>
<div class="sectionbody">
<div class="para"><p>There are two ways of querying the database. Database objects of a
single <tt><em>Dbo</em></tt> class can be queried using
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Session.html#5587d7d9b1708f123d158bb615b6f199">Session::find<Dbo>(<em>condition</em>)</a></tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> joe <span style="color: #990000">=</span> session<span style="color: #990000">.</span>find<span style="color: #990000"><</span>User<span style="color: #990000">></span>
<span style="color: #990000">(</span><span style="color: #FF0000">"where name = ?"</span><span style="color: #990000">)</span>
<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">bind</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Joe"</span><span style="color: #990000">);</span>
std<span style="color: #990000">::</span>cerr <span style="color: #990000"><<</span> <span style="color: #FF0000">"Joe has karma: "</span> <span style="color: #990000"><<</span> joe<span style="color: #990000">-></span>karma <span style="color: #990000"><<</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>All queries use prepared statements with positional argument
binding. The <tt>Session::find<T>()</tt> method returns a
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Query.html">Query</a></tt> object, which allows binding
of parameters using
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Query.html#8470965879eb9d0d7d64913e3239d710">Query::bind()</a></tt>. In
this case the query should expect a single result and is casted
directly to a database object pointer.</p></div>
<div class="para"><p>The query formulated to the database is:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">select</span></span> id<span style="color: #990000">,</span> version<span style="color: #990000">,</span> name<span style="color: #990000">,</span> password<span style="color: #990000">,</span> role<span style="color: #990000">,</span> karma
<span style="font-weight: bold"><span style="color: #0000FF">from</span></span> user
<span style="font-weight: bold"><span style="color: #0000FF">where</span></span> name <span style="color: #990000">=</span> <span style="color: #990000">?</span>
</tt></pre></div></div>
<div class="para"><p>The more general way for querying uses
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Session.html#d39e953ef521eaa58c4c7e6ca1a6c19e">Session::query<Result>(<em>sql</em>)</a></tt>,
which supports not only database objects as results. The query of
above is equivalent to:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> joe2 <span style="color: #990000">=</span> session<span style="color: #990000">.</span>query<span style="color: #990000"><</span> dbo<span style="color: #990000">::</span>ptr<span style="color: #990000"><</span>User<span style="color: #990000">></span> <span style="color: #990000">></span>
<span style="color: #990000">(</span><span style="color: #FF0000">"select u from user u where name = ?"</span><span style="color: #990000">)</span>
<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">bind</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Joe"</span><span style="color: #990000">);</span>
</tt></pre></div></div>
<div class="para"><p>And this generates similar SQL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">select</span></span> u<span style="color: #990000">.</span>id<span style="color: #990000">,</span> u<span style="color: #990000">.</span>version<span style="color: #990000">,</span> u<span style="color: #990000">.</span>name<span style="color: #990000">,</span> u<span style="color: #990000">.</span>password<span style="color: #990000">,</span> u<span style="color: #990000">.</span>role<span style="color: #990000">,</span> u<span style="color: #990000">.</span>karma
<span style="font-weight: bold"><span style="color: #0000FF">from</span></span> user u
<span style="font-weight: bold"><span style="color: #0000FF">where</span></span> name <span style="color: #990000">=</span> <span style="color: #990000">?</span>
</tt></pre></div></div>
<div class="para"><p>The <tt><em>sql</em></tt> statement passed to the method may be arbitrary sql which
returns results that are compatible with the <tt><em>Result</em></tt> type. The
<tt><em>select</em></tt> part of the SQL query may be rewritten (as in the example
above) to return the individual fields of a queried database object.</p></div>
<div class="para"><p>To illustrate that <tt>Session::query<Result>()</tt> may be used to return
other types, consider the query below where an <tt>int</tt> result is
returned.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #009900">int</span> count <span style="color: #990000">=</span> session<span style="color: #990000">.</span>query<span style="color: #990000"><</span><span style="color: #009900">int</span><span style="color: #990000">></span>
<span style="color: #990000">(</span><span style="color: #FF0000">"select count(*) from user where name = ?"</span><span style="color: #990000">)</span>
<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">bind</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Joe"</span><span style="color: #990000">);</span>
</tt></pre></div></div>
<div class="para"><p>The queries above were expecting unique results, but queries can also
have multiple results. A <tt>Session::query<<em>Result</em>>()</tt> may therefore
return a <tt>dbo::collection< <em>Result</em> ></tt> (for multiple results) or a
unique <tt><em>Result</em></tt> as in the examples above for convenience. Similarly,
<tt>Session::find<<em>Dbo</em>>()</tt> may return a <tt>collection< ptr<<em>Dbo</em>> ></tt> or a
unique <tt><em>ptr<_Dbo</em>></tt>. If a unique result is asked, but the query found
multiple results, a
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1NoUniqueResultException.html">NoUniqueResultException</a></tt>
will be thrown.</p></div>
<div class="para"><p><tt><a href="../../reference/html/classWt_1_1Dbo_1_1collection.html">collection<<em>T</em>></a></tt> is an
STL-compatible collection which has iterators that implement the
<tt>InputIterator</tt> requirements. Thus, you can only iterate the results
of a collection once. After the results have been iterated both the
<tt>Query</tt> object and the <tt>collection</tt> can no longer be used.</p></div>
<div class="para"><p>The following code shows how you may multiple results of a query may
be iterated:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> dbo<span style="color: #990000">::</span><span style="color: #008080">collection< dbo::ptr<User> ></span> Users<span style="color: #990000">;</span>
<span style="color: #008080">Users</span> users <span style="color: #990000">=</span> session<span style="color: #990000">.</span>find<span style="color: #990000"><</span>User<span style="color: #990000">>();</span>
std<span style="color: #990000">::</span>cerr <span style="color: #990000"><<</span> <span style="color: #FF0000">"We have "</span> <span style="color: #990000"><<</span> users<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000"><<</span> <span style="color: #FF0000">" users:"</span> <span style="color: #990000"><<</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #990000">(</span>Users<span style="color: #990000">::</span><span style="color: #008080">const_iterator</span> i <span style="color: #990000">=</span> users<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">begin</span></span><span style="color: #990000">();</span> i <span style="color: #990000">!=</span> users<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">end</span></span><span style="color: #990000">();</span> <span style="color: #990000">++</span>i<span style="color: #990000">)</span>
std<span style="color: #990000">::</span>cerr <span style="color: #990000"><<</span> <span style="color: #FF0000">" user "</span> <span style="color: #990000"><<</span> <span style="color: #990000">(*</span>i<span style="color: #990000">)-></span>name
<span style="color: #990000"><<</span> <span style="color: #FF0000">" with karma of "</span> <span style="color: #990000"><<</span> <span style="color: #990000">(*</span>i<span style="color: #990000">)-></span>karma <span style="color: #990000"><<</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>This code will perform two database queries: one for the call to
<tt>collection::size()</tt> and one for iterating the results:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">select</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">from</span></span> user<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">select</span></span> id<span style="color: #990000">,</span> version<span style="color: #990000">,</span> name<span style="color: #990000">,</span> password<span style="color: #990000">,</span> role<span style="color: #990000">,</span> karma <span style="font-weight: bold"><span style="color: #0000FF">from</span></span> user
</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Warning</div>
</td>
<td class="content">
<div class="para"><p>A query uses a prepared statement to execute, and prepares a new
statement if no statement was yet prepared for that query. Because a
prepared statement is usually not reentrant and at the same time a
query will use an existing statement if one exists, you need to be
careful to not have two collections with the same statement busy at
the same time. Thus while iterating the results of a query you cannot
use that same query again. Therefore it may be necessary to copy the
results into a standard container (such as <tt>std::vector</tt>) before
iterating them.</p></div>
</td>
</tr></table>
</div>
</div>
<h2 id="_updating_objects">5. Updating objects</h2>
<div class="sectionbody">
<div class="para"><p>Unlike most other smart pointers, <tt>ptr<<em>Dbo</em>></tt> is read-only by
default: it returns a <tt>const <em>Dbo</em>*</tt>. To modify a database object, you
need to call the <tt><a href="../../reference/html/classWt_1_1Dbo_1_1ptr.html#8f9f4ec6ed2bd8febe8b62525c9576da">ptr::modify()</a></tt> method, which returns a non-const
object. This mark the object as dirty and the modifications will later
be synchronized to the database.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> joe <span style="color: #990000">=</span> session<span style="color: #990000">.</span>find<span style="color: #990000"><</span>User<span style="color: #990000">>(</span><span style="color: #FF0000">"where name = ?"</span><span style="color: #990000">).</span><span style="font-weight: bold"><span style="color: #000000">bind</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Joe"</span><span style="color: #990000">);</span>
joe<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>karma<span style="color: #990000">++;</span>
joe<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>password <span style="color: #990000">=</span> <span style="color: #FF0000">"public"</span><span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>Database synchronization does not happen instantaneously, instead,
they are delayed until explicitly asked, using
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1ptr.html#af91f5ef8bcdabfafcd9abb4182625bd">ptr<<em>Dbo</em>>::flush()</a></tt>
or
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Session.html#b896d119f1e0fb79adffe2282b92ef17">Session::flush()</a></tt>,
until a query is executed whose results may be affected by the changes
made, or until the transaction is committed.</p></div>
<div class="para"><p>The previous code will generate the following SQL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">select</span></span> id<span style="color: #990000">,</span> version<span style="color: #990000">,</span> name<span style="color: #990000">,</span> password<span style="color: #990000">,</span> role<span style="color: #990000">,</span> karma
<span style="font-weight: bold"><span style="color: #0000FF">from</span></span> user
<span style="font-weight: bold"><span style="color: #0000FF">where</span></span> name <span style="color: #990000">=</span> <span style="color: #990000">?;</span>
<span style="font-weight: bold"><span style="color: #0000FF">update</span></span> user
<span style="font-weight: bold"><span style="color: #0000FF">set</span></span> version <span style="color: #990000">=</span> <span style="color: #990000">?,</span> name <span style="color: #990000">=</span> <span style="color: #990000">?,</span> password <span style="color: #990000">=</span> <span style="color: #990000">?,</span> role <span style="color: #990000">=</span> <span style="color: #990000">?,</span> karma <span style="color: #990000">=</span> <span style="color: #990000">?</span>
<span style="font-weight: bold"><span style="color: #0000FF">where</span></span> id <span style="color: #990000">=</span> <span style="color: #990000">?</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> version <span style="color: #990000">=</span> <span style="color: #990000">?</span>
</tt></pre></div></div>
<div class="para"><p>We already saw how using <tt>Session::add(ptr<<em>Dbo</em>>)</tt>, we added a new
object to the database. The opposite operation is
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1ptr.html#f4b26afebd56abc42005ef9f954d8fee">ptr<<em>Dbo</em>>::remove()</a></tt>: it deletes the object in the database.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> joe <span style="color: #990000">=</span> session<span style="color: #990000">.</span>find<span style="color: #990000"><</span>User<span style="color: #990000">>(</span><span style="color: #FF0000">"where name = ?"</span><span style="color: #990000">).</span><span style="font-weight: bold"><span style="color: #000000">bind</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"Joe"</span><span style="color: #990000">);</span>
joe<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">();</span>
</tt></pre></div></div>
<div class="para"><p>After removing an object, the transient object can still be used, and
can even be re-added to the database.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
<div class="para"><p>Like <tt>modify()</tt>, also the <tt>add()</tt> and <tt>remove()</tt> operations defer
synchronization with the database, and therefore the following code
does not actually have any effect on the database:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> silly <span style="color: #990000">=</span> session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">User</span></span><span style="color: #990000">());</span>
silly<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"Silly"</span><span style="color: #990000">;</span>
silly<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">remove</span></span><span style="color: #990000">();</span>
</tt></pre></div></div>
</td>
</tr></table>
</div>
</div>
<h2 id="_mapping_relations">6. Mapping relations</h2>
<div class="sectionbody">
<h3 id="_em_many_to_one_em_relations">6.1. <em>Many-to-One</em> relations</h3><div style="clear:left"></div>
<div class="para"><p>Let's add posts to our blogging example, and define a Man-to-One
relation between posts and users. In the code below, we limit
ourselves to the statements important for defining the relationship.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><Wt/Dbo/Dbo></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><string></span>
<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> dbo <span style="color: #990000">=</span> Wt<span style="color: #990000">::</span>Dbo<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">User</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Post</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<User></span> user<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000"><</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">></span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">belongsTo</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> user<span style="color: #990000">,</span> <span style="color: #FF0000">"user"</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">User</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">collection< dbo::ptr<Post> ></span> posts<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000"><</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">></span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">hasMany</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> posts<span style="color: #990000">,</span> dbo<span style="color: #990000">::</span>ManyToOne<span style="color: #990000">,</span> <span style="color: #FF0000">"user"</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>At the <em>Many</em>-side, we add a reference to a user, and in the
<tt>persist()</tt> method we call
<tt><a href="../../reference/html/group__dbo.html#gb4d8cd501e0e5bcb7e590315758f05d8">belongsTo()</a></tt>. This
allows us to reference the user to which this post belongs. The last
argument will correspond to the name of the database column which
defines the relationship.</p></div>
<div class="para"><p>At the <em>One</em>-side, we add a collection of posts, and in the
<tt>persist()</tt> method we call
<tt><a href="../../reference/html/group__dbo.html#gac5c7ca40abd8040451dfb0e93bca9b5">hasMany()</a></tt>. The
join field must be the same name as in reciproce belongsTo() method
call.</p></div>
<div class="para"><p>If we add the Post class too to our session using
<tt>Session::mapClass()</tt>, and create the schema, the following SQL is
generated:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> user<span style="color: #990000">(</span>
<span style="color: #990000">...</span>
<span style="font-style: italic"><span style="color: #9A1900">-- table user is unaffected by the relationship</span></span>
<span style="color: #990000">);</span>
<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> post<span style="color: #990000">(</span>
<span style="color: #990000">...</span>
user_id integer <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> user<span style="color: #990000">(</span>id<span style="color: #990000">)</span>
<span style="color: #990000">)</span>
</tt></pre></div></div>
<div class="para"><p>Note the <tt>user_id</tt> field which corresponds to the join name “user”.</p></div>
<div class="para"><p>At the <em>Many</em>-side, you may read or write the <tt>ptr</tt> to set a user to
which this post belongs.</p></div>
<div class="para"><p>The collection at the <em>One</em>-side allows us to retrieve all associated
elements, but is read-only: inserting elements will not have any
effect: to add a <em>post</em> to a <em>user</em>, you need to set the <em>user</em> for
the <em>post</em>, rather than adding the post to the collection in
user.</p></div>
<div class="para"><p>Example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<Post></span> post <span style="color: #990000">=</span> session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">Post</span></span><span style="color: #990000">());</span>
post<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>user <span style="color: #990000">=</span> joe<span style="color: #990000">;</span>
<span style="font-style: italic"><span style="color: #9A1900">// will print 'Joe has 1 post(s).'</span></span>
std<span style="color: #990000">::</span>cerr <span style="color: #990000"><<</span> <span style="color: #FF0000">"Joe has "</span> <span style="color: #990000"><<</span> joe<span style="color: #990000">-></span>posts<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000"><<</span> <span style="color: #FF0000">" post(s)."</span> <span style="color: #990000"><<</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>As you can see, as soon as <em>joe</em> is set as <em>user</em> for the new post, the
post is reflected in the <em>posts</em> collection of <em>joe</em>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Warning</div>
</td>
<td class="content">
<div class="para"><p>The collection uses a prepared statement to execute. Collections will
try to share a single prepared statement, but prepared statements are
usually not reentrant. As a result, you need to be careful to not have
two collections with the same statement busy at the same time. Thus
while iterating a collection, you need to be sure you will not
reentrantly iterate the same collection (of the same or another
object). Therefore it may be necessary to copy the results into a
standard container (such as <tt>std::vector</tt>) before iterating them.</p></div>
</td>
</tr></table>
</div>
<h3 id="_em_many_to_many_em_relations">6.2. <em>Many-to-Many</em> relations</h3><div style="clear:left"></div>
<div class="para"><p>To illustrate <em>Many-to-Many</em> relations, we will add tags to our
blogging example, and define an <em>Many-to-Many</em> relation between posts
and tags. In the code below, we again limit ourselves to the
statements important for defining the relationship.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><Wt/Dbo/Dbo></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000"><string></span>
<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> dbo <span style="color: #990000">=</span> Wt<span style="color: #990000">::</span>Dbo<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Tag</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Post</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">collection< dbo::ptr<Tag> ></span> tags<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000"><</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">></span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">hasMany</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> tags<span style="color: #990000">,</span> dbo<span style="color: #990000">::</span>ManyToMany<span style="color: #990000">,</span> <span style="color: #FF0000">"post_tags"</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Tag</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">collection< dbo::ptr<Post> ></span> posts<span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000"><</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">></span>
<span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&</span> a<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">hasMany</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> posts<span style="color: #990000">,</span> dbo<span style="color: #990000">::</span>ManyToMany<span style="color: #990000">,</span> <span style="color: #FF0000">"post_tags"</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="para"><p>As expected, the relationship is reflected in almost the same way in
both classes: they both have a <tt>collection</tt> of database objects of the
related class, and in the <tt>persist()</tt> method we call <tt>hasMany()</tt>. The
join field in this case will correspond to the name of a join-table
used to persist the relation.</p></div>
<div class="para"><p>Adding the Post class to our session using <tt>Session::mapClass()</tt>, we
now get the following SQL for creating the schema:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> post<span style="color: #990000">(</span>
<span style="color: #990000">...</span>
<span style="font-style: italic"><span style="color: #9A1900">-- table post is unaffected by the relationship</span></span>
<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> tag<span style="color: #990000">(</span>
<span style="color: #990000">...</span>
<span style="font-style: italic"><span style="color: #9A1900">-- table tag is unaffected by the relationship</span></span>
<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> post_tags<span style="color: #990000">(</span>
post_id integer <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> post<span style="color: #990000">(</span>id<span style="color: #990000">),</span>
tag_id integer <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> tag<span style="color: #990000">(</span>id<span style="color: #990000">),</span>
<span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span><span style="color: #990000">(</span>post_id<span style="color: #990000">,</span> tag_id<span style="color: #990000">)</span>
<span style="color: #990000">)</span>
</tt></pre></div></div>
<div class="para"><p>The collection at either side of the <em>Many-to-Many</em> relation allows us
to retrieve all associated elements. Unlike a collection in a
<em>Many-to-One</em> relation however, we may now also
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1collection.html#7b497add0d6ac2b1e098820f9ee64c93">insert()</a></tt>
and
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1collection.html#35d046e991b3fec59af85d501fd49e51">erase()</a></tt>
items from the collection. To define a relation between a post and a
tag, you need to add the post to the tag's <em>posts</em> collection, or the
tag to the post's <em>tags</em> collection. You may not do both! The change
will automatically be reflected in the reciproce collection. Likewise,
to undo the relation between a post and a tag, you should remove the
tag from the post's <em>tags</em> collection, or the post from the tag's
<em>posts</em> collection, but not both.</p></div>
<div class="para"><p>Example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.11.1
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<Post></span> post <span style="color: #990000">=</span> <span style="color: #990000">...</span>
dbo<span style="color: #990000">::</span><span style="color: #008080">ptr<Tag></span> cooking <span style="color: #990000">=</span> session<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">Tag</span></span><span style="color: #990000">());</span>
cooking<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"Cooking"</span><span style="color: #990000">;</span>
post<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">modify</span></span><span style="color: #990000">()-></span>tags<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">insert</span></span><span style="color: #990000">(</span>cooking<span style="color: #990000">);</span>
<span style="font-style: italic"><span style="color: #9A1900">// will print '1 post(s) tagged with Cooking.'</span></span>
std<span style="color: #990000">::</span>cerr <span style="color: #990000"><<</span> cooking<span style="color: #990000">-></span>posts<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000"><<</span> <span style="color: #FF0000">" post(s) tagged with Cooking."</span> <span style="color: #990000"><<</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Warning</div>
</td>
<td class="content">
<div class="para"><p>The same warning as above applies here as well.</p></div>
</td>
</tr></table>
</div>
<h3 id="_em_one_to_one_em_relations">6.3. <em>One-to-One</em> relations</h3><div style="clear:left"></div>
<div class="para"><p><em>One-to-One</em> relations are currently not supported, but can be
simulated using <em>Many-to-One</em> relations as they have the same database
schema structure.</p></div>
</div>
<h2 id="_transactions_and_concurrency">7. Transactions and concurrency</h2>
<div class="sectionbody">
<div class="para"><p>Reading data from the database or flushing changes to the database
require an active transaction. A
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1Transaction.html">Transaction</a></tt> is a RIIA
(Resource-Initialization-is-Acquisition) class which at the same time
provides isolation between concurrent sessions and atomicity for
persisting changes to the database.</p></div>
<div class="para"><p>The library implements optimistic locking, which allows detection
(rather than avoidance) of concurrent modifications. It is a
recommended and widely used strategy for dealing with concurrency
issues in a scalable manner as no write locks are needed on the
database. To detect a concurrent modification, a <tt>version</tt> field is
added to each table which is incremented on each modification. When
performing a modification (such as updating or removing an object), it
is checked that the version of the record in the database is the same
as the version of the object that was originally read from the
database.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<div class="title">Note</div>
</td>
<td class="content">
<div class="title">Transaction isolation levels</div>The minimum level of isolation which is required for the library's
<em>optimistic locking</em> strategy is <em>Read Committed</em>: modifications in a
transaction are only visible to other sessions as soon as they are
committed. This is usually the lowest level of isolation supported by
a database (SQLite3 is currently the only backend and provides this
isolation level by default).</td>
</tr></table>
</div>
<div class="para"><p>The <tt>Transaction</tt> class is a light-weight proxy that references a
<em>logical</em> transaction: multiple (usually nested) Transaction objects
may be instantiated simultaneously, which each need to be committed
for the logical transaction to be committed. In this way you can
easily protect individual methods which require database access with
such a transaction object, which will automatically participate in a
wider transaction if that is available.</p></div>
<div class="para"><p>Transactions may fail and dealing with failing transactions is an
integral aspect of their usage. When the library detects a concurrent
modification, a
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1StaleObjectException.html">StaleObjectException</a></tt>
is thrown. Other exceptions may be thrown, including exceptions in the
backend driver when for example the database schema is not compatible
with the mapping. There may also be problems detected by the business
logic which may raise an exception and cause the transaction to be
rolled back. When a transaction is rolled back, the modified database
objects are not successfully synchronized with the database, but may
possibly be synchronized later in a new transaction.</p></div>
<div class="para"><p>Obviously, many exceptions will be fatal. One notable exception is the
<tt>StaleObjectException</tt> however. Different strategies are possible to
deal with this exception. Regardless of the approach, you will at
least need to
<tt><a href="../../reference/html/classWt_1_1Dbo_1_1ptr.html#bb1db71ef910748437d69bf11a04eb6e">reread()</a></tt>
the stale database object(s) before being able to commit changes made
in a new transaction.</p></div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2009-12-29 15:51:52 CEST
</div>
</div>
</body>
</html>
|