[Asterisk-code-review] cdr pgsql: Use PQescapeStringConn for escaping names. (asterisk[13])
Joshua Colp
asteriskteam at digium.com
Wed May 13 04:35:17 CDT 2015
Joshua Colp has submitted this change and it was merged.
Change subject: cdr_pgsql: Use PQescapeStringConn for escaping names.
......................................................................
cdr_pgsql: Use PQescapeStringConn for escaping names.
Use function PQescapeStringConn for escaping the name
of the table and schema instead of doing it manually.
Change-Id: I6709165e2d00463e9c813d24f17830ad4910b599
---
M cdr/cdr_pgsql.c
1 file changed, 10 insertions(+), 32 deletions(-)
Approvals:
Joshua Colp: Looks good to me, approved; Verified
diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c
index 094a492..aea5669 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -670,42 +670,20 @@
version = PQserverVersion(conn);
if (version >= 70300) {
- char *schemaname, *tablename;
+ char *schemaname, *tablename, *tmp_schemaname, *tmp_tablename;
if (strchr(table, '.')) {
- schemaname = ast_strdupa(table);
- tablename = strchr(schemaname, '.');
- *tablename++ = '\0';
+ tmp_schemaname = ast_strdupa(table);
+ tmp_tablename = strchr(tmp_schemaname, '.');
+ *tmp_tablename++ = '\0';
} else {
- schemaname = "";
- tablename = table;
+ tmp_schemaname = "";
+ tmp_tablename = table;
}
+ tablename = ast_alloca(strlen(tmp_tablename) * 2 + 1);
+ PQescapeStringConn(conn, tablename, tmp_tablename, strlen(tmp_tablename), NULL);
- /* Escape special characters in schemaname */
- if (strchr(schemaname, '\\') || strchr(schemaname, '\'')) {
- char *tmp = schemaname, *ptr;
-
- ptr = schemaname = ast_alloca(strlen(tmp) * 2 + 1);
- for (; *tmp; tmp++) {
- if (strchr("\\'", *tmp)) {
- *ptr++ = *tmp;
- }
- *ptr++ = *tmp;
- }
- *ptr = '\0';
- }
- /* Escape special characters in tablename */
- if (strchr(tablename, '\\') || strchr(tablename, '\'')) {
- char *tmp = tablename, *ptr;
-
- ptr = tablename = ast_alloca(strlen(tmp) * 2 + 1);
- for (; *tmp; tmp++) {
- if (strchr("\\'", *tmp)) {
- *ptr++ = *tmp;
- }
- *ptr++ = *tmp;
- }
- *ptr = '\0';
- }
+ schemaname = ast_alloca(strlen(tmp_schemaname) * 2 + 1);
+ PQescapeStringConn(conn, schemaname, tmp_schemaname, strlen(tmp_schemaname), NULL);
snprintf(sqlcmd, sizeof(sqlcmd), "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND c.relname = '%s' AND n.nspname = %s%s%s) INNER JOIN pg_catalog.pg_attribute a ON (NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum ORDER BY n.nspname, c.relname, attnum",
tablename,
--
To view, visit https://gerrit.asterisk.org/441
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I6709165e2d00463e9c813d24f17830ad4910b599
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Rodrigo Ramirez Norambuena <decipher.hk at gmail.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
More information about the asterisk-code-review
mailing list