File: index.html

package info (click to toggle)
soci 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,944 kB
  • sloc: ansic: 169,887; cpp: 54,198; javascript: 12,258; ada: 1,973; sh: 36; makefile: 12; xml: 2
file content (749 lines) | stat: -rw-r--r-- 31,689 bytes parent folder | download
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
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        
        
        <link rel="shortcut icon" href="../img/favicon.ico">
        <title>Data Types - SOCI (4.1.2)</title>
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/fontawesome.min.css" rel="stylesheet">
        <link href="../css/brands.min.css" rel="stylesheet">
        <link href="../css/solid.min.css" rel="stylesheet">
        <link href="../css/v4-font-face.min.css" rel="stylesheet">
        <link href="../css/base.css" rel="stylesheet">
        <link id="hljs-light" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" >
        <link id="hljs-dark" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css" disabled>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
        <script>hljs.highlightAll();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="..">SOCI (4.1.2)</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbar-collapse" aria-controls="navbar-collapse" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="nav-item">
                                <a href=".." class="nav-link">Home</a>
                            </li>
                            <li class="nav-item dropdown">
                                <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"  aria-expanded="false">Overview</a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../quickstart/" class="dropdown-item">Getting Started</a>
</li>
                                    
<li>
    <a href="../installation/" class="dropdown-item">Installation</a>
</li>
                                    
<li>
    <a href="../structure/" class="dropdown-item">Library Structure</a>
</li>
                                    
<li>
    <a href="../license/" class="dropdown-item">License</a>
</li>
                                    
<li>
    <a href="../faq/" class="dropdown-item">FAQ</a>
</li>
                                </ul>
                            </li>
                            <li class="nav-item dropdown">
                                <a href="#" class="nav-link dropdown-toggle active" aria-current="page" role="button" data-bs-toggle="dropdown"  aria-expanded="false">User Guide</a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../connections/" class="dropdown-item">Connections</a>
</li>
                                    
<li>
    <a href="../queries/" class="dropdown-item">Queries</a>
</li>
                                    
<li>
    <a href="../binding/" class="dropdown-item">Data Binding</a>
</li>
                                    
<li>
    <a href="../indicators/" class="dropdown-item">Data Indicators</a>
</li>
                                    
<li>
    <a href="./" class="dropdown-item active" aria-current="page">Data Types</a>
</li>
                                    
<li>
    <a href="../lobs/" class="dropdown-item">LOBs</a>
</li>
                                    
<li>
    <a href="../statements/" class="dropdown-item">Statements</a>
</li>
                                    
<li>
    <a href="../transactions/" class="dropdown-item">Transactions</a>
</li>
                                    
<li>
    <a href="../procedures/" class="dropdown-item">Procedures</a>
</li>
                                    
<li>
    <a href="../errors/" class="dropdown-item">Errors</a>
</li>
                                    
<li>
    <a href="../logging/" class="dropdown-item">Logging</a>
</li>
                                    
<li>
    <a href="../interfaces/" class="dropdown-item">Interfaces</a>
</li>
                                </ul>
                            </li>
                            <li class="nav-item dropdown">
                                <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"  aria-expanded="false">Backends</a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../backends/" class="dropdown-item">Features</a>
</li>
                                    
<li>
    <a href="../backends/db2/" class="dropdown-item">DB2</a>
</li>
                                    
<li>
    <a href="../backends/firebird/" class="dropdown-item">Firebird</a>
</li>
                                    
<li>
    <a href="../backends/mysql/" class="dropdown-item">MySQL</a>
</li>
                                    
<li>
    <a href="../backends/odbc/" class="dropdown-item">ODBC</a>
</li>
                                    
<li>
    <a href="../backends/oracle/" class="dropdown-item">Oracle</a>
</li>
                                    
<li>
    <a href="../backends/postgresql/" class="dropdown-item">PostgreSQL</a>
</li>
                                    
<li>
    <a href="../backends/sqlite3/" class="dropdown-item">SQLite3</a>
</li>
                                </ul>
                            </li>
                            <li class="nav-item dropdown">
                                <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"  aria-expanded="false">Miscellaneous</a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../beyond/" class="dropdown-item">Beyond SQL</a>
