Skip to content

Commit

Permalink
The driver now validates the filter if it's wrong creates an exceptio…
Browse files Browse the repository at this point in the history
…n to be shown in the shell
  • Loading branch information
crosstantine committed Sep 18, 2012
1 parent 4733e26 commit 5a42135
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
11 changes: 9 additions & 2 deletions db/driverbase/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,20 @@ BSONObj* Connection::findByKey(const std::string& db, const std::string& ns, con
return bsonresult;
}

std::vector<BSONObj*>* Connection::find(const std::string& db, const std::string& ns, const std::string& filter) {
std::vector<BSONObj*>* Connection::find(const std::string& db, const std::string& ns, const std::string& filter) throw(ParseException) {
return find(db, ns, "*", filter);
}

std::vector<BSONObj*>* Connection::find(const std::string& db, const std::string& ns, const std::string& select, const std::string& filter) {
std::vector<BSONObj*>* Connection::find(const std::string& db, const std::string& ns, const std::string& select, const std::string& filter) throw(ParseException) {
if (_logger->isDebug()) _logger->debug("executing find db: %s, ns: %s, select: %s, filter: %s", db.c_str(), ns.c_str(), select.c_str(), filter.c_str());

try {
FilterParser* parser = FilterParser::parse(filter);
delete parser;
} catch (ParseException e) {
_logger->error("An error ocurred parsing the filter %s", filter.c_str());
throw e;
}
FindCommand cmd;
cmd.setFilter(filter);
cmd.setSelect(select);
Expand Down
5 changes: 3 additions & 2 deletions db/driverbase/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include "bson.h"
#include "util.h"
#include "filterparser.h"

#ifdef WINDOWS
#define LibraryExport __declspec( dllexport )
Expand Down Expand Up @@ -43,8 +44,8 @@ namespace djondb {
bool insert(const std::string& db, const std::string& ns, const BSONObj& obj);
BSONObj* findByKey(const std::string& db, const std::string& ns, const std::string& select, const std::string& id);
BSONObj* findByKey(const std::string& db, const std::string& ns, const std::string& id);
std::vector<BSONObj*>* find(const std::string& db, const std::string& ns, const std::string& select, const std::string& filter);
std::vector<BSONObj*>* find(const std::string& db, const std::string& ns, const std::string& filter);
std::vector<BSONObj*>* find(const std::string& db, const std::string& ns, const std::string& select, const std::string& filter) throw(ParseException);
std::vector<BSONObj*>* find(const std::string& db, const std::string& ns, const std::string& filter) throw(ParseException);
bool update(const std::string& db, const std::string& ns, const std::string& json);
bool update(const std::string& db, const std::string& ns, const BSONObj& bson);

Expand Down
34 changes: 19 additions & 15 deletions db/shell/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,25 +416,29 @@ v8::Handle<v8::Value> find(const v8::Arguments& args) {
}
*/

std::vector<BSONObj*>* result = __djonConnection->find(db, ns, select, filter);

std::stringstream ss;
ss << "[";
if (result->size() > 0) {
for (std::vector<BSONObj*>::const_iterator i = result->begin(); i != result->end(); i++) {
BSONObj* obj = *i;
if (i != result->begin()) {
ss << ", ";
try {
std::vector<BSONObj*>* result = __djonConnection->find(db, ns, select, filter);

std::stringstream ss;
ss << "[";
if (result->size() > 0) {
for (std::vector<BSONObj*>::const_iterator i = result->begin(); i != result->end(); i++) {
BSONObj* obj = *i;
if (i != result->begin()) {
ss << ", ";
}
ss << obj->toChar();
}
ss << obj->toChar();
}
}
ss << "]";
ss << "]";

std::string sresult = ss.str();
std::string sresult = ss.str();

delete result;
return parseJSON(v8::String::New(sresult.c_str()));
delete result;
return parseJSON(v8::String::New(sresult.c_str()));
} catch (ParseException e) {
return v8::ThrowException(v8::String::New("the filter expression contains an error\n"));
}
/*
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
v8::Handle<v8::Object> global = context->Global();
Expand Down
4 changes: 2 additions & 2 deletions db/shell/shell.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\third_party\includes;..\util;C:\development\boost;..\bson;..\driverbase"
AdditionalIncludeDirectories="..\third_party\includes;..\util;..\db;C:\development\boost;..\bson;..\driverbase"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;"
MinimalRebuild="true"
BasicRuntimeChecks="3"
Expand Down Expand Up @@ -115,7 +115,7 @@
/>
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="..\third_party\includes;..\util;C:\development\boost;..\bson;..\driverbase"
AdditionalIncludeDirectories="..\third_party\includes;..\util;..\db;C:\development\boost;..\bson;..\driverbase"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;"
RuntimeLibrary="2"
UsePrecompiledHeader="0"
Expand Down

0 comments on commit 5a42135

Please sign in to comment.