Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased filtering for SR records #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions JSONDB/JSONDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ std::string JSONDB::generateWhereClause(JsonBox::Object request)
return ss.str();
}

std::string JSONDB::generateSelectFields(JsonBox::Object request)
{
std::stringstream ss;
std::vector<std::string> fields_str;

JsonBox::Array fields = request["fields"].getArray();

if (fields.size() > 0) {
JsonBox::Array::iterator jit;
for (jit = fields.begin(); jit != fields.end(); jit++) {
fields_str.push_back(jit->getString());
}
ss << implode(", ", fields_str);
} else {
ss << "*";
}
return ss.str();
}

std::string JSONDB::implode(std::string delimiter, std::vector<std::string> pieces)
{
std::stringstream ss;
Expand Down Expand Up @@ -258,6 +277,22 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries)
}
std::string msisdn = jit->second.getString();

jit = fields.find("ipaddr");
if (jit == fields.end()) {
response["code"] = JsonBox::Value(406);
response["data"] = JsonBox::Value("missing ipaddr");
return response;
}
std::string ipaddr = jit->second.getString();

jit = fields.find("port");
if (jit == fields.end()) {
response["code"] = JsonBox::Value(406);
response["data"] = JsonBox::Value("missing port");
return response;
}
std::string port = jit->second.getString();

jit = fields.find("ki");
if (jit == fields.end()) {
response["code"] = JsonBox::Value(406);
Expand All @@ -270,8 +305,8 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries)
JsonBox::Object responseSIP;
JsonBox::Object responseDIAL;

queryTMP << "insert into sip_buddies (username, name, callerid, ki, host, allow, ipaddr)";
queryTMP << " values (\"" << imsi << "\",\"" << name << "\",\"" << msisdn << "\",\"" << ki << "\",\"dynamic\",\"gsm\",\"127.0.0.1\")";
queryTMP << "insert into sip_buddies (username, name, callerid, ki, host, allow, ipaddr, port)";
queryTMP << " values (\"" << imsi << "\",\"" << name << "\",\"" << msisdn << "\",\"" << ki << "\",\"dynamic\",\"gsm\",\"" << ipaddr << "\",\"" << port << "\" )";
responseSIP = execOnly(queryTMP.str());

queryTMP.str("");
Expand All @@ -289,6 +324,10 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries)

} else if (action.compare("read") == 0) {
q << "select sip_buddies.username as \"imsi\", sip_buddies.name as \"name\", dialdata_table.exten as \"msisdn\" from sip_buddies left outer join dialdata_table on sip_buddies.username = dialdata_table.dial";

// add match clause
q << generateWhereClause(request);

return read(q.str());

} else if (action.compare("update") == 0) {
Expand Down Expand Up @@ -320,11 +359,26 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries)
}
std::string msisdn = jit->second.getString();

jit = fields.find("ipaddr");
if (jit == fields.end()) {
response["code"] = JsonBox::Value(406);
response["data"] = JsonBox::Value("missing ipaddr");
return response;
}
std::string ipaddr = jit->second.getString();

jit = fields.find("port");
if (jit == fields.end()) {
response["code"] = JsonBox::Value(406);
response["data"] = JsonBox::Value("missing port");
return response;
}
std::string port = jit->second.getString();
std::stringstream queryTMP;
JsonBox::Object responseSIP;
JsonBox::Object responseDIAL;

queryTMP << "update sip_buddies set name=\"" << name << "\", callerid=\"" << msisdn << "\" where username=\"" << imsi << "\"";
queryTMP << "update sip_buddies set name=\"" << name << "\", callerid=\"" << msisdn << "\", ipaddr=\"" << ipaddr << "\", port=\"" << port << "\" where username=\"" << imsi << "\"";
responseSIP = execOnly(queryTMP.str());

queryTMP.str("");
Expand Down Expand Up @@ -403,11 +457,15 @@ JsonBox::Object JSONDB::query(JsonBox::Object request, unsigned retries)
// READ
} else if (action.compare("read") == 0) {
// start query
q << "select * from " << table;
q << "select ";

// add select fields
q << generateSelectFields(request);

q << " from " << table;

// add match clause
q << generateWhereClause(request);

return read(q.str());

// UPDATE
Expand Down
1 change: 1 addition & 0 deletions JSONDB/JSONDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class JSONDB {

sqlite3* mDB;
std::string generateWhereClause(JsonBox::Object request);
std::string generateSelectFields(JsonBox::Object request);
std::string implode(std::string delimiter, std::vector<std::string> pieces);
JsonBox::Object read(std::string query, unsigned retries = 5);
JsonBox::Object execOnly(std::string query, unsigned retries = 5);
Expand Down