Skip to content

Commit fc25608

Browse files
committed
Add field methods to Table class
1 parent 805ebfe commit fc25608

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

common/table.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,48 @@ void Table::del(std::string key, std::string /* op */)
8383
"DEL operation failed");
8484
}
8585

86+
bool Table::getField(std::string key, std::string field, std::string &value)
87+
{
88+
redisReply *reply = (redisReply*)redisCommand(
89+
m_db->getContext(),
90+
"HGET %s %s",
91+
getKeyName(key).c_str(),
92+
field.c_str());
93+
94+
if (reply->type != REDIS_REPLY_STRING)
95+
{
96+
freeReplyObject(reply);
97+
return false;
98+
}
99+
100+
value = std::string(reply->str);
101+
102+
freeReplyObject(reply);
103+
104+
return true;
105+
}
106+
107+
void Table::setField(std::string key, std::string field, std::string value)
108+
{
109+
FieldValueTuple entry(field, value);
110+
111+
std::vector<FieldValueTuple> values { entry };
112+
113+
set(key, values);
114+
}
115+
116+
void Table::delField(std::string key, std::string field)
117+
{
118+
RedisReply r(m_db, string("HDEL ") + getKeyName(key) + " " + field, REDIS_REPLY_INTEGER);
119+
if (r.getContext()->type != REDIS_REPLY_INTEGER)
120+
throw system_error(make_error_code(errc::io_error),
121+
"DEL operation failed");
122+
}
123+
124+
Table::~Table()
125+
{
126+
}
127+
86128
void Table::multi()
87129
{
88130
while (!m_expectedResults.empty())

common/table.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class Table {
3333
/* Delete an entry in the DB directly (op not in used) */
3434
virtual void del(std::string key, std::string op = "");
3535

36+
bool getField(std::string key, std::string field, std::string &value);
37+
void setField(std::string key, std::string field, std::string value);
38+
void delField(std::string key, std::string field);
39+
40+
virtual ~Table();
41+
3642
protected:
3743
/* Return the actual key name as a comibation of tableName:key */
3844
std::string getKeyName(std::string key);

0 commit comments

Comments
 (0)