</li>
                                    
<li>
    <a href="../multithreading/" class="dropdown-item">Multi-threading</a>
</li>
                                    
<li>
    <a href="../boost/" class="dropdown-item">Boost</a>
</li>
                                    
<li>
    <a href="../utilities/" class="dropdown-item">Utilities</a>
</li>
                                    
<li>
    <a href="../vagrant/" class="dropdown-item">Vagrant</a>
</li>
                                </ul>
                            </li>
                            <li class="nav-item dropdown">
                                <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"  aria-expanded="false">API</a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../api/client/" class="dropdown-item">Client API</a>
</li>
                                    
<li>
    <a href="../api/backend/" class="dropdown-item">Backend API</a>
</li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">Ada</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../languages/ada/" class="dropdown-item">Ada Bindings</a>
</li>
            
<li>
    <a href="../languages/ada/concepts/" class="dropdown-item">Ada Concepts</a>
</li>
            
<li>
    <a href="../languages/ada/idioms/" class="dropdown-item">Ada Idioms</a>
</li>
            
<li>
    <a href="../languages/ada/reference/" class="dropdown-item">Ada API Reference</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ms-md-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-bs-toggle="modal" data-bs-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../indicators/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> Previous
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../lobs/" class="nav-link">
                                    Next <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="https://github.com/SOCI/soci/edit/master/docs/types.md" class="nav-link"><i class="fa-brands fa-github"></i> Edit on GitHub</a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-bs-toggle="collapse" data-bs-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-body-tertiary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-bs-level="1"><a href="#data-types" class="nav-link">Data Types</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-bs-level="2"><a href="#static-binding" class="nav-link">Static binding</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-bs-level="2"><a href="#dynamic-binding" class="nav-link">Dynamic binding</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-bs-level="2"><a href="#user-defined-c-types" class="nav-link">User-defined C++ types</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-bs-level="2"><a href="#object-relational-mapping" class="nav-link">Object-Relational Mapping</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="data-types">Data Types</h1>
<h2 id="static-binding">Static binding</h2>
<p>The static binding for types is most useful when the types used in the database are known at compile time - this was already presented above with the help of <code>into</code> and <code>use</code> functions.</p>
<p>The following types are currently supported for use with <code>into</code> and <code>use</code> expressions:</p>
<ul>
<li><code>char</code> (for character values)</li>
<li><code>int8_t</code>, <code>uint8_t</code>, <code>int16_t</code>, <code>uint16_t</code>, <code>int32_t</code>, <code>uint32_t</code>, <code>int64_t</code>, <code>uint64_t</code>, <code>double</code> (for numeric values)</li>
<li><code>std::string</code> (for string values)</li>
<li><code>std::tm</code> (for datetime values)</li>
<li><code>soci::statement</code> (for nested statements and PL/SQL cursors)</li>
<li><code>soci::blob</code> (for Binary Large OBjects)</li>
<li><code>soci::row_id</code> (for row identifiers)</li>
</ul>
<p>See the test code that accompanies the library to see how each of these types is used.</p>
<p>Note that the fixed-size types <code>int32_t</code>, <code>int64_t</code> etc. are only typedefs for the fundamental types <code>int</code>, <code>long</code> etc.
This means that the two can be used interchangeably when interacting with SOCI and you can choose which one of them you want to use in your code.</p>
<h3 id="static-binding-for-bulk-operations">Static binding for bulk operations</h3>
<p>Bulk inserts, updates, and selects are supported through the following <code>std::vector</code> based into and use types:</p>
<ul>
<li><code>std::vector&lt;char&gt;</code></li>
<li><code>std::vector&lt;int8_t&gt;</code></li>
<li><code>std::vector&lt;uint8_t&gt;</code></li>
<li><code>std::vector&lt;int16_t&gt;</code></li>
<li><code>std::vector&lt;uint16_t&gt;</code></li>
<li><code>std::vector&lt;int32_t&gt;</code></li>
<li><code>std::vector&lt;uint32_t&gt;</code></li>
<li><code>std::vector&lt;int64_t&gt;</code></li>
<li><code>std::vector&lt;uint64_t&gt;</code></li>
<li><code>std::vector&lt;double&gt;</code></li>
<li><code>std::vector&lt;std::string&gt;</code></li>
<li><code>std::vector&lt;std::tm&gt;</code></li>
</ul>
<p>Use of the vector based types mirrors that of the standard types, with the size of the vector used to specify the number of records to process at a time.
See below for examples.</p>
<p>Bulk operations are supported also for <code>std::vector</code>s of the user-provided types that have appropriate conversion routines defines.</p>
<h2 id="dynamic-binding">Dynamic binding</h2>
<p>For certain applications it is desirable to be able to select data from arbitrarily structured tables (e.g. via "<code>select * from ...</code>") and format the resulting data based upon its type.</p>
<p>SOCI supports binding dynamic resultset through the <code>soci::row</code> and <code>soci::column_properties</code> classes.</p>
<p>Data is selected into a <code>row</code> object, which holds <code>column_properties</code> objects describing the attributes of data contained in each column.
Once the data type for each column is known, the data can be formatted appropriately.</p>
<p>For example, the code below creates an XML document from a selected row of data from an arbitrary table:</p>
<pre><code class="language-cpp">row r;
sql &lt;&lt; &quot;select * from some_table&quot;, into(r);

