Skip to content

Commit

Permalink
Adding OR and WHERE methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano committed May 2, 2011
1 parent b0620f0 commit eb55994
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
43 changes: 31 additions & 12 deletions query.cc
Expand Up @@ -12,6 +12,8 @@ void node_db::Query::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::Func
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "from", From);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "join", Join);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "where", Where);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "and", And);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "or", Or);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "limit", Limit);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "add", Add);
NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "execute", Execute);
Expand All @@ -22,7 +24,7 @@ void node_db::Query::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::Func
}

node_db::Query::Query(): node::EventEmitter(),
connection(NULL), async(true), cast(true), bufferText(false), whereAdded(false), cbStart(NULL), cbSuccess(NULL), cbFinish(NULL) {
connection(NULL), async(true), cast(true), bufferText(false), cbStart(NULL), cbSuccess(NULL), cbFinish(NULL) {
}

node_db::Query::~Query() {
Expand Down Expand Up @@ -301,12 +303,34 @@ v8::Handle<v8::Value> node_db::Query::Join(const v8::Arguments& args) {
v8::Handle<v8::Value> node_db::Query::Where(const v8::Arguments& args) {
v8::HandleScope scope;

ARG_CHECK_STRING(0, conditions);
ARG_CHECK_OPTIONAL_ARRAY(1, values);
node_db::Query *query = node::ObjectWrap::Unwrap<node_db::Query>(args.This());
assert(query);

return scope.Close(query->addCondition(args, "WHERE"));
}

v8::Handle<v8::Value> node_db::Query::And(const v8::Arguments& args) {
v8::HandleScope scope;

node_db::Query *query = node::ObjectWrap::Unwrap<node_db::Query>(args.This());
assert(query);

return scope.Close(query->addCondition(args, "AND"));
}

v8::Handle<v8::Value> node_db::Query::Or(const v8::Arguments& args) {
v8::HandleScope scope;

node_db::Query *query = node::ObjectWrap::Unwrap<node_db::Query>(args.This());
assert(query);

return scope.Close(query->addCondition(args, "OR"));
}

v8::Handle<v8::Value> node_db::Query::addCondition(const v8::Arguments& args, const char* separator) {
ARG_CHECK_STRING(0, conditions);
ARG_CHECK_OPTIONAL_ARRAY(1, values);

v8::String::Utf8Value conditions(args[0]->ToString());
std::string currentConditions = *conditions;
v8::Local<v8::Array> currentValues;
Expand All @@ -315,20 +339,15 @@ v8::Handle<v8::Value> node_db::Query::Where(const v8::Arguments& args) {
}

try {
currentConditions = query->parseQuery(currentConditions, *currentValues);
currentConditions = this->parseQuery(currentConditions, *currentValues);
} catch(const node_db::Exception& exception) {
THROW_EXCEPTION(exception.what())
}

if (query->whereAdded) {
query->sql << " AND ";
} else {
query->whereAdded = true;
query->sql << " WHERE ";
}
query->sql << currentConditions;
this->sql << " " << separator << " ";
this->sql << currentConditions;

return scope.Close(args.This());
return args.This();
}

v8::Handle<v8::Value> node_db::Query::Limit(const v8::Arguments& args) {
Expand Down
4 changes: 3 additions & 1 deletion query.h
Expand Up @@ -41,7 +41,6 @@ class Query : public node::EventEmitter {
bool async;
bool cast;
bool bufferText;
bool whereAdded;
v8::Persistent<v8::Array> values;
v8::Persistent<v8::Function>* cbStart;
v8::Persistent<v8::Function>* cbSuccess;
Expand All @@ -56,6 +55,8 @@ class Query : public node::EventEmitter {
static v8::Handle<v8::Value> From(const v8::Arguments& args);
static v8::Handle<v8::Value> Join(const v8::Arguments& args);
static v8::Handle<v8::Value> Where(const v8::Arguments& args);
static v8::Handle<v8::Value> And(const v8::Arguments& args);
static v8::Handle<v8::Value> Or(const v8::Arguments& args);
static v8::Handle<v8::Value> Limit(const v8::Arguments& args);
static v8::Handle<v8::Value> Add(const v8::Arguments& args);
static v8::Handle<v8::Value> Execute(const v8::Arguments& args);
Expand All @@ -65,6 +66,7 @@ class Query : public node::EventEmitter {
void executeFinished(execute_request_t* request);
static void freeRequest(execute_request_t* request, bool freeAll = true);
std::string selectField(v8::Local<v8::Value> value) const throw(Exception&);
v8::Handle<v8::Value> addCondition(const v8::Arguments& args, const char* separator);
v8::Local<v8::Object> row(Result* result, row_t* currentRow) const;
std::string parseQuery(const std::string& query, v8::Array* values) const throw(Exception&);
std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true) const throw(Exception&);
Expand Down

0 comments on commit eb55994

Please sign in to comment.