Skip to content

Commit

Permalink
Add field methods to Table class
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Mar 18, 2016
1 parent 805ebfe commit 2953876
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
55 changes: 55 additions & 0 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,61 @@ void Table::del(std::string key, std::string /* op */)
"DEL operation failed");
}

std::string Table::getField(std::string key, std::string field)
{
std::string value;

if (tryGetField(key, field, value))
{
return value;
}

throw system_error(make_error_code(errc::io_error),
"HGET operation failed");
}

bool Table::tryGetField(std::string key, std::string field, std::string &value)
{
redisReply *reply = (redisReply*)redisCommand(
m_db->getContext(),
"HGET %s %s",
getKeyName(key).c_str(),
field.c_str());

if (reply->type != REDIS_REPLY_STRING)
{
freeReplyObject(reply);
return false;
}

value = std::string(reply->str);

freeReplyObject(reply);

return true;
}

void Table::setField(std::string key, std::string field, std::string value)
{
FieldValueTuple entry(field, value);

std::vector<FieldValueTuple> values { entry };

set(key, values);
}

void Table::delField(std::string key, std::string field)
{
RedisReply r(m_db, string("HDEL ") + getKeyName(key) + " " + field, REDIS_REPLY_INTEGER);
if (r.getContext()->type != REDIS_REPLY_INTEGER)
throw system_error(make_error_code(errc::io_error),
"DEL operation failed");
}

Table::~Table()
{
}

void Table::multi()
{
while (!m_expectedResults.empty())
Expand Down
7 changes: 7 additions & 0 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class Table {
/* Delete an entry in the DB directly (op not in used) */
virtual void del(std::string key, std::string op = "");

std::string getField(std::string key, std::string field);
bool tryGetField(std::string key, std::string field, std::string &value);
void setField(std::string key, std::string field, std::string value);
void delField(std::string key, std::string field);

virtual ~Table();

protected:
/* Return the actual key name as a comibation of tableName:key */
std::string getKeyName(std::string key);
Expand Down

0 comments on commit 2953876

Please sign in to comment.