Add hmset

This commit is contained in:
fbraem
2015-11-18 19:36:46 +01:00
parent 6e00fac89b
commit fd5579cb8c
4 changed files with 67 additions and 0 deletions

View File

@@ -99,6 +99,9 @@ public:
static Command hmget(const std::string& hash, const StringVec& fields);
/// Returns an HMGET command
static Command hmset(const std::string& hash, std::map<std::string, std::string>& fields);
/// Returns a HMSET command
static Command hset(const std::string& hash, const std::string& field, const std::string& value, bool create = true);
/// Returns an HSET or HSETNX (when create is false) command

View File

@@ -188,6 +188,19 @@ Command Command::hmget(const std::string& hash, const StringVec& fields)
return cmd;
}
Command Command::hmset(const std::string& hash, std::map<std::string, std::string>& fields)
{
Command cmd("HMSET");
cmd << hash;
for(std::map<std::string, std::string>::const_iterator it = fields.begin(); it != fields.end(); ++it)
{
cmd << it->first << it->second;
}
return cmd;
}
Command Command::hset(const std::string& hash, const std::string& field, const std::string& value, bool create)
{
Command cmd(create ? "HSET" : "HSETNX");

View File

@@ -703,6 +703,55 @@ void RedisTest::testHSET()
}
}
void RedisTest::testHMSET()
{
if (!_connected)
{
std::cout << "Not connected, test skipped." << std::endl;
return;
}
delKey("myhash");
std::map<std::string, std::string> fields;
fields.insert(std::make_pair<std::string, std::string>("field1", "Hello"));
fields.insert(std::make_pair<std::string, std::string>("field2", "World"));
Command hmset = Command::hmset("myhash", fields);
try
{
std::string result = _redis.execute<std::string>(hmset);
assert(result.compare("OK") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
Command hget = Command::hget("myhash", "field1");
try
{
BulkString s = _redis.execute<BulkString>(hget);
assert(s.value().compare("Hello") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
hget = Command::hget("myhash", "field2");
try
{
BulkString s = _redis.execute<BulkString>(hget);
assert(s.value().compare("World") == 0);
}
catch(RedisException &e)
{
fail(e.message());
}
}
void RedisTest::testINCR()
{
if (!_connected)
@@ -2741,6 +2790,7 @@ CppUnit::Test* RedisTest::suite()
CppUnit_addTest(pSuite, RedisTest, testHINCRBY);
CppUnit_addTest(pSuite, RedisTest, testHKEYS);
CppUnit_addTest(pSuite, RedisTest, testHMGET);
CppUnit_addTest(pSuite, RedisTest, testHMSET);
CppUnit_addTest(pSuite, RedisTest, testHSET);
CppUnit_addTest(pSuite, RedisTest, testINCR);
CppUnit_addTest(pSuite, RedisTest, testINCRBY);

View File

@@ -43,6 +43,7 @@ public:
void testHINCRBY();
void testHKEYS();
void testHMGET();
void testHMSET();
void testHSET();
void testINCR();
void testINCRBY();