std::ostringstream doc;
doc &lt;&lt; &quot;&lt;row&gt;&quot; &lt;&lt; std::endl;
for(std::size_t i = 0; i != r.size(); ++i)
{
    const column_properties &amp; props = r.get_properties(i);

    doc &lt;&lt; '&lt;' &lt;&lt; props.get_name() &lt;&lt; '&gt;';

    switch(props.get_db_type())
    {
    case db_string:
        doc &lt;&lt; r.get&lt;std::string&gt;(i);
        break;
    case db_double:
        doc &lt;&lt; r.get&lt;double&gt;(i);
        break;
    case db_int8:
        doc &lt;&lt; r.get&lt;int8_t&gt;(i);
        break;
    case db_uint8:
        doc &lt;&lt; r.get&lt;uint8_t&gt;(i);
        break;
    case db_int16:
        doc &lt;&lt; r.get&lt;int16_t&gt;(i);
        break;
    case db_uint16:
        doc &lt;&lt; r.get&lt;uint16_t&gt;(i);
        break;
    case db_int32:
        doc &lt;&lt; r.get&lt;int32_t&gt;(i);
        break;
    case db_uint32:
        doc &lt;&lt; r.get&lt;uint32_t&gt;(i);
        break;
    case db_int64:
        doc &lt;&lt; r.get&lt;int64_t&gt;(i);
        break;
    case db_uint64:
        doc &lt;&lt; r.get&lt;uint64_t&gt;(i);
        break;
    case db_date:
        std::tm when = r.get&lt;std::tm&gt;(i);
        doc &lt;&lt; asctime(&amp;when);
        break;
    }

    doc &lt;&lt; &quot;&lt;/&quot; &lt;&lt; props.get_name() &lt;&lt; '&gt;' &lt;&lt; std::endl;
}
doc &lt;&lt; &quot;&lt;/row&gt;&quot;;
</code></pre>
<p>The type <code>T</code> parameter that should be passed to <code>row::get&lt;T&gt;()</code> depends on the SOCI data type that is returned from <code>data_type column_properties::get_data_type()</code> or <code>db_type column_properties::get_db_type()</code>.
Users are encouraged to use the latter as it supports a wider range of numerical C++ types.</p>
<p><code>row::get&lt;T&gt;()</code> throws an exception of type <code>std::bad_cast</code> if an incorrect type <code>T</code> is requested.</p>
<table>
<thead>
<tr>
<th>SOCI Data Type (<code>data_type</code>)</th>
<th><code>row::get&lt;T&gt;</code> specialization</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dt_double</code></td>
<td><code>double</code></td>
</tr>
<tr>
<td><code>dt_integer</code></td>
<td><code>int</code></td>
</tr>
<tr>
<td><code>dt_long_long</code></td>
<td><code>long long</code></td>
</tr>
<tr>
<td><code>dt_unsigned_long_long</code></td>
<td><code>unsigned long long</code></td>
</tr>
<tr>
<td><code>dt_string</code></td>
<td><code>std::string</code></td>
</tr>
<tr>
<td><code>dt_date</code></td>
<td><code>std::tm</code></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>SOCI Data Type (<code>db_type</code>)</th>
<th><code>row::get&lt;T&gt;</code> specialization</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>db_double</code></td>
<td><code>double</code></td>
</tr>
<tr>
<td><code>db_int8</code></td>
<td><code>int8_t</code></td>
</tr>
<tr>
<td><code>db_uint8</code></td>
<td><code>uint8_t</code></td>
</tr>
<tr>
<td><code>db_int16</code></td>
<td><code>int16_t</code></td>
</tr>
<tr>
<td><code>db_uint16</code></td>
<td><code>uint16_t</code></td>
</tr>
<tr>
<td><code>db_int32</code></td>
<td><code>int32_t</code></td>
</tr>
<tr>
<td><code>db_uint32</code></td>
<td><code>uint32_t</code></td>
</tr>
<tr>
<td><code>db_int64</code></td>
<td><code>int64_t</code></td>
</tr>
<tr>
<td><code>db_uint64</code></td>
<td><code>uint64_t</code></td>
</tr>
<tr>
<td><code>db_string</code></td>
<td><code>std::string</code></td>
</tr>
<tr>
<td><code>db_date</code></td>
<td><code>std::tm</code></td>
</tr>
</tbody>
</table>
<p>The mapping of underlying database column types to SOCI datatypes is database specific.
See the <a href="../backends/">backend documentation</a> for details.</p>
<p>While <code>row::get&lt;T&gt;()</code> (semantically) copies the internally stored data (that was fetched from the database), there is also <code>row::move_as&lt;T&gt;</code>, which
instead makes use of C++11's move semantics by moving the data out of the <code>row</code> instance. In case the used <code>T</code> does not support move semantics,
<code>move_as</code> is functionally equivalent to <code>get</code>.</p>
<h3 id="dealing-with-blobs">Dealing with Blobs</h3>
<p>If the fetched data is of type <code>db_blob</code> it is strongly recommended to use <code>row::move_as&lt;blob&gt;()</code> in order to obtain the data as a <code>blob</code> object. This
allows for the most flexible and efficient access of the underlying data.</p>
<p>Note that it is not possible to <code>row::get&lt;blob&gt;()</code> as <code>blob</code> objects are not copyable. However, you can instead get the blob's data into a
<code>std::string</code> or a <code>std::vector&lt;T&gt;</code> as long as <code>T</code> has a size of exactly one byte (i.e. is a byte-like type such as <code>char</code> or <code>std::byte</code>). Depending
on the used backend, this will either directly fetch the data from the database directly into the provided container or copy the already fetched data
into the provided container.</p>
<p>If you want to use a different container than the ones mentioned above, you need to specialize the <code>soci::is_contiguous_resizable_container</code> trait.
E.g.</p>
<pre><code class="language-cpp">template&lt;&gt;
struct ::soci::is_contiguous_resizable_container&lt;MyAwesomeContainer&gt; : std::true_type {};

