diff options
author | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-12-13 08:36:35 +0000 |
---|---|---|
committer | tilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-12-13 08:36:35 +0000 |
commit | a41b34a63c52608e7e5c8e6aced613815461f4c2 (patch) | |
tree | 0559c99680217b78c0bda37a131b5f09794f43c6 /cdr/cdr_pgsql.c | |
parent | b298a3aa9b396c4055bf75104b4fb89212c44d4a (diff) |
Merge ast_str_opaque branch (discontinue usage of ast_str internals)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@163991 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'cdr/cdr_pgsql.c')
-rw-r--r-- | cdr/cdr_pgsql.c | 143 |
1 files changed, 71 insertions, 72 deletions
diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c index eddc8187d..34ba9dd0c 100644 --- a/cdr/cdr_pgsql.c +++ b/cdr/cdr_pgsql.c @@ -73,31 +73,31 @@ struct columns { static AST_RWLIST_HEAD_STATIC(psql_columns, columns); -#define LENGTHEN_BUF1(size) \ - do { \ - /* Lengthen buffer, if necessary */ \ - if (sql->used + size + 1 > sql->len) { \ - if (ast_str_make_space(&sql, ((sql->len + size + 1) / 512 + 1) * 512) != 0) { \ +#define LENGTHEN_BUF1(size) \ + do { \ + /* Lengthen buffer, if necessary */ \ + if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \ + if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \ ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \ - ast_free(sql); \ - ast_free(sql2); \ - AST_RWLIST_UNLOCK(&psql_columns); \ - return -1; \ - } \ - } \ + ast_free(sql); \ + ast_free(sql2); \ + AST_RWLIST_UNLOCK(&psql_columns); \ + return -1; \ + } \ + } \ } while (0) -#define LENGTHEN_BUF2(size) \ - do { \ - if (sql2->used + size + 1 > sql2->len) { \ - if (ast_str_make_space(&sql2, ((sql2->len + size + 3) / 512 + 1) * 512) != 0) { \ +#define LENGTHEN_BUF2(size) \ + do { \ + if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \ + if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \ ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \ - ast_free(sql); \ - ast_free(sql2); \ - AST_RWLIST_UNLOCK(&psql_columns); \ - return -1; \ - } \ - } \ + ast_free(sql); \ + ast_free(sql2); \ + AST_RWLIST_UNLOCK(&psql_columns); \ + return -1; \ + } \ + } \ } while (0) static int pgsql_log(struct ast_cdr *cdr) @@ -125,7 +125,8 @@ static int pgsql_log(struct ast_cdr *cdr) struct columns *cur; struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2); char buf[257], escapebuf[513], *value; - + int first = 1; + if (!sql || !sql2) { if (sql) { ast_free(sql); @@ -150,85 +151,86 @@ static int pgsql_log(struct ast_cdr *cdr) if (cur->notnull && !cur->hasdefault) { /* Field is NOT NULL (but no default), must include it anyway */ LENGTHEN_BUF1(strlen(cur->name) + 2); - ast_str_append(&sql, 0, "\"%s\",", cur->name); + ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name); LENGTHEN_BUF2(3); - ast_str_append(&sql2, 0, "'',"); + ast_str_append(&sql2, 0, "%s''", first ? "" : ","); + first = 0; } continue; } LENGTHEN_BUF1(strlen(cur->name) + 2); - ast_str_append(&sql, 0, "\"%s\",", cur->name); + ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name); if (strcmp(cur->name, "start") == 0 || strcmp(cur->name, "calldate") == 0) { if (strncmp(cur->type, "int", 3) == 0) { - LENGTHEN_BUF2(12); - ast_str_append(&sql2, 0, "%ld", cdr->start.tv_sec); + LENGTHEN_BUF2(13); + ast_str_append(&sql2, 0, "%s%ld", first ? "" : ",", cdr->start.tv_sec); } else if (strncmp(cur->type, "float", 5) == 0) { - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "%f", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0); } else { /* char, hopefully */ - LENGTHEN_BUF2(30); + LENGTHEN_BUF2(31); ast_localtime(&cdr->start, &tm, NULL); ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm); - ast_str_append(&sql2, 0, "%s", buf); + ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", buf); } } else if (strcmp(cur->name, "answer") == 0) { if (strncmp(cur->type, "int", 3) == 0) { - LENGTHEN_BUF2(12); - ast_str_append(&sql2, 0, "%ld", cdr->answer.tv_sec); + LENGTHEN_BUF2(13); + ast_str_append(&sql2, 0, "%s%ld", first ? "" : ",", cdr->answer.tv_sec); } else if (strncmp(cur->type, "float", 5) == 0) { - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "%f", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0); } else { /* char, hopefully */ - LENGTHEN_BUF2(30); + LENGTHEN_BUF2(31); ast_localtime(&cdr->start, &tm, NULL); ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm); - ast_str_append(&sql2, 0, "%s", buf); + ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", buf); } } else if (strcmp(cur->name, "end") == 0) { if (strncmp(cur->type, "int", 3) == 0) { - LENGTHEN_BUF2(12); - ast_str_append(&sql2, 0, "%ld", cdr->end.tv_sec); + LENGTHEN_BUF2(13); + ast_str_append(&sql2, 0, "%s%ld", first ? "" : ",", cdr->end.tv_sec); } else if (strncmp(cur->type, "float", 5) == 0) { - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0); } else { /* char, hopefully */ - LENGTHEN_BUF2(30); + LENGTHEN_BUF2(31); ast_localtime(&cdr->end, &tm, NULL); ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm); - ast_str_append(&sql2, 0, "%s", buf); + ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", buf); } } else if (strcmp(cur->name, "duration") == 0 || strcmp(cur->name, "billsec") == 0) { if (cur->type[0] == 'i') { /* Get integer, no need to escape anything */ ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0); - LENGTHEN_BUF2(12); - ast_str_append(&sql2, 0, "%s", value); + LENGTHEN_BUF2(13); + ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", value); } else if (strncmp(cur->type, "float", 5) == 0) { struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer; - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0); } else { /* Char field, probably */ struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer; - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "'%f'", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s'%f'", first ? "" : ",", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0); } } else if (strcmp(cur->name, "disposition") == 0 || strcmp(cur->name, "amaflags") == 0) { if (strncmp(cur->type, "int", 3) == 0) { /* Integer, no need to escape anything */ ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 1); - LENGTHEN_BUF2(12); - ast_str_append(&sql2, 0, "%s", value); + LENGTHEN_BUF2(13); + ast_str_append(&sql2, 0, "%s%s", first ? "" : ",", value); } else { /* Although this is a char field, there are no special characters in the values for these fields */ ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0); - LENGTHEN_BUF2(30); - ast_str_append(&sql2, 0, "'%s'", value); + LENGTHEN_BUF2(31); + ast_str_append(&sql2, 0, "%s'%s'", first ? "" : ",", value); } } else { /* Arbitrary field, could be anything */ @@ -236,20 +238,20 @@ static int pgsql_log(struct ast_cdr *cdr) if (strncmp(cur->type, "int", 3) == 0) { long long whatever; if (value && sscanf(value, "%lld", &whatever) == 1) { - LENGTHEN_BUF2(25); - ast_str_append(&sql2, 0, "%lld", whatever); + LENGTHEN_BUF2(26); + ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", whatever); } else { - LENGTHEN_BUF2(1); - ast_str_append(&sql2, 0, "0"); + LENGTHEN_BUF2(2); + ast_str_append(&sql2, 0, "%s0", first ? "" : ","); } } else if (strncmp(cur->type, "float", 5) == 0) { long double whatever; if (value && sscanf(value, "%Lf", &whatever) == 1) { - LENGTHEN_BUF2(50); - ast_str_append(&sql2, 0, "%30Lf", whatever); + LENGTHEN_BUF2(51); + ast_str_append(&sql2, 0, "%s%30Lf", first ? "" : ",", whatever); } else { - LENGTHEN_BUF2(1); - ast_str_append(&sql2, 0, "0"); + LENGTHEN_BUF2(2); + ast_str_append(&sql2, 0, "%s0", first ? "" : ","); } /* XXX Might want to handle dates, times, and other misc fields here XXX */ } else { @@ -257,19 +259,16 @@ static int pgsql_log(struct ast_cdr *cdr) PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL); else escapebuf[0] = '\0'; - LENGTHEN_BUF2(strlen(escapebuf) + 2); - ast_str_append(&sql2, 0, "'%s'", escapebuf); + LENGTHEN_BUF2(strlen(escapebuf) + 3); + ast_str_append(&sql2, 0, "%s'%s'", first ? "" : ",", escapebuf); } } - LENGTHEN_BUF2(1); - ast_str_append(&sql2, 0, ","); + first = 0; } AST_RWLIST_UNLOCK(&psql_columns); - LENGTHEN_BUF1(sql2->len); - sql->str[sql->used - 1] = ')'; - sql2->str[sql2->used - 1] = ')'; - ast_str_append(&sql, 0, "%s", sql2->str); - ast_verb(11, "[%s]\n", sql->str); + LENGTHEN_BUF1(ast_str_strlen(sql2) + 2); + ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2)); + ast_verb(11, "[%s]\n", ast_str_buffer(sql)); ast_debug(2, "inserting a CDR record.\n"); @@ -297,7 +296,7 @@ static int pgsql_log(struct ast_cdr *cdr) return -1; } } - result = PQexec(conn, sql->str); + result = PQexec(conn, ast_str_buffer(sql)); if (PQresultStatus(result) != PGRES_COMMAND_OK) { pgerror = PQresultErrorMessage(result); ast_log(LOG_ERROR, "Failed to insert call detail record into database!\n"); @@ -308,7 +307,7 @@ static int pgsql_log(struct ast_cdr *cdr) ast_log(LOG_ERROR, "Connection reestablished.\n"); connected = 1; PQclear(result); - result = PQexec(conn, sql->str); + result = PQexec(conn, ast_str_buffer(sql)); if (PQresultStatus(result) != PGRES_COMMAND_OK) { pgerror = PQresultErrorMessage(result); ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n"); |