Debian Codegraph @

src/modules/sqlops/README

raw
1 SQLOps Module
2
3 Daniel-Constantin Mierla
4
5 asipto.com
6 <[email protected]>
7
8 Edited by
9
10 Daniel-Constantin Mierla
11
12 <[email protected]>
13
14 Copyright © 2008 http://www.asipto.com
15 __________________________________________________________________
16
17 Table of Contents
18
19 1. Admin Guide
20
21 1. Overview
22 2. Dependencies
23
24 2.1. Kamailio Modules
25 2.2. External Libraries or Applications
26
27 3. Parameters
28
29 3.1. sqlcon (str)
30 3.2. sqlres (str)
31 3.3. tr_buf_size (int)
32 3.4. log_buf_size (int)
33 3.5. connect_mode (int)
34 3.6. results_maxsize (int)
35
36 4. Functions
37
38 4.1. sql_query(connection, query[, result])
39 4.2. sql_xquery(connection, query, result)
40 4.3. sql_pvquery(connection, query, result)
41 4.4. sql_result_free(result)
42 4.5. sql_query_async(connection, query)
43
44 5. Exported pseudo-variables
45
46 5.1. $dbr(result=>key)
47 5.2. $sqlrows(con)
48
49 List of Examples
50
51 1.1. Set sqlcon parameter
52 1.2. Set sqlres parameter
53 1.3. Set tr_buf_size parameter
54 1.4. Set log_buf_size parameter
55 1.5. Set connect_mode parameter
56 1.6. Set results_maxsize parameter
57 1.7. sql_query() usage
58 1.8. sql_xquery() usage
59 1.9. sql_pvquery() usage
60 1.10. sql_result_free() usage
61 1.11. sql_query_async() usage
62 1.12. $dbr(result=>key) usage
63 1.13. $sqlrows(con) usage
64
65 Chapter 1. Admin Guide
66
67 Table of Contents
68
69 1. Overview
70 2. Dependencies
71
72 2.1. Kamailio Modules
73 2.2. External Libraries or Applications
74
75 3. Parameters
76
77 3.1. sqlcon (str)
78 3.2. sqlres (str)
79 3.3. tr_buf_size (int)
80 3.4. log_buf_size (int)
81 3.5. connect_mode (int)
82 3.6. results_maxsize (int)
83
84 4. Functions
85
86 4.1. sql_query(connection, query[, result])
87 4.2. sql_xquery(connection, query, result)
88 4.3. sql_pvquery(connection, query, result)
89 4.4. sql_result_free(result)
90 4.5. sql_query_async(connection, query)
91
92 5. Exported pseudo-variables
93
94 5.1. $dbr(result=>key)
95 5.2. $sqlrows(con)
96
97 1. Overview
98
99 The SQLOPS module adds support for raw SQL queries in the configuration
100 file.
101
102 Among the features:
103 * Multiple database connections - the sqlops module can connect to
104 many databases on different servers using different DB driver
105 modules at the same time.
106 * Many query results - the module can store many results of different
107 SQL queries in separate structures at the same time. Thus it is
108 possible to work in parallel with several queries and results.
109 * Access via pseudo-variables - the content of the SQL query result
110 is accessible via pseudo-variables. Please note that only integer
111 and string variables are supported at the moment because of the
112 internal usage of “AVPs” to hold the values. So it is not possible
113 for example to return floating point or big integer values this
114 way.
115 * Array indexes - fast access to result values via array position:
116 [row,column].
117 * Persistence in process space - a result can be used many times in
118 the same worker process. Query once, use many times.
119 * Results can be stored in xavps - columns are accessed by their
120 names, rows by xavp index. Xavp's are available during the
121 transactions lifetime and don't need to be destroyed manually.
122
123 2. Dependencies
124
125 2.1. Kamailio Modules
126 2.2. External Libraries or Applications
127
128 2.1. Kamailio Modules
129
130 The following modules must be loaded before this module:
131 * a DB SQL module (mysql, postgres, ...).
132
133 2.2. External Libraries or Applications
134
135 The following libraries or applications must be installed before
136 running Kamailio with this module loaded:
137 * None.
138
139 3. Parameters
140
141 3.1. sqlcon (str)
142 3.2. sqlres (str)
143 3.3. tr_buf_size (int)
144 3.4. log_buf_size (int)
145 3.5. connect_mode (int)
146 3.6. results_maxsize (int)
147
148 3.1. sqlcon (str)
149
150 The definition of a DB connection. The value of the parameter must have
151 the following format:
152 * "connection_name=>database_url"
153
154 This parameter may be set multiple times to get many DB connections in
155 the same configuration file.
156 * connection_name - string specifying the name of a database
157 connection. This string is used by the “sql_query()” function to
158 refer to the DB connection.
159 * database_url - Standardized Kamailio database URL used to connect
160 to database.
161
162 Default value is NULL.
163
164 Example 1.1. Set sqlcon parameter
165 ...
166 modparam("sqlops","sqlcon","cb=>mysql://kamailio:kamailiorw@localhost/kamailio")
167 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
168 ...
169
170 3.2. sqlres (str)
171
172 The definition of a database result ID. The value of the parameter can
173 be any string. Results IDs are also added at fixup time when
174 sql_query() parameters are parsed, so there is no need to declare them
175 via module parameter unless you want to use them from within other
176 modules and be available in all application processes.
177
178 Default value is NULL.
179
180 Example 1.2. Set sqlres parameter
181 ...
182 modparam("sqlops", "sqlres", "ra")
183 ...
184
185 3.3. tr_buf_size (int)
186
187 The size of the transformations for SQL operations.
188
189 Default value is 2048.
190
191 Example 1.3. Set tr_buf_size parameter
192 ...
193 modparam("sqlops", "tr_buf_size", 4096)
194 ...
195
196 3.4. log_buf_size (int)
197
198 The size of the buffer (characters) when logging raw SQL operations.
199
200 Note: When the buffer is smaller than the SQL operation, the operation
201 is logged truncated up to log_buf_size.
202
203 Default value is 128.
204
205 Example 1.4. Set log_buf_size parameter
206 ...
207 modparam("sqlops", "log_buf_size", 4096)
208 ...
209
210 3.5. connect_mode (int)
211
212 Control how the module will connect to database. Values:
213 * 0 connect at start up or fail
214 * 1 connect at start up but continue even if connecting to database
215 server fails
216 * 2 do not connect until needed (this will limit the amount of idle
217 connections)
218
219 Default value is 0.
220
221 Example 1.5. Set connect_mode parameter
222 ...
223 modparam("sqlops", "connect_mode", 1)
224 ...
225
226 3.6. results_maxsize (int)
227
228 Set the upper limit of how many result containers are going to be
229 defined.
230
231 Note that a result container can be used for different SQL queries, it
232 is not required to have result container per SQL query. Use different
233 containers when the result records are needed at the same time (e.g.
234 compare a field from one result with a field from another result).
235
236 Default value is 32.
237
238 Example 1.6. Set results_maxsize parameter
239 ...
240 modparam("sqlops", "results_maxsize", 64)
241 ...
242
243 4. Functions
244
245 4.1. sql_query(connection, query[, result])
246 4.2. sql_xquery(connection, query, result)
247 4.3. sql_pvquery(connection, query, result)
248 4.4. sql_result_free(result)
249 4.5. sql_query_async(connection, query)
250
251 Note that sql_query(), sql_xquery() and sql_pvquery() functions have
252 the following return values:
253 * -1 - error in parameters or query execution
254 * 1 - query successful, at least one row in resultset (for SELECTs)
255 * 2 - query successful, no rows returned
256 * 3 - query successful, at least one row returned, but result
257 parameter was not given
258
259 4.1. sql_query(connection, query[, result])
260
261 Make an SQL query using 'connection' and store data in 'result'.
262 * connection - the name of the connection to be used for the query
263 (defined via the “sqlcon” parameter).
264 * query - SQL query string or pseudo-variables containing SQL query.
265 * result - string name to identify the result. Will be used by
266 $dbr(...) pseudo-variable to access result attributes. If omitted,
267 any resultset will be discarded. The result parameter should
268 normally only be omitted when no result is expected (INSERT,
269 UPDATE, DELETE).
270
271 This function can be used from ANY_ROUTE.
272
273 Example 1.7. sql_query() usage
274 ...
275 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
276 ...
277 sql_query("ca", "select * from domain", "ra");
278 xlog("number of rows in table domain: $dbr(ra=>rows)\n");
279 sql_result_free("ra");
280 ...
281
282 4.2. sql_xquery(connection, query, result)
283
284 Make an SQL query using 'connection' and store data in 'result' xavp.
285 * connection - the name of the connection to be used for the query
286 (defined via the “sqlcon” parameter).
287 * query - SQL query string or pseudo-variables containing SQL query.
288 * result - string name to identify the result xavp. Each row will be
289 added to this xavp, each column can be accessed by its name.
290
291 This function can be used from ANY_ROUTE.
292
293 Example 1.8. sql_xquery() usage
294 ...
295 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
296 ...
297 sql_xquery("ca", "select * from domain", "ra");
298 xlog("first domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
299 ...
300 if (sql_xquery("ca", "select * from domain", "ra") == 1) {
301 xlog("domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
302 }
303 ..
304
305 4.3. sql_pvquery(connection, query, result)
306
307 Make an SQL query using 'connection' and store data in arbitrary pseudo
308 variables specified by 'result' parameter.
309 * connection - the name of the connection to be used for query
310 (defined via the “sqlcon” parameter).
311 * query - SQL query string or pseudo-variables containing SQL query.
312 * result - a list with PV names where to store the result. The format
313 is “$pv;$pv;...”. Every PV that is writable may be used (for
314 example $var, $avp, $xavp, $ru, $du, $sht, etc).
315 The PV are assigned values in the following order: last row to
316 first row, first field to last field. Assignment has the same
317 behavior as assigning in the script itself with one exception for
318 avp's, a NULL value will not delete an avp, but will be skipped
319 over.
320 Beware that if multiple rows are returned, non-(x)avp variables
321 will only hold the last added value, which corresponds to the first
322 returned row.
323 The value type of the PV (string or integer) will be derived from
324 the type of the columns. Please note that only these two datatypes
325 are supported, other datatypes will/may be converted to string.
326
327 This function can be used from ANY_ROUTE. Returns -1 on Error, 1 on
328 successful query, 2 if query provided no result set, 3 if result is
329 ignored (no result parameter given)
330
331 Example 1.9. sql_pvquery() usage
332 ...
333 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
334 ...
335 sql_pvquery("ca", "select 'col1', 2, NULL, 'sip:[email protected]'",
336 "$var(a), $avp(col2), $xavp(item[0]=>s), $ru");
337 ...
338
339 4.4. sql_result_free(result)
340
341 Free data in SQL 'result'.
342
343 This function can be used from ANY_ROUTE.
344
345 Example 1.10. sql_result_free() usage
346 ...
347 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
348 ...
349 sql_query("ca", "select * from domain", "ra");
350 xlog("number of rows in table domain: $dbr(ra=>rows)\n");
351 ...
352 sql_result_free("ra");
353 ...
354
355 4.5. sql_query_async(connection, query)
356
357 Make an async SQL query using 'connection', if implemented by db driver
358 module (e.g. db_mysql). The query is executed in another process and
359 result is not available back to config, thus it should be used only for
360 sql statements that don't return values (e.g. insert, delete,
361 update...). Note that async_workers core parameter must be set in order
362 to enable the asynchronous framework needed by this function.
363 * connection - the name of the connection to be used for the query
364 (defined via “sqlcon” parameter).
365 * query - SQL query string or pseudo-variables containing SQL query.
366
367 This function can be used from ANY_ROUTE.
368
369 Example 1.11. sql_query_async() usage
370 ...
371 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
372 ...
373 sql_query_async("ca", "delete from domain");
374 ...
375
376 5. Exported pseudo-variables
377
378 5.1. $dbr(result=>key)
379 5.2. $sqlrows(con)
380
381 5.1. $dbr(result=>key)
382
383 Access hash table entries.
384
385 The “result” must be the name identifying a SQL result (third parameter
386 of sql_query(...)).
387
388 The “key” can be:
389 * rows - return the number of rows in query result
390 * cols - return the number of columns in the result.
391 * [row,col] - return the value at position (row,col) in the result
392 set. 'row' and 'col' must be integer or pseudo-variable holding an
393 integer.
394 * colname[N] - return the name of the N-th column in the result set.
395
396 Example 1.12. $dbr(result=>key) usage
397 ...
398 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
399 ...
400 sql_query("ca", "select * from domain", "ra");
401 xlog("rows: $dbr(ra=>rows) cols: $dbr(ra=>cols)\n");
402 if($dbr(ra=>rows)>0)
403 {
404 $var(i) = 0;
405 while($var(i)<$dbr(ra=>cols))
406 {
407 xlog("--- SCRIPT: column[$var(i)] = $dbr(ra=>colname[$var(i)])\n");
408 $var(i) = $var(i) + 1;
409 }
410 $var(i) = 0;
411 while($var(i)<$dbr(ra=>rows))
412 {
413 $var(j) = 0;
414 while($var(j)<$dbr(ra=>cols))
415 {
416 xlog("[$var(i),$var(j)] = $dbr(ra=>[$var(i),$var(j)])\n");
417 $var(j) = $var(j) + 1;
418 }
419 $var(i) = $var(i) + 1;
420 }
421 }
422 sql_result_free("ra");
423 ...
424
425
426 ...
427 if (sql_xquery("ca", "select * from domain", "ra") == 1)
428 {
429 # non-destructive iteration
430 $var(i) = 0;
431 while($xavp(ra[$var(i)]) != $null)
432 {
433 xlog("[id, domain] = [$xavp(ra[$var(i)]=>id), $xavp(ra[$var(i)]=>domain)
434 ]\n");
435 $var(i) = $var(i) + 1;
436 }
437
438 # destructive iteration
439 while($xavp(ra) != $null)
440 {
441 xlog("[id, domain] = [$xavp(ra=>id), $xavp(ra=>domain)]\n");
442 pv_unset("$xavp(ra)");
443 }
444 }
445 ...
446
447 5.2. $sqlrows(con)
448
449 Number of affected rows of the previous query on the specified
450 connection. Its primary use is to get the number of rows affected by
451 UPDATE, INSERT and DELETE queries.
452
453 “con” must be the name identifying a DB connection.
454
455 Example 1.13. $sqlrows(con) usage
456 ...
457 modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
458 ...
459 sql_query("ca", "update domain set domain='mydomain' where id=5");
460 xlog("Affected rows: $sqlrows(ca)\n");
461 ...