template&lt;typename ValueType&gt;
struct ::soci::is_contiguous_resizable_container&lt;boost::container::vector&lt;ValueType&gt;, std::enable_if_t&lt;sizeof(ValueType) == sizeof(char)&gt;&gt; : std::true_type {};
</code></pre>
<p>Potentially, you may also need to specialize the <code>soci::contiguous_resizable_container_accessor</code> struct, if the default implementation doesn't work
for your custom container type:</p>
<pre><code class="language-cpp">template&lt;typename T, typename = std::enable_if_t&lt;is_contiguous_resizable_container_v&lt;T&gt;&gt;&gt;
struct contiguous_resizable_container_accessor
{
    // Gets the pointer to the beginning of the data store
    static void *data(T &amp;container)
    {
        static_assert(sizeof(decltype(container[0])) == sizeof(char), &quot;Expected value-type of container to be byte-sized&quot;);
        return &amp;container[0];
    }

    // Gets the size **in bytes** of this container
    static std::size_t size(const T &amp;container) { return container.size(); }

    // Resizes the container to the given size **in bytes**
    static void resize(T &amp;container, std::size_t size) { container.resize(size); }
};
</code></pre>
<h3 id="indicators">Indicators</h3>
<p>The <code>row</code> also provides access to indicators for each column:</p>
<pre><code class="language-cpp">row r;
sql &lt;&lt; &quot;select name from some_table where id = 1&quot;, into(r);
if (r.get_indicator(0) != soci::i_null)
{
    std::cout &lt;&lt; r.get&lt;std::string&gt;(0);
}
</code></pre>
<h3 id="stream-api">Stream API</h3>
<p>It is also possible to extract data from the <code>row</code> object using its stream-like interface, where each extracted variable should have matching type respective to its position in the chain:</p>
<pre><code class="language-cpp">row r;
sql &lt;&lt; &quot;select name, address, age from persons where id = 123&quot;, into(r);

