diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index 970156e5a..d1f0a5f92 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -81,9 +81,18 @@ public: static Command hget(const std::string& hash, const std::string& field); /// Returns an HGET command + static Command hgetall(const std::string& hash); + /// Returns an HGETALL command + + static Command hincrby(const std::string& hash, const std::string& field, Int64 by = 1); + /// Returns an HINCRBY 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 + static Command hset(const std::string& hash, const std::string& field, Int64 value, bool create = true); + /// Returns an HSET or HSETNX (when create is false) command + static Command incr(const std::string& key, Int64 by = 0); /// Returns an INCR or INCRBY command. Calls INCR when by is omitted or zero. diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 6b0e945f5..29abb69e8 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -158,6 +158,24 @@ Command Command::hget(const std::string& hash, const std::string& field) return cmd; } +Command Command::hgetall(const std::string& hash) +{ + Command cmd("HGETALL"); + + cmd << hash; + + return cmd; +} + +Command Command::hincrby(const std::string& hash, const std::string& field, Int64 by) +{ + Command cmd("HINCRBY"); + + cmd << hash << field << NumberFormatter::format(by); + + return cmd; +} + Command Command::hset(const std::string& hash, const std::string& field, const std::string& value, bool create) { Command cmd(create ? "HSET" : "HSETNX"); @@ -167,6 +185,11 @@ Command Command::hset(const std::string& hash, const std::string& field, const s return cmd; } +Command Command::hset(const std::string& hash, const std::string& field, Int64 value, bool create) +{ + return hset(hash, field, NumberFormatter::format(value), create); +} + Command Command::incr(const std::string& key, Int64 by) { Command cmd(by == 0 ? "INCR" : "INCRBY"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index 26a193e1e..fdfa2e2f1 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -462,6 +462,105 @@ void RedisTest::testHEXISTS() } } +void RedisTest::testHGETALL() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + delKey("myhash"); + + Command hset = Command::hset("myhash", "field1", "Hello"); + try + { + Poco::Int64 value = _redis.execute(hset); + assert(value == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + hset = Command::hset("myhash", "field2", "World"); + try + { + Poco::Int64 value = _redis.execute(hset); + assert(value == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command hgetall = Command::hgetall("myhash"); + try + { + Array result = _redis.execute(hgetall); + assert(result.size() == 4); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + +void RedisTest::testHINCRBY() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + delKey("myhash"); + + Command hset = Command::hset("myhash", "field", 5); + try + { + Poco::Int64 value = _redis.execute(hset); + assert(value == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command hincrby = Command::hincrby("myhash", "field"); + try + { + Poco::Int64 n = _redis.execute(hincrby); + assert(n == 6); + } + catch(RedisException &e) + { + fail(e.message()); + } + + hincrby = Command::hincrby("myhash", "field", -1); + try + { + Poco::Int64 n = _redis.execute(hincrby); + assert(n == 5); + } + catch(RedisException &e) + { + fail(e.message()); + } + + hincrby = Command::hincrby("myhash", "field", -10); + try + { + Poco::Int64 n = _redis.execute(hincrby); + assert(n == -5); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + void RedisTest::testHSET() { if (!_connected) @@ -2528,6 +2627,8 @@ CppUnit::Test* RedisTest::suite() CppUnit_addTest(pSuite, RedisTest, testEVAL); CppUnit_addTest(pSuite, RedisTest, testHDEL); CppUnit_addTest(pSuite, RedisTest, testHEXISTS); + CppUnit_addTest(pSuite, RedisTest, testHGETALL); + CppUnit_addTest(pSuite, RedisTest, testHINCRBY); 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 642358012..b4039380b 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -39,6 +39,8 @@ public: void testEVAL(); void testHDEL(); void testHEXISTS(); + void testHGETALL(); + void testHINCRBY(); void testHSET(); void testINCR(); void testINCRBY();