-
Notifications
You must be signed in to change notification settings - Fork 3
/
c09database.xml
1174 lines (1131 loc) · 33.6 KB
/
c09database.xml
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
<chapter id="c09database">
<title>Database API</title>
<para>
Internally, &kamailio; uses a reduced set of SQL operations to access
the records on the storage system. This allowed to write DB driver modules
for non-SQL storage systems, such as <emphasis role="strong">db_text</emphasis>
-- a tiny DB engine using text files.
</para>
<para>
Therefore, the interface provides data types and functions that are independent
of underlying DB storage. A DB driver module has to implement the functions specified
by the interface and provide a function named <emphasis role="strong">db_bind_api(...)</emphasis>
to link to the interface functions.
</para>
<para>
Starting with version 3.0.0, &kamailio; has two variants of database APIs, stored
as internal libraries, named <emphasis role="strong">srdb1</emphasis> and
<emphasis role="strong">srdb2</emphasis>. Most of &kamailio; modules are using
<emphasis role="strong">srdb1</emphasis>, thus for this document the focus will
be on how to use this option.
</para>
<para>
The DB1 interface is implemented in the <emphasis role="strong">lib/srdb1</emphasis> directory. To
use it, one has to include the file <emphasis role="strong">lib/srdb1/db.h</emphasis>.
</para>
<section id="c09apistructure">
<title>DB1 API Structure</title>
<para>
It is the structure that gets filled when binding to a DB driver module. It links to
the interface functions implemented in the module.
</para>
<example id="c09d_db_func">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef struct db_func {
unsigned int cap; /* Capability vector of the database transport */
db_use_table_f use_table; /* Specify table name */
db_init_f init; /* Initialize database connection */
db_close_f close; /* Close database connection */
db_query_f query; /* query a table */
db_fetch_result_f fetch_result; /* fetch result */
db_raw_query_f raw_query; /* Raw query - SQL */
db_free_result_f free_result; /* Free a query result */
db_insert_f insert; /* Insert into table */
db_delete_f delete; /* Delete from table */
db_update_f update; /* Update table */
db_replace_f replace; /* Replace row in a table */
db_last_inserted_id_f last_inserted_id; /* Retrieve the last inserted ID
in a table */
db_insert_update_f insert_update; /* Insert into table, update on duplicate key */
db_insert_delayed_f insert_delayed; /* Insert delayed into table */
db_affected_rows_f affected_rows; /* Numer of affected rows for last query */
} db_func_t;
...
</programlisting>
</example>
<para>
The attribute <emphasis role="strong">cap</emphasis> is a bitmask of implemented
functions, making easy to detect the capabilities of the DB driver module. A module
using the DB API should check at startup that the DB driver configured to be used
has the required capabilities. For example, <emphasis role="strong">msilo</emphasis>
module need <emphasis role="strong">select</emphasis>,
<emphasis role="strong">delete</emphasis> and
<emphasis role="strong">insert</emphasis> capabilities. The flags for capabilities
are enumerated in the next figure (located in <emphasis role="strong">lib/srdb1/db_cap.h</emphasis>).
</para>
<programlisting format="linespecific">
...
typedef enum db_cap {
DB_CAP_QUERY = 1 << 0, /* driver can perform queries */
DB_CAP_RAW_QUERY = 1 << 1, /* driver can perform raw queries */
DB_CAP_INSERT = 1 << 2, /* driver can insert data */
DB_CAP_DELETE = 1 << 3, /* driver can delete data */
DB_CAP_UPDATE = 1 << 4, /* driver can update data */
DB_CAP_REPLACE = 1 << 5, /* driver can replace (also known as INSERT OR UPDATE) data */
DB_CAP_FETCH = 1 << 6, /* driver supports fetch result queries */
DB_CAP_LAST_INSERTED_ID = 1 << 7, /* driver can return the ID of the last insert operation */
DB_CAP_INSERT_UPDATE = 1 << 8, /* driver can insert data into database, update on duplicate */
DB_CAP_INSERT_DELAYED = 1 << 9, /* driver can do insert delayed */
DB_CAP_AFFECTED_ROWS = 1 << 10 /* driver can return number of rows affected by last query */
} db_cap_t;
...
</programlisting>
</section>
<section id="c09dbapi_functions">
<title>DB1 API Functions</title>
<para>
</para>
<section id="c09f_init">
<title>Function init(...)</title>
<para>
Parse the DB URL and open a new connection to database.
</para>
<example id="c09t_init">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef db1_con_t* (*db_init_f) (const str* _url);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_url</emphasis> - database URL. Its format depends
on DB driver. For an SQL server like MySQL has to be:
<emphasis role="strong">mysql://username:password@server:port/database</emphasis>.
For <emphasis role="strong">db_text</emphasis> has to be:
<emphasis role="strong">text:///path/to/db/directory</emphasis>.
</para>
</listitem>
</itemizedlist>
<para>
The function returns pointer to db1_con_t* representing the connection structure or NULL
in case of error.
</para>
</section>
<section id="c09f_close">
<title>Function close(...)</title>
<para>
The function closes previously open connection and frees all previously allocated
memory. The function db_close must be the very last function called.
</para>
<example id="c09t_close">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef void (*db_close_f) (db1_con_t* _h);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - db1_con_t structure representing the
database connection.
</para>
</listitem>
</itemizedlist>
<para>
The function returns nothing.
</para>
</section>
<section id="c09f_use_table">
<title>Function use_table(...)</title>
<para>
Specify table name that will be used for subsequent operations (insert, delete,
update, query).
</para>
<example id="c09t_use_table">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_use_table_f)(db1_con_t* _h, const str * _t);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_t</emphasis> - table name.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_query">
<title>Function query(...)</title>
<para>
Query table for specified rows.
This function implements the SELECT SQL directive.
</para>
<example id="c09t_query">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_query_f) (const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
const db_key_t _o, db1_res_t** _r);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of column names that will be
compared and their values must match.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_op</emphasis> - array of operators to be used with
key-value pairs.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values, columns specified
in _k parameter must match these values.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_c</emphasis> - array of column names that you
are interested in.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of key-value pairs to match
in _k and _v parameters.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_nc</emphasis> - number of columns in _c parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_o</emphasis> - order by statement for query.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_r</emphasis> - address of variable where pointer to
the result will be stored.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
<para> <emphasis role="strong">Note:</emphasis>
If _k and _v parameters are NULL and _n is zero, you will get the whole table.
If _c is NULL and _nc is zero, you will get all table columns in the result.
Parameter _r will point to a dynamically allocated structure, it is neccessary to
call db_free_result function once you are finished with the result.
If _op is 0, equal (=) will be used for all key-value pairs comparisons.
Strings in the result are not duplicated, they will be discarded if you call. Make
a copy of db_free_result if you need to keep it after db_free_result.
You must call db_free_result before you can call db_query again!
</para>
</section>
<section id="c09f_fetch_result">
<title>Function fetch_result(...)</title>
<para>
Fetch a number of rows from a result.
</para>
<example id="c09t_fetch_result">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_fetch_result_f) (const db1_con_t* _h, db1_res_t** _r, const int _n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_r</emphasis> - structure for the result.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - the number of rows that should be fetched.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_raw_query">
<title>Function raw_query(...)</title>
<para>
This function can be used to do database specific queries. Please use this function
only if needed, as this creates portability issues for the different databases.
Also keep in mind that you need to escape all external data sources that you use.
You could use the escape_common and unescape_common functions in the core for this task.
</para>
<example id="c09t_raw_query">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_raw_query_f) (const db1_con_t* _h, const str* _s, db1_res_t** _r);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_s</emphasis> - the SQL query.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_r</emphasis> - structure for the result.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_free_result">
<title>Function free_result(...)</title>
<para>
Free a result allocated by db_query.
</para>
<example id="c09t_free_result">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_free_result_f) (db1_con_t* _h, db1_res_t* _r);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_r</emphasis> - pointer to db1_res_t structure to
destroy.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_insert">
<title>Function insert(...)</title>
<para>
Insert a row into the specified table.
</para>
<example id="c09t_insert">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_insert_f) (const db1_con_t* _h, const db_key_t* _k,
const db_val_t* _v, const int _n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names).
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values for keys specified
in _k parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of keys-value pairs int _k
and _v parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_delete">
<title>Function delete(...)</title>
<para>
Delete a row from the specified table.
</para>
<example id="c09t_delete">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_delete_f) (const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
const db_val_t* _v, const int _n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names) that
will be matched.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_o</emphasis> - array of operators to be used with
key-value pairs.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values that the row must
match to be deleted.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of keys-value pairs int _k
and _v parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_update">
<title>Function update(...)</title>
<para>
Update some rows in the specified table.
</para>
<example id="c09t_update">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_update_f) (const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv,
const int _n, const int _un);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names)
that will be matched.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_o</emphasis> - array of operators to be used with key-value pairs.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values that the row must match to be modified.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_uk</emphasis> - array of keys (column names) that will be modified.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_uv</emphasis> - new values for keys specified
in _k parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of key-value pairs in
_v parameters.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_un</emphasis> - number of key-value pairs in
_uv parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_replace">
<title>Function replace(...)</title>
<para>
Insert a row and replace if one already exists.
</para>
<example id="c09t_replace">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_replace_f) (const db1_con_t* handle, const db_key_t* keys,
const db_val_t* vals, const int n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names).
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values for keys specified
in _k parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of keys-value pairs int _k
and _v parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_last_inserted_id">
<title>Function last_inserted_id(...)</title>
<para>
Retrieve the last inserted ID in a table.
</para>
<example id="c09t_last_inserted_id">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_last_inserted_id_f) (const db1_con_t* _h);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - structure representing database
connection
</para>
</listitem>
</itemizedlist>
<para>
The function returns the ID as integer or returns 0 if the previous statement
does not use an AUTO_INCREMENT value.
</para>
</section>
<section id="c09f_insert_update">
<title>Function insert_update(...)</title>
<para>
Insert a row into specified table, update on duplicate key.
</para>
<example id="c09t_insert_update">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_insert_update_f) (const db1_con_t* _h, const db_key_t* _k,
const db_val_t* _v, const int _n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names).
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values for keys specified
in _k parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of keys-value pairs int _k
and _v parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_insert_delayed">
<title>Function insert_delayed(...)</title>
<para>
Insert delayed a row into specified table - don't wait for confirmation from database
server.
</para>
<example id="c09t_insert_delayed">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_insert_delayed_f) (const db1_con_t* _h, const db_key_t* _k,
const db_val_t* _v, const int _n);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - database connection handle.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_k</emphasis> - array of keys (column names).
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_v</emphasis> - array of values for keys specified
in _k parameter.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">_n</emphasis> - number of keys-value pairs int _k
and _v parameters.
</para>
</listitem>
</itemizedlist>
<para>
The function 0 if everything is OK, otherwise returns value negative value.
</para>
</section>
<section id="c09f_affected_rows">
<title>Function affected_rows(...)</title>
<para>
Retrieve the number of affected rows by last operation done to database.
</para>
<example id="c09t_affected_rows">
<title>Function type</title>
<programlisting format="linespecific">
...
typedef int (*db_affected_rows_f) (const db1_con_t* _h);
...
</programlisting>
</example>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">_h</emphasis> - structure representing database
connection
</para>
</listitem>
</itemizedlist>
<para>
The function returns the number of affected rows by previous DB operation.
</para>
</section>
</section>
<section id="c09dbapi_datatypes">
<title>DB API Data Types</title>
<para>
</para>
<section id="c09t_db_key_t">
<title>Type db_key_t</title>
<para>
This type represents a database key (column). Every time you need to specify
a key value, this type should be used.
</para>
<example id="c09d_db_key_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef str* db_key_t;
...
</programlisting>
</example>
</section>
<section id="c09t_db_op_t">
<title>Type db_op_t</title>
<para>
This type represents an expression operator uses for SQL queries.
</para>
<example id="c09d_db_op_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef const char* db_op_t;
...
</programlisting>
</example>
<para>
Predefined operators are:
</para>
<example id="c09d_db_op_t_predefined">
<title>DB Expression Operators</title>
<programlisting format="linespecific">
...
typedef const char* db_op_t;
/** operator less than */
#define OP_LT "<"
/** operator greater than */
#define OP_GT ">"
/** operator equal */
#define OP_EQ "="
/** operator less than equal */
#define OP_LEQ "<="
/** operator greater than equal */
#define OP_GEQ ">="
/** operator negation */
#define OP_NEQ "!="
...
</programlisting>
</example>
</section>
<section id="c09t_db_type_t">
<title>Type db_type_t</title>
<para>
Each cell in a database table can be of a different type. To distinguish
among these types, the db_type_t enumeration is used. Every value of the
enumeration represents one datatype that is recognized by the database API.
</para>
<example id="c09d_db_type_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef enum {
DB1_INT, /* represents an 32 bit integer number */
DB1_BIGINT, /* represents an 64 bit integer number */
DB1_DOUBLE, /* represents a floating point number */
DB1_STRING, /* represents a zero terminated const char* */
DB1_STR, /* represents a string of 'str' type */
DB1_DATETIME, /* represents date and time */
DB1_BLOB, /* represents a large binary object */
DB1_BITMAP /* an one-dimensional array of 32 flags */
} db_type_t;
...
</programlisting>
</example>
</section>
<section id="c09t_db_val_t">
<title>Type db_val_t</title>
<para>
This structure represents a value in the database. Several datatypes are
recognized and converted by the database API. These datatypes are
automatically recognized, converted from internal database representation
and stored in the variable of corresponding type.
Modules that want to use this values needs to copy them to another memory
location, because after the call to free_result there are not more available.
If the structure holds a pointer to a string value that needs to be freed
because the module allocated new memory for it then the free flag must be
set to a non-zero value. A free flag of zero means that the string data must
be freed internally by the database driver.
</para>
<example id="c09d_db_val_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef struct {
db_type_t type; /* Type of the value */
int nul; /* Means that the column in database has no value */
int free; /* Means that the value should be freed */
/** Column value structure that holds the actual data in a union. */
union {
int int_val; /* integer value */
long long ll_val; /* long long value */
double double_val; /* double value */
time_t time_val; /* unix time_t value */
const char* string_val; /* zero terminated string */
str str_val; /* str type string value */
str blob_val; /* binary object data */
unsigned int bitmap_val; /* Bitmap data type */
} val;
} db_val_t;
...
</programlisting>
</example>
</section>
<section id="c09t_db1_con_t">
<title>Type db_con_t</title>
<para>
This structure represents a database connection, pointer to this structure
are used as a connection handle from modules uses the db API.
</para>
<example id="c09d_db1_con_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef struct {
const str* table; /* Default table that should be used */
unsigned long tail; /* Variable length tail, database module specific */
} db1_con_t;
...
</programlisting>
</example>
</section>
<section id="c09t_db_row_t">
<title>Type db_row_t</title>
<para>
Structure holding the result of a query table function. It represents one
row in a database table. In other words, the row is an array of db_val_t
variables, where each db_val_t variable represents exactly one cell in the
table.
</para>
<example id="c09d_db_row_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef struct db_row {
db_val_t* values; /* Columns in the row */
int n; /* Number of columns in the row */
} db_row_t;
...
</programlisting>
</example>
</section>
<section id="c09t_db1_res_t">
<title>Type db1_res_t</title>
<para>
This type represents a result returned by db_query function (see below).
The result can consist of zero or more rows (see db_row_t description).
</para>
<para>
<emphasis role="strong">Note:</emphasis> A variable of type db_res_t
returned by db_query function uses dynamically allocated memory, don't forget
to call db_free_result if you don't need the variable anymore. You will
encounter memory leaks if you fail to do this! In addition to zero or more
rows, each db_res_t object contains also an array of db_key_t objects. The
objects represent keys (names of columns).
</para>
<example id="c09d_db1_res_t">
<title>Definition</title>
<programlisting format="linespecific">
...
typedef struct db1_res {
struct {
db_key_t* names; /* Column names */
db_type_t* types; /* Column types */
int n; /* Number of columns */
} col;
struct db_row* rows; /* Rows */
int n; /* Number of rows in current fetch */
int res_rows; /* Number of total rows in query */
int last_row; /* Last row */
} db1_res_t;
...
</programlisting>
</example>
</section>
</section>
<section id="c09macros">
<title>Macros</title>
<para>
The DB API offers a set of macros to make easier to access the attributes in various data
structures.
</para>
<para>
Macros for <emphasis role="strong">db_res_t</emphasis>:
</para>
<itemizedlist>
<listitem>
<para>
RES_NAMES(res) - get the pointer to column names
</para>
</listitem>
<listitem>
<para>
RES_COL_N(res) - get the number of columns
</para>
</listitem>
<listitem>
<para>
RES_ROWS(res) - get the pointer to rows
</para>
</listitem>
<listitem>
<para>
RES_ROW_N(res) - get the number of rows
</para>
</listitem>
</itemizedlist>
<para>
Macros for <emphasis role="strong">db_val_t</emphasis>:
</para>
<itemizedlist>
<listitem>
<para>
ROW_VALUES(row) - get the pointer to values in the row
</para>
</listitem>