From 8d08cc3909497d9d30774410b15ab5b19d21c2c5 Mon Sep 17 00:00:00 2001 From: fbraem Date: Tue, 17 Nov 2015 21:38:07 +0100 Subject: [PATCH] Add hexists --- Redis/include/Poco/Redis/Command.h | 5 +++- Redis/src/Command.cpp | 9 ++++++ Redis/testsuite/src/RedisTest.cpp | 45 ++++++++++++++++++++++++++++++ Redis/testsuite/src/RedisTest.h | 1 + 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Redis/include/Poco/Redis/Command.h b/Redis/include/Poco/Redis/Command.h index 94a6f1ebe..970156e5a 100644 --- a/Redis/include/Poco/Redis/Command.h +++ b/Redis/include/Poco/Redis/Command.h @@ -71,10 +71,13 @@ public: static Command hdel(const std::string& hash, const std::string& field); /// Returns an HDEL command - + static Command hdel(const std::string& hash, const std::vector& fields); /// Returns an HDEL command + static Command hexists(const std::string& hash, const std::string& field); + /// Returns an HEXISTS command + static Command hget(const std::string& hash, const std::string& field); /// Returns an HGET command diff --git a/Redis/src/Command.cpp b/Redis/src/Command.cpp index 78a08bb11..6b0e945f5 100644 --- a/Redis/src/Command.cpp +++ b/Redis/src/Command.cpp @@ -140,6 +140,15 @@ Command Command::hdel(const std::string& hash, const std::vector& f return cmd; } +Command Command::hexists(const std::string& hash, const std::string& field) +{ + Command cmd("HEXISTS"); + + cmd << hash << field; + + return cmd; +} + Command Command::hget(const std::string& hash, const std::string& field) { Command cmd("HGET"); diff --git a/Redis/testsuite/src/RedisTest.cpp b/Redis/testsuite/src/RedisTest.cpp index 7a310cfa9..26a193e1e 100644 --- a/Redis/testsuite/src/RedisTest.cpp +++ b/Redis/testsuite/src/RedisTest.cpp @@ -418,6 +418,50 @@ void RedisTest::testHDEL() } } +void RedisTest::testHEXISTS() +{ + if (!_connected) + { + std::cout << "Not connected, test skipped." << std::endl; + return; + } + + delKey("myhash"); + + Command hset = Command::hset("myhash", "field1", "foo"); + try + { + Poco::Int64 value = _redis.execute(hset); + assert(value == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + Command hexists = Command::hexists("myhash", "field1"); + try + { + Poco::Int64 result = _redis.execute(hexists); + assert(result == 1); + } + catch(RedisException &e) + { + fail(e.message()); + } + + hexists = Command::hexists("myhash", "field2"); + try + { + Poco::Int64 result = _redis.execute(hexists); + assert(result == 0); + } + catch(RedisException &e) + { + fail(e.message()); + } +} + void RedisTest::testHSET() { if (!_connected) @@ -2483,6 +2527,7 @@ CppUnit::Test* RedisTest::suite() CppUnit_addTest(pSuite, RedisTest, testError); CppUnit_addTest(pSuite, RedisTest, testEVAL); CppUnit_addTest(pSuite, RedisTest, testHDEL); + CppUnit_addTest(pSuite, RedisTest, testHEXISTS); 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 541ce363f..642358012 100644 --- a/Redis/testsuite/src/RedisTest.h +++ b/Redis/testsuite/src/RedisTest.h @@ -38,6 +38,7 @@ public: void testError(); void testEVAL(); void testHDEL(); + void testHEXISTS(); void testHSET(); void testINCR(); void testINCRBY();