cdr_adaptive_odbc: Add ability to set character for quoted identifiers.
authorRodrigo Ramírez Norambuena <decipher.hk@gmail.com>
Tue, 21 Apr 2015 22:27:16 +0000 (19:27 -0300)
committerRodrigo Ramírez Norambuena <decipher.hk@gmail.com>
Tue, 5 May 2015 08:38:33 +0000 (04:38 -0400)
Added the ability to set the character to quote identifiers. This
allows adding the character at the start and end of table and column
names. This setting is configurable for cdr_adaptive_odbc via the
quoted_identifiers in configuration file cdr_adaptive_odbc.conf.

ASTERISK-25006

Change-Id: I0b9a56b79ca13a727a803d88ed3b8643e37632b8

CHANGES
cdr/cdr_adaptive_odbc.c
configs/samples/cdr_adaptive_odbc.conf.sample

diff --git a/CHANGES b/CHANGES
index 94ed559..18abdd4 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -136,6 +136,17 @@ cel_pgsql
 * Added a new option, 'usegmtime', which causes timestamps in CEL events
   to be logged in GMT.
 
+CDR Backends
+------------------
+
+cdr_adaptive_odbc
+------------------
+ * Added the ability to set the character to quote identifiers. This
+   allows adding the character at the start and end of table and column
+   names. This setting is configurable for cdr_adaptive_odbc via the
+   quoted_identifiers in configuration file cdr_adaptive_odbc.conf.
+
+
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 13.3.0 to Asterisk 13.4.0 ------------
 ------------------------------------------------------------------------------
index 83877cb..e6b60dc 100644 (file)
@@ -82,6 +82,7 @@ struct tables {
        char *connection;
        char *table;
        char *schema;
+       char quoted_identifiers;
        unsigned int usegmtime:1;
        AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
        AST_RWLIST_ENTRY(tables) list;
@@ -101,6 +102,7 @@ static int load_config(void)
        char connection[40];
        char table[40];
        char schema[40];
+       char quoted_identifiers;
        int lenconnection, lentable, lenschema, usegmtime = 0;
        SQLLEN sqlptr;
        int res = 0;
@@ -149,6 +151,16 @@ static int load_config(void)
                ast_copy_string(schema, tmp, sizeof(schema));
                lenschema = strlen(schema);
 
+               if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "quoted_identifiers"))) {
+                       tmp = "";
+               }
+               quoted_identifiers = tmp[0];
+               if (strlen(tmp) > 1) {
+                       ast_log(LOG_ERROR, "The quoted_identifiers setting only accepts a single character,"
+                               " while a value of '%s' was provided. This option has been disabled as a result.\n", tmp);
+                       quoted_identifiers = '\0';
+               }
+
                res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
                if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
                        ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
@@ -164,7 +176,7 @@ static int load_config(void)
                        continue;
                }
 
-               tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1 + lenschema + 1);
+               tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1 + lenschema + 1 + 1);
                if (!tableptr) {
                        ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'%s%s%s\n", table, connection,
                                lenschema ? " (schema '" : "", lenschema ? schema : "", lenschema ? "')" : "");
@@ -181,6 +193,7 @@ static int load_config(void)
                ast_copy_string(tableptr->connection, connection, lenconnection + 1);
                ast_copy_string(tableptr->table, table, lentable + 1);
                ast_copy_string(tableptr->schema, schema, lenschema + 1);
+               tableptr->quoted_identifiers = quoted_identifiers;
 
                ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
 
@@ -400,10 +413,27 @@ static int odbc_log(struct ast_cdr *cdr)
 
        AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
                int first = 1;
+               int quoted = 0;
+
+               if (tableptr->quoted_identifiers != '\0'){
+                       quoted = 1;
+               }
+
                if (ast_strlen_zero(tableptr->schema)) {
-                       ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
+                       if (quoted) {
+                               ast_str_set(&sql, 0, "INSERT INTO %c%s%c (",
+                                       tableptr->quoted_identifiers, tableptr->table, tableptr->quoted_identifiers );
+                       }else{
+                               ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
+                       }
                } else {
-                       ast_str_set(&sql, 0, "INSERT INTO %s.%s (", tableptr->schema, tableptr->table);
+                       if (quoted) {
+                               ast_str_set(&sql, 0, "INSERT INTO %c%s%c.%c%s%c (",
+                                               tableptr->quoted_identifiers, tableptr->schema, tableptr->quoted_identifiers,
+                                               tableptr->quoted_identifiers, tableptr->table,  tableptr->quoted_identifiers);
+                       }else{
+                               ast_str_set(&sql, 0, "INSERT INTO %s.%s (", tableptr->schema, tableptr->table);
+                       }
                }
                ast_str_set(&sql2, 0, " VALUES (");
 
@@ -708,7 +738,11 @@ static int odbc_log(struct ast_cdr *cdr)
                                        ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
                                        continue;
                                }
-                               ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
+                               if (quoted) {
+                                       ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
+                               } else {
+                                       ast_str_append(&sql, 0, "%s%c%s%c", first ? "" : ",", tableptr->quoted_identifiers, entry->name, tableptr->quoted_identifiers);
+                               }
                                first = 0;
                        } else if (entry->filtervalue
                                && ((!entry->negatefiltervalue && entry->filtervalue[0] != '\0')
index f3c806e..58a5966 100644 (file)
@@ -57,4 +57,7 @@
 ; for this is to allow different sections to specify different values for
 ; a certain named column, presumably separated by filters.
 ;static "Some Special Value" => identifier_code
+;
+; Add quoted indentifiers for table and columns names.
+;quoted_identifiers=" ; Default to null