string name, address;
int age;

r &gt;&gt; name &gt;&gt; address &gt;&gt; age;
</code></pre>
<p>Note, however, that this interface is <em>not</em> compatible with the standard <code>std::istream</code> class and that it is only possible to extract a single row at a time - for "safety" reasons the row boundary is preserved and it is necessary to perform the <code>fetch</code> operation explicitly for each consecutive row.</p>
<h2 id="user-defined-c-types">User-defined C++ types</h2>
<p>SOCI can be easily extended with support for user-defined datatypes.</p>
<p>The extension mechanism relies on appropriate specialization of the <code>type_conversion</code> structure that converts to and from one of the following SOCI base types:</p>
<ul>
<li><code>double</code></li>
<li><code>int8_t</code></li>
<li><code>uint8_t</code></li>
<li><code>int16_t</code></li>
<li><code>uint16_t</code></li>
<li><code>int32_t</code></li>
<li><code>uint32_t</code></li>
<li><code>int64_t</code></li>
<li><code>uint64_t</code></li>
<li><code>std::string</code></li>
<li><code>char</code></li>
<li><code>std::tm</code></li>
</ul>
<p>There are three required class members for a valid <code>type_conversion</code> specialization:</p>
<ul>
<li>the <code>base_type</code> type definition, aliasing either one of the base types <em>or another user-defined type</em></li>
<li>the <code>from_base()</code> static member function, converting from the base type</li>
<li>the <code>to_base()</code> static member function, converting to the base type</li>
</ul>
<p>Note that no database-specific code is required to define user conversion.</p>
<p>The following example shows how the user can extend SOCI to support his own type <code>MyInt</code>, which here is some wrapper for the fundamental <code>int</code> type:</p>
<pre><code class="language-cpp">class MyInt
{
public:
    MyInt() {}
    MyInt(int i) : i_(i) {}

    void set(int i) { i_ = i; }
    int get() const { return i_; }

private:
    int i_;
};

namespace soci
{
    template &lt;&gt;
    struct type_conversion&lt;MyInt&gt;
    {
        typedef int base_type;

        static void from_base(int i, indicator ind, MyInt &amp; mi)
        {
            if (ind == i_null)
            {
                throw soci_error(&quot;Null value not allowed for this type&quot;);
            }

            mi.set(i);
        }

        static void to_base(const MyInt &amp; mi, int &amp; i, indicator &amp; ind)
        {
            i = mi.get();
            ind = i_ok;
        }
    };
}
</code></pre>
<p>The above specialization for <code>soci::type_conversion&lt;MyInt&gt;</code> is enough to enable the following:</p>
<pre><code class="language-cpp">MyInt i;

sql &lt;&lt; &quot;select count(*) from person&quot;, into(i);

