If any field is not null, but has no default, then it must be set or the insert will...
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2003 - 2006
5  *
6  * Matthew D. Hardeman <mhardemn@papersoft.com> 
7  * Adapted from the MySQL CDR logger originally by James Sharp 
8  *
9  * Modified September 2003
10  * Matthew D. Hardeman <mhardemn@papersoft.com>
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  * This program is free software, distributed under the terms of
19  * the GNU General Public License Version 2. See the LICENSE file
20  * at the top of the source tree.
21  */
22
23 /*! \file
24  *
25  * \brief PostgreSQL CDR logger 
26  * 
27  * \author Matthew D. Hardeman <mhardemn@papersoft.com> 
28  * \extref PostgreSQL http://www.postgresql.org/
29  *
30  * See also
31  * \arg \ref Config_cdr
32  * \arg http://www.postgresql.org/
33  * \ingroup cdr_drivers
34  */
35
36 /*** MODULEINFO
37         <depend>pgsql</depend>
38  ***/
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include <time.h>
45
46 #include <libpq-fe.h>
47
48 #include "asterisk/config.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/cdr.h"
51 #include "asterisk/module.h"
52
53 #define DATE_FORMAT "'%Y-%m-%d %T'"
54
55 static char *name = "pgsql";
56 static char *config = "cdr_pgsql.conf";
57 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
58 static int connected = 0;
59 static int maxsize = 512, maxsize2 = 512;
60
61 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
62
63 static PGconn   *conn = NULL;
64
65 struct columns {
66         char *name;
67         char *type;
68         int len;
69         unsigned int notnull:1;
70         unsigned int hasdefault:1;
71         AST_RWLIST_ENTRY(columns) list;
72 };
73
74 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
75
76 #define LENGTHEN_BUF1(size)                                                                                                             \
77                         do {                                                                                                                            \
78                                 /* Lengthen buffer, if necessary */                                                             \
79                                 if ((newsize = lensql + (size) + 3) > sizesql) {        \
80                                         if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) {      \
81                                                 sql = tmp;                                                                                              \
82                                                 sizesql = (newsize / 512 + 1) * 512;                                    \
83                                         } else {                                                                                                        \
84                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
85                                                 ast_free(sql);                                                                                  \
86                                                 ast_free(sql2);                                                                                 \
87                                                 AST_RWLIST_UNLOCK(&psql_columns);                                               \
88                                                 return -1;                                                                                              \
89                                         }                                                                                                                       \
90                                 }                                                                                                                               \
91                         } while (0)
92
93 #define LENGTHEN_BUF2(size)                                                                                                             \
94                         do {                                                                                                                            \
95                                 if ((newsize = lensql2 + (size) + 3) > sizesql2) {                              \
96                                         if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) {     \
97                                                 sql2 = tmp;                                                                                             \
98                                                 sizesql2 = (newsize / 512 + 1) * 512;                                   \
99                                         } else {                                                                                                        \
100                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n");      \
101                                                 ast_free(sql);                                                                                  \
102                                                 ast_free(sql2);                                                                                 \
103                                                 AST_RWLIST_UNLOCK(&psql_columns);                                               \
104                                                 return -1;                                                                                              \
105                                         }                                                                                                                       \
106                                 }                                                                                                                               \
107                         } while (0)
108
109 static int pgsql_log(struct ast_cdr *cdr)
110 {
111         struct ast_tm tm;
112         char *pgerror;
113         PGresult *result;
114
115         ast_mutex_lock(&pgsql_lock);
116
117         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
118                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
119                 if (PQstatus(conn) != CONNECTION_BAD) {
120                         connected = 1;
121                 } else {
122                         pgerror = PQerrorMessage(conn);
123                         ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
124                         ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
125                         PQfinish(conn);
126                         conn = NULL;
127                 }
128         }
129
130         if (connected) {
131                 struct columns *cur;
132                 int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
133                 char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp, *value;
134                 char buf[257], escapebuf[513];
135   
136                 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", table);
137                 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
138   
139                 AST_RWLIST_RDLOCK(&psql_columns);
140                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
141                         /* For fields not set, simply skip them */
142                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
143                         if (!value) {
144                                 if (cur->notnull && !cur->hasdefault) {
145                                         /* Field is NOT NULL (but no default), must include it anyway */
146                                         LENGTHEN_BUF1(strlen(cur->name));
147                                         lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", cur->name);
148                                         LENGTHEN_BUF2(3);
149                                         strcat(sql2, "'',");
150                                         lensql2 += 3;
151                                 }
152                                 continue;
153                         }
154                         
155                         LENGTHEN_BUF1(strlen(cur->name));
156                         lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", cur->name);
157
158                         if (strcmp(cur->name, "start") == 0 || strcmp(cur->name, "calldate") == 0) {
159                                 if (strncmp(cur->type, "int", 3) == 0) {
160                                         LENGTHEN_BUF2(12);
161                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->start.tv_sec);
162                                 } else if (strncmp(cur->type, "float", 5) == 0) {
163                                         LENGTHEN_BUF2(30);
164                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0);
165                                 } else {
166                                         /* char, hopefully */
167                                         LENGTHEN_BUF2(30);
168                                         ast_localtime(&cdr->start, &tm, NULL);
169                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
170                                 }
171                         } else if (strcmp(cur->name, "answer") == 0) {
172                                 if (strncmp(cur->type, "int", 3) == 0) {
173                                         LENGTHEN_BUF2(12);
174                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->answer.tv_sec);
175                                 } else if (strncmp(cur->type, "float", 5) == 0) {
176                                         LENGTHEN_BUF2(30);
177                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0);
178                                 } else {
179                                         /* char, hopefully */
180                                         LENGTHEN_BUF2(30);
181                                         ast_localtime(&cdr->start, &tm, NULL);
182                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
183                                 }
184                         } else if (strcmp(cur->name, "end") == 0) {
185                                 if (strncmp(cur->type, "int", 3) == 0) {
186                                         LENGTHEN_BUF2(12);
187                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->end.tv_sec);
188                                 } else if (strncmp(cur->type, "float", 5) == 0) {
189                                         LENGTHEN_BUF2(30);
190                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0);
191                                 } else {
192                                         /* char, hopefully */
193                                         LENGTHEN_BUF2(30);
194                                         ast_localtime(&cdr->end, &tm, NULL);
195                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
196                                 }
197                         } else if (strcmp(cur->name, "duration") == 0 || strcmp(cur->name, "billsec") == 0) {
198                                 if (cur->type[0] == 'i') {
199                                         /* Get integer, no need to escape anything */
200                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
201                                         LENGTHEN_BUF2(12);
202                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%s", value);
203                                 } else if (strncmp(cur->type, "float", 5) == 0) {
204                                         struct timeval *tv = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
205                                         LENGTHEN_BUF2(30);
206                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->end.tv_sec - tv->tv_sec + cdr->end.tv_usec / 1000000.0 - tv->tv_usec / 1000000.0);
207                                 } else {
208                                         /* Char field, probably */
209                                         struct timeval *tv = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
210                                         LENGTHEN_BUF2(30);
211                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%f'", (double)cdr->end.tv_sec - tv->tv_sec + cdr->end.tv_usec / 1000000.0 - tv->tv_usec / 1000000.0);
212                                 }
213                         } else if (strcmp(cur->name, "disposition") == 0 || strcmp(cur->name, "amaflags") == 0) {
214                                 if (strncmp(cur->type, "int", 3) == 0) {
215                                         /* Integer, no need to escape anything */
216                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 1);
217                                         LENGTHEN_BUF2(12);
218                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%s", value);
219                                 } else {
220                                         /* Although this is a char field, there are no special characters in the values for these fields */
221                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
222                                         LENGTHEN_BUF2(30);
223                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%s'", value);
224                                 }
225                         } else {
226                                 /* Arbitrary field, could be anything */
227                                 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
228                                 if (strncmp(cur->type, "int", 3) == 0) {
229                                         long long whatever;
230                                         if (value && sscanf(value, "%lld", &whatever) == 1) {
231                                                 LENGTHEN_BUF2(25);
232                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld", whatever);
233                                         } else {
234                                                 LENGTHEN_BUF2(1);
235                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "0");
236                                         }
237                                 } else if (strncmp(cur->type, "float", 5) == 0) {
238                                         long double whatever;
239                                         if (value && sscanf(value, "%Lf", &whatever) == 1) {
240                                                 LENGTHEN_BUF2(50);
241                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%30Lf", whatever);
242                                         } else {
243                                                 LENGTHEN_BUF2(1);
244                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "0");
245                                         }
246                                 /* XXX Might want to handle dates, times, and other misc fields here XXX */
247                                 } else {
248                                         if (value)
249                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
250                                         else
251                                                 escapebuf[0] = '\0';
252                                         LENGTHEN_BUF2(strlen(escapebuf) + 2);
253                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%s'", escapebuf);
254                                 }
255                         }
256                         LENGTHEN_BUF2(1);
257                         strcat(sql2 + lensql2, ",");
258                         lensql2++;
259                 }
260                 AST_RWLIST_UNLOCK(&psql_columns);
261                 LENGTHEN_BUF1(lensql2);
262                 sql[lensql - 1] = ')';
263                 sql2[lensql2 - 1] = ')';
264                 strcat(sql + lensql, sql2);
265                 ast_verb(11, "[%s]\n", sql);
266
267                 ast_debug(2, "cdr_pgsql: inserting a CDR record.\n");
268
269                 /* Test to be sure we're still connected... */
270                 /* If we're connected, and connection is working, good. */
271                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
272                 if (PQstatus(conn) == CONNECTION_OK) {
273                         connected = 1;
274                 } else {
275                         ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
276                         PQreset(conn);
277                         if (PQstatus(conn) == CONNECTION_OK) {
278                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
279                                 connected = 1;
280                         } else {
281                                 pgerror = PQerrorMessage(conn);
282                                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
283                                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
284                                 PQfinish(conn);
285                                 conn = NULL;
286                                 connected = 0;
287                                 ast_mutex_unlock(&pgsql_lock);
288                                 return -1;
289                         }
290                 }
291                 result = PQexec(conn, sql);
292                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
293                         pgerror = PQresultErrorMessage(result);
294                         ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
295                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
296                         ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
297                         PQreset(conn);
298                         if (PQstatus(conn) == CONNECTION_OK) {
299                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
300                                 connected = 1;
301                                 PQclear(result);
302                                 result = PQexec(conn, sql);
303                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
304                                         pgerror = PQresultErrorMessage(result);
305                                         ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
306                                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
307                                 }
308                         }
309                         ast_mutex_unlock(&pgsql_lock);
310                         PQclear(result);
311                         return -1;
312                 }
313                 PQclear(result);
314         }
315         ast_mutex_unlock(&pgsql_lock);
316         return 0;
317 }
318
319 static int unload_module(void)
320 {
321         struct columns *cur;
322         ast_cdr_unregister(name);
323
324         /* Give all threads time to finish */
325         usleep(1);
326         PQfinish(conn);
327
328         if (pghostname)
329                 ast_free(pghostname);
330         if (pgdbname)
331                 ast_free(pgdbname);
332         if (pgdbuser)
333                 ast_free(pgdbuser);
334         if (pgpassword)
335                 ast_free(pgpassword);
336         if (pgdbport)
337                 ast_free(pgdbport);
338         if (table)
339                 ast_free(table);
340
341         AST_RWLIST_WRLOCK(&psql_columns);
342         while ((cur = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
343                 ast_free(cur);
344         }
345         AST_RWLIST_UNLOCK(&psql_columns);
346
347         return 0;
348 }
349
350 static int config_module(int reload)
351 {
352         struct ast_variable *var;
353         char *pgerror;
354         struct columns *cur;
355         PGresult *result;
356         const char *tmp;
357         struct ast_config *cfg;
358         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
359
360         if ((cfg = ast_config_load(config, config_flags)) == NULL) {
361                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
362                 return -1;
363         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
364                 return 0;
365
366         if (!(var = ast_variable_browse(cfg, "global"))) {
367                 ast_config_destroy(cfg);
368                 return 0;
369         }
370
371         if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
372                 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
373                 tmp = "";       /* connect via UNIX-socket by default */
374         }
375
376         if (pghostname)
377                 ast_free(pghostname);
378         if (!(pghostname = ast_strdup(tmp))) {
379                 ast_config_destroy(cfg);
380                 return -1;
381         }
382
383         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
384                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
385                 tmp = "asteriskcdrdb";
386         }
387
388         if (pgdbname)
389                 ast_free(pgdbname);
390         if (!(pgdbname = ast_strdup(tmp))) {
391                 ast_config_destroy(cfg);
392                 return -1;
393         }
394
395         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
396                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
397                 tmp = "asterisk";
398         }
399
400         if (pgdbuser)
401                 ast_free(pgdbuser);
402         if (!(pgdbuser = ast_strdup(tmp))) {
403                 ast_config_destroy(cfg);
404                 return -1;
405         }
406
407         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
408                 ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
409                 tmp = "";
410         }
411
412         if (pgpassword)
413                 ast_free(pgpassword);
414         if (!(pgpassword = ast_strdup(tmp))) {
415                 ast_config_destroy(cfg);
416                 return -1;
417         }
418
419         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
420                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
421                 tmp = "5432";
422         }
423
424         if (pgdbport)
425                 ast_free(pgdbport);
426         if (!(pgdbport = ast_strdup(tmp))) {
427                 ast_config_destroy(cfg);
428                 return -1;
429         }
430
431         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
432                 ast_log(LOG_WARNING,"CDR table not specified.  Assuming cdr\n");
433                 tmp = "cdr";
434         }
435
436         if (table)
437                 ast_free(table);
438         if (!(table = ast_strdup(tmp))) {
439                 ast_config_destroy(cfg);
440                 return -1;
441         }
442
443         if (option_debug) {
444                 if (ast_strlen_zero(pghostname)) {
445                         ast_debug(1, "cdr_pgsql: using default unix socket\n");
446                 } else {
447                         ast_debug(1, "cdr_pgsql: got hostname of %s\n", pghostname);
448                 }
449                 ast_debug(1, "cdr_pgsql: got port of %s\n", pgdbport);
450                 ast_debug(1, "cdr_pgsql: got user of %s\n", pgdbuser);
451                 ast_debug(1, "cdr_pgsql: got dbname of %s\n", pgdbname);
452                 ast_debug(1, "cdr_pgsql: got password of %s\n", pgpassword);
453                 ast_debug(1, "cdr_pgsql: got sql table name of %s\n", table);
454         }
455
456         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
457         if (PQstatus(conn) != CONNECTION_BAD) {
458                 char sqlcmd[256];
459                 char *fname, *ftype, *flen, *fnotnull, *fdef;
460                 int i, rows;
461                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
462                 connected = 1;
463
464                 /* Query the columns */
465                 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", table);
466                 result = PQexec(conn, sqlcmd);
467                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
468                         pgerror = PQresultErrorMessage(result);
469                         ast_log(LOG_ERROR, "cdr_pgsql: Failed to query database columns: %s\n", pgerror);
470                         PQclear(result);
471                         unload_module();
472                         return AST_MODULE_LOAD_DECLINE;
473                 }
474
475                 rows = PQntuples(result);
476                 for (i = 0; i < rows; i++) {
477                         fname = PQgetvalue(result, i, 0);
478                         ftype = PQgetvalue(result, i, 1);
479                         flen = PQgetvalue(result, i, 2);
480                         fnotnull = PQgetvalue(result, i, 3);
481                         fdef = PQgetvalue(result, i, 4);
482                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
483                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
484                         if (cur) {
485                                 sscanf(flen, "%d", &cur->len);
486                                 cur->name = (char *)cur + sizeof(*cur);
487                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
488                                 strcpy(cur->name, fname);
489                                 strcpy(cur->type, ftype);
490                                 if (*fnotnull == 't') {
491                                         cur->notnull = 1;
492                                 } else {
493                                         cur->notnull = 0;
494                                 }
495                                 if (!ast_strlen_zero(fdef)) {
496                                         cur->hasdefault = 1;
497                                 } else {
498                                         cur->hasdefault = 0;
499                                 }
500                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
501                         }
502                 }
503                 PQclear(result);
504         } else {
505                 pgerror = PQerrorMessage(conn);
506                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
507                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
508                 connected = 0;
509         }
510
511         ast_config_destroy(cfg);
512
513         return ast_cdr_register(name, ast_module_info->description, pgsql_log);
514 }
515
516 static int load_module(void)
517 {
518         return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
519 }
520
521 static int reload(void)
522 {
523         return config_module(1);
524 }
525
526 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
527                 .load = load_module,
528                 .unload = unload_module,
529                 .reload = reload,
530                );