Skip to content

Commit

Permalink
#403 Only use native escape functions if PG version >= 9.0.0. Otherwi…
Browse files Browse the repository at this point in the history
…se use the JS functions.
  • Loading branch information
rpedela committed Jul 29, 2013
1 parent baaacd2 commit cf07a4f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/native/index.js
Expand Up @@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter;
var ConnectionParameters = require(__dirname + '/../connection-parameters');
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
var JsClient = require(__dirname + '/../client'); // used to import JS escape functions

var binding;

Expand Down Expand Up @@ -80,6 +81,15 @@ Connection.prototype.endCopyFrom = function (msg) {
this._endCopyFrom(msg);
};

// use JS version if native version undefined
// happens when PG version < 9.0.0
if (!Connection.prototype.escapeIdentifier) {
Connection.prototype.escapeIdentifier = JsClient.prototype.escapeIdentifier;
}
if (!Connection.prototype.escapeLiteral) {
Connection.prototype.escapeLiteral = JsClient.prototype.escapeLiteral;
}

Connection.prototype.query = function(config, values, callback) {
var query = (config instanceof NativeQuery) ? config :
new NativeQuery(config, values, callback);
Expand Down
9 changes: 9 additions & 0 deletions src/binding.cc
Expand Up @@ -8,6 +8,9 @@
#define LOG(msg) printf("%s\n",msg);
#define TRACE(msg) //printf("%s\n", msg);

#if PG_VERSION_NUM > 90000
#define ESCAPE_SUPPORTED
#endif

#define THROW(msg) return ThrowException(Exception::Error(String::New(msg)));

Expand Down Expand Up @@ -67,8 +70,10 @@ class Connection : public ObjectWrap {
command_symbol = NODE_PSYMBOL("command");

NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
#ifdef ESCAPE_SUPPORTED
NODE_SET_PROTOTYPE_METHOD(t, "escapeIdentifier", EscapeIdentifier);
NODE_SET_PROTOTYPE_METHOD(t, "escapeLiteral", EscapeLiteral);
#endif
NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery);
NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams);
NODE_SET_PROTOTYPE_METHOD(t, "_sendPrepare", SendPrepare);
Expand Down Expand Up @@ -132,6 +137,7 @@ class Connection : public ObjectWrap {
return Undefined();
}

#ifdef ESCAPE_SUPPORTED
//v8 entry point into Connection#escapeIdentifier
static Handle<Value>
EscapeIdentifier(const Arguments& args)
Expand Down Expand Up @@ -183,6 +189,7 @@ class Connection : public ObjectWrap {

return scope.Close(jsStr);
}
#endif

//v8 entry point into Connection#_sendQuery
static Handle<Value>
Expand Down Expand Up @@ -361,6 +368,7 @@ class Connection : public ObjectWrap {
return args.This();
}

#ifdef ESCAPE_SUPPORTED
char * EscapeIdentifier(const char *str)
{
TRACE("js::EscapeIdentifier")
Expand All @@ -372,6 +380,7 @@ class Connection : public ObjectWrap {
TRACE("js::EscapeLiteral")
return PQescapeLiteral(connection_, str, strlen(str));
}
#endif

int Send(const char *queryText)
{
Expand Down

0 comments on commit cf07a4f

Please sign in to comment.