Skip to content

Commit

Permalink
simplify add_connsction string handling, use StringInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
markokr committed Apr 16, 2007
1 parent f2f292e commit bcc29cc
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/cluster.c
Expand Up @@ -158,38 +158,32 @@ free_connlist(ProxyCluster *cluster)
static ProxyConnection *
add_connection(ProxyCluster *cluster, char *connstr)
{
int i,
len;
int i;
ProxyConnection *conn;
MemoryContext old_ctx;
char *username,
*newstr;
char *username;
StringInfo final;

final = makeStringInfo();
appendStringInfoString(final, connstr);

/* append current user if not specified in connstr */
if (strstr(connstr, "user=") == NULL)
{
username = GetUserNameFromId(GetSessionUserId());
len = strlen(connstr) + strlen(username) + 6 + 1;
newstr = palloc(len);
strcpy(newstr, connstr);
strcat(newstr, " user=");
strcat(newstr, username);
connstr = newstr;
appendStringInfo(final, " user=%s", username);
}

/* check if already have it */
for (i = 0; i < cluster->conn_count; i++)
{
conn = &cluster->conn_list[i];
if (strcmp(conn->connstr, connstr) == 0)
if (strcmp(conn->connstr, final->data) == 0)
return conn;
}

/* add new connection */
old_ctx = MemoryContextSwitchTo(cluster_mem);
conn = &cluster->conn_list[cluster->conn_count++];
conn->connstr = pstrdup(connstr);
MemoryContextSwitchTo(old_ctx);
conn->connstr = MemoryContextStrdup(cluster_mem, final->data);

return conn;
}
Expand Down

0 comments on commit bcc29cc

Please sign in to comment.