mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 19:25:53 +02:00
Add hmset
This commit is contained in:
@@ -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
|
||||
|
||||
|
@@ -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");
|
||||
|
@@ -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);
|
||||
|
@@ -43,6 +43,7 @@ public:
|
||||
void testHINCRBY();
|
||||
void testHKEYS();
|
||||
void testHMGET();
|
||||
void testHMSET();
|
||||
void testHSET();
|
||||
void testINCR();
|
||||
void testINCRBY();
|
||||
|
Reference in New Issue
Block a user