diff options
author | Mark Michelson <mmichelson@digium.com> | 2014-05-02 20:07:08 +0000 |
---|---|---|
committer | Mark Michelson <mmichelson@digium.com> | 2014-05-02 20:07:08 +0000 |
commit | ff1658ed3b681e5083546c9d62899c3cd6a932c3 (patch) | |
tree | cd6d5400fc437c64162abdc2e4adefc98105f275 /res/res_config_pgsql.c | |
parent | 119599407b95f96c15535d1c86e88b840c933ee7 (diff) |
Return the number of rows affected by a SQL insert, rather than an object ID.
The realtime API specifies that the store callback is supposed to return the number
of rows affected. res_config_pgsql was instead returning an Oid cast as an int, which
during any nominal execution would be cast to 0. Returning 0 when more than 0 rows were
inserted causes problems to the function's callers.
To give an idea of how strange code can be, this is the necessary code change to fix
a device state issue reported against chan_pjsip in Asterisk 12+. The issue was that
the registrar would attempt to insert contacts into the database. Because of the 0
return from res_config_pgsql, the registrar would think that the contact was not successfully
inserted, even though it actually was. As such, even though the contact was query-able
and it was possible to call the endpoint, Asterisk would "think" the endpoint was unregistered,
meaning it would report the device state as UNAVAILABLE instead of NOT_INUSE.
The necessary fix applies to all versions of Asterisk, so even though the bug reported
only applies to Asterisk 12+, the code correction is being inserted into 1.8+.
Closes issue ASTERISK-23707
Reported by Mark Michelson
........
Merged revisions 413224 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 413225 from http://svn.asterisk.org/svn/asterisk/branches/11
........
Merged revisions 413226 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@413227 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_config_pgsql.c')
-rw-r--r-- | res/res_config_pgsql.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 27494a12f..58925a076 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -921,7 +921,7 @@ static int update2_pgsql(const char *database, const char *tablename, const stru static int store_pgsql(const char *database, const char *table, const struct ast_variable *fields) { RAII_VAR(PGresult *, result, NULL, PQclear); - Oid insertid; + int numrows; struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256); struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256); struct ast_str *sql2 = ast_str_thread_get(&where_buf, 256); @@ -978,10 +978,10 @@ static int store_pgsql(const char *database, const char *table, const struct ast return -1; } - insertid = PQoidValue(result); + numrows = atoi(PQcmdTuples(result)); ast_mutex_unlock(&pgsql_lock); - ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid); + ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s.", table); /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html * An integer greater than zero indicates the number of rows affected @@ -989,8 +989,9 @@ static int store_pgsql(const char *database, const char *table, const struct ast * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.) */ - if (insertid >= 0) - return (int) insertid; + if (numrows >= 0) { + return numrows; + } return -1; } |