res_config_odbc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief odbc+odbc plugin for portable configuration engine
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. * \author Anthony Minessale II <anthmct@yahoo.com>
  26. *
  27. * \arg http://www.unixodbc.org
  28. */
  29. /*** MODULEINFO
  30. <depend>res_odbc</depend>
  31. <depend>generic_odbc</depend>
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/file.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/config.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/lock.h"
  42. #include "asterisk/res_odbc.h"
  43. #include "asterisk/utils.h"
  44. #include "asterisk/stringfields.h"
  45. /*! Initial SQL query buffer size to allocate. */
  46. #define SQL_BUF_SIZE 1024
  47. AST_THREADSTORAGE(sql_buf);
  48. AST_THREADSTORAGE(rowdata_buf);
  49. struct custom_prepare_struct {
  50. const char *sql;
  51. const char *extra;
  52. AST_DECLARE_STRING_FIELDS(
  53. AST_STRING_FIELD(encoding)[256];
  54. );
  55. const struct ast_variable *fields;
  56. unsigned long long skip;
  57. };
  58. #define ENCODE_CHUNK(buffer, s) \
  59. do { \
  60. char *eptr = buffer; \
  61. const char *vptr = s; \
  62. for (; *vptr && eptr < buffer + sizeof(buffer); vptr++) { \
  63. if (strchr("^;", *vptr)) { \
  64. /* We use ^XX, instead of %XX because '%' is a special character in SQL */ \
  65. snprintf(eptr, buffer + sizeof(buffer) - eptr, "^%02hhX", *vptr); \
  66. eptr += 3; \
  67. } else { \
  68. *eptr++ = *vptr; \
  69. } \
  70. } \
  71. if (eptr < buffer + sizeof(buffer)) { \
  72. *eptr = '\0'; \
  73. } else { \
  74. buffer[sizeof(buffer) - 1] = '\0'; \
  75. } \
  76. } while(0)
  77. static void decode_chunk(char *chunk)
  78. {
  79. for (; *chunk; chunk++) {
  80. if (*chunk == '^' && strchr("0123456789ABCDEF", chunk[1]) && strchr("0123456789ABCDEF", chunk[2])) {
  81. sscanf(chunk + 1, "%02hhX", (unsigned char *)chunk);
  82. memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1);
  83. }
  84. }
  85. }
  86. static inline int is_text(const struct odbc_cache_columns *column)
  87. {
  88. return column->type == SQL_CHAR || column->type == SQL_VARCHAR || column->type == SQL_LONGVARCHAR
  89. || column->type == SQL_WCHAR || column->type == SQL_WVARCHAR || column->type == SQL_WLONGVARCHAR;
  90. }
  91. static SQLHSTMT custom_prepare(struct odbc_obj *obj, void *data)
  92. {
  93. int res, x = 1, count = 0;
  94. struct custom_prepare_struct *cps = data;
  95. const struct ast_variable *field;
  96. char encodebuf[1024];
  97. SQLHSTMT stmt;
  98. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  99. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  100. ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
  101. return NULL;
  102. }
  103. ast_debug(1, "Skip: %llu; SQL: %s\n", cps->skip, cps->sql);
  104. res = SQLPrepare(stmt, (unsigned char *)cps->sql, SQL_NTS);
  105. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  106. if (res == SQL_ERROR) {
  107. ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Prepare");
  108. }
  109. ast_log(LOG_WARNING, "SQL Prepare failed! [%s]\n", cps->sql);
  110. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  111. return NULL;
  112. }
  113. for (field = cps->fields; field; field = field->next) {
  114. const char *newval = field->value;
  115. if ((1LL << count++) & cps->skip) {
  116. ast_debug(1, "Skipping field '%s'='%s' (%llo/%llo)\n", field->name, newval, 1ULL << (count - 1), cps->skip);
  117. continue;
  118. }
  119. ast_debug(1, "Parameter %d ('%s') = '%s'\n", x, field->name, newval);
  120. if (strchr(newval, ';') || strchr(newval, '^')) {
  121. ENCODE_CHUNK(encodebuf, newval);
  122. ast_string_field_set(cps, encoding[x], encodebuf);
  123. newval = cps->encoding[x];
  124. }
  125. SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
  126. }
  127. if (!ast_strlen_zero(cps->extra)) {
  128. const char *newval = cps->extra;
  129. ast_debug(1, "Parameter %d = '%s'\n", x, newval);
  130. if (strchr(newval, ';') || strchr(newval, '^')) {
  131. ENCODE_CHUNK(encodebuf, newval);
  132. ast_string_field_set(cps, encoding[x], encodebuf);
  133. newval = cps->encoding[x];
  134. }
  135. SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
  136. }
  137. return stmt;
  138. }
  139. /*!
  140. * \brief Excute an SQL query and return ast_variable list
  141. * \param database
  142. * \param table
  143. * \param ap list containing one or more field/operator/value set.
  144. *
  145. * Select database and preform query on table, prepare the sql statement
  146. * Sub-in the values to the prepared statement and execute it. Return results
  147. * as a ast_variable list.
  148. *
  149. * \retval var on success
  150. * \retval NULL on failure
  151. */
  152. static struct ast_variable *realtime_odbc(const char *database, const char *table, const struct ast_variable *fields)
  153. {
  154. struct odbc_obj *obj;
  155. SQLHSTMT stmt;
  156. char coltitle[256];
  157. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  158. struct ast_str *rowdata = ast_str_thread_get(&rowdata_buf, 128);
  159. char *op;
  160. const struct ast_variable *field = fields;
  161. char *stringp;
  162. char *chunk;
  163. SQLSMALLINT collen;
  164. int res;
  165. int x;
  166. struct ast_variable *var=NULL, *prev=NULL;
  167. SQLULEN colsize;
  168. SQLSMALLINT colcount=0;
  169. SQLSMALLINT datatype;
  170. SQLSMALLINT decimaldigits;
  171. SQLSMALLINT nullable;
  172. SQLLEN indicator;
  173. struct custom_prepare_struct cps = { .fields = fields, };
  174. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  175. if (!table || !field || !sql || !rowdata) {
  176. return NULL;
  177. }
  178. obj = ast_odbc_request_obj2(database, connected_flag);
  179. if (!obj) {
  180. ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", database);
  181. return NULL;
  182. }
  183. op = !strchr(field->name, ' ') ? " =" : "";
  184. ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s ?%s", table, field->name, op,
  185. strcasestr(field->name, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\\\'" : "");
  186. while ((field = field->next)) {
  187. op = !strchr(field->name, ' ') ? " =" : "";
  188. ast_str_append(&sql, 0, " AND %s%s ?%s", field->name, op,
  189. strcasestr(field->name, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\\\'" : "");
  190. }
  191. cps.sql = ast_str_buffer(sql);
  192. if (ast_string_field_init(&cps, 256)) {
  193. ast_odbc_release_obj(obj);
  194. return NULL;
  195. }
  196. stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
  197. ast_string_field_free_memory(&cps);
  198. if (!stmt) {
  199. ast_odbc_release_obj(obj);
  200. return NULL;
  201. }
  202. res = SQLNumResultCols(stmt, &colcount);
  203. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  204. ast_log(LOG_WARNING, "SQL Column Count error! [%s]\n", ast_str_buffer(sql));
  205. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  206. ast_odbc_release_obj(obj);
  207. return NULL;
  208. }
  209. res = SQLFetch(stmt);
  210. if (res == SQL_NO_DATA) {
  211. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  212. ast_odbc_release_obj(obj);
  213. return NULL;
  214. }
  215. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  216. ast_log(LOG_WARNING, "SQL Fetch error! [%s]\n", ast_str_buffer(sql));
  217. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  218. ast_odbc_release_obj(obj);
  219. return NULL;
  220. }
  221. for (x = 0; x < colcount; x++) {
  222. colsize = 0;
  223. collen = sizeof(coltitle);
  224. res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
  225. &datatype, &colsize, &decimaldigits, &nullable);
  226. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  227. ast_log(LOG_WARNING, "SQL Describe Column error! [%s]\n", ast_str_buffer(sql));
  228. if (var)
  229. ast_variables_destroy(var);
  230. ast_odbc_release_obj(obj);
  231. return NULL;
  232. }
  233. ast_str_reset(rowdata);
  234. indicator = 0;
  235. res = SQLGetData(stmt, x + 1, SQL_CHAR, ast_str_buffer(rowdata), ast_str_size(rowdata), &indicator);
  236. ast_str_update(rowdata);
  237. if (indicator == SQL_NULL_DATA) {
  238. ast_str_reset(rowdata);
  239. } else if (!ast_str_strlen(rowdata)) {
  240. /* Because we encode the empty string for a NULL, we will encode
  241. * actual empty strings as a string containing a single whitespace. */
  242. ast_str_set(&rowdata, -1, "%s", " ");
  243. } else if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) {
  244. if (indicator != ast_str_strlen(rowdata)) {
  245. /* If the available space was not enough to contain the row data enlarge and read in the rest */
  246. ast_str_make_space(&rowdata, indicator + 1);
  247. res = SQLGetData(stmt, x + 1, SQL_CHAR, ast_str_buffer(rowdata) + ast_str_strlen(rowdata),
  248. ast_str_size(rowdata) - ast_str_strlen(rowdata), &indicator);
  249. ast_str_update(rowdata);
  250. }
  251. }
  252. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  253. ast_log(LOG_WARNING, "SQL Get Data error! [%s]\n", ast_str_buffer(sql));
  254. if (var)
  255. ast_variables_destroy(var);
  256. ast_odbc_release_obj(obj);
  257. return NULL;
  258. }
  259. stringp = ast_str_buffer(rowdata);
  260. while (stringp) {
  261. chunk = strsep(&stringp, ";");
  262. if (!ast_strlen_zero(ast_strip(chunk))) {
  263. if (strchr(chunk, '^')) {
  264. decode_chunk(chunk);
  265. }
  266. if (prev) {
  267. prev->next = ast_variable_new(coltitle, chunk, "");
  268. if (prev->next) {
  269. prev = prev->next;
  270. }
  271. } else {
  272. prev = var = ast_variable_new(coltitle, chunk, "");
  273. }
  274. }
  275. }
  276. }
  277. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  278. ast_odbc_release_obj(obj);
  279. return var;
  280. }
  281. /*!
  282. * \brief Excute an Select query and return ast_config list
  283. * \param database
  284. * \param table
  285. * \param ap list containing one or more field/operator/value set.
  286. *
  287. * Select database and preform query on table, prepare the sql statement
  288. * Sub-in the values to the prepared statement and execute it.
  289. * Execute this prepared query against several ODBC connected databases.
  290. * Return results as an ast_config variable.
  291. *
  292. * \retval var on success
  293. * \retval NULL on failure
  294. */
  295. static struct ast_config *realtime_multi_odbc(const char *database, const char *table, const struct ast_variable *fields)
  296. {
  297. struct odbc_obj *obj;
  298. SQLHSTMT stmt;
  299. char coltitle[256];
  300. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  301. struct ast_str *rowdata = ast_str_thread_get(&rowdata_buf, 128);
  302. const char *initfield;
  303. char *op;
  304. const struct ast_variable *field = fields;
  305. char *stringp;
  306. char *chunk;
  307. SQLSMALLINT collen;
  308. int res;
  309. int x;
  310. struct ast_variable *var=NULL;
  311. struct ast_config *cfg=NULL;
  312. struct ast_category *cat=NULL;
  313. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  314. SQLULEN colsize;
  315. SQLSMALLINT colcount=0;
  316. SQLSMALLINT datatype;
  317. SQLSMALLINT decimaldigits;
  318. SQLSMALLINT nullable;
  319. SQLLEN indicator;
  320. struct custom_prepare_struct cps = { .fields = fields, };
  321. if (!table || !field || !sql || !rowdata) {
  322. return NULL;
  323. }
  324. obj = ast_odbc_request_obj2(database, connected_flag);
  325. if (!obj) {
  326. return NULL;
  327. }
  328. initfield = ast_strdupa(field->name);
  329. if ((op = strchr(initfield, ' '))) {
  330. *op = '\0';
  331. }
  332. op = !strchr(field->name, ' ') ? " =" : "";
  333. ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s ?%s", table, field->name, op,
  334. strcasestr(field->name, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\\\'" : "");
  335. while ((field = field->next)) {
  336. op = !strchr(field->name, ' ') ? " =" : "";
  337. ast_str_append(&sql, 0, " AND %s%s ?%s", field->name, op,
  338. strcasestr(field->name, "LIKE") && !ast_odbc_backslash_is_escape(obj) ? " ESCAPE '\\\\'" : "");
  339. }
  340. ast_str_append(&sql, 0, " ORDER BY %s", initfield);
  341. cps.sql = ast_str_buffer(sql);
  342. if (ast_string_field_init(&cps, 256)) {
  343. ast_odbc_release_obj(obj);
  344. return NULL;
  345. }
  346. stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
  347. ast_string_field_free_memory(&cps);
  348. if (!stmt) {
  349. ast_odbc_release_obj(obj);
  350. return NULL;
  351. }
  352. res = SQLNumResultCols(stmt, &colcount);
  353. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  354. ast_log(LOG_WARNING, "SQL Column Count error! [%s]\n", ast_str_buffer(sql));
  355. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  356. ast_odbc_release_obj(obj);
  357. return NULL;
  358. }
  359. cfg = ast_config_new();
  360. if (!cfg) {
  361. ast_log(LOG_WARNING, "Out of memory!\n");
  362. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  363. ast_odbc_release_obj(obj);
  364. return NULL;
  365. }
  366. while ((res=SQLFetch(stmt)) != SQL_NO_DATA) {
  367. var = NULL;
  368. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  369. ast_log(LOG_WARNING, "SQL Fetch error! [%s]\n", ast_str_buffer(sql));
  370. continue;
  371. }
  372. cat = ast_category_new_anonymous();
  373. if (!cat) {
  374. continue;
  375. }
  376. for (x=0;x<colcount;x++) {
  377. colsize = 0;
  378. collen = sizeof(coltitle);
  379. res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
  380. &datatype, &colsize, &decimaldigits, &nullable);
  381. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  382. ast_log(LOG_WARNING, "SQL Describe Column error! [%s]\n", ast_str_buffer(sql));
  383. ast_category_destroy(cat);
  384. goto next_sql_fetch;
  385. }
  386. ast_str_reset(rowdata);
  387. indicator = 0;
  388. res = SQLGetData(stmt, x + 1, SQL_CHAR, ast_str_buffer(rowdata), ast_str_size(rowdata), &indicator);
  389. ast_str_update(rowdata);
  390. if (indicator == SQL_NULL_DATA) {
  391. continue;
  392. }
  393. if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) {
  394. if (indicator != ast_str_strlen(rowdata)) {
  395. /* If the available space was not enough to contain the row data enlarge and read in the rest */
  396. ast_str_make_space(&rowdata, indicator + 1);
  397. res = SQLGetData(stmt, x + 1, SQL_CHAR, ast_str_buffer(rowdata) + ast_str_strlen(rowdata),
  398. ast_str_size(rowdata) - ast_str_strlen(rowdata), &indicator);
  399. ast_str_update(rowdata);
  400. }
  401. }
  402. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  403. ast_log(LOG_WARNING, "SQL Get Data error! [%s]\n", ast_str_buffer(sql));
  404. ast_category_destroy(cat);
  405. goto next_sql_fetch;
  406. }
  407. stringp = ast_str_buffer(rowdata);
  408. while (stringp) {
  409. chunk = strsep(&stringp, ";");
  410. if (!ast_strlen_zero(ast_strip(chunk))) {
  411. if (strchr(chunk, '^')) {
  412. decode_chunk(chunk);
  413. }
  414. if (!strcmp(initfield, coltitle)) {
  415. ast_category_rename(cat, chunk);
  416. }
  417. var = ast_variable_new(coltitle, chunk, "");
  418. ast_variable_append(cat, var);
  419. }
  420. }
  421. }
  422. ast_category_append(cfg, cat);
  423. next_sql_fetch:;
  424. }
  425. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  426. ast_odbc_release_obj(obj);
  427. return cfg;
  428. }
  429. /*!
  430. * \brief Excute an UPDATE query
  431. * \param database
  432. * \param table
  433. * \param keyfield where clause field
  434. * \param lookup value of field for where clause
  435. * \param ap list containing one or more field/value set(s).
  436. *
  437. * Update a database table, prepare the sql statement using keyfield and lookup
  438. * control the number of records to change. All values to be changed are stored in ap list.
  439. * Sub-in the values to the prepared statement and execute it.
  440. *
  441. * \retval number of rows affected
  442. * \retval -1 on failure
  443. */
  444. static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, const struct ast_variable *fields)
  445. {
  446. struct odbc_obj *obj;
  447. SQLHSTMT stmt;
  448. SQLLEN rowcount=0;
  449. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  450. const struct ast_variable *field = fields;
  451. int res, count = 0, paramcount = 0;
  452. struct custom_prepare_struct cps = { .extra = lookup, .fields = fields, };
  453. struct odbc_cache_tables *tableptr;
  454. struct odbc_cache_columns *column = NULL;
  455. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  456. if (!table || !field || !keyfield || !sql) {
  457. return -1;
  458. }
  459. tableptr = ast_odbc_find_table(database, table);
  460. if (!(obj = ast_odbc_request_obj2(database, connected_flag))) {
  461. ast_odbc_release_table(tableptr);
  462. return -1;
  463. }
  464. if (tableptr && !ast_odbc_find_column(tableptr, keyfield)) {
  465. ast_log(LOG_WARNING, "Key field '%s' does not exist in table '%s@%s'. Update will fail\n", keyfield, table, database);
  466. }
  467. ast_str_set(&sql, 0, "UPDATE %s SET ", table);
  468. while (field) {
  469. if ((tableptr && (column = ast_odbc_find_column(tableptr, field->name))) || count >= 64) {
  470. if (paramcount++) {
  471. ast_str_append(&sql, 0, ", ");
  472. }
  473. /* NULL test for non-text columns */
  474. if (count < 64 && ast_strlen_zero(field->value) && column->nullable && !is_text(column)) {
  475. ast_str_append(&sql, 0, "%s=NULL", field->name);
  476. cps.skip |= (1LL << count);
  477. } else {
  478. /* Value is not an empty string, or column is of text type, or we couldn't fit any more into cps.skip (count >= 64 ?!). */
  479. ast_str_append(&sql, 0, "%s=?", field->name);
  480. }
  481. } else { /* the column does not exist in the table */
  482. cps.skip |= (1LL << count);
  483. }
  484. ++count;
  485. field = field->next;
  486. }
  487. ast_str_append(&sql, 0, " WHERE %s=?", keyfield);
  488. ast_odbc_release_table(tableptr);
  489. cps.sql = ast_str_buffer(sql);
  490. if (ast_string_field_init(&cps, 256)) {
  491. ast_odbc_release_obj(obj);
  492. return -1;
  493. }
  494. stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
  495. ast_string_field_free_memory(&cps);
  496. if (!stmt) {
  497. ast_odbc_release_obj(obj);
  498. return -1;
  499. }
  500. res = SQLRowCount(stmt, &rowcount);
  501. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  502. ast_odbc_release_obj(obj);
  503. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  504. ast_log(LOG_WARNING, "SQL Row Count error! [%s]\n", ast_str_buffer(sql));
  505. return -1;
  506. }
  507. if (rowcount >= 0) {
  508. return (int) rowcount;
  509. }
  510. return -1;
  511. }
  512. struct update2_prepare_struct {
  513. const char *database;
  514. const char *table;
  515. const struct ast_variable *lookup_fields;
  516. const struct ast_variable *update_fields;
  517. };
  518. static SQLHSTMT update2_prepare(struct odbc_obj *obj, void *data)
  519. {
  520. int res, x = 1, first = 1;
  521. struct update2_prepare_struct *ups = data;
  522. const struct ast_variable *field;
  523. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  524. SQLHSTMT stmt;
  525. struct odbc_cache_tables *tableptr;
  526. if (!sql) {
  527. return NULL;
  528. }
  529. tableptr = ast_odbc_find_table(ups->database, ups->table);
  530. if (!tableptr) {
  531. ast_log(LOG_ERROR, "Could not retrieve metadata for table '%s@%s'. Update will fail!\n", ups->table, ups->database);
  532. return NULL;
  533. }
  534. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  535. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  536. ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
  537. ast_odbc_release_table(tableptr);
  538. return NULL;
  539. }
  540. ast_str_set(&sql, 0, "UPDATE %s SET ", ups->table);
  541. for (field = ups->update_fields; field; field = field->next) {
  542. if (ast_odbc_find_column(tableptr, field->name)) {
  543. ast_str_append(&sql, 0, "%s%s=? ", first ? "" : ", ", field->name);
  544. SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(field->name), 0, (void *)field->value, 0, NULL);
  545. first = 0;
  546. } else {
  547. ast_log(LOG_NOTICE, "Not updating column '%s' in '%s@%s' because that column does not exist!\n", field->name, ups->table, ups->database);
  548. }
  549. }
  550. ast_str_append(&sql, 0, "WHERE");
  551. first = 1;
  552. for (field = ups->lookup_fields; field; field = field->next) {
  553. if (!ast_odbc_find_column(tableptr, field->name)) {
  554. ast_log(LOG_ERROR, "One or more of the criteria columns '%s' on '%s@%s' for this update does not exist!\n", field->name, ups->table, ups->database);
  555. ast_odbc_release_table(tableptr);
  556. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  557. return NULL;
  558. }
  559. ast_str_append(&sql, 0, "%s %s=?", first ? "" : " AND", field->name);
  560. SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(field->value), 0, (void *)field->value, 0, NULL);
  561. first = 0;
  562. }
  563. /* Done with the table metadata */
  564. ast_odbc_release_table(tableptr);
  565. res = SQLPrepare(stmt, (unsigned char *)ast_str_buffer(sql), SQL_NTS);
  566. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  567. if (res == SQL_ERROR) {
  568. ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Prepare");
  569. }
  570. ast_log(LOG_WARNING, "SQL Prepare failed! [%s]\n", ast_str_buffer(sql));
  571. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  572. return NULL;
  573. }
  574. return stmt;
  575. }
  576. /*!
  577. * \brief Execute an UPDATE query
  578. * \param database
  579. * \param table
  580. * \param ap list containing one or more field/value set(s).
  581. *
  582. * Update a database table, preparing the sql statement from a list of
  583. * key/value pairs specified in ap. The lookup pairs are specified first
  584. * and are separated from the update pairs by a sentinel value.
  585. * Sub-in the values to the prepared statement and execute it.
  586. *
  587. * \retval number of rows affected
  588. * \retval -1 on failure
  589. */
  590. static int update2_odbc(const char *database, const char *table, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
  591. {
  592. struct odbc_obj *obj;
  593. SQLHSTMT stmt;
  594. struct update2_prepare_struct ups = { .database = database, .table = table, .lookup_fields = lookup_fields, .update_fields = update_fields, };
  595. struct ast_str *sql;
  596. int res;
  597. SQLLEN rowcount = 0;
  598. if (!(obj = ast_odbc_request_obj(database, 0))) {
  599. return -1;
  600. }
  601. if (!(stmt = ast_odbc_prepare_and_execute(obj, update2_prepare, &ups))) {
  602. ast_odbc_release_obj(obj);
  603. return -1;
  604. }
  605. res = SQLRowCount(stmt, &rowcount);
  606. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  607. ast_odbc_release_obj(obj);
  608. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  609. /* Since only a single thread can access this memory, we can retrieve what would otherwise be lost. */
  610. sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  611. ast_assert(sql != NULL);
  612. ast_log(LOG_WARNING, "SQL Row Count error! [%s]\n", ast_str_buffer(sql));
  613. return -1;
  614. }
  615. if (rowcount >= 0) {
  616. return (int)rowcount;
  617. }
  618. return -1;
  619. }
  620. /*!
  621. * \brief Excute an INSERT query
  622. * \param database
  623. * \param table
  624. * \param ap list containing one or more field/value set(s)
  625. *
  626. * Insert a new record into database table, prepare the sql statement.
  627. * All values to be changed are stored in ap list.
  628. * Sub-in the values to the prepared statement and execute it.
  629. *
  630. * \retval number of rows affected
  631. * \retval -1 on failure
  632. */
  633. static int store_odbc(const char *database, const char *table, const struct ast_variable *fields)
  634. {
  635. struct odbc_obj *obj;
  636. SQLHSTMT stmt;
  637. SQLLEN rowcount=0;
  638. const struct ast_variable *field = fields;
  639. struct ast_str *keys;
  640. struct ast_str *vals;
  641. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  642. int res;
  643. struct custom_prepare_struct cps = { .fields = fields, };
  644. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  645. keys = ast_str_create(SQL_BUF_SIZE / 2);
  646. vals = ast_str_create(SQL_BUF_SIZE / 4);
  647. if (!table || !field || !keys || !vals || !sql) {
  648. ast_free(vals);
  649. ast_free(keys);
  650. return -1;
  651. }
  652. obj = ast_odbc_request_obj2(database, connected_flag);
  653. if (!obj) {
  654. ast_free(vals);
  655. ast_free(keys);
  656. return -1;
  657. }
  658. ast_str_set(&keys, 0, "%s", field->name);
  659. ast_str_set(&vals, 0, "?");
  660. while ((field = field->next)) {
  661. ast_str_append(&keys, 0, ", %s", field->name);
  662. ast_str_append(&vals, 0, ", ?");
  663. }
  664. ast_str_set(&sql, 0, "INSERT INTO %s (%s) VALUES (%s)",
  665. table, ast_str_buffer(keys), ast_str_buffer(vals));
  666. ast_free(vals);
  667. ast_free(keys);
  668. cps.sql = ast_str_buffer(sql);
  669. if (ast_string_field_init(&cps, 256)) {
  670. ast_odbc_release_obj(obj);
  671. return -1;
  672. }
  673. stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
  674. ast_string_field_free_memory(&cps);
  675. if (!stmt) {
  676. ast_odbc_release_obj(obj);
  677. return -1;
  678. }
  679. res = SQLRowCount(stmt, &rowcount);
  680. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  681. ast_odbc_release_obj(obj);
  682. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  683. ast_log(LOG_WARNING, "SQL Row Count error! [%s]\n", ast_str_buffer(sql));
  684. return -1;
  685. }
  686. if (rowcount >= 0)
  687. return (int)rowcount;
  688. return -1;
  689. }
  690. /*!
  691. * \brief Excute an DELETE query
  692. * \param database
  693. * \param table
  694. * \param keyfield where clause field
  695. * \param lookup value of field for where clause
  696. * \param ap list containing one or more field/value set(s)
  697. *
  698. * Delete a row from a database table, prepare the sql statement using keyfield and lookup
  699. * control the number of records to change. Additional params to match rows are stored in ap list.
  700. * Sub-in the values to the prepared statement and execute it.
  701. *
  702. * \retval number of rows affected
  703. * \retval -1 on failure
  704. */
  705. static int destroy_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, const struct ast_variable *fields)
  706. {
  707. struct odbc_obj *obj;
  708. SQLHSTMT stmt;
  709. SQLLEN rowcount=0;
  710. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  711. const struct ast_variable *field;
  712. int res;
  713. struct custom_prepare_struct cps = { .extra = lookup, .fields = fields, };
  714. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  715. if (!table || !sql) {
  716. return -1;
  717. }
  718. obj = ast_odbc_request_obj2(database, connected_flag);
  719. if (!obj) {
  720. return -1;
  721. }
  722. ast_str_set(&sql, 0, "DELETE FROM %s WHERE ", table);
  723. for (field = fields; field; field = field->next) {
  724. ast_str_append(&sql, 0, "%s=? AND ", field->name);
  725. }
  726. ast_str_append(&sql, 0, "%s=?", keyfield);
  727. cps.sql = ast_str_buffer(sql);
  728. if (ast_string_field_init(&cps, 256)) {
  729. ast_odbc_release_obj(obj);
  730. return -1;
  731. }
  732. stmt = ast_odbc_prepare_and_execute(obj, custom_prepare, &cps);
  733. ast_string_field_free_memory(&cps);
  734. if (!stmt) {
  735. ast_odbc_release_obj(obj);
  736. return -1;
  737. }
  738. res = SQLRowCount(stmt, &rowcount);
  739. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  740. ast_odbc_release_obj(obj);
  741. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  742. ast_log(LOG_WARNING, "SQL Row Count error! [%s]\n", ast_str_buffer(sql));
  743. return -1;
  744. }
  745. if (rowcount >= 0)
  746. return (int)rowcount;
  747. return -1;
  748. }
  749. struct config_odbc_obj {
  750. char *sql;
  751. unsigned long cat_metric;
  752. char category[128];
  753. char var_name[128];
  754. char *var_val;
  755. unsigned long var_val_size;
  756. SQLLEN err;
  757. };
  758. static SQLHSTMT length_determination_odbc_prepare(struct odbc_obj *obj, void *data)
  759. {
  760. struct config_odbc_obj *q = data;
  761. SQLHSTMT sth;
  762. int res;
  763. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth);
  764. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  765. ast_verb(4, "Failure in AllocStatement %d\n", res);
  766. return NULL;
  767. }
  768. res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS);
  769. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  770. ast_verb(4, "Error in PREPARE %d\n", res);
  771. SQLFreeHandle(SQL_HANDLE_STMT, sth);
  772. return NULL;
  773. }
  774. SQLBindCol(sth, 1, SQL_C_ULONG, &q->var_val_size, sizeof(q->var_val_size), &q->err);
  775. return sth;
  776. }
  777. static SQLHSTMT config_odbc_prepare(struct odbc_obj *obj, void *data)
  778. {
  779. struct config_odbc_obj *q = data;
  780. SQLHSTMT sth;
  781. int res;
  782. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth);
  783. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  784. ast_verb(4, "Failure in AllocStatement %d\n", res);
  785. return NULL;
  786. }
  787. res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS);
  788. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  789. ast_verb(4, "Error in PREPARE %d\n", res);
  790. SQLFreeHandle(SQL_HANDLE_STMT, sth);
  791. return NULL;
  792. }
  793. SQLBindCol(sth, 1, SQL_C_ULONG, &q->cat_metric, sizeof(q->cat_metric), &q->err);
  794. SQLBindCol(sth, 2, SQL_C_CHAR, q->category, sizeof(q->category), &q->err);
  795. SQLBindCol(sth, 3, SQL_C_CHAR, q->var_name, sizeof(q->var_name), &q->err);
  796. SQLBindCol(sth, 4, SQL_C_CHAR, q->var_val, q->var_val_size, &q->err);
  797. return sth;
  798. }
  799. static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl, const char *who_asked)
  800. {
  801. struct ast_variable *new_v;
  802. struct ast_category *cur_cat;
  803. int res = 0;
  804. struct odbc_obj *obj;
  805. struct ast_str *sql = ast_str_thread_get(&sql_buf, SQL_BUF_SIZE);
  806. unsigned int last_cat_metric = 0;
  807. SQLSMALLINT rowcount = 0;
  808. SQLHSTMT stmt;
  809. char last[128] = "";
  810. struct config_odbc_obj q;
  811. struct ast_flags loader_flags = { 0 };
  812. struct ast_flags connected_flag = { RES_ODBC_CONNECTED };
  813. memset(&q, 0, sizeof(q));
  814. if (!file || !strcmp (file, "res_config_odbc.conf") || !sql) {
  815. return NULL; /* cant configure myself with myself ! */
  816. }
  817. obj = ast_odbc_request_obj2(database, connected_flag);
  818. if (!obj)
  819. return NULL;
  820. ast_str_set(&sql, 0, "SELECT MAX(LENGTH(var_val)) FROM %s WHERE filename='%s'",
  821. table, file);
  822. q.sql = ast_str_buffer(sql);
  823. stmt = ast_odbc_prepare_and_execute(obj, length_determination_odbc_prepare, &q);
  824. if (!stmt) {
  825. ast_log(LOG_WARNING, "SQL select error! [%s]\n", ast_str_buffer(sql));
  826. ast_odbc_release_obj(obj);
  827. return NULL;
  828. }
  829. res = SQLNumResultCols(stmt, &rowcount);
  830. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  831. ast_log(LOG_WARNING, "SQL NumResultCols error! [%s]\n", ast_str_buffer(sql));
  832. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  833. ast_odbc_release_obj(obj);
  834. return NULL;
  835. }
  836. if (!rowcount) {
  837. ast_log(LOG_NOTICE, "found nothing\n");
  838. ast_odbc_release_obj(obj);
  839. return cfg;
  840. }
  841. /* There will be only one result for this, the maximum length of a variable value */
  842. if (SQLFetch(stmt) == SQL_NO_DATA) {
  843. ast_log(LOG_NOTICE, "Failed to determine maximum length of a configuration value\n");
  844. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  845. ast_odbc_release_obj(obj);
  846. return NULL;
  847. }
  848. /* Reset stuff to a fresh state for the actual query which will retrieve all configuration */
  849. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  850. ast_str_set(&sql, 0, "SELECT cat_metric, category, var_name, var_val FROM %s ", table);
  851. ast_str_append(&sql, 0, "WHERE filename='%s' AND commented=0 ", file);
  852. ast_str_append(&sql, 0, "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ");
  853. q.sql = ast_str_buffer(sql);
  854. q.var_val_size += 1;
  855. q.var_val = ast_malloc(q.var_val_size);
  856. if (!q.var_val) {
  857. ast_log(LOG_WARNING, "Could not create buffer for reading in configuration values for '%s'\n", file);
  858. ast_odbc_release_obj(obj);
  859. return NULL;
  860. }
  861. stmt = ast_odbc_prepare_and_execute(obj, config_odbc_prepare, &q);
  862. if (!stmt) {
  863. ast_log(LOG_WARNING, "SQL select error! [%s]\n", ast_str_buffer(sql));
  864. ast_odbc_release_obj(obj);
  865. ast_free(q.var_val);
  866. return NULL;
  867. }
  868. res = SQLNumResultCols(stmt, &rowcount);
  869. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  870. ast_log(LOG_WARNING, "SQL NumResultCols error! [%s]\n", ast_str_buffer(sql));
  871. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  872. ast_odbc_release_obj(obj);
  873. ast_free(q.var_val);
  874. return NULL;
  875. }
  876. if (!rowcount) {
  877. ast_log(LOG_NOTICE, "found nothing\n");
  878. ast_odbc_release_obj(obj);
  879. ast_free(q.var_val);
  880. return cfg;
  881. }
  882. cur_cat = ast_config_get_current_category(cfg);
  883. while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
  884. if (!strcmp (q.var_name, "#include")) {
  885. if (!ast_config_internal_load(q.var_val, cfg, loader_flags, "", who_asked)) {
  886. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  887. ast_odbc_release_obj(obj);
  888. ast_free(q.var_val);
  889. return NULL;
  890. }
  891. continue;
  892. }
  893. if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
  894. cur_cat = ast_category_new_dynamic(q.category);
  895. if (!cur_cat) {
  896. break;
  897. }
  898. strcpy(last, q.category);
  899. last_cat_metric = q.cat_metric;
  900. ast_category_append(cfg, cur_cat);
  901. }
  902. new_v = ast_variable_new(q.var_name, q.var_val, "");
  903. ast_variable_append(cur_cat, new_v);
  904. }
  905. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  906. ast_odbc_release_obj(obj);
  907. ast_free(q.var_val);
  908. return cfg;
  909. }
  910. #define warn_length(col, size) ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is not long enough to contain realtime data (needs %d)\n", table, database, col->name, size)
  911. #define warn_type(col, type) ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is of the incorrect type (%d) to contain the required realtime data\n", table, database, col->name, col->type)
  912. static int require_odbc(const char *database, const char *table, va_list ap)
  913. {
  914. struct odbc_cache_tables *tableptr = ast_odbc_find_table(database, table);
  915. struct odbc_cache_columns *col;
  916. char *elm;
  917. int type, size;
  918. if (!tableptr) {
  919. return -1;
  920. }
  921. while ((elm = va_arg(ap, char *))) {
  922. type = va_arg(ap, require_type);
  923. size = va_arg(ap, int);
  924. /* Check if the field matches the criteria */
  925. AST_RWLIST_TRAVERSE(&tableptr->columns, col, list) {
  926. if (strcmp(col->name, elm) == 0) {
  927. /* Type check, first. Some fields are more particular than others */
  928. switch (col->type) {
  929. case SQL_CHAR:
  930. case SQL_VARCHAR:
  931. case SQL_LONGVARCHAR:
  932. #ifdef HAVE_ODBC_WCHAR
  933. case SQL_WCHAR:
  934. case SQL_WVARCHAR:
  935. case SQL_WLONGVARCHAR:
  936. #endif
  937. case SQL_BINARY:
  938. case SQL_VARBINARY:
  939. case SQL_LONGVARBINARY:
  940. case SQL_GUID:
  941. #define CHECK_SIZE(n) \
  942. if (col->size < n) { \
  943. warn_length(col, n); \
  944. } \
  945. break;
  946. switch (type) {
  947. case RQ_UINTEGER1: CHECK_SIZE(3) /* 255 */
  948. case RQ_INTEGER1: CHECK_SIZE(4) /* -128 */
  949. case RQ_UINTEGER2: CHECK_SIZE(5) /* 65535 */
  950. case RQ_INTEGER2: CHECK_SIZE(6) /* -32768 */
  951. case RQ_UINTEGER3: /* 16777215 */
  952. case RQ_INTEGER3: CHECK_SIZE(8) /* -8388608 */
  953. case RQ_DATE: /* 2008-06-09 */
  954. case RQ_UINTEGER4: CHECK_SIZE(10) /* 4200000000 */
  955. case RQ_INTEGER4: CHECK_SIZE(11) /* -2100000000 */
  956. case RQ_DATETIME: /* 2008-06-09 16:03:47 */
  957. case RQ_UINTEGER8: CHECK_SIZE(19) /* trust me */
  958. case RQ_INTEGER8: CHECK_SIZE(20) /* ditto */
  959. case RQ_FLOAT:
  960. case RQ_CHAR: CHECK_SIZE(size)
  961. }
  962. #undef CHECK_SIZE
  963. break;
  964. case SQL_TYPE_DATE:
  965. if (type != RQ_DATE) {
  966. warn_type(col, type);
  967. }
  968. break;
  969. case SQL_TYPE_TIMESTAMP:
  970. case SQL_TIMESTAMP:
  971. if (type != RQ_DATE && type != RQ_DATETIME) {
  972. warn_type(col, type);
  973. }
  974. break;
  975. case SQL_BIT:
  976. warn_length(col, size);
  977. break;
  978. #define WARN_TYPE_OR_LENGTH(n) \
  979. if (!ast_rq_is_int(type)) { \
  980. warn_type(col, type); \
  981. } else { \
  982. warn_length(col, n); \
  983. }
  984. case SQL_TINYINT:
  985. if (type != RQ_UINTEGER1) {
  986. WARN_TYPE_OR_LENGTH(size)
  987. }
  988. break;
  989. case SQL_C_STINYINT:
  990. if (type != RQ_INTEGER1) {
  991. WARN_TYPE_OR_LENGTH(size)
  992. }
  993. break;
  994. case SQL_C_USHORT:
  995. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_UINTEGER2) {
  996. WARN_TYPE_OR_LENGTH(size)
  997. }
  998. break;
  999. case SQL_SMALLINT:
  1000. case SQL_C_SSHORT:
  1001. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_INTEGER2) {
  1002. WARN_TYPE_OR_LENGTH(size)
  1003. }
  1004. break;
  1005. case SQL_C_ULONG:
  1006. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
  1007. type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
  1008. type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
  1009. type != RQ_INTEGER4) {
  1010. WARN_TYPE_OR_LENGTH(size)
  1011. }
  1012. break;
  1013. case SQL_INTEGER:
  1014. case SQL_C_SLONG:
  1015. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
  1016. type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
  1017. type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
  1018. type != RQ_INTEGER4) {
  1019. WARN_TYPE_OR_LENGTH(size)
  1020. }
  1021. break;
  1022. case SQL_C_UBIGINT:
  1023. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
  1024. type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
  1025. type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
  1026. type != RQ_UINTEGER4 && type != RQ_INTEGER4 &&
  1027. type != RQ_INTEGER8) {
  1028. WARN_TYPE_OR_LENGTH(size)
  1029. }
  1030. break;
  1031. case SQL_BIGINT:
  1032. case SQL_C_SBIGINT:
  1033. if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
  1034. type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
  1035. type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
  1036. type != RQ_UINTEGER4 && type != RQ_INTEGER4 &&
  1037. type != RQ_INTEGER8) {
  1038. WARN_TYPE_OR_LENGTH(size)
  1039. }
  1040. break;
  1041. #undef WARN_TYPE_OR_LENGTH
  1042. case SQL_NUMERIC:
  1043. case SQL_DECIMAL:
  1044. case SQL_FLOAT:
  1045. case SQL_REAL:
  1046. case SQL_DOUBLE:
  1047. if (!ast_rq_is_int(type) && type != RQ_FLOAT) {
  1048. warn_type(col, type);
  1049. }
  1050. break;
  1051. default:
  1052. ast_log(LOG_WARNING, "Realtime table %s@%s: column type (%d) unrecognized for column '%s'\n", table, database, col->type, elm);
  1053. }
  1054. break;
  1055. }
  1056. }
  1057. if (!col) {
  1058. ast_log(LOG_WARNING, "Realtime table %s@%s requires column '%s', but that column does not exist!\n", table, database, elm);
  1059. }
  1060. }
  1061. AST_RWLIST_UNLOCK(&tableptr->columns);
  1062. return 0;
  1063. }
  1064. #undef warn_length
  1065. #undef warn_type
  1066. static int unload_odbc(const char *a, const char *b)
  1067. {
  1068. return ast_odbc_clear_cache(a, b);
  1069. }
  1070. static struct ast_config_engine odbc_engine = {
  1071. .name = "odbc",
  1072. .load_func = config_odbc,
  1073. .realtime_func = realtime_odbc,
  1074. .realtime_multi_func = realtime_multi_odbc,
  1075. .store_func = store_odbc,
  1076. .destroy_func = destroy_odbc,
  1077. .update_func = update_odbc,
  1078. .update2_func = update2_odbc,
  1079. .require_func = require_odbc,
  1080. .unload_func = unload_odbc,
  1081. };
  1082. static int unload_module (void)
  1083. {
  1084. ast_config_engine_deregister(&odbc_engine);
  1085. return 0;
  1086. }
  1087. static int load_module (void)
  1088. {
  1089. ast_config_engine_register(&odbc_engine);
  1090. return 0;
  1091. }
  1092. static int reload_module(void)
  1093. {
  1094. return 0;
  1095. }
  1096. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Realtime ODBC configuration",
  1097. .support_level = AST_MODULE_SUPPORT_CORE,
  1098. .load = load_module,
  1099. .unload = unload_module,
  1100. .reload = reload_module,
  1101. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  1102. );