Skip to content

Commit

Permalink
common: Add field methods to Table class
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Cudnik <kcudnik@microsoft.com>
  • Loading branch information
kcudnik committed Mar 22, 2016
1 parent f34a1c3 commit 1ed0be7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
63 changes: 63 additions & 0 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ void Table::del(std::string key, std::string /* op */)
"DEL operation failed");
}

bool Table::getField(std::string key, std::string field, std::string &value)
{
std::string hget = formatHGET(getKeyName(key).c_str(),
field.c_str());

RedisReply r(m_db, hget, REDIS_REPLY_INTEGER);

if (r.getContext()->type != REDIS_REPLY_STRING)
{
return false;
}

value = std::string(r.getContext()->str);

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)
{
std::string hdel = formatHDEL(getKeyName(key), field);

RedisReply r(m_db, hdel, 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 Expand Up @@ -166,4 +207,26 @@ string Table::formatHSET(const string& key, const string& field,
return hset;
}

string Table::formatHGET(const string& key, const string& field)
{
char *temp;
int len = redisFormatCommand(&temp, "HGET %s %s",
key.c_str(),
field.c_str());
string hget(temp, len);
free(temp);
return hget;
}

string Table::formatHDEL(const string& key, const string& field)
{
char *temp;
int len = redisFormatCommand(&temp, "HDEL %s %s",
key.c_str(),
field.c_str());
string hdel(temp, len);
free(temp);
return hdel;
}

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

bool getField(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 All @@ -57,6 +63,14 @@ class Table {
const std::string &field,
const std::string &value);

/* Format HGET key field command */
static std::string formatHGET(const std::string& key,
const std::string& field);

/* Format HDEL key field command */
static std::string formatHDEL(const std::string& key,
const std::string& field);

DBConnector *m_db;
std::string m_tableName;

Expand Down

0 comments on commit 1ed0be7

Please sign in to comment.