Skip to content

Commit 4fcfc66

Browse files
committed
Merge pull request brianc#423 from rpedela/master
Add support for single row mode
2 parents 39e0483 + b0a4c65 commit 4fcfc66

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
*.log
44
.lock-wscript
55
build/
6+
*~

lib/native/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,18 @@ Connection.prototype._pulseQueryQueue = function(initialConnection) {
130130
this._activeQuery = query;
131131
if(query.name) {
132132
if(this._namedQueries[query.name]) {
133-
this._sendQueryPrepared(query.name, query.values||[]);
133+
this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode);
134134
} else {
135135
this._namedQuery = true;
136136
this._namedQueries[query.name] = true;
137-
this._sendPrepare(query.name, query.text, (query.values||[]).length);
137+
this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode);
138138
}
139139
} else if(query.values) {
140140
//call native function
141-
this._sendQueryWithParams(query.text, query.values);
141+
this._sendQueryWithParams(query.text, query.values, query.singleRowMode);
142142
} else {
143143
//call native function
144-
this._sendQuery(query.text);
144+
this._sendQuery(query.text, query.singleRowMode);
145145
}
146146
};
147147

lib/native/query.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ var NativeQuery = function(config, values, callback) {
2020
this.text = c.text;
2121
this.values = c.values;
2222
this.callback = c.callback;
23+
this.singleRowMode = false;
24+
25+
if(!this.callback) {
26+
this.singleRowMode = true;
27+
}
2328

2429
this._result = new Result(config.rowMode);
2530
this._addedFields = false;

src/binding.cc

+39-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#define ESCAPE_SUPPORTED
1414
#endif
1515

16+
#if PG_VERSION_NUM >= 90200
17+
#define SINGLE_ROW_SUPPORTED
18+
#endif
19+
1620
#define THROW(msg) return ThrowException(Exception::Error(String::New(msg)));
1721

1822
using namespace v8;
@@ -204,7 +208,9 @@ class Connection : public ObjectWrap {
204208
}
205209

206210
char* queryText = MallocCString(args[0]);
207-
int result = self->Send(queryText);
211+
bool singleRowMode = (bool)args[1]->Int32Value();
212+
213+
int result = self->Send(queryText, singleRowMode);
208214
free(queryText);
209215
if(result == 0) {
210216
lastErrorMessage = self->GetLastError();
@@ -234,7 +240,8 @@ class Connection : public ObjectWrap {
234240
String::Utf8Value queryName(args[0]);
235241
String::Utf8Value queryText(args[1]);
236242
int length = args[2]->Int32Value();
237-
self->SendPrepare(*queryName, *queryText, length);
243+
bool singleRowMode = (bool)args[3]->Int32Value();
244+
self->SendPrepare(*queryName, *queryText, length, singleRowMode);
238245

239246
return Undefined();
240247
}
@@ -274,12 +281,13 @@ class Connection : public ObjectWrap {
274281
}
275282

276283
char* queryText = MallocCString(args[0]);
284+
bool singleRowMode = (bool)args[2]->Int32Value();
277285

278286
int result = 0;
279287
if(isPrepared) {
280-
result = self->SendPreparedQuery(queryText, len, paramValues);
288+
result = self->SendPreparedQuery(queryText, len, paramValues, singleRowMode);
281289
} else {
282-
result = self->SendQueryParams(queryText, len, paramValues);
290+
result = self->SendQueryParams(queryText, len, paramValues, singleRowMode);
283291
}
284292

285293
free(queryText);
@@ -383,33 +391,53 @@ class Connection : public ObjectWrap {
383391
}
384392
#endif
385393

386-
int Send(const char *queryText)
394+
void enableSingleRowMode(bool enable)
395+
{
396+
#ifdef SINGLE_ROW_SUPPORTED
397+
if(enable == true) {
398+
int mode = PQsetSingleRowMode(connection_);
399+
if(mode == 1) {
400+
TRACE("PQsetSingleRowMode enabled")
401+
} else {
402+
TRACE("PQsetSingleRowMode disabled")
403+
}
404+
} else {
405+
TRACE("PQsetSingleRowMode disabled")
406+
}
407+
#endif
408+
}
409+
410+
int Send(const char *queryText, bool singleRowMode)
387411
{
388412
TRACE("js::Send")
389413
int rv = PQsendQuery(connection_, queryText);
414+
enableSingleRowMode(singleRowMode);
390415
StartWrite();
391416
return rv;
392417
}
393418

394-
int SendQueryParams(const char *command, const int nParams, const char * const *paramValues)
419+
int SendQueryParams(const char *command, const int nParams, const char * const *paramValues, bool singleRowMode)
395420
{
396421
TRACE("js::SendQueryParams")
397422
int rv = PQsendQueryParams(connection_, command, nParams, NULL, paramValues, NULL, NULL, 0);
423+
enableSingleRowMode(singleRowMode);
398424
StartWrite();
399425
return rv;
400426
}
401427

402-
int SendPrepare(const char *name, const char *command, const int nParams)
428+
int SendPrepare(const char *name, const char *command, const int nParams, bool singleRowMode)
403429
{
404430
TRACE("js::SendPrepare")
405431
int rv = PQsendPrepare(connection_, name, command, nParams, NULL);
432+
enableSingleRowMode(singleRowMode);
406433
StartWrite();
407434
return rv;
408435
}
409436

410-
int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues)
437+
int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues, bool singleRowMode)
411438
{
412439
int rv = PQsendQueryPrepared(connection_, name, nParams, paramValues, NULL, NULL, 0);
440+
enableSingleRowMode(singleRowMode);
413441
StartWrite();
414442
return rv;
415443
}
@@ -631,6 +659,9 @@ class Connection : public ObjectWrap {
631659
ExecStatusType status = PQresultStatus(result);
632660
switch(status) {
633661
case PGRES_TUPLES_OK:
662+
#ifdef SINGLE_ROW_SUPPORTED
663+
case PGRES_SINGLE_TUPLE:
664+
#endif
634665
{
635666
EmitRowDescription(result);
636667
HandleTuplesResult(result);

0 commit comments

Comments
 (0)