sqlops_admin.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <para>
  14. The SQLOPS module adds support for raw SQL queries in the configuration file.
  15. </para>
  16. <para>
  17. Among the features:
  18. </para>
  19. <itemizedlist>
  20. <listitem>
  21. <para>
  22. <emphasis>Multiple database connections</emphasis> - the sqlops module
  23. can connect to many databases on different servers using different DB
  24. driver modules at the same time.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <emphasis>Many query results</emphasis> - the module can store
  30. many results of different SQL queries in separate structures at
  31. the same time. Thus it is possible to work in parallel with several
  32. queries and results.
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. <emphasis>Access via pseudo-variables</emphasis> - the content
  38. of the SQL query result is accessible via pseudo-variables. Please
  39. note that only integer and string variables are supported at the
  40. moment because of the internal usage of <quote>AVPs</quote> to hold
  41. the values. So it is not possible for example to return floating
  42. point or big integer values this way.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <emphasis>Array indexes</emphasis> - fast access to result values
  48. via array position: [row,column].
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. <emphasis>Persistence in process space</emphasis> - a result can
  54. be used many times in the same worker process. Query once, use many
  55. times.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. <emphasis>Results can be stored in xavps</emphasis>
  61. - columns are accessed by their names, rows by xavp index. Xavp's
  62. are available during the transactions lifetime and don't need to
  63. be destroyed manually.
  64. </para>
  65. </listitem>
  66. </itemizedlist>
  67. </section>
  68. <section>
  69. <title>Dependencies</title>
  70. <section>
  71. <title>&kamailio; Modules</title>
  72. <para>
  73. The following modules must be loaded before this module:
  74. <itemizedlist>
  75. <listitem>
  76. <para>
  77. <emphasis>a DB SQL module (mysql, postgres, ...)</emphasis>.
  78. </para>
  79. </listitem>
  80. </itemizedlist>
  81. </para>
  82. </section>
  83. <section>
  84. <title>External Libraries or Applications</title>
  85. <para>
  86. The following libraries or applications must be installed before running
  87. &kamailio; with this module loaded:
  88. <itemizedlist>
  89. <listitem>
  90. <para>
  91. <emphasis>None</emphasis>.
  92. </para>
  93. </listitem>
  94. </itemizedlist>
  95. </para>
  96. </section>
  97. </section>
  98. <section>
  99. <title>Parameters</title>
  100. <section>
  101. <title><varname>sqlcon</varname> (str)</title>
  102. <para>
  103. The definition of a DB connection. The value of the parameter must have
  104. the following format:
  105. </para>
  106. <itemizedlist>
  107. <listitem>
  108. <para>
  109. "connection_name=&gt;database_url"
  110. </para>
  111. </listitem>
  112. </itemizedlist>
  113. <para>
  114. This parameter may be set multiple times to get many DB connections
  115. in the same configuration file.
  116. </para>
  117. <itemizedlist>
  118. <listitem>
  119. <para>
  120. <emphasis>connection_name</emphasis> - string specifying the name
  121. of a database connection. This string is used by the <quote>sql_query()</quote>
  122. function to refer to the DB connection.
  123. </para>
  124. </listitem>
  125. <listitem>
  126. <para>
  127. <emphasis>database_url</emphasis> - Standardized &kamailio; database URL
  128. used to connect to database.
  129. </para>
  130. </listitem>
  131. </itemizedlist>
  132. <para>
  133. <emphasis>
  134. Default value is NULL.
  135. </emphasis>
  136. </para>
  137. <example>
  138. <title>Set <varname>sqlcon</varname> parameter</title>
  139. <programlisting format="linespecific">
  140. ...
  141. modparam("sqlops","sqlcon","cb=&gt;mysql://kamailio:[email protected]/testdb")
  142. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  143. ...
  144. </programlisting>
  145. </example>
  146. </section>
  147. <section>
  148. <title><varname>sqlres</varname> (str)</title>
  149. <para>
  150. The definition of a database result ID. The value of the parameter can be
  151. any string. Results IDs are also added at fixup time when sql_query()
  152. parameters are parsed, so there is no need to decalare them via module
  153. parameter unless you want to use them from within other modules and be
  154. available in all application processes.
  155. </para>
  156. <para>
  157. <emphasis>
  158. Default value is NULL.
  159. </emphasis>
  160. </para>
  161. <example>
  162. <title>Set <varname>sqlres</varname> parameter</title>
  163. <programlisting format="linespecific">
  164. ...
  165. modparam("sqlops", "sqlres", "ra")
  166. ...
  167. </programlisting>
  168. </example>
  169. </section>
  170. </section>
  171. <section>
  172. <title>Functions</title>
  173. <para>
  174. Note that sql_query(), sql_xquery() and sql_pvquery() functions have
  175. the following return values:
  176. </para>
  177. <itemizedlist>
  178. <listitem>
  179. <para>
  180. <emphasis>-1</emphasis> - error in parameters or query execution
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. <emphasis>1</emphasis> - query successful, at least one row in resultset (for SELECTs)
  186. </para>
  187. </listitem>
  188. <listitem>
  189. <para>
  190. <emphasis>2</emphasis> - query successful, no rows returned
  191. </para>
  192. </listitem>
  193. </itemizedlist>
  194. <section>
  195. <title>
  196. <function moreinfo="none">sql_query(connection, query[, result])</function>
  197. </title>
  198. <para>
  199. Make an SQL query using 'connection' and store data in 'result'.
  200. </para>
  201. <itemizedlist>
  202. <listitem>
  203. <para>
  204. <emphasis>connection</emphasis> - the name of the connection
  205. to be used for the query (defined via the <quote>sqlcon</quote> parameter).
  206. </para>
  207. </listitem>
  208. <listitem>
  209. <para>
  210. <emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
  211. </para>
  212. </listitem>
  213. <listitem>
  214. <para>
  215. <emphasis>result</emphasis> - string name to identify the
  216. result. Will be used by $dbr(...) pseudo-variable to access
  217. result attributes.
  218. If omitted, any resultset will be discarded. The result parameter should
  219. normally only be omitted when no result is expected (INSERT, UPDATE, DELETE).
  220. </para>
  221. </listitem>
  222. </itemizedlist>
  223. <para>
  224. This function can be used from ANY_ROUTE.
  225. </para>
  226. <example>
  227. <title><function>sql_query()</function> usage</title>
  228. <programlisting format="linespecific">
  229. ...
  230. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  231. ...
  232. sql_query("ca", "select * from domain", "ra");
  233. xlog("number of rows in table domain: $dbr(ra=&gt;rows)\n");
  234. sql_result_free("ra");
  235. ...
  236. </programlisting>
  237. </example>
  238. </section>
  239. <section>
  240. <title>
  241. <function moreinfo="none">sql_xquery(connection, query, result)</function>
  242. </title>
  243. <para>
  244. Make an SQL query using 'connection' and store data in 'result' xavp.
  245. </para>
  246. <itemizedlist>
  247. <listitem>
  248. <para>
  249. <emphasis>connection</emphasis> - the name of the connection
  250. to be used for the query (defined via the <quote>sqlcon</quote> parameter).
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
  256. </para>
  257. </listitem>
  258. <listitem>
  259. <para>
  260. <emphasis>result</emphasis> - string name to identify the
  261. result xavp. Each row will be added to this xavp, each column can
  262. be accessed by its name.
  263. </para>
  264. </listitem>
  265. </itemizedlist>
  266. <para>
  267. This function can be used from ANY_ROUTE.
  268. </para>
  269. <example>
  270. <title><function>sql_xquery()</function> usage</title>
  271. <programlisting format="linespecific">
  272. ...
  273. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  274. ...
  275. sql_xquery("ca", "select * from domain", "ra");
  276. xlog("first domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
  277. ...
  278. if (sql_xquery("ca", "select * from domain", "ra") == 1) {
  279. xlog("domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
  280. }
  281. ..
  282. </programlisting>
  283. </example>
  284. </section>
  285. <section>
  286. <title>
  287. <function moreinfo="none">sql_pvquery(connection, query, result)</function>
  288. </title>
  289. <para>
  290. Make an SQL query using 'connection' and store data in arbitrary
  291. pseudo variables specified by 'result' parameter.
  292. </para>
  293. <itemizedlist>
  294. <listitem>
  295. <para>
  296. <emphasis>connection</emphasis> - the name of the connection
  297. to be used for query (defined via the <quote>sqlcon</quote> parameter).
  298. </para>
  299. </listitem>
  300. <listitem>
  301. <para>
  302. <emphasis>query</emphasis> - SQL query string or pseudo-variables containing SQL query.
  303. </para>
  304. </listitem>
  305. <listitem>
  306. <para>
  307. <emphasis>result</emphasis> - a list with PV names where
  308. to store the result. The format is
  309. <quote>$pv;$pv;...</quote>. Every PV that is writable may
  310. be used (for example $var, $avp, $xavp, $ru, $du, $sht, etc).
  311. </para>
  312. <para>
  313. The PV are assigned values in the following order: last row
  314. to first row, first field to last field. Assignment has the
  315. same behavior as assigning in the script itself with one
  316. exception for avp's, a NULL value will not delete an avp, but
  317. will be skipped over.
  318. </para>
  319. <para>
  320. Beware that if multiple rows are returned, non-(x)avp variables
  321. will only hold the last added value, which corresponds to the
  322. first returned row.
  323. </para>
  324. <para>
  325. The value type of the PV (string or integer) will
  326. be derived from the type of the columns. Please note that only
  327. these two datatypes are supported, other datatypes will/may be
  328. converted to string.
  329. </para>
  330. </listitem>
  331. </itemizedlist>
  332. <para>
  333. This function can be used from ANY_ROUTE.
  334. </para>
  335. <example>
  336. <title><function>sql_pvquery()</function> usage</title>
  337. <programlisting format="linespecific">
  338. ...
  339. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  340. ...
  341. sql_pvquery("ca", "select 'col1', 2, NULL, 'sip:[email protected]'",
  342. "$var(a), $avp(col2), $xavp(item[0]=>s), $ru");
  343. ...
  344. </programlisting>
  345. </example>
  346. </section>
  347. <section>
  348. <title>
  349. <function moreinfo="none">sql_result_free(result)</function>
  350. </title>
  351. <para>
  352. Free data in SQL 'result'.
  353. </para>
  354. <para>
  355. This function can be used from ANY_ROUTE.
  356. </para>
  357. <example>
  358. <title><function>sql_result_free()</function> usage</title>
  359. <programlisting format="linespecific">
  360. ...
  361. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  362. ...
  363. sql_query("ca", "select * from domain", "ra");
  364. xlog("number of rows in table domain: $dbr(ra=&gt;rows)\n");
  365. ...
  366. sql_result_free("ra");
  367. ...
  368. </programlisting>
  369. </example>
  370. </section>
  371. <section id="sqlops.f.sql_query_async">
  372. <title>
  373. <function moreinfo="none">sql_query_async(connection, query)</function>
  374. </title>
  375. <para>
  376. Make an async SQL query using 'connection', if implemented by db
  377. driver module (e.g., db_mysql). The query is executed in another
  378. process and result is not available back to config, thus it should
  379. be used only for sql statements that don't return values (e.g.,
  380. insert, delete, update...).
  381. </para>
  382. <itemizedlist>
  383. <listitem>
  384. <para>
  385. <emphasis>connection</emphasis> - the name of the connection
  386. to be used for the query (defined via <quote>sqlcon</quote>
  387. parameter).
  388. </para>
  389. </listitem>
  390. <listitem>
  391. <para>
  392. <emphasis>query</emphasis> - SQL query string or
  393. pseudo-variables containing SQL query.
  394. </para>
  395. </listitem>
  396. </itemizedlist>
  397. <para>
  398. This function can be used from ANY_ROUTE.
  399. </para>
  400. <example>
  401. <title><function>sql_query_async()</function> usage</title>
  402. <programlisting format="linespecific">
  403. ...
  404. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  405. ...
  406. sql_query_async("ca", "delete from domain");
  407. ...
  408. </programlisting>
  409. </example>
  410. </section>
  411. </section>
  412. <section>
  413. <title>Exported pseudo-variables</title>
  414. <section>
  415. <title><varname>$dbr(result=>key)</varname></title>
  416. <para>
  417. Access hash table entries.
  418. </para>
  419. <para>
  420. The <quote>result</quote> must be the name identifying a SQL
  421. result (third parameter of sql_query(...)).
  422. </para>
  423. <para>
  424. The <quote>key</quote> can be:
  425. </para>
  426. <itemizedlist>
  427. <listitem>
  428. <para><emphasis>rows</emphasis> - return the number of rows in
  429. query result</para>
  430. </listitem>
  431. <listitem>
  432. <para><emphasis>cols</emphasis> - return the number of
  433. columns in the result.</para>
  434. </listitem>
  435. <listitem>
  436. <para><emphasis>[row,col]</emphasis> - return the value
  437. at position (row,col) in the result set. 'row' and 'col' must
  438. be integer or pseudo-variable holding an integer.</para>
  439. </listitem>
  440. <listitem>
  441. <para><emphasis>colname[N]</emphasis> - return the name
  442. of the N-th column in the result set.</para>
  443. </listitem>
  444. </itemizedlist>
  445. <example>
  446. <title><function moreinfo="none">$dbr(result=&gt;key)</function> usage</title>
  447. <programlisting format="linespecific">
  448. ...
  449. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  450. ...
  451. sql_query("ca", "select * from domain", "ra");
  452. xlog("rows: $dbr(ra=&gt;rows) cols: $dbr(ra=&gt;cols)\n");
  453. if($dbr(ra=>rows)>0)
  454. {
  455. $var(i) = 0;
  456. while($var(i)&lt;$dbr(ra=&gt;cols))
  457. {
  458. xlog("--- SCRIPT: column[$var(i)] = $dbr(ra=&gt;colname[$var(i)])\n");
  459. $var(i) = $var(i) + 1;
  460. }
  461. $var(i) = 0;
  462. while($var(i)&lt;$dbr(ra=&gt;rows))
  463. {
  464. $var(j) = 0;
  465. while($var(j)&lt;$dbr(ra=&gt;cols))
  466. {
  467. xlog("[$var(i),$var(j)] = $dbr(ra=&gt;[$var(i),$var(j)])\n");
  468. $var(j) = $var(j) + 1;
  469. }
  470. $var(i) = $var(i) + 1;
  471. }
  472. }
  473. sql_result_free("ra");
  474. ...
  475. ...
  476. if (sql_xquery("ca", "select * from domain", "ra") == 1)
  477. {
  478. # non-destructive iteration
  479. $var(i) = 0;
  480. while($xavp(ra[$var(i)]) != $null)
  481. {
  482. xlog("[id, domain] = [$xavp(ra[$var(i)]=&gt;id), $xavp(ra[$var(i)]=&gt;domain)]\n");
  483. $var(i) = $var(i) + 1;
  484. }
  485. # destructive iteration
  486. while($xavp(ra) != $null)
  487. {
  488. xlog("[id, domain] = [$xavp(ra=&gt;id), $xavp(ra=&gt;domain)]\n");
  489. pv_unset("$xavp(ra)");
  490. }
  491. }
  492. ...
  493. </programlisting>
  494. </example>
  495. </section>
  496. <section>
  497. <title><varname>$sqlrows(con)</varname></title>
  498. <para>
  499. Number of affected rows of the previous query on the
  500. specified connection. It's primary use is to get the number
  501. of rows affected by UPDATE, INSERT and DELETE queries.
  502. </para>
  503. <para>
  504. <quote>con</quote> must be the name identifying a DB
  505. connection.
  506. </para>
  507. <example>
  508. <title><function moreinfo="none">$sqlrows(con)</function> usage</title>
  509. <programlisting format="linespecific">
  510. ...
  511. modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
  512. ...
  513. sql_query("ca", "update domain set domain='mydomain' where id=5");
  514. xlog("Affected rows: $sqlrows(ca)\n");
  515. ...
  516. </programlisting>
  517. </example>
  518. </section>
  519. </section>
  520. </chapter>