cout &lt;&lt; &quot;We have &quot; &lt;&lt; i.get() &lt;&lt; &quot; persons in the database.\n&quot;;
</code></pre>
<p>Note that there is a number of types from the Boost library integrated with SOCI out of the box, see <a href="../boost/">Integration with Boost</a> for complete description. Use these as examples of conversions for more complext data types.</p>
<p>Another possibility to extend SOCI with custom data types is to use the <code>into_type&lt;T&gt;</code> and <code>use_type&lt;T&gt;</code> class templates, which specializations can be user-provided. These specializations need to implement the interface defined by, respectively, the <code>into_type_base</code> and <code>use_type_base</code>
classes.</p>
<p>Note that when specializing these template classes the only convention is that when the indicator
variable is used (see below), it should appear in the second position. Please refer to the library source code to see how this is done for the standard types.</p>
<h2 id="object-relational-mapping">Object-Relational Mapping</h2>
<p>SOCI provides a class called <code>values</code> specifically to enable object-relational mapping via <code>type_conversion</code> specializations.</p>
<p>For example, the following code maps a <code>Person</code> object to and from a database table containing columns <code>"ID"</code>, <code>"FIRST_NAME"</code>, <code>"LAST_NAME"</code>, and <code>"GENDER"</code>.</p>
<p>Note that the mapping is non-invasive - the <code>Person</code> object itself does not contain any SOCI-specific code:</p>
<pre><code class="language-cpp">struct Person
{
    int id;
    std::string firstName;
    std::string lastName;
    std::string gender;
};

namespace soci
{
    template&lt;&gt;
    struct type_conversion&lt;Person&gt;
    {
        typedef values base_type;

        static void from_base(values const &amp; v, indicator /* ind */, Person &amp; p)
        {
            p.id = v.get&lt;int&gt;(&quot;ID&quot;);
            p.firstName = v.get&lt;std::string&gt;(&quot;FIRST_NAME&quot;);
            p.lastName = v.get&lt;std::string&gt;(&quot;LAST_NAME&quot;);

            // p.gender will be set to the default value &quot;unknown&quot;
            // when the column is null:
            p.gender = v.get&lt;std::string&gt;(&quot;GENDER&quot;, &quot;unknown&quot;);

            // alternatively, the indicator can be tested directly:
            // if (v.indicator(&quot;GENDER&quot;) == i_null)
            // {
            //     p.gender = &quot;unknown&quot;;
            // }
            // else
            // {
            //     p.gender = v.get&lt;std::string&gt;(&quot;GENDER&quot;);
            // }
        }

        static void to_base(const Person &amp; p, values &amp; v, indicator &amp; ind)
        {
            v.set(&quot;ID&quot;, p.id);
            v.set(&quot;FIRST_NAME&quot;, p.firstName);
            v.set(&quot;LAST_NAME&quot;, p.lastName);
            v.set(&quot;GENDER&quot;, p.gender, p.gender.empty() ? i_null : i_ok);
            ind = i_ok;
        }
    };
}
</code></pre>
<p>With the above <code>type_conversion</code> specialization in place, it is possible to use <code>Person</code> directly with SOCI:</p>
<pre><code class="language-cpp">session sql(oracle, &quot;service=db1 user=scott password=tiger&quot;);

Person p;
p.id = 1;
p.lastName = &quot;Smith&quot;;
p.firstName = &quot;Pat&quot;;
sql &lt;&lt; &quot;insert into person(id, first_name, last_name) &quot;
        &quot;values(:ID, :FIRST_NAME, :LAST_NAME)&quot;, use(p);

Person p1;
sql &lt;&lt; &quot;select * from person&quot;, into(p1);
assert(p1.id == 1);
assert(p1.firstName + p.lastName == &quot;PatSmith&quot;);
assert(p1.gender == &quot;unknown&quot;);

p.firstName = &quot;Patricia&quot;;
sql &lt;&lt; &quot;update person set first_name = :FIRST_NAME &quot;
        &quot;where id = :ID&quot;, use(p);
</code></pre>
<p>Note: The <code>values</code> class is currently not suited for use outside of <code>type_conversion</code>specializations.
It is specially designed to facilitate object-relational mapping when used as shown above.</p></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
                <p>Copyright &copy; 2017-2025 <a href="https://soci.sourceforge.net/people.html">SOCI Team</a>.</p>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script src="../js/bootstrap.bundle.min.js"></script>
        <script>
            var base_url = "..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../js/base.js"></script>
        <script src="../search/main.js"></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">Search</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>From here you can search these documents. Enter your search terms below.</p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results" data-no-results-text="No results found"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>