File: db2_ops.xml

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (646 lines) | stat: -rw-r--r-- 17,917 bytes parent folder | download | duplicates (4)
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" 
   "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<book id="db2_ops" xmlns:xi="http://www.w3.org/2001/XInclude">
    <bookinfo>
        <title>db2_ops module</title>
	<authorgroup>
	    <author>
		<firstname>Tomas</firstname>
		<surname>Mandys</surname>
		<affiliation><orgname>Iptel.org</orgname></affiliation>
		<address>
		    <email>tomas dot mandys at iptel dot org</email>
		</address>
	    </author>
	</authorgroup>
	<copyright>
	    <year>2007</year>
	    <holder>Tomas Mandys</holder>
	</copyright>

    </bookinfo>
    <toc></toc>

    <chapter>
	<title>Admin Guide</title>
    <section id="db_ops.overview">
		<title>Overview</title>
		<para>
		The module introduces possibility to run SQL queries from script.
		</para>
    </section>

	<section id="db_ops.dep">
		<title>Dependencies</title>
				   
		<para>
		none
		</para>
	</section>

	<section id="db_ops.syntax">
		<title>ABNF syntax</title>
		<programlisting>
	xltext = text_parsed_by_xl_lib
	database = type "://" user:psw "@" host "/" database_name
	field = xltext
	fields = field [ "," field ... ]
	op = "=" | "&lt;" | ">" | "&lt;=" | ">=" | "&lt;>" | "!=" 
	where = fields
	ops = op [ "," op ... ]
	order = field
	type = "s" | "i" | "d" | "f" | "t" ; enables to force particular type when writing to db driver (string/int/double/float/datetime), valueable especially for datetime
	value = [type ":"] xltext
	values = value [ "," value ...]
	extra_op = name "=" [type ":"] text
	extra_ops = extra_op [ "," extra_op]

	select = [database "/"] "select/" table "/" fields "/" where "/" ops "/" order "/" values [ "/" extra_ops ]
	insert = [database "/"] "insert/" table "/" fields "/" values [ "/" extra_ops ]
	update = [database "/"] "update/" table "/" fields "/" where "/" ops "/" values [ "/" extra_ops ]
	replace = [database "/"] "replace/" table "/" fields "/" values [ "/" extra_ops ]
	delete = [database "/"] "delete/" table "/" where "/" ops "/" values [ "/" extra_ops ]
	raw_query = [database "/"] "select ...." | "insert ..." [[ / "values" [ "/" extra_ops ]]  # not delimited by "/"
	query = (select | insert | update | replace | delete | raw_query)
	query_id = alphanum
	handle = alphanum  (plain text possible but alphanum recommended)
	declare_query_param = query_id "=" query
	declare_handle_param = handle
		</programlisting>
	</section>

	<section id="db_ops.parameters">

		<title>Parameters</title>

		<section id="db_url">
			<title><varname>db_url</varname> (string)</title>
			<para>
			Default database URL. 
			</para>
			<para>
			The format is:
			</para>
			<programlisting>
			db_url = database
			</programlisting>
			<example>
				<title>Example <varname>db_url</varname></title>
				<programlisting>
	...
	modparam("db_ops", "db_url", "mysql://ser:123@127.0.0.1:12345/ser");
	...
				</programlisting>
			</example>
		</section>

		<section id="declare_query">
			<title><varname>declare_query</varname> (string)</title>
			<para>
			Declare query_id for <varname>@db.query_id</varname> (see select syntax) or for reference from <function>db_query(query_id)</function>.
			Queries are pre-compiled therefore volatile stuff must be passed via parameters (AVP or so).
			</para>
			<para>
			The format is:
			</para>
			<programlisting>
			declare_query = declare_query_param
			</programlisting>
			<example>
				<title>Example <varname>declare_query</varname></title>
				<programlisting>
	...
	modparam("db_ops", "declare_query", "sel1=select/location/received/uid///%$f.uid");
	...
				</programlisting>
			</example>
		</section>

		<section id="declare_handle">
			<title><varname>declare_handle</varname> (string)</title>
			<para>
			Declare handle for fetching.
			</para>
			<para>
			The format is:
			</para>
			<programlisting>
			declare_handle = declare_handle_param
			</programlisting>
			<example>
				<title>Example <varname>declare_handle</varname></title>
				<programlisting>
	...
	modparam("db_ops", "declare_handle", "my_handle");
	...
				</programlisting>
			</example>
		</section>
	</section>

	<section id="db_ops.functions">
		<title>Functions</title>

		<section id="db_query">
			<title>
				<function>db_query(query | query_id [,handle] )</function>
			</title>
			<para>
				Executes query and in case of SELECT returns result via <emphasis>handle</emphasis>, seeks the first 
				record and returns TRUE if table is not empty.
				The result is accesible using <function>@db.fetch</function> select. See also <varname>declare_handle</varname>.
				<emphasis>Query_id</emphasis> references to query declared using <varname>declare_query</varname>,
				<emphasis>handle</emphasis> references to query declared using <varname>declare_handle</varname>.
			</para>

			<example>
				<title><function>db_query</function> usage</title>
				<programlisting>
	...
	db_query("delete/silo///");
	if (db_query("select/silo/body/uid//inc_time/%$f.uid", my_handle)) { 	
	...
	}
	...
	if (db_query(sel1, my_handle)) {

	}
				</programlisting>
			</example>
		</section>

		<section id="db_close">
			<title>
				<function>db_close(handle)</function>
			</title>
			<para>
				Close table that has been opened using <function>db_query</function>.
				Note all close after script processing automatically.
			</para>
			<example>
				<title><function>db_close</function> usage</title>
				<programlisting>
	...
	db_close(my_handle);
	...
				</programlisting>
			</example>
		</section>

		<section id="db_first">
			<title>
				<function>db_first(handle)</function>
			</title>
			<para>
				Returns TRUE if table is not empty. Note that rewind might not be supported by particular db driver.
			</para>
			<example>
				<title><function>db_first</function> usage</title>
				<programlisting>
	...
	if (db_first(my_handle)) {
	...
	}
	...
				</programlisting>
			</example>
		</section>

		<section id="db_next">
			<title>
				<function>db_next(handle)</function>
			</title>
			<para>
				Moves to the next record and returns TRUE if not EOF.
			</para>
			<example>
				<title><function>db_next</function> usage</title>
				<programlisting>
	...
	if (db_next(my_handle)) {
	...
	}
	...
				</programlisting>
			</example>
		</section>

		<section id="db_seek">
			<title>
				<function>db_seek(handle, row_no)</function>
			</title>
			<para>
				Seeks at the row no (origin is zero) and Returns TRUE in case of success. Backward seek might not be supported by db driver.
			</para>
			<example>
				<title><function>db_seek</function> usage</title>
				<programlisting>
	...
	if (db_seek(my_handle, $row_no)) {
	...
	}
	...
				</programlisting>
			</example>
		</section>

		<section id="db_foreach">
			<title>
				<function>db_foreach(handle, route)</function>
			</title>
			<para>
				Call specific route for each row, loop is interrupted if route returns code &lt;= 0.
				Return code of the last route call is returned as result of <function>db_foreach</function> (or -1 when no select is empty).
			</para>
			<example>
				<title><function>db_foreach</function> usage</title>
				<programlisting>

	route["print_row"] {
	....
	}
	
	...
	if (db_foreach(my_handle, print_row)) {
	...
	}
	...
				</programlisting>
			</example>
		</section>

		<section id="db_proper">
			<title>
				<function>db_proper()</function>
			</title>
			<para>
				Hack which enables using db_ops queries in failure route.
				Call it at the beginning of <emphasis>failure_route</emphasis> block.
			</para>
			<example>
				<title><function>db_proper</function> usage</title>
				<programlisting>

	failure_route["my_failure"] {
		db_proper();
		db_query(my_query, my_handle);		
	....
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id">
			<title>
				<function>@db.query.query_id</function>
			</title>
			<para>
				Returns value of the first row and the first field.
			</para>

			<example>
				<title><function>db.query.query_id</function> usage</title>
				<programlisting>

	if (@db.query.my_query == "my") {
	....
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id.count">
			<title>
				<function>@db.query.query_id.count</function>
			</title>
			<para>
				Returns number of rows in select query.
			</para>

			<example>
				<title><function>db.query.query_id.count</function> usage</title>
				<programlisting>

	if (@db.query.my_query.count == "2") {
	....
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id.is_empty">
			<title>
				<function>@db.query.query_id.is_empty</function>
			</title>
			<para>
				Returns 1 if select query is empty.
			</para>

			<example>
				<title><function>db.query.query_id.is_empty</function> usage</title>
				<programlisting>

	if (@db.query.my_query.is_query == "1") {
		# query is empty
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id.field">
			<title>
				<function>@db.query.query_id.field[m]</function>
			</title>
			<para>
				Returns value of the first row and the m-th field.
				<emphasis>@*.field</emphasis> supports <emphasis>select_any_uri</emphasis> and <emphasis>select_any_nameaddr</emphasis>.
			</para>

			<example>
				<title><function>db.query.query_id.field[m]</function> usage</title>
				<programlisting>

	if (@db.query.my_query.field[2] == "xyz") {
		...
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id.row">
			<title>
				<function>@db.query.query_id.row[n]</function>
			</title>
			<para>
				Returns value of the n-th row and the first field, negative values count from the end (-1 == last row).
			</para>

			<example>
				<title><function>db.query.query_id.row[n]</function> usage</title>
				<programlisting>

	if (@db.query.my_query.row[2] == "xyz") {
		...
	}
				</programlisting>
			</example>
		</section>

		<section id="db.query.query_id.row.field">
			<title>
				<function>@db.query.query_id.row[n].field[m]</function>
			</title>
			<para>
				Returns value of the n-th row and the m-th field.
				<emphasis>@*.field</emphasis> supports <emphasis>select_any_uri</emphasis> and <emphasis>select_any_nameaddr</emphasis>.
			</para>

			<example>
				<title><function>db.query.query_id.row[n].field[m]</function> usage</title>
				<programlisting>

	if (@db.query.my_query.row[2].field[1] == "xyz") {
		...
	}
				</programlisting>
			</example>
		</section>

		
		<section id="db.fetch.handle">
			<title>
				<function>@db.fetch.handle</function>
			</title>
			<para>
				Similar functionality as <function>@db.query</function> selects with exception that 
				operation is performed at query has been opened by <function>db_query</function>.
			</para>
			<para>
				<function>@db.fetch.handle.count</function> invalidates current record, do <function>db_seek</function>/<function>db_first</function>!
			</para>
			<para>
				 Note all opened queries are closed in POST_SCRIPT callback not to leak memory.
			</para>

			<example>
				<title><function>db.fetch.handle</function> usage</title>
				<programlisting>

	db_query(sel1, my_handle);
	if (@db.fetch.my_handle.is_empty == "0") {
		if (@db.fetch.my_handle == "xyz") {

		}
	}
	db_close(my_handle);
	
				</programlisting>
			</example>
		</section>

		<section id="db.fetch.handle.row_no">
			<title>
				<function>@db.fetch.handle.row_no</function>
			</title>
			<para>
				Returns current row number (origin is zero).
			</para>

			<example>
				<title><function>db.fetch.handle.row_no</function> usage</title>
				<programlisting>

	db_query(sel1, my_handle);
	...
	db_next(my_handle);
	if (@db.handle == "xyz") {
		db_next(my_handle);
	}
	if (@db.fetch.my_handle.row_no == "1") {

		}
	}
	if (@db.fetch.my_handle.count == "10") {
		...
	}
	if (@db.fetch.my_handle.row_no == "1") {	# always false because .count has invalidated current record!

	}	
	db_close(my_handle);
	
				</programlisting>
			</example>
		</section>

	</section>

        <section id="db_ops.examples">
		<title>Examples</title>
		<example>
			<title>db_ops common example</title>
			<programlisting>
modparam("db_ops", "declare_query", "sel1=select/location/received/uid///%$f.uid");  
modparam("db_ops", "declare_query", "sel2=select/subscriber/email_address,greeting/uid,allow_find///%$uidparam,1");  
modparam("db_ops", "declare_query", "sel3=select/silo/body/uid//inc_time/%$f.uid");  
modparam("db_ops", "declare_query", "del1=delete from location where expires&lt;now()"); # raw query

# @db.query.sel1             ..  SELECT received FROM location WHERE uid = "%$f.uid"
# @db.query.sel1.count       ..  SELECT count(*) FROM location WHERE uid = "%$f.uid"
# @db.query.sel2.field[0]    ..  SELECT email_address FROM subscriber WHERE uid = "%$f.uid" AND allow_find=1
# @db.query.sel2.field[1]    ..  SELECT greeting FROM subscriber WHERE uid = "%$f.uid" AND allow_find=1
# @db.query.sel3.count       ..  SELECT count(*) FROM silo WHERE uid = "%$f.uid"
# @db.query.sel1.is_empty


db_query("delete/silo///");     #  DELETE FROM silo
db_query("delete/silo/expired/&lt;=/%Ts");     #  DELETE FROM silo WHERE expired &lt;= now;
db_query("insert/foo/bar,rab,abr,rbs/%$f.id,'hello world %fu',1,2");  # INSERT INTO foo(bar,rab,abr,rbs) VALUES ("%$f.id","hello world %fu",1,2)
db_query("update/foo/rab,abr/bar//'hello world %f',1,2,%$f.id");      # UPDATE foo SET rab="hello world %fu",rbs=45 WHERE bar="%$f.id"

db_query("mysql://pretorian:sandra@net/delete/fbi/identities//");

if (db_query("select/silo/body/uid//inc_time/%$f.uid", my_handle)) { #  SELECT body FROM silo WHERE uid = "%$f.uid" ORDER BY inc_time
    @db.fetch.my_handle             ..  get first raw
    if (db_next(my_handle)) {
	@db.fetch.my_handle      ..  get second raw
    }
}

if (db_query("select/silo/src_addr,dest_addr,body/uid//inc_time/%$t.uid", my_next_handle)) { # SELECT src_addr,dest_addr,body FROM silo WHERE uid = "%$t.uid" ORDER BY inc_time
    @db.fetch.my_next_handle.row[-1].field[1]	.. get last dest_addr, not supported now!
}
db_close(my_handle);
db_close(my_next_handle);

# parametrization of queries
$uidparam="xx";
@db.query.sel2

$uidparam="yy";
@db.query.sel2

$uidparam="zz";
db_query(sel2, my_handle);

$uidparam="qq";
db_query(sel2, my_next_handle);
if (@db.fetch.my_handle == @db.fetch.my_next_handle) ....

db_close(my_handle);
db_close(my_next_handle);

db_query(sel3, my_handle);
forach(my_handle, PROCESS_ROW_ROUTE);

			</programlisting>

			<title>db_ops example with timer</title>
			<programlisting>
loadmodule "mysql.so"
loadmodule "xprint.so"
loadmodule "db_ops.so"
loadmodule "timer.so"

modparam("timer", "declare_timer", "timer_route,1000,,enable");

# -------------------------  request routing logic -------------------

modparam("db_ops", "db_url", "mysql://admin:123456789@127.0.0.1:12345/ser");

modparam("db_ops", "declare_query", "q1=select/location/uid,aor,contact,received//////"); #key=location_key,key_omit=i:3");
modparam("db_ops", "declare_query", "q2=select/location/uid,aor,contact,received/uid///%$f.uid/key=location_key,key_omit=i:1");
# select raw query not yet supported
modparam("db_ops", "declare_query", "q3=select uid,aor,contact,received from location where uid=?/%$f.uid");
modparam("db_ops", "declare_query", "q4=mysql://admin:123456789@127.0.0.1:12345/ser/replace/location/uid,aor,contact,received,expires,q,callid,cseq,user_agent,instance,path,service_route,assoc_uri,flags,nated_contact,term_toi/QWERTY,aor@qqq,1.2.3.4:5678,1.2.3.4:5678;proto=tcp,d:1000,0.8,CAALL,6543,bubak,INSTANCE@bubak,sip:path_to_localhost,,,0,1,,");
modparam("db_ops", "declare_query", "q5=mysql://admin:123456789@127.0.0.1:12345/ser/delete from location where uid=?/s:QWERTY");

modparam("db_ops", "declare_handle", "h0");
modparam("db_ops", "declare_handle", "h1");

route["print_count"] {
# testing count
	xplog("L_INFO", "print count\n");
	db_query("q1", "h1");
	xplog("L_INFO", "fetch: row_no: %@db.fetch.h1.row_no, count: %@db.fetch.h1.count, row_no: %@db.fetch.h1.row_no, is_empty: %@db.fetch.h1.is_empty, row_no: %@db.fetch.h1.row_no\n");

	xplog("L_INFO", "query: is_empty: %@db.query.q1.is_empty, count: %@db.query.q1.count\n");
}

route["print_record"] {
	xplog("L_INFO","row_no: %@db.fetch.h0.row_no, record: %@db.fetch.h0.field[0], %@db.fetch.h0.field[1],  %@db.fetch.h0.field[2],  %@db.fetch.h0.field[3]\n");

}

route["print_tbl"] {
	route("print_record");
	if (!db_next("h0")) return;
	route("print_record");
	if (!db_next("h0")) return;
	route("print_record");
	if(!db_seek("h0", 5)) return;
	route("print_record");
	if (!db_first("h0")) return;
	route("print_record");

}

route["db_test"] {

	route("print_count");
	
	$f.uid="QWERTY";
	xplog("L_INFO", "query #1\n");
	if (db_query("q1", "h0")) {
		route("print_tbl");
	}
	db_close("h0");

	xplog("L_INFO", "replace\n"); 
	db_query("q4");

	xplog("L_INFO", "query #2\n");
	if (db_query("q2", "h0")) {
		route("print_tbl");
	}
	db_close("h0");

	xplog("L_INFO", "query #3\n");
	if (db_query("q3", "h0")) {
		route("print_tbl");
	}
	db_close("h0");

	route("print_count");


	db_query("q5");
	route("print_count");

	
	xplog("L_INFO", "foreach\n");
	db_query("q1", "h0");
	db_foreach("h0", "print_record");

	xplog("L_INFO", "query test\n");
	xplog("L_INFO", "#0: %@db.query.q1.row[0].field[0], %@db.query.q1.row[0].field[1], %@db.query.q1.row[0].field[2], %@db.query.q1.row[0].field[3]\n");
	xplog("L_INFO", "#1: %@db.query.q1.row[1].field[0], %@db.query.q1.row[1].field[1], %@db.query.q1.row[1].field[2], %@db.query.q1.row[1].field[3]\n");
	xplog("L_INFO", "#3: %@db.query.q1.row[3].field[0], %@db.query.q1.row[3].field[1], %@db.query.q1.row[3].field[2], %@db.query.q1.row[3].field[3]\n");


}

route["timer_route"] {
	xplog("L_INFO", "\n\n\ntimer\n");
	route("db_test");

	timer_enable(0, 0);
	xplog("L_INFO", "\n\n\n");
}
			</programlisting>
		</example>
	</section>
    </chapter>
</book>