From 7ee5504038bb81debb13c2d76fc080400f57e201 Mon Sep 17 00:00:00 2001 From: rpedela Date: Thu, 22 Aug 2013 13:26:09 -0600 Subject: [PATCH 1/5] Ignore gedit backup files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 81d95c88..44602419 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ *.log .lock-wscript build/ +*~ From 6b6b92b9758948d1bab3ba29ec987cc36ade48ce Mon Sep 17 00:00:00 2001 From: rpedela Date: Thu, 22 Aug 2013 13:28:48 -0600 Subject: [PATCH 2/5] #181 #366 Add support for single row mode in Postgres 9.2+. This will enable single row mode only when the user wants to stream rows. --- lib/native/index.js | 8 ++++---- lib/native/query.js | 5 +++++ src/binding.cc | 45 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/native/index.js b/lib/native/index.js index efb37bfa..cb6c38f2 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -130,18 +130,18 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) { this._activeQuery = query; if(query.name) { if(this._namedQueries[query.name]) { - this._sendQueryPrepared(query.name, query.values||[]); + this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode); } else { this._namedQuery = true; this._namedQueries[query.name] = true; - this._sendPrepare(query.name, query.text, (query.values||[]).length); + this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode); } } else if(query.values) { //call native function - this._sendQueryWithParams(query.text, query.values); + this._sendQueryWithParams(query.text, query.values, query.singleRowMode); } else { //call native function - this._sendQuery(query.text); + this._sendQuery(query.text, query.singleRowMode); } }; diff --git a/lib/native/query.js b/lib/native/query.js index 905d1f78..d2b06b8e 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -20,6 +20,11 @@ var NativeQuery = function(config, values, callback) { this.text = c.text; this.values = c.values; this.callback = c.callback; + this.singleRowMode = false; + + if(!this.callback) { + this.singleRowMode = true; + } this._result = new Result(config.rowMode); this._addedFields = false; diff --git a/src/binding.cc b/src/binding.cc index a9a7943f..2333e1f7 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -13,6 +13,10 @@ #define ESCAPE_SUPPORTED #endif +#if PG_VERSION_NUM >= 90200 +#define SINGLE_ROW_SUPPORTED +#endif + #define THROW(msg) return ThrowException(Exception::Error(String::New(msg))); using namespace v8; @@ -204,7 +208,9 @@ public: } char* queryText = MallocCString(args[0]); - int result = self->Send(queryText); + bool singleRowMode = (bool)args[1]->Int32Value(); + + int result = self->Send(queryText, singleRowMode); free(queryText); if(result == 0) { lastErrorMessage = self->GetLastError(); @@ -234,7 +240,8 @@ public: String::Utf8Value queryName(args[0]); String::Utf8Value queryText(args[1]); int length = args[2]->Int32Value(); - self->SendPrepare(*queryName, *queryText, length); + bool singleRowMode = (bool)args[3]->Int32Value(); + self->SendPrepare(*queryName, *queryText, length, singleRowMode); return Undefined(); } @@ -274,12 +281,13 @@ public: } char* queryText = MallocCString(args[0]); + bool singleRowMode = (bool)args[2]->Int32Value(); int result = 0; if(isPrepared) { - result = self->SendPreparedQuery(queryText, len, paramValues); + result = self->SendPreparedQuery(queryText, len, paramValues, singleRowMode); } else { - result = self->SendQueryParams(queryText, len, paramValues); + result = self->SendQueryParams(queryText, len, paramValues, singleRowMode); } free(queryText); @@ -383,33 +391,53 @@ protected: } #endif - int Send(const char *queryText) + void enableSingleRowMode(bool enable) + { +#ifdef SINGLE_ROW_SUPPORTED + if(enable == true) { + int mode = PQsetSingleRowMode(connection_); + if(mode == 1) { + TRACE("PQsetSingleRowMode enabled") + } else { + TRACE("PQsetSingleRowMode disabled") + } + } else { + TRACE("PQsetSingleRowMode disabled") + } +#endif + } + + int Send(const char *queryText, bool singleRowMode) { TRACE("js::Send") int rv = PQsendQuery(connection_, queryText); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendQueryParams(const char *command, const int nParams, const char * const *paramValues) + int SendQueryParams(const char *command, const int nParams, const char * const *paramValues, bool singleRowMode) { TRACE("js::SendQueryParams") int rv = PQsendQueryParams(connection_, command, nParams, NULL, paramValues, NULL, NULL, 0); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendPrepare(const char *name, const char *command, const int nParams) + int SendPrepare(const char *name, const char *command, const int nParams, bool singleRowMode) { TRACE("js::SendPrepare") int rv = PQsendPrepare(connection_, name, command, nParams, NULL); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues) + int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues, bool singleRowMode) { int rv = PQsendQueryPrepared(connection_, name, nParams, paramValues, NULL, NULL, 0); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } @@ -631,6 +659,7 @@ protected: ExecStatusType status = PQresultStatus(result); switch(status) { case PGRES_TUPLES_OK: + case PGRES_SINGLE_TUPLE: { EmitRowDescription(result); HandleTuplesResult(result); From 4d6482b1d57a0fcd05a02ba4f0b3d996beb3d1af Mon Sep 17 00:00:00 2001 From: rpedela Date: Thu, 22 Aug 2013 13:26:09 -0600 Subject: [PATCH 3/5] Ignore gedit backup files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 81d95c88..44602419 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ *.log .lock-wscript build/ +*~ From cd4565ba1f88cf9f1d98dece1f29df2a467d8064 Mon Sep 17 00:00:00 2001 From: rpedela Date: Thu, 22 Aug 2013 13:28:48 -0600 Subject: [PATCH 4/5] #181 #366 Add support for single row mode in Postgres 9.2+. This will enable single row mode only when the user wants to stream rows. --- lib/native/index.js | 8 ++++---- lib/native/query.js | 5 +++++ src/binding.cc | 45 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/native/index.js b/lib/native/index.js index efb37bfa..cb6c38f2 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -130,18 +130,18 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) { this._activeQuery = query; if(query.name) { if(this._namedQueries[query.name]) { - this._sendQueryPrepared(query.name, query.values||[]); + this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode); } else { this._namedQuery = true; this._namedQueries[query.name] = true; - this._sendPrepare(query.name, query.text, (query.values||[]).length); + this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode); } } else if(query.values) { //call native function - this._sendQueryWithParams(query.text, query.values); + this._sendQueryWithParams(query.text, query.values, query.singleRowMode); } else { //call native function - this._sendQuery(query.text); + this._sendQuery(query.text, query.singleRowMode); } }; diff --git a/lib/native/query.js b/lib/native/query.js index 905d1f78..d2b06b8e 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -20,6 +20,11 @@ var NativeQuery = function(config, values, callback) { this.text = c.text; this.values = c.values; this.callback = c.callback; + this.singleRowMode = false; + + if(!this.callback) { + this.singleRowMode = true; + } this._result = new Result(config.rowMode); this._addedFields = false; diff --git a/src/binding.cc b/src/binding.cc index a9a7943f..2333e1f7 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -13,6 +13,10 @@ #define ESCAPE_SUPPORTED #endif +#if PG_VERSION_NUM >= 90200 +#define SINGLE_ROW_SUPPORTED +#endif + #define THROW(msg) return ThrowException(Exception::Error(String::New(msg))); using namespace v8; @@ -204,7 +208,9 @@ public: } char* queryText = MallocCString(args[0]); - int result = self->Send(queryText); + bool singleRowMode = (bool)args[1]->Int32Value(); + + int result = self->Send(queryText, singleRowMode); free(queryText); if(result == 0) { lastErrorMessage = self->GetLastError(); @@ -234,7 +240,8 @@ public: String::Utf8Value queryName(args[0]); String::Utf8Value queryText(args[1]); int length = args[2]->Int32Value(); - self->SendPrepare(*queryName, *queryText, length); + bool singleRowMode = (bool)args[3]->Int32Value(); + self->SendPrepare(*queryName, *queryText, length, singleRowMode); return Undefined(); } @@ -274,12 +281,13 @@ public: } char* queryText = MallocCString(args[0]); + bool singleRowMode = (bool)args[2]->Int32Value(); int result = 0; if(isPrepared) { - result = self->SendPreparedQuery(queryText, len, paramValues); + result = self->SendPreparedQuery(queryText, len, paramValues, singleRowMode); } else { - result = self->SendQueryParams(queryText, len, paramValues); + result = self->SendQueryParams(queryText, len, paramValues, singleRowMode); } free(queryText); @@ -383,33 +391,53 @@ protected: } #endif - int Send(const char *queryText) + void enableSingleRowMode(bool enable) + { +#ifdef SINGLE_ROW_SUPPORTED + if(enable == true) { + int mode = PQsetSingleRowMode(connection_); + if(mode == 1) { + TRACE("PQsetSingleRowMode enabled") + } else { + TRACE("PQsetSingleRowMode disabled") + } + } else { + TRACE("PQsetSingleRowMode disabled") + } +#endif + } + + int Send(const char *queryText, bool singleRowMode) { TRACE("js::Send") int rv = PQsendQuery(connection_, queryText); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendQueryParams(const char *command, const int nParams, const char * const *paramValues) + int SendQueryParams(const char *command, const int nParams, const char * const *paramValues, bool singleRowMode) { TRACE("js::SendQueryParams") int rv = PQsendQueryParams(connection_, command, nParams, NULL, paramValues, NULL, NULL, 0); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendPrepare(const char *name, const char *command, const int nParams) + int SendPrepare(const char *name, const char *command, const int nParams, bool singleRowMode) { TRACE("js::SendPrepare") int rv = PQsendPrepare(connection_, name, command, nParams, NULL); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } - int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues) + int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues, bool singleRowMode) { int rv = PQsendQueryPrepared(connection_, name, nParams, paramValues, NULL, NULL, 0); + enableSingleRowMode(singleRowMode); StartWrite(); return rv; } @@ -631,6 +659,7 @@ protected: ExecStatusType status = PQresultStatus(result); switch(status) { case PGRES_TUPLES_OK: + case PGRES_SINGLE_TUPLE: { EmitRowDescription(result); HandleTuplesResult(result); From b0a4c655674beab890cad5438fd059b2fed0d8db Mon Sep 17 00:00:00 2001 From: rpedela Date: Wed, 4 Sep 2013 11:50:52 -0600 Subject: [PATCH 5/5] Surround PGRES_SINGLE_TUPLE with an #ifdef for single row mode support. --- src/binding.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 2333e1f7..0d397220 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -659,7 +659,9 @@ protected: ExecStatusType status = PQresultStatus(result); switch(status) { case PGRES_TUPLES_OK: +#ifdef SINGLE_ROW_SUPPORTED case PGRES_SINGLE_TUPLE: +#endif { EmitRowDescription(result); HandleTuplesResult(result);