README 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. SQLOps Module
  2. Daniel-Constantin Mierla
  3. asipto.com
  4. <[email protected]>
  5. Edited by
  6. Daniel-Constantin Mierla
  7. <[email protected]>
  8. Copyright © 2008 http://www.asipto.com
  9. __________________________________________________________________
  10. Table of Contents
  11. 1. Admin Guide
  12. 1. Overview
  13. 2. Dependencies
  14. 2.1. Kamailio Modules
  15. 2.2. External Libraries or Applications
  16. 3. Parameters
  17. 3.1. sqlcon (str)
  18. 3.2. sqlres (str)
  19. 4. Functions
  20. 4.1. sql_query(connection, query[, result])
  21. 4.2. sql_xquery(connection, query, result)
  22. 4.3. sql_pvquery(connection, query, result)
  23. 4.4. sql_result_free(result)
  24. 4.5. sql_query_async(connection, query)
  25. 5. Exported pseudo-variables
  26. 5.1. $dbr(result=>key)
  27. 5.2. $sqlrows(con)
  28. List of Examples
  29. 1.1. Set sqlcon parameter
  30. 1.2. Set sqlres parameter
  31. 1.3. sql_query() usage
  32. 1.4. sql_xquery() usage
  33. 1.5. sql_pvquery() usage
  34. 1.6. sql_result_free() usage
  35. 1.7. sql_query_async() usage
  36. 1.8. $dbr(result=>key) usage
  37. 1.9. $sqlrows(con) usage
  38. Chapter 1. Admin Guide
  39. Table of Contents
  40. 1. Overview
  41. 2. Dependencies
  42. 2.1. Kamailio Modules
  43. 2.2. External Libraries or Applications
  44. 3. Parameters
  45. 3.1. sqlcon (str)
  46. 3.2. sqlres (str)
  47. 4. Functions
  48. 4.1. sql_query(connection, query[, result])
  49. 4.2. sql_xquery(connection, query, result)
  50. 4.3. sql_pvquery(connection, query, result)
  51. 4.4. sql_result_free(result)
  52. 4.5. sql_query_async(connection, query)
  53. 5. Exported pseudo-variables
  54. 5.1. $dbr(result=>key)
  55. 5.2. $sqlrows(con)
  56. 1. Overview
  57. The SQLOPS module adds support for raw SQL queries in the configuration
  58. file.
  59. Among the features:
  60. * Multiple database connections - the sqlops module can connect to
  61. many databases on different servers using different DB driver
  62. modules at the same time.
  63. * Many query results - the module can store many results of different
  64. SQL queries in separate structures at the same time. Thus it is
  65. possible to work in parallel with several queries and results.
  66. * Access via pseudo-variables - the content of the SQL query result
  67. is accessible via pseudo-variables. Please note that only integer
  68. and string variables are supported at the moment because of the
  69. internal usage of "AVPs" to hold the values. So it is not possible
  70. for example to return floating point or big integer values this
  71. way.
  72. * Array indexes - fast access to result values via array position:
  73. [row,column].
  74. * Persistence in process space - a result can be used many times in
  75. the same worker process. Query once, use many times.
  76. * Results can be stored in xavps - columns are accessed by their
  77. names, rows by xavp index. Xavp's are available during the
  78. transactions lifetime and don't need to be destroyed manually.
  79. 2. Dependencies
  80. 2.1. Kamailio Modules
  81. 2.2. External Libraries or Applications
  82. 2.1. Kamailio Modules
  83. The following modules must be loaded before this module:
  84. * a DB SQL module (mysql, postgres, ...).
  85. 2.2. External Libraries or Applications
  86. The following libraries or applications must be installed before
  87. running Kamailio with this module loaded:
  88. * None.
  89. 3. Parameters
  90. 3.1. sqlcon (str)
  91. 3.2. sqlres (str)
  92. 3.1. sqlcon (str)
  93. The definition of a DB connection. The value of the parameter must have
  94. the following format:
  95. * "connection_name=>database_url"
  96. This parameter may be set multiple times to get many DB connections in
  97. the same configuration file.
  98. * connection_name - string specifying the name of a database
  99. connection. This string is used by the "sql_query()" function to
  100. refer to the DB connection.
  101. * database_url - Standardized Kamailio database URL used to connect
  102. to database.
  103. Default value is NULL.
  104. Example 1.1. Set sqlcon parameter
  105. ...
  106. modparam("sqlops","sqlcon","cb=>mysql://kamailio:[email protected]/testdb")
  107. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  108. ...
  109. 3.2. sqlres (str)
  110. The definition of a database result ID. The value of the parameter can
  111. be any string. Results IDs are also added at fixup time when
  112. sql_query() parameters are parsed, so there is no need to decalare them
  113. via module parameter unless you want to use them from within other
  114. modules and be available in all application processes.
  115. Default value is NULL.
  116. Example 1.2. Set sqlres parameter
  117. ...
  118. modparam("sqlops", "sqlres", "ra")
  119. ...
  120. 4. Functions
  121. 4.1. sql_query(connection, query[, result])
  122. 4.2. sql_xquery(connection, query, result)
  123. 4.3. sql_pvquery(connection, query, result)
  124. 4.4. sql_result_free(result)
  125. 4.5. sql_query_async(connection, query)
  126. Note that sql_query(), sql_xquery() and sql_pvquery() functions have
  127. the following return values:
  128. * -1 - error in parameters or query execution
  129. * 1 - query successful, at least one row in resultset (for SELECTs)
  130. * 2 - query successful, no rows returned
  131. 4.1. sql_query(connection, query[, result])
  132. Make an SQL query using 'connection' and store data in 'result'.
  133. * connection - the name of the connection to be used for the query
  134. (defined via the "sqlcon" parameter).
  135. * query - SQL query string or pseudo-variables containing SQL query.
  136. * result - string name to identify the result. Will be used by
  137. $dbr(...) pseudo-variable to access result attributes. If omitted,
  138. any resultset will be discarded. The result parameter should
  139. normally only be omitted when no result is expected (INSERT,
  140. UPDATE, DELETE).
  141. This function can be used from ANY_ROUTE.
  142. Example 1.3. sql_query() usage
  143. ...
  144. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  145. ...
  146. sql_query("ca", "select * from domain", "ra");
  147. xlog("number of rows in table domain: $dbr(ra=>rows)\n");
  148. sql_result_free("ra");
  149. ...
  150. 4.2. sql_xquery(connection, query, result)
  151. Make an SQL query using 'connection' and store data in 'result' xavp.
  152. * connection - the name of the connection to be used for the query
  153. (defined via the "sqlcon" parameter).
  154. * query - SQL query string or pseudo-variables containing SQL query.
  155. * result - string name to identify the result xavp. Each row will be
  156. added to this xavp, each column can be accessed by its name.
  157. This function can be used from ANY_ROUTE.
  158. Example 1.4. sql_xquery() usage
  159. ...
  160. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  161. ...
  162. sql_xquery("ca", "select * from domain", "ra");
  163. xlog("first domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
  164. ...
  165. if (sql_xquery("ca", "select * from domain", "ra") == 1) {
  166. xlog("domain: $xavp(ra=>domain) with id: $xavp(ra=>domain_id)\n");
  167. }
  168. ..
  169. 4.3. sql_pvquery(connection, query, result)
  170. Make an SQL query using 'connection' and store data in arbitrary pseudo
  171. variables specified by 'result' parameter.
  172. * connection - the name of the connection to be used for query
  173. (defined via the "sqlcon" parameter).
  174. * query - SQL query string or pseudo-variables containing SQL query.
  175. * result - a list with PV names where to store the result. The format
  176. is "$pv;$pv;...". Every PV that is writable may be used (for
  177. example $var, $avp, $xavp, $ru, $du, $sht, etc).
  178. The PV are assigned values in the following order: last row to
  179. first row, first field to last field. Assignment has the same
  180. behavior as assigning in the script itself with one exception for
  181. avp's, a NULL value will not delete an avp, but will be skipped
  182. over.
  183. Beware that if multiple rows are returned, non-(x)avp variables
  184. will only hold the last added value, which corresponds to the first
  185. returned row.
  186. The value type of the PV (string or integer) will be derived from
  187. the type of the columns. Please note that only these two datatypes
  188. are supported, other datatypes will/may be converted to string.
  189. This function can be used from ANY_ROUTE.
  190. Example 1.5. sql_pvquery() usage
  191. ...
  192. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  193. ...
  194. sql_pvquery("ca", "select 'col1', 2, NULL, 'sip:[email protected]'",
  195. "$var(a), $avp(col2), $xavp(item[0]=>s), $ru");
  196. ...
  197. 4.4. sql_result_free(result)
  198. Free data in SQL 'result'.
  199. This function can be used from ANY_ROUTE.
  200. Example 1.6. sql_result_free() usage
  201. ...
  202. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  203. ...
  204. sql_query("ca", "select * from domain", "ra");
  205. xlog("number of rows in table domain: $dbr(ra=>rows)\n");
  206. ...
  207. sql_result_free("ra");
  208. ...
  209. 4.5. sql_query_async(connection, query)
  210. Make an async SQL query using 'connection', if implemented by db driver
  211. module (e.g., db_mysql). The query is executed in another process and
  212. result is not available back to config, thus it should be used only for
  213. sql statements that don't return values (e.g., insert, delete,
  214. update...).
  215. * connection - the name of the connection to be used for the query
  216. (defined via "sqlcon" parameter).
  217. * query - SQL query string or pseudo-variables containing SQL query.
  218. This function can be used from ANY_ROUTE.
  219. Example 1.7. sql_query_async() usage
  220. ...
  221. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  222. ...
  223. sql_query_async("ca", "delete from domain");
  224. ...
  225. 5. Exported pseudo-variables
  226. 5.1. $dbr(result=>key)
  227. 5.2. $sqlrows(con)
  228. 5.1. $dbr(result=>key)
  229. Access hash table entries.
  230. The "result" must be the name identifying a SQL result (third parameter
  231. of sql_query(...)).
  232. The "key" can be:
  233. * rows - return the number of rows in query result
  234. * cols - return the number of columns in the result.
  235. * [row,col] - return the value at position (row,col) in the result
  236. set. 'row' and 'col' must be integer or pseudo-variable holding an
  237. integer.
  238. * colname[N] - return the name of the N-th column in the result set.
  239. Example 1.8. $dbr(result=>key) usage
  240. ...
  241. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  242. ...
  243. sql_query("ca", "select * from domain", "ra");
  244. xlog("rows: $dbr(ra=>rows) cols: $dbr(ra=>cols)\n");
  245. if($dbr(ra=>rows)>0)
  246. {
  247. $var(i) = 0;
  248. while($var(i)<$dbr(ra=>cols))
  249. {
  250. xlog("--- SCRIPT: column[$var(i)] = $dbr(ra=>colname[$var(i)])\n");
  251. $var(i) = $var(i) + 1;
  252. }
  253. $var(i) = 0;
  254. while($var(i)<$dbr(ra=>rows))
  255. {
  256. $var(j) = 0;
  257. while($var(j)<$dbr(ra=>cols))
  258. {
  259. xlog("[$var(i),$var(j)] = $dbr(ra=>[$var(i),$var(j)])\n");
  260. $var(j) = $var(j) + 1;
  261. }
  262. $var(i) = $var(i) + 1;
  263. }
  264. }
  265. sql_result_free("ra");
  266. ...
  267. ...
  268. if (sql_xquery("ca", "select * from domain", "ra") == 1)
  269. {
  270. # non-destructive iteration
  271. $var(i) = 0;
  272. while($xavp(ra[$var(i)]) != $null)
  273. {
  274. xlog("[id, domain] = [$xavp(ra[$var(i)]=>id), $xavp(ra[$var(i)]=>domain)
  275. ]\n");
  276. $var(i) = $var(i) + 1;
  277. }
  278. # destructive iteration
  279. while($xavp(ra) != $null)
  280. {
  281. xlog("[id, domain] = [$xavp(ra=>id), $xavp(ra=>domain)]\n");
  282. pv_unset("$xavp(ra)");
  283. }
  284. }
  285. ...
  286. 5.2. $sqlrows(con)
  287. Number of affected rows of the previous query on the specified
  288. connection. It's primary use is to get the number of rows affected by
  289. UPDATE, INSERT and DELETE queries.
  290. "con" must be the name identifying a DB connection.
  291. Example 1.9. $sqlrows(con) usage
  292. ...
  293. modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
  294. ...
  295. sql_query("ca", "update domain set domain='mydomain' where id=5");
  296. xlog("Affected rows: $sqlrows(ca)\n");
  297. ...