99374a824e7c46ec2a3e5237a32940a9ca2c9292
[asterisk/asterisk.git] / cel / cel_pgsql.c
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 <mhardemn@papersoft.com> 
8  * Adapted from the MySQL CDR logger originally by James Sharp 
9  *
10  * Modified April, 2007; Dec, 2008
11  * Steve Murphy <murf@digium.com>
12
13  * Modified September 2003
14  * Matthew D. Hardeman <mhardemn@papersoft.com>
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 <murf@digium.com>
32  * \extref PostgreSQL http://www.postgresql.org/
33  *
34  * See also
35  * \arg \ref Config_cel
36  * \extref PostgreSQL http://www.postgresql.org/
37  * \ingroup cel_drivers
38  */
39
40 /*** MODULEINFO
41         <depend>pgsql</depend>
42         <support_level>extended</support_level>
43  ***/
44
45 #include "asterisk.h"
46
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48
49 #include <libpq-fe.h>
50
51 #include "asterisk/config.h"
52 #include "asterisk/options.h"
53 #include "asterisk/channel.h"
54 #include "asterisk/cel.h"
55 #include "asterisk/module.h"
56 #include "asterisk/logger.h"
57 #include "asterisk.h"
58
59 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
60
61 static char *config = "cel_pgsql.conf";
62 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
63 static int connected = 0;
64 static int maxsize = 512, maxsize2 = 512;
65
66 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
67
68 static PGconn   *conn = NULL;
69 static PGresult *result = NULL;
70 static struct ast_event_sub *event_sub = NULL;
71
72 struct columns {
73         char *name;
74         char *type;
75         int len;
76         unsigned int notnull:1;
77         unsigned int hasdefault:1;
78         AST_RWLIST_ENTRY(columns) list;
79 };
80
81 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
82
83 #define LENGTHEN_BUF1(size) \
84         do { \
85                 /* Lengthen buffer, if necessary */ \
86                 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
87                         if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
88                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
89                                 ast_free(sql); \
90                                 ast_free(sql2); \
91                                 AST_RWLIST_UNLOCK(&psql_columns); \
92                                 return; \
93                         } \
94                 } \
95         } while (0)
96
97 #define LENGTHEN_BUF2(size) \
98         do { \
99                 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
100                         if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
101                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
102                                 ast_free(sql); \
103                                 ast_free(sql2); \
104                                 AST_RWLIST_UNLOCK(&psql_columns); \
105                                 return; \
106                         } \
107                 } \
108         } while (0)
109
110 static void pgsql_log(const struct ast_event *event, void *userdata)
111 {
112         struct ast_tm tm;
113         char timestr[128];
114         char *pgerror;
115         struct ast_cel_event_record record = {
116                 .version = AST_CEL_EVENT_RECORD_VERSION,
117         };
118
119         if (ast_cel_fill_record(event, &record)) {
120                 return;
121         }
122
123         ast_mutex_lock(&pgsql_lock);
124
125         ast_localtime(&record.event_time, &tm, NULL);
126         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
127
128         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
129                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
130                 if (PQstatus(conn) != CONNECTION_BAD) {
131                         connected = 1;
132                 } else {
133                         pgerror = PQerrorMessage(conn);
134                         ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
135                         ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
136                         PQfinish(conn);
137                         conn = NULL;
138                 }
139         }
140         if (connected) {
141                 struct columns *cur;
142                 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
143                 char buf[257], escapebuf[513];
144                 const char *value;
145                 int first = 1;
146
147                 if (!sql || !sql2) {
148                         goto ast_log_cleanup;
149                 }
150
151                 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
152                 ast_str_set(&sql2, 0, " VALUES (");
153
154 #define SEP (first ? "" : ",")
155
156                 AST_RWLIST_RDLOCK(&psql_columns);
157                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
158                         LENGTHEN_BUF1(strlen(cur->name) + 2);
159                         ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
160
161                         if (strcmp(cur->name, "eventtime") == 0) {
162                                 if (strncmp(cur->type, "int", 3) == 0) {
163                                         LENGTHEN_BUF2(13);
164                                         ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
165                                 } else if (strncmp(cur->type, "float", 5) == 0) {
166                                         LENGTHEN_BUF2(31);
167                                         ast_str_append(&sql2, 0, "%s%f",
168                                                 SEP,
169                                                 (double) record.event_time.tv_sec +
170                                                 (double) record.event_time.tv_usec / 1000000.0);
171                                 } else {
172                                         /* char, hopefully */
173                                         LENGTHEN_BUF2(31);
174                                         ast_localtime(&record.event_time, &tm, NULL);
175                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
176                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
177                                 }
178                         } else if (strcmp(cur->name, "eventtype") == 0) {
179                                 if (cur->type[0] == 'i') {
180                                         /* Get integer, no need to escape anything */
181                                         LENGTHEN_BUF2(5);
182                                         ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
183                                 } else if (strncmp(cur->type, "float", 5) == 0) {
184                                         LENGTHEN_BUF2(31);
185                                         ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
186                                 } else {
187                                         /* Char field, probably */
188                                         LENGTHEN_BUF2(strlen(record.event_name) + 1);
189                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, record.event_name);
190                                 }
191                         } else if (strcmp(cur->name, "amaflags") == 0) {
192                                 if (strncmp(cur->type, "int", 3) == 0) {
193                                         /* Integer, no need to escape anything */
194                                         LENGTHEN_BUF2(13);
195                                         ast_str_append(&sql2, 0, "%s%d", SEP, record.amaflag);
196                                 } else {
197                                         /* Although this is a char field, there are no special characters in the values for these fields */
198                                         LENGTHEN_BUF2(31);
199                                         ast_str_append(&sql2, 0, "%s'%d'", SEP, record.amaflag);
200                                 }
201                         } else {
202                                 /* Arbitrary field, could be anything */
203                                 if (strcmp(cur->name, "userdeftype") == 0) {
204                                         value = record.user_defined_name;
205                                 } else if (strcmp(cur->name, "cid_name") == 0) {
206                                         value = record.caller_id_name;
207                                 } else if (strcmp(cur->name, "cid_num") == 0) {
208                                         value = record.caller_id_num;
209                                 } else if (strcmp(cur->name, "cid_ani") == 0) {
210                                         value = record.caller_id_ani;
211                                 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
212                                         value = record.caller_id_rdnis;
213                                 } else if (strcmp(cur->name, "cid_dnid") == 0) {
214                                         value = record.caller_id_dnid;
215                                 } else if (strcmp(cur->name, "exten") == 0) {
216                                         value = record.extension;
217                                 } else if (strcmp(cur->name, "context") == 0) {
218                                         value = record.context;
219                                 } else if (strcmp(cur->name, "channame") == 0) {
220                                         value = record.channel_name;
221                                 } else if (strcmp(cur->name, "appname") == 0) {
222                                         value = record.application_name;
223                                 } else if (strcmp(cur->name, "appdata") == 0) {
224                                         value = record.application_data;
225                                 } else if (strcmp(cur->name, "accountcode") == 0) {
226                                         value = record.account_code;
227                                 } else if (strcmp(cur->name, "peeraccount") == 0) {
228                                         value = record.peer_account;
229                                 } else if (strcmp(cur->name, "uniqueid") == 0) {
230                                         value = record.unique_id;
231                                 } else if (strcmp(cur->name, "linkedid") == 0) {
232                                         value = record.linked_id;
233                                 } else if (strcmp(cur->name, "userfield") == 0) {
234                                         value = record.user_field;
235                                 } else if (strcmp(cur->name, "peer") == 0) {
236                                         value = record.peer;
237                                 } else if (strcmp(cur->name, "extra") == 0) {
238                                         value = record.extra;
239                                 } else {
240                                         value = NULL;
241                                 }
242
243                                 if (value == NULL) {
244                                         ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
245                                 } else if (strncmp(cur->type, "int", 3) == 0) {
246                                         long long whatever;
247                                         if (value && sscanf(value, "%30lld", &whatever) == 1) {
248                                                 LENGTHEN_BUF2(26);
249                                                 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
250                                         } else {
251                                                 LENGTHEN_BUF2(2);
252                                                 ast_str_append(&sql2, 0, "%s0", SEP);
253                                         }
254                                 } else if (strncmp(cur->type, "float", 5) == 0) {
255                                         long double whatever;
256                                         if (value && sscanf(value, "%30Lf", &whatever) == 1) {
257                                                 LENGTHEN_BUF2(51);
258                                                 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
259                                         } else {
260                                                 LENGTHEN_BUF2(2);
261                                                 ast_str_append(&sql2, 0, "%s0", SEP);
262                                         }
263                                         /* XXX Might want to handle dates, times, and other misc fields here XXX */
264                                 } else {
265                                         if (value) {
266                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
267                                         } else {
268                                                 escapebuf[0] = '\0';
269                                         }
270                                         LENGTHEN_BUF2(strlen(escapebuf) + 3);
271                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
272                                 }
273                         }
274                         first = 0;
275                 }
276                 AST_RWLIST_UNLOCK(&psql_columns);
277                 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
278                 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
279                 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
280
281                 ast_debug(2, "inserting a CEL record.\n");
282                 /* Test to be sure we're still connected... */
283                 /* If we're connected, and connection is working, good. */
284                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
285                 if (PQstatus(conn) == CONNECTION_OK) {
286                         connected = 1;
287                 } else {
288                         ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
289                         PQreset(conn);
290                         if (PQstatus(conn) == CONNECTION_OK) {
291                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
292                                 connected = 1;
293                         } else {
294                                 pgerror = PQerrorMessage(conn);
295                                 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
296                                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
297                                 PQfinish(conn);
298                                 conn = NULL;
299                                 connected = 0;
300                                 goto ast_log_cleanup;
301                         }
302                 }
303                 result = PQexec(conn, ast_str_buffer(sql));
304                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
305                         pgerror = PQresultErrorMessage(result);
306                         ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
307                         ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
308                         ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
309                         PQreset(conn);
310                         if (PQstatus(conn) == CONNECTION_OK) {
311                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
312                                 connected = 1;
313                                 PQclear(result);
314                                 result = PQexec(conn, ast_str_buffer(sql));
315                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
316                                         pgerror = PQresultErrorMessage(result);
317                                         ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
318                                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
319                                 }
320                         }
321                         PQclear(result);
322                         goto ast_log_cleanup;
323                 }
324
325 ast_log_cleanup:
326                 ast_free(sql);
327                 ast_free(sql2);
328         }
329
330         ast_mutex_unlock(&pgsql_lock);
331 }
332
333 static int my_unload_module(void)
334 {
335         struct columns *current;
336         AST_RWLIST_WRLOCK(&psql_columns);
337         if (event_sub) {
338                 event_sub = ast_event_unsubscribe(event_sub);
339                 event_sub = NULL;
340         }
341         if (conn) {
342                 PQfinish(conn);
343                 conn = NULL;
344         }
345         if (pghostname) {
346                 ast_free(pghostname);
347                 pghostname = NULL;
348         }
349         if (pgdbname) {
350                 ast_free(pgdbname);
351                 pgdbname = NULL;
352         }
353         if (pgdbuser) {
354                 ast_free(pgdbuser);
355                 pgdbuser = NULL;
356         }
357         if (pgpassword) {
358                 ast_free(pgpassword);
359                 pgpassword = NULL;
360         }
361         if (pgdbport) {
362                 ast_free(pgdbport);
363                 pgdbport = NULL;
364         }
365         if (table) {
366                 ast_free(table);
367                 table = NULL;
368         }
369         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
370                 ast_free(current);
371         }
372         AST_RWLIST_UNLOCK(&psql_columns);
373         return 0;
374 }
375
376 static int unload_module(void)
377 {
378         return my_unload_module();
379 }
380
381 static int process_my_load_module(struct ast_config *cfg)
382 {
383         struct ast_variable *var;
384         char *pgerror;
385         const char *tmp;
386         PGresult *result;
387         struct columns *cur;
388
389         if (!(var = ast_variable_browse(cfg, "global"))) {
390                 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
391                 return AST_MODULE_LOAD_DECLINE;
392         }
393         if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
394                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
395                 tmp = "";       /* connect via UNIX-socket by default */
396         }
397         if (pghostname)
398                 ast_free(pghostname);
399         if (!(pghostname = ast_strdup(tmp))) {
400                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
401                 return AST_MODULE_LOAD_DECLINE;
402         }
403         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
404                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
405                 tmp = "asteriskceldb";
406         }
407         if (pgdbname)
408                 ast_free(pgdbname);
409         if (!(pgdbname = ast_strdup(tmp))) {
410                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
411                 return AST_MODULE_LOAD_DECLINE;
412         }
413         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
414                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
415                 tmp = "asterisk";
416         }
417         if (pgdbuser)
418                 ast_free(pgdbuser);
419         if (!(pgdbuser = ast_strdup(tmp))) {
420                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
421                 return AST_MODULE_LOAD_DECLINE;
422         }
423         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
424                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
425                 tmp = "";
426         }
427         if (pgpassword)
428                 ast_free(pgpassword);
429         if (!(pgpassword = ast_strdup(tmp))) {
430                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
431                 return AST_MODULE_LOAD_DECLINE;
432         }
433         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
434                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
435                 tmp = "5432";
436         }
437         if (pgdbport)
438                 ast_free(pgdbport);
439         if (!(pgdbport = ast_strdup(tmp))) {
440                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
441                 return AST_MODULE_LOAD_DECLINE;
442         }
443         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
444                 ast_log(LOG_WARNING,"CEL table not specified.  Assuming cel\n");
445                 tmp = "cel";
446         }
447         if (table)
448                 ast_free(table);
449         if (!(table = ast_strdup(tmp))) {
450                 return AST_MODULE_LOAD_DECLINE;
451         }
452         if (option_debug) {
453                 if (ast_strlen_zero(pghostname)) {
454                         ast_debug(3, "cel_pgsql: using default unix socket\n");
455                 } else {
456                         ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
457                 }
458                 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
459                 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
460                 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
461                 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
462                 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
463         }
464
465         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
466         if (PQstatus(conn) != CONNECTION_BAD) {
467                 char sqlcmd[512];
468                 char *fname, *ftype, *flen, *fnotnull, *fdef;
469                 char *tableptr;
470                 int i, rows;
471
472                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
473                 connected = 1;
474
475                 /* Remove any schema name from the table */
476                 if ((tableptr = strrchr(table, '.'))) {
477                         tableptr++;
478                 } else {
479                         tableptr = table;
480                 }
481
482                 /* Query the columns */
483                 snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc from pg_class c, pg_type t, pg_attribute a left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", tableptr);
484                 result = PQexec(conn, sqlcmd);
485                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
486                         pgerror = PQresultErrorMessage(result);
487                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
488                         PQclear(result);
489                         unload_module();
490                         return AST_MODULE_LOAD_DECLINE;
491                 }
492
493                 rows = PQntuples(result);
494                 for (i = 0; i < rows; i++) {
495                         fname = PQgetvalue(result, i, 0);
496                         ftype = PQgetvalue(result, i, 1);
497                         flen = PQgetvalue(result, i, 2);
498                         fnotnull = PQgetvalue(result, i, 3);
499                         fdef = PQgetvalue(result, i, 4);
500                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
501                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
502                         if (cur) {
503                                 sscanf(flen, "%30d", &cur->len);
504                                 cur->name = (char *)cur + sizeof(*cur);
505                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
506                                 strcpy(cur->name, fname);
507                                 strcpy(cur->type, ftype);
508                                 if (*fnotnull == 't') {
509                                         cur->notnull = 1;
510                                 } else {
511                                         cur->notnull = 0;
512                                 }
513                                 if (!ast_strlen_zero(fdef)) {
514                                         cur->hasdefault = 1;
515                                 } else {
516                                         cur->hasdefault = 0;
517                                 }
518                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
519                         }
520                 }
521                 PQclear(result);
522         } else {
523                 pgerror = PQerrorMessage(conn);
524                 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
525                 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
526                 connected = 0;
527         }
528         return AST_MODULE_LOAD_SUCCESS;
529 }
530
531 static int my_load_module(int reload)
532 {
533         struct ast_config *cfg;
534         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
535
536         if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
537                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
538                 return AST_MODULE_LOAD_DECLINE;
539         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
540                 return AST_MODULE_LOAD_SUCCESS;
541         }
542
543         process_my_load_module(cfg);
544         ast_config_destroy(cfg);
545
546         event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
547
548         if (!event_sub) {
549                 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
550                 return AST_MODULE_LOAD_DECLINE;
551         }
552
553         return AST_MODULE_LOAD_SUCCESS;
554 }
555
556 static int load_module(void)
557 {
558         return my_load_module(0);
559 }
560
561 static int reload(void)
562 {
563         my_unload_module();
564         return my_load_module(1);
565 }
566
567 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
568         .load = load_module,
569         .unload = unload_module,
570         .reload = reload,
571         .load_pri = AST_MODPRI_CDR_DRIVER,
572 );