From fd5579cb8c88e36c42a1f54ca7a6aafdc001b509 Mon Sep 17 00:00:00 2001 From: fbraem Date: Wed, 18 Nov 2015 19:36:46 +0100 Subject: [PATCH] Add hmset --- Redis/include/Poco/Redis/Command.h | 3 ++ Redis/src/Command.cpp | 13 ++++++++ Redis/testsuite/src/RedisTest.cpp | 50 ++++++++++++++++++++++++++++++ Redis/testsuite/src/RedisTest.h | 1 + 4 files changed, 67 insertions(+) diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index 3ccd05653..0b1961b9c 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -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& 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 diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 3a420ef47..39842b41b 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -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& fields) +{ + Command cmd("HMSET"); + + cmd << hash; + for(std::map::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"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index 85d691c7d..f63648e74 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -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 fields; + fields.insert(std::make_pair("field1", "Hello")); + fields.insert(std::make_pair("field2", "World")); + + Command hmset = Command::hmset("myhash", fields); + try + { + std::string result = _redis.execute(hmset); + assert(result.compare("OK") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command hget = Command::hget("myhash", "field1"); + try + { + BulkString s = _redis.execute(hget); + assert(s.value().compare("Hello") == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } + + hget = Command::hget("myhash", "field2"); + try + { + BulkString s = _redis.execute(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); diff --git a/Redis/testsuite/src/RedisTest.h b/Redis/testsuite/src/RedisTest.h index 3119c964c..95a23fc99 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -43,6 +43,7 @@ public: void testHINCRBY(); void testHKEYS(); void testHMGET(); + void testHMSET(); void testHSET(); void testINCR(); void testINCRBY();