Debian Codegraph @

cel/cel_pgsql.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008
5 *
6 * Steve Murphy - adapted to CEL, from:
7 * Matthew D. Hardeman <[email protected]>
8 * Adapted from the MySQL CDR logger originally by James Sharp
9 *
10 * Modified April, 2007; Dec, 2008
11 * Steve Murphy <[email protected]>
12
13 * Modified September 2003
14 * Matthew D. Hardeman <[email protected]>
15 *
16 * See http://www.asterisk.org for more information about
17 * the Asterisk project. Please do not directly contact
18 * any of the maintainers of this project for assistance;
19 * the project provides a web site, mailing lists and IRC
20 * channels for your use.
21 *
22 * This program is free software, distributed under the terms of
23 * the GNU General Public License Version 2. See the LICENSE file
24 * at the top of the source tree.
25 */
26
27 /*! \file
28 *
29 * \brief PostgreSQL CEL logger
30 *
31 * \author Steve Murphy <[email protected]>
32 * PostgreSQL http://www.postgresql.org/
33 * \ingroup cel_drivers
34 */
35
36 /*** MODULEINFO
37 <depend>pgsql</depend>
38 <support_level>extended</support_level>
39 ***/
40
41 #include "asterisk.h"
42
43 #include <libpq-fe.h>
44
45 #include "asterisk/config.h"
46 #include "asterisk/options.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/cel.h"
49 #include "asterisk/module.h"
50 #include "asterisk/logger.h"
51 #include "asterisk.h"
52
53 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
54
55 #define PGSQL_BACKEND_NAME "CEL PGSQL backend"
56
57 #define PGSQL_MIN_VERSION_SCHEMA 70300
58
59 static char *config = "cel_pgsql.conf";
60
61 static char *pghostname;
62 static char *pgdbname;
63 static char *pgdbuser;
64 static char *pgpassword;
65 static char *pgappname;
66 static char *pgdbport;
67 static char *table;
68 static char *schema;
69
70 static int connected = 0;
71 /* Optimization to reduce number of memory allocations */
72 static int maxsize = 512, maxsize2 = 512;
73 static int usegmtime = 0;
74
75 /*! \brief show_user_def is off by default */
76 #define CEL_SHOW_USERDEF_DEFAULT 0
77
78 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
79 static unsigned char cel_show_user_def;
80
81 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
82
83 static PGconn *conn = NULL;
84 static PGresult *result = NULL;
85
86 struct columns {
87 char *name;
88 char *type;
89 int len;
90 unsigned int notnull:1;
91 unsigned int hasdefault:1;
92 AST_RWLIST_ENTRY(columns) list;
93 };
94
95 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
96
97 #define LENGTHEN_BUF(size, var_sql) \
98 do { \
99 /* Lengthen buffer, if necessary */ \
100 if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \
101 if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 3) / 512 + 1) * 512) != 0) { \
102 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", pghostname, table); \
103 ast_free(sql); \
104 ast_free(sql2); \
105 AST_RWLIST_UNLOCK(&psql_columns); \
106 return; \
107 } \
108 } \
109 } while (0)
110
111 #define LENGTHEN_BUF1(size) \
112 LENGTHEN_BUF(size, sql);
113 #define LENGTHEN_BUF2(size) \
114 LENGTHEN_BUF(size, sql2);
115
116 static void pgsql_reconnect(void)
117 {
118 struct ast_str *conn_info = ast_str_create(128);
119 if (!conn_info) {
120 ast_log(LOG_ERROR, "Failed to allocate memory for connection string.\n");
121 return;
122 }
123
124 if (conn) {
125 PQfinish(conn);
126 conn = NULL;
127 }
128
129 ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
130 pghostname, pgdbport, pgdbname, pgdbuser);
131
132 if (!ast_strlen_zero(pgappname)) {
133 ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
134 }
135
136 if (!ast_strlen_zero(pgpassword)) {
137 ast_str_append(&conn_info, 0, " password=%s", pgpassword);
138 }
139
140 conn = PQconnectdb(ast_str_buffer(conn_info));
141 ast_free(conn_info);
142 }
143
144
145 static void pgsql_log(struct ast_event *event)
146 {
147 struct ast_tm tm;
148 char timestr[128];
149 char *pgerror;
150 struct ast_cel_event_record record = {
151 .version = AST_CEL_EVENT_RECORD_VERSION,
152 };
153
154 if (ast_cel_fill_record(event, &record)) {
155 return;
156 }
157
158 ast_mutex_lock(&pgsql_lock);
159
160 ast_localtime(&record.event_time, &tm, usegmtime ? "GMT" : NULL);
161 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
162
163 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
164 pgsql_reconnect();
165 if (PQstatus(conn) != CONNECTION_BAD) {
166 connected = 1;
167 } else {
168 pgerror = PQerrorMessage(conn);
169 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
170 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
171 PQfinish(conn);
172 conn = NULL;
173 }
174 }
175 if (connected) {
176 struct columns *cur;
177 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
178 char buf[257];
179 char *escapebuf = NULL;
180 const char *value;
181 int first = 1;
182 size_t bufsize = 513;
183
184 escapebuf = ast_malloc(bufsize);
185 if (!escapebuf || !sql || !sql2) {
186 goto ast_log_cleanup;
187 }
188
189 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
190 ast_str_set(&sql2, 0, " VALUES (");
191
192 #define SEP (first ? "" : ",")
193
194 AST_RWLIST_RDLOCK(&psql_columns);
195 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
196 LENGTHEN_BUF1(strlen(cur->name) + 2);
197 ast_str_append(&sql, 0, "%s\"%s\"", SEP, cur->name);
198
199 if (strcmp(cur->name, "eventtime") == 0) {
200 if (strncmp(cur->type, "int", 3) == 0) {
201 LENGTHEN_BUF2(13);
202 ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
203 } else if (strncmp(cur->type, "float", 5) == 0) {
204 LENGTHEN_BUF2(31);
205 ast_str_append(&sql2, 0, "%s%f",
206 SEP,
207 (double) record.event_time.tv_sec +
208 (double) record.event_time.tv_usec / 1000000.0);
209 } else {
210 /* char, hopefully */
211 LENGTHEN_BUF2(31);
212 ast_localtime(&record.event_time, &tm, usegmtime ? "GMT" : NULL);
213 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
214 ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
215 }
216 } else if (strcmp(cur->name, "eventtype") == 0) {
217 if (cur->type[0] == 'i') {
218 /* Get integer, no need to escape anything */
219 LENGTHEN_BUF2(5);
220 ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
221 } else if (strncmp(cur->type, "float", 5) == 0) {
222 LENGTHEN_BUF2(31);
223 ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
224 } else {
225 /* Char field, probably */
226 const char *event_name;
227 size_t required_size;
228
229 event_name = (!cel_show_user_def
230 && record.event_type == AST_CEL_USER_DEFINED)
231 ? record.user_defined_name : record.event_name;
232 required_size = strlen(event_name) * 2 + 1;
233 if (required_size > bufsize) {
234 char *tmpbuf = ast_realloc(escapebuf, required_size);
235 if (!tmpbuf) {
236 AST_RWLIST_UNLOCK(&psql_columns);
237 goto ast_log_cleanup;
238 }
239 escapebuf = tmpbuf;
240 bufsize = required_size;
241 }
242 PQescapeStringConn(conn, escapebuf, event_name,
243 strlen(event_name), NULL);
244 LENGTHEN_BUF2(strlen(escapebuf) + 3);
245 ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
246 }
247 } else if (strcmp(cur->name, "amaflags") == 0) {
248 if (strncmp(cur->type, "int", 3) == 0) {
249 /* Integer, no need to escape anything */
250 LENGTHEN_BUF2(13);
251 ast_str_append(&sql2, 0, "%s%u", SEP, record.amaflag);
252 } else {
253 /* Although this is a char field, there are no special characters in the values for these fields */
254 LENGTHEN_BUF2(31);
255 ast_str_append(&sql2, 0, "%s'%u'", SEP, record.amaflag);
256 }
257 } else {
258 /* Arbitrary field, could be anything */
259 if (strcmp(cur->name, "userdeftype") == 0) {
260 value = record.user_defined_name;
261 } else if (strcmp(cur->name, "cid_name") == 0) {
262 value = record.caller_id_name;
263 } else if (strcmp(cur->name, "cid_num") == 0) {
264 value = record.caller_id_num;
265 } else if (strcmp(cur->name, "cid_ani") == 0) {
266 value = record.caller_id_ani;
267 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
268 value = record.caller_id_rdnis;
269 } else if (strcmp(cur->name, "cid_dnid") == 0) {
270 value = record.caller_id_dnid;
271 } else if (strcmp(cur->name, "exten") == 0) {
272 value = record.extension;
273 } else if (strcmp(cur->name, "context") == 0) {
274 value = record.context;
275 } else if (strcmp(cur->name, "channame") == 0) {
276 value = record.channel_name;
277 } else if (strcmp(cur->name, "appname") == 0) {
278 value = record.application_name;
279 } else if (strcmp(cur->name, "appdata") == 0) {
280 value = record.application_data;
281 } else if (strcmp(cur->name, "accountcode") == 0) {
282 value = record.account_code;
283 } else if (strcmp(cur->name, "peeraccount") == 0) {
284 value = record.peer_account;
285 } else if (strcmp(cur->name, "uniqueid") == 0) {
286 value = record.unique_id;
287 } else if (strcmp(cur->name, "linkedid") == 0) {
288 value = record.linked_id;
289 } else if (strcmp(cur->name, "userfield") == 0) {
290 value = record.user_field;
291 } else if (strcmp(cur->name, "peer") == 0) {
292 value = record.peer;
293 } else if (strcmp(cur->name, "extra") == 0) {
294 value = record.extra;
295 } else {
296 value = NULL;
297 }
298
299 if (value == NULL) {
300 ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
301 } else if (strncmp(cur->type, "int", 3) == 0) {
302 long long whatever;
303 if (value && sscanf(value, "%30lld", &whatever) == 1) {
304 LENGTHEN_BUF2(26);
305 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
306 } else {
307 LENGTHEN_BUF2(2);
308 ast_str_append(&sql2, 0, "%s0", SEP);
309 }
310 } else if (strncmp(cur->type, "float", 5) == 0) {
311 long double whatever;
312 if (value && sscanf(value, "%30Lf", &whatever) == 1) {
313 LENGTHEN_BUF2(51);
314 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
315 } else {
316 LENGTHEN_BUF2(2);
317 ast_str_append(&sql2, 0, "%s0", SEP);
318 }
319 /* XXX Might want to handle dates, times, and other misc fields here XXX */
320 } else {
321 if (value) {
322 size_t required_size = strlen(value) * 2 + 1;
323
324 /* If our argument size exceeds our buffer, grow it,
325 * as PQescapeStringConn() expects the buffer to be
326 * adequitely sized and does *NOT* do size checking.
327 */
328 if (required_size > bufsize) {
329 char *tmpbuf = ast_realloc(escapebuf, required_size);
330
331 if (!tmpbuf) {
332 AST_RWLIST_UNLOCK(&psql_columns);
333 goto ast_log_cleanup;
334 }
335
336 escapebuf = tmpbuf;
337 bufsize = required_size;
338 }
339 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
340 } else {
341 escapebuf[0] = '\0';
342 }
343 LENGTHEN_BUF2(strlen(escapebuf) + 3);
344 ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
345 }
346 }
347 first = 0;
348 }
349 AST_RWLIST_UNLOCK(&psql_columns);
350 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
351 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
352
353 ast_debug(3, "Inserting a CEL record: [%s].\n", ast_str_buffer(sql));
354 /* Test to be sure we're still connected... */
355 /* If we're connected, and connection is working, good. */
356 /* Otherwise, attempt reconnect. If it fails... sorry... */
357 if (PQstatus(conn) == CONNECTION_OK) {
358 connected = 1;
359 } else {
360 ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
361 PQreset(conn);
362 if (PQstatus(conn) == CONNECTION_OK) {
363 ast_log(LOG_NOTICE, "Connection reestablished.\n");
364 connected = 1;
365 } else {
366 pgerror = PQerrorMessage(conn);
367 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
368 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
369 PQfinish(conn);
370 conn = NULL;
371 connected = 0;
372 goto ast_log_cleanup;
373 }
374 }
375 result = PQexec(conn, ast_str_buffer(sql));
376 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
377 pgerror = PQresultErrorMessage(result);
378 ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
379 ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
380 ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
381 PQreset(conn);
382 if (PQstatus(conn) == CONNECTION_OK) {
383 ast_log(LOG_NOTICE, "Connection reestablished.\n");
384 connected = 1;
385 PQclear(result);
386 result = PQexec(conn, ast_str_buffer(sql));
387 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
388 pgerror = PQresultErrorMessage(result);
389 ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
390 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
391 }
392 }
393 }
394 PQclear(result);
395
396 /* Next time, just allocate buffers that are that big to start with. */
397 if (ast_str_strlen(sql) > maxsize) {
398 maxsize = ast_str_strlen(sql);
399 }
400 if (ast_str_strlen(sql2) > maxsize2) {
401 maxsize2 = ast_str_strlen(sql2);
402 }
403
404 ast_log_cleanup:
405 ast_free(sql);
406 ast_free(sql2);
407 ast_free(escapebuf);
408 }
409
410 ast_mutex_unlock(&pgsql_lock);
411 }
412
413 static int my_unload_module(void)
414 {
415 struct columns *current;
416
417 ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
418 AST_RWLIST_WRLOCK(&psql_columns);
419 if (conn) {
420 PQfinish(conn);
421 conn = NULL;
422 }
423 if (pghostname) {
424 ast_free(pghostname);
425 pghostname = NULL;
426 }
427 if (pgdbname) {
428 ast_free(pgdbname);
429 pgdbname = NULL;
430 }
431 if (pgdbuser) {
432 ast_free(pgdbuser);
433 pgdbuser = NULL;
434 }
435 if (pgpassword) {
436 ast_free(pgpassword);
437 pgpassword = NULL;
438 }
439 if (pgappname) {
440 ast_free(pgappname);
441 pgappname = NULL;
442 }
443 if (pgdbport) {
444 ast_free(pgdbport);
445 pgdbport = NULL;
446 }
447 if (table) {
448 ast_free(table);
449 table = NULL;
450 }
451 if (schema) {
452 ast_free(schema);
453 schema = NULL;
454 }
455 while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
456 ast_free(current);
457 }
458 AST_RWLIST_UNLOCK(&psql_columns);
459 return 0;
460 }
461
462 static int unload_module(void)
463 {
464 return my_unload_module();
465 }
466
467 static int process_my_load_module(struct ast_config *cfg)
468 {
469 struct ast_variable *var;
470 char *pgerror;
471 const char *tmp;
472 PGresult *result;
473 struct columns *cur;
474
475 if (!(var = ast_variable_browse(cfg, "global"))) {
476 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
477 return AST_MODULE_LOAD_DECLINE;
478 }
479 if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
480 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
481 tmp = ""; /* connect via UNIX-socket by default */
482 }
483 if (pghostname)
484 ast_free(pghostname);
485 if (!(pghostname = ast_strdup(tmp))) {
486 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
487 return AST_MODULE_LOAD_DECLINE;
488 }
489 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
490 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
491 tmp = "asteriskceldb";
492 }
493 if (pgdbname)
494 ast_free(pgdbname);
495 if (!(pgdbname = ast_strdup(tmp))) {
496 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
497 return AST_MODULE_LOAD_DECLINE;
498 }
499 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
500 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
501 tmp = "asterisk";
502 }
503 if (pgdbuser)
504 ast_free(pgdbuser);
505 if (!(pgdbuser = ast_strdup(tmp))) {
506 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
507 return AST_MODULE_LOAD_DECLINE;
508 }
509 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
510 ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
511 tmp = "";
512 }
513 if (pgpassword)
514 ast_free(pgpassword);
515 if (!(pgpassword = ast_strdup(tmp))) {
516 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
517 return AST_MODULE_LOAD_DECLINE;
518 }
519 if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
520 tmp = "";
521 }
522 if (pgappname) {
523 ast_free(pgappname);
524 }
525 if (!(pgappname = ast_strdup(tmp))) {
526 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying appname info\n");
527 return AST_MODULE_LOAD_DECLINE;
528 }
529
530 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
531 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
532 tmp = "5432";
533 }
534 if (pgdbport)
535 ast_free(pgdbport);
536 if (!(pgdbport = ast_strdup(tmp))) {
537 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
538 return AST_MODULE_LOAD_DECLINE;
539 }
540 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
541 ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
542 tmp = "cel";
543 }
544 if (table)
545 ast_free(table);
546 if (!(table = ast_strdup(tmp))) {
547 return AST_MODULE_LOAD_DECLINE;
548 }
549 cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
550 if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
551 cel_show_user_def = ast_true(tmp) ? 1 : 0;
552 }
553 if ((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) {
554 usegmtime = ast_true(tmp);
555 } else {
556 usegmtime = 0;
557 }
558 if (!(tmp = ast_variable_retrieve(cfg, "global", "schema"))) {
559 tmp = "";
560 }
561 if (schema) {
562 ast_free(schema);
563 }
564 if (!(schema = ast_strdup(tmp))) {
565 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying schema info\n");
566 return AST_MODULE_LOAD_DECLINE;
567 }
568 if (DEBUG_ATLEAST(3)) {
569 if (ast_strlen_zero(pghostname)) {
570 ast_log(LOG_DEBUG, "cel_pgsql: using default unix socket\n");
571 } else {
572 ast_log(LOG_DEBUG, "cel_pgsql: got hostname of %s\n", pghostname);
573 }
574 ast_log(LOG_DEBUG, "cel_pgsql: got port of %s\n", pgdbport);
575 ast_log(LOG_DEBUG, "cel_pgsql: got user of %s\n", pgdbuser);
576 ast_log(LOG_DEBUG, "cel_pgsql: got dbname of %s\n", pgdbname);
577 ast_log(LOG_DEBUG, "cel_pgsql: got password of %s\n", pgpassword);
578 ast_log(LOG_DEBUG, "cel_pgsql: got sql table name of %s\n", table);
579 ast_log(LOG_DEBUG, "cel_pgsql: got show_user_defined of %s\n",
580 cel_show_user_def ? "Yes" : "No");
581 }
582
583 pgsql_reconnect();
584 if (PQstatus(conn) != CONNECTION_BAD) {
585 char sqlcmd[768];
586 char *fname, *ftype, *flen, *fnotnull, *fdef, *tablename, *tmp_tablename;
587 int i, rows, version;
588
589 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
590 connected = 1;
591
592 version = PQserverVersion(conn);
593 /* Remove any schema name from the table */
594 if ((tmp_tablename = strrchr(table, '.'))) {
595 tmp_tablename++;
596 } else {
597 tmp_tablename = table;
598 }
599 tablename = ast_alloca(strlen(tmp_tablename) * 2 + 1);
600 PQescapeStringConn(conn, tablename, tmp_tablename, strlen(tmp_tablename), NULL);
601 if (version >= PGSQL_MIN_VERSION_SCHEMA) {
602 char *schemaname;
603 int lenschema;
604 lenschema = strlen(schema);
605 schemaname = ast_alloca(lenschema * 2 + 1);
606 PQescapeStringConn(conn, schemaname, schema, lenschema, NULL);
607
608 snprintf(sqlcmd, sizeof(sqlcmd),
609 "SELECT a.attname, t.typname, a.attlen, a.attnotnull, pg_catalog.pg_get_expr(d.adbin, d.adrelid) adsrc, a.atttypmod "
610 "FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
611 "AND c.relname = '%s' AND n.nspname = %s%s%s) "
612 "INNER JOIN pg_catalog.pg_attribute a ON ("
613 "NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) "
614 "INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) "
615 "LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid "
616 "AND d.adnum = a.attnum "
617 "ORDER BY n.nspname, c.relname, attnum",
618 tablename,
619 lenschema == 0 ? "" : "'", lenschema == 0 ? "current_schema()" : schemaname, lenschema == 0 ? "" : "'");
620 } else {
621 snprintf(sqlcmd, sizeof(sqlcmd),
622 "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod "
623 "FROM pg_class c, pg_type t, pg_attribute a "
624 "LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid "
625 "AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid "
626 "AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", tablename);
627 }
628 /* Query the columns */
629 result = PQexec(conn, sqlcmd);
630 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
631 pgerror = PQresultErrorMessage(result);
632 ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
633 PQclear(result);
634 unload_module();
635 return AST_MODULE_LOAD_DECLINE;
636 }
637
638 rows = PQntuples(result);
639 for (i = 0; i < rows; i++) {
640 fname = PQgetvalue(result, i, 0);
641 ftype = PQgetvalue(result, i, 1);
642 flen = PQgetvalue(result, i, 2);
643 fnotnull = PQgetvalue(result, i, 3);
644 fdef = PQgetvalue(result, i, 4);
645 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
646 cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
647 if (cur) {
648 sscanf(flen, "%30d", &cur->len);
649 cur->name = (char *)cur + sizeof(*cur);
650 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
651 strcpy(cur->name, fname);
652 strcpy(cur->type, ftype);
653 if (*fnotnull == 't') {
654 cur->notnull = 1;
655 } else {
656 cur->notnull = 0;
657 }
658 if (!ast_strlen_zero(fdef)) {
659 cur->hasdefault = 1;
660 } else {
661 cur->hasdefault = 0;
662 }
663 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
664 }
665 }
666 PQclear(result);
667 } else {
668 pgerror = PQerrorMessage(conn);
669 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
670 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
671 connected = 0;
672 PQfinish(conn);
673 conn = NULL;
674 }
675 return AST_MODULE_LOAD_SUCCESS;
676 }
677
678 static int my_load_module(int reload)
679 {
680 struct ast_config *cfg;
681 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
682
683 if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
684 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
685 return AST_MODULE_LOAD_DECLINE;
686 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
687 return AST_MODULE_LOAD_SUCCESS;
688 }
689
690 if (reload) {
691 my_unload_module();
692 }
693
694 process_my_load_module(cfg);
695 ast_config_destroy(cfg);
696
697 if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
698 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
699 return AST_MODULE_LOAD_DECLINE;
700 }
701
702 return AST_MODULE_LOAD_SUCCESS;
703 }
704
705 static int load_module(void)
706 {
707 return my_load_module(0);
708 }
709
710 static int reload(void)
711 {
712 return my_load_module(1);
713 }
714
715 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
716 .support_level = AST_MODULE_SUPPORT_EXTENDED,
717 .load = load_module,
718 .unload = unload_module,
719 .reload = reload,
720 .load_pri = AST_MODPRI_CDR_DRIVER,
721 .requires = "cel",
722 );