-
Notifications
You must be signed in to change notification settings - Fork 909
/
sqlops_admin.xml
610 lines (594 loc) · 16.6 KB
/
sqlops_admin.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
<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../../doc/docbook/entities.xml">
%docentities;
]>
<!-- Module User's Guide -->
<chapter>
<title>&adminguide;</title>
<section>
<title>Overview</title>
<para>
The SQLOPS module adds support for raw SQL queries in the configuration file.
</para>
<para>
Among the features:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>Multiple database connections</emphasis> - the sqlops module
can connect to many databases on different servers using different DB
driver modules at the same time.
</para>
</listitem>
<listitem>
<para>
<emphasis>Many query results</emphasis> - the module can store
many results of different SQL queries in separate structures at
the same time. Thus it is possible to work in parallel with several
queries and results.
</para>
</listitem>
<listitem>
<para>
<emphasis>Access via pseudo-variables</emphasis> - the content
of the SQL query result is accessible via pseudo-variables. Please
note that only integer and string variables are supported at the
moment because of the internal usage of <quote>AVPs</quote> to hold
the values. So it is not possible for example to return floating
point or big integer values this way.
</para>
</listitem>
<listitem>
<para>
<emphasis>Array indexes</emphasis> - fast access to result values
via array position: [row,column].
</para>
</listitem>
<listitem>
<para>
<emphasis>Persistence in process space</emphasis> - a result can
be used many times in the same worker process. Query once, use many
times.
</para>
</listitem>
<listitem>
<para>
<emphasis>Results can be stored in xavps</emphasis>
- columns are accessed by their names, rows by xavp index. Xavp's
are available during the transactions lifetime and don't need to
be destroyed manually.
</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Dependencies</title>
<section>
<title>&kamailio; Modules</title>
<para>
The following modules must be loaded before this module:
<itemizedlist>
<listitem>
<para>
<emphasis>a DB SQL module (mysql, postgres, ...)</emphasis>.
</para>
</listitem>
</itemizedlist>
</para>
</section>
<section>
<title>External Libraries or Applications</title>
<para>
The following libraries or applications must be installed before running
&kamailio; with this module loaded:
<itemizedlist>
<listitem>
<para>
<emphasis>None</emphasis>.
</para>
</listitem>
</itemizedlist>
</para>
</section>
</section>
<section>
<title>Parameters</title>
<section id="sqlops.p.sqlcon">
<title><varname>sqlcon</varname> (str)</title>
<para>
The definition of a DB connection. The value of the parameter must have
the following format:
</para>
<itemizedlist>
<listitem>
<para>
"connection_name=>database_url"
</para>
</listitem>
</itemizedlist>
<para>
This parameter may be set multiple times to get many DB connections
in the same configuration file.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection_name</emphasis> - string specifying the name
of a database connection. This string is used by the <quote>sql_query()</quote>
function to refer to the DB connection.
</para>
</listitem>
<listitem>
<para>
<emphasis>database_url</emphasis> - Standardized &kamailio; database URL
used to connect to database.
</para>
</listitem>
</itemizedlist>
<para>
<emphasis>
Default value is NULL.
</emphasis>
</para>
<example>
<title>Set <varname>sqlcon</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","cb=>&defaultdb;")
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
</programlisting>
</example>
</section>
<section id="sqlops.p.sqlres">
<title><varname>sqlres</varname> (str)</title>
<para>
The definition of a database result ID. The value of the parameter can be
any string. Results IDs are also added at fixup time when sql_query()
parameters are parsed, so there is no need to declare them via module
parameter unless you want to use them from within other modules and be
available in all application processes.
</para>
<para>
<emphasis>
Default value is NULL.
</emphasis>
</para>
<example>
<title>Set <varname>sqlres</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops", "sqlres", "ra")
...
</programlisting>
</example>
</section>
<section id="sqlops.p.tr_buf_size">
<title><varname>tr_buf_size</varname> (int)</title>
<para>
The size of the transformations for SQL operations.
</para>
<para>
<emphasis>
Default value is 2048.
</emphasis>
</para>
<example>
<title>Set <varname>tr_buf_size</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops", "tr_buf_size", 4096)
...
</programlisting>
</example>
</section>
<section id="sqlops.p.connect_mode">
<title><varname>connect_mode</varname> (int)</title>
<para>
Control if the module must stop loading when connecting to database
server fails during start up. Values: 0 - stop loading; 1 - continue
even if connecting to database server fails.
</para>
<para>
<emphasis>
Default value is 0.
</emphasis>
</para>
<example>
<title>Set <varname>connect_mode</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops", "connect_mode", 1)
...
</programlisting>
</example>
</section>
<section id="sqlops.results_maxsize">
<title><varname>results_maxsize</varname> (int)</title>
<para>
Set the upper limit of how many result containers are going to be
defined.
</para>
<para>
Note that a result container can be used for different SQL queries, it
is not required to have result container per SQL query. Use different
containers when the result records are needed at the same time (e.g.,
compare a field from one result with a field from another result).
</para>
<para>
<emphasis>
Default value is 32.
</emphasis>
</para>
<example>
<title>Set <varname>results_maxsize</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("sqlops", "results_maxsize", 64)
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Functions</title>
<para>
Note that sql_query(), sql_xquery() and sql_pvquery() functions have
the following return values:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>-1</emphasis> - error in parameters or query execution
</para>
</listitem>
<listitem>
<para>
<emphasis>1</emphasis> - query successful, at least one row in resultset (for SELECTs)
</para>
</listitem>
<listitem>
<para>
<emphasis>2</emphasis> - query successful, no rows returned
</para>
<para>
<emphasis>3</emphasis> - query successful, at least one row
returned, but result parameter was not given
</para>
</listitem>
</itemizedlist>
<section>
<title>
<function moreinfo="none">sql_query(connection, query[, result])</function>
</title>
<para>
Make an SQL query using 'connection' and store data in 'result'.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection</emphasis> - the name of the connection
to be used for the query (defined via the <quote>sqlcon</quote> parameter).
</para>
</listitem>
<listitem>
<para>
<emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
</para>
</listitem>
<listitem>
<para>
<emphasis>result</emphasis> - string name to identify the
result. Will be used by $dbr(...) pseudo-variable to access
result attributes.
If omitted, any resultset will be discarded. The result parameter should
normally only be omitted when no result is expected (INSERT, UPDATE, DELETE).
</para>
</listitem>
</itemizedlist>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>sql_query()</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_query("ca", "select * from domain", "ra");
xlog("number of rows in table domain: $dbr(ra=>rows)\n");
sql_result_free("ra");
...
</programlisting>
</example>
</section>
<section>
<title>
<function moreinfo="none">sql_xquery(connection, query, result)</function>
</title>
<para>
Make an SQL query using 'connection' and store data in 'result' xavp.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection</emphasis> - the name of the connection
to be used for the query (defined via the <quote>sqlcon</quote> parameter).
</para>
</listitem>
<listitem>
<para>
<emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
</para>
</listitem>
<listitem>
<para>
<emphasis>result</emphasis> - string name to identify the
result xavp. Each row will be added to this xavp, each column can
be accessed by its name.
</para>
</listitem>
</itemizedlist>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>sql_xquery()</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_xquery("ca", "select * from domain", "ra");
xlog("first domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
...
if (sql_xquery("ca", "select * from domain", "ra") == 1) {
xlog("domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
}
..
</programlisting>
</example>
</section>
<section>
<title>
<function moreinfo="none">sql_pvquery(connection, query, result)</function>
</title>
<para>
Make an SQL query using 'connection' and store data in arbitrary
pseudo variables specified by 'result' parameter.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection</emphasis> - the name of the connection
to be used for query (defined via the <quote>sqlcon</quote> parameter).
</para>
</listitem>
<listitem>
<para>
<emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
</para>
</listitem>
<listitem>
<para>
<emphasis>result</emphasis> - a list with PV names where
to store the result. The format is
<quote>$pv;$pv;...</quote>. Every PV that is writable may
be used (for example $var, $avp, $xavp, $ru, $du, $sht, etc).
</para>
<para>
The PV are assigned values in the following order: last row
to first row, first field to last field. Assignment has the
same behavior as assigning in the script itself with one
exception for avp's, a NULL value will not delete an avp, but
will be skipped over.
</para>
<para>
Beware that if multiple rows are returned, non-(x)avp variables
will only hold the last added value, which corresponds to the
first returned row.
</para>
<para>
The value type of the PV (string or integer) will
be derived from the type of the columns. Please note that only
these two datatypes are supported, other datatypes will/may be
converted to string.
</para>
</listitem>
</itemizedlist>
<para>
This function can be used from ANY_ROUTE. Returns -1 on Error,
1 on successful query, 2 if query provided no result set,
3 if result is ignored (no result parameter given)
</para>
<example>
<title><function>sql_pvquery()</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_pvquery("ca", "select 'col1', 2, NULL, 'sip:test@example.com'",
"$var(a), $avp(col2), $xavp(item[0]=>s), $ru");
...
</programlisting>
</example>
</section>
<section>
<title>
<function moreinfo="none">sql_result_free(result)</function>
</title>
<para>
Free data in SQL 'result'.
</para>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>sql_result_free()</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_query("ca", "select * from domain", "ra");
xlog("number of rows in table domain: $dbr(ra=>rows)\n");
...
sql_result_free("ra");
...
</programlisting>
</example>
</section>
<section id="sqlops.f.sql_query_async">
<title>
<function moreinfo="none">sql_query_async(connection, query)</function>
</title>
<para>
Make an async SQL query using 'connection', if implemented by db
driver module (e.g., db_mysql). The query is executed in another
process and result is not available back to config, thus it should
be used only for sql statements that don't return values (e.g.,
insert, delete, update...). Note that async_workers core parameter
must be set in order to enable the asynchronous framework
needed by this function.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection</emphasis> - the name of the connection
to be used for the query (defined via <quote>sqlcon</quote>
parameter).
</para>
</listitem>
<listitem>
<para>
<emphasis>query</emphasis> - SQL query string or
pseudo-variables containing SQL query.
</para>
</listitem>
</itemizedlist>
<para>
This function can be used from ANY_ROUTE.
</para>
<example>
<title><function>sql_query_async()</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_query_async("ca", "delete from domain");
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Exported pseudo-variables</title>
<section>
<title><varname>$dbr(result=>key)</varname></title>
<para>
Access hash table entries.
</para>
<para>
The <quote>result</quote> must be the name identifying a SQL
result (third parameter of sql_query(...)).
</para>
<para>
The <quote>key</quote> can be:
</para>
<itemizedlist>
<listitem>
<para><emphasis>rows</emphasis> - return the number of rows in
query result</para>
</listitem>
<listitem>
<para><emphasis>cols</emphasis> - return the number of
columns in the result.</para>
</listitem>
<listitem>
<para><emphasis>[row,col]</emphasis> - return the value
at position (row,col) in the result set. 'row' and 'col' must
be integer or pseudo-variable holding an integer.</para>
</listitem>
<listitem>
<para><emphasis>colname[N]</emphasis> - return the name
of the N-th column in the result set.</para>
</listitem>
</itemizedlist>
<example>
<title><function moreinfo="none">$dbr(result=>key)</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_query("ca", "select * from domain", "ra");
xlog("rows: $dbr(ra=>rows) cols: $dbr(ra=>cols)\n");
if($dbr(ra=>rows)>0)
{
$var(i) = 0;
while($var(i)<$dbr(ra=>cols))
{
xlog("--- SCRIPT: column[$var(i)] = $dbr(ra=>colname[$var(i)])\n");
$var(i) = $var(i) + 1;
}
$var(i) = 0;
while($var(i)<$dbr(ra=>rows))
{
$var(j) = 0;
while($var(j)<$dbr(ra=>cols))
{
xlog("[$var(i),$var(j)] = $dbr(ra=>[$var(i),$var(j)])\n");
$var(j) = $var(j) + 1;
}
$var(i) = $var(i) + 1;
}
}
sql_result_free("ra");
...
...
if (sql_xquery("ca", "select * from domain", "ra") == 1)
{
# non-destructive iteration
$var(i) = 0;
while($xavp(ra[$var(i)]) != $null)
{
xlog("[id, domain] = [$xavp(ra[$var(i)]=>id), $xavp(ra[$var(i)]=>domain)]\n");
$var(i) = $var(i) + 1;
}
# destructive iteration
while($xavp(ra) != $null)
{
xlog("[id, domain] = [$xavp(ra=>id), $xavp(ra=>domain)]\n");
pv_unset("$xavp(ra)");
}
}
...
</programlisting>
</example>
</section>
<section>
<title><varname>$sqlrows(con)</varname></title>
<para>
Number of affected rows of the previous query on the
specified connection. Its primary use is to get the number
of rows affected by UPDATE, INSERT and DELETE queries.
</para>
<para>
<quote>con</quote> must be the name identifying a DB
connection.
</para>
<example>
<title><function moreinfo="none">$sqlrows(con)</function> usage</title>
<programlisting format="linespecific">
...
modparam("sqlops","sqlcon","ca=>&exampledb;")
...
sql_query("ca", "update domain set domain='mydomain' where id=5");
xlog("Affected rows: $sqlrows(ca)\n");
...
</programlisting>
</example>
</section>
</section>
</chapter>