From 96e34b3ce11e4cda35b9127b6a75756fb42f12ba Mon Sep 17 00:00:00 2001
From: Pieter Noordhuis <pcnoordhuis@gmail.com>
Date: Sun, 6 Mar 2011 20:46:40 +0100
Subject: [PATCH] Store SELECTed database for reconnect (issue #468)

---
 src/redis-cli.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/redis-cli.c b/src/redis-cli.c
index e19a555f4..fc2d4d732 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -263,11 +263,9 @@ static int cliAuth() {
 /* Send SELECT dbnum to the server */
 static int cliSelect() {
     redisReply *reply;
-    char dbnum[16];
     if (config.dbnum == 0) return REDIS_OK;
 
-    snprintf(dbnum,sizeof(dbnum),"%d",config.dbnum);
-    reply = redisCommand(context,"SELECT %s",dbnum);
+    reply = redisCommand(context,"SELECT %d",config.dbnum);
     if (reply != NULL) {
         freeReplyObject(reply);
         return REDIS_OK;
@@ -491,6 +489,10 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
         if (cliReadReply(output_raw) != REDIS_OK) {
             free(argvlen);
             return REDIS_ERR;
+        } else {
+            /* Store database number when SELECT was successfully executed. */
+            if (!strcasecmp(command,"select") && argc == 2)
+                config.dbnum = atoi(argv[1]);
